View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.rice.student.kew.xml;
17  
18  import java.security.GeneralSecurityException;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.log4j.Logger;
24  import org.jdom.Element;
25  import org.jdom.Namespace;
26  import org.kuali.rice.kew.xml.UserXmlParser;
27  
28  import org.kuali.rice.core.api.CoreApiServiceLocator;
29  import org.kuali.rice.kew.xml.UserXmlParser;
30  import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationBo;
31  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
32  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
33  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
34  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
35  import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
36  import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
37  import org.kuali.rice.krad.service.KRADServiceLocator;
38  import org.kuali.rice.krad.service.SequenceAccessorService;
39  
40  
41  /**
42   * Adds password to the User xml ingestion
43   *
44   */
45  public class KSUserXmlParser extends UserXmlParser {
46  	
47      protected final Logger LOG = Logger.getLogger(getClass());
48  	
49      private static final Namespace NAMESPACE = Namespace.getNamespace("", "ns:workflow/User");
50  
51      private static final String WORKFLOW_ID_ELEMENT = "workflowId";
52      private static final String AUTHENTICATION_ID_ELEMENT = "authenticationId";
53      private static final String PRINCIPAL_ID_ELEMENT = "principalId";
54      private static final String PRINCIPAL_NAME_ELEMENT = "principalName";
55      private static final String EMPL_ID_ELEMENT = "emplId";
56      private static final String EMAIL_ELEMENT = "emailAddress";
57      private static final String GIVEN_NAME_ELEMENT = "givenName";
58      private static final String LAST_NAME_ELEMENT = "lastName";    
59      private static final String TYPE_ELEMENT = "type";
60      private static final String PASSWORD_ELEMENT = "password";
61      private static final String HASH_SUFFIX = "(&^HSH#&)";
62  
63  	private static final String AFFILIATION_CD_ELEMENT = "affiliationTypeCode";
64  	private static final String ACTIVE_ELEMENT = "active";
65  	private static final String CAMPUS_CD_ELEMENT = "campusCode";
66  	
67      @Override
68      protected EntityBo constructEntity(Element userElement) {
69          SequenceAccessorService sas = KRADServiceLocator.getSequenceAccessorService();
70      	
71      	String firstName = userElement.getChildTextTrim(GIVEN_NAME_ELEMENT, NAMESPACE);
72          String lastName = userElement.getChildTextTrim(LAST_NAME_ELEMENT, NAMESPACE);
73          String emplId = userElement.getChildTextTrim(EMPL_ID_ELEMENT, NAMESPACE);
74          String entityTypeCode = userElement.getChildTextTrim(TYPE_ELEMENT, NAMESPACE);
75          if (StringUtils.isBlank(entityTypeCode)) {
76          	entityTypeCode = "PERSON";
77          }
78      	
79          Long entityId = sas.getNextAvailableSequenceNumber("KRIM_ENTITY_ID_S", 
80          		EntityEmploymentBo.class);
81          
82          // if they define an empl id, let's set that up
83          EntityEmploymentBo emplInfo = null;
84          if (!StringUtils.isBlank(emplId)) {
85          	emplInfo = new EntityEmploymentBo();
86          	emplInfo.setActive(true);
87          	emplInfo.setEmployeeId(emplId);
88          	emplInfo.setPrimary(true);
89          	emplInfo.setEntityId("" + entityId);
90          }
91          
92      	
93  		EntityBo entity = new EntityBo();
94  		
95  		//Set active on the entity
96  		String active = userElement.getChildTextTrim(ACTIVE_ELEMENT, NAMESPACE);
97  		if("false".equals(active)){
98  			entity.setActive(false);
99  		}else{
100 			entity.setActive(true);
101         }
102 
103         entity.setId("" + entityId);
104 		List<EntityEmploymentBo> emplInfos = new ArrayList<EntityEmploymentBo>();
105 		if (emplInfo != null) {
106 			emplInfos.add(emplInfo);
107 		}
108 		entity.setEmploymentInformation(emplInfos);
109 
110 		//Add affiliations
111 		String affiliationTypeCode = userElement.getChildTextTrim(AFFILIATION_CD_ELEMENT, NAMESPACE);
112 		if (!StringUtils.isBlank(affiliationTypeCode)) {
113 			if(entity.getAffiliations()==null){
114 				entity.setAffiliations(new ArrayList<EntityAffiliationBo>());
115 			}
116 			EntityAffiliationBo affiliation = new EntityAffiliationBo();
117 
118             Long affiliationId = sas.getNextAvailableSequenceNumber(
119 					"KRIM_ENTITY_AFLTN_ID_S", EntityAffiliationBo.class);
120 			affiliation.setId(""+affiliationId);
121 			affiliation.setAffiliationTypeCode(affiliationTypeCode);
122 			affiliation.setActive(true);
123 			affiliation.setDefaultValue(true);
124 			affiliation.setEntityId(entity.getId());
125 			String campusCode = userElement.getChildTextTrim(CAMPUS_CD_ELEMENT, NAMESPACE);
126 			if(!StringUtils.isBlank(campusCode)){
127 				affiliation.setCampusCode(campusCode);
128 			}
129 			entity.getAffiliations().add(affiliation);
130 		}		
131 		
132 		EntityTypeContactInfoBo entityType = new EntityTypeContactInfoBo();
133 		entity.getEntityTypeContactInfos().add(entityType);
134 		entityType.setEntityTypeCode(entityTypeCode);
135 		entityType.setEntityId(entity.getId());
136 		entityType.setActive(true);
137 		
138 		if (!StringUtils.isBlank(firstName) || !StringUtils.isBlank(lastName)) {
139 			Long entityNameId = sas.getNextAvailableSequenceNumber(
140 					"KRIM_ENTITY_NM_ID_S", EntityNameBo.class);
141 			EntityNameBo name = new EntityNameBo();
142             name.setActive(true);
143             name.setId("" + entityNameId);
144             name.setEntityId(entity.getId());
145             // must be in krim_ent_nm_typ_t.ent_nm_typ_cd
146             name.setNameCode("PRFR");
147             name.setFirstName(firstName);
148             name.setMiddleName("");
149             name.setLastName(lastName);
150             name.setDefaultValue(true);
151 
152 			entity.getNames().add(name);
153 		}
154 		
155 		KRADServiceLocator.getBusinessObjectService().save(entity);
156 		
157 		String emailAddress = userElement.getChildTextTrim(EMAIL_ELEMENT, NAMESPACE);
158 		if (!StringUtils.isBlank(emailAddress)) {
159 			Long emailId = sas.getNextAvailableSequenceNumber(
160 					"KRIM_ENTITY_EMAIL_ID_S", EntityEmailBo.class);
161 			EntityEmailBo email = new EntityEmailBo();
162             email.setActive(true);
163 			email.setEntityTypeCode("PERSON");
164 			// must be in krim_email_typ_t.email_typ_cd:
165 			email.setEmailTypeCode("WRK");
166 			email.setEmailAddress(emailAddress);
167 			email.setDefaultValue(true);
168 			email.setEntityId(entity.getId());
169 
170 			KRADServiceLocator.getBusinessObjectService().save(email);
171 		}
172 		
173 		return entity;
174     }
175     
176     @Override
177 	protected PrincipalBo constructPrincipal(Element userElement, String entityId) {
178     	String principalId = userElement.getChildTextTrim(WORKFLOW_ID_ELEMENT, NAMESPACE);
179     	if (principalId == null) {
180     		principalId = userElement.getChildTextTrim(PRINCIPAL_ID_ELEMENT, NAMESPACE);
181     	}
182     	String principalName = userElement.getChildTextTrim(AUTHENTICATION_ID_ELEMENT, NAMESPACE);
183     	if (principalName == null) {
184     		principalName = userElement.getChildTextTrim(PRINCIPAL_NAME_ELEMENT, NAMESPACE);
185     	}
186     	String password= userElement.getChildTextTrim(PASSWORD_ELEMENT, NAMESPACE);
187     	
188     	
189 		PrincipalBo principal = new PrincipalBo();
190 		principal.setActive(true);
191 		principal.setPrincipalId(principalId);
192 		principal.setPrincipalName(principalName);
193 		principal.setEntityId(entityId);
194 		try {
195 			principal.setPassword(CoreApiServiceLocator.getEncryptionService().hash(password)+HASH_SUFFIX);
196 		} catch (GeneralSecurityException e) {
197 			LOG.warn("Error hashing password.",e);
198 		}
199 		KRADServiceLocator.getBusinessObjectService().save(principal);
200 		
201 		return principal;
202     }
203 }