UserGroupSaveAction.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.setting.security.user;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.exception.SystemIllegalStateException;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.beans.PropertyUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.Relation;
import com.mycim.valueobject.consts.LinkTypeList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.security.User;
import com.mycim.valueobject.security.UserGroup;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.CimSetupAction;
import com.mycim.webapp.forms.security.UserGroupInfoForm;
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;

/**
 * @author Johnson Wang
 **/
public class UserGroupSaveAction extends CimSetupAction {

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

        UserGroupInfoForm theform = (UserGroupInfoForm) form;

        // if user click the Cancel button
        if (request.getParameter(Constants.CANCEL_KEY) != null) {
            if (mapping.getAttribute() != null) {
                request.removeAttribute(mapping.getAttribute());
            }

            request.removeAttribute(SessionNames.USERGROUP_KEY);

            return (mapping.findForward("setup"));
        }

        UserGroup userGroup = (UserGroup) request.getAttribute(SessionNames.USERGROUP_KEY);

        // Validate the transactional control token
        if (!isTokenValid(request) || (userGroup == null)) {
            resetToken(request);
            throw new SystemIllegalStateException(
                    Errors.create().key(MessageIdList.SYSTEM_TRANSACTION_NOT_TOKEN).content("验证token失败!").build());
        }

        // who operate this object?
        userGroup.setTransPerformedby(LocalContext.getUserId());

        // copy function is a special function other than adding, deleting or updating
        if (request.getParameter(Constants.COPY_KEY) != null) {
            // the new Id is stored in the url COPY key
            String id = request.getParameter(Constants.COPY_KEY);

            userGroup.setInstanceId(id);

            doCopy(theform, userGroup);

            // for logic equal tag, copy is the same action as create
            theform.setTransId(Constants.CREATE_KEY);

            return (mapping.findForward("modify"));
        } // end of copy function

        // member of action, when there is a one2many function
        if (request.getParameter(Constants.MEMBERS_KEY) != null) {
            theform.setTransId(Constants.MODIFY_KEY);

            long userRrn = 0;
            Relation relation = new Relation();

            if (Constants.DELETE_KEY.equals(request.getParameter(Constants.MEMBERS_KEY))) {
                if (request.getParameter(Constants.ITEM_KEY) != null) {
                    userRrn = new Long(request.getParameter(Constants.ITEM_KEY));
                    relation.setTransId(Constants.DELETE_KEY);
                }
            } else {
                String userId = theform.getUserId().trim().toUpperCase();
                User theUser = new User(userId, getNamedSpace(ObjectList.USER_KEY, LocalContext.getFacilityRrn()),
                                        ObjectList.USER_KEY);

                userRrn = baseService.getNamedObjectRrn(theUser);

                // if no such user exist, do nothing
                Assert.isFalse(userRrn <= 0,
                               Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("{} 没有找到对象!")
                                     .args("user").build());

                relation.setTransId(Constants.CREATE_KEY);
            }

            // constructs the relation object
            relation.setFromRrn(userRrn);
            relation.setToRrn(userGroup.getInstanceRrn());
            relation.setLinkType(LinkTypeList.USER_USERGROUP_KEY);

            processDetails(relation);
            userGroup.setUsers(this.getUsers(userGroup));

            theform.setUserId("");

            return (mapping.findForward("modify"));
        } // end of maintain users

        // when inserting or updating, populate the value object for process method
        Long userGroupRrn = userGroup.getInstanceRrn();
        Collection users = userGroup.getUsers();

        PropertyUtils.copyProperties(userGroup, theform);
        userGroup.setUsers(users);

        String dest = "modify";

        if (request.getParameter(Constants.DELETE_KEY) != null) {
            // execute the process method that will understand action
            // thru value object property transId
            userGroup.setTransId(Constants.DELETE_KEY);

            // clear id
            theform.setInstanceId(null);

            dest = "setup";
        } else if (request.getParameter(Constants.CREATE_KEY) != null) {
            userGroup.setTransId(Constants.CREATE_KEY);
        } else if (request.getParameter(Constants.MODIFY_KEY) != null) {
            userGroup.setTransId(Constants.MODIFY_KEY);

            // dest = "setup";
        } else {
            this.processItemAction(request, theform);
            theform.setTransId(Constants.MODIFY_KEY);

            return (mapping.findForward("modify"));
        }

        // no matter add or modify, they all execute the same function
        process(userGroup);
        userGroup.setInstanceRrn(userGroupRrn);
        // differet from other one2many model, label has fixed items
        userGroup.setUsers(this.getUsers(userGroup));

        // just for logic equals tag
        theform.setTransId(Constants.MODIFY_KEY);

        return (mapping.findForward(dest));
    } // End perform

    private void processDetails(Relation relation) throws Exception {
        String action = relation.getTransId();
        if (action.equals(Constants.CREATE_KEY)) {
            // avoid duplication
            if (baseService.getRelation(relation) == null) {

                securityService.addUserToUsergroup(relation);
            }
        } else if (action.equals(Constants.DELETE_KEY)) {

            securityService.removeUserFromGroup(relation);
        }
    } // end of processdetails

}

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