ChecklistJobEditAction.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.checklist;

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.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.NamedObject;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.ems.ChecklistJob;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.EmsSetupAction;
import com.mycim.webapp.forms.DoChecklistInfoForm;
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;

public class ChecklistJobEditAction extends EmsSetupAction {

    public static final String STARTED = "STARTED";

    public static final String CANCELED = "CANCELED";

    public static final String COMPLETED = "COMPLETED";

    private static final String VIEW_TYPE = "viewtype";

    private static final String PAGE_FORM = "pagefrom";

    private static final String QC_TYPE = "QC";

    private static final String NOQC_TYPE = "NOQC";

    private static final String V_ACTIVE_KEY = "viewactive";

    private static final String V_ALL_ACTIVE_KEY = "viewallactive";

    private static final String V_ALL_IN_ACTIVE_KEY = "viewallinactive";

    private static final String V_IN_ACTIVE = "viewinactive";

    private static final String V_CANCEL_KEY = "viewcancel";

    private static final String V_DATA = "viewdata";

    private boolean isCarrierFlag = false;

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) {
        DoChecklistInfoForm theform = (DoChecklistInfoForm) form;
        isCarrierFlag = StringUtils.isNotBlank(theform.getIsCpm());
        getEquipment(theform);
        return mapping.findForward(Constants.CREATE_KEY);
    }

    public ActionForward viewcancel(ActionMapping mapping, DoChecklistInfoForm theform, HttpServletRequest request,
                                    HttpServletResponse response) {
        String filter = getFilter(theform, request);
        List<ChecklistJob> collection = new ArrayList<>();
        if (StringUtils.isEmpty(theform.getEntityId())) {
            collection = this.getCancelChecklistJobs(filter);
        } else {
            NamedObject equipment = getEquipment(theform);
            collection = this.getCancelChecklistJobs(equipment.getInstanceRrn(), filter);
        }
        request.setAttribute(VIEW_TYPE, V_CANCEL_KEY);
        request.setAttribute(SessionNames.COLLECTION_KEY, collection);
        return mapping.findForward(V_DATA);
    }

    public ActionForward viewactive(ActionMapping mapping, DoChecklistInfoForm theform, HttpServletRequest request,
                                    HttpServletResponse response) {
        String filter = getFilter(theform, request);
        List<ChecklistJob> collection;
        if (StringUtils.isEmpty(theform.getEntityId())) {
            if (StringUtils.equals("surecomplate", request.getParameter(PAGE_FORM))) {
                collection = this.getActiveChecklistJobsApproving();
            } else {
                collection = this.getActiveChecklistJobs(filter);
            }
            request.setAttribute(PAGE_FORM, request.getParameter(PAGE_FORM));
            request.setAttribute(VIEW_TYPE, V_ALL_ACTIVE_KEY);

        } else {
            NamedObject equipment = getEquipment(theform);
            if (StringUtils.equals("surecomplate", request.getParameter(PAGE_FORM))) {
                collection = this.getActiveChecklistJobsApproving(equipment.getInstanceRrn());
            } else {
                collection = this.getActiveChecklistJobs(equipment.getInstanceRrn(), filter);
            }
            request.setAttribute(VIEW_TYPE, V_ACTIVE_KEY);
        }
        request.setAttribute(SessionNames.COLLECTION_KEY, collection);
        return mapping.findForward(V_DATA);
    }

    public ActionForward viewinactive(ActionMapping mapping, DoChecklistInfoForm theform, HttpServletRequest request,
                                      HttpServletResponse response) {
        String filter = getFilter(theform, request);
        List<ChecklistJob> collection;

        if (StringUtils.isEmpty(theform.getEntityId())) {
            collection = this.getInActiveChecklistJobs(filter);
            request.setAttribute(VIEW_TYPE, V_ALL_IN_ACTIVE_KEY);
        } else {
            NamedObject equipment = getEquipment(theform);
            collection = this.getInActiveChecklistJobs(equipment.getInstanceRrn(), filter);
            request.setAttribute(VIEW_TYPE, V_IN_ACTIVE);
        }

        request.setAttribute(SessionNames.COLLECTION_KEY, collection);
        return mapping.findForward(V_DATA);
    }

    private List<ChecklistJob> getCancelChecklistJobs(String filter) {
        return emsService.getChecklistJobs(" checklist_job_state = 'CANCELED'" + getFilterWhere(filter) + isCpm());
    }

    private List<ChecklistJob> getCancelChecklistJobs(long equipmentRrn, String filter) {
        return emsService.getChecklistJobs(
                " entity_rrn = " + equipmentRrn + " and checklist_job_state = 'CANCELED'" + getFilterWhere(filter) +
                        isCpm());
    }

    private List<ChecklistJob> getInActiveChecklistJobs(long equipmentRrn, String filter) {
        return emsService.getChecklistJobs(
                " entity_rrn = " + equipmentRrn + " and checklist_job_state = '" + COMPLETED + "'" +
                        getFilterWhere(filter) + isCpm());
    }

    private List<ChecklistJob> getActiveChecklistJobsApproving(long equipmentRrn) {
        String extendCondition =
                " attri_2 = 'approving' and entity_rrn = " + equipmentRrn + " and " + "checklist_job_state = '" +
                        STARTED + "'";
        return emsService.getChecklistJobs(extendCondition);
    }

    private List<ChecklistJob> getActiveChecklistJobs(long equipmentRrn, String filter) {
        return emsService.getChecklistJobs(
                " entity_rrn = " + equipmentRrn + " and checklist_job_state = '" + STARTED + "'" +
                        getFilterWhere(filter) + isCpm());
    }

    private NamedObject getEquipment(DoChecklistInfoForm theform) {
        NamedObject equipment = baseService.getNamedObject(new NamedObject(theform.getEntityId().toUpperCase(),
                                                                           getNamedSpace(ObjectList.ENTITY_KEY,
                                                                                         LocalContext.getFacilityRrn()),
                                                                           ObjectList.ENTITY_KEY));
        Assert.isFalse(equipment == null ||
                               (!StringUtils.equalsIgnoreCase(ObjectList.EQUIPMENT_KEY, equipment.getObjectType()) &&
                                       !StringUtils
                                               .equalsIgnoreCase(ObjectList.CARRIER_KEY, equipment.getObjectType())),
                       Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("{} 没有找到对象!")
                             .args(isCarrierFlag ? "Carrier" : "EQP").build());
        theform.setEntityRrn(equipment.getInstanceRrn());
        theform.setEntityId(equipment.getInstanceId());
        theform.setNamedSpace(equipment.getNamedSpace());
        theform.setInstanceDesc(equipment.getInstanceDesc());
        return equipment;
    }

    private List<ChecklistJob> getInActiveChecklistJobs(String filter) {
        return emsService
                .getChecklistJobs(" checklist_job_state = '" + COMPLETED + "'" + getFilterWhere(filter) + isCpm());
    }

    private List<ChecklistJob> getActiveChecklistJobs(String filter) {
        return emsService
                .getChecklistJobs(" checklist_job_state = '" + STARTED + "'" + getFilterWhere(filter) + isCpm());
    }

    private String getFilterWhere(String filter) {
        String filterWhere = "";
        if (QC_TYPE.equalsIgnoreCase(filter)) {
            filterWhere = " and checklist_rrn in (select instance_rrn from named_object where object = " +
                    "'CHECKLIST' AND OBJECT_SUBTYPE='SQC')";
        } else if (NOQC_TYPE.equalsIgnoreCase(filter)) {
            filterWhere = " and checklist_rrn in (select instance_rrn from named_object where object = " +
                    "'CHECKLIST' AND OBJECT_SUBTYPE='PM')";
        }
        return filterWhere;
    }

    private List<ChecklistJob> getActiveChecklistJobsApproving() {
        return this.emsService.getChecklistJobs(" attri_2 = 'approving' and checklist_job_state = '" + STARTED + "'");
    }

    private String isCpm() {
        String str = "";
        if (isCarrierFlag) {
            str = " and object_type='" + ObjectList.CARRIER_KEY + "'";
        } else {
            str = " and object_type not in ('" + ObjectList.CARRIER_KEY + "')";
        }
        return str;
    }

    private String getFilter(DoChecklistInfoForm theform, HttpServletRequest request) {
        String filter = "ALL";
        if (request.getParameter("filter") != null) {
            filter = request.getParameter("filter");
        }
        if (StringUtils.isNotEmpty(theform.getObjectSubtype())) {
            filter = "SQC".equals(theform.getObjectSubtype()) ? QC_TYPE : NOQC_TYPE;
        }

        isCarrierFlag = StringUtils.isNotBlank(theform.getIsCpm());
        return filter;
    }

}

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