ObjectAttributeValueSaveAction.java

package com.mycim.webapp.actions.setting.system.objectAttribute;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.i18n.I18nUtils;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.logging.Logger;
import com.mycim.framework.logging.LoggerFactory;
import com.mycim.framework.utils.MiscUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.bas.NamedObject;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.SystemSetupAction;
import com.mycim.webapp.forms.system.ObjectAttributeInfoForm;
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.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ObjectAttributeValueSaveAction extends SystemSetupAction {

    private static final Logger log = LoggerFactory.getLogger(ObjectAttributeValueSaveAction.class);

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

        ObjectAttributeInfoForm theform = (ObjectAttributeInfoForm) form;
        String id = StringUtils.trimToUpperCase(theform.getInstanceId());
        String object = theform.getObjectAttributeObject();
        String namedSpace = this.getNamedSpace(object, LocalContext.getFacilityRrn());
        String flag = "N";
        log.info("namedSpace = " + namedSpace);
        // display radio message
        Map instance = new HashMap();
        instance.put("object", theform.getObjectAttributeObject());
        instance.put("id", id);
        instance.put("namedSpace", namedSpace);
        // According to the instance of object, retrieve the value object
        if (StringUtils.equals("PROCESS", object) || StringUtils.equals("ROUTE", object)) {
            object = "WFL";
        }
        NamedObject namedObject = new NamedObject(id, getNamedSpace(object, LocalContext.getFacilityRrn()), object);
        long instanceRrn = baseService.getNamedObjectRrn(namedObject);
        Assert.isFalse(instanceRrn <= 0, Errors.create().key(MessageIdList.OBJECTATTRIBUTE_MISSING_INSTANCERRN)
                                               .content("没有找到instanceRrn!").build());
        instance.put("instanceRrn", instanceRrn);
        Collection collection = this
                .getObjectAttributeValue(instanceRrn, namedSpace, object, theform.getAttributeGroup(),
                                         I18nUtils.getCurrentLanguage());
        // set new Value into form Bean
        theform.setObjectAttributeNamedSpace(namedSpace);
        request.setAttribute(SessionNames.COLLECTION_KEY, collection);
        request.setAttribute(SessionNames.OBJECT_KEY, instance);
        theform.setTransId(Constants.MODIFY_KEY);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        ObjectAttributeInfoForm theform = (ObjectAttributeInfoForm) form;
        theform.setInstanceId("");
        log.info("sourcetytp = " + theform.getSourceType());
        if ("LOT".equalsIgnoreCase(theform.getSourceType())) {
            return mapping.findForward("lotsetup");
        }
        return mapping.findForward(Constants.SETUP_KEY);
    }

    public ActionForward onchange(ActionMapping mapping, ObjectAttributeInfoForm theform,
                                  HttpServletRequest request) throws Exception {
        String id = StringUtils.trimToUpperCase(theform.getInstanceId());
        String object = StringUtils.trimToUpperCase(theform.getObjectAttributeObject());
        String namedSpace = theform.getObjectAttributeNamedSpace();
        NamedObject namedObject = new NamedObject(id, getNamedSpace(object, LocalContext.getFacilityRrn()), object);
        long instanceRrn = baseService.getNamedObjectRrn(namedObject);
        Assert.isFalse(instanceRrn <= 0, Errors.create().key(MessageIdList.OBJECTATTRIBUTE_MISSING_INSTANCERRN)
                                               .content("没有找到instanceRrn!").build());
        Collection collection = this
                .getObjectAttributeValue(instanceRrn, namedSpace, object, theform.getAttributeGroup(),
                                         I18nUtils.getCurrentLanguage());

        collection = this.getObjectAttributeValue(instanceRrn, namedSpace, object, theform.getAttributeGroup(),
                                                  I18nUtils.getCurrentLanguage());
        request.setAttribute(SessionNames.COLLECTION_KEY, collection);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward modify(ActionMapping mapping, ObjectAttributeInfoForm theform,
                                HttpServletRequest request) throws Exception {
        String id = StringUtils.trimToUpperCase(theform.getInstanceId());
        String object = StringUtils.trimToUpperCase(theform.getObjectAttributeObject());
        String namedSpace = theform.getObjectAttributeNamedSpace();
        Map instance = new HashMap();
        instance.put("object", object);
        instance.put("id", id);
        instance.put("namedSpace", namedSpace);
        NamedObject namedObject = new NamedObject(id, getNamedSpace(object, LocalContext.getFacilityRrn()), object);
        long instanceRrn = baseService.getNamedObjectRrn(namedObject);
        Assert.isFalse(instanceRrn <= 0, Errors.create().key(MessageIdList.OBJECTATTRIBUTE_MISSING_INSTANCERRN)
                                               .content("没有找到instanceRrn!").build());
        instance.put("instanceRrn", instanceRrn);

        Collection collection = this
                .getObjectAttributeValue(instanceRrn, namedSpace, object, theform.getAttributeGroup(),
                                         I18nUtils.getCurrentLanguage());
        request.setAttribute("group", theform.getAttributeGroup());
        Long fieldRrn = new Long(request.getParameter(Constants.ITEM_KEY));
        Map value = getValueFromCollectionByFieldRrn(collection, fieldRrn);
        if (MapUtils.isNotEmpty(value)) {
            theform.setModifiableFlag(MiscUtils.unparseCheckBox(MapUtils.getString(value, "MODIFIABLEFLAG")));
            theform.setSystemUse(MiscUtils.unparseCheckBox(MapUtils.getString(value, "SYSTEMUSE")));
            theform.setAttributeGroup(MapUtils.getString(value, "ATTRIBUTEGROUP"));
            theform.setAttributeValue(MapUtils.getString(value, "ATTRIBUTEVALUE"));
            theform.setNamedSpace(MapUtils.getString(value, "NAMEDSPACE"));
        }
        log.info("group = " + theform.getAttributeGroup());
        String fieldId = getInstanceId(fieldRrn.longValue());
        theform.setFieldId(fieldId);
        request.setAttribute(SessionNames.ITEM_KEY, value);
        return mapping.findForward(Constants.MEMBERS_KEY);
    }

    public ActionForward delete(ActionMapping mapping, ObjectAttributeInfoForm theform,
                                HttpServletRequest request) throws Exception {
        String id = StringUtils.trimToUpperCase(theform.getInstanceId());
        String object = StringUtils.trimToUpperCase(theform.getObjectAttributeObject());
        String namedSpace = theform.getObjectAttributeNamedSpace();
        Map instance = new HashMap();
        instance.put("object", object);
        instance.put("id", id);
        instance.put("namedSpace", namedSpace);
        NamedObject namedObject = new NamedObject(id, getNamedSpace(object, LocalContext.getFacilityRrn()), object);
        long instanceRrn = baseService.getNamedObjectRrn(namedObject);
        Assert.isFalse(instanceRrn <= 0, Errors.create().key(MessageIdList.OBJECTATTRIBUTE_MISSING_INSTANCERRN)
                                               .content("没有找到instanceRrn!").build());
        instance.put("instanceRrn", instanceRrn);

        Collection collection = this
                .getObjectAttributeValue(instanceRrn, namedSpace, object, theform.getAttributeGroup(),
                                         I18nUtils.getCurrentLanguage());
        request.setAttribute("group", theform.getAttributeGroup());
        Long fieldRrn = new Long(request.getParameter(Constants.ITEM_KEY));
        Map value = getValueFromCollectionByFieldRrn(collection, fieldRrn);
        if (MapUtils.isNotEmpty(value)) {
            theform.setModifiableFlag(MiscUtils.unparseCheckBox(MapUtils.getString(value, "MODIFIABLEFLAG")));
            theform.setSystemUse(MiscUtils.unparseCheckBox(MapUtils.getString(value, "SYSTEMUSE")));
            theform.setAttributeGroup(MapUtils.getString(value, "ATTRIBUTEGROUP"));
            theform.setAttributeValue(MapUtils.getString(value, "ATTRIBUTEVALUE"));
            theform.setNamedSpace(MapUtils.getString(value, "NAMEDSPACE"));
        }
        log.info("group = " + theform.getAttributeGroup());
        value.put("ATTRIBUTEVALUE", "");
        value.put("instanceRrn", "" + instance.get("instanceRrn"));
        value.put("instanceVersion", "1");
        this.maintainAttributeValue(value);
        collection = this.getObjectAttributeValue(((Long) instance.get("instanceRrn")).longValue(),
                                                  (String) instance.get("namedSpace"), (String) instance.get("object"),
                                                  theform.getAttributeGroup(), I18nUtils.getCurrentLanguage());
        request.setAttribute(SessionNames.COLLECTION_KEY, collection);
        request.setAttribute(SessionNames.OBJECT_KEY, instance);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    private Map getValueFromCollectionByFieldRrn(Collection collection, Long fieldRrn) {
        Iterator it = collection.iterator();
        while (it.hasNext()) {
            Map item = (Map) it.next();
            Long rrn = MapUtils.getLong(item, "FIELD_RRN");
            if (rrn.equals(fieldRrn)) {
                return item;
            }
        }
        return null;
    }

}