View Javadoc

1   /**
2    * Copyright 2005-2012 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().getGroupByNamespaceCodeAndName(
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 
337         if (getKualiRuleService().applyRules(new AddRoleEvent("",personDocumentForm.getPersonDocument(), newRole))) {
338          Role role = KimApiServiceLocator.getRoleService().getRole(newRole.getRoleId());
339          if(!validateRole(newRole.getRoleId(), role, PersonDocumentRoleRule.ERROR_PATH, "Person")){
340           return mapping.findForward(RiceConstants.MAPPING_BASIC);
341          }
342          newRole.setRoleName(role.getName());
343          newRole.setNamespaceCode(role.getNamespaceCode());
344          newRole.setKimTypeId(role.getKimTypeId());
345             KimDocumentRoleMember roleMember = new KimDocumentRoleMember();
346             roleMember.setMemberId(personDocumentForm.getPrincipalId());
347             roleMember.setMemberTypeCode(MemberType.PRINCIPAL.getCode());
348             roleMember.setRoleId(newRole.getRoleId());
349             newRole.setNewRolePrncpl(roleMember);
350          if(!validateRoleAssignment(personDocumentForm.getPersonDocument(), newRole)){
351           return mapping.findForward(RiceConstants.MAPPING_BASIC);
352          }
353          KimTypeService kimTypeService = getKimTypeService(KimTypeBo.to(newRole.getKimRoleType()));
354          //AttributeDefinitionMap definitions = kimTypeService.getAttributeDefinitions();
355          // role type populated from form is not a complete record
356          if ( kimTypeService != null ) {
357           newRole.setDefinitions(kimTypeService.getAttributeDefinitions(newRole.getKimTypeId()));
358          }
359          KimDocumentRoleMember newRolePrncpl = newRole.getNewRolePrncpl();
360          newRole.refreshReferenceObject("assignedResponsibilities");
361 
362          for (KimAttributeField key : newRole.getDefinitions()) {
363           KimDocumentRoleQualifier qualifier = new KimDocumentRoleQualifier();
364           //qualifier.setQualifierKey(key);
365           setAttrDefnIdForQualifier(qualifier,key);
366           newRolePrncpl.getQualifiers().add(qualifier);
367          }
368          if (newRole.getDefinitions().isEmpty()) {
369           List<KimDocumentRoleMember> rolePrncpls = new ArrayList<KimDocumentRoleMember>();
370           setupRoleRspActions(newRole, newRolePrncpl);
371              rolePrncpls.add(newRolePrncpl);
372           newRole.setRolePrncpls(rolePrncpls);
373          }
374          //newRole.setNewRolePrncpl(newRolePrncpl);
375          newRole.setAttributeEntry( getUiDocumentService().getAttributeEntries( newRole.getDefinitions() ) );
376          personDocumentForm.getPersonDocument().getRoles().add(newRole);
377          personDocumentForm.setNewRole(new PersonDocumentRole());
378         }
379         return mapping.findForward(RiceConstants.MAPPING_BASIC);
380     }
381     
382 	protected boolean validateRoleAssignment(IdentityManagementPersonDocument document, PersonDocumentRole newRole){
383         boolean rulePassed = true;
384         if(!document.validAssignRole(newRole)){
385 			GlobalVariables.getMessageMap().putError("newRole.roleId", 
386 					RiceKeyConstants.ERROR_ASSIGN_ROLE, 
387 					new String[] {newRole.getNamespaceCode(), newRole.getRoleName()});
388 	        rulePassed = false;
389         }
390 		return rulePassed;
391 	}
392 
393     protected void setupRoleRspActions(PersonDocumentRole role, KimDocumentRoleMember rolePrncpl) {
394         for (RoleResponsibilityBo roleResp : role.getAssignedResponsibilities()) {
395         	if (getResponsibilityInternalService().areActionsAtAssignmentLevelById(roleResp.getRoleResponsibilityId())) {
396         		KimDocumentRoleResponsibilityAction roleRspAction = new KimDocumentRoleResponsibilityAction();
397         		roleRspAction.setRoleResponsibilityId("*");        		
398         		roleRspAction.refreshReferenceObject("roleResponsibility");
399         		if(rolePrncpl.getRoleRspActions()==null || rolePrncpl.getRoleRspActions().size()<1){
400         			if(rolePrncpl.getRoleRspActions()==null) {
401 						rolePrncpl.setRoleRspActions(new ArrayList<KimDocumentRoleResponsibilityAction>());
402 					}
403         			 rolePrncpl.getRoleRspActions().add(roleRspAction);
404         		}
405         	}        	
406         }
407     }
408     
409 //	protected boolean isUniqueRoleRspAction(List<KimDocumentRoleResponsibilityAction> roleRspActions, KimDocumentRoleResponsibilityAction roleRspAction){
410 //    	if(roleRspActions==null || roleRspAction==null) return false;
411 //    	for(KimDocumentRoleResponsibilityAction roleRspActionTemp: roleRspActions){
412 //    		if((StringUtils.isNotEmpty(roleRspActionTemp.getRoleMemberId()) && roleRspActionTemp.getRoleMemberId().equals(roleRspAction.getRoleMemberId())) && 
413 //    			(StringUtils.isNotEmpty(roleRspActionTemp.getRoleResponsibilityId())	&& roleRspActionTemp.getRoleResponsibilityId().equals(roleRspAction.getRoleResponsibilityId())))
414 //    			return false;
415 //    	}
416 //    	return true;
417 //    }
418 	    
419 
420     protected void setAttrDefnIdForQualifier(KimDocumentRoleQualifier qualifier,KimAttributeField definition) {
421    		qualifier.setKimAttrDefnId(definition.getId());
422    		qualifier.refreshReferenceObject("kimAttribute");
423     }
424 
425     public ActionForward deleteRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
426         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
427         personDocumentForm.getPersonDocument().getRoles().remove(getLineToDelete(request));
428         return mapping.findForward(RiceConstants.MAPPING_BASIC);
429     }
430 
431     public ActionForward addRoleQualifier(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
432         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
433         IdentityManagementPersonDocument personDOc = personDocumentForm.getPersonDocument();
434         int selectedRoleIdx = getSelectedLine(request);
435         PersonDocumentRole role = personDOc.getRoles().get(selectedRoleIdx);
436         KimDocumentRoleMember newRolePrncpl = role.getNewRolePrncpl();
437         newRolePrncpl.setMemberTypeCode(MemberType.PRINCIPAL.getCode());
438         newRolePrncpl.setMemberId(personDOc.getPrincipalId());
439     	
440     	if (getKualiRuleService().applyRules(new AddPersonDocumentRoleQualifierEvent("",
441     			personDOc, newRolePrncpl, role, selectedRoleIdx))) {
442         	setupRoleRspActions(role, newRolePrncpl);
443     		role.getRolePrncpls().add(newRolePrncpl);
444             KimDocumentRoleMember roleMember = new KimDocumentRoleMember();
445             roleMember.setMemberTypeCode(MemberType.PRINCIPAL.getCode());
446             roleMember.setMemberId(personDocumentForm.getPrincipalId());
447 	        role.setNewRolePrncpl(roleMember);
448 	        for (KimAttributeField key : role.getDefinitions()) {
449 	        	KimDocumentRoleQualifier qualifier = new KimDocumentRoleQualifier();
450 	        	//qualifier.setQualifierKey(key);
451 	        	setAttrDefnIdForQualifier(qualifier,key);
452 	        	role.getNewRolePrncpl().getQualifiers().add(qualifier);
453 	        }
454     	}
455 
456         return mapping.findForward(RiceConstants.MAPPING_BASIC);
457     }
458 
459     public ActionForward deleteRoleQualifier(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
460         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
461         String selectedIndexes = getSelectedParentChildIdx(request);
462         if (selectedIndexes != null) {
463 	        String [] indexes = StringUtils.split(selectedIndexes,":");
464 	        PersonDocumentRole role = personDocumentForm.getPersonDocument().getRoles().get(Integer.parseInt(indexes[0]));
465 	        role.getRolePrncpls().remove(Integer.parseInt(indexes[1]));
466         }
467         return mapping.findForward(RiceConstants.MAPPING_BASIC);
468     }
469     
470     public ActionForward addDelegationMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
471         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
472         IdentityManagementPersonDocument personDocument = personDocumentForm.getPersonDocument();
473         RoleDocumentDelegationMember newDelegationMember = personDocumentForm.getNewDelegationMember();
474         KimTypeAttributesHelper attrHelper = newDelegationMember.getAttributesHelper();
475         if (getKualiRuleService().applyRules(new AddPersonDelegationMemberEvent("", personDocumentForm.getPersonDocument(), newDelegationMember))) {
476 	        //RoleImpl roleBo = (RoleImpl)getUiDocumentService().getMember(KimApiConstants.KimUIConstants.MEMBER_TYPE_ROLE_CODE, newDelegationMember.getRoleDao().getRoleId());
477 	        Role role;
478         	role = KimApiServiceLocator.getRoleService().getRole(newDelegationMember.getRoleBo().getId());
479 	        if (role != null) {
480 		        if(!validateRole(newDelegationMember.getRoleBo().getId(),role, PersonDocumentRoleRule.ERROR_PATH, "Person")){
481 		        	return mapping.findForward(RiceConstants.MAPPING_BASIC);
482 		        }
483 		        newDelegationMember.setRoleBo(RoleBo.from(role));
484 	        }
485 	        KimAttributeField attrDefinition;
486 	        RoleMemberBo roleMember = KIMServiceLocatorInternal.getUiDocumentService().getRoleMember(newDelegationMember.getRoleMemberId());
487 	        Map<String, String>
488                     roleMemberAttributes = (new AttributeValidationHelper()).convertAttributesToMap(roleMember.getAttributeDetails());
489 	        for (KimAttributeField key: attrHelper.getDefinitions()) {
490 	        	RoleDocumentDelegationMemberQualifier qualifier = new RoleDocumentDelegationMemberQualifier();
491 	        	attrDefinition = key;
492 	        	qualifier.setKimAttrDefnId(attrHelper.getKimAttributeDefnId(attrDefinition));
493 	        	qualifier.setAttrVal(attrHelper.getAttributeValue(roleMemberAttributes, attrDefinition.getAttributeField().getName()));
494 	        	newDelegationMember.setMemberId(personDocument.getPrincipalId());
495 	        	newDelegationMember.setMemberTypeCode(MemberType.PRINCIPAL.getCode());
496 	        	newDelegationMember.getQualifiers().add(qualifier);
497 	        }
498 	        //newDelegationMember.setAttributeEntry(getUiDocumentService().getAttributeEntries(newDelegationMember.getAttributesHelper().getDefinitions())));
499 	        personDocument.getDelegationMembers().add(newDelegationMember);
500 	        personDocumentForm.setNewDelegationMember(new RoleDocumentDelegationMember());
501         }
502         return mapping.findForward(RiceConstants.MAPPING_BASIC);
503     }
504     
505     public ActionForward deleteDelegationMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
506         IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
507         personDocumentForm.getPersonDocument().getDelegationMembers().remove(getLineToDelete(request));
508         return mapping.findForward(RiceConstants.MAPPING_BASIC);
509     }
510 
511 	@Override
512 	public ActionForward save(ActionMapping mapping, ActionForm form,
513 			HttpServletRequest request, HttpServletResponse response)
514 			throws Exception {
515 
516 		return super.save(mapping, form, request, response);
517 	}
518 	
519     @Override
520     public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
521         IdentityManagementPersonDocumentForm impdForm = (IdentityManagementPersonDocumentForm) form;
522 
523         ActionForward forward = this.refreshAfterDelegationMemberRoleSelection(mapping, impdForm, request);
524         if (forward != null) {
525             return forward;
526         }
527 
528         return super.refresh(mapping, form, request, response);
529 	}
530     
531     protected ActionForward refreshAfterDelegationMemberRoleSelection(ActionMapping mapping, IdentityManagementPersonDocumentForm impdForm, HttpServletRequest request) {
532         String refreshCaller = impdForm.getRefreshCaller();
533 
534         boolean isRoleLookupable = KimConstants.KimUIConstants.ROLE_LOOKUPABLE_IMPL.equals(refreshCaller);
535         boolean isRoleMemberLookupable = KimConstants.KimUIConstants.KIM_DOCUMENT_ROLE_MEMBER_LOOKUPABLE_IMPL.equals(refreshCaller);
536 
537         // do not execute the further refreshing logic if the refresh caller is not a lookupable
538         if (!isRoleLookupable && !isRoleMemberLookupable) {
539             return null;
540         }
541 
542         //In case of delegation member lookup impdForm.getNewDelegationMemberRoleId() will be populated.
543         if(impdForm.getNewDelegationMemberRoleId() == null){
544             return null;
545         }
546         if(isRoleLookupable){
547             return renderRoleMemberSelection(mapping, request, impdForm);
548         }
549 
550         String roleMemberId = request.getParameter(KimConstants.PrimaryKeyConstants.ROLE_MEMBER_ID);
551         if(StringUtils.isNotBlank(roleMemberId)){
552             impdForm.getNewDelegationMember().setRoleMemberId(roleMemberId);
553             RoleMemberBo roleMember = getUiDocumentService().getRoleMember(roleMemberId);
554             impdForm.getNewDelegationMember().setRoleMemberMemberId(roleMember.getMemberId());
555             impdForm.getNewDelegationMember().setRoleMemberMemberTypeCode(roleMember.getType().getCode());
556             impdForm.getNewDelegationMember().setRoleMemberName(getUiDocumentService().getMemberName(MemberType.fromCode(impdForm.getNewDelegationMember().getRoleMemberMemberTypeCode()), impdForm.getNewDelegationMember().getRoleMemberMemberId()));
557             impdForm.getNewDelegationMember().setRoleMemberNamespaceCode(getUiDocumentService().getMemberNamespaceCode(MemberType.fromCode(impdForm.getNewDelegationMember().getRoleMemberMemberTypeCode()), impdForm.getNewDelegationMember().getRoleMemberMemberId()));
558 
559             Role role;
560         	role = KimApiServiceLocator.getRoleService().getRole(impdForm.getNewDelegationMember().getRoleBo().getId());
561 	        if (role != null) {
562 		        if(!validateRole(impdForm.getNewDelegationMember().getRoleBo().getId(), role, PersonDocumentRoleRule.ERROR_PATH, "Person")){
563 		        	return mapping.findForward(RiceConstants.MAPPING_BASIC);
564 		        }
565 		        impdForm.getNewDelegationMember().setRoleBo(RoleBo.from(role));
566 	        }
567         }
568         impdForm.setNewDelegationMemberRoleId(null);
569         return null;
570     }
571     
572     protected ActionForward renderRoleMemberSelection(ActionMapping mapping, HttpServletRequest request, IdentityManagementPersonDocumentForm impdForm) {
573         Properties props = new Properties();
574 
575         props.put(KRADConstants.SUPPRESS_ACTIONS, Boolean.toString(true));
576         props.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, KimDocumentRoleMember.class.getName());
577         props.put(KRADConstants.LOOKUP_ANCHOR, KRADConstants.ANCHOR_TOP_OF_FORM);
578         props.put(KRADConstants.LOOKED_UP_COLLECTION_NAME, KimConstants.KimUIConstants.ROLE_MEMBERS_COLLECTION_NAME);
579 
580         String conversionPatttern = "{0}" + KRADConstants.FIELD_CONVERSION_PAIR_SEPARATOR + "{0}";
581         StringBuilder fieldConversion = new StringBuilder();
582         fieldConversion.append(MessageFormat.format(conversionPatttern, 
583        		KimConstants.PrimaryKeyConstants.SUB_ROLE_ID)).append(KRADConstants.FIELD_CONVERSIONS_SEPARATOR);
584         fieldConversion.append(MessageFormat.format(conversionPatttern, 
585            		KimConstants.PrimaryKeyConstants.ROLE_MEMBER_ID)).append(KRADConstants.FIELD_CONVERSIONS_SEPARATOR);
586 
587         props.put(KRADConstants.CONVERSION_FIELDS_PARAMETER, fieldConversion);
588 
589         props.put(KimConstants.PrimaryKeyConstants.SUB_ROLE_ID, impdForm.getNewDelegationMember().getRoleBo().getId());
590 
591         props.put(KRADConstants.RETURN_LOCATION_PARAMETER, this.getReturnLocation(request, mapping));
592      //   props.put(KRADConstants.BACK_LOCATION, this.getReturnLocation(request, mapping));
593 
594         props.put(KRADConstants.LOOKUP_AUTO_SEARCH, "Yes");
595         props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.SEARCH_METHOD);
596 
597         props.put(KRADConstants.DOC_FORM_KEY, GlobalVariables.getUserSession().addObjectWithGeneratedKey(impdForm));
598      //   props.put(KRADConstants.DOC_NUM, impdForm.getDocument().getDocumentNumber());
599 
600         // TODO: how should this forward be handled
601         String url = UrlFactory.parameterizeUrl(getApplicationBaseUrl() + "/kr/" + KRADConstants.LOOKUP_ACTION, props);
602 
603         impdForm.registerEditableProperty("methodToCall");
604 
605         return new ActionForward(url, true);
606     }
607 
608 
609     public ResponsibilityInternalService getResponsibilityInternalService() {
610     	if ( responsibilityInternalService == null ) {
611     		responsibilityInternalService = KimImplServiceLocator.getResponsibilityInternalService();
612     	}
613 		return responsibilityInternalService;
614 	}
615 }