Coverage Report - org.kuali.rice.kim.web.struts.action.IdentityManagementGroupDocumentAction
 
Classes in this File Line Coverage Branch Coverage Complexity
IdentityManagementGroupDocumentAction
0%
0/111
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 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.api.type.KimType;
 29  
 import org.kuali.rice.kim.bo.Role;
 30  
 import org.kuali.rice.kim.bo.role.dto.KimRoleInfo;
 31  
 import org.kuali.rice.kim.bo.ui.GroupDocumentMember;
 32  
 import org.kuali.rice.kim.document.IdentityManagementGroupDocument;
 33  
 import org.kuali.rice.kim.rule.event.ui.AddGroupMemberEvent;
 34  
 import org.kuali.rice.kim.util.KimConstants;
 35  
 import org.kuali.rice.kim.web.struts.form.IdentityManagementGroupDocumentForm;
 36  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 37  
 import org.kuali.rice.kns.util.GlobalVariables;
 38  
 import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
 39  
 import org.kuali.rice.kns.web.struts.form.KualiTableRenderFormMetadata;
 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 {
 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  
         @Override
 66  
         public ActionForward execute(ActionMapping mapping, ActionForm form,
 67  
                         HttpServletRequest request, HttpServletResponse response) throws Exception {
 68  0
         IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 69  0
         if ( StringUtils.isBlank( groupDocumentForm.getGroupId() ) ) {
 70  0
             String groupId = request.getParameter(KimConstants.PrimaryKeyConstants.GROUP_ID);
 71  0
                 groupDocumentForm.setGroupId(groupId);
 72  
         }
 73  0
                 String kimTypeId = request.getParameter(KimConstants.PrimaryKeyConstants.KIM_TYPE_ID);
 74  0
         setKimType(kimTypeId, groupDocumentForm);
 75  
 
 76  
         
 77  0
                 KualiTableRenderFormMetadata memberTableMetadata = groupDocumentForm.getMemberTableMetadata();
 78  0
                 if (groupDocumentForm.getMemberRows() != null) {
 79  0
                         memberTableMetadata.jumpToPage(memberTableMetadata.getViewedPageNumber(), groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
 80  
                 }
 81  
                 
 82  0
                 ActionForward forward = super.execute(mapping, groupDocumentForm, request, response);
 83  
                 
 84  0
                 groupDocumentForm.setCanAssignGroup(validAssignGroup(groupDocumentForm.getGroupDocument()));
 85  0
                 return forward;
 86  
     }
 87  
     
 88  
     protected void setKimType(String kimTypeId, IdentityManagementGroupDocumentForm groupDocumentForm){
 89  0
                 if ( StringUtils.isNotBlank(kimTypeId) ) {
 90  0
                         KimType kType = KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId);
 91  0
                         groupDocumentForm.setKimType(kType);
 92  0
                         if (groupDocumentForm.getGroupDocument() != null) {
 93  0
                                 groupDocumentForm.getGroupDocument().setKimType(kType);
 94  
                         }
 95  0
                 } else if ( groupDocumentForm.getGroupDocument() != null && StringUtils.isNotBlank(groupDocumentForm.getGroupDocument().getGroupTypeId() ) ) {
 96  0
                         groupDocumentForm.setKimType(KimApiServiceLocator.getKimTypeInfoService().getKimType(
 97  
                                         groupDocumentForm.getGroupDocument().getGroupTypeId()));
 98  0
                         groupDocumentForm.getGroupDocument().setKimType(groupDocumentForm.getKimType());
 99  
                 }
 100  0
     }
 101  
     
 102  
     /**
 103  
      * This overridden method ...
 104  
      * 
 105  
      * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#loadDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
 106  
      */
 107  
     @Override
 108  
     protected void loadDocument(KualiDocumentFormBase form)
 109  
                     throws WorkflowException {
 110  0
             super.loadDocument(form);
 111  
             
 112  0
             IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 113  0
         setKimType(groupDocumentForm.getGroupId(), groupDocumentForm);
 114  0
         groupDocumentForm.setMember(groupDocumentForm.getGroupDocument().getBlankMember());
 115  
 
 116  0
                 KualiTableRenderFormMetadata memberTableMetadata = groupDocumentForm.getMemberTableMetadata();
 117  0
                 if (groupDocumentForm.getMemberRows() != null) {
 118  0
                     memberTableMetadata.jumpToFirstPage(groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
 119  
                 }
 120  0
     }
 121  
     
 122  
     /**
 123  
      * This overridden method ...
 124  
      * 
 125  
      * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#createDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
 126  
      */
 127  
     @Override
 128  
     protected void createDocument(KualiDocumentFormBase form)
 129  
                     throws WorkflowException {
 130  0
             super.createDocument(form);
 131  0
             IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 132  0
             if ( groupDocumentForm.getGroupId() == null ) {
 133  0
                     groupDocumentForm.getGroupDocument().setKimType(groupDocumentForm.getKimType());
 134  0
                     groupDocumentForm.getGroupDocument().initializeDocumentForNewGroup();
 135  0
                     groupDocumentForm.setGroupId( groupDocumentForm.getGroupDocument().getGroupId() );
 136  0
                     setKimType(groupDocumentForm.getGroupDocument().getGroupTypeId(), groupDocumentForm);
 137  
             } else {
 138  0
                     loadGroupIntoDocument( groupDocumentForm.getGroupId(), groupDocumentForm );
 139  
             }
 140  0
                 KualiTableRenderFormMetadata memberTableMetadata = groupDocumentForm.getMemberTableMetadata();
 141  0
                 if (groupDocumentForm.getMemberRows() != null) {
 142  0
                     memberTableMetadata.jumpToFirstPage(groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
 143  
                 }
 144  0
     }
 145  
 
 146  
 
 147  
     protected void loadGroupIntoDocument( String groupId, IdentityManagementGroupDocumentForm groupDocumentForm){
 148  0
         Group group = KimApiServiceLocator.getGroupService().getGroup(groupId);
 149  0
         getUiDocumentService().loadGroupDoc(groupDocumentForm.getGroupDocument(), group);
 150  0
     }    
 151  
         
 152  
         /***
 153  
          * @see org.kuali.rice.kim.web.struts.action.IdentityManagementDocumentActionBase#getActionName()
 154  
          */
 155  
         public String getActionName(){
 156  0
                 return KimConstants.KimUIConstants.KIM_GROUP_DOCUMENT_ACTION;
 157  
         }
 158  
         
 159  
         protected boolean validAssignGroup(IdentityManagementGroupDocument document){
 160  0
         boolean rulePassed = true;
 161  0
         Map<String,String> additionalPermissionDetails = new HashMap<String,String>();
 162  0
         if (!StringUtils.isEmpty(document.getGroupNamespace())) {
 163  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, document.getGroupNamespace());
 164  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.GROUP_NAME, document.getGroupName());
 165  0
                 if (!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
 166  
                                 document, 
 167  
                                 KimConstants.NAMESPACE_CODE, 
 168  
                                 KimConstants.PermissionTemplateNames.POPULATE_GROUP, 
 169  
                                 GlobalVariables.getUserSession().getPrincipalId(), 
 170  
                                 additionalPermissionDetails, null)){
 171  0
                         rulePassed = false;
 172  
                 }
 173  
         }
 174  0
                 return rulePassed;
 175  
         }
 176  
 
 177  
         public ActionForward changeMemberTypeCode(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 178  0
                 IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 179  0
         groupDocumentForm.getMember().setMemberName("");
 180  0
         return refresh(mapping, groupDocumentForm, request, response);
 181  
         }        
 182  
         
 183  
     public ActionForward addMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 184  0
         IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 185  0
         GroupDocumentMember newMember = groupDocumentForm.getMember();
 186  
         
 187  
         //See if possible to add with just Group Details filled in (not returned from lookup)
 188  0
         if (StringUtils.isEmpty(newMember.getMemberId()) 
 189  
                         && StringUtils.isNotEmpty(newMember.getMemberName())
 190  
                         && StringUtils.isNotEmpty(newMember.getMemberNamespaceCode())
 191  
                         && StringUtils.equals(newMember.getMemberTypeCode(), KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) {
 192  0
                 Group tempGroup = KimApiServiceLocator.getIdentityManagementService().getGroupByName(newMember.getMemberNamespaceCode(), newMember.getMemberName());
 193  0
                 if (tempGroup != null) {
 194  0
                         newMember.setMemberId(tempGroup.getId());
 195  
                 }
 196  
         }
 197  
         
 198  
         //See if possible to grab details for Principal
 199  0
         if (StringUtils.isEmpty(newMember.getMemberId()) 
 200  
                         && StringUtils.isNotEmpty(newMember.getMemberName())
 201  
                         && StringUtils.equals(newMember.getMemberTypeCode(), KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE)) {
 202  0
                 Principal principal = KimApiServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(newMember.getMemberName());
 203  0
                 if (principal != null) {
 204  0
                         newMember.setMemberId(principal.getPrincipalId());
 205  
                 }
 206  
         }
 207  0
         if(checkKimDocumentGroupMember(newMember) && 
 208  
                         KNSServiceLocatorWeb.getKualiRuleService().applyRules(new AddGroupMemberEvent("", groupDocumentForm.getGroupDocument(), newMember))){
 209  0
                 newMember.setDocumentNumber(groupDocumentForm.getDocument().getDocumentNumber());
 210  0
                 groupDocumentForm.getGroupDocument().addMember(newMember);
 211  0
                 groupDocumentForm.setMember(groupDocumentForm.getGroupDocument().getBlankMember());
 212  0
                 groupDocumentForm.getMemberTableMetadata().jumpToLastPage(groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
 213  
        }
 214  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 215  
     }
 216  
 
 217  
     protected boolean checkKimDocumentGroupMember(GroupDocumentMember newMember){
 218  0
         if(StringUtils.isBlank(newMember.getMemberTypeCode()) || StringUtils.isBlank(newMember.getMemberId())){
 219  0
                 GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_EMPTY_ENTRY,
 220  
                                 new String[] {"Member Type Code and Member ID"});
 221  0
                 return false;
 222  
                 }
 223  0
             if(KimConstants.KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE.equals(newMember.getMemberTypeCode())){
 224  0
                 Principal principalInfo = getIdentityManagementService().getPrincipal(newMember.getMemberId());
 225  0
                 if (principalInfo == null) {
 226  0
                         GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
 227  
                                     new String[] {newMember.getMemberId()});
 228  0
                     return false;
 229  
                 }
 230  
                 else {
 231  0
                         newMember.setMemberName(principalInfo.getPrincipalName());
 232  
                 }
 233  0
         } else if(KimConstants.KimUIConstants.MEMBER_TYPE_GROUP_CODE.equals(newMember.getMemberTypeCode())){
 234  0
                 Group groupInfo = null;
 235  0
                 groupInfo = getIdentityManagementService().getGroup(newMember.getMemberId());
 236  0
                 if (groupInfo == null) {
 237  0
                         GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
 238  
                                     new String[] {newMember.getMemberId()});
 239  0
                     return false;
 240  
                 }
 241  
                 else {
 242  0
                         newMember.setMemberName(groupInfo.getName());
 243  0
                 newMember.setMemberNamespaceCode(groupInfo.getNamespaceCode());
 244  
                 }
 245  0
         } else if(KimConstants.KimUIConstants.MEMBER_TYPE_ROLE_CODE.equals(newMember.getMemberTypeCode())){
 246  0
                 KimRoleInfo roleInfo = null;
 247  0
                 roleInfo = KimApiServiceLocator.getRoleService().getRole(newMember.getMemberId());
 248  0
                 if (roleInfo == null) {
 249  0
                         GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
 250  
                                     new String[] {newMember.getMemberId()});
 251  0
                     return false;
 252  
                 }
 253  0
                 else if(StringUtils.equals(newMember.getMemberTypeCode(), KimConstants.KimUIConstants.MEMBER_TYPE_ROLE_CODE) 
 254  
                             && !validateRole(newMember.getMemberId(), (Role)roleInfo, "document.member.memberId", "Role")){
 255  0
                     return false;
 256  
             }
 257  
                 else {
 258  0
                         newMember.setMemberName(roleInfo.getRoleName());
 259  0
                 newMember.setMemberNamespaceCode(roleInfo.getNamespaceCode());
 260  
                 }
 261  
                 }
 262  0
         return true;
 263  
     }
 264  
     
 265  
     public ActionForward deleteMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 266  0
         IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
 267  0
         GroupDocumentMember memberToBeInactivated = groupDocumentForm.getGroupDocument().getMembers().get(getLineToDelete(request));    
 268  0
         Calendar cal = Calendar.getInstance();
 269  0
         memberToBeInactivated.setActiveToDate(new Timestamp(cal.getTimeInMillis()));        
 270  0
         groupDocumentForm.getGroupDocument().getMembers().set(getLineToDelete(request), memberToBeInactivated);   
 271  0
         groupDocumentForm.setMember(groupDocumentForm.getGroupDocument().getBlankMember());
 272  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 273  
     }
 274  
     
 275  
 }