TechnologyListAction.java

package com.mycim.webapp.actions.equipment;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.Relation;
import com.mycim.valueobject.consts.LinkTypeList;
import com.mycim.valueobject.prp.EqpMonitorEntityRelation;
import com.mycim.valueobject.prp.ProcessPlanning;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.EmsSetupAction;
import com.mycim.webapp.forms.EntityInfoForm;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class TechnologyListAction extends EmsSetupAction {
    private static final String EQUIPMENT_FLAG = "equipmentReadOnlyFlag";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws ServletException {
        String user = LocalContext.getUserId();
        Long facilityRrn = LocalContext.getFacilityRrn();

        EntityInfoForm theform = (EntityInfoForm) form;

        long entityRrn = 0;
        String entityId = theform.getInstanceId();
        /*Entity theEntity = new Entity(entityId, getNamedSpace(ObjectList.ENTITY_KEY, facilityRrn),
                                      ObjectList.ENTITY_KEY);*/

        entityRrn = baseService.getNamedObjectRrn(entityId, this.getNamedSpace(ObjectList.ENTITY_KEY, facilityRrn),
                                                  ObjectList.ENTITY_KEY);
        String linkType = "", flowType = request.getParameter("flowType");
        if (StringUtils.isNotBlank(request.getParameter("flowType"))) {
            if (StringUtils.equalsIgnoreCase(flowType, "dummy")) {
                linkType = LinkTypeList.ENTITY_TO_DUMMYPROCESS;
                theform.setObjectType("dummy");
            }
            if (StringUtils.equalsIgnoreCase(flowType, "monitor")) {
                linkType = LinkTypeList.ENTITY_TO_MONITORPROCESS;
                theform.setObjectType("monitor");
            }
        }
        if (request.getParameter(Constants.CANCEL_KEY) != null) {
            return (mapping.findForward("setup"));
        } else if (request.getParameter(Constants.MEMBERS_KEY) != null) {
            if (request.getParameter(Constants.MEMBERS_KEY).equals("delete")) {
                if (request.getParameter("sourceProcessRrn") != null && request.getParameter("processRrn") != null) {
                    Long sourceProcessRrn = NumberUtils.toLong(request.getParameter("sourceProcessRrn"));
                    Long processRrn = NumberUtils.toLong(request.getParameter("processRrn"));
                    EqpMonitorEntityRelation relation = new EqpMonitorEntityRelation();
                    relation.setEntityRrn(entityRrn);
                    relation.setProcessRrn(processRrn);
                    relation.setSourceProcessRrn(sourceProcessRrn);

                    dmmService.deleteDMMRelation(relation);

                } else if (request.getParameter(Constants.ITEM_KEY) != null) {
                    Long technologyRrn = new Long(request.getParameter(Constants.ITEM_KEY));
                    Relation relation = new Relation();
                    relation.setFromRrn(new Long(entityRrn));
                    relation.setToRrn(technologyRrn);
                    relation.setLinkType(linkType);
                    relation.setTransPerformedby(user);
                    relation.setTransId(Constants.DELETE_KEY);
                    baseService.deleteRelation(relation);
                }
            }
        } else if (request.getParameter("add") != null) {
            String technologyId = StringUtils.trimToUpperCase(theform.getTechnologyId());

            Assert.isFalse(StringUtils.isBlank(technologyId),
                           Errors.create().key(MessageIdList.PROCESS_EMPTY_ID).content("流程号不能为空!").build());

            long technologyRrn = getInstanceRrn(technologyId, facilityRrn, ObjectList.WFL_KEY);

            Assert.isFalse(technologyRrn == 0,
                           Errors.create().key(MessageIdList.PROCESS_PROCESS_MISSING).content("流程不存在").build());

            ProcessPlanning processPlanning = new ProcessPlanning(technologyId,
                                                                  getNamedSpace(ObjectList.WFL_KEY, facilityRrn),
                                                                  ObjectList.WFL_KEY);
            processPlanning = (ProcessPlanning) getInstance(processPlanning);

            if (StringUtils.equalsIgnoreCase(LinkTypeList.ENTITY_TO_MONITORPROCESS, linkType)) {

                String sourceProcessId = StringUtils.trimToUpperCase(theform.getSourceProcessId());
                Assert.isFalse(StringUtils.isBlank(sourceProcessId),
                               Errors.create().key(MessageIdList.PROCESS_EMPTY_ID).content("流程号不能为空!").build());
                long sourceProcessRrn = getInstanceRrn(sourceProcessId, facilityRrn, ObjectList.WFL_KEY);
                Assert.isFalse(sourceProcessRrn == 0,
                               Errors.create().key(MessageIdList.PROCESS_PROCESS_MISSING).content("流程不存在").build());
                List<EqpMonitorEntityRelation> relations = dmmService.getDMMRelationByEqptRrn(entityRrn);

                for (EqpMonitorEntityRelation relation : relations) {
                    Assert.isFalse(technologyRrn == relation.getProcessRrn().longValue() &&
                                           sourceProcessRrn == relation.getSourceProcessRrn().longValue(),
                                   Errors.create().key(MessageIdList.TECHNOLOGYLIST_RELATION_ALREADY_EXISTS)
                                         .content("关系已存在").build());
                }

                EqpMonitorEntityRelation relation = new EqpMonitorEntityRelation();
                relation.setEntityRrn(entityRrn);
                relation.setProcessRrn(technologyRrn);
                relation.setSourceProcessRrn(sourceProcessRrn);
                relation.setOperationRrn(0L);

                dmmService.insertDMMRelation(relation);

            } else {
                Collection technologys = recipeService.getRelation4Recipe(entityRrn, linkType);
                if (technologys != null && technologys.size() > 0) {
                    for (Iterator it = technologys.iterator(); it.hasNext(); ) {
                        Map technology = (Map) it.next();
                        if (technology != null) {
                            String id = (String) technology.get("recipeId");

                            Assert.isFalse(technologyId.equals(id),
                                           Errors.create().key(MessageIdList.TECHNOLOGYLIST_PROCESS_ALREADY_EXISTS)
                                                 .content("工艺流程已经存在!").build());
                        }
                    }
                }

                Relation relation = new Relation();
                relation.setFromRrn(new Long(entityRrn));
                relation.setLinkType(linkType);

                relation.setToRrn(processPlanning.getInstanceRrn());
                relation.setTransPerformedby(user);
                relation.setTransId(Constants.ADD_KEY);

                baseService.insertRelation(relation);
            }

            theform.setTechnologyId("");
        }

        Collection flowList = null;
        if (StringUtils.isNotBlank(theform.getSearchRecipeId())) {
            String searchId = StringUtils.trimToUpperCase(theform.getSearchRecipeId());
            flowList = baseService.getFullRelation4Recipe(entityRrn, linkType, searchId);
            theform.setSearchRecipeId(searchId);
        } else if (StringUtils.equalsIgnoreCase(LinkTypeList.ENTITY_TO_MONITORPROCESS, linkType)) {
            flowList = dmmService.getDMMRelationByEqptRrn(entityRrn);
        } else {
            flowList = baseService.getFullRelation4Recipe(entityRrn, linkType);
            theform.setSearchRecipeId("");
        }

        request.setAttribute("technologyList", flowList);
        theform.setTransId("modify");
        String equipmentReadOnlyFlag = WebUtils.getParameter(EQUIPMENT_FLAG, request);
        if (StringUtils.isNotBlank(equipmentReadOnlyFlag)) {
            request.setAttribute(EQUIPMENT_FLAG, "1");
        }
        return mapping.getInputForward();
    }

}