# ReticleFamilyManagerImpl.java

    package com.mycim.server.reticle.manager.imp;

    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.base.manager.RelationManager;
    import com.mycim.server.base.manager.TransactionLogManager;
    import com.mycim.server.reticle.dao.ReticleFamilyDAO;
    import com.mycim.server.reticle.manager.ReticleFamilyManager;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.bas.NamedObject;
    import com.mycim.valueobject.bas.ReticleFamilyRelation;
    import com.mycim.valueobject.bas.TransactionLog;
    import com.mycim.valueobject.consts.DataBaseNames;
    import com.mycim.valueobject.consts.TransactionNames;
    import com.mycim.valueobject.ems.ReticleFamily;
    import com.mycim.valueobject.prp.ProductVariableEnum;
    import com.mycim.valueobject.table.RelationHistoryTable;
    import com.mycim.valueobject.table.RelationTable;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    @Service
    @Transactional
    public class ReticleFamilyManagerImpl implements ReticleFamilyManager {

        @Autowired
        NamedObjectManager namedObjectManager;

        @Autowired
        TransactionLogManager transactionLogManager;

        @Autowired
        ReticleFamilyDAO reticleFamilyDAO;

        @Autowired
        RelationManager relationManager;

        @Override
        public ReticleFamily getReticleFamily(ReticleFamily reticleFamily) {
            // get the rrn
            NamedObject namedObject = namedObjectManager.getNamedObject(reticleFamily);
            if (namedObject == null) {
                return null;
            }
            reticleFamily.copyNamedObject(namedObject);
            return reticleFamily;
        }

        @Override
        public String getReticleFamilyStatus(String id) {
            return reticleFamilyDAO.getReticleFamilyStatus(id);
        }

        @Override
        public long insertReticleFamily(ReticleFamily reticleFamily) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(reticleFamily.getTransPerformedby(), reticleFamily.getTransId());
            long instanceRrn = namedObjectManager.insertNamedObject(reticleFamily, transactionLog.getTransRrn());
            //插入新主表和新历史表中的数据和记录
            reticleFamilyDAO.insertReticleFamily(reticleFamily);
            reticleFamilyDAO.insertReticleFamilyHistory(reticleFamily, transactionLog);
            transactionLogManager.markTransactionLog(transactionLog);

            return instanceRrn;
        }

        @Override
        public void deleteReticleFamily(ReticleFamily reticleFamily) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(reticleFamily.getTransPerformedby(), reticleFamily.getTransId());
            transactionLog.setTransId(TransactionNames.DELETE_KEY);
            transactionLog.setTransPerformedBy(reticleFamily.getTransPerformedby());
            // delete the Entity group from the table named object.
            long groupRrn = namedObjectManager.deleteNamedObject(reticleFamily, transactionLog.getTransRrn());
            reticleFamilyDAO.deleteReticleFamily(groupRrn);
            reticleFamily.setInstanceRrn(groupRrn);
            //插入新历史表中的删除记录 和 删除新表中的数据
            reticleFamilyDAO.insertReticleFamilyHistory(reticleFamily, transactionLog);
            reticleFamilyDAO.deleteReticleFamily(reticleFamily);
            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public void updateReticleFamily(ReticleFamily reticleFamily) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(reticleFamily.getTransPerformedby(), reticleFamily.getTransId());
            transactionLog.setTransId(TransactionNames.MODIFY_KEY);
            namedObjectManager.updateNamedObject(reticleFamily, transactionLog.getTransRrn());
            //修改新表中的数据 和 插入新历史表中的修改记录
            reticleFamily.setLastUpdateTimestamp(transactionLog.getTransStartTimestamp());
            reticleFamilyDAO.updateReticleFamily(reticleFamily);
            reticleFamilyDAO.insertReticleFamilyHistory(reticleFamily, transactionLog);
            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public long isProductLay4ReticleFamily(long reticleFamilyRrn) {
            return reticleFamilyDAO.isProductLay4ReticleFamily(reticleFamilyRrn);
        }

        @Override
        public long isProductLay(long entityRrn) {
            return reticleFamilyDAO.isProductLay(entityRrn);
        }

        @Override
        public void insertReticleFamilyRelation(ReticleFamilyRelation relation) {
            if (relationManager.getRelation(relation.getFromRrn(), relation.getToRrn(), relation.getLinkType()) == null) {
                TransactionLog transactionLog = transactionLogManager
                        .startTransactionLog(relation.getTransPerformedby(), relation.getTransId().toUpperCase());
                //执行原Relation的操作
                relationManager.insertRelation(relation, transactionLog.getTransRrn());
                //插入新拆出的表的记录
                relationManager.insertSpecificRelationTable(
                        new RelationTable(DataBaseNames.REL_RETICLE_RETICLEFAMILY, relation, transactionLog));
                relationManager.insertSpecificRelationHistoryTable(
                        new RelationHistoryTable(DataBaseNames.REL_RETICLE_RETICLEFAMILY_H,
                                                 DataBaseNames.REL_RETICLE_RETICLEFAMILY, relation, transactionLog));
                transactionLogManager.markTransactionLog(transactionLog);
            }
        }

        @Override
        public void deleteReticleFamilyRelation(ReticleFamilyRelation relation) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(relation.getTransPerformedby(), relation.getTransId().toUpperCase());
            //先执行原 Relation 表的操作
            relationManager.deleteRelation(relation, transactionLog.getTransRrn());
            relationManager.deleteSpecificRelationTable(relation, DataBaseNames.REL_RETICLE_RETICLEFAMILY, DataBaseNames.REL_RETICLE_RETICLEFAMILY_H, transactionLog);
            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public String checkReticleFamilyAndCreate(String reticleFamilyId) {
            boolean availableFlag = ProductVariableEnum.isUnAvailableFlag(reticleFamilyId);
            if (availableFlag) {
                return "Reticle Group ID: " + reticleFamilyId + " cannot contain spaces, quotes or commas!<br>";
            }
            long facilityRrn = LocalContext.getFacilityRrn();
            ReticleFamily reticleFamily = new ReticleFamily(reticleFamilyId, namedObjectManager
                    .getNamedSpace(facilityRrn, ObjectList.RETICLEFAMILY_KEY), ObjectList.RETICLEFAMILY_KEY);
            reticleFamily.setObjectType(ObjectList.RETICLEFAMILY_KEY);
            reticleFamily.setFacilityRrn(facilityRrn);
            reticleFamily.setCreatedUserRrn(LocalContext.getUserRrn());
            reticleFamily.setLastUpdateUserRrn(LocalContext.getUserRrn());
            reticleFamily.setTransId(TransactionNames.CREATE_KEY);
            reticleFamily.setTransPerformedby(LocalContext.getUserId());

            insertReticleFamily(reticleFamily);
            return StringUtils.EMPTY;
        }

    }