Coverage Report - org.kuali.rice.kim.web.struts.action.IdentityManagementRoleDocumentAction
 
Classes in this File Line Coverage Branch Coverage Complexity
IdentityManagementRoleDocumentAction
0%
0/267
0%
0/166
4.556
 
 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 org.apache.commons.lang.StringUtils;
 19  
 import org.apache.struts.action.ActionForm;
 20  
 import org.apache.struts.action.ActionForward;
 21  
 import org.apache.struts.action.ActionMapping;
 22  
 import org.kuali.rice.core.util.RiceConstants;
 23  
 import org.kuali.rice.core.util.RiceKeyConstants;
 24  
 import org.kuali.rice.kew.exception.WorkflowException;
 25  
 import org.kuali.rice.kim.api.entity.principal.Principal;
 26  
 import org.kuali.rice.kim.api.group.Group;
 27  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 28  
 import org.kuali.rice.kim.bo.Role;
 29  
 
 30  
 
 31  
 import org.kuali.rice.kim.bo.role.dto.KimRoleInfo;
 32  
 import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
 33  
 import org.kuali.rice.kim.bo.ui.KimDocumentRolePermission;
 34  
 import org.kuali.rice.kim.bo.ui.KimDocumentRoleQualifier;
 35  
 import org.kuali.rice.kim.bo.ui.KimDocumentRoleResponsibility;
 36  
 import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMember;
 37  
 import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMemberQualifier;
 38  
 import org.kuali.rice.kim.document.IdentityManagementRoleDocument;
 39  
 import org.kuali.rice.kim.impl.responsibility.AddResponsibilityEvent;
 40  
 import org.kuali.rice.kim.impl.responsibility.ResponsibilityBo;
 41  
 import org.kuali.rice.kim.lookup.KimTypeLookupableHelperServiceImpl;
 42  
 import org.kuali.rice.kim.rule.event.ui.AddDelegationMemberEvent;
 43  
 import org.kuali.rice.kim.rule.event.ui.AddMemberEvent;
 44  
 import org.kuali.rice.kim.rule.event.ui.AddPermissionEvent;
 45  
 import org.kuali.rice.kim.util.KimConstants;
 46  
 import org.kuali.rice.kim.web.struts.form.IdentityManagementRoleDocumentForm;
 47  
 import org.kuali.rice.kns.document.Document;
 48  
 import org.kuali.rice.kns.question.ConfirmationQuestion;
 49  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 50  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 51  
 import org.kuali.rice.kns.util.GlobalVariables;
 52  
 import org.kuali.rice.kns.util.KNSConstants;
 53  
 import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
 54  
 import org.kuali.rice.kns.web.struts.form.KualiTableRenderFormMetadata;
 55  
 import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
 56  
 
 57  
 import javax.servlet.http.HttpServletRequest;
 58  
 import javax.servlet.http.HttpServletResponse;
 59  
 import java.sql.Timestamp;
 60  
 import java.util.ArrayList;
 61  
 import java.util.Arrays;
 62  
 import java.util.Calendar;
 63  
 import java.util.HashMap;
 64  
 import java.util.List;
 65  
 import java.util.Map;
 66  
 
 67  
 /**
 68  
  *
 69  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 70  
  *
 71  
  */
 72  
 public class IdentityManagementRoleDocumentAction extends IdentityManagementDocumentActionBase {
 73  
 
 74  
         public static final String CHANGE_DEL_ROLE_MEMBER_METHOD_TO_CALL = "changeDelegationRoleMember";
 75  
         public static final String SWITCH_TO_ROLE_MEMBER_METHOD_TO_CALL = "jumpToRoleMember";
 76  
         public static final String REMOVE_AFFECTED_DELEGATES_QUESTION_ID = "RemoveAffectedDelegates";
 77  
 
 78  0
         protected List<String> methodToCallToUncheckedList = new ArrayList<String>();
 79  
         {
 80  0
                 methodToCallToUncheckedList.add(CHANGE_DEL_ROLE_MEMBER_METHOD_TO_CALL);
 81  0
                 methodToCallToUncheckedList.add(CHANGE_MEMBER_TYPE_CODE_METHOD_TO_CALL);
 82  0
                 methodToCallToUncheckedList.add(CHANGE_NAMESPACE_METHOD_TO_CALL);
 83  0
                 methodToCallToUncheckedList.add(SWITCH_TO_ROLE_MEMBER_METHOD_TO_CALL);
 84  
         }
 85  
         /**
 86  
          * This constructs a ...
 87  
          *
 88  
          */
 89  
         public IdentityManagementRoleDocumentAction() {
 90  0
                 super();
 91  0
                 for(String methodToCallToUncheck: methodToCallToUncheckedList)
 92  0
                         addMethodToCallToUncheckedList(methodToCallToUncheck);
 93  0
         }
 94  
 
 95  
     public ActionForward sort(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 96  0
         IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
 97  0
         this.createDocument(roleDocumentForm);
 98  
 
 99  
         // mimic loading the document; consider calling the loadDocument method instead
 100  0
         Document document = roleDocumentForm.getDocument();
 101  0
         KualiWorkflowDocument workflowDocument = roleDocumentForm.getDocument().getDocumentHeader().getWorkflowDocument();
 102  0
         roleDocumentForm.populateHeaderFields(workflowDocument);
 103  0
         roleDocumentForm.setDocId(document.getDocumentNumber());
 104  0
         roleDocumentForm.setCanAssignRole(validAssignRole(roleDocumentForm.getRoleDocument()));
 105  
 
 106  0
         if (KimTypeLookupableHelperServiceImpl.hasDerivedRoleTypeService(roleDocumentForm.getRoleDocument().getKimType())) {
 107  0
             roleDocumentForm.setCanModifyAssignees(false);
 108  
         }
 109  0
         GlobalVariables.getUserSession().addObject(KimConstants.KimUIConstants.KIM_ROLE_DOCUMENT_SHORT_KEY, roleDocumentForm.getRoleDocument());
 110  0
         return refresh(mapping, roleDocumentForm, request, response);
 111  
     }
 112  
 
 113  
         @Override
 114  
         public ActionForward execute(ActionMapping mapping, ActionForm form,
 115  
                         HttpServletRequest request, HttpServletResponse response) throws Exception {
 116  0
         IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
 117  0
         if ( roleDocumentForm.getRoleId() == null ) {
 118  0
             String roleId = request.getParameter(KimConstants.PrimaryKeyConstants.ROLE_ID);
 119  0
                 roleDocumentForm.setRoleId(roleId);
 120  
         }
 121  
         
 122  0
                 String kimTypeId = request.getParameter(KimConstants.PrimaryKeyConstants.KIM_TYPE_ID);
 123  
                 // TODO: move this into the UI service - action should not be making ORM-layer calls
 124  0
                 setKimType(kimTypeId, roleDocumentForm);
 125  
 
 126  0
                 KualiTableRenderFormMetadata memberTableMetadata = roleDocumentForm.getMemberTableMetadata();
 127  0
                 if (roleDocumentForm.getRoleDocument()!=null && roleDocumentForm.getMemberRows() != null) {
 128  0
                         memberTableMetadata.jumpToPage(memberTableMetadata.getViewedPageNumber(), roleDocumentForm.getMemberRows().size(), roleDocumentForm.getRecordsPerPage());
 129  
                 }
 130  
 
 131  
                 // KULRICE-4762: active delegates of "inactivated" role members cause validation problems
 132  0
                 ActionForward forward = promptForAffectedDelegates(mapping, form, request, response,
 133  
                                 roleDocumentForm);
 134  
                 // if we need to prompt the user due to affected delegates, do so:
 135  0
                 if (forward != null) return forward;
 136  
 
 137  0
                 forward = super.execute(mapping, roleDocumentForm, request, response);
 138  
 
 139  0
                 roleDocumentForm.setCanAssignRole(validAssignRole(roleDocumentForm.getRoleDocument()));
 140  0
                 if(KimTypeLookupableHelperServiceImpl.hasDerivedRoleTypeService(roleDocumentForm.getRoleDocument().getKimType())) {
 141  0
                         roleDocumentForm.setCanModifyAssignees(false);
 142  
                 }
 143  0
                 GlobalVariables.getUserSession().addObject(KimConstants.KimUIConstants.KIM_ROLE_DOCUMENT_SHORT_KEY, roleDocumentForm.getRoleDocument());
 144  0
                 return forward;
 145  
     }
 146  
 
 147  
     /**
 148  
      * This overridden method ...
 149  
      *
 150  
      * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#loadDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
 151  
      */
 152  
     @Override
 153  
     protected void loadDocument(KualiDocumentFormBase form)
 154  
                     throws WorkflowException {
 155  0
             super.loadDocument(form);
 156  
 
 157  0
             IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
 158  0
             setKimType(roleDocumentForm.getRoleDocument().getRoleTypeId(), roleDocumentForm);
 159  0
             getUiDocumentService().setDelegationMembersInDocument( roleDocumentForm.getRoleDocument() );
 160  
 
 161  0
         roleDocumentForm.setMember(roleDocumentForm.getRoleDocument().getBlankMember());
 162  0
         roleDocumentForm.setDelegationMember(roleDocumentForm.getRoleDocument().getBlankDelegationMember());
 163  
 
 164  0
                 KualiTableRenderFormMetadata memberTableMetadata = roleDocumentForm.getMemberTableMetadata();
 165  0
                 if (roleDocumentForm.getMemberRows() != null) {
 166  0
                     memberTableMetadata.jumpToFirstPage(roleDocumentForm.getMemberRows().size(), roleDocumentForm.getRecordsPerPage());
 167  
                 }
 168  0
     }
 169  
 
 170  
     /**
 171  
      * This overridden method ...
 172  
      *
 173  
      * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#createDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
 174  
      */
 175  
     @Override
 176  
     protected void createDocument(KualiDocumentFormBase form)
 177  
                     throws WorkflowException {
 178  0
             super.createDocument(form);
 179  0
             IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
 180  
 
 181  0
             if ( roleDocumentForm.getRoleId() == null ) {
 182  0
                     roleDocumentForm.getRoleDocument().setKimType(roleDocumentForm.getKimType());
 183  0
                     roleDocumentForm.getRoleDocument().initializeDocumentForNewRole();
 184  0
                     roleDocumentForm.setRoleId( roleDocumentForm.getRoleDocument().getRoleId() );
 185  0
             roleDocumentForm.setKimType(KimApiServiceLocator.getKimTypeInfoService().getKimType(roleDocumentForm.getRoleDocument().getRoleTypeId()));
 186  
             } else {
 187  0
                     loadRoleIntoDocument( roleDocumentForm.getRoleId(), roleDocumentForm );
 188  
             }
 189  
 
 190  0
             roleDocumentForm.setMember(roleDocumentForm.getRoleDocument().getBlankMember());
 191  0
         roleDocumentForm.setDelegationMember(roleDocumentForm.getRoleDocument().getBlankDelegationMember());
 192  
 
 193  0
                 KualiTableRenderFormMetadata memberTableMetadata = roleDocumentForm.getMemberTableMetadata();
 194  0
                 if (roleDocumentForm.getMemberRows() != null) {
 195  0
                     memberTableMetadata.jumpToFirstPage(roleDocumentForm.getMemberRows().size(), roleDocumentForm.getRecordsPerPage());
 196  
                 }
 197  0
     }
 198  
 
 199  
     protected void setKimType(String kimTypeId, IdentityManagementRoleDocumentForm roleDocumentForm){
 200  0
                 if ( StringUtils.isNotBlank(kimTypeId) ) {
 201  0
             roleDocumentForm.setKimType(KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId));
 202  0
             if (roleDocumentForm.getRoleDocument() != null) {
 203  0
                     roleDocumentForm.getRoleDocument().setKimType(roleDocumentForm.getKimType());
 204  
             }
 205  0
                 } else if ( roleDocumentForm.getRoleDocument() != null && StringUtils.isNotBlank( roleDocumentForm.getRoleDocument().getRoleTypeId() ) ) {
 206  0
             roleDocumentForm.setKimType(KimApiServiceLocator.getKimTypeInfoService().getKimType(
 207  
                             roleDocumentForm.getRoleDocument().getRoleTypeId()));
 208  0
                 roleDocumentForm.getRoleDocument().setKimType(roleDocumentForm.getKimType());
 209  
                 }
 210  0
     }
 211  
 
 212  
     protected void loadRoleIntoDocument( String roleId, IdentityManagementRoleDocumentForm roleDocumentForm){
 213  0
         KimRoleInfo role = KimApiServiceLocator.getRoleService().getRole(roleId);
 214  0
         roleDocumentForm.getRoleDocument().setMemberMetaDataTypeToSort(roleDocumentForm.getMemberTableMetadata().getColumnToSortIndex());
 215  0
         getUiDocumentService().loadRoleDoc(roleDocumentForm.getRoleDocument(), role);
 216  0
     }
 217  
 
 218  
         /***
 219  
          * @see org.kuali.rice.kim.web.struts.action.IdentityManagementDocumentActionBase#getActionName()
 220  
          */
 221  
         public String getActionName(){
 222  0
                 return KimConstants.KimUIConstants.KIM_ROLE_DOCUMENT_ACTION;
 223  
         }
 224  
 
 225  
         protected boolean validAssignRole(IdentityManagementRoleDocument document){
 226  0
         boolean rulePassed = true;
 227  0
         if(StringUtils.isNotEmpty(document.getRoleNamespace())){
 228  0
                 Map<String,String> additionalPermissionDetails = new HashMap<String,String>();
 229  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, document.getRoleNamespace());
 230  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.ROLE_NAME, document.getRoleName());
 231  0
                         if (!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
 232  
                                         document,
 233  
                                         KimConstants.NAMESPACE_CODE,
 234  
                                         KimConstants.PermissionTemplateNames.ASSIGN_ROLE,
 235  
                                         GlobalVariables.getUserSession().getPrincipalId(),
 236  
                                         additionalPermissionDetails, null)){
 237  0
                     rulePassed = false;
 238  
                         }
 239  
         }
 240  0
                 return rulePassed;
 241  
         }
 242  
 
 243  
         public ActionForward changeMemberTypeCode(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 244  0
                 IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
 245  0
         roleDocumentForm.getMember().setMemberId("");
 246  0
         return refresh(mapping, roleDocumentForm, request, response);
 247  
         }
 248  
 
 249  
         public ActionForward changeDelegationMemberTypeCode(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 250  0
                 IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
 251  0
                 KimDocumentRoleMember roleMember = roleDocumentForm.getRoleDocument().getMember(roleDocumentForm.getDelegationMember().getRoleMemberId());
 252  0
                 if(roleMember!=null){
 253  
                         RoleDocumentDelegationMemberQualifier delegationMemberQualifier;
 254  0
                         for(KimDocumentRoleQualifier roleQualifier: roleMember.getQualifiers()){
 255  0
                                 delegationMemberQualifier = roleDocumentForm.getDelegationMember().getQualifier(roleQualifier.getKimAttrDefnId());
 256  0
                                 delegationMemberQualifier.setAttrVal(roleQualifier.getAttrVal());
 257  
                         }
 258  
                 }
 259  0
         return refresh(mapping, roleDocumentForm, request, response);
 260  
         }
 261  
 
 262  
     public ActionForward addResponsibility(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 263  0
         IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
 264  0
         KimDocumentRoleResponsibility newResponsibility = roleDocumentForm.getResponsibility();
 265  0
         if(newResponsibility!=null && StringUtils.isNotBlank(newResponsibility.getResponsibilityId())){
 266  0
                 Map<String, String> criteria = new HashMap<String, String>();
 267  0
                 criteria.put(KimConstants.PrimaryKeyConstants.RESPONSIBILITY_ID, newResponsibility.getResponsibilityId());
 268  0
                 ResponsibilityBo responsibilityImpl = KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(ResponsibilityBo.class, criteria);
 269  0
                 newResponsibility.setKimResponsibility(responsibilityImpl);
 270  
         }
 271  
 
 272  0
         if (KNSServiceLocatorWeb.getKualiRuleService().applyRules(new AddResponsibilityEvent("",roleDocumentForm.getRoleDocument(), newResponsibility))) {
 273  0
             if (newResponsibility != null) {
 274  0
                 newResponsibility.setDocumentNumber(roleDocumentForm.getDocument().getDocumentNumber());
 275  
             }
 276  0
             roleDocumentForm.getRoleDocument().addResponsibility(newResponsibility);
 277  0
                 roleDocumentForm.setResponsibility(new KimDocumentRoleResponsibility());
 278  0
                 roleDocumentForm.getRoleDocument().updateMembers(newResponsibility);
 279  
         }
 280  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 281  
     }
 282  
 
 283  
     public ActionForward deleteResponsibility(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 284  0
         IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
 285  0
         roleDocumentForm.getRoleDocument().getResponsibilities().remove(getLineToDelete(request));
 286  0
         roleDocumentForm.getRoleDocument().updateMembers(roleDocumentForm);
 287  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 288  
     }
 289  
 
 290  
     public ActionForward addPermission(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 291  0
         IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
 292  0
         KimDocumentRolePermission newPermission = roleDocumentForm.getPermission();
 293  0
         if (KNSServiceLocatorWeb.getKualiRuleService().applyRules(new AddPermissionEvent("", roleDocumentForm.getRoleDocument(), newPermission))) {
 294  0
                 newPermission.setDocumentNumber(roleDocumentForm.getDocument().getDocumentNumber());
 295  0
                 newPermission.setRoleId(roleDocumentForm.getRoleDocument().getRoleId());
 296  0
                 roleDocumentForm.getRoleDocument().getPermissions().add(newPermission);
 297  0
                 roleDocumentForm.setPermission(new KimDocumentRolePermission());
 298  
         }
 299  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 300  
     }
 301  
 
 302  
     public ActionForward addMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 303  0
         IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
 304  0
         KimDocumentRoleMember newMember = roleDocumentForm.getMember();
 305  
 
 306  
         //See if possible to add with just Group Details filled in (not returned from lookup)
 307  0
         if (StringUtils.isEmpty(newMember.getMemberId())
 308  
                         && StringUtils.isNotEmpty(newMember.getMemberName())
 309  
                         && StringUtils.isNotEmpty(newMember.getMemberNamespaceCode())
 310  
                         && StringUtils.equals(newMember.getMemberTypeCode(), KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) {
 311  0
                 Group tempGroup = KimApiServiceLocator.getIdentityManagementService().getGroupByName(newMember.getMemberNamespaceCode(), newMember.getMemberName());
 312  0
                 if (tempGroup != null) {
 313  0
                         newMember.setMemberId(tempGroup.getId());
 314  
                 }
 315  
         }
 316  
 
 317  
         //See if possible to grab details for Principal
 318  0
         if (StringUtils.isEmpty(newMember.getMemberId())
 319  
                         && StringUtils.isNotEmpty(newMember.getMemberName())
 320  
                         && StringUtils.equals(newMember.getMemberTypeCode(), KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE)) {
 321  0
                 Principal principal = KimApiServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(newMember.getMemberName());
 322  0
                 if (principal != null) {
 323  0
                         newMember.setMemberId(principal.getPrincipalId());
 324  
                 }
 325  
         }
 326  0
         if(checkKimDocumentRoleMember(newMember) &&
 327  
                         KNSServiceLocatorWeb.getKualiRuleService().applyRules(new AddMemberEvent("", roleDocumentForm.getRoleDocument(), newMember))){
 328  0
                 newMember.setDocumentNumber(roleDocumentForm.getDocument().getDocumentNumber());
 329  0
                 roleDocumentForm.getRoleDocument().addMember(newMember);
 330  0
                 roleDocumentForm.setMember(roleDocumentForm.getRoleDocument().getBlankMember());
 331  0
                 roleDocumentForm.getMemberTableMetadata().jumpToLastPage(roleDocumentForm.getMemberRows().size(), roleDocumentForm.getRecordsPerPage());
 332  
         }
 333  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 334  
     }
 335  
 
 336  
     protected boolean checkKimDocumentRoleMember(KimDocumentRoleMember newMember){
 337  0
             boolean memberExists = false;
 338  0
         String memberName = null;
 339  0
         String memberNamespace = null;
 340  
 
 341  0
         if(StringUtils.isBlank(newMember.getMemberTypeCode()) || StringUtils.isBlank(newMember.getMemberId())){
 342  0
                 GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_EMPTY_ENTRY,
 343  
                                 new String[] {"Member Type Code and Member ID"});
 344  0
                 return false;
 345  
                 }
 346  
 
 347  0
         if(KimConstants.KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE.equals(newMember.getMemberTypeCode())){
 348  0
                 Principal pi = this.getIdentityService().getPrincipal(newMember.getMemberId());
 349  0
                 if(pi != null){
 350  0
                         memberExists = true;
 351  0
                         memberName = pi.getPrincipalName();
 352  0
                         memberNamespace = "";
 353  
                 }
 354  0
         }else if(KimConstants.KimUIConstants.MEMBER_TYPE_GROUP_CODE.equals(newMember.getMemberTypeCode())){
 355  0
                 Group gi = KimApiServiceLocator.getGroupService().getGroup(newMember.getMemberId());
 356  0
                 if(gi != null){
 357  0
                         memberExists = true;
 358  0
                         memberName = gi.getName();
 359  0
                         memberNamespace = gi.getNamespaceCode();
 360  
                 }
 361  0
         } else if(KimConstants.KimUIConstants.MEMBER_TYPE_ROLE_CODE.equals(newMember.getMemberTypeCode())){
 362  0
                 Role ri = KimApiServiceLocator.getRoleService().getRole(newMember.getMemberId());
 363  0
                 if(!validateRole(newMember.getMemberId(), ri, "document.member.memberId", "Role")){
 364  0
                 return false;
 365  
                     }else{
 366  0
                             memberExists = true;
 367  0
                            memberName = ri.getRoleName();
 368  0
                            memberNamespace = ri.getNamespaceCode();
 369  
         }
 370  
         }
 371  
 
 372  0
         if(!memberExists){
 373  0
                 GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
 374  
                                 new String[] {newMember.getMemberId()});
 375  0
                 return false;
 376  
                 }
 377  0
         newMember.setMemberName(memberName);
 378  0
         newMember.setMemberNamespaceCode(memberNamespace);
 379  0
         return true;
 380  
     }
 381  
 
 382  
     public ActionForward deleteMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 383  0
         IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
 384  0
         KimDocumentRoleMember inactivatedRoleMember = roleDocumentForm.getRoleDocument().getMembers().get(getLineToDelete(request));
 385  
 
 386  
                 // KULRICE-4762: active delegates of "inactivated" role members cause validation problems
 387  0
                 ActionForward forward = promptForAffectedDelegates(mapping, form, request, response,
 388  
                                 roleDocumentForm, /* we haven't actually inactivated them yet, so specify them here */ inactivatedRoleMember);
 389  
                 // if we need to prompt the user due to affected delegates, do so:
 390  0
                 if (forward != null) return forward;
 391  
 
 392  0
         Calendar cal = Calendar.getInstance();
 393  0
         inactivatedRoleMember.setActiveToDate(new Timestamp(cal.getTimeInMillis()));
 394  
 
 395  0
         roleDocumentForm.getRoleDocument().getMembers().set(getLineToDelete(request), inactivatedRoleMember);
 396  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 397  
     }
 398  
 
 399  
     public ActionForward deletePermission(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 400  0
         IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
 401  0
         roleDocumentForm.getRoleDocument().getPermissions().remove(getLineToDelete(request));
 402  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 403  
     }
 404  
 
 405  
     protected boolean checkDelegationMember(RoleDocumentDelegationMember newMember){
 406  0
         if(StringUtils.isBlank(newMember.getMemberTypeCode()) || StringUtils.isBlank(newMember.getMemberId())){
 407  0
                 GlobalVariables.getMessageMap().putError("document.delegationMember.memberId", RiceKeyConstants.ERROR_EMPTY_ENTRY,
 408  
                                 new String[] {"Member Type Code and Member ID"});
 409  0
                 return false;
 410  
                 }
 411  0
             if(KimConstants.KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE.equals(newMember.getMemberTypeCode())){
 412  0
                 Principal principalInfo = getIdentityManagementService().getPrincipal(newMember.getMemberId());
 413  0
                 if (principalInfo == null) {
 414  0
                         GlobalVariables.getMessageMap().putError("document.delegationMember.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
 415  
                                     new String[] {newMember.getMemberId()});
 416  0
                     return false;
 417  
                 }
 418  
                 else {
 419  0
                         newMember.setMemberName(principalInfo.getPrincipalName());
 420  
                 }
 421  0
         } else if(KimConstants.KimUIConstants.MEMBER_TYPE_GROUP_CODE.equals(newMember.getMemberTypeCode())){
 422  0
                 Group groupInfo = null;
 423  0
                 groupInfo = getIdentityManagementService().getGroup(newMember.getMemberId());
 424  0
                 if (groupInfo == null) {
 425  0
                         GlobalVariables.getMessageMap().putError("document.delegationMember.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
 426  
                                     new String[] {newMember.getMemberId()});
 427  0
                     return false;
 428  
                 }
 429  
                 else {
 430  0
                         newMember.setMemberName(groupInfo.getName());
 431  0
                 newMember.setMemberNamespaceCode(groupInfo.getNamespaceCode());
 432  
                 }
 433  0
         } else if(KimConstants.KimUIConstants.MEMBER_TYPE_ROLE_CODE.equals(newMember.getMemberTypeCode())){
 434  0
                 KimRoleInfo roleInfo = null;
 435  0
                 roleInfo = KimApiServiceLocator.getRoleService().getRole(newMember.getMemberId());
 436  0
                 if (roleInfo == null) {
 437  0
                         GlobalVariables.getMessageMap().putError("document.delegationMember.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
 438  
                                     new String[] {newMember.getMemberId()});
 439  0
                     return false;
 440  
                 }
 441  
                 else {
 442  0
                         newMember.setMemberName(roleInfo.getRoleName());
 443  0
                 newMember.setMemberNamespaceCode(roleInfo.getNamespaceCode());
 444  
                 }
 445  
                 }
 446  0
         return true;
 447  
     }
 448  
 
 449  
     public ActionForward addDelegationMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 450  0
         IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
 451  0
         RoleDocumentDelegationMember newDelegationMember = roleDocumentForm.getDelegationMember();
 452  
 
 453  
       //See if possible to add with just Group Details filled in (not returned from lookup)
 454  0
         if (StringUtils.isEmpty(newDelegationMember.getMemberId())
 455  
                         && StringUtils.isNotEmpty(newDelegationMember.getMemberName())
 456  
                         && StringUtils.isNotEmpty(newDelegationMember.getMemberNamespaceCode())
 457  
                         && StringUtils.equals(newDelegationMember.getMemberTypeCode(), KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) {
 458  0
                 Group tempGroup = KimApiServiceLocator.getIdentityManagementService().getGroupByName(newDelegationMember.getMemberNamespaceCode(), newDelegationMember.getMemberName());
 459  0
                 if (tempGroup != null) {
 460  0
                         newDelegationMember.setMemberId(tempGroup.getId());
 461  
                 }
 462  
         }
 463  
 
 464  
         //See if possible to grab details for Principal
 465  0
         if (StringUtils.isEmpty(newDelegationMember.getMemberId())
 466  
                         && StringUtils.isNotEmpty(newDelegationMember.getMemberName())
 467  
                         && StringUtils.equals(newDelegationMember.getMemberTypeCode(), KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE)) {
 468  0
                 Principal principal = KimApiServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(newDelegationMember.getMemberName());
 469  0
                 if (principal != null) {
 470  0
                         newDelegationMember.setMemberId(principal.getPrincipalId());
 471  
                 }
 472  
         }
 473  
 
 474  0
         if (checkDelegationMember(newDelegationMember) && KNSServiceLocatorWeb.getKualiRuleService().applyRules(
 475  
                         new AddDelegationMemberEvent("", roleDocumentForm.getRoleDocument(), newDelegationMember))) {
 476  0
                 newDelegationMember.setDocumentNumber(roleDocumentForm.getDocument().getDocumentNumber());
 477  0
                 roleDocumentForm.getRoleDocument().addDelegationMember(newDelegationMember);
 478  0
                 roleDocumentForm.setDelegationMember(roleDocumentForm.getRoleDocument().getBlankDelegationMember());
 479  
         }
 480  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 481  
     }
 482  
 
 483  
     public ActionForward deleteDelegationMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 484  0
         IdentityManagementRoleDocumentForm roleDocumentForm = (IdentityManagementRoleDocumentForm) form;
 485  
         // Removing, not inactivating -- is this what we really want?
 486  0
         roleDocumentForm.getRoleDocument().getDelegationMembers().remove(getLineToDelete(request));
 487  0
         roleDocumentForm.setDelegationMember(roleDocumentForm.getRoleDocument().getBlankDelegationMember());
 488  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 489  
     }
 490  
 
 491  
     /**
 492  
      * @see org.kuali.rice.kns.web.struts.action.KualiTableRenderAction#switchToPage(org.apache.struts.action.ActionMapping,
 493  
      *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 494  
      */
 495  
     public ActionForward jumpToRoleMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 496  0
         IdentityManagementRoleDocumentForm idmForm = (IdentityManagementRoleDocumentForm) form;
 497  0
         String delegationRoleMemberId = getDelegationRoleMemberToJumpTo(request);
 498  0
         KualiTableRenderFormMetadata memberTableMetadata = idmForm.getMemberTableMetadata();
 499  0
         memberTableMetadata.jumpToPage(idmForm.getPageNumberOfRoleMemberId(delegationRoleMemberId),
 500  
                                                                         idmForm.getMemberRows().size(), idmForm.getRecordsPerPage());
 501  0
         memberTableMetadata.setColumnToSortIndex(memberTableMetadata.getPreviouslySortedColumnIndex());
 502  0
         idmForm.setAnchor(delegationRoleMemberId);
 503  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 504  
     }
 505  
 
 506  
     protected String getDelegationRoleMemberToJumpTo(HttpServletRequest request) {
 507  0
         String delegationRoleMemberIdToJumpTo = "";
 508  0
         String parameterName = (String) request.getAttribute(KNSConstants.METHOD_TO_CALL_ATTRIBUTE);
 509  0
         if (StringUtils.isNotBlank(parameterName)) {
 510  0
             delegationRoleMemberIdToJumpTo = StringUtils.substringBetween(parameterName, ".dmrmi", ".");
 511  
         }
 512  0
         return delegationRoleMemberIdToJumpTo;
 513  
     }
 514  
 
 515  
 
 516  
         /**
 517  
          * Side-effecting method returns an ActionForward if needed for handling prompting of the user about automatically
 518  
          * "inactivating" active delegates of inactive role members.  If the user has already responded "Yes", delegates are
 519  
          * "inactivated" here, and a null forward is returned.  Otherwise, an appropriate forward is returned.
 520  
          *
 521  
          * @param roleMembersToConsiderInactive additional role members to consider inactive for the purposes of this computation
 522  
          */
 523  
         private ActionForward promptForAffectedDelegates(ActionMapping mapping,
 524  
                         ActionForm form, HttpServletRequest request,
 525  
                         HttpServletResponse response,
 526  
                         IdentityManagementRoleDocumentForm roleDocumentForm, KimDocumentRoleMember ... roleMembersToConsiderInactive)
 527  
                         throws Exception {
 528  
                 // KULRICE-4762: Role: Removed an Assignee who has delegations associated with him and now the Role cannot be updated
 529  
                 // To solve this issue, prompt for confirmation if there are active delegates for the role member being "inactivated",
 530  
                 // and upon confirmation, "inactivate" the delegates too.
 531  0
                 List<RoleDocumentDelegationMember> activeDelegatesOfInactiveRoleMembers =
 532  
                         getActiveDelegatesOfInactiveRoleMembers(roleDocumentForm, roleMembersToConsiderInactive);
 533  0
                 ActionForward forward = getAffectedDelegatesQuestionActionForward(activeDelegatesOfInactiveRoleMembers, mapping, form, request,
 534  
                                 response, roleDocumentForm);
 535  
                 // if the question logic gave us a forward, do it
 536  0
                 if (forward != null) return forward;
 537  
                 // otherwise, inactivate affected delegates
 538  0
                 if (activeDelegatesOfInactiveRoleMembers.size() > 0) {
 539  0
                         Calendar cal = Calendar.getInstance();
 540  
                         // deactivate (inactivate?) delegates
 541  0
                         for (RoleDocumentDelegationMember delegateToDeactivate : activeDelegatesOfInactiveRoleMembers) {
 542  0
                                 delegateToDeactivate.setActiveToDate(new Timestamp(cal.getTimeInMillis()));
 543  
                         }
 544  
                 }
 545  0
                 return null;
 546  
         }
 547  
 
 548  
         /**
 549  
          * <p>If there are active delegates of an "inactivated" role member, return an ActionForward to prompt the user
 550  
          * letting them know that the delegates will be "inactivated" too if they proceed.
 551  
          * <p>Also, if the user has already responded to the question and the response was (1) "Yes", then return null, signifying
 552  
          * that we can go ahead and take the needed action to "inactivate" the delegates; or (2) "No", then return a basic forward that
 553  
          * will cancel further action.
 554  
          */
 555  
         private ActionForward getAffectedDelegatesQuestionActionForward(List<RoleDocumentDelegationMember> activeDelegatesOfInactiveRoleMembers,
 556  
                         ActionMapping mapping, ActionForm form, HttpServletRequest request,
 557  
                         HttpServletResponse response,
 558  
                         IdentityManagementRoleDocumentForm roleDocumentForm)
 559  
         throws Exception {
 560  
 
 561  0
                 if (activeDelegatesOfInactiveRoleMembers.size() > 0) {
 562  0
                         Object question = getQuestion(request);
 563  
                         // logic for delegates question
 564  0
                         if (question == null || !REMOVE_AFFECTED_DELEGATES_QUESTION_ID.equals(question)) {
 565  0
                                 return performQuestionWithoutInput(mapping, form, request, response, REMOVE_AFFECTED_DELEGATES_QUESTION_ID,
 566  
                                                 getKualiConfigurationService().getPropertyString(RiceKeyConstants.QUESTION_ACTIVE_DELEGATES_FOR_INACTIVE_MEMBERS),
 567  
                                                 KNSConstants.CONFIRMATION_QUESTION, roleDocumentForm.getMethodToCall(), StringUtils.EMPTY);
 568  
                         }
 569  0
                         Object buttonClicked = request.getParameter(KNSConstants.QUESTION_CLICKED_BUTTON);
 570  0
                         if ((REMOVE_AFFECTED_DELEGATES_QUESTION_ID.equals(question)) && ConfirmationQuestion.YES.equals(buttonClicked)) {
 571  
                                 // the question was answered in the affirmative.
 572  
                         // fall through, no special mapping to return
 573  
                         } else {
 574  
                                 // NO was clicked ... what to do?  Return basic mapping without "inactivating" anything
 575  0
                                 return mapping.findForward(RiceConstants.MAPPING_BASIC);
 576  
                         }
 577  
                 }
 578  
 
 579  0
                 return null;
 580  
         }
 581  
 
 582  
         /**
 583  
          * This method returns a list of all active delegates for role members that are inactive
 584  
          *
 585  
          * @param roleDocumentForm form bean
 586  
          * @param roleMembersToConsiderInactive additional role members to consider inactive for the purposes of this computation
 587  
          * @return the active delegates of inactive role members
 588  
          */
 589  
         private List<RoleDocumentDelegationMember> getActiveDelegatesOfInactiveRoleMembers(
 590  
                         IdentityManagementRoleDocumentForm roleDocumentForm, KimDocumentRoleMember ... roleMembersToConsiderInactive) {
 591  0
                 List<KimDocumentRoleMember> roleMembers = roleDocumentForm.getMemberRows();
 592  0
                 List<KimDocumentRoleMember> inactiveRoleMembers = new ArrayList<KimDocumentRoleMember>();
 593  0
                 List<RoleDocumentDelegationMember> activeDelegatesOfInactivatedRoleMembers = new ArrayList<RoleDocumentDelegationMember>();
 594  
 
 595  0
         inactiveRoleMembers.addAll(Arrays.asList(roleMembersToConsiderInactive));
 596  
 
 597  0
                 if (roleMembers != null) for (KimDocumentRoleMember roleMember : roleMembers) if (roleMember != null) {
 598  0
                         if (!roleMember.isActive()) inactiveRoleMembers.add(roleMember);
 599  
                 }
 600  
 
 601  0
                 for (KimDocumentRoleMember inactiveRoleMember : inactiveRoleMembers) {
 602  
                         // check if there are delegates for the member being removed
 603  0
                         List<RoleDocumentDelegationMember> delegationMembers = roleDocumentForm.getRoleDocument().getDelegationMembers();
 604  0
                         if (delegationMembers != null) for (RoleDocumentDelegationMember delegationMember : delegationMembers) {
 605  0
                                 if (delegationMember != null && delegationMember.isActive()) {
 606  
                                         // if the roleMember for this delegation is the same as the inactivatedRoleMember
 607  0
                                         if (delegationMember.getRoleMemberId().equals(inactiveRoleMember.getRoleMemberId())) {
 608  0
                                                 activeDelegatesOfInactivatedRoleMembers.add(delegationMember);
 609  
                                         }
 610  
                                 }
 611  
                         }
 612  0
                 }
 613  0
                 return activeDelegatesOfInactivatedRoleMembers;
 614  
         }
 615  
 
 616  
         /**
 617  
          * 
 618  
          * This method overrides validateRole() from IdentityManagementDocumentActionBase.
 619  
          * The difference with this method is that it allows derived roles.   
 620  
          * The base implementation returns false if the role is a derived role.
 621  
          * 
 622  
          * @see org.kuali.rice.kim.web.struts.action.IdentityManagementDocumentActionBase#validateRole(java.lang.String, org.kuali.rice.kim.bo.Role, java.lang.String, java.lang.String)
 623  
          */
 624  
     protected boolean validateRole( String roleId, Role role, String propertyName, String message){
 625  0
             if ( role == null ) {
 626  0
                 GlobalVariables.getMessageMap().putError(propertyName, RiceKeyConstants.ERROR_INVALID_ROLE, roleId );
 627  0
                     return false;
 628  
             }
 629  0
             return true;
 630  
     }
 631  
  
 632  
 
 633  
 }