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