TechnologySaveAction.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.SubTechnology;
import com.mycim.valueobject.prp.Technology;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.PrpSetupAction;
import com.mycim.webapp.forms.TechnologyFrom;
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/29
 **/
public class TechnologySaveAction extends PrpSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        TechnologyFrom theForm = (TechnologyFrom) form;
        theForm.setInstanceId(StringUtils.trim(theForm.getInstanceId()));
        if (StringUtils.isEmpty(theForm.getInstanceId())) {
            theForm.setTransId(Constants.CREATE_KEY);
            return new ActionForward(mapping.getInputForward());
        }
        Technology technology = getTechnology(theForm);
        PropertyUtils.copyProperties(theForm, technology);
        if (technology.getInstanceRrn() == 0) {
            theForm.setTransId(Constants.CREATE_KEY);
        } else {
            theForm.setCacheSubTechnologys(WebUtils.getCacheObj2String(theForm.getSubTechnologys()));
            theForm.setTransId(Constants.MODIFY_KEY);
            request.setAttribute("subTechnologys", theForm.getSubTechnologys());
            Assert.isTrue(StringUtils.isEqual(technology.getObject(), ObjectList.TECHNOLOGY),
                          Errors.create().key(MessageIdList.TECHNOLOGY_NOT_ID).content("It is not technology Id!")
                                .build());
        }
        return mapping.getInputForward();
    }

    public ActionForward createOrModify(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                        HttpServletResponse response) {
        String user = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();
        TechnologyFrom theForm = (TechnologyFrom) form;
        theForm.setInstanceId(StringUtils.trim(theForm.getInstanceId()));
        Assert.isFalse(StringUtils.isEmpty(theForm.getInstanceId()),
                       Errors.create().key(MessageIdList.TECHNOLOGY_WRITE_ID).content("Please write technology Id!")
                             .build());
        Technology technology = new Technology(theForm.getInstanceId(),
                                               getNamedSpace(ObjectList.TECHNOLOGY, facilityRrn),
                                               ObjectList.TECHNOLOGY);
        PropertyUtils.copyProperties(technology, theForm);
        technology.setInstanceRrn(
                getInstanceRrn(theForm.getInstanceId(), getNamedSpace(ObjectList.TECHNOLOGY, facilityRrn),
                               ObjectList.TECHNOLOGY));
        technology.setTransPerformedby(user);
        String transId = prpService.createOrModifyTechnology(technology);
        theForm.setTransId(Constants.MODIFY_KEY);
        if (StringUtils.equals(transId, TransactionNames.MODIFY_KEY)) {
            request.setAttribute("subTechnologys",
                                 (List<SubTechnology>) WebUtils.getCacheString2Obj(theForm.getCacheSubTechnologys()));
        } else {
            theForm.setCacheSubTechnologys(WebUtils.getCacheObj2String(new ArrayList<SubTechnology>()));
        }
        theForm.setSubTechnologyId("");
        WebUtils.setSuccessMsg(request);
        return mapping.getInputForward();
    }

    public ActionForward addSubTechnology(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                          HttpServletResponse response) {
        long facilityRrn = LocalContext.getFacilityRrn();
        TechnologyFrom theForm = (TechnologyFrom) form;
        String subTechnologyId = StringUtils.trimToUpperCase(theForm.getSubTechnologyId());

        long subTechnologyRrn = getInstanceRrn(subTechnologyId, facilityRrn, ObjectList.SUB_TECHNOLOGY);
        long technologyRrn = getInstanceRrn(theForm.getInstanceId(), facilityRrn, ObjectList.TECHNOLOGY);
        Assert.isFalse(subTechnologyRrn <= 0 || StringUtils.isEmpty(subTechnologyId),
                       Errors.create().key(MessageIdList.TECHNOLOGY_ENTER_CORRECT)
                             .content("Please " + "enter " + "the " + "correct " + "subTechnology Id!").build());

        List<SubTechnology> subTechnologys = (List<SubTechnology>) WebUtils
                .getCacheString2Obj(theForm.getCacheSubTechnologys());
        if (CollectionUtils.isEmpty(subTechnologys)) {
            subTechnologys = new ArrayList<>();
        } else {
            for (SubTechnology subTechnology : subTechnologys) {
                Assert.isFalse(subTechnologyRrn == subTechnology.getInstanceRrn(),
                               Errors.create().key(MessageIdList.TECHNOLOGY_NOT_SUBMISSION)
                                     .content("Please do not" + " " + "repeat the" + " submission!").build());
            }
        }

        Relation relation = new Relation(0L, subTechnologyRrn, LinkTypeList.TECHNOLOGY_SUBTECHNOLOGY_KEY);
        relation = baseService.getRelation(relation);
        Assert.isTrue(relation == null, Errors.create().key(MessageIdList.TECHNOLOGY_ONE_SUBTECHNOLOGY)
                                              .content("Please do not repeat the submission!").build());
        relation = new Relation(technologyRrn, subTechnologyRrn, LinkTypeList.TECHNOLOGY_SUBTECHNOLOGY_KEY);
        relation.setTransPerformedby(LocalContext.getUserId());
        relation.setTransId(TransactionNames.CREATE_KEY);
        relation.setSequenceNumber(1);
        baseService.insertRelation(relation);
        SubTechnology subTechnology = new SubTechnology();
        subTechnology.setInstanceRrn(subTechnologyRrn);
        subTechnology.setInstanceId(subTechnologyId);
        subTechnology.setNamedSpace(getInstanceDesc(subTechnologyRrn));
        subTechnology.setObject(ObjectList.SUB_TECHNOLOGY);
        subTechnologys.add(subTechnology);

        request.setAttribute("subTechnologys", subTechnologys);
        theForm.setSubTechnologys(subTechnologys);
        theForm.setCacheSubTechnologys(WebUtils.getCacheObj2String(subTechnologys));
        theForm.setSubTechnologyId("");
        WebUtils.setSuccessMsg(request);
        return mapping.getInputForward();
    }

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

        Technology technology = new Technology(theForm.getInstanceId(),
                                               getNamedSpace(ObjectList.TECHNOLOGY, facilityRrn),
                                               ObjectList.TECHNOLOGY);
        PropertyUtils.copyProperties(technology, theForm);
        technology.setInstanceRrn(
                getInstanceRrn(theForm.getInstanceId(), getNamedSpace(ObjectList.TECHNOLOGY, facilityRrn),
                               ObjectList.TECHNOLOGY));
        technology.setTransPerformedby(user);
        technology.setTransId(TransactionNames.DELETE_KEY);

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

    public ActionForward deleteSubTechnology(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                             HttpServletResponse response) {
        long facilityRrn = LocalContext.getFacilityRrn();
        TechnologyFrom theForm = (TechnologyFrom) form;
        long subTechnologyRrn = WebUtils.getParameterLong("subTechnologyRrn", request);
        long technologyRrn = getInstanceRrn(theForm.getInstanceId(), facilityRrn, ObjectList.TECHNOLOGY);

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

        List<SubTechnology> subTechnologys = (List<SubTechnology>) WebUtils
                .getCacheString2Obj(theForm.getCacheSubTechnologys());
        Iterator<SubTechnology> iterator = subTechnologys.iterator();
        while (iterator.hasNext()) {
            SubTechnology subTechnology = iterator.next();
            if (subTechnologyRrn == subTechnology.getInstanceRrn()) {
                iterator.remove();
            }
        }
        request.setAttribute("subTechnologys", subTechnologys);
        theForm.setSubTechnologys(subTechnologys);
        theForm.setCacheSubTechnologys(WebUtils.getCacheObj2String(subTechnologys));
        WebUtils.setSuccessMsg(request);
        return mapping.getInputForward();
    }

    private Technology getTechnology(TechnologyFrom theForm) {
        Technology technology = new Technology(theForm.getInstanceId(),
                                               getNamedSpace(ObjectList.TECHNOLOGY, LocalContext.getFacilityRrn()),
                                               ObjectList.TECHNOLOGY);
        return prpService.getTechnology(technology);
    }

}