View Javadoc

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