BORSetupAction.java

package com.mycim.webapp.actions.bor;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.beans.PropertyUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.ObjectVersion;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.prp.BOR;
import com.mycim.valueobject.prp.BORVersion;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.PrpSetupAction;
import com.mycim.webapp.forms.BorVersionInfoForm;
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.List;

/**
 * 资源清单管理入口
 *
 * @author
 */
public class BORSetupAction extends PrpSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        BorVersionInfoForm theform = (BorVersionInfoForm) form;
        if (StringUtils.isEmpty(theform.getInstanceId())) {
            theform.setTransId(Constants.CREATE_KEY);
            return mapping.findForward(Constants.CREATE_KEY);
        }

        BOR bor = prpService.getBOR(LocalContext.getFacilityRrn(), theform.getInstanceId());
        PropertyUtils.copyProperties(theform, bor);
        if (bor.getInstanceRrn() == 0) {
            theform.setVersionEditEnable("false");
            theform.setObjectDeleteEnable("true");
            theform.setTransId(Constants.CREATE_KEY);
            return mapping.findForward(Constants.CREATE_KEY);
        } else {
            List<BORVersion> versions = prpService.getBORVersions(bor);
            theform.setVersionEditEnable(String.valueOf(checkVersionEditEnable(versions)));
            theform.setObjectDeleteEnable(checkObjectDeleteEnabled(versions));
            theform.setTransId(Constants.MODIFY_KEY);

            return mapping.findForward(Constants.MODIFY_KEY);
        }
    }

    public ActionForward saveOrUpdata(ActionMapping mapping, BorVersionInfoForm theform, HttpServletRequest request,
                                      HttpServletResponse response) {
        Assert.isFalse(StringUtils.isEmpty(theform.getInstanceId()),
                       Errors.create().content("The bor id cannot be empty!").build());
        BOR bor = prpService.getBOR(LocalContext.getFacilityRrn(), theform.getInstanceId());
        bor.setTransPerformedby(LocalContext.getUserId());

        if (StringUtils.equals(theform.getTransId(), "create") || StringUtils.equals(theform.getTransId(), "modify")) {
            return createOrModifyBORObject(mapping, request, theform, bor);
        } else if (StringUtils.equals(theform.getTransId(), "delete")) {
            return deleteBORObject(mapping, request, theform, bor);

        }
        return WebUtils.NULLActionForward;
    }


    public ActionForward showBorVersion(ActionMapping mapping, HttpServletRequest request, BorVersionInfoForm theform) {

        BOR bor = prpService.getBOR(LocalContext.getFacilityRrn(), theform.getInstanceId());
        List<BORVersion> versions = prpService.getBORVersions(bor);
        BORVersion borVersion = null;
        ;
        if (versions.isEmpty()) {
            borVersion = new BORVersion();
            borVersion.copyNamedObject(bor);

            int newVersionId = bor.getCurrentVersion().intValue() + 1;

            borVersion.setInstanceVersion(newVersionId);
            borVersion.setVersionId(Integer.toString(newVersionId));
            borVersion.setVersionDesc("Automatic schema generation");
            borVersion.setTransId(Constants.CREATE_KEY);
            borVersion.setVersionStatus("UNFROZEN");
            borVersion.setTransPerformedby(LocalContext.getUserId());

            baseService.insertObjectVersionWithEcn(borVersion);
            versions = prpService.getBORVersions(bor);
            ObjectVersion objectVersion = (ObjectVersion) versions.iterator().next();
            borVersion.setVersionStatus(objectVersion.getVersionStatus());
            borVersion.setObjectSubtype(ObjectList.ITEM_KEY);

            bor.setVersions(versions);
        } else {
            borVersion = new BORVersion();

            borVersion.copyObjectVersion(versions.get(0));

            borVersion.setResources(prpService.getBorResources(borVersion));
            borVersion.copyNamedObject(bor);

            if (ObjectList.BOE_KEY.equals(borVersion.getObjectType())) {
                borVersion.setObjectSubtype(ObjectList.ENTITYGROUP_KEY);
            } else {
                borVersion.setObjectSubtype(ObjectList.ITEM_KEY);
            }

            bor.setVersions(versions);
        }

        request.setAttribute(SessionNames.BORVERSION_KEY, borVersion);
        PropertyUtils.copyProperties(theform, borVersion);

        theform.setChildResourceCount(String.valueOf(borVersion.getResources().size()));
        theform.setVersionEditEnable(String.valueOf(checkVersionEditEnable(borVersion)));
        theform.setObjectDeleteEnable(checkObjectDeleteEnabled(versions));

        theform.setTransId(Constants.MODIFY_KEY);

        return mapping.findForward("showversion");

    }


    private ActionForward createOrModifyBORObject(ActionMapping mapping, HttpServletRequest request,
                                                  BorVersionInfoForm theform, BOR bor) {
        PropertyUtils.copyProperties(bor, theform);
        bor.setObjectSubtype(ObjectList.ITEM_KEY);

        process(bor);
        bor.setVersions(prpService.getBORVersions(bor));

        theform.setObjectDeleteEnable(checkObjectDeleteEnabled(bor.getVersions()));
        theform.setTransId(Constants.MODIFY_KEY);

        return mapping.findForward(Constants.MODIFY_KEY);
    }

    private ActionForward deleteBORObject(ActionMapping mapping, HttpServletRequest request, BorVersionInfoForm theform,
                                          BOR bor) {
        PropertyUtils.copyProperties(bor, theform);
        process(bor);
        theform.setInstanceId("");
        theform.setInstanceDesc("");
        theform.setTransId(Constants.CREATE_KEY);
        return mapping.findForward(Constants.CREATE_KEY);
    }

    private String checkObjectDeleteEnabled(List<? extends ObjectVersion> versionObjects) {
        String checked = "true";
        if (CollectionUtils.isNotEmpty(versionObjects)) {
            // 判断所有版本中是否有激活的版本,有则不能删除,无则能删除
            for (Object version : versionObjects) {
                ObjectVersion versionObject = (ObjectVersion) version;
                if ((versionObject.getVersionStatus() != null) &&
                        versionObject.getVersionStatus().equalsIgnoreCase("ACTIVE")) {
                    checked = "false";
                    break;
                }
            }
        }
        return checked;
    }

}