EntityCounterSaveAction.java

package com.mycim.webapp.actions.entitycounter;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.MiscUtils;
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.consts.SessionNames;
import com.mycim.valueobject.ems.Entity;
import com.mycim.valueobject.ems.EntityCounter;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.EmsSetupAction;
import com.mycim.webapp.forms.EntityCounterForm;
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 Counter
 *
 * @author pinyan.song
 * @version 6.0.0
 * @date 2019-11-13 15:21
 **/
public class EntityCounterSaveAction extends EmsSetupAction {
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        EntityCounterForm theform = (EntityCounterForm) form;
        Entity entity = getEntity(theform);
        request.setAttribute(SessionNames.ENTITY_KEY, entity);
        PropertyUtils.copyProperties(theform, entity);
        processItemAction(request, theform);
        theform.setTransId(Constants.MAINTAINCOUNTER4R_KEY);
        return mapping.getInputForward();
    }

    public ActionForward resetMembers(ActionMapping mapping, EntityCounterForm theform, HttpServletRequest request,
                                      HttpServletResponse response) {
        Entity entity = getEntity(theform);
        int sequenceNumber = WebUtils.getParameterInt(Constants.ITEM_KEY, request) - 1;
        EntityCounter entityCounter = ((List<EntityCounter>) entity.getEntitycounters()).get(sequenceNumber);
        long entityReset = entityCounter.getEntityRrn();
        String counterType = entityCounter.getCounterType();

        EntityCounter entityCounter1 = emsService.getActualCounter(entityReset, counterType);
        entityCounter.setActualCount(entityCounter1.getActualCount());

        PropertyUtils.copyProperties(theform, entityCounter);

        theform.setAlarmEnableFlag(MiscUtils.changeCheckBox(entityCounter.getAlarmEnableFlag()));
        theform.setWarnAlarmEnableFlag(MiscUtils.changeCheckBox(entityCounter.getWarnAlarmEnableFlag()));
        theform.setTransId(Constants.MODIFY_KEY);

        return mapping.findForward("resetmembers");
    }

    public ActionForward reset(ActionMapping mapping, EntityCounterForm theform, HttpServletRequest request,
                               HttpServletResponse response) {
        EntityCounter entityCounter = getEntityCounter(theform);
        long entityReset = entityCounter.getEntityRrn();
        String counterType = theform.getCounterType();
        EntityCounter entityCounter1 = emsService.getActualCounter(entityReset, counterType);
        long resetValue = theform.getActualCount() - entityCounter1.getActualCount();
        entityCounter1.setTransPerformedBy(entityCounter.getTransPerformedBy());
        emsService.updateActualValueForReset(entityCounter1, entityReset, counterType, resetValue);
        WebUtils.setSuccessMsg(request);
        return init(mapping, theform, request, response);
    }

    public ActionForward addMember(ActionMapping mapping, EntityCounterForm theform, HttpServletRequest request,
                                   HttpServletResponse response) {
        theform.setTransId(Constants.CREATE_KEY);
        return mapping.findForward(Constants.MEMBERS_KEY);
    }

    public ActionForward deleteMembers(ActionMapping mapping, EntityCounterForm theform, HttpServletRequest request,
                                       HttpServletResponse response) {
        Entity entity = getEntity(theform);
        int sequenceNumber = WebUtils.getParameterInt(Constants.ITEM_KEY, request) - 1;
        EntityCounter entityCounter = ((List<EntityCounter>) entity.getEntitycounters()).get(sequenceNumber);
        emsService.deleteEntityCounter(entityCounter);
        WebUtils.setSuccessMsg(request);
        return init(mapping, theform, request, response);
    }

    public ActionForward modifyMembers(ActionMapping mapping, EntityCounterForm theform, HttpServletRequest request,
                                       HttpServletResponse response) {
        Entity entity = getEntity(theform);
        int sequenceNumber = WebUtils.getParameterInt(Constants.ITEM_KEY, request) - 1;
        EntityCounter entityCounter = ((List<EntityCounter>) entity.getEntitycounters()).get(sequenceNumber);
        PropertyUtils.copyProperties(theform, entityCounter);
        theform.setAlarmEnableFlag(MiscUtils.changeCheckBox(entityCounter.getAlarmEnableFlag()));
        theform.setWarnAlarmEnableFlag(MiscUtils.changeCheckBox(entityCounter.getWarnAlarmEnableFlag()));
        theform.setTransId(Constants.MODIFY_KEY);
        return mapping.findForward(Constants.MEMBERS_KEY);
    }

    public ActionForward create(ActionMapping mapping, EntityCounterForm theform, HttpServletRequest request,
                                HttpServletResponse response) {
        EntityCounter entityCounter = getEntityCounter(theform);
        validateThis(entityCounter);

        long instanceRrn = emsService.insertEntityCounter(entityCounter);
        Assert.isFalse(instanceRrn == 0,
                       Errors.create().key(MessageIdList.ENTITY_COUNT_IS_EXISTS).content("This Count already exists.")
                             .build());

        WebUtils.setSuccessMsg(request);
        return init(mapping, theform, request, response);
    }

    public ActionForward modify(ActionMapping mapping, EntityCounterForm theform, HttpServletRequest request,
                                HttpServletResponse response) {
        EntityCounter entityCounter = getEntityCounter(theform);
        validateThis(entityCounter);

        emsService.updateEntityCounter(entityCounter);

        WebUtils.setSuccessMsg(request);
        return init(mapping, theform, request, response);
    }

    public ActionForward delete(ActionMapping mapping, EntityCounterForm theform, HttpServletRequest request,
                                HttpServletResponse response) {
        EntityCounter entityCounter = getEntityCounter(theform);
        emsService.deleteEntityCounter(entityCounter);
        WebUtils.setSuccessMsg(request);
        return init(mapping, theform, request, response);
    }

    private EntityCounter getEntityCounter(EntityCounterForm theform) {
        Entity entity = getEntity(theform);
        EntityCounter entityCounter = new EntityCounter();
        entityCounter.setEntityRrn(entity.getInstanceRrn());
        entityCounter.setTransPerformedBy(LocalContext.getUserId());

        PropertyUtils.copyProperties(entityCounter, theform);
        entityCounter.setAlarmEnableFlag(MiscUtils.parseCheckBox(theform.getAlarmEnableFlag()));
        entityCounter.setWarnAlarmEnableFlag(MiscUtils.parseCheckBox(theform.getWarnAlarmEnableFlag()));
        return entityCounter;
    }

    private void validateThis(EntityCounter instance) {
        if ((instance.getWarningCount() != null) && (instance.getTargetCount() != null)) {
            Assert.isFalse(instance.getWarningCount() > instance.getTargetCount(),
                           Errors.create().key(MessageIdList.ENTITYPM_TARGET_GREATER_WARNING).content("目标计数应大于警告计数!")
                                 .build());
        }

        // format valid value for special column
        if (StringUtils.isNotEmpty(instance.getChecklistId())) {
            long checklistRrn = getInstanceRrn(instance.getChecklistId(), this.getNamedSpace(ObjectList.CHECKLIST_KEY,
                                                                                             LocalContext
                                                                                                     .getFacilityRrn()),
                                               ObjectList.CHECKLIST_KEY);

            Assert.isFalse(checklistRrn <= 0,
                           Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("{} 没有找到对象!")
                                 .args("checkList").build());

            instance.setChecklistRrn(checklistRrn);
        }
    }

    private Entity getEntity(EntityCounterForm theform) {
        Entity entity = new Entity(theform.getInstanceId().trim().toUpperCase(),
                                   getNamedSpace(ObjectList.ENTITY_KEY, LocalContext.getFacilityRrn()),
                                   ObjectList.ENTITY_KEY);
        entity = (Entity) getInstance(entity);

        Assert.isFalse(entity == null,
                       Errors.create().key(MessageIdList.EQUIPMENT_MISSING).content("equipment not exist").build());

        entity.setEntitycounters(emsService.getEntityCountersOfEntity(entity.getInstanceRrn()));
        return entity;
    }

}