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