View Javadoc

1   /**
2    * Copyright 2005-2015 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.kew.xml;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.jdom.Document;
20  import org.jdom.Element;
21  import org.jdom.JDOMException;
22  import org.jdom.Namespace;
23  import org.kuali.rice.core.api.util.xml.XmlException;
24  import org.kuali.rice.core.api.util.xml.XmlHelper;
25  import org.kuali.rice.kim.api.identity.CodedAttribute;
26  import org.kuali.rice.kim.api.identity.email.EntityEmail;
27  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
28  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
29  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
30  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
31  import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
32  import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
33  import org.kuali.rice.krad.service.KRADServiceLocator;
34  import org.kuali.rice.krad.service.SequenceAccessorService;
35  import org.xml.sax.SAXException;
36  
37  import javax.xml.parsers.ParserConfigurationException;
38  import java.io.IOException;
39  import java.io.InputStream;
40  import java.util.ArrayList;
41  import java.util.Collections;
42  import java.util.Iterator;
43  import java.util.List;
44  
45  /**
46   * Parses users from XML.
47   * 
48   * This is really meant for use only in the unit tests and was written to help ease
49   * transition over to KIM.  There are numerous unit tests which took advantage of
50   * the ability to import "users" from XML in KEW.  KIM does not provide XML
51   * import capabilities in the initial implementation so this class provides that.
52   *
53   * @author Kuali Rice Team (rice.collab@kuali.org)
54   *
55   */
56  public class UserXmlParser {
57      
58      private static final Namespace NAMESPACE = Namespace.getNamespace("", "ns:workflow/User");
59  
60      private static final String USERS_ELEMENT = "users";
61      private static final String USER_ELEMENT = "user";
62      private static final String WORKFLOW_ID_ELEMENT = "workflowId";
63      private static final String AUTHENTICATION_ID_ELEMENT = "authenticationId";
64      private static final String PRINCIPAL_ID_ELEMENT = "principalId";
65      private static final String PRINCIPAL_NAME_ELEMENT = "principalName";
66      private static final String EMPL_ID_ELEMENT = "emplId";
67      private static final String EMAIL_ELEMENT = "emailAddress";
68      private static final String GIVEN_NAME_ELEMENT = "givenName";
69      private static final String LAST_NAME_ELEMENT = "lastName";    
70      private static final String TYPE_ELEMENT = "type";
71      
72      public void parseUsers(InputStream input) throws IOException, XmlException {
73          try {
74              Document doc = XmlHelper.trimSAXXml(input);
75              Element root = doc.getRootElement();
76              parseUsers(root);
77          } catch (JDOMException e) {
78              throw new XmlException("Parse error.", e);
79          } catch (SAXException e){
80              throw new XmlException("Parse error.",e);
81          } catch(ParserConfigurationException e){
82              throw new XmlException("Parse error.",e);
83          }
84      }
85  
86      public void parseUsers(Element root) throws XmlException {
87      	for (Iterator usersElementIt = root.getChildren(USERS_ELEMENT, NAMESPACE).iterator(); usersElementIt.hasNext();) {
88      		Element usersElement = (Element) usersElementIt.next();
89      		for (Iterator iterator = usersElement.getChildren(USER_ELEMENT, NAMESPACE).iterator(); iterator.hasNext();) {
90      			Element userElement = (Element) iterator.next();
91      			EntityBo entity = constructEntity(userElement);
92      			constructPrincipal(userElement, entity.getId());
93      		}
94      	}
95      }
96      
97      protected EntityBo constructEntity(Element userElement) {
98          SequenceAccessorService sas = KRADServiceLocator.getSequenceAccessorService();
99      	
100     	String firstName = userElement.getChildTextTrim(GIVEN_NAME_ELEMENT, NAMESPACE);
101         String lastName = userElement.getChildTextTrim(LAST_NAME_ELEMENT, NAMESPACE);
102         String emplId = userElement.getChildTextTrim(EMPL_ID_ELEMENT, NAMESPACE);
103         String entityTypeCode = userElement.getChildTextTrim(TYPE_ELEMENT, NAMESPACE);
104         if (StringUtils.isBlank(entityTypeCode)) {
105         	entityTypeCode = "PERSON";
106         }
107     	
108         Long entityId = sas.getNextAvailableSequenceNumber("KRIM_ENTITY_ID_S", 
109         		EntityEmploymentBo.class);
110         
111         // if they define an empl id, let's set that up
112         EntityEmploymentBo emplInfo = null;
113         if (!StringUtils.isBlank(emplId)) {
114         	emplInfo = new EntityEmploymentBo();
115         	emplInfo.setActive(true);
116         	emplInfo.setEmployeeId(emplId);
117         	emplInfo.setPrimary(true);
118         	emplInfo.setEntityId("" + entityId);
119         	emplInfo.setId(emplId);
120         	emplInfo.setEntityAffiliationId(null);
121         }
122         
123     	
124 		EntityBo entity = new EntityBo();
125 		entity.setActive(true);
126 		entity.setId("" + entityId);
127 		List<EntityEmploymentBo> emplInfos = new ArrayList<EntityEmploymentBo>();
128 		if (emplInfo != null) {
129 			emplInfos.add(emplInfo);
130 		}
131 		entity.setEmploymentInformation(emplInfos);
132 		
133 		EntityTypeContactInfoBo entityType = new EntityTypeContactInfoBo();
134 		//identity.getEntityTypes().add(entityType);
135 		entityType.setEntityTypeCode(entityTypeCode);
136 		entityType.setEntityId(entity.getId());
137 		entityType.setActive(true);
138 		entityType.setVersionNumber(new Long(1));
139 		String emailAddress = userElement.getChildTextTrim(EMAIL_ELEMENT, NAMESPACE);
140 		if (!StringUtils.isBlank(emailAddress)) {
141 			Long emailId = sas.getNextAvailableSequenceNumber(
142 					"KRIM_ENTITY_EMAIL_ID_S", EntityEmailBo.class);
143 			EntityEmail.Builder email = EntityEmail.Builder.create();
144 			email.setActive(true);
145 			email.setId("" + emailId);
146 			email.setEntityTypeCode(entityTypeCode);
147 			// must be in krim_email_typ_t.email_typ_cd:
148 			email.setEmailType(CodedAttribute.Builder.create("WRK"));
149 			email.setVersionNumber(new Long(1));
150 			email.setEmailAddress(emailAddress);
151 			email.setDefaultValue(true);
152 			email.setEntityId(entity.getId());
153 			List<EntityEmailBo> emailAddresses = new ArrayList<EntityEmailBo>(1);
154 			emailAddresses.add(EntityEmailBo.from(email.build()));
155 			entityType.setEmailAddresses(emailAddresses);
156 			//email = (KimEntityEmailImpl)KRADServiceLocatorInternal.getBusinessObjectService().save(email);
157 		}
158 		List<EntityTypeContactInfoBo> entityTypes = new ArrayList<EntityTypeContactInfoBo>(1);
159 		entityTypes.add(entityType);
160 		entity.setEntityTypeContactInfos(entityTypes);
161 		
162 		if (!StringUtils.isBlank(firstName) || !StringUtils.isBlank(lastName)) {
163 			Long entityNameId = sas.getNextAvailableSequenceNumber(
164 					"KRIM_ENTITY_NM_ID_S", EntityNameBo.class);
165 			EntityNameBo name = new EntityNameBo();
166 			name.setActive(true);
167 			name.setId("" + entityNameId);
168 			name.setEntityId(entity.getId());
169 			// must be in krim_ent_nm_typ_t.ent_nm_typ_cd
170 			name.setNameCode("PRFR");
171 			name.setFirstName(firstName);
172 			name.setMiddleName("");
173 			name.setLastName(lastName);
174 			name.setDefaultValue(true);
175 			
176 			entity.setNames(Collections.singletonList(name));
177 		}
178 
179 		entity =  KRADServiceLocator.getBusinessObjectService().save(entity);
180 		
181 		return entity;
182     }
183     
184     protected PrincipalBo constructPrincipal(Element userElement, String entityId) {
185     	String principalId = userElement.getChildTextTrim(WORKFLOW_ID_ELEMENT, NAMESPACE);
186     	if (principalId == null) {
187     		principalId = userElement.getChildTextTrim(PRINCIPAL_ID_ELEMENT, NAMESPACE);
188     	}
189     	String principalName = userElement.getChildTextTrim(AUTHENTICATION_ID_ELEMENT, NAMESPACE);
190     	if (principalName == null) {
191     		principalName = userElement.getChildTextTrim(PRINCIPAL_NAME_ELEMENT, NAMESPACE);
192     	}
193     	
194 		PrincipalBo principal = new PrincipalBo();
195 		principal.setActive(true);
196 		principal.setPrincipalId(principalId);
197 		principal.setPrincipalName(principalName);
198 		principal.setEntityId(entityId);
199 		principal = (PrincipalBo) KRADServiceLocator.getBusinessObjectService().save(principal);
200 		
201 		return principal;
202     }
203 
204 }