View Javadoc

1   /*
2    * Copyright 2007-2008 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.text.MessageFormat;
20  import java.util.ArrayList;
21  import java.util.Calendar;
22  import java.util.List;
23  import java.util.Properties;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.apache.struts.action.ActionForm;
30  import org.apache.struts.action.ActionForward;
31  import org.apache.struts.action.ActionMapping;
32  import org.kuali.rice.core.util.RiceConstants;
33  import org.kuali.rice.kew.exception.WorkflowException;
34  import org.kuali.rice.kim.bo.entity.dto.KimEntityDefaultInfo;
35  import org.kuali.rice.kim.bo.entity.dto.KimPrincipalInfo;
36  import org.kuali.rice.kim.bo.group.dto.GroupInfo;
37  import org.kuali.rice.kim.bo.impl.GroupImpl;
38  import org.kuali.rice.kim.bo.impl.RoleImpl;
39  import org.kuali.rice.kim.bo.role.dto.KimRoleInfo;
40  import org.kuali.rice.kim.bo.role.impl.RoleMemberImpl;
41  import org.kuali.rice.kim.bo.role.impl.RoleResponsibilityImpl;
42  import org.kuali.rice.kim.bo.types.dto.AttributeSet;
43  import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
44  import org.kuali.rice.kim.bo.ui.KimDocumentRoleQualifier;
45  import org.kuali.rice.kim.bo.ui.KimDocumentRoleResponsibilityAction;
46  import org.kuali.rice.kim.bo.ui.PersonDocumentAddress;
47  import org.kuali.rice.kim.bo.ui.PersonDocumentAffiliation;
48  import org.kuali.rice.kim.bo.ui.PersonDocumentCitizenship;
49  import org.kuali.rice.kim.bo.ui.PersonDocumentEmail;
50  import org.kuali.rice.kim.bo.ui.PersonDocumentEmploymentInfo;
51  import org.kuali.rice.kim.bo.ui.PersonDocumentGroup;
52  import org.kuali.rice.kim.bo.ui.PersonDocumentName;
53  import org.kuali.rice.kim.bo.ui.PersonDocumentPhone;
54  import org.kuali.rice.kim.bo.ui.PersonDocumentRole;
55  import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMember;
56  import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMemberQualifier;
57  import org.kuali.rice.kim.document.IdentityManagementPersonDocument;
58  import org.kuali.rice.kim.document.KimTypeAttributesHelper;
59  import org.kuali.rice.kim.document.rule.AttributeValidationHelper;
60  import org.kuali.rice.kim.rule.event.ui.AddGroupEvent;
61  import org.kuali.rice.kim.rule.event.ui.AddPersonDelegationMemberEvent;
62  import org.kuali.rice.kim.rule.event.ui.AddPersonDocumentRoleQualifierEvent;
63  import org.kuali.rice.kim.rule.event.ui.AddRoleEvent;
64  import org.kuali.rice.kim.rules.ui.PersonDocumentRoleRule;
65  import org.kuali.rice.kim.service.KIMServiceLocator;
66  import org.kuali.rice.kim.service.support.KimTypeService;
67  import org.kuali.rice.kim.util.KIMPropertyConstants;
68  import org.kuali.rice.kim.util.KimConstants;
69  import org.kuali.rice.kim.web.struts.form.IdentityManagementPersonDocumentForm;
70  import org.kuali.rice.kns.datadictionary.AttributeDefinition;
71  import org.kuali.rice.kns.datadictionary.KimDataDictionaryAttributeDefinition;
72  import org.kuali.rice.kns.datadictionary.KimNonDataDictionaryAttributeDefinition;
73  import org.kuali.rice.kns.util.GlobalVariables;
74  import org.kuali.rice.kns.util.KNSConstants;
75  import org.kuali.rice.kns.util.RiceKeyConstants;
76  import org.kuali.rice.kns.util.UrlFactory;
77  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
78  
79  /**
80   * This is a description of what this class does - shyu don't forget to fill this in. 
81   * 
82   * @author Kuali Rice Team (rice.collab@kuali.org)
83   *
84   */
85  public class IdentityManagementPersonDocumentAction extends IdentityManagementDocumentActionBase {
86  
87  	@Override
88  	public ActionForward execute(ActionMapping mapping, ActionForm form,
89  			HttpServletRequest request, HttpServletResponse response)
90  			throws Exception {
91  		ActionForward forward = null;
92          IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
93          // accept either the principal name or principal ID, looking up the latter if necessary
94          // this allows inquiry links to work even when only the principal name is present
95          String principalId = request.getParameter(KIMPropertyConstants.Person.PRINCIPAL_ID);
96          String principalName = request.getParameter(KIMPropertyConstants.Person.PRINCIPAL_NAME);
97          if ( StringUtils.isBlank(principalId) && StringUtils.isNotBlank(principalName) ) {
98          	KimPrincipalInfo principal = KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName);
99          	if ( principal != null ) {
100         		principalId = principal.getPrincipalId();
101         	}
102         }
103         if ( principalId != null ) {
104         	personDocumentForm.setPrincipalId(principalId);
105         }
106         forward = super.execute(mapping, form, request, response);
107         
108         personDocumentForm.setCanModifyEntity(getUiDocumentService().canModifyEntity(GlobalVariables.getUserSession().getPrincipalId(), personDocumentForm.getPrincipalId()));
109         KimEntityDefaultInfo origEntity = null;
110         if(personDocumentForm.getPersonDocument()!=null)
111         	origEntity = getIdentityManagementService().getEntityDefaultInfo(personDocumentForm.getPersonDocument().getEntityId());
112         boolean isCreatingNew = (personDocumentForm.getPersonDocument()==null || origEntity==null)?true:false;
113         personDocumentForm.setCanOverrideEntityPrivacyPreferences(isCreatingNew || getUiDocumentService().canOverrideEntityPrivacyPreferences(GlobalVariables.getUserSession().getPrincipalId(), personDocumentForm.getPrincipalId()));
114 		return forward;
115     }
116     
117 	/**
118 	 * This overridden method ...
119 	 * 
120 	 * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#loadDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
121 	 */
122 	@Override
123 	protected void loadDocument(KualiDocumentFormBase form)
124 			throws WorkflowException {
125 		super.loadDocument(form);
126         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
127 		IdentityManagementPersonDocument personDoc = personDocumentForm.getPersonDocument();
128 		populateRoleInformation(personDoc);
129 	}
130 
131 	protected void populateRoleInformation( IdentityManagementPersonDocument personDoc ) {
132 		for (PersonDocumentRole role : personDoc.getRoles()) {
133 //			try {
134 	        KimTypeService kimTypeService = getKimTypeService(role.getKimRoleType());
135 	        if ( kimTypeService != null ) {
136 	        	role.setDefinitions(kimTypeService.getAttributeDefinitions(role.getKimTypeId()));
137 	        }
138         	// when post again, it will need this during populate
139             role.setNewRolePrncpl(new KimDocumentRoleMember());
140             for (String key : role.getDefinitions().keySet()) {
141             	KimDocumentRoleQualifier qualifier = new KimDocumentRoleQualifier();
142             	//qualifier.setQualifierKey(key);
143 	        	setAttrDefnIdForQualifier(qualifier,role.getDefinitions().get(key));
144             	role.getNewRolePrncpl().getQualifiers().add(qualifier);
145             }
146 	        role.setAttributeEntry( getUiDocumentService().getAttributeEntries( role.getDefinitions() ) );
147 		}
148 	}
149 	
150 	/**
151 	 * This overridden method ...
152 	 * 
153 	 * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#createDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
154 	 */
155 	@Override
156 	protected void createDocument(KualiDocumentFormBase form)
157 			throws WorkflowException {
158 		super.createDocument(form);
159         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
160         if(StringUtils.isBlank(personDocumentForm.getPrincipalId())){
161     		personDocumentForm.getPersonDocument().initializeDocumentForNewPerson();
162     		personDocumentForm.setPrincipalId(personDocumentForm.getPersonDocument().getPrincipalId());
163         } else {
164         	getUiDocumentService().loadEntityToPersonDoc(personDocumentForm.getPersonDocument(), personDocumentForm.getPrincipalId() );
165         	populateRoleInformation( personDocumentForm.getPersonDocument() );
166         	if(personDocumentForm.getPersonDocument()!=null) 
167         		personDocumentForm.getPersonDocument().setIfRolesEditable();
168         }
169 	}
170 	
171 	/***
172 	 * @see org.kuali.rice.kim.web.struts.action.IdentityManagementDocumentActionBase#getActionName()
173 	 */
174 	public String getActionName(){
175 		return KimConstants.KimUIConstants.KIM_PERSON_DOCUMENT_ACTION;
176 	}
177 
178 	public ActionForward addAffln(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
179         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
180         PersonDocumentAffiliation newAffln = personDocumentForm.getNewAffln();
181         newAffln.setDocumentNumber(personDocumentForm.getPersonDocument().getDocumentNumber());
182         newAffln.refreshReferenceObject("affiliationType");
183         personDocumentForm.getPersonDocument().getAffiliations().add(newAffln);
184         personDocumentForm.setNewAffln(new PersonDocumentAffiliation());        
185         return mapping.findForward(RiceConstants.MAPPING_BASIC);
186     }
187 	
188     public ActionForward deleteAffln(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
189         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
190         personDocumentForm.getPersonDocument().getAffiliations().remove(getLineToDelete(request));
191         return mapping.findForward(RiceConstants.MAPPING_BASIC);
192     }
193     public ActionForward addCitizenship(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
194         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
195         PersonDocumentCitizenship newCitizenship = personDocumentForm.getNewCitizenship();
196         personDocumentForm.getPersonDocument().getCitizenships().add(newCitizenship);
197         personDocumentForm.setNewCitizenship(new PersonDocumentCitizenship());        
198         return mapping.findForward(RiceConstants.MAPPING_BASIC);
199     }
200     
201     public ActionForward deleteCitizenship(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
202         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
203         personDocumentForm.getPersonDocument().getCitizenships().remove(getLineToDelete(request));
204         return mapping.findForward(RiceConstants.MAPPING_BASIC);
205     }
206 
207     public ActionForward addEmpInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
208         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
209         IdentityManagementPersonDocument personDOc = personDocumentForm.getPersonDocument();
210         PersonDocumentAffiliation affiliation = personDOc.getAffiliations().get(getSelectedLine(request));        
211         PersonDocumentEmploymentInfo newempInfo = affiliation.getNewEmpInfo();
212         newempInfo.setDocumentNumber(personDOc.getDocumentNumber());
213         newempInfo.setVersionNumber(new Long(1));
214         affiliation.getEmpInfos().add(newempInfo);
215         affiliation.setNewEmpInfo(new PersonDocumentEmploymentInfo());        
216         return mapping.findForward(RiceConstants.MAPPING_BASIC);
217     }
218     
219     public ActionForward deleteEmpInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
220         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
221         String selectedIndexes = getSelectedParentChildIdx(request);
222         if (selectedIndexes != null) {
223 	        String [] indexes = StringUtils.split(selectedIndexes,":");
224 	        PersonDocumentAffiliation affiliation = personDocumentForm.getPersonDocument().getAffiliations().get(Integer.parseInt(indexes[0]));
225 	        affiliation.getEmpInfos().remove(Integer.parseInt(indexes[1]));
226         }
227         return mapping.findForward(RiceConstants.MAPPING_BASIC);
228     }
229     
230     protected String getSelectedParentChildIdx(HttpServletRequest request) {
231     	String lineNumber = null;
232         String parameterName = (String) request.getAttribute(KNSConstants.METHOD_TO_CALL_ATTRIBUTE);
233         if (StringUtils.isNotBlank(parameterName)) {
234             lineNumber = StringUtils.substringBetween(parameterName, ".line", ".");
235         }
236         return lineNumber;
237     }
238 
239     public ActionForward addName(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
240         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
241         PersonDocumentName newName = personDocumentForm.getNewName();
242         newName.setDocumentNumber(personDocumentForm.getDocument().getDocumentNumber());
243         personDocumentForm.getPersonDocument().getNames().add(newName);
244         personDocumentForm.setNewName(new PersonDocumentName());        
245         return mapping.findForward(RiceConstants.MAPPING_BASIC);
246     }
247     
248     public ActionForward deleteName(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
249         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
250         personDocumentForm.getPersonDocument().getNames().remove(getLineToDelete(request));
251         return mapping.findForward(RiceConstants.MAPPING_BASIC);
252     }
253 
254     public ActionForward addAddress(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
255         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
256         PersonDocumentAddress newAddress = personDocumentForm.getNewAddress();
257         newAddress.setDocumentNumber(personDocumentForm.getDocument().getDocumentNumber());
258         personDocumentForm.getPersonDocument().getAddrs().add(newAddress);
259         personDocumentForm.setNewAddress(new PersonDocumentAddress());        
260         return mapping.findForward(RiceConstants.MAPPING_BASIC);
261     }
262     
263     public ActionForward deleteAddress(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
264         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
265         personDocumentForm.getPersonDocument().getAddrs().remove(getLineToDelete(request));
266         return mapping.findForward(RiceConstants.MAPPING_BASIC);
267     }
268 
269     public ActionForward addPhone(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
270         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
271         PersonDocumentPhone newPhone = personDocumentForm.getNewPhone();
272         newPhone.setDocumentNumber(personDocumentForm.getDocument().getDocumentNumber());
273         personDocumentForm.getPersonDocument().getPhones().add(newPhone);
274         personDocumentForm.setNewPhone(new PersonDocumentPhone());        
275         return mapping.findForward(RiceConstants.MAPPING_BASIC);
276     }
277     
278     public ActionForward deletePhone(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
279         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
280         personDocumentForm.getPersonDocument().getPhones().remove(getLineToDelete(request));
281         return mapping.findForward(RiceConstants.MAPPING_BASIC);
282     }
283 
284     public ActionForward addEmail(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
285         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
286         PersonDocumentEmail newEmail = personDocumentForm.getNewEmail();
287         newEmail.setDocumentNumber(personDocumentForm.getDocument().getDocumentNumber());
288         personDocumentForm.getPersonDocument().getEmails().add(newEmail);
289         personDocumentForm.setNewEmail(new PersonDocumentEmail());        
290         return mapping.findForward(RiceConstants.MAPPING_BASIC);
291     }
292     
293     public ActionForward deleteEmail(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
294         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
295         personDocumentForm.getPersonDocument().getEmails().remove(getLineToDelete(request));
296         return mapping.findForward(RiceConstants.MAPPING_BASIC);
297     }
298 
299     public ActionForward addGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
300         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
301         PersonDocumentGroup newGroup = personDocumentForm.getNewGroup();
302         if (newGroup.getGroupName() == null 
303         		&& newGroup.getNamespaceCode() == null 
304         		&& newGroup.getGroupId() != null) {
305         	GroupInfo tempGroup = KIMServiceLocator.getIdentityManagementService().getGroup(newGroup.getGroupId());
306         	newGroup.setGroupName(tempGroup.getGroupName());
307 	        newGroup.setNamespaceCode(tempGroup.getNamespaceCode());
308 	        newGroup.setKimTypeId(tempGroup.getKimTypeId());
309         } else if (newGroup.getGroupName() != null 
310         		&& newGroup.getNamespaceCode() != null 
311         		&& newGroup.getGroupId() == null) {
312         	GroupInfo tempGroup = KIMServiceLocator.getIdentityManagementService().getGroupByName(newGroup.getNamespaceCode(), newGroup.getGroupName());
313         	newGroup.setGroupId(tempGroup.getGroupId());
314 	        newGroup.setKimTypeId(tempGroup.getKimTypeId());
315         }
316         if (getKualiRuleService().applyRules(new AddGroupEvent("",personDocumentForm.getPersonDocument(), newGroup))) {
317 	        GroupInfo group = getIdentityManagementService().getGroup(newGroup.getGroupId());
318 	        newGroup.setGroupName(group.getGroupName());
319 	        newGroup.setNamespaceCode(group.getNamespaceCode());
320 	        newGroup.setKimTypeId(group.getKimTypeId());
321         	personDocumentForm.getPersonDocument().getGroups().add(newGroup);
322 	        personDocumentForm.setNewGroup(new PersonDocumentGroup());
323         }
324         return mapping.findForward(RiceConstants.MAPPING_BASIC);
325     }
326     
327     public ActionForward deleteGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
328         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
329         PersonDocumentGroup inactivedGroupMembership = personDocumentForm.getPersonDocument().getGroups().get(getLineToDelete(request));
330         Calendar cal = Calendar.getInstance();
331         inactivedGroupMembership.setActiveToDate(new Timestamp(cal.getTimeInMillis()));        
332         personDocumentForm.getPersonDocument().getGroups().set(getLineToDelete(request), inactivedGroupMembership);
333         return mapping.findForward(RiceConstants.MAPPING_BASIC);
334     }
335 
336     public ActionForward addRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
337         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
338         PersonDocumentRole newRole = personDocumentForm.getNewRole();
339         if (getKualiRuleService().applyRules(new AddRoleEvent("",personDocumentForm.getPersonDocument(), newRole))) {
340 	        KimRoleInfo role = KIMServiceLocator.getRoleService().getRole(newRole.getRoleId());
341 	        if(!validateRole(newRole.getRoleId(), role, PersonDocumentRoleRule.ERROR_PATH, "Person")){
342 	        	return mapping.findForward(RiceConstants.MAPPING_BASIC);
343 	        }
344 	        newRole.setRoleName(role.getRoleName());
345 	        newRole.setNamespaceCode(role.getNamespaceCode());
346 	        newRole.setKimTypeId(role.getKimTypeId());
347 	        if(!validateRoleAssignment(personDocumentForm.getPersonDocument(), newRole)){
348 	        	return mapping.findForward(RiceConstants.MAPPING_BASIC);
349 	        }
350 	        KimTypeService kimTypeService = getKimTypeService(newRole.getKimRoleType());
351 	        //AttributeDefinitionMap definitions = kimTypeService.getAttributeDefinitions();
352 	        // role type populated from form is not a complete record
353 	        newRole.getKimRoleType().setKimTypeId(newRole.getKimTypeId());
354 	        if ( kimTypeService != null ) {
355 	        	newRole.setDefinitions(kimTypeService.getAttributeDefinitions(newRole.getKimTypeId()));
356 	        }
357 	        KimDocumentRoleMember newRolePrncpl = newRole.getNewRolePrncpl();
358 	        newRole.refreshReferenceObject("assignedResponsibilities");
359 	        
360 	        for (String key : newRole.getDefinitions().keySet()) {
361 	        	KimDocumentRoleQualifier qualifier = new KimDocumentRoleQualifier();
362 	        	//qualifier.setQualifierKey(key);
363 	        	setAttrDefnIdForQualifier(qualifier,newRole.getDefinitions().get(key));
364 	        	newRolePrncpl.getQualifiers().add(qualifier);
365 	        }
366 	        if (newRole.getDefinitions().isEmpty()) {
367 	        	List<KimDocumentRoleMember> rolePrncpls = new ArrayList<KimDocumentRoleMember>();
368 	        	setupRoleRspActions(newRole, newRolePrncpl);
369 	            rolePrncpls.add(newRolePrncpl);
370 	        	newRole.setRolePrncpls(rolePrncpls);
371 	        }
372 	        //newRole.setNewRolePrncpl(newRolePrncpl);
373 	        newRole.setAttributeEntry( getUiDocumentService().getAttributeEntries( newRole.getDefinitions() ) );
374 	        personDocumentForm.getPersonDocument().getRoles().add(newRole);
375 	        personDocumentForm.setNewRole(new PersonDocumentRole());
376         }
377         return mapping.findForward(RiceConstants.MAPPING_BASIC);
378     }
379     
380 	protected boolean validateRoleAssignment(IdentityManagementPersonDocument document, PersonDocumentRole newRole){
381         boolean rulePassed = true;
382         if(!document.validAssignRole(newRole)){
383 			GlobalVariables.getMessageMap().putError("newRole.roleId", 
384 					RiceKeyConstants.ERROR_ASSIGN_ROLE, 
385 					new String[] {newRole.getNamespaceCode(), newRole.getRoleName()});
386 	        rulePassed = false;
387         }
388 		return rulePassed;
389 	}
390 
391     protected void setupRoleRspActions(PersonDocumentRole role, KimDocumentRoleMember rolePrncpl) {
392         for (RoleResponsibilityImpl roleResp : role.getAssignedResponsibilities()) {
393         	if (getResponsibilityService().areActionsAtAssignmentLevelById(roleResp.getResponsibilityId())) {
394         		KimDocumentRoleResponsibilityAction roleRspAction = new KimDocumentRoleResponsibilityAction();
395         		roleRspAction.setRoleResponsibilityId("*");        		
396         		roleRspAction.refreshReferenceObject("roleResponsibility");
397         		if(rolePrncpl.getRoleRspActions()==null || rolePrncpl.getRoleRspActions().size()<1){
398         			if(rolePrncpl.getRoleRspActions()==null)
399         				rolePrncpl.setRoleRspActions(new ArrayList<KimDocumentRoleResponsibilityAction>());
400         			 rolePrncpl.getRoleRspActions().add(roleRspAction);
401         		}
402         	}        	
403         }
404     }
405     
406 //	protected boolean isUniqueRoleRspAction(List<KimDocumentRoleResponsibilityAction> roleRspActions, KimDocumentRoleResponsibilityAction roleRspAction){
407 //    	if(roleRspActions==null || roleRspAction==null) return false;
408 //    	for(KimDocumentRoleResponsibilityAction roleRspActionTemp: roleRspActions){
409 //    		if((StringUtils.isNotEmpty(roleRspActionTemp.getRoleMemberId()) && roleRspActionTemp.getRoleMemberId().equals(roleRspAction.getRoleMemberId())) && 
410 //    			(StringUtils.isNotEmpty(roleRspActionTemp.getRoleResponsibilityId())	&& roleRspActionTemp.getRoleResponsibilityId().equals(roleRspAction.getRoleResponsibilityId())))
411 //    			return false;
412 //    	}
413 //    	return true;
414 //    }
415 	    
416 
417     protected void setAttrDefnIdForQualifier(KimDocumentRoleQualifier qualifier,AttributeDefinition definition) {
418     	if (definition instanceof KimDataDictionaryAttributeDefinition) {
419     		qualifier.setKimAttrDefnId(((KimDataDictionaryAttributeDefinition)definition).getKimAttrDefnId());
420     		qualifier.refreshReferenceObject("kimAttribute");
421     	} else {
422     		qualifier.setKimAttrDefnId(((KimNonDataDictionaryAttributeDefinition)definition).getKimAttrDefnId());
423     		qualifier.refreshReferenceObject("kimAttribute");
424 
425     	}
426     }
427     
428     public ActionForward deleteRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
429         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
430         personDocumentForm.getPersonDocument().getRoles().remove(getLineToDelete(request));
431         return mapping.findForward(RiceConstants.MAPPING_BASIC);
432     }
433 
434     public ActionForward addRoleQualifier(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
435         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
436         IdentityManagementPersonDocument personDOc = personDocumentForm.getPersonDocument();
437         int selectedRoleIdx = getSelectedLine(request);
438         PersonDocumentRole role = personDOc.getRoles().get(selectedRoleIdx);
439         KimDocumentRoleMember newRolePrncpl = role.getNewRolePrncpl();
440     	
441     	if (getKualiRuleService().applyRules(new AddPersonDocumentRoleQualifierEvent("",
442     			personDOc, newRolePrncpl, role, selectedRoleIdx))) {
443         	setupRoleRspActions(role, newRolePrncpl);
444     		role.getRolePrncpls().add(newRolePrncpl);
445 	        role.setNewRolePrncpl(new KimDocumentRoleMember());
446 	        for (String key : role.getDefinitions().keySet()) {
447 	        	KimDocumentRoleQualifier qualifier = new KimDocumentRoleQualifier();
448 	        	//qualifier.setQualifierKey(key);
449 	        	setAttrDefnIdForQualifier(qualifier,role.getDefinitions().get(key));
450 	        	role.getNewRolePrncpl().getQualifiers().add(qualifier);
451 	        }
452     	}
453 
454         return mapping.findForward(RiceConstants.MAPPING_BASIC);
455     }
456 
457     public ActionForward deleteRoleQualifier(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
458         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
459         String selectedIndexes = getSelectedParentChildIdx(request);
460         if (selectedIndexes != null) {
461 	        String [] indexes = StringUtils.split(selectedIndexes,":");
462 	        PersonDocumentRole role = personDocumentForm.getPersonDocument().getRoles().get(Integer.parseInt(indexes[0]));
463 	        role.getRolePrncpls().remove(Integer.parseInt(indexes[1]));
464         }
465         return mapping.findForward(RiceConstants.MAPPING_BASIC);
466     }
467     
468     public ActionForward addDelegationMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
469         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
470         IdentityManagementPersonDocument personDocument = (IdentityManagementPersonDocument)personDocumentForm.getPersonDocument();
471         RoleDocumentDelegationMember newDelegationMember = personDocumentForm.getNewDelegationMember();
472         KimTypeAttributesHelper attrHelper = newDelegationMember.getAttributesHelper();
473         if (getKualiRuleService().applyRules(new AddPersonDelegationMemberEvent("", personDocumentForm.getPersonDocument(), newDelegationMember))) {
474 	        //RoleImpl roleImpl = (RoleImpl)getUiDocumentService().getMember(KimConstants.KimUIConstants.MEMBER_TYPE_ROLE_CODE, newDelegationMember.getRoleImpl().getRoleId());
475 	        KimRoleInfo roleInfo = null;
476         	roleInfo = KIMServiceLocator.getRoleService().getRole(newDelegationMember.getRoleImpl().getRoleId());
477 	        if (roleInfo != null) {
478 		        if(!validateRole(newDelegationMember.getRoleImpl().getRoleId(),roleInfo, PersonDocumentRoleRule.ERROR_PATH, "Person")){
479 		        	return mapping.findForward(RiceConstants.MAPPING_BASIC);
480 		        }
481 		        newDelegationMember.setRoleImpl(convertRoleInfoToRoleImpl(roleInfo));
482 	        }
483 	        AttributeDefinition attrDefinition;
484 	        RoleMemberImpl roleMember = KIMServiceLocator.getUiDocumentService().getRoleMember(newDelegationMember.getRoleMemberId());
485 	        AttributeSet roleMemberAttributes = (new AttributeValidationHelper()).convertAttributesToMap(roleMember.getAttributes());
486 	        for (String key: attrHelper.getDefinitions().keySet()) {
487 	        	RoleDocumentDelegationMemberQualifier qualifier = new RoleDocumentDelegationMemberQualifier();
488 	        	attrDefinition = attrHelper.getDefinitions().get(key);
489 	        	qualifier.setKimAttrDefnId(attrHelper.getKimAttributeDefnId(attrDefinition));
490 	        	qualifier.setAttrVal(attrHelper.getAttributeValue(roleMemberAttributes, attrDefinition.getName()));
491 	        	newDelegationMember.setMemberId(personDocument.getPrincipalId());
492 	        	newDelegationMember.setMemberTypeCode(KimConstants.KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE);
493 	        	newDelegationMember.getQualifiers().add(qualifier);
494 	        }
495 	        //newDelegationMember.setAttributeEntry(getUiDocumentService().getAttributeEntries(newDelegationMember.getAttributesHelper().getDefinitions())));
496 	        personDocument.getDelegationMembers().add(newDelegationMember);
497 	        personDocumentForm.setNewDelegationMember(new RoleDocumentDelegationMember());
498         }
499         return mapping.findForward(RiceConstants.MAPPING_BASIC);
500     }
501     
502     public ActionForward deleteDelegationMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
503         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
504         personDocumentForm.getPersonDocument().getDelegationMembers().remove(getLineToDelete(request));
505         return mapping.findForward(RiceConstants.MAPPING_BASIC);
506     }
507 
508 	@Override
509 	public ActionForward save(ActionMapping mapping, ActionForm form,
510 			HttpServletRequest request, HttpServletResponse response)
511 			throws Exception {
512 
513 		return super.save(mapping, form, request, response);
514 	}
515 	
516     @Override
517     public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
518         IdentityManagementPersonDocumentForm impdForm = (IdentityManagementPersonDocumentForm) form;
519 
520         ActionForward forward = this.refreshAfterDelegationMemberRoleSelection(mapping, impdForm, request);
521         if (forward != null) {
522             return forward;
523         }
524 
525         return super.refresh(mapping, form, request, response);
526 	}
527 
528     public ActionForward performLookup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
529     	return super.performLookup(mapping, form, request, response);
530     }
531     
532     protected ActionForward refreshAfterDelegationMemberRoleSelection(ActionMapping mapping, IdentityManagementPersonDocumentForm impdForm, HttpServletRequest request) {
533         String refreshCaller = impdForm.getRefreshCaller();
534 
535         boolean isRoleLookupable = KimConstants.KimUIConstants.ROLE_LOOKUPABLE_IMPL.equals(refreshCaller);
536         boolean isRoleMemberLookupable = KimConstants.KimUIConstants.KIM_DOCUMENT_ROLE_MEMBER_LOOKUPABLE_IMPL.equals(refreshCaller);
537 
538         // do not execute the further refreshing logic if the refresh caller is not a lookupable
539         if (!isRoleLookupable && !isRoleMemberLookupable) {
540             return null;
541         }
542 
543         //In case of delegation member lookup impdForm.getNewDelegationMemberRoleId() will be populated.
544         if(impdForm.getNewDelegationMemberRoleId() == null){
545             return null;
546         }
547         if(isRoleLookupable){
548             return renderRoleMemberSelection(mapping, request, impdForm);
549         }
550 
551         String roleMemberId = request.getParameter(KimConstants.PrimaryKeyConstants.ROLE_MEMBER_ID);
552         if(StringUtils.isNotBlank(roleMemberId)){
553             impdForm.getNewDelegationMember().setRoleMemberId(roleMemberId);
554             RoleMemberImpl roleMember = getUiDocumentService().getRoleMember(roleMemberId);
555             impdForm.getNewDelegationMember().setRoleMemberMemberId(roleMember.getMemberId());
556             impdForm.getNewDelegationMember().setRoleMemberMemberTypeCode(roleMember.getMemberTypeCode());
557             impdForm.getNewDelegationMember().setRoleMemberName(getUiDocumentService().getMemberName(impdForm.getNewDelegationMember().getRoleMemberMemberTypeCode(), impdForm.getNewDelegationMember().getRoleMemberMemberId()));
558             impdForm.getNewDelegationMember().setRoleMemberNamespaceCode(getUiDocumentService().getMemberNamespaceCode(impdForm.getNewDelegationMember().getRoleMemberMemberTypeCode(), impdForm.getNewDelegationMember().getRoleMemberMemberId()));
559 	        //RoleImpl roleImpl = (RoleImpl)getUiDocumentService().getMember(KimConstants.KimUIConstants.MEMBER_TYPE_ROLE_CODE, impdForm.getNewDelegationMember().getRoleImpl().getRoleId());
560             KimRoleInfo roleInfo = null;
561         	roleInfo = KIMServiceLocator.getRoleService().getRole(impdForm.getNewDelegationMember().getRoleImpl().getRoleId());
562 	        if (roleInfo != null) {
563 		        if(!validateRole(impdForm.getNewDelegationMember().getRoleImpl().getRoleId(), roleInfo, PersonDocumentRoleRule.ERROR_PATH, "Person")){
564 		        	return mapping.findForward(RiceConstants.MAPPING_BASIC);
565 		        }
566 		        impdForm.getNewDelegationMember().setRoleImpl(convertRoleInfoToRoleImpl(roleInfo));
567 	        }
568         }
569         impdForm.setNewDelegationMemberRoleId(null);
570         return null;
571     }
572     
573     protected ActionForward renderRoleMemberSelection(ActionMapping mapping, HttpServletRequest request, IdentityManagementPersonDocumentForm impdForm) {
574         Properties props = new Properties();
575 
576         props.put(KNSConstants.SUPPRESS_ACTIONS, Boolean.toString(true));
577         props.put(KNSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, KimDocumentRoleMember.class.getName());
578         props.put(KNSConstants.LOOKUP_ANCHOR, KNSConstants.ANCHOR_TOP_OF_FORM);
579         props.put(KNSConstants.LOOKED_UP_COLLECTION_NAME, KimConstants.KimUIConstants.ROLE_MEMBERS_COLLECTION_NAME);
580 
581         String conversionPatttern = "{0}" + KNSConstants.FIELD_CONVERSION_PAIR_SEPARATOR + "{0}";
582         StringBuilder fieldConversion = new StringBuilder();
583         fieldConversion.append(MessageFormat.format(conversionPatttern, 
584        		KimConstants.PrimaryKeyConstants.ROLE_ID)).append(KNSConstants.FIELD_CONVERSIONS_SEPARATOR);
585         fieldConversion.append(MessageFormat.format(conversionPatttern, 
586            		KimConstants.PrimaryKeyConstants.ROLE_MEMBER_ID)).append(KNSConstants.FIELD_CONVERSIONS_SEPARATOR);
587 
588         props.put(KNSConstants.CONVERSION_FIELDS_PARAMETER, fieldConversion);
589 
590         props.put(KimConstants.PrimaryKeyConstants.ROLE_ID, impdForm.getNewDelegationMember().getRoleImpl().getRoleId());
591 
592         props.put(KNSConstants.RETURN_LOCATION_PARAMETER, this.getReturnLocation(request, mapping));
593         props.put(KNSConstants.BACK_LOCATION, this.getReturnLocation(request, mapping));
594 
595         props.put(KNSConstants.LOOKUP_AUTO_SEARCH, "Yes");
596         props.put(KNSConstants.DISPATCH_REQUEST_PARAMETER, KNSConstants.SEARCH_METHOD);
597 
598         props.put(KNSConstants.DOC_FORM_KEY, GlobalVariables.getUserSession().addObject(impdForm));
599         props.put(KNSConstants.DOC_NUM, impdForm.getDocument().getDocumentNumber());
600 
601         // TODO: how should this forward be handled
602         String url = UrlFactory.parameterizeUrl(getBasePath(request) + "/kr/" + KNSConstants.LOOKUP_ACTION, props);
603 
604         impdForm.registerEditableProperty("methodToCall");
605 
606         return new ActionForward(url, true);
607     }
608     
609     private RoleImpl convertRoleInfoToRoleImpl(KimRoleInfo roleInfo) {
610 	    RoleImpl roleImpl = new RoleImpl();
611 	    roleImpl.setKimTypeId(roleInfo.getKimTypeId());
612 	    roleImpl.setNamespaceCode(roleInfo.getNamespaceCode());
613 	    roleImpl.setRoleDescription(roleInfo.getRoleDescription());
614 	    roleImpl.setRoleId(roleInfo.getRoleId());
615 	    roleImpl.setRoleName(roleInfo.getRoleName());
616 	    roleImpl.setActive(roleInfo.isActive());
617 	    return roleImpl;
618     }
619 }