1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.xml;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import javax.xml.parsers.ParserConfigurationException;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.jdom.Document;
28 import org.jdom.Element;
29 import org.jdom.JDOMException;
30 import org.jdom.Namespace;
31 import org.kuali.rice.kew.exception.InvalidXmlException;
32 import org.kuali.rice.kew.util.XmlHelper;
33 import org.kuali.rice.kim.bo.entity.impl.KimEntityEmailImpl;
34 import org.kuali.rice.kim.bo.entity.impl.KimEntityEmploymentInformationImpl;
35 import org.kuali.rice.kim.bo.entity.impl.KimEntityEntityTypeImpl;
36 import org.kuali.rice.kim.bo.entity.impl.KimEntityImpl;
37 import org.kuali.rice.kim.bo.entity.impl.KimEntityNameImpl;
38 import org.kuali.rice.kim.bo.entity.impl.KimPrincipalImpl;
39 import org.kuali.rice.kns.service.KNSServiceLocator;
40 import org.kuali.rice.kns.service.SequenceAccessorService;
41 import org.xml.sax.SAXException;
42
43
44
45
46
47
48
49
50
51
52
53
54 public class UserXmlParser implements XmlConstants {
55
56 private static final Namespace NAMESPACE = Namespace.getNamespace("", "ns:workflow/User");
57
58 private static final String USERS_ELEMENT = "users";
59 private static final String USER_ELEMENT = "user";
60 private static final String WORKFLOW_ID_ELEMENT = "workflowId";
61 private static final String AUTHENTICATION_ID_ELEMENT = "authenticationId";
62 private static final String PRINCIPAL_ID_ELEMENT = "principalId";
63 private static final String PRINCIPAL_NAME_ELEMENT = "principalName";
64 private static final String EMPL_ID_ELEMENT = "emplId";
65 private static final String EMAIL_ELEMENT = "emailAddress";
66 private static final String GIVEN_NAME_ELEMENT = "givenName";
67 private static final String LAST_NAME_ELEMENT = "lastName";
68 private static final String TYPE_ELEMENT = "type";
69
70 public void parseUsers(InputStream input) throws IOException, InvalidXmlException {
71 try {
72 Document doc = XmlHelper.trimSAXXml(input);
73 Element root = doc.getRootElement();
74 parseUsers(root);
75 } catch (JDOMException e) {
76 throw new InvalidXmlException("Parse error.", e);
77 } catch (SAXException e){
78 throw new InvalidXmlException("Parse error.",e);
79 } catch(ParserConfigurationException e){
80 throw new InvalidXmlException("Parse error.",e);
81 }
82 }
83
84 public void parseUsers(Element root) throws InvalidXmlException {
85 for (Iterator usersElementIt = root.getChildren(USERS_ELEMENT, NAMESPACE).iterator(); usersElementIt.hasNext();) {
86 Element usersElement = (Element) usersElementIt.next();
87 for (Iterator iterator = usersElement.getChildren(USER_ELEMENT, NAMESPACE).iterator(); iterator.hasNext();) {
88 Element userElement = (Element) iterator.next();
89 KimEntityImpl entity = constructEntity(userElement);
90 constructPrincipal(userElement, entity.getEntityId());
91 }
92 }
93 }
94
95 protected KimEntityImpl constructEntity(Element userElement) {
96 SequenceAccessorService sas = KNSServiceLocator.getSequenceAccessorService();
97
98 String firstName = userElement.getChildTextTrim(GIVEN_NAME_ELEMENT, NAMESPACE);
99 String lastName = userElement.getChildTextTrim(LAST_NAME_ELEMENT, NAMESPACE);
100 String emplId = userElement.getChildTextTrim(EMPL_ID_ELEMENT, NAMESPACE);
101 String entityTypeCode = userElement.getChildTextTrim(TYPE_ELEMENT, NAMESPACE);
102 if (StringUtils.isBlank(entityTypeCode)) {
103 entityTypeCode = "PERSON";
104 }
105
106 Long entityId = sas.getNextAvailableSequenceNumber("KRIM_ENTITY_ID_S",
107 KimEntityEmploymentInformationImpl.class);
108
109
110 KimEntityEmploymentInformationImpl emplInfo = null;
111 if (!StringUtils.isBlank(emplId)) {
112 emplInfo = new KimEntityEmploymentInformationImpl();
113 emplInfo.setActive(true);
114 emplInfo.setEmployeeId(emplId);
115 emplInfo.setPrimary(true);
116 emplInfo.setEntityId("" + entityId);
117 emplInfo.setEntityEmploymentId(emplId);
118 }
119
120
121 KimEntityImpl entity = new KimEntityImpl();
122 entity.setActive(true);
123 entity.setEntityId("" + entityId);
124 List<KimEntityEmploymentInformationImpl> emplInfos = new ArrayList<KimEntityEmploymentInformationImpl>();
125 if (emplInfo != null) {
126 emplInfos.add(emplInfo);
127 }
128 entity.setEmploymentInformation(emplInfos);
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
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
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 protected KimPrincipalImpl constructPrincipal(Element userElement, String entityId) {
175 String principalId = userElement.getChildTextTrim(WORKFLOW_ID_ELEMENT, NAMESPACE);
176 if (principalId == null) {
177 principalId = userElement.getChildTextTrim(PRINCIPAL_ID_ELEMENT, NAMESPACE);
178 }
179 String principalName = userElement.getChildTextTrim(AUTHENTICATION_ID_ELEMENT, NAMESPACE);
180 if (principalName == null) {
181 principalName = userElement.getChildTextTrim(PRINCIPAL_NAME_ELEMENT, NAMESPACE);
182 }
183
184 KimPrincipalImpl principal = new KimPrincipalImpl();
185 principal.setActive(true);
186 principal.setPrincipalId(principalId);
187 principal.setPrincipalName(principalName);
188 principal.setEntityId(entityId);
189 KNSServiceLocator.getBusinessObjectService().save(principal);
190
191 return principal;
192 }
193
194 }