EntityCounterTypeSaveAction.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.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.EntityCounterType;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.EmsSetupAction;
import com.mycim.webapp.forms.EntityCounterTypeForm;
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 Detail
 *
 * @author pinyan.song
 * @version 6.0.0
 * @date 2019-11-15 14:18
 **/
public class EntityCounterTypeSaveAction extends EmsSetupAction {
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        EntityCounterTypeForm theform = (EntityCounterTypeForm) 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 addMember(ActionMapping mapping, EntityCounterTypeForm theform, HttpServletRequest request,
                                   HttpServletResponse response) throws Exception {
        theform.setTransId(Constants.CREATE_KEY);
        return mapping.findForward(Constants.MEMBERS_KEY);
    }

    public ActionForward modifyMembers(ActionMapping mapping, EntityCounterTypeForm theform, HttpServletRequest request,
                                       HttpServletResponse response) throws Exception {
        Entity entity = getEntity(theform);
        int sequenceNumber = WebUtils.getParameterInt(Constants.ITEM_KEY, request);
        EntityCounterType entityCounterType = ((List<EntityCounterType>) entity.getEntitycounters())
                .get(sequenceNumber - 1);
        PropertyUtils.copyProperties(theform, entityCounterType);
        theform.setTransId(Constants.MODIFY_KEY);
        return mapping.findForward(Constants.MEMBERS_KEY);
    }

    public ActionForward deleteMembers(ActionMapping mapping, EntityCounterTypeForm theform, HttpServletRequest request,
                                       HttpServletResponse response) throws Exception {
        Entity entity = getEntity(theform);
        int sequenceNumber = WebUtils.getParameterInt(Constants.ITEM_KEY, request);
        EntityCounterType entityCounterType = ((List<EntityCounterType>) entity.getEntitycounters())
                .get(sequenceNumber - 1);
        entityCounterType.setEntityRrn(entity.getInstanceRrn());
        emsService.deleteEntityCounterType(entityCounterType);
        WebUtils.setSuccessMsg(request);
        return init(mapping, theform, request, response);
    }


    public ActionForward create(ActionMapping mapping, EntityCounterTypeForm theform, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        EntityCounterType entityCounterType = getEntityCounterType(theform);
        validateThis(entityCounterType);

        long instanceRrn = emsService.insertEntityCounterType(entityCounterType);
        Assert.isFalse(instanceRrn == 0, Errors.create().key(MessageIdList.ENTITY_COUNT_TYPE_IS_EXISTS)
                                               .content("This Count type already exists.").build());

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

    public ActionForward modify(ActionMapping mapping, EntityCounterTypeForm theform, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        EntityCounterType entityCounterType = getEntityCounterType(theform);
        validateThis(entityCounterType);
        emsService.updateEntityCounterType(entityCounterType);
        WebUtils.setSuccessMsg(request);
        return init(mapping, theform, request, response);
    }


    public ActionForward delete(ActionMapping mapping, EntityCounterTypeForm theform, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        EntityCounterType entityCounterType = getEntityCounterType(theform);
        emsService.deleteEntityCounterType(entityCounterType);
        WebUtils.setSuccessMsg(request);
        return init(mapping, theform, request, response);
    }

    private void validateThis(EntityCounterType entityCounterType) {
        Assert.isFalse(StringUtils.isEmpty(entityCounterType.getCounterType()),
                       Errors.create().key(MessageIdList.ENTITY_MISSING_COUNTER_TYPE)
                             .content("The counter type" + " " + "cannot be " + "empty, please " + "re-enter.")
                             .build());

        Assert.isTrue(StringUtils.isNotEmpty(entityCounterType.getEventId()),
                      Errors.create().key(MessageIdList.ENTITY_MISSING_EVENT_ID)
                            .content("Event Id is needed for " + "transaction!").build());
        long _eventRrn = 0;
        _eventRrn = getInstanceRrn(entityCounterType.getEventId().toUpperCase(),
                                   getNamedSpace(ObjectList.EVENT_KEY, LocalContext.getFacilityRrn()),
                                   ObjectList.EVENT_KEY);

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

        entityCounterType.setEventRrn(_eventRrn);

        if (StringUtils.isNotEmpty(entityCounterType.getRecipeId())) {
            long _recipeRrn = 0;
            _recipeRrn = getInstanceRrn(entityCounterType.getRecipeId().toUpperCase(),
                                        getNamedSpace(ObjectList.RECIPE_KEY, LocalContext.getFacilityRrn()),
                                        ObjectList.RECIPE_KEY);

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

            entityCounterType.setRecipeRrn(_recipeRrn);
        }
    }

    private EntityCounterType getEntityCounterType(EntityCounterTypeForm theform) {
        Entity entity = getEntity(theform);
        EntityCounterType entityCounterType = new EntityCounterType();
        entityCounterType.setEntityRrn(entity.getInstanceRrn());
        entityCounterType.setTransPerformedBy(LocalContext.getUserId());

        PropertyUtils.copyProperties(entityCounterType, theform);
        return entityCounterType;
    }


    private Entity getEntity(EntityCounterTypeForm 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.getEntityCounterTypesOfEntity(entity.getInstanceRrn()));
        return entity;
    }

}