View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.document;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.collections.CollectionUtils;
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.ojb.broker.PersistenceBroker;
26  import org.apache.ojb.broker.PersistenceBrokerException;
27  import org.kuali.rice.kew.dto.DocumentRouteStatusChangeDTO;
28  import org.kuali.rice.kim.bo.entity.dto.KimEntityEmploymentInformationInfo;
29  import org.kuali.rice.kim.bo.impl.KimAttributes;
30  import org.kuali.rice.kim.bo.role.dto.KimRoleInfo;
31  import org.kuali.rice.kim.bo.types.dto.KimTypeInfo;
32  import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
33  import org.kuali.rice.kim.bo.ui.KimDocumentRoleQualifier;
34  import org.kuali.rice.kim.bo.ui.PersonDocumentAddress;
35  import org.kuali.rice.kim.bo.ui.PersonDocumentAffiliation;
36  import org.kuali.rice.kim.bo.ui.PersonDocumentCitizenship;
37  import org.kuali.rice.kim.bo.ui.PersonDocumentEmail;
38  import org.kuali.rice.kim.bo.ui.PersonDocumentEmploymentInfo;
39  import org.kuali.rice.kim.bo.ui.PersonDocumentGroup;
40  import org.kuali.rice.kim.bo.ui.PersonDocumentName;
41  import org.kuali.rice.kim.bo.ui.PersonDocumentPhone;
42  import org.kuali.rice.kim.bo.ui.PersonDocumentPrivacy;
43  import org.kuali.rice.kim.bo.ui.PersonDocumentRole;
44  import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMember;
45  import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMemberQualifier;
46  import org.kuali.rice.kim.service.KIMServiceLocator;
47  import org.kuali.rice.kim.service.UiDocumentService;
48  import org.kuali.rice.kim.util.KimCommonUtils;
49  import org.kuali.rice.kim.util.KimConstants;
50  import org.kuali.rice.kns.service.DocumentHelperService;
51  import org.kuali.rice.kns.service.KNSServiceLocator;
52  import org.kuali.rice.kns.util.GlobalVariables;
53  
54  /**
55   * This is a description of what this class does - shyu don't forget to fill
56   * this in.
57   *
58   * @author Kuali Rice Team (rice.collab@kuali.org)
59   *
60   */
61  public class IdentityManagementPersonDocument extends IdentityManagementKimDocument {
62  
63      protected static final long serialVersionUID = -534993712085516925L;
64      // principal data
65      protected String principalId;
66      protected String principalName;   
67      protected String entityId;
68      protected String password;
69  
70      // ext id - now hard coded for "tax id" & "univ id"
71      protected String univId = "";
72      // affiliation data
73      protected List<PersonDocumentAffiliation> affiliations;
74  
75      protected String campusCode = "";
76      // external identifier data
77      protected Map<String, String> externalIdentifiers = null;
78  
79      protected boolean active;
80  
81      // citizenship
82      protected List<PersonDocumentCitizenship> citizenships;
83      // protected List<DocEmploymentInfo> employmentInformations;
84      protected List<PersonDocumentName> names;
85      protected List<PersonDocumentAddress> addrs;
86      protected List<PersonDocumentPhone> phones;
87      protected List<PersonDocumentEmail> emails;
88      protected List<PersonDocumentGroup> groups;
89      protected List<PersonDocumentRole> roles;
90  
91      protected PersonDocumentPrivacy privacy;
92  
93      public IdentityManagementPersonDocument() {
94          affiliations = new ArrayList<PersonDocumentAffiliation>();
95          citizenships = new ArrayList<PersonDocumentCitizenship>();
96          // employmentInformations = new ArrayList<DocEmploymentInfo>();
97          names = new ArrayList<PersonDocumentName>();
98          addrs = new ArrayList<PersonDocumentAddress>();
99          phones = new ArrayList<PersonDocumentPhone>();
100         emails = new ArrayList<PersonDocumentEmail>();
101         groups = new ArrayList<PersonDocumentGroup>();
102         roles = new ArrayList<PersonDocumentRole>();
103         privacy = new PersonDocumentPrivacy();
104         this.active = true;
105         // privacy.setDocumentNumber(documentNumber);
106     }
107 
108     public String getPrincipalId() {
109         return this.principalId;
110     }
111 
112     public void setPrincipalId(String principalId) {
113         this.principalId = principalId;
114     }
115 
116     public String getPrincipalName() {
117         return this.principalName;
118     }
119 
120     /*
121      * sets the principal name.  
122      * Principal names are converted to lower case.
123      */
124     public void setPrincipalName(String principalName) {
125         this.principalName = principalName; // != null ? principalName.toLowerCase() : principalName ;
126     }
127 
128     public String getEntityId() {
129         return this.entityId;
130     }
131 
132     public void setEntityId(String entityId) {
133         this.entityId = entityId;
134     }
135 
136     public List<PersonDocumentAffiliation> getAffiliations() {
137         return this.affiliations;
138     }
139 
140     public void setAffiliations(List<PersonDocumentAffiliation> affiliations) {
141         this.affiliations = affiliations;
142     }
143 
144     public String getCampusCode() {
145         return this.campusCode;
146     }
147 
148     public void setCampusCode(String campusCode) {
149         this.campusCode = campusCode;
150     }
151 
152     public Map<String, String> getExternalIdentifiers() {
153         return this.externalIdentifiers;
154     }
155 
156     public void setExternalIdentifiers(Map<String, String> externalIdentifiers) {
157         this.externalIdentifiers = externalIdentifiers;
158     }
159 
160     public String getPassword() {
161         return this.password;
162     }
163 
164     public void setPassword(String password) {
165         this.password = password;
166     }
167 
168     public boolean isActive() {
169         return this.active;
170     }
171 
172     public void setActive(boolean active) {
173         this.active = active;
174     }
175 
176     public List<PersonDocumentCitizenship> getCitizenships() {
177         return this.citizenships;
178     }
179 
180     public void setCitizenships(List<PersonDocumentCitizenship> citizenships) {
181         this.citizenships = citizenships;
182     }
183 
184     public List<PersonDocumentName> getNames() {
185         return this.names;
186     }
187 
188     public void setNames(List<PersonDocumentName> names) {
189         this.names = names;
190     }
191 
192     public List<PersonDocumentAddress> getAddrs() {
193         return this.addrs;
194     }
195 
196     public void setAddrs(List<PersonDocumentAddress> addrs) {
197         this.addrs = addrs;
198     }
199 
200     public List<PersonDocumentPhone> getPhones() {
201         return this.phones;
202     }
203 
204     public void setPhones(List<PersonDocumentPhone> phones) {
205         this.phones = phones;
206     }
207 
208     public List<PersonDocumentEmail> getEmails() {
209         return this.emails;
210     }
211 
212     public void setEmails(List<PersonDocumentEmail> emails) {
213         this.emails = emails;
214     }
215 
216     public void setGroups(List<PersonDocumentGroup> groups) {
217         this.groups = groups;
218     }
219 
220     public List<PersonDocumentRole> getRoles() {
221         return this.roles;
222     }
223 
224     public void setRoles(List<PersonDocumentRole> roles) {
225         this.roles = roles;
226     }
227 
228     public List<PersonDocumentGroup> getGroups() {
229         return this.groups;
230     }
231 
232     public String getUnivId() {
233         return this.univId;
234     }
235 
236     public void setUnivId(String univId) {
237         this.univId = univId;
238     }
239 
240     public PersonDocumentPrivacy getPrivacy() {
241         return this.privacy;
242     }
243 
244     public void setPrivacy(PersonDocumentPrivacy privacy) {
245         this.privacy = privacy;
246     }
247 
248     public void initializeDocumentForNewPerson() {
249         if(StringUtils.isBlank(this.principalId)){
250             this.principalId = getSequenceAccessorService().getNextAvailableSequenceNumber(KimConstants.SequenceNames.KRIM_PRNCPL_ID_S).toString();
251         }
252         if(StringUtils.isBlank(this.entityId)){
253             this.entityId = getSequenceAccessorService().getNextAvailableSequenceNumber(KimConstants.SequenceNames.KRIM_ENTITY_ID_S).toString();
254         }
255     }
256 
257     @SuppressWarnings("unchecked")
258     @Override
259     public List buildListOfDeletionAwareLists() {
260         List managedLists = super.buildListOfDeletionAwareLists();
261         List<PersonDocumentEmploymentInfo> empInfos = new ArrayList<PersonDocumentEmploymentInfo>();
262         for (PersonDocumentAffiliation affiliation : getAffiliations()) {
263             empInfos.addAll(affiliation.getEmpInfos());
264         }
265 
266         managedLists.add(empInfos);
267         managedLists.add(getAffiliations());
268         managedLists.add(getCitizenships());
269         managedLists.add(getPhones());
270         managedLists.add(getAddrs());
271         managedLists.add(getEmails());
272         managedLists.add(getNames());
273         managedLists.add(getGroups());
274         managedLists.add(getRoles());
275         return managedLists;
276     }
277 
278     /**
279      * @see org.kuali.rice.kns.document.DocumentBase#doRouteStatusChange(org.kuali.rice.kew.dto.DocumentRouteStatusChangeDTO)
280      */
281     @Override
282     public void doRouteStatusChange(DocumentRouteStatusChangeDTO statusChangeEvent) {
283         super.doRouteStatusChange(statusChangeEvent);
284         if (getDocumentHeader().getWorkflowDocument().stateIsProcessed()) {
285         	setIfRolesEditable();
286             KIMServiceLocator.getUiDocumentService().saveEntityPerson(this);
287         }
288     }
289 
290 
291     @Override
292     public void prepareForSave(){
293         if (StringUtils.isBlank(getPrivacy().getDocumentNumber())) {
294             getPrivacy().setDocumentNumber(
295                     getDocumentNumber());
296         }
297         setEmployeeRecordIds();
298         for (PersonDocumentRole role : getRoles()) {
299             for (KimDocumentRoleMember rolePrncpl : role.getRolePrncpls()) {
300                 rolePrncpl.setDocumentNumber(getDocumentNumber());
301                 for (KimDocumentRoleQualifier qualifier : rolePrncpl.getQualifiers()) {
302                     qualifier.setDocumentNumber(getDocumentNumber());
303                     qualifier.setKimTypId(role.getKimTypeId());
304                 }
305             }
306         }
307         if(getDelegationMembers()!=null){
308             for(RoleDocumentDelegationMember delegationMember: getDelegationMembers()){
309                 delegationMember.setDocumentNumber(getDocumentNumber());
310                 for (RoleDocumentDelegationMemberQualifier qualifier: delegationMember.getQualifiers()) {
311                     qualifier.setDocumentNumber(getDocumentNumber());
312                     qualifier.setKimTypId(delegationMember.getRoleImpl().getKimTypeId());
313                 }
314                 addDelegationMemberToDelegation(delegationMember);
315             }
316         }
317     }
318 
319     protected void setEmployeeRecordIds(){
320     	List<KimEntityEmploymentInformationInfo> empInfos = getUiDocumentService().getEntityEmploymentInformationInfo(getEntityId());
321         for(PersonDocumentAffiliation affiliation: getAffiliations()) {
322             int employeeRecordCounter = CollectionUtils.isEmpty(empInfos) ? 0 : empInfos.size();
323             for(PersonDocumentEmploymentInfo empInfo: affiliation.getEmpInfos()){
324                 if(CollectionUtils.isNotEmpty(empInfos)){
325                     for(KimEntityEmploymentInformationInfo origEmpInfo: empInfos){
326                         if (origEmpInfo.getEntityEmploymentId().equals(empInfo.getEntityEmploymentId())) {
327                             empInfo.setEmploymentRecordId(origEmpInfo.getEmploymentRecordId());
328                         }
329                     }
330                 }
331                 if(StringUtils.isEmpty(empInfo.getEmploymentRecordId())){
332                     employeeRecordCounter++;
333                     empInfo.setEmploymentRecordId(employeeRecordCounter+"");
334                 }
335             }
336         }
337     }
338 
339     public KimTypeAttributesHelper getKimTypeAttributesHelper(String roleId) {
340         KimRoleInfo roleInfo = KIMServiceLocator.getRoleService().getRole(roleId);
341         KimTypeInfo kimTypeInfo = KIMServiceLocator.getTypeInfoService().getKimType(roleInfo.getKimTypeId());
342         return new KimTypeAttributesHelper(kimTypeInfo);
343         //addDelegationRoleKimTypeAttributeHelper(roleId, helper);
344     }
345 
346 	public void setIfRolesEditable(){
347 		if(CollectionUtils.isNotEmpty(getRoles())){
348 			for(PersonDocumentRole role: getRoles()){
349 				role.setEditable(validAssignRole(role));
350 			}
351 		}
352 	}
353 
354 	public boolean validAssignRole(PersonDocumentRole role){
355         boolean rulePassed = true;
356         if(StringUtils.isNotEmpty(role.getNamespaceCode())){
357 	        Map<String,String> additionalPermissionDetails = new HashMap<String,String>();
358 	        additionalPermissionDetails.put(KimAttributes.NAMESPACE_CODE, role.getNamespaceCode());
359 	        additionalPermissionDetails.put(KimAttributes.ROLE_NAME, role.getRoleName());
360 			if (!getDocumentHelperService().getDocumentAuthorizer(this).isAuthorizedByTemplate(
361 					this,
362 					KimConstants.NAMESPACE_CODE,
363 					KimConstants.PermissionTemplateNames.ASSIGN_ROLE,
364 					GlobalVariables.getUserSession().getPrincipalId(),
365 					additionalPermissionDetails, null)){
366 	            rulePassed = false;
367 			}
368         }
369         return rulePassed;
370 	}
371 
372 	protected transient DocumentHelperService documentHelperService;
373 	protected transient UiDocumentService uiDocumentService;
374 
375 	protected DocumentHelperService getDocumentHelperService() {
376 	    if ( documentHelperService == null ) {
377 	        documentHelperService = KNSServiceLocator.getDocumentHelperService();
378 		}
379 	    return this.documentHelperService;
380 	}
381 
382 	protected UiDocumentService getUiDocumentService() {
383 	    if (uiDocumentService == null ) {
384 	    	uiDocumentService = KIMServiceLocator.getUiDocumentService();
385 		}
386 	    return this.uiDocumentService;
387 	}
388 
389 }