SubTechnologySaveAction.java

package com.mycim.webapp.actions.technology;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.beans.PropertyUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
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.consts.TransactionNames;
import com.mycim.valueobject.prp.Item;
import com.mycim.valueobject.prp.SubTechnology;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.PrpSetupAction;
import com.mycim.webapp.forms.SubTechnologyFrom;
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.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author liuji.li
 * @version 6.0.0
 * @date 2019/10/28
 **/
public class SubTechnologySaveAction extends PrpSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        SubTechnologyFrom theForm = (SubTechnologyFrom) form;
        theForm.setInstanceId(StringUtils.trim(theForm.getInstanceId()));
        if (StringUtils.isEmpty(theForm.getInstanceId())) {
            theForm.setTransId(Constants.CREATE_KEY);
            return new ActionForward(mapping.getInputForward());
        }
        SubTechnology subTechnology = getSubTechnology(theForm);
        PropertyUtils.copyProperties(theForm, subTechnology);
        if (subTechnology.getInstanceRrn() == 0) {
            theForm.setTransId(Constants.CREATE_KEY);
        } else {
            theForm.setCacheProducts(WebUtils.getCacheObj2String(theForm.getProducts()));
            theForm.setTransId(Constants.MODIFY_KEY);
            request.setAttribute("products", theForm.getProducts());
            Assert.isTrue(StringUtils.isEqual(subTechnology.getObject(), ObjectList.SUB_TECHNOLOGY),
                          Errors.create().key(MessageIdList.SUBTECHNOLOGY_NOT_ID).content("It is not subTechnology!")
                                .build());
        }

        return mapping.getInputForward();
    }

    public ActionForward createOrModify(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                        HttpServletResponse response) {
        String user = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();
        SubTechnologyFrom theForm = (SubTechnologyFrom) form;
        theForm.setInstanceId(StringUtils.trim(theForm.getInstanceId()));
        Assert.isFalse(StringUtils.isEmpty(theForm.getInstanceId()),
                       Errors.create().key(MessageIdList.SUBTECHNOLOGY_WRITE_ID)
                             .content("Please write subTechnology Id!").build());
        SubTechnology subTechnology = new SubTechnology(theForm.getInstanceId(),
                                                        getNamedSpace(ObjectList.SUB_TECHNOLOGY, facilityRrn),
                                                        ObjectList.SUB_TECHNOLOGY);
        PropertyUtils.copyProperties(subTechnology, theForm);
        subTechnology.setInstanceRrn(
                getInstanceRrn(theForm.getInstanceId(), getNamedSpace(ObjectList.SUB_TECHNOLOGY, facilityRrn),
                               ObjectList.SUB_TECHNOLOGY));
        subTechnology.setTransPerformedby(user);
        SubTechnology newSubTechnology = prpService.createOrModifySubTechnology(subTechnology);

        Relation relation = null;
        if (StringUtils.isNotBlank(theForm.getTechnologyId())) {
            long technologyRrn = getInstanceRrn(theForm.getTechnologyId(), facilityRrn, ObjectList.TECHNOLOGY);
            Assert.isFalse(technologyRrn <= 0,
                           Errors.create().key(MessageIdList.TECHNOLOGY_NOT_ID).content("It is not technology Id!")
                                 .build());
            relation = new Relation(technologyRrn, newSubTechnology.getInstanceRrn(),
                                    LinkTypeList.TECHNOLOGY_SUBTECHNOLOGY_KEY);
            relation.setTransPerformedby(LocalContext.getUserId());
            if (theForm.getTechnologyRrn() == null || theForm.getTechnologyRrn() <= 0) {
                relation.setTransId(TransactionNames.CREATE_KEY);
                baseService.insertRelation(relation);
            } else if (technologyRrn != theForm.getTechnologyRrn()) {
                relation.setTransId(TransactionNames.MODIFY_KEY);
                baseService.updateRelationFromRrn(relation);
            }
            theForm.setTechnologyRrn(relation.getFromRrn());
        } else if (theForm.getTechnologyRrn() != null && theForm.getTechnologyRrn() > 0) {
            relation = new Relation(theForm.getTechnologyRrn(), subTechnology.getInstanceRrn(),
                                    LinkTypeList.TECHNOLOGY_SUBTECHNOLOGY_KEY);
            relation.setTransPerformedby(LocalContext.getUserId());
            relation.setTransId(TransactionNames.DELETE_KEY);
            baseService.deleteRelation(relation);
            theForm.setTechnologyRrn(null);
        }
        theForm.setTransId(Constants.MODIFY_KEY);
        if (StringUtils.equals(newSubTechnology.getTransId(), TransactionNames.MODIFY_KEY)) {
            request.setAttribute("products", (List<Item>) WebUtils.getCacheString2Obj(theForm.getCacheProducts()));
        } else {
            theForm.setCacheProducts(WebUtils.getCacheObj2String(new ArrayList<Item>()));
        }
        theForm.setProductId("");
        WebUtils.setSuccessMsg(request);
        return mapping.getInputForward();
    }

    public ActionForward addProduct(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                    HttpServletResponse response) {
        long facilityRrn = LocalContext.getFacilityRrn();
        SubTechnologyFrom theForm = (SubTechnologyFrom) form;
        String productId = StringUtils.trimToUpperCase(theForm.getProductId());
        long subTechnologyRrn = getInstanceRrn(theForm.getInstanceId(), facilityRrn, ObjectList.SUB_TECHNOLOGY);
        long productRrn = getInstanceRrn(productId, facilityRrn, ObjectList.PRODUCT_KEY);
        Assert.isFalse(productRrn <= 0 || StringUtils.isEmpty(productId),
                       Errors.create().key(MessageIdList.SUBTECHNOLOGY_ENTER_CORRECT)
                             .content("Please enter the " + "correct product Id!").build());
        List<Item> products = (List<Item>) WebUtils.getCacheString2Obj(theForm.getCacheProducts());
        if (CollectionUtils.isEmpty(products)) {
            products = new ArrayList<>();
        } else {
            for (Item item : products) {
                Assert.isFalse(productRrn == item.getInstanceRrn(),
                               Errors.create().key(MessageIdList.SUBTECHNOLOGY_NOT_SUBMISSION)
                                     .content("Please do not repeat the " + "submission!").build());
            }
        }
        Relation relation = new Relation(0L, productRrn, LinkTypeList.SUBTECHNOLOGY_PRODUCT_KEY);
        relation = baseService.getRelation(relation);
        Assert.isTrue(relation == null, Errors.create().key(MessageIdList.SUBTECHNOLOGY_ONE_PRODUCT)
                                              .content("This product has been bound with other subTechnology Id!")
                                              .build());
        relation = new Relation(subTechnologyRrn, productRrn, LinkTypeList.SUBTECHNOLOGY_PRODUCT_KEY);
        relation.setTransPerformedby(LocalContext.getUserId());
        relation.setTransId(TransactionNames.CREATE_KEY);
        relation.setSequenceNumber(1);
        baseService.insertRelation(relation);
        Item item = new Item(theForm.getProductId(), getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn),
                             ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);
        products.add(item);
        request.setAttribute("products", products);
        theForm.setCacheProducts(WebUtils.getCacheObj2String(products));
        theForm.setProductId("");
        WebUtils.setSuccessMsg(request);
        return mapping.getInputForward();
    }

    public ActionForward deleteProduct(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                       HttpServletResponse response) {
        long facilityRrn = LocalContext.getFacilityRrn();
        SubTechnologyFrom theForm = (SubTechnologyFrom) form;
        long productRrn = WebUtils.getParameterLong("productRrn", request);
        long subTechnologyRrn = getInstanceRrn(theForm.getInstanceId(), facilityRrn, ObjectList.SUB_TECHNOLOGY);

        Relation relation = new Relation(subTechnologyRrn, productRrn, LinkTypeList.SUBTECHNOLOGY_PRODUCT_KEY);
        relation.setTransPerformedby(LocalContext.getUserId());
        relation.setTransId(TransactionNames.DELETE_KEY);
        baseService.deleteRelation(relation);

        List<Item> products = (List<Item>) WebUtils.getCacheString2Obj(theForm.getCacheProducts());
        Iterator<Item> iterator = products.iterator();
        while (iterator.hasNext()) {
            Item item = iterator.next();
            if (productRrn == item.getInstanceRrn()) {
                iterator.remove();
            }
        }
        request.setAttribute("products", products);
        theForm.setCacheProducts(WebUtils.getCacheObj2String(products));
        WebUtils.setSuccessMsg(request);
        return mapping.getInputForward();
    }

    public ActionForward deleteSubTechnology(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                             HttpServletResponse response) {
        String user = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();
        SubTechnologyFrom theForm = (SubTechnologyFrom) form;
        theForm.setInstanceId(StringUtils.trim(theForm.getInstanceId()));
        List<Item> products = (List<Item>) WebUtils.getCacheString2Obj(theForm.getCacheProducts());
        Assert.isFalse(CollectionUtils.isNotEmpty(products), Errors.create().key(MessageIdList.SUBTECHNOLOGY_NOT_DELETE)
                                                                   .content(
                                                                           "The subTechnology has been bound to" + " " +
                                                                                   "the product and cannot be " +
                                                                                   "deleted!").build());

        SubTechnology subTechnology = new SubTechnology(theForm.getInstanceId(),
                                                        getNamedSpace(ObjectList.SUB_TECHNOLOGY, facilityRrn),
                                                        ObjectList.SUB_TECHNOLOGY);
        PropertyUtils.copyProperties(subTechnology, theForm);
        subTechnology.setInstanceRrn(
                getInstanceRrn(theForm.getInstanceId(), getNamedSpace(ObjectList.SUB_TECHNOLOGY, facilityRrn),
                               ObjectList.SUB_TECHNOLOGY));
        subTechnology.setTransPerformedby(user);
        subTechnology.setTransId(TransactionNames.DELETE_KEY);
        if (theForm.getTechnologyRrn() != null && theForm.getTechnologyRrn() > 0) {
            Relation relation = new Relation(theForm.getTechnologyRrn(), subTechnology.getInstanceRrn(),
                                             LinkTypeList.TECHNOLOGY_SUBTECHNOLOGY_KEY);
            relation.setTransPerformedby(LocalContext.getUserId());
            relation.setTransId(TransactionNames.DELETE_KEY);
            baseService.deleteRelation(relation);
            theForm.setTechnologyRrn(null);
        }
        prpService.deleteSubTechnology(subTechnology);

        theForm.setTransId(Constants.CREATE_KEY);
        theForm.setInstanceId("");
        theForm.setInstanceDesc("");
        WebUtils.setSuccessMsg(request);
        return mapping.getInputForward();
    }

    private SubTechnology getSubTechnology(SubTechnologyFrom theForm) {
        SubTechnology subTechnology = new SubTechnology(theForm.getInstanceId(),
                                                        getNamedSpace(ObjectList.SUB_TECHNOLOGY,
                                                                      LocalContext.getFacilityRrn()),
                                                        ObjectList.SUB_TECHNOLOGY);
        return prpService.getSubTechnology(subTechnology);
    }

}