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 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.util.xml.XmlException; |
24 | |
import org.kuali.rice.core.util.xml.XmlHelper; |
25 | |
import org.kuali.rice.kim.api.entity.Type; |
26 | |
import org.kuali.rice.kim.api.entity.email.EntityEmail; |
27 | |
import org.kuali.rice.kim.bo.entity.impl.KimEntityEmploymentInformationImpl; |
28 | |
import org.kuali.rice.kim.bo.entity.impl.KimEntityImpl; |
29 | |
import org.kuali.rice.kim.bo.entity.impl.KimEntityNameImpl; |
30 | |
import org.kuali.rice.kim.impl.entity.email.EntityEmailBo; |
31 | |
import org.kuali.rice.kim.impl.entity.principal.PrincipalBo; |
32 | |
import org.kuali.rice.kim.impl.entity.type.EntityTypeDataBo; |
33 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
34 | |
import org.kuali.rice.kns.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.Iterator; |
42 | |
import java.util.List; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 0 | public class UserXmlParser { |
56 | |
|
57 | 0 | private static final Namespace NAMESPACE = Namespace.getNamespace("", "ns:workflow/User"); |
58 | |
|
59 | |
private static final String USERS_ELEMENT = "users"; |
60 | |
private static final String USER_ELEMENT = "user"; |
61 | |
private static final String WORKFLOW_ID_ELEMENT = "workflowId"; |
62 | |
private static final String AUTHENTICATION_ID_ELEMENT = "authenticationId"; |
63 | |
private static final String PRINCIPAL_ID_ELEMENT = "principalId"; |
64 | |
private static final String PRINCIPAL_NAME_ELEMENT = "principalName"; |
65 | |
private static final String EMPL_ID_ELEMENT = "emplId"; |
66 | |
private static final String EMAIL_ELEMENT = "emailAddress"; |
67 | |
private static final String GIVEN_NAME_ELEMENT = "givenName"; |
68 | |
private static final String LAST_NAME_ELEMENT = "lastName"; |
69 | |
private static final String TYPE_ELEMENT = "type"; |
70 | |
|
71 | |
public void parseUsers(InputStream input) throws IOException, XmlException { |
72 | |
try { |
73 | 0 | Document doc = XmlHelper.trimSAXXml(input); |
74 | 0 | Element root = doc.getRootElement(); |
75 | 0 | parseUsers(root); |
76 | 0 | } catch (JDOMException e) { |
77 | 0 | throw new XmlException("Parse error.", e); |
78 | 0 | } catch (SAXException e){ |
79 | 0 | throw new XmlException("Parse error.",e); |
80 | 0 | } catch(ParserConfigurationException e){ |
81 | 0 | throw new XmlException("Parse error.",e); |
82 | 0 | } |
83 | 0 | } |
84 | |
|
85 | |
public void parseUsers(Element root) throws XmlException { |
86 | 0 | for (Iterator usersElementIt = root.getChildren(USERS_ELEMENT, NAMESPACE).iterator(); usersElementIt.hasNext();) { |
87 | 0 | Element usersElement = (Element) usersElementIt.next(); |
88 | 0 | for (Iterator iterator = usersElement.getChildren(USER_ELEMENT, NAMESPACE).iterator(); iterator.hasNext();) { |
89 | 0 | Element userElement = (Element) iterator.next(); |
90 | 0 | KimEntityImpl entity = constructEntity(userElement); |
91 | 0 | constructPrincipal(userElement, entity.getEntityId()); |
92 | 0 | } |
93 | 0 | } |
94 | 0 | } |
95 | |
|
96 | |
protected KimEntityImpl constructEntity(Element userElement) { |
97 | 0 | SequenceAccessorService sas = KNSServiceLocator.getSequenceAccessorService(); |
98 | |
|
99 | 0 | String firstName = userElement.getChildTextTrim(GIVEN_NAME_ELEMENT, NAMESPACE); |
100 | 0 | String lastName = userElement.getChildTextTrim(LAST_NAME_ELEMENT, NAMESPACE); |
101 | 0 | String emplId = userElement.getChildTextTrim(EMPL_ID_ELEMENT, NAMESPACE); |
102 | 0 | String entityTypeCode = userElement.getChildTextTrim(TYPE_ELEMENT, NAMESPACE); |
103 | 0 | if (StringUtils.isBlank(entityTypeCode)) { |
104 | 0 | entityTypeCode = "PERSON"; |
105 | |
} |
106 | |
|
107 | 0 | Long entityId = sas.getNextAvailableSequenceNumber("KRIM_ENTITY_ID_S", |
108 | |
KimEntityEmploymentInformationImpl.class); |
109 | |
|
110 | |
|
111 | 0 | KimEntityEmploymentInformationImpl emplInfo = null; |
112 | 0 | if (!StringUtils.isBlank(emplId)) { |
113 | 0 | emplInfo = new KimEntityEmploymentInformationImpl(); |
114 | 0 | emplInfo.setActive(true); |
115 | 0 | emplInfo.setEmployeeId(emplId); |
116 | 0 | emplInfo.setPrimary(true); |
117 | 0 | emplInfo.setEntityId("" + entityId); |
118 | 0 | emplInfo.setEntityEmploymentId(emplId); |
119 | 0 | emplInfo.setEntityAffiliationId(null); |
120 | |
} |
121 | |
|
122 | |
|
123 | 0 | KimEntityImpl entity = new KimEntityImpl(); |
124 | 0 | entity.setActive(true); |
125 | 0 | entity.setEntityId("" + entityId); |
126 | 0 | List<KimEntityEmploymentInformationImpl> emplInfos = new ArrayList<KimEntityEmploymentInformationImpl>(); |
127 | 0 | if (emplInfo != null) { |
128 | 0 | emplInfos.add(emplInfo); |
129 | |
} |
130 | 0 | entity.setEmploymentInformation(emplInfos); |
131 | |
|
132 | 0 | EntityTypeDataBo entityType = new EntityTypeDataBo(); |
133 | |
|
134 | 0 | entityType.setEntityTypeCode(entityTypeCode); |
135 | 0 | entityType.setEntityId(entity.getEntityId()); |
136 | 0 | entityType.setActive(true); |
137 | 0 | entityType.setVersionNumber(new Long(1)); |
138 | 0 | String emailAddress = userElement.getChildTextTrim(EMAIL_ELEMENT, NAMESPACE); |
139 | 0 | if (!StringUtils.isBlank(emailAddress)) { |
140 | 0 | Long emailId = sas.getNextAvailableSequenceNumber( |
141 | |
"KRIM_ENTITY_EMAIL_ID_S", EntityEmailBo.class); |
142 | 0 | EntityEmail.Builder email = EntityEmail.Builder.create(); |
143 | 0 | email.setActive(true); |
144 | 0 | email.setId("" + emailId); |
145 | 0 | email.setEntityTypeCode(entityTypeCode); |
146 | |
|
147 | 0 | email.setEmailType(Type.Builder.create("WRK")); |
148 | 0 | email.setVersionNumber(new Long(1)); |
149 | 0 | email.setEmailAddress(emailAddress); |
150 | 0 | email.setDefaultValue(true); |
151 | 0 | email.setEntityId(entity.getEntityId()); |
152 | 0 | List<EntityEmailBo> emailAddresses = new ArrayList<EntityEmailBo>(1); |
153 | 0 | emailAddresses.add(EntityEmailBo.from(email.build())); |
154 | 0 | entityType.setEmailAddresses(emailAddresses); |
155 | |
|
156 | |
} |
157 | 0 | List<EntityTypeDataBo> entityTypes = new ArrayList<EntityTypeDataBo>(1); |
158 | 0 | entityTypes.add(entityType); |
159 | 0 | entity.setEntityTypes(entityTypes); |
160 | |
|
161 | 0 | if (!StringUtils.isBlank(firstName) || !StringUtils.isBlank(lastName)) { |
162 | 0 | Long entityNameId = sas.getNextAvailableSequenceNumber( |
163 | |
"KRIM_ENTITY_NM_ID_S", KimEntityNameImpl.class); |
164 | 0 | KimEntityNameImpl name = new KimEntityNameImpl(); |
165 | 0 | name.setActive(true); |
166 | 0 | name.setEntityNameId("" + entityNameId); |
167 | 0 | name.setEntityId(entity.getEntityId()); |
168 | |
|
169 | 0 | name.setNameTypeCode("PRFR"); |
170 | 0 | name.setFirstName(firstName); |
171 | 0 | name.setMiddleName(""); |
172 | 0 | name.setLastName(lastName); |
173 | 0 | name.setDefaultValue(true); |
174 | |
|
175 | 0 | entity.getNames().add(name); |
176 | |
} |
177 | |
|
178 | 0 | entity = (KimEntityImpl) KNSServiceLocator.getBusinessObjectService().save(entity); |
179 | |
|
180 | 0 | return entity; |
181 | |
} |
182 | |
|
183 | |
protected PrincipalBo constructPrincipal(Element userElement, String entityId) { |
184 | 0 | String principalId = userElement.getChildTextTrim(WORKFLOW_ID_ELEMENT, NAMESPACE); |
185 | 0 | if (principalId == null) { |
186 | 0 | principalId = userElement.getChildTextTrim(PRINCIPAL_ID_ELEMENT, NAMESPACE); |
187 | |
} |
188 | 0 | String principalName = userElement.getChildTextTrim(AUTHENTICATION_ID_ELEMENT, NAMESPACE); |
189 | 0 | if (principalName == null) { |
190 | 0 | principalName = userElement.getChildTextTrim(PRINCIPAL_NAME_ELEMENT, NAMESPACE); |
191 | |
} |
192 | |
|
193 | 0 | PrincipalBo principal = new PrincipalBo(); |
194 | 0 | principal.setActive(true); |
195 | 0 | principal.setPrincipalId(principalId); |
196 | 0 | principal.setPrincipalName(principalName); |
197 | 0 | principal.setEntityId(entityId); |
198 | 0 | principal = (PrincipalBo) KNSServiceLocator.getBusinessObjectService().save(principal); |
199 | |
|
200 | 0 | return principal; |
201 | |
} |
202 | |
|
203 | |
} |