EntityGroupUsedAction.java

package com.mycim.webapp.actions.operation.entitygroup;

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.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.Relation;
import com.mycim.valueobject.consts.LinkTypeList;
import com.mycim.valueobject.ems.Entity;
import com.mycim.valueobject.prp.Operation;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.PrpSetupAction;
import com.mycim.webapp.forms.OperationInfoForm;
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.List;

/**
 * Entity Group Used
 *
 * @author Johnson.Wang
 * @version 6.0.0
 * @date 2019/8/28
 **/
public class EntityGroupUsedAction extends PrpSetupAction {

    private Operation operation = null;

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

        infoForm.setEntityId(null);

        Operation operation = getOperation(infoForm.getInstanceId());

        PropertyUtils.copyProperties(form, operation);

        List<Relation> allEqp = emsService.getAllEntities(operation.getEntityGroupRrn());

        request.setAttribute("entitygroup", allEqp);

        return new ActionForward(mapping.getInput());
    }

    public ActionForward create(ActionMapping mapping, OperationInfoForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        Operation operation = getOperation(form.getInstanceId());

        String entityId = form.getEntityId().trim().toUpperCase();
        Entity theEntity = new Entity(entityId, getNamedSpace(ObjectList.ENTITY_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.ENTITY_KEY);
        long entityRrn = baseService.getNamedObjectRrn(theEntity);
        Assert.isFalse(entityRrn == 0,
                       Errors.create().key(MessageIdList.ENTITY_INVALID_ID).content("No such Entity ID: {}")
                             .args(entityId).build());

        emsService.addEntityToEntityGroup(operation.getEntityGroupRrn(), entityRrn);

        return init(mapping, form, request, response);
    }

    public ActionForward update(ActionMapping mapping, OperationInfoForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        String entitybegin = "";
        String entityend = "";
        String linktype = request.getParameter(Constants.LIKETYPE_KEY);
        long entityRrn = new Long(request.getParameter(Constants.FROM_RRN)).longValue();
        long entityGroupRrn = new Long(request.getParameter(Constants.TO_RRN)).longValue();
        if (StringUtils.equals(linktype, LinkTypeList.ENTITY_ENTITYROUP_KEY)) {
            entitybegin = LinkTypeList.ENTITY_GROUP_INACTIVE_KEY;
            entityend = LinkTypeList.ENTITY_ENTITYROUP_KEY;
        } else {
            entitybegin = LinkTypeList.ENTITY_ENTITYROUP_KEY;
            entityend = LinkTypeList.ENTITY_GROUP_INACTIVE_KEY;
        }
        baseService.updateLinkType(entitybegin, entityRrn, entityGroupRrn, entityend);
        return init(mapping, form, request, response);
    }

    public ActionForward delete(ActionMapping mapping, OperationInfoForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        long entityRrn = new Long(request.getParameter(Constants.FROM_RRN)).longValue();
        long entityGroupRrn = new Long(request.getParameter(Constants.TO_RRN)).longValue();
        String linktype = request.getParameter(Constants.LIKETYPE_KEY);
        Relation relation = new Relation();
        relation.setFromRrn(entityRrn);
        relation.setToRrn(entityGroupRrn);
        relation.setLinkType(linktype);
        relation.setTransId(Constants.DELETE_KEY);
        relation.setTransPerformedby(LocalContext.getUserId());
        baseService.deleteRelation(relation);
        return init(mapping, form, request, response);
    }

    private Operation getOperation(String operationId) {
        /*if (operation != null && operation.getInstanceId().equalsIgnoreCase(operationId)) {
            return this.operation;
        }*/
        operation = new Operation(operationId, getNamedSpace(ObjectList.OPERATION_KEY, LocalContext.getFacilityRrn()),
                                  ObjectList.OPERATION_KEY);

        operation = (Operation) getInstance(operation);
        Assert.notNull(operation,
                       Errors.create().key(MessageIdList.OPERATION_INVALID_ID).content("Invalid step id!").build());
        return operation;
    }

}