Coverage Report - org.kuali.rice.kim.web.struts.action.IdentityManagementGroupDocumentAction
 
Classes in this File Line Coverage Branch Coverage Complexity
IdentityManagementGroupDocumentAction
0%
0/112
0%
0/64
4.083
 
 1  
 /*
 2  
  * Copyright 2007-2009 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kim.web.struts.action;
 17  
 
 18  
 import java.sql.Timestamp;
 19  
 import java.util.Calendar;
 20  
 import java.util.HashMap;
 21  
 import java.util.Map;
 22  
 
 23  
 import javax.servlet.http.HttpServletRequest;
 24  
 import javax.servlet.http.HttpServletResponse;
 25  
 
 26  
 import org.apache.commons.lang.StringUtils;
 27  
 import org.apache.struts.action.ActionForm;
 28  
 import org.apache.struts.action.ActionForward;
 29  
 import org.apache.struts.action.ActionMapping;
 30  
 import org.kuali.rice.core.util.RiceConstants;
 31  
 import org.kuali.rice.kew.exception.WorkflowException;
 32  
 import org.kuali.rice.kim.bo.Role;
 33  
 import org.kuali.rice.kim.bo.entity.dto.KimPrincipalInfo;
 34  
 import org.kuali.rice.kim.bo.group.dto.GroupInfo;
 35  
 import org.kuali.rice.kim.bo.impl.KimAttributes;
 36  
 import org.kuali.rice.kim.bo.impl.RoleImpl;
 37  
 import org.kuali.rice.kim.bo.role.dto.KimRoleInfo;
 38  
 import org.kuali.rice.kim.bo.types.dto.KimTypeInfo;
 39  
 import org.kuali.rice.kim.bo.ui.GroupDocumentMember;
 40  
 import org.kuali.rice.kim.document.IdentityManagementGroupDocument;
 41  
 import org.kuali.rice.kim.rule.event.ui.AddGroupMemberEvent;
 42  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 43  
 import org.kuali.rice.kim.util.KimConstants;
 44  
 import org.kuali.rice.kim.web.struts.form.IdentityManagementGroupDocumentForm;
 45  
 import org.kuali.rice.kns.bo.BusinessObject;
 46  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 47  
 import org.kuali.rice.kns.util.GlobalVariables;
 48  
 import org.kuali.rice.kns.util.RiceKeyConstants;
 49  
 import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
 50  
 import org.kuali.rice.kns.web.struts.form.KualiTableRenderFormMetadata;
 51  
 
 52  
 /**
 53  
  * 
 54  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 55  
  *
 56  
  */
 57  
 public class IdentityManagementGroupDocumentAction extends IdentityManagementDocumentActionBase {
 58  
 
 59  
         /**
 60  
          * This constructs a ...
 61  
          * 
 62  
          */
 63  
         public IdentityManagementGroupDocumentAction() {
 64  0
                 super();
 65  0
                 addMethodToCallToUncheckedList( CHANGE_MEMBER_TYPE_CODE_METHOD_TO_CALL );
 66  0
                 addMethodToCallToUncheckedList( CHANGE_NAMESPACE_METHOD_TO_CALL );
 67  0
         }
 68  
         
 69  
         @Override
 70  
         public ActionForward execute(ActionMapping mapping, ActionForm form,
 71  
                         HttpServletRequest request, HttpServletResponse response) throws Exception {
 72  0
         IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 73  0
         if ( StringUtils.isBlank( groupDocumentForm.getGroupId() ) ) {
 74  0
             String groupId = request.getParameter(KimConstants.PrimaryKeyConstants.GROUP_ID);
 75  0
                 groupDocumentForm.setGroupId(groupId);
 76  
         }
 77  0
                 String kimTypeId = request.getParameter(KimConstants.PrimaryKeyConstants.KIM_TYPE_ID);
 78  0
         setKimType(kimTypeId, groupDocumentForm);
 79  
 
 80  
         
 81  0
                 KualiTableRenderFormMetadata memberTableMetadata = groupDocumentForm.getMemberTableMetadata();
 82  0
                 if (groupDocumentForm.getMemberRows() != null) {
 83  0
                         memberTableMetadata.jumpToPage(memberTableMetadata.getViewedPageNumber(), groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
 84  
                 }
 85  
                 
 86  0
                 ActionForward forward = super.execute(mapping, groupDocumentForm, request, response);
 87  
                 
 88  0
                 groupDocumentForm.setCanAssignGroup(validAssignGroup(groupDocumentForm.getGroupDocument()));
 89  0
                 return forward;
 90  
     }
 91  
     
 92  
     protected void setKimType(String kimTypeId, IdentityManagementGroupDocumentForm groupDocumentForm){
 93  0
                 if ( StringUtils.isNotBlank(kimTypeId) ) {
 94  0
                         KimTypeInfo kType = KIMServiceLocator.getTypeInfoService().getKimType(kimTypeId);
 95  0
                         groupDocumentForm.setKimType(kType);
 96  0
                         if (groupDocumentForm.getGroupDocument() != null) {
 97  0
                                 groupDocumentForm.getGroupDocument().setKimType(kType);
 98  
                         }
 99  0
                 } else if ( groupDocumentForm.getGroupDocument() != null && StringUtils.isNotBlank(groupDocumentForm.getGroupDocument().getGroupTypeId() ) ) {
 100  0
                         groupDocumentForm.setKimType(KIMServiceLocator.getTypeInfoService().getKimType(
 101  
                                         groupDocumentForm.getGroupDocument().getGroupTypeId()));
 102  0
                         groupDocumentForm.getGroupDocument().setKimType(groupDocumentForm.getKimType());
 103  
                 }
 104  0
     }
 105  
     
 106  
     /**
 107  
      * This overridden method ...
 108  
      * 
 109  
      * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#loadDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
 110  
      */
 111  
     @Override
 112  
     protected void loadDocument(KualiDocumentFormBase form)
 113  
                     throws WorkflowException {
 114  0
             super.loadDocument(form);
 115  
             
 116  0
             IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 117  0
         setKimType(groupDocumentForm.getGroupId(), groupDocumentForm);
 118  0
         groupDocumentForm.setMember(groupDocumentForm.getGroupDocument().getBlankMember());
 119  
 
 120  0
                 KualiTableRenderFormMetadata memberTableMetadata = groupDocumentForm.getMemberTableMetadata();
 121  0
                 if (groupDocumentForm.getMemberRows() != null) {
 122  0
                     memberTableMetadata.jumpToFirstPage(groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
 123  
                 }
 124  0
     }
 125  
     
 126  
     /**
 127  
      * This overridden method ...
 128  
      * 
 129  
      * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#createDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
 130  
      */
 131  
     @Override
 132  
     protected void createDocument(KualiDocumentFormBase form)
 133  
                     throws WorkflowException {
 134  0
             super.createDocument(form);
 135  0
             IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 136  0
             if ( groupDocumentForm.getGroupId() == null ) {
 137  0
                     groupDocumentForm.getGroupDocument().setKimType(groupDocumentForm.getKimType());
 138  0
                     groupDocumentForm.getGroupDocument().initializeDocumentForNewGroup();
 139  0
                     groupDocumentForm.setGroupId( groupDocumentForm.getGroupDocument().getGroupId() );
 140  0
                     setKimType(groupDocumentForm.getGroupDocument().getGroupTypeId(), groupDocumentForm);
 141  
             } else {
 142  0
                     loadGroupIntoDocument( groupDocumentForm.getGroupId(), groupDocumentForm );
 143  
             }
 144  0
                 KualiTableRenderFormMetadata memberTableMetadata = groupDocumentForm.getMemberTableMetadata();
 145  0
                 if (groupDocumentForm.getMemberRows() != null) {
 146  0
                     memberTableMetadata.jumpToFirstPage(groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
 147  
                 }
 148  0
     }
 149  
 
 150  
 
 151  
     protected void loadGroupIntoDocument( String groupId, IdentityManagementGroupDocumentForm groupDocumentForm){
 152  0
         GroupInfo group = KIMServiceLocator.getGroupService().getGroupInfo(groupId);
 153  0
         getUiDocumentService().loadGroupDoc(groupDocumentForm.getGroupDocument(), group);
 154  0
     }    
 155  
         
 156  
         /***
 157  
          * @see org.kuali.rice.kim.web.struts.action.IdentityManagementDocumentActionBase#getActionName()
 158  
          */
 159  
         public String getActionName(){
 160  0
                 return KimConstants.KimUIConstants.KIM_GROUP_DOCUMENT_ACTION;
 161  
         }
 162  
         
 163  
         protected boolean validAssignGroup(IdentityManagementGroupDocument document){
 164  0
         boolean rulePassed = true;
 165  0
         Map<String,String> additionalPermissionDetails = new HashMap<String,String>();
 166  0
         if (!StringUtils.isEmpty(document.getGroupNamespace())) {
 167  0
                 additionalPermissionDetails.put(KimAttributes.NAMESPACE_CODE, document.getGroupNamespace());
 168  0
                 additionalPermissionDetails.put(KimAttributes.GROUP_NAME, document.getGroupName());
 169  0
                 if (!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
 170  
                                 document, 
 171  
                                 KimConstants.NAMESPACE_CODE, 
 172  
                                 KimConstants.PermissionTemplateNames.POPULATE_GROUP, 
 173  
                                 GlobalVariables.getUserSession().getPrincipalId(), 
 174  
                                 additionalPermissionDetails, null)){
 175  0
                         rulePassed = false;
 176  
                 }
 177  
         }
 178  0
                 return rulePassed;
 179  
         }
 180  
 
 181  
         public ActionForward changeMemberTypeCode(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 182  0
                 IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 183  0
         groupDocumentForm.getMember().setMemberName("");
 184  0
         return refresh(mapping, groupDocumentForm, request, response);
 185  
         }        
 186  
         
 187  
     public ActionForward addMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 188  0
         IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 189  0
         GroupDocumentMember newMember = groupDocumentForm.getMember();
 190  
         
 191  
         //See if possible to add with just Group Details filled in (not returned from lookup)
 192  0
         if (StringUtils.isEmpty(newMember.getMemberId()) 
 193  
                         && StringUtils.isNotEmpty(newMember.getMemberName())
 194  
                         && StringUtils.isNotEmpty(newMember.getMemberNamespaceCode())
 195  
                         && StringUtils.equals(newMember.getMemberTypeCode(), KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) {
 196  0
                 GroupInfo tempGroup = KIMServiceLocator.getIdentityManagementService().getGroupByName(newMember.getMemberNamespaceCode(), newMember.getMemberName());
 197  0
                 if (tempGroup != null) {
 198  0
                         newMember.setMemberId(tempGroup.getGroupId());
 199  
                 }
 200  
         }
 201  
         
 202  
         //See if possible to grab details for Principal
 203  0
         if (StringUtils.isEmpty(newMember.getMemberId()) 
 204  
                         && StringUtils.isNotEmpty(newMember.getMemberName())
 205  
                         && StringUtils.equals(newMember.getMemberTypeCode(), KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE)) {
 206  0
                 KimPrincipalInfo principal = KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(newMember.getMemberName());
 207  0
                 if (principal != null) {
 208  0
                         newMember.setMemberId(principal.getPrincipalId());
 209  
                 }
 210  
         }
 211  0
         if(checkKimDocumentGroupMember(newMember) && 
 212  
                         KNSServiceLocator.getKualiRuleService().applyRules(new AddGroupMemberEvent("", groupDocumentForm.getGroupDocument(), newMember))){
 213  0
                 newMember.setDocumentNumber(groupDocumentForm.getDocument().getDocumentNumber());
 214  0
                 groupDocumentForm.getGroupDocument().addMember(newMember);
 215  0
                 groupDocumentForm.setMember(groupDocumentForm.getGroupDocument().getBlankMember());
 216  0
                 groupDocumentForm.getMemberTableMetadata().jumpToLastPage(groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
 217  
        }
 218  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 219  
     }
 220  
 
 221  
     protected boolean checkKimDocumentGroupMember(GroupDocumentMember newMember){
 222  0
         if(StringUtils.isBlank(newMember.getMemberTypeCode()) || StringUtils.isBlank(newMember.getMemberId())){
 223  0
                 GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_EMPTY_ENTRY,
 224  
                                 new String[] {"Member Type Code and Member ID"});
 225  0
                 return false;
 226  
                 }
 227  0
             if(KimConstants.KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE.equals(newMember.getMemberTypeCode())){
 228  0
                 KimPrincipalInfo principalInfo = null;
 229  0
                 principalInfo = getIdentityManagementService().getPrincipal(newMember.getMemberId());
 230  0
                 if (principalInfo == null) {
 231  0
                         GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
 232  
                                     new String[] {newMember.getMemberId()});
 233  0
                     return false;
 234  
                 }
 235  
                 else {
 236  0
                         newMember.setMemberName(principalInfo.getPrincipalName());
 237  
                 }
 238  0
         } else if(KimConstants.KimUIConstants.MEMBER_TYPE_GROUP_CODE.equals(newMember.getMemberTypeCode())){
 239  0
                 GroupInfo groupInfo = null;
 240  0
                 groupInfo = getIdentityManagementService().getGroup(newMember.getMemberId());
 241  0
                 if (groupInfo == null) {
 242  0
                         GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
 243  
                                     new String[] {newMember.getMemberId()});
 244  0
                     return false;
 245  
                 }
 246  
                 else {
 247  0
                         newMember.setMemberName(groupInfo.getGroupName());
 248  0
                 newMember.setMemberNamespaceCode(groupInfo.getNamespaceCode());
 249  
                 }
 250  0
         } else if(KimConstants.KimUIConstants.MEMBER_TYPE_ROLE_CODE.equals(newMember.getMemberTypeCode())){
 251  0
                 KimRoleInfo roleInfo = null;
 252  0
                 roleInfo = KIMServiceLocator.getRoleService().getRole(newMember.getMemberId());   
 253  0
                 if (roleInfo == null) {
 254  0
                         GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
 255  
                                     new String[] {newMember.getMemberId()});
 256  0
                     return false;
 257  
                 }
 258  0
                 else if(StringUtils.equals(newMember.getMemberTypeCode(), KimConstants.KimUIConstants.MEMBER_TYPE_ROLE_CODE) 
 259  
                             && !validateRole(newMember.getMemberId(), (Role)roleInfo, "document.member.memberId", "Role")){
 260  0
                     return false;
 261  
             }
 262  
                 else {
 263  0
                         newMember.setMemberName(roleInfo.getRoleName());
 264  0
                 newMember.setMemberNamespaceCode(roleInfo.getNamespaceCode());
 265  
                 }
 266  
                 }
 267  0
         return true;
 268  
     }
 269  
     
 270  
     public ActionForward deleteMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 271  0
         IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 272  0
         GroupDocumentMember memberToBeInactivated = groupDocumentForm.getGroupDocument().getMembers().get(getLineToDelete(request));    
 273  0
         Calendar cal = Calendar.getInstance();
 274  0
         memberToBeInactivated.setActiveToDate(new Timestamp(cal.getTimeInMillis()));        
 275  0
         groupDocumentForm.getGroupDocument().getMembers().set(getLineToDelete(request), memberToBeInactivated);   
 276  0
         groupDocumentForm.setMember(groupDocumentForm.getGroupDocument().getBlankMember());
 277  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 278  
     }
 279  
     
 280  
 }