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