View Javadoc

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