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