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
3.846
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.api.membership.MemberType;
 23  
 import org.kuali.rice.core.api.util.RiceConstants;
 24  
 import org.kuali.rice.core.api.util.RiceKeyConstants;
 25  
 import org.kuali.rice.kew.api.exception.WorkflowException;
 26  
 import org.kuali.rice.kim.api.KimConstants;
 27  
 import org.kuali.rice.kim.api.group.Group;
 28  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 29  
 import org.kuali.rice.kim.api.role.Role;
 30  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 31  
 import org.kuali.rice.kim.api.type.KimType;
 32  
 import org.kuali.rice.kim.bo.ui.GroupDocumentMember;
 33  
 import org.kuali.rice.kim.document.IdentityManagementGroupDocument;
 34  
 import org.kuali.rice.kim.rule.event.ui.AddGroupMemberEvent;
 35  
 import org.kuali.rice.kim.web.struts.form.IdentityManagementGroupDocumentForm;
 36  
 import org.kuali.rice.kns.web.struts.action.KualiTableRenderAction;
 37  
 import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
 38  
 import org.kuali.rice.kns.web.struts.form.KualiTableRenderFormMetadata;
 39  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 40  
 import org.kuali.rice.krad.util.GlobalVariables;
 41  
 
 42  
 import javax.servlet.http.HttpServletRequest;
 43  
 import javax.servlet.http.HttpServletResponse;
 44  
 import java.sql.Timestamp;
 45  
 import java.util.Calendar;
 46  
 import java.util.HashMap;
 47  
 import java.util.Map;
 48  
 
 49  
 /**
 50  
  * 
 51  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 52  
  *
 53  
  */
 54  
 public class IdentityManagementGroupDocumentAction extends IdentityManagementDocumentActionBase implements KualiTableRenderAction {
 55  
 
 56  
         /**
 57  
          * This constructs a ...
 58  
          * 
 59  
          */
 60  
         public IdentityManagementGroupDocumentAction() {
 61  0
                 super();
 62  0
                 addMethodToCallToUncheckedList( CHANGE_MEMBER_TYPE_CODE_METHOD_TO_CALL );
 63  0
                 addMethodToCallToUncheckedList( CHANGE_NAMESPACE_METHOD_TO_CALL );
 64  0
         }
 65  
         
 66  
     /**
 67  
      * This method doesn't actually sort the column - it's just that we need a sort method in
 68  
      * order to exploit the existing methodToCall logic. The sorting is handled in the execute
 69  
      * method below, and delegated to the KualiTableRenderFormMetadata object. 
 70  
      * 
 71  
      * @param mapping
 72  
      * @param form 
 73  
      * @param request
 74  
      * @param response
 75  
      * @return
 76  
      * @throws Exception
 77  
      */
 78  
     public ActionForward sort(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 79  
 
 80  0
             return mapping.findForward(RiceConstants.MAPPING_BASIC);
 81  
     }
 82  
     
 83  
     @Override
 84  
         public ActionForward execute(ActionMapping mapping, ActionForm form,
 85  
                         HttpServletRequest request, HttpServletResponse response) throws Exception {
 86  0
         IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 87  0
         if ( StringUtils.isBlank( groupDocumentForm.getGroupId() ) ) {
 88  0
             String groupId = request.getParameter(KimConstants.PrimaryKeyConstants.GROUP_ID);
 89  0
                 groupDocumentForm.setGroupId(groupId);
 90  
         }
 91  0
                 String kimTypeId = request.getParameter(KimConstants.PrimaryKeyConstants.KIM_TYPE_ID);
 92  0
         setKimType(kimTypeId, groupDocumentForm);
 93  
 
 94  
         
 95  0
                 KualiTableRenderFormMetadata memberTableMetadata = groupDocumentForm.getMemberTableMetadata();
 96  0
                 if (groupDocumentForm.getMemberRows() != null) {
 97  0
                         memberTableMetadata.jumpToPage(memberTableMetadata.getViewedPageNumber(), groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
 98  
                         // KULRICE-3972: need to be able to sort by column header like on lookups when editing large roles and groups
 99  0
                         memberTableMetadata.sort(groupDocumentForm.getMemberRows(), groupDocumentForm.getRecordsPerPage());
 100  
                 }
 101  
                 
 102  0
                 ActionForward forward = super.execute(mapping, groupDocumentForm, request, response);
 103  
                 
 104  0
                 groupDocumentForm.setCanAssignGroup(validAssignGroup(groupDocumentForm.getGroupDocument()));
 105  0
                 return forward;
 106  
     }
 107  
     
 108  
     protected void setKimType(String kimTypeId, IdentityManagementGroupDocumentForm groupDocumentForm){
 109  0
                 if ( StringUtils.isNotBlank(kimTypeId) ) {
 110  0
                         KimType kType = KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId);
 111  0
                         groupDocumentForm.setKimType(kType);
 112  0
                         if (groupDocumentForm.getGroupDocument() != null) {
 113  0
                                 groupDocumentForm.getGroupDocument().setKimType(kType);
 114  
                         }
 115  0
                 } else if ( groupDocumentForm.getGroupDocument() != null && StringUtils.isNotBlank(groupDocumentForm.getGroupDocument().getGroupTypeId() ) ) {
 116  0
                         groupDocumentForm.setKimType(KimApiServiceLocator.getKimTypeInfoService().getKimType(
 117  
                                         groupDocumentForm.getGroupDocument().getGroupTypeId()));
 118  0
                         groupDocumentForm.getGroupDocument().setKimType(groupDocumentForm.getKimType());
 119  
                 }
 120  0
     }
 121  
     
 122  
     /**
 123  
      * This overridden method ...
 124  
      * 
 125  
      * @see org.kuali.rice.krad.web.struts.action.KualiDocumentActionBase#loadDocument(org.kuali.rice.krad.web.struts.form.KualiDocumentFormBase)
 126  
      */
 127  
     @Override
 128  
     protected void loadDocument(KualiDocumentFormBase form)
 129  
                     throws WorkflowException {
 130  0
             super.loadDocument(form);
 131  
             
 132  0
             IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 133  0
         setKimType(groupDocumentForm.getGroupId(), groupDocumentForm);
 134  0
         groupDocumentForm.setMember(groupDocumentForm.getGroupDocument().getBlankMember());
 135  
 
 136  0
                 KualiTableRenderFormMetadata memberTableMetadata = groupDocumentForm.getMemberTableMetadata();
 137  0
                 if (groupDocumentForm.getMemberRows() != null) {
 138  0
                     memberTableMetadata.jumpToFirstPage(groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
 139  
                 }
 140  0
     }
 141  
     
 142  
     /**
 143  
      * This overridden method ...
 144  
      * 
 145  
      * @see org.kuali.rice.krad.web.struts.action.KualiDocumentActionBase#createDocument(org.kuali.rice.krad.web.struts.form.KualiDocumentFormBase)
 146  
      */
 147  
     @Override
 148  
     protected void createDocument(KualiDocumentFormBase form)
 149  
                     throws WorkflowException {
 150  0
             super.createDocument(form);
 151  0
             IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 152  0
             if ( groupDocumentForm.getGroupId() == null ) {
 153  0
                     groupDocumentForm.getGroupDocument().setKimType(groupDocumentForm.getKimType());
 154  0
                     groupDocumentForm.getGroupDocument().initializeDocumentForNewGroup();
 155  0
                     groupDocumentForm.setGroupId( groupDocumentForm.getGroupDocument().getGroupId() );
 156  0
                     setKimType(groupDocumentForm.getGroupDocument().getGroupTypeId(), groupDocumentForm);
 157  
             } else {
 158  0
                     loadGroupIntoDocument( groupDocumentForm.getGroupId(), groupDocumentForm );
 159  
             }
 160  0
                 KualiTableRenderFormMetadata memberTableMetadata = groupDocumentForm.getMemberTableMetadata();
 161  0
                 if (groupDocumentForm.getMemberRows() != null) {
 162  0
                     memberTableMetadata.jumpToFirstPage(groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
 163  
                 }
 164  0
     }
 165  
 
 166  
 
 167  
     protected void loadGroupIntoDocument( String groupId, IdentityManagementGroupDocumentForm groupDocumentForm){
 168  0
         Group group = KimApiServiceLocator.getGroupService().getGroup(groupId);
 169  0
         getUiDocumentService().loadGroupDoc(groupDocumentForm.getGroupDocument(), group);
 170  0
     }    
 171  
         
 172  
         /***
 173  
          * @see org.kuali.rice.kim.web.struts.action.IdentityManagementDocumentActionBase#getActionName()
 174  
          */
 175  
         public String getActionName(){
 176  0
                 return KimConstants.KimUIConstants.KIM_GROUP_DOCUMENT_ACTION;
 177  
         }
 178  
         
 179  
         protected boolean validAssignGroup(IdentityManagementGroupDocument document){
 180  0
         boolean rulePassed = true;
 181  0
         Map<String,String> additionalPermissionDetails = new HashMap<String,String>();
 182  0
         if (!StringUtils.isEmpty(document.getGroupNamespace())) {
 183  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, document.getGroupNamespace());
 184  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.GROUP_NAME, document.getGroupName());
 185  0
                 if (!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
 186  
                                 document, 
 187  
                                 KimConstants.NAMESPACE_CODE, 
 188  
                                 KimConstants.PermissionTemplateNames.POPULATE_GROUP, 
 189  
                                 GlobalVariables.getUserSession().getPrincipalId(), 
 190  
                                 additionalPermissionDetails, null)){
 191  0
                         rulePassed = false;
 192  
                 }
 193  
         }
 194  0
                 return rulePassed;
 195  
         }
 196  
 
 197  
         public ActionForward changeMemberTypeCode(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 198  0
                 IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 199  0
         groupDocumentForm.getMember().setMemberName("");
 200  0
         return refresh(mapping, groupDocumentForm, request, response);
 201  
         }        
 202  
         
 203  
     public ActionForward addMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 204  0
         IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 205  0
         GroupDocumentMember newMember = groupDocumentForm.getMember();
 206  
         
 207  
         //See if possible to add with just Group Details filled in (not returned from lookup)
 208  0
         if (StringUtils.isEmpty(newMember.getMemberId()) 
 209  
                         && StringUtils.isNotEmpty(newMember.getMemberName())
 210  
                         && StringUtils.isNotEmpty(newMember.getMemberNamespaceCode())
 211  
                         && StringUtils.equals(newMember.getMemberTypeCode(), KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE.getCode())) {
 212  0
                 Group tempGroup = KimApiServiceLocator.getGroupService().getGroupByNameAndNamespaceCode(
 213  
                     newMember.getMemberNamespaceCode(), newMember.getMemberName());
 214  0
                 if (tempGroup != null) {
 215  0
                         newMember.setMemberId(tempGroup.getId());
 216  
                 }
 217  
         }
 218  
         
 219  
         //See if possible to grab details for Principal
 220  0
         if (StringUtils.isEmpty(newMember.getMemberId()) 
 221  
                         && StringUtils.isNotEmpty(newMember.getMemberName())
 222  
                         && StringUtils.equals(newMember.getMemberTypeCode(), KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.getCode())) {
 223  0
                 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(newMember.getMemberName());
 224  0
                 if (principal != null) {
 225  0
                         newMember.setMemberId(principal.getPrincipalId());
 226  
                 }
 227  
         }
 228  0
         if(checkKimDocumentGroupMember(newMember) && 
 229  
                         KRADServiceLocatorWeb.getKualiRuleService().applyRules(new AddGroupMemberEvent("", groupDocumentForm.getGroupDocument(), newMember))){
 230  0
                 newMember.setDocumentNumber(groupDocumentForm.getDocument().getDocumentNumber());
 231  0
                 groupDocumentForm.getGroupDocument().addMember(newMember);
 232  0
                 groupDocumentForm.setMember(groupDocumentForm.getGroupDocument().getBlankMember());
 233  0
                 groupDocumentForm.getMemberTableMetadata().jumpToLastPage(groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
 234  
        }
 235  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 236  
     }
 237  
 
 238  
     protected boolean checkKimDocumentGroupMember(GroupDocumentMember newMember){
 239  0
         if(StringUtils.isBlank(newMember.getMemberTypeCode()) || StringUtils.isBlank(newMember.getMemberId())){
 240  0
                 GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_EMPTY_ENTRY,
 241  
                                 new String[] {"Member Type Code and Member ID"});
 242  0
                 return false;
 243  
                 }
 244  0
             if(MemberType.PRINCIPAL.getCode().equals(newMember.getMemberTypeCode())){
 245  0
                 Principal principalInfo = getIdentityService().getPrincipal(newMember.getMemberId());
 246  0
                 if (principalInfo == null) {
 247  0
                         GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
 248  
                                     new String[] {newMember.getMemberId()});
 249  0
                     return false;
 250  
                 }
 251  
                 else {
 252  0
                         newMember.setMemberName(principalInfo.getPrincipalName());
 253  
                 }
 254  0
         } else if(MemberType.GROUP.getCode().equals(newMember.getMemberTypeCode())){
 255  0
                 Group groupInfo = null;
 256  0
                 groupInfo = KimApiServiceLocator.getGroupService().getGroup(newMember.getMemberId());
 257  0
                 if (groupInfo == null) {
 258  0
                         GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
 259  
                                     new String[] {newMember.getMemberId()});
 260  0
                     return false;
 261  
                 }
 262  
                 else {
 263  0
                         newMember.setMemberName(groupInfo.getName());
 264  0
                 newMember.setMemberNamespaceCode(groupInfo.getNamespaceCode());
 265  
                 }
 266  0
         } else if(MemberType.ROLE.getCode().equals(newMember.getMemberTypeCode())){
 267  0
                 Role role = KimApiServiceLocator.getRoleService().getRole(newMember.getMemberId());
 268  0
                 if (role == null) {
 269  0
                         GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
 270  
                                     new String[] {newMember.getMemberId()});
 271  0
                     return false;
 272  
                 }
 273  0
                 else if(StringUtils.equals(newMember.getMemberTypeCode(), MemberType.ROLE.getCode())
 274  
                             && !validateRole(newMember.getMemberId(), role, "document.member.memberId", "Role")){
 275  0
                     return false;
 276  
             }
 277  
                 else {
 278  0
                         newMember.setMemberName(role.getName());
 279  0
                 newMember.setMemberNamespaceCode(role.getNamespaceCode());
 280  
                 }
 281  
                 }
 282  0
         return true;
 283  
     }
 284  
     
 285  
     public ActionForward deleteMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 286  0
         IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 287  0
         GroupDocumentMember memberToBeInactivated = groupDocumentForm.getGroupDocument().getMembers().get(getLineToDelete(request));    
 288  0
         Calendar cal = Calendar.getInstance();
 289  0
         memberToBeInactivated.setActiveToDate(new Timestamp(cal.getTimeInMillis()));        
 290  0
         groupDocumentForm.getGroupDocument().getMembers().set(getLineToDelete(request), memberToBeInactivated);   
 291  0
         groupDocumentForm.setMember(groupDocumentForm.getGroupDocument().getBlankMember());
 292  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 293  
     }
 294  
     
 295  
 }