ChildRecipeAction.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.recipe;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
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.Recipe;
import com.mycim.valueobject.prp.RecipeCondition;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.RecipeSetupAction;
import com.mycim.webapp.forms.RecipeVersionInfoForm;
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.List;
import java.util.Map;

/**
 * sub recipe
 *
 * @author pinyan.song
 * @version 6.0.0
 * @date 2019-9-1 14:59
 **/
public class ChildRecipeAction extends RecipeSetupAction {
    private static String NOCONDITION = "NOCONDITION";

    private static String PARAMBASED = "PARAMBASED";

    private static String WAFERQTYBASED = "WAFERQTYBASED";

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) {
        RecipeVersionInfoForm theform = (RecipeVersionInfoForm) form;
        String recipeId = theform.getInstanceId();
        Recipe recipe = new Recipe(recipeId, getNamedSpace(ObjectList.RECIPE_KEY, LocalContext.getFacilityRrn()),
                                   ObjectList.RECIPE_KEY);

        long recipeRrn = baseService.getNamedObjectRrn(recipe);

        Long childRecipeRrn = new Long(request.getParameter(Constants.ITEM_KEY));
        Relation relation = new Relation();
        relation.setFromRrn(recipeRrn);
        relation.setToRrn(childRecipeRrn);
        relation.setLinkType(LinkTypeList.RECIPEFAMILY_TO_RECIPE);
        relation.setTransPerformedby(LocalContext.getUserId());
        relation.setTransId(TransactionNames.DELETE_KEY);

        recipeService.deleteRelation4RecipeFamilyToRecipe(relation);
        return init(mapping, theform, request, response);
    }

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        RecipeVersionInfoForm theform = (RecipeVersionInfoForm) form;
        String recipeId = theform.getInstanceId();
        List recipeList = new ArrayList();
        if (StringUtils.isNotBlank(recipeId)) {
            Recipe recipe = new Recipe(recipeId, getNamedSpace(ObjectList.RECIPE_KEY, LocalContext.getFacilityRrn()),
                                       ObjectList.RECIPE_KEY);

            long recipeRrn = baseService.getNamedObjectRrn(recipe);

            recipeList = baseService.getRelationForRecipe(recipeRrn, LinkTypeList.RECIPEFAMILY_TO_RECIPE);
            List<RecipeCondition> recipeConditions = recipeService.getRecipeCondition(recipeRrn);

            if (recipeConditions != null && !recipeConditions.isEmpty()) {
                RecipeCondition recipeCondition = (RecipeCondition) recipeConditions.iterator().next();
                if (recipeCondition.getProductRrn() > 0) {
                    theform.setProductId(getInstanceId(recipeCondition.getProductRrn()));
                }
                if (recipeCondition.getProcessRrn() > 0) {
                    theform.setProcessId(getInstanceId(recipeCondition.getProcessRrn()));
                }
                if (recipeCondition.getRouteRrn() > 0) {
                    theform.setRouteId(getInstanceId(recipeCondition.getRouteRrn()));
                }
                if (recipeCondition.getOperationRrn() > 0) {
                    theform.setOperationId(getInstanceId(recipeCondition.getOperationRrn()));
                }
                if (recipeCondition.getParameterRrn() > 0) {
                    theform.setParameterId(getInstanceId(recipeCondition.getParameterRrn()));
                }
                if (StringUtils.isNotBlank(recipeCondition.getPuType())) {
                    theform.setPuType(recipeCondition.getPuType());
                }
            }
        }

        /*String condition = WebUtils.getParameterUpperCase("condition", request);

        if (StringUtils.isEmpty(condition)) {
            condition = NOCONDITION;
        }

        if (recipeConditions != null && !recipeConditions.isEmpty()) {
            condition = PARAMBASED;
        }

        if (!recipeList.isEmpty()) {
            Map recipeInfo = (Map) recipeList.iterator().next();

            if (StringUtils.isNotBlank(MapUtils.getString(recipeInfo, "minWaferQty"))) {
                condition = WAFERQTYBASED;
            }

        }*/
        request.setAttribute("condition", NOCONDITION);
        request.setAttribute("recipeList", recipeList);

        theform.setTransId(Constants.MODIFY_KEY);
        return mapping.getInputForward();
    }

    public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                             HttpServletResponse response) {
        RecipeVersionInfoForm theform = (RecipeVersionInfoForm) form;
        String recipeId = theform.getInstanceId();
        Recipe recipe = new Recipe(recipeId, getNamedSpace(ObjectList.RECIPE_KEY, LocalContext.getFacilityRrn()),
                                   ObjectList.RECIPE_KEY);

        long recipeRrn = baseService.getNamedObjectRrn(recipe);
        theform.setIsParamSelect(StringUtils.EMPTY);
        String condition = WebUtils.getParameterUpperCase("condition", request);

        String childRecipeId = StringUtils.trimToUpperCase(request.getParameter("childRecipeId"));
        Assert.isFalse(StringUtils.isEmpty(childRecipeId),
                       Errors.create().key(MessageIdList.RECIPE_IS_EMPTY).content("The recipe cannot be empty!")
                             .build());

        long childRecipeRrn = getInstanceRrn(childRecipeId, LocalContext.getFacilityRrn(), ObjectList.RECIPE_KEY);
        Assert.isFalse(childRecipeRrn == 0,
                       Errors.create().key(MessageIdList.RECIPE_NOT_EXIST).content("Recipe does not exist!").build());

        Relation relation = new Relation();
        relation.setFromRrn(recipeRrn);
        relation.setLinkType(LinkTypeList.RECIPEFAMILY_TO_RECIPE);
        relation.setToRrn(childRecipeRrn);
        relation.setTransPerformedby(LocalContext.getUserId());
        relation.setTransId(TransactionNames.CREATE_KEY);

        List<Map> recipes = baseService.getRelationForRecipe(recipeRrn, LinkTypeList.RECIPEFAMILY_TO_RECIPE);
        if (recipes != null && !recipes.isEmpty()) {
            for (Map childRecipe : recipes) {
                if (childRecipe != null) {
                    String id = (String) childRecipe.get("recipeId");

                    Assert.isFalse(childRecipeId.equals(id),
                                   Errors.create().key(MessageIdList.RECIPE_IS_EXISTS).content("Recipe exists!")
                                         .build());
                }
            }
        }

        theform.setMinWaferQty(StringUtils.trimToEmpty(theform.getMinWaferQty()));
        theform.setMaxWaferQty(StringUtils.trimToEmpty(theform.getMaxWaferQty()));
        theform.setMinParaVal(StringUtils.trimToEmpty(theform.getMinParaVal()));
        theform.setMaxParaVal(StringUtils.trimToEmpty(theform.getMaxParaVal()));

        validationRule(condition, theform, relation, recipeRrn);

        recipeService.insertRelation4RecipeFamilyToRecipe(relation);
        long productRrn = 0;
        long processRrn = 0;
        long routeRrn = 0;
        long operationRrn = 0;
        String puType = "";
        long parameterRrn = 0;
        if (StringUtils.isNotBlank(theform.getProductId())) {
            productRrn = getInstanceRrn(theform.getProductId(), LocalContext.getFacilityRrn(), ObjectList.PRODUCT_KEY);
        }
        if (StringUtils.isNotBlank(theform.getProcessId())) {
            processRrn = getInstanceRrn(theform.getProcessId(), LocalContext.getFacilityRrn(), ObjectList.WFL_KEY);
        }
        if (StringUtils.isNotBlank(theform.getRouteId())) {
            routeRrn = getInstanceRrn(theform.getRouteId(), LocalContext.getFacilityRrn(), ObjectList.WFL_KEY);
        }
        if (StringUtils.isNotBlank(theform.getOperationId())) {
            operationRrn = getInstanceRrn(theform.getOperationId(), LocalContext.getFacilityRrn(),
                                          ObjectList.OPERATION_KEY);
        }
        if (StringUtils.isNotBlank(theform.getPuType())) {
            puType = theform.getPuType();
        }
        if (StringUtils.isNotBlank(theform.getParameterId())) {
            parameterRrn = getInstanceRrn(theform.getParameterId(), LocalContext.getFacilityRrn(),
                                          ObjectList.PARAMETER_KEY);
        }

        RecipeCondition recipeCondition = new RecipeCondition(
                getInstanceRrn(theform.getInstanceId(), LocalContext.getFacilityRrn(), ObjectList.RECIPE_KEY),
                productRrn, processRrn, routeRrn, operationRrn, puType, parameterRrn, theform.getIsParamSelect());

        recipeService.saveRecipeCondition(recipeCondition);
        theform.setChildRecipeId(StringUtils.EMPTY);
        theform.setMinWaferQty(StringUtils.EMPTY);
        theform.setMaxWaferQty(StringUtils.EMPTY);
        theform.setMaxParaVal(StringUtils.EMPTY);
        theform.setMinParaVal(StringUtils.EMPTY);
        return init(mapping, theform, request, response);
    }

    private void validationRule(String condition, RecipeVersionInfoForm theform, Relation relation, long recipeRrn) {
        if (StringUtils.equalsIgnoreCase(condition, WAFERQTYBASED)) {
            Assert.isFalse(
                    StringUtils.isBlank(theform.getMinWaferQty()) || StringUtils.isBlank(theform.getMaxWaferQty()),
                    Errors.create().key(MessageIdList.WAFER_QTY_CANNOT_EMPTY)
                          .content("Lower and upper wafer qty limit cannot be empty!").build());

            Assert.isFalse(theform.getMinWaferQty().length() > 5 || theform.getMaxWaferQty().length() > 5,
                           Errors.create().key(MessageIdList.WAFER_QTY_CANNOT_99999)
                                 .content("Wafer qty limits cannot be " + "larger than 99999!").build());

            Integer minWaferQty = NumberUtils.toInt(theform.getMinWaferQty(), 0);
            Integer maxWaferQty = NumberUtils.toInt(theform.getMaxWaferQty(), 0);

            Assert.isFalse(minWaferQty > maxWaferQty,
                           Errors.create().key(MessageIdList.WAFER_QTY_GREATER_THAN_PARAMETER)
                                 .content("Wafer qty upper limit must be greater than" + " " + "the parameter limit!")
                                 .build());

            relation.setAttributedata6(theform.getMinWaferQty());
            relation.setAttributedata7(theform.getMaxWaferQty());

            boolean isSubRecipeLegal = recipeService.isSubRecipeLegal(relation);
            Assert.isTrue(isSubRecipeLegal, Errors.create().key(MessageIdList.WAFER_QTY_UNREASONABLE)
                                                  .content("Wafer qty limits set unreasonable!").build());
        }

        if (StringUtils.equalsIgnoreCase(condition, PARAMBASED)) {
            theform.setIsParamSelect("on");
            Assert.isFalse(StringUtils.isBlank(theform.getMinParaVal()),
                           Errors.create().key(MessageIdList.PARAMETER_MISSING_LOWER_LIMIT)
                                 .content("Parameter lower limit " + "cannot be empty!").build());

            Assert.isFalse(StringUtils.isBlank(theform.getMaxParaVal()),
                           Errors.create().key(MessageIdList.PARAMETER_MISSING_UPPER_LIMIT)
                                 .content("Parameter upper limit " + "cannot be empty!").build());

            Assert.isFalse(theform.getMinParaVal().length() > 5 || theform.getMaxParaVal().length() > 5,
                           Errors.create().key(MessageIdList.PARAMETER_LOWER_LIMIT_99999)
                                 .content("Parameter limits cannot be " + "larger than 99999!").build());

            Assert.isFalse(Double.parseDouble(theform.getMinParaVal()) > Double.parseDouble(theform.getMaxParaVal()),
                           Errors.create().key(MessageIdList.PARAMETER_UPPER_GREATER_THAN_LOWER)
                                 .content("Parameter upper limit must be greater than the lower limit!").build());
            List<Map> minParaValLimits = recipeService.getParaValLimits(recipeRrn, "");
            List<Map> maxParaValLimits = recipeService.getParaValLimits(recipeRrn, "desc");
            if (minParaValLimits != null && minParaValLimits.size() > 0 && maxParaValLimits != null &&
                    maxParaValLimits.size() > 0) {
                Map minParaValLimit = (Map) minParaValLimits.iterator().next();
                Map maxParaValLimit = (Map) maxParaValLimits.iterator().next();
                Assert.isTrue((Double.parseDouble(theform.getMinParaVal()) <
                                      MapUtils.getDoubleValue(minParaValLimit, "minParaVal") &&
                                      Double.parseDouble(theform.getMaxParaVal()) <=
                                              MapUtils.getDoubleValue(minParaValLimit, "minParaVal")) ||
                                      ((Double.parseDouble(theform.getMinParaVal()) >=
                                              MapUtils.getDoubleValue(maxParaValLimit, "maxParaVal") &&
                                              Double.parseDouble(theform.getMaxParaVal()) >
                                                      MapUtils.getDoubleValue(maxParaValLimit, "maxParaVal"))),
                              Errors.create().key(MessageIdList.PARAMETER_LIMITS_UNREASONABLE)
                                    .content("Parameter limits set unreasonable!").build());
                relation.setAttributedata2(theform.getMinParaVal());
                relation.setAttributedata3(theform.getMaxParaVal());

            } else if (minParaValLimits.isEmpty() && maxParaValLimits.isEmpty()) {
                relation.setAttributedata2(theform.getMinParaVal());
                relation.setAttributedata3(theform.getMaxParaVal());
            }
        }
    }

}