GetAttributesAction.java

package com.mycim.webapp.actions.attribute;

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.lang.StringUtils;
import com.mycim.valueobject.bas.NamedObject;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.AbstractAction;
import com.mycim.webapp.forms.RootForm;
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.*;

/**
 * @author Johnson Wang
 **/
public class GetAttributesAction extends AbstractAction {

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

    private static final boolean RELEASED = true;

    public ActionForward cancel(ActionMapping mapping, RootForm theForm, HttpServletRequest request,
                                HttpServletResponse response) {
        request.setAttribute("objtype", theForm.getObjectType().toLowerCase());

        return mapping.findForward("setupentry");
    }

    public ActionForward modify(ActionMapping mapping, RootForm theForm, HttpServletRequest request,
                                HttpServletResponse response) {
        String object = theForm.getObjectType();

        NamedObject instance = getCurrentInstance(object, theForm.getInstanceId());
        long instanceRrn = instance.getInstanceRrn();

        List<Map> objectAttributeValues = new ArrayList();
        String[] attributeValues = request.getParameterValues("attributeValue");
        String[] fieldRrns = request.getParameterValues("fieldRrn");
        String[] fieldTypes = request.getParameterValues("fieldType");

        int count = 0;
        for (int i = 0; i < fieldRrns.length; i++) {
            if (StringUtils.startsWith(fieldTypes[i], "#")) {
                String fieldName = "attributeValue_" + fieldRrns[i];
                String fieldValue = WebUtils.getParameter(fieldName, request);
                Map attributeValue = new HashMap();
                attributeValue.put("instanceRrn", new Long(instanceRrn).toString());
                attributeValue.put("instanceVersion", "1");
                attributeValue.put("fieldRrn", fieldRrns[i]);
                attributeValue.put("attributeValue", fieldValue);
                objectAttributeValues.add(attributeValue);

            } else {
                Map attributeValue = new HashMap();
                attributeValue.put("instanceRrn", new Long(instanceRrn).toString());
                attributeValue.put("instanceVersion", "1");
                attributeValue.put("fieldRrn", fieldRrns[i]);

                String filedValue = attributeValues[count];
                log.debug("instanceRrn(" + instanceRrn + ")-fieldRrn(" + fieldRrns[i] + ")attributeValues[i](" +
                                  filedValue + ")");
                attributeValue.put("attributeValue", filedValue);
                objectAttributeValues.add(attributeValue);
                count++;
            }

        }

        sysService.maintainAttribute(objectAttributeValues);

        String obj = instance.getObject();

        if (object.equalsIgnoreCase("route")) {
            obj = "ROUTE";
        }

        if (object.equalsIgnoreCase("process")) {
            obj = "PROCESS";
        }

        List<Map> attributes = sysService
                .getAllAttributes(instanceRrn, getNamedSpace(obj, LocalContext.getFacilityRrn()), obj,
                                  I18nUtils.getCurrentLanguage());
        //buildCheckBox(theform, attributes);
        if (attributes.isEmpty()) {
            theForm.setPages(new Integer(0));
        } else {
            theForm.setPages(new Integer(1));
        }

        request.setAttribute("collection", attributes);

        request.setAttribute("objecttype", object);
        theForm.setTransId("modify");

        return (new ActionForward(mapping.getInput()));
    }

    private NamedObject getCurrentInstance(String object, String instanceId) {

        return baseService.getNamedObject(
                new NamedObject(instanceId, getNamedSpace(object, LocalContext.getFacilityRrn()), object));
    }

    public ActionForward copy(ActionMapping mapping, RootForm theForm, HttpServletRequest request,
                              HttpServletResponse response) {
        String instanceId = request.getParameter(Constants.COPY_KEY);
        String object = theForm.getObjectType();
        NamedObject instance = getCurrentInstance(object, theForm.getInstanceId());
        long instanceRrn = getInstanceRrn(instanceId, LocalContext.getFacilityRrn(), instance.getObject());
        if (object != null) {
            object = object.toUpperCase();
        }
        if (object.equalsIgnoreCase("entity")) {
            object = "equipment";
        }

        if (object.equalsIgnoreCase("item")) {
            object = "product";
        }

        if (object.equalsIgnoreCase("route")) {
            object = "ROUTE";
        }

        if (object.equalsIgnoreCase("process")) {
            object = "PROCESS";
        }

        theForm.setObjectType(object);

        log.debug("params object is " + object);

        List<Map> attributes = sysService
                .getAllAttributes(instanceRrn, getNamedSpace(object, LocalContext.getFacilityRrn()), object,
                                  I18nUtils.getCurrentLanguage());
        buildCheckBox(theForm, attributes);
        if (attributes.isEmpty()) {
            theForm.setPages(0);
        } else {
            theForm.setPages(1);
        }
        request.setAttribute("collection", attributes);
        request.setAttribute("objecttype", object);
        theForm.setTransId("modify");

        return (new ActionForward(mapping.getInput()));
    }

    private void buildCheckBox(RootForm theform, Collection attributes) {
        Iterator it = attributes.iterator();
        // buildcheckbox
        while (it.hasNext()) {
            HashMap attribute = new HashMap();
            attribute = (HashMap) it.next();
            String attributeValue = (String) attribute.get("attributeValue");
            if (StringUtils.equals(attributeValue, "0")) {
                theform.setAttributeValue("0");
            } else if (StringUtils.isEmpty(attributeValue)) {
                theform.setAttributeValue(null);
            } else {
                theform.setAttributeValue("1");
            }
        }
    }

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

        return qryAttributes(mapping, (RootForm) form, request, response);
    }

    public ActionForward qryAttributes(ActionMapping mapping, RootForm theForm, HttpServletRequest request,
                                       HttpServletResponse response) {

        if (StringUtils.isNotBlank(request.getParameter("equipmentReadOnlyFlag"))) {
            request.setAttribute("equipmentReadOnlyFlag", 1);
        }
        String object = request.getParameter(Constants.OBJECTTYPE_KEY);

        if (StringUtils.isNotEmpty(object)) {
            object = object.toUpperCase();
        }

        NamedObject instance = getCurrentInstance(object, theForm.getInstanceId());

        // when no such object exist
        if (instance == null) {
            theForm.setPages(0);
            theForm.setTransId("modify");

            return (new ActionForward(mapping.getInput()));
        }

        long instanceRrn = instance.getInstanceRrn();

        String obj = instance.getObject();

        if (object.equalsIgnoreCase("entity")) {
            object = "equipment";
        }

        if (object.equalsIgnoreCase("item")) {
            object = "product";
        }

        if (object.equalsIgnoreCase("route")) {
            obj = "ROUTE";
        }

        if (object.equalsIgnoreCase("process")) {
            obj = "PROCESS";
        }

        theForm.setObjectType(object);

        log.debug("params object is " + object);

        List<Map> attributes = sysService
                .getAllAttributes(instanceRrn, getNamedSpace(obj, LocalContext.getFacilityRrn()), obj,
                                  I18nUtils.getCurrentLanguage());
        if (attributes.isEmpty()) {
            theForm.setPages(0);
        } else {
            theForm.setPages(1);
        }

        request.setAttribute("collection", attributes);
        theForm.setTransId("modify");

        return (new ActionForward(mapping.getInput()));
    }


}