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  import org.kuali.rice.kim.bo.entity.impl.KimEntityAffiliationImpl;
28  import org.kuali.rice.kim.bo.entity.impl.KimEntityEmailImpl;
29  import org.kuali.rice.kim.bo.entity.impl.KimEntityEmploymentInformationImpl;
30  import org.kuali.rice.kim.bo.entity.impl.KimEntityEntityTypeImpl;
31  import org.kuali.rice.kim.bo.entity.impl.KimEntityImpl;
32  import org.kuali.rice.kim.bo.entity.impl.KimEntityNameImpl;
33  import org.kuali.rice.kim.bo.entity.impl.KimPrincipalImpl;
34  import org.kuali.rice.kns.service.KNSServiceLocator;
35  import org.kuali.rice.kns.service.SequenceAccessorService;
36  
37  /**
38   * Adds password to the User xml ingestion
39   *
40   */
41  public class KSUserXmlParser extends UserXmlParser {
42  	
43      protected final Logger LOG = Logger.getLogger(getClass());
44  	
45      private static final Namespace NAMESPACE = Namespace.getNamespace("", "ns:workflow/User");
46  
47      private static final String WORKFLOW_ID_ELEMENT = "workflowId";
48      private static final String AUTHENTICATION_ID_ELEMENT = "authenticationId";
49      private static final String PRINCIPAL_ID_ELEMENT = "principalId";
50      private static final String PRINCIPAL_NAME_ELEMENT = "principalName";
51      private static final String EMPL_ID_ELEMENT = "emplId";
52      private static final String EMAIL_ELEMENT = "emailAddress";
53      private static final String GIVEN_NAME_ELEMENT = "givenName";
54      private static final String LAST_NAME_ELEMENT = "lastName";    
55      private static final String TYPE_ELEMENT = "type";
56      private static final String PASSWORD_ELEMENT = "password";
57      private static final String HASH_SUFFIX = "(&^HSH#&)";
58  
59  	private static final String AFFILIATION_CD_ELEMENT = "affiliationTypeCode";
60  	private static final String ACTIVE_ELEMENT = "active";
61  	private static final String CAMPUS_CD_ELEMENT = "campusCode";
62  	
63      @Override
64      protected KimEntityImpl constructEntity(Element userElement) {
65          SequenceAccessorService sas = KNSServiceLocator.getSequenceAccessorService();
66      	
67      	String firstName = userElement.getChildTextTrim(GIVEN_NAME_ELEMENT, NAMESPACE);
68          String lastName = userElement.getChildTextTrim(LAST_NAME_ELEMENT, NAMESPACE);
69          String emplId = userElement.getChildTextTrim(EMPL_ID_ELEMENT, NAMESPACE);
70          String entityTypeCode = userElement.getChildTextTrim(TYPE_ELEMENT, NAMESPACE);
71          if (StringUtils.isBlank(entityTypeCode)) {
72          	entityTypeCode = "PERSON";
73          }
74      	
75          Long entityId = sas.getNextAvailableSequenceNumber("KRIM_ENTITY_ID_S", 
76          		KimEntityEmploymentInformationImpl.class);
77          
78          // if they define an empl id, let's set that up
79          KimEntityEmploymentInformationImpl emplInfo = null;
80          if (!StringUtils.isBlank(emplId)) {
81          	emplInfo = new KimEntityEmploymentInformationImpl();
82          	emplInfo.setActive(true);
83          	emplInfo.setEmployeeId(emplId);
84          	emplInfo.setPrimary(true);
85          	emplInfo.setEntityId("" + entityId);
86          	emplInfo.setEntityEmploymentId(emplId);
87          }
88          
89      	
90  		KimEntityImpl entity = new KimEntityImpl();
91  		
92  		//Set active on the entity
93  		String active = userElement.getChildTextTrim(ACTIVE_ELEMENT, NAMESPACE);
94  		if("false".equals(active)){
95  			entity.setActive(false);
96  		}else{
97  			entity.setActive(true);
98  		}
99  		
100 		
101 		entity.setEntityId("" + entityId);
102 		List<KimEntityEmploymentInformationImpl> emplInfos = new ArrayList<KimEntityEmploymentInformationImpl>();
103 		if (emplInfo != null) {
104 			emplInfos.add(emplInfo);
105 		}
106 		entity.setEmploymentInformation(emplInfos);
107 		
108 		//Add affiliations
109 		String affiliationTypeCode = userElement.getChildTextTrim(AFFILIATION_CD_ELEMENT, NAMESPACE);
110 		if (!StringUtils.isBlank(affiliationTypeCode)) {
111 			if(entity.getAffiliations()==null){
112 				entity.setAffiliations(new ArrayList<KimEntityAffiliationImpl>());
113 			}
114 			KimEntityAffiliationImpl affiliation = new KimEntityAffiliationImpl();
115 			
116 			Long affiliationId = sas.getNextAvailableSequenceNumber(
117 					"KRIM_ENTITY_AFLTN_ID_S", KimEntityAffiliationImpl.class);
118 			affiliation.setEntityAffiliationId(""+affiliationId);
119 			affiliation.setAffiliationTypeCode(affiliationTypeCode);
120 			affiliation.setActive(true);
121 			affiliation.setDefault(true);
122 			affiliation.setEntityId(entity.getEntityId());
123 			String campusCode = userElement.getChildTextTrim(CAMPUS_CD_ELEMENT, NAMESPACE);
124 			if(!StringUtils.isBlank(campusCode)){
125 				affiliation.setCampusCode(campusCode);
126 			}
127 			entity.getAffiliations().add(affiliation);
128 		}		
129 		
130 		KimEntityEntityTypeImpl entityType = new KimEntityEntityTypeImpl();
131 		entity.getEntityTypes().add(entityType);
132 		entityType.setEntityTypeCode(entityTypeCode);
133 		entityType.setEntityId(entity.getEntityId());
134 		entityType.setActive(true);
135 		
136 		if (!StringUtils.isBlank(firstName) || !StringUtils.isBlank(lastName)) {
137 			Long entityNameId = sas.getNextAvailableSequenceNumber(
138 					"KRIM_ENTITY_NM_ID_S", KimEntityNameImpl.class);
139 			KimEntityNameImpl name = new KimEntityNameImpl();
140 			name.setActive(true);
141 			name.setEntityNameId("" + entityNameId);
142 			name.setEntityId(entity.getEntityId());
143 			// must be in krim_ent_nm_typ_t.ent_nm_typ_cd
144 			name.setNameTypeCode("PRFR");
145 			name.setFirstName(firstName);
146 			name.setMiddleName("");
147 			name.setLastName(lastName);
148 			name.setDefault(true);
149 			
150 			entity.getNames().add(name);
151 		}
152 		
153 		KNSServiceLocator.getBusinessObjectService().save(entity);
154 		
155 		String emailAddress = userElement.getChildTextTrim(EMAIL_ELEMENT, NAMESPACE);
156 		if (!StringUtils.isBlank(emailAddress)) {
157 			Long emailId = sas.getNextAvailableSequenceNumber(
158 					"KRIM_ENTITY_EMAIL_ID_S", KimEntityEmailImpl.class);
159 			KimEntityEmailImpl email = new KimEntityEmailImpl();
160 			email.setActive(true);
161 			email.setEntityEmailId("" + emailId);
162 			email.setEntityTypeCode("PERSON");
163 			// must be in krim_email_typ_t.email_typ_cd:
164 			email.setEmailTypeCode("WRK");
165 			email.setEmailAddress(emailAddress);
166 			email.setDefault(true);
167 			email.setEntityId(entity.getEntityId());
168 			KNSServiceLocator.getBusinessObjectService().save(email);
169 		}
170 		
171 		return entity;
172     }
173     
174     @Override
175 	protected KimPrincipalImpl constructPrincipal(Element userElement, String entityId) {
176     	String principalId = userElement.getChildTextTrim(WORKFLOW_ID_ELEMENT, NAMESPACE);
177     	if (principalId == null) {
178     		principalId = userElement.getChildTextTrim(PRINCIPAL_ID_ELEMENT, NAMESPACE);
179     	}
180     	String principalName = userElement.getChildTextTrim(AUTHENTICATION_ID_ELEMENT, NAMESPACE);
181     	if (principalName == null) {
182     		principalName = userElement.getChildTextTrim(PRINCIPAL_NAME_ELEMENT, NAMESPACE);
183     	}
184     	String password= userElement.getChildTextTrim(PASSWORD_ELEMENT, NAMESPACE);
185     	
186     	
187 		KimPrincipalImpl principal = new KimPrincipalImpl();
188 		principal.setActive(true);
189 		principal.setPrincipalId(principalId);
190 		principal.setPrincipalName(principalName);
191 		principal.setEntityId(entityId);
192 		try {
193 			principal.setPassword(KNSServiceLocator.getEncryptionService().hash(password)+HASH_SUFFIX);
194 		} catch (GeneralSecurityException e) {
195 			LOG.warn("Error hashing password.",e);
196 		}
197 		KNSServiceLocator.getBusinessObjectService().save(principal);
198 		
199 		return principal;
200     }
201 }