ObjectAttributeFieldSaveAction.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.threadlocal.LocalContext;
import com.mycim.framework.utils.MiscUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.sys.ObjectAttribute;
import com.mycim.valueobject.sys.ObjectAttributeField;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.SystemSetupAction;
import com.mycim.webapp.forms.system.ObjectAttributeInfoForm;
import org.apache.commons.beanutils.PropertyUtils;
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;

/**
 * 对象属性定义详情页
 *
 * @author yanbing.chen
 * @date 2019/8/19
 * @since 1.8
 **/
public class ObjectAttributeFieldSaveAction extends SystemSetupAction {

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

        ObjectAttributeInfoForm theform = (ObjectAttributeInfoForm) form;
        String id = StringUtils.trimToUpperCase(theform.getObjectAttributeObject());
        String versionId = StringUtils.trimToUpperCase(theform.getVersionFlag());
        ObjectAttributeField objectAttributeField = new ObjectAttributeField(id, getNamedSpace(id, LocalContext
                .getFacilityRrn()), versionId);
        if (request.getParameter(Constants.MODIFY_KEY) != null) {
            Long fieldRrn = new Long(request.getParameter(Constants.ITEM_KEY));
            objectAttributeField.setFieldRrn(fieldRrn);
            objectAttributeField = (ObjectAttributeField) getInstance(objectAttributeField);
            PropertyUtils.copyProperties(theform, objectAttributeField);
            theform.setModifiableFlag(MiscUtils.unparseCheckBox(objectAttributeField.getModifiableFlag()));
            theform.setSystemUse(MiscUtils.unparseCheckBox(objectAttributeField.getSystemUse()));
            theform.setAttributeGroup(objectAttributeField.getAttributeGroup());
            String fieldId = getInstanceId(Long.parseLong(request.getParameter(Constants.ITEM_KEY)));
            theform.setFieldId(fieldId);
            request.setAttribute(SessionNames.OBJECTATTRIBUTEFIELD_KEY, objectAttributeField);
            theform.setTransId(Constants.MODIFY_KEY);
            return mapping.findForward(Constants.MEMBERS_KEY);
        } else {
            theform.setTransId(Constants.CREATE_KEY);
            request.setAttribute(SessionNames.OBJECTATTRIBUTEFIELD_KEY, objectAttributeField);
            return mapping.findForward(Constants.MEMBERS_KEY);
        }
    }

    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        ObjectAttributeInfoForm theform = (ObjectAttributeInfoForm) form;
        ObjectAttribute objectAttribute = new ObjectAttribute(theform.getObjectAttributeObject(),
                                                              getNamedSpace(theform.getObjectAttributeObject(),
                                                                            LocalContext.getFacilityRrn()));
        objectAttribute.setVersionFlag(theform.getVersionFlag());
        // According to the instance of object, retrieve the value object
        objectAttribute = (ObjectAttribute) getInstance(objectAttribute);
        // if user click the Cancel button
        request.setAttribute(SessionNames.OBJECTATTRIBUTE_KEY, objectAttribute);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward modify(ActionMapping mapping, ObjectAttributeInfoForm theform,
                                HttpServletRequest request) throws Exception {
        ObjectAttributeField objectAttributeField = new ObjectAttributeField();
        objectAttributeField.setObjectAttributeFieldObject(theform.getObjectAttributeObject());
        objectAttributeField.setObjectAttributeFieldNamedSpace(theform.getObjectAttributeNamedSpace());
        objectAttributeField.setVersionFlag(theform.getVersionFlag());
        objectAttributeField.setTransPerformedby(LocalContext.getUserId());
        objectAttributeField = (ObjectAttributeField) getInstance(objectAttributeField);
        boolean check = true;
        String fieldId = theform.getFieldId();
        if ((fieldId != null) && !StringUtils.trim(fieldId).equals("")) {
            String trimToUpperFieldId = StringUtils.trimToUpperCase(fieldId);
            // get field rrn by id
            long fieldRrn = getInstanceRrn(trimToUpperFieldId,
                                           getNamedSpace(ObjectList.FIELD_KEY, LocalContext.getFacilityRrn()),
                                           ObjectList.FIELD_KEY);
            Assert.isFalse(fieldRrn <= 0,
                           Errors.create().key(MessageIdList.FIELD_MISSING_FIELD).content("没有找到这个字段!").build());
            objectAttributeField.setFieldRrn(fieldRrn);
        } else {
            check = false;
        }
        ObjectAttribute objectAttribute = new ObjectAttribute(theform.getObjectAttributeObject(),
                                                              getNamedSpace(theform.getObjectAttributeObject(),
                                                                            LocalContext.getFacilityRrn()));
        objectAttribute.setVersionFlag(theform.getVersionFlag());
        // According to the instance of object, retrieve the value object
        objectAttribute = (ObjectAttribute) getInstance(objectAttribute);

        objectAttributeField.setModifiableFlag(MiscUtils.parseCheckBox(theform.getModifiableFlag()));
        objectAttributeField.setSystemUse(MiscUtils.parseCheckBox(theform.getSystemUse()));
        if (theform.getDefaultValue() != null) {
            objectAttributeField.setDefaultValue(theform.getDefaultValue());
        } else {
            objectAttributeField.setDefaultValue("");
        }
        if (theform.getAttributeGroup() != null) {
            objectAttributeField.setAttributeGroup(theform.getAttributeGroup());
        } else {
            objectAttributeField.setDefaultValue("");
        }
        if (request.getParameter(Constants.MODIFY_KEY) != null) {
            // execute the process method that will understand action
            // thru value object property transId
            objectAttributeField.setTransId(Constants.MODIFY_KEY);
        } else if (request.getParameter(Constants.CREATE_KEY) != null) {
            objectAttributeField.setTransId(Constants.CREATE_KEY);
        }
        // no matter modify or delete, they all execute the same function
        if (check) {
            process(objectAttributeField);
            objectAttribute = (ObjectAttribute) getInstance(objectAttribute);
            request.setAttribute(SessionNames.OBJECTATTRIBUTE_KEY, objectAttribute);
        }
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward delete(ActionMapping mapping, ObjectAttributeInfoForm theform,
                                HttpServletRequest request) throws Exception {
        ObjectAttributeField objectAttributeField = new ObjectAttributeField();
        objectAttributeField.setObjectAttributeFieldObject(theform.getObjectAttributeObject());
        objectAttributeField.setObjectAttributeFieldNamedSpace(theform.getObjectAttributeNamedSpace());
        objectAttributeField.setVersionFlag(theform.getVersionFlag());
        objectAttributeField.setTransPerformedby(LocalContext.getUserId());
        objectAttributeField = (ObjectAttributeField) getInstance(objectAttributeField);
        String fieldId = theform.getFieldId();
        if ((fieldId != null) && !StringUtils.trim(fieldId).equals("")) {
            String trimToUpperFieldId = StringUtils.trimToUpperCase(fieldId);
            // get field rrn by id
            long fieldRrn = getInstanceRrn(trimToUpperFieldId,
                                           getNamedSpace(ObjectList.FIELD_KEY, LocalContext.getFacilityRrn()),
                                           ObjectList.FIELD_KEY);
            Assert.isFalse(fieldRrn <= 0,
                           Errors.create().key(MessageIdList.FIELD_MISSING_FIELD).content("没有找到这个字段!").build());
            objectAttributeField.setFieldRrn(fieldRrn);
        }
        ObjectAttribute objectAttribute = new ObjectAttribute(theform.getObjectAttributeObject(),
                                                              getNamedSpace(theform.getObjectAttributeObject(),
                                                                            LocalContext.getFacilityRrn()));
        objectAttribute.setVersionFlag(theform.getVersionFlag());
        // According to the instance of object, retrieve the value object
        objectAttribute = (ObjectAttribute) getInstance(objectAttribute);
        // delete Reference
        objectAttributeField.setTransId(Constants.DELETE_KEY);
        process(objectAttributeField);
        objectAttribute = (ObjectAttribute) getInstance(objectAttribute);
        request.setAttribute(SessionNames.OBJECTATTRIBUTE_KEY, objectAttribute);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

}