ProductInstructionAction.java

package com.mycim.webapp.actions.product;

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.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.ContextNames;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.wip.Lot;
import com.mycim.webapp.actions.PrpSetupAction;
import com.mycim.webapp.forms.ProductInstructionForm;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 产品流程提示(备注)定义
 *
 * @author sandy
 * @version 6.0.0
 * @date 2019/9/10
 **/
public class ProductInstructionAction extends PrpSetupAction {

    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e'
            , 'f'};

    public static byte[] toBytes(String str) {
        if (str == null || str.trim().equals("")) {
            return new byte[0];
        }

        byte[] bytes = new byte[str.length() / 2];
        for (int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }

        return bytes;
    }

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {

        return mapping.findForward("init");
    }

    public ActionForward addmember(ActionMapping mapping, ActionForm form,
                                   HttpServletRequest request) throws Exception {

        ProductInstructionForm theform = (ProductInstructionForm) form;

        if (StringUtils.isNotBlank((String) request.getAttribute("target"))) {
            theform.setCacheTarget((String) request.getAttribute("target"));
        }
        Map cachePageInfo = (Map) getCacheString2Obj(theform.getCachePageInfo());
        if (cachePageInfo == null) {
            cachePageInfo = new HashMap();
        }
        String productId = theform.getInstanceId();
        long productRrn = this
                .getInstanceRrn(productId, this.getNamedSpace("PRODUCT", LocalContext.getFacilityRrn()), "PRODUCT");
        Long processRrn = theform.getProcessRrn();
        String routeId = theform.getProcessStepIdVersion();
        Assert.isFalse((routeId == null) || ("".equalsIgnoreCase(routeId)),
                       Errors.create().key(MessageIdList.ROUTE_EMPTY_ID).content("route id empty").build());
        String operationId = theform.getOperationId();
        Assert.isFalse((operationId == null) || ("".equalsIgnoreCase(operationId)),
                       Errors.create().key(MessageIdList.OPERATION_INVALID_ID).content("Invalid step id!").build());

        Map instructionMap = new HashMap();
        instructionMap.put("context_id", ContextNames.CTX_INSTRUCTION);
        instructionMap.put("context_rrn", getInstanceRrn(ContextNames.CTX_INSTRUCTION,
                                                         getNamedSpace(ObjectList.CONTEXT_KEY,
                                                                       LocalContext.getFacilityRrn()),
                                                         ObjectList.CONTEXT_KEY) + "");

        long routeRrn = this
                .getInstanceRrn(routeId, this.getNamedSpace("WFL", LocalContext.getFacilityRrn()), "WFL", "ROUTE");

        long operationRrn = this
                .getInstanceRrn(operationId, this.getNamedSpace("OPERATION", LocalContext.getFacilityRrn()),
                                "OPERATION");
        instructionMap.put("context_key1", String.valueOf(productRrn));
        instructionMap.put("context_key2", processRrn.toString());
        instructionMap.put("context_key3", String.valueOf(routeRrn));
        instructionMap.put("context_key4", String.valueOf(operationRrn));
        instructionMap.put("context_key7", theform.getActionPoint());
        long refRrn = this
                .getInstanceRrn("CTX_INSTRUCTION", this.getNamedSpace("CONTEXT", LocalContext.getFacilityRrn()),
                                "CONTEXT");
        int sequenceNumber = prpService.getMaxSeq(refRrn);
        instructionMap.put("sequence", String.valueOf(sequenceNumber));
        instructionMap.put("result_value1", theform.getReason());
        instructionMap.put("result_value2", LocalContext.getUserId());
        String isVerifyRemark = StringUtils.isNotEmpty(theform.getIsVerifyRemark()) ? theform.getIsVerifyRemark() : "N";
        instructionMap.put("result_value3", isVerifyRemark);
        prpService.addContextInstruction(instructionMap);
        Collection instructionListbyProduct = new ArrayList();
        instructionListbyProduct = prpService.getContextInstructionByProduct(refRrn, productRrn);

        // session.setAttribute(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);
        request.setAttribute(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);
        cachePageInfo.put(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);
        request.setAttribute("productRrn", productRrn);
        request.setAttribute("processRrn", processRrn);

        theform.setDepartment("");
        theform.setDeptExt("");
        theform.setReason("");

        request.setAttribute(SessionNames.FUTUREHOLDFLAGTYPE_KEY, theform.getByPlanLotFlag());
        request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, getCacheString2Obj(theform.getCacheLotInfo()));
        Lot lot = (Lot) getCacheString2Obj(theform.getCacheLot());
        if (lot != null) {
            request.setAttribute(SessionNames.LOT_KEY, lot);
            request.setAttribute("stepNumber", lot.getStepNumberInProcess());
            request.setAttribute("lotRrn", new Long(lot.getLotRrn()));
            request.setAttribute("_lotId", lot.getLotId());
            request.setAttribute("productRrn", lot.getProductRrn());
        }
        theform.setCachePageInfo(getCacheObj2String(cachePageInfo));

        return new ActionForward(mapping.getInput());

    }

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request) throws Exception {

        ProductInstructionForm theform = (ProductInstructionForm) form;

        if (StringUtils.isNotBlank((String) request.getAttribute("target"))) {
            theform.setCacheTarget((String) request.getAttribute("target"));
        }
        Map cachePageInfo = (Map) getCacheString2Obj(theform.getCachePageInfo());
        if (cachePageInfo == null) {
            cachePageInfo = new HashMap();
        }
        String seqNum = request.getParameter("seqNum");
        String productId = theform.getInstanceId();
        long refRrn = this
                .getInstanceRrn("CTX_INSTRUCTION", this.getNamedSpace("CONTEXT", LocalContext.getFacilityRrn()),
                                "CONTEXT");
        String processId = request.getParameter("processId");
        String routeId = request.getParameter("routeId");
        String operationId = request.getParameter("operationId");
        String instruction = request.getParameter("instruction");
        String actionPoint = request.getParameter("actionPoint");
        request.setAttribute("actionPoint", actionPoint);
        request.setAttribute("productId", productId);
        request.setAttribute("processId", processId);
        request.setAttribute("routeId", routeId);
        request.setAttribute("operationId", operationId);
        request.setAttribute("refRrn", new Long(refRrn));
        request.setAttribute("seqNum", seqNum);
        request.setAttribute("instruction", instruction);

        request.setAttribute(SessionNames.FUTUREHOLDFLAGTYPE_KEY, theform.getByPlanLotFlag());
        request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, getCacheString2Obj(theform.getCacheLotInfo()));
        Lot lot = (Lot) getCacheString2Obj(theform.getCacheLot());
        if (lot != null) {
            request.setAttribute(SessionNames.LOT_KEY, lot);
            request.setAttribute("stepNumber", lot.getStepNumberInProcess());
            request.setAttribute("lotRrn", new Long(lot.getLotRrn()));
            request.setAttribute("_lotId", lot.getLotId());
            request.setAttribute("productRrn", lot.getProductRrn());
        }
        theform.setCachePageInfo(getCacheObj2String(cachePageInfo));

        return (mapping.findForward("delete"));

    }

    public ActionForward doDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request) throws Exception {

        ProductInstructionForm theform = (ProductInstructionForm) form;

        if (StringUtils.isNotBlank((String) request.getAttribute("target"))) {
            theform.setCacheTarget((String) request.getAttribute("target"));
        }
        Map cachePageInfo = (Map) getCacheString2Obj(theform.getCachePageInfo());
        if (cachePageInfo == null) {
            cachePageInfo = new HashMap();
        }

        Long facilityRrn = LocalContext.getFacilityRrn();

        String refRrn = request.getParameter("refRrn");
        String productId = request.getParameter("productId");
        String seqNum = request.getParameter("seqNum");
        String actionPoint = request.getParameter("actionPoint");
        long productRrn = this.getInstanceRrn(productId, this.getNamedSpace("PRODUCT", facilityRrn), "PRODUCT");
        String processId = request.getParameter("processId");
        long processRrn = 0;
        processRrn = this.getInstanceRrn(processId, this.getNamedSpace("WFL", facilityRrn), "WFL", "ROUTE");
        String routeId = request.getParameter("routeId");
        long routeRrn = this.getInstanceRrn(routeId, this.getNamedSpace("WFL", facilityRrn), "WFL", "ROUTE");
        String operationId = request.getParameter("operationId");
        long operationRrn = this.getInstanceRrn(operationId, this.getNamedSpace("OPERATION", facilityRrn), "OPERATION");

        Map map = new HashMap();

        map.put("context_rrn", refRrn);
        map.put("seqNum", seqNum);
        map.put("context_key1", productRrn + "");
        map.put("context_key2", processRrn + "");
        map.put("context_key3", routeRrn + "");
        map.put("context_key4", operationRrn + "");
        map.put("context_key7", actionPoint);
        map.put("type", "product");
        prpService.deleteContextInstruction(map);
        Collection instructionListbyProduct = new ArrayList();
        instructionListbyProduct = prpService.getContextInstructionByProduct(Long.parseLong(refRrn), productRrn);

        request.setAttribute(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);
        cachePageInfo.put(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);

        theform.setInstanceDesc(this.getInstanceDesc(productRrn));
        theform.setNamedSpace(this.getNamedSpace("PRODUCT", facilityRrn));
        theform.setInstanceId(productId);

        theform.setProcessStepIdVersion(routeId);
        request.setAttribute("productRrn", new Long(productRrn));

        request.setAttribute(SessionNames.FUTUREHOLDFLAGTYPE_KEY, theform.getByPlanLotFlag());
        request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, getCacheString2Obj(theform.getCacheLotInfo()));
        Lot lot = (Lot) getCacheString2Obj(theform.getCacheLot());
        if (lot != null) {
            request.setAttribute(SessionNames.LOT_KEY, lot);
            request.setAttribute("stepNumber", lot.getStepNumberInProcess());
            request.setAttribute("lotRrn", new Long(lot.getLotRrn()));
            request.setAttribute("_lotId", lot.getLotId());
            request.setAttribute("productRrn", lot.getProductRrn());
        }
        theform.setCachePageInfo(getCacheObj2String(cachePageInfo));
        return new ActionForward(mapping.getInput());
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request) throws Exception {
        ProductInstructionForm theform = (ProductInstructionForm) form;
        String id = StringUtils.trimToUpperCase(theform.getInstanceId());
        if (StringUtils.isNotBlank((String) request.getAttribute("target"))) {
            theform.setCacheTarget((String) request.getAttribute("target"));
        }
        Map cachePageInfo = (Map) getCacheString2Obj(theform.getCachePageInfo());
        if (cachePageInfo == null) {
            cachePageInfo = new HashMap();
        }

        long productRrn = this
                .getInstanceRrn(id, this.getNamedSpace("PRODUCT", LocalContext.getFacilityRrn()), "PRODUCT");
        Assert.isFalse(productRrn == 0,
                       Errors.create().key(MessageIdList.PRODUCT_PRODUCT_MISSING).content("product not find").build());

        request.setAttribute("productRrn", new Long(productRrn));
        cachePageInfo.put("productRrn", new Long(productRrn));

        theform.setInstanceDesc(this.getInstanceDesc(productRrn));
        theform.setNamedSpace(this.getNamedSpace("PRODUCT", LocalContext.getFacilityRrn()));
        long refRrn = this
                .getInstanceRrn("CTX_INSTRUCTION", this.getNamedSpace("CONTEXT", LocalContext.getFacilityRrn()),
                                "CONTEXT");
        Collection instructionListbyProduct = new ArrayList();
        instructionListbyProduct = prpService.getContextInstructionByProduct(refRrn, productRrn);

        request.setAttribute(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);
        cachePageInfo.put(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);

        theform.setInstanceId(id);

        request.setAttribute(SessionNames.FUTUREHOLDFLAGTYPE_KEY, theform.getByPlanLotFlag());
        cachePageInfo.put(SessionNames.FUTUREHOLDFLAGTYPE_KEY, theform.getByPlanLotFlag());
        request.setAttribute(SessionNames.FUTUREHOLDFLAGTYPE_KEY, theform.getByPlanLotFlag());
        request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, getCacheString2Obj(theform.getCacheLotInfo()));
        Lot lot = (Lot) getCacheString2Obj(theform.getCacheLot());
        if (lot != null) {
            request.setAttribute(SessionNames.LOT_KEY, lot);
            request.setAttribute("stepNumber", lot.getStepNumberInProcess());
            request.setAttribute("lotRrn", new Long(lot.getLotRrn()));
            request.setAttribute("_lotId", lot.getLotId());
            request.setAttribute("productRrn", lot.getProductRrn());
        }
        theform.setCachePageInfo(getCacheObj2String(cachePageInfo));
        return new ActionForward(mapping.getInput());
    }

    public Object getCacheString2Obj(String pageDataString) throws Exception {
        if (pageDataString != null && !"".equals(pageDataString)) {
            String string = URLDecoder.decode(pageDataString);

            BufferedInputStream bff = new BufferedInputStream(new ByteArrayInputStream(toBytes(string)));
            ObjectInputStream objectInputStream;

            objectInputStream = new ObjectInputStream(bff);
            return objectInputStream.readObject();

        }

        return null;
    }

    public String getCacheObj2String(Object obj) throws Exception {
        if (obj == null) {
            return "";
        }
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(byteStream);
        oo.writeObject(obj);

        String string = bytesToHexFun2(byteStream.toByteArray());
        return string;
    }

    public String bytesToHexFun2(byte[] bytes) {
        char[] buf = new char[bytes.length * 2];
        int index = 0;
        for (byte b : bytes) { // 利用位运算进行转换,可以看作方法一的变种
            buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
            buf[index++] = HEX_CHAR[b & 0xf];
        }

        return new String(buf);
    }


}