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