EntityAttrEqptSaveAction.java

/*
 *        @ Copyright 2001 FA Software;
 *        All right reserved. No part of this program may be reproduced or
 *        transmitted in any form or by any means, electronic or
 *        mechanical, including photocopying, recording, or by any
 *        information storage or retrieval system without written
 *        permission from FA Software, except for inclusion of brief
 *        quotations in a review.
 */
package com.mycim.webapp.actions.equipment;

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.framework.utils.lang.math.NumberUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.ems.Entity;
import com.mycim.valueobject.ems.Equipment;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.EmsSetupAction;
import com.mycim.webapp.forms.EntityAttrEqptForm;
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.HashMap;
import java.util.Map;

public class EntityAttrEqptSaveAction extends EmsSetupAction {

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

        Entity entity = new Entity(theform.getInstanceId(),
                                   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());

        Equipment equipment = emsService.getEquipment(entity.getInstanceRrn());

        PropertyUtils.copyProperties(equipment, entity);
        PropertyUtils.copyProperties(theform, equipment);

        theform.setDynamicBatchFlag(MiscUtils.changeCheckBox(equipment.getDynamicBatchFlag()));
        theform.setTransportTimeStr(DateUtils.formatDateForSeconds(equipment.getTransportTime()));

        if ((equipment.getConcurrentJobSize() != null) && (equipment.getConcurrentJobSize().compareTo(0L) <= 0)) {
            theform.setConcurrentJobSize(null);
        }

        if ((equipment.getNumberOfRunningJobs() != null) && (equipment.getNumberOfRunningJobs().compareTo(0L) <= 0)) {
            theform.setNumberOfRunningJobs(null);
        }

        if ((equipment.getProcessingBatchSizeMin() != null) &&
                (equipment.getProcessingBatchSizeMin().compareTo(0L) <= 0)) {
            theform.setProcessingBatchSizeMin(null);
        }

        if ((equipment.getProcessingBatchSizeMax() != null) &&
                (equipment.getProcessingBatchSizeMax().compareTo(0L) <= 0)) {
            theform.setProcessingBatchSizeMax(null);
        }

        if ((equipment.getProcessingBatchSizeAvg() != null) &&
                (equipment.getProcessingBatchSizeAvg().compareTo(0L) <= 0)) {
            theform.setProcessingBatchSizeAvg(null);
        }

        if ((equipment.getCostJobSize() != null) && (NumberUtils.toLong(equipment.getCostJobSize()) <= 0)) {
            theform.setCostJobSize(null);
        }

        if ((equipment.getCostLotSize() != null) && (NumberUtils.toLong(equipment.getCostLotSize())) <= 0) {
            theform.setCostLotSize(null);
        }

        if ((equipment.getSpeedFactor() != null) && (equipment.getSpeedFactor().compareTo(0.0) <= 0)) {
            theform.setSpeedFactor(null);
        }

        theform.setEquipmentModelId(equipment.getEquipmentModelId());
        theform.setTransId(Constants.MAINTAINATTR4R_KEY);

        if (StringUtils.isNotBlank(WebUtils.getParameter("equipmentReadOnlyFlag", request))) {
            request.setAttribute("equipmentReadOnlyFlag", 1);
        }
        return new ActionForward(mapping.getInput());

    }


    public ActionForward modify(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {

        EntityAttrEqptForm theform = (EntityAttrEqptForm) form;

        Entity entity = new Entity(theform.getInstanceId(),
                                   getNamedSpace(ObjectList.ENTITY_KEY, LocalContext.getFacilityRrn()),
                                   ObjectList.ENTITY_KEY);

        entity = (Entity) getInstance(entity);

        Equipment equipment = emsService.getEquipment(entity.getInstanceRrn());
        Long controlingStationRrn = equipment.getControlingStationRrn();
        PropertyUtils.copyProperties(equipment, entity);
        PropertyUtils.copyProperties(equipment, theform);
        equipment.setTransPerformedby(LocalContext.getUserId());
        equipment.setTransId(Constants.MODIFY_KEY);
        equipment.setObject(entity.getObject());
        equipment.setObjectType(entity.getObjectType());
        equipment.setObjectSubtype(entity.getObjectSubtype());
        equipment.setInstanceStatus(entity.getInstanceStatus());
        equipment.setControlingStationRrn(controlingStationRrn);

        if (StringUtils.isNotEmpty(theform.getProcessEngineerId())) {
            long engineerRrn = getInstanceRrn(theform.getProcessEngineerId(),
                                              getNamedSpace(ObjectList.USER_KEY, LocalContext.getFacilityRrn()),
                                              ObjectList.USER_KEY);

            Assert.isFalse(engineerRrn <= 0, Errors.create().key(MessageIdList.ENTITY_MISSING_ENGINEER_ID)
                                                   .content("Not Define Engineer Id").build());
            equipment.setProcessEngineerRrn(engineerRrn);
        }

        if (StringUtils.isNotEmpty(theform.getControlingStationId())) {
            long stationRrn = getInstanceRrn(theform.getControlingStationId(),
                                             getNamedSpace(ObjectList.STATION_KEY, LocalContext.getFacilityRrn()),
                                             ObjectList.STATION_KEY);

            Assert.isFalse(stationRrn <= 0,
                           Errors.create().key(MessageIdList.STATION_INVALID_ID).content("station id invalid").build());
            equipment.setControlingStationRrn(stationRrn);
        }
        if (StringUtils.isNotEmpty(theform.getCostJobSize())) {
            long costJobSize = Long.parseLong(theform.getCostJobSize());

            Assert.isFalse(costJobSize < 0, Errors.create().key(MessageIdList.JOB_SIZE_LESS_THAN_0)
                                                  .content("Cost Job Size should not be less than 0!").build());
        }
        if (StringUtils.isNotEmpty(theform.getEquipmentModelId())) {
            long equipmentModelRrn = getInstanceRrn(theform.getEquipmentModelId(),
                                                    getNamedSpace(ObjectList.EQUIPMENTMODEL_KEY,
                                                                  LocalContext.getFacilityRrn()),
                                                    ObjectList.EQUIPMENTMODEL_KEY);

            Assert.isFalse(equipmentModelRrn <= 0, Errors.create().key(MessageIdList.EQUIPMENT_INVALID_ID)
                                                         .content("Equipment Model id invalid!").build());
            equipment.setEquipmentModelRrn(equipmentModelRrn);
        }

        if ((theform.getProcessingBatchSizeMin() != null) && (theform.getProcessingBatchSizeMax() != null)) {
            Assert.isFalse(theform.getProcessingBatchSizeMax() < theform.getProcessingBatchSizeMin(),
                           Errors.create().key(MessageIdList.SYSTEM_MAX_NOT_LESS_THAN_MIN)
                                 .content("Max should not be smaller than Min!").build());

            Assert.isFalse(theform.getProcessingBatchSizeMin() == 0,
                           Errors.create().key(MessageIdList.SYSTEM_MIN_LARGER_THAN_0)
                                 .content("Max should not be smaller " + "than Min!").build());
        }

        if (StringUtils.isNotEmpty(theform.getTransportTimeStr())) {
            equipment.setTransportTime(DateUtils.getSecondsTime(theform.getTransportTimeStr()));
        }

        equipment.setDynamicBatchFlag(MiscUtils.parseCheckBox(theform.getDynamicBatchFlag()));
        equipment.setInstanceRrn(entity.getInstanceRrn());

        Map entityExtMap = new HashMap();
        entityExtMap = emsService.getEquipmentExtMap(equipment.getInstanceRrn());
        equipment.setEquipmentLocation((String) entityExtMap.get("equipmentLocation"));
        equipment.setEquipmentOwner((String) entityExtMap.get("equipmentOwner"));
        equipment.setEquipmentWph((String) entityExtMap.get("equipmentWph"));
        equipment.setEquipmentModel((String) entityExtMap.get("equipmentModel"));
        equipment.setPollutionLevel((String) entityExtMap.get("pollutionLevel"));
        process(equipment);

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

}

/*
 * Modification Log Log No : Name : Modified Date: Description :
 */