| 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 | 0 |  public class UserXmlParser implements XmlConstants { | 
  | 55 |  |       | 
  | 56 | 0 |      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 | 0 |              Document doc = XmlHelper.trimSAXXml(input); | 
  | 73 | 0 |              Element root = doc.getRootElement(); | 
  | 74 | 0 |              parseUsers(root); | 
  | 75 | 0 |          } catch (JDOMException e) { | 
  | 76 | 0 |              throw new InvalidXmlException("Parse error.", e); | 
  | 77 | 0 |          } catch (SAXException e){ | 
  | 78 | 0 |              throw new InvalidXmlException("Parse error.",e); | 
  | 79 | 0 |          } catch(ParserConfigurationException e){ | 
  | 80 | 0 |              throw new InvalidXmlException("Parse error.",e); | 
  | 81 | 0 |          } | 
  | 82 | 0 |      } | 
  | 83 |  |   | 
  | 84 |  |      public void parseUsers(Element root) throws InvalidXmlException { | 
  | 85 | 0 |              for (Iterator usersElementIt = root.getChildren(USERS_ELEMENT, NAMESPACE).iterator(); usersElementIt.hasNext();) { | 
  | 86 | 0 |                      Element usersElement = (Element) usersElementIt.next(); | 
  | 87 | 0 |                      for (Iterator iterator = usersElement.getChildren(USER_ELEMENT, NAMESPACE).iterator(); iterator.hasNext();) { | 
  | 88 | 0 |                              Element userElement = (Element) iterator.next(); | 
  | 89 | 0 |                              KimEntityImpl entity = constructEntity(userElement); | 
  | 90 | 0 |                              constructPrincipal(userElement, entity.getEntityId()); | 
  | 91 | 0 |                      } | 
  | 92 | 0 |              } | 
  | 93 | 0 |      } | 
  | 94 |  |       | 
  | 95 |  |      protected KimEntityImpl constructEntity(Element userElement) { | 
  | 96 | 0 |          SequenceAccessorService sas = KNSServiceLocator.getSequenceAccessorService(); | 
  | 97 |  |               | 
  | 98 | 0 |              String firstName = userElement.getChildTextTrim(GIVEN_NAME_ELEMENT, NAMESPACE); | 
  | 99 | 0 |          String lastName = userElement.getChildTextTrim(LAST_NAME_ELEMENT, NAMESPACE); | 
  | 100 | 0 |          String emplId = userElement.getChildTextTrim(EMPL_ID_ELEMENT, NAMESPACE); | 
  | 101 | 0 |          String entityTypeCode = userElement.getChildTextTrim(TYPE_ELEMENT, NAMESPACE); | 
  | 102 | 0 |          if (StringUtils.isBlank(entityTypeCode)) { | 
  | 103 | 0 |                  entityTypeCode = "PERSON"; | 
  | 104 |  |          } | 
  | 105 |  |               | 
  | 106 | 0 |          Long entityId = sas.getNextAvailableSequenceNumber("KRIM_ENTITY_ID_S",  | 
  | 107 |  |                          KimEntityEmploymentInformationImpl.class); | 
  | 108 |  |           | 
  | 109 |  |           | 
  | 110 | 0 |          KimEntityEmploymentInformationImpl emplInfo = null; | 
  | 111 | 0 |          if (!StringUtils.isBlank(emplId)) { | 
  | 112 | 0 |                  emplInfo = new KimEntityEmploymentInformationImpl(); | 
  | 113 | 0 |                  emplInfo.setActive(true); | 
  | 114 | 0 |                  emplInfo.setEmployeeId(emplId); | 
  | 115 | 0 |                  emplInfo.setPrimary(true); | 
  | 116 | 0 |                  emplInfo.setEntityId("" + entityId); | 
  | 117 | 0 |                  emplInfo.setEntityEmploymentId(emplId); | 
  | 118 |  |          } | 
  | 119 |  |           | 
  | 120 |  |               | 
  | 121 | 0 |                  KimEntityImpl entity = new KimEntityImpl(); | 
  | 122 | 0 |                  entity.setActive(true); | 
  | 123 | 0 |                  entity.setEntityId("" + entityId); | 
  | 124 | 0 |                  List<KimEntityEmploymentInformationImpl> emplInfos = new ArrayList<KimEntityEmploymentInformationImpl>(); | 
  | 125 | 0 |                  if (emplInfo != null) { | 
  | 126 | 0 |                          emplInfos.add(emplInfo); | 
  | 127 |  |                  } | 
  | 128 | 0 |                  entity.setEmploymentInformation(emplInfos); | 
  | 129 |  |                   | 
  | 130 | 0 |                  KimEntityEntityTypeImpl entityType = new KimEntityEntityTypeImpl(); | 
  | 131 | 0 |                  entity.getEntityTypes().add(entityType); | 
  | 132 | 0 |                  entityType.setEntityTypeCode(entityTypeCode); | 
  | 133 | 0 |                  entityType.setEntityId(entity.getEntityId()); | 
  | 134 | 0 |                  entityType.setActive(true); | 
  | 135 |  |                   | 
  | 136 | 0 |                  if (!StringUtils.isBlank(firstName) || !StringUtils.isBlank(lastName)) { | 
  | 137 | 0 |                          Long entityNameId = sas.getNextAvailableSequenceNumber( | 
  | 138 |  |                                          "KRIM_ENTITY_NM_ID_S", KimEntityNameImpl.class); | 
  | 139 | 0 |                          KimEntityNameImpl name = new KimEntityNameImpl(); | 
  | 140 | 0 |                          name.setActive(true); | 
  | 141 | 0 |                          name.setEntityNameId("" + entityNameId); | 
  | 142 | 0 |                          name.setEntityId(entity.getEntityId()); | 
  | 143 |  |                           | 
  | 144 | 0 |                          name.setNameTypeCode("PRFR"); | 
  | 145 | 0 |                          name.setFirstName(firstName); | 
  | 146 | 0 |                          name.setMiddleName(""); | 
  | 147 | 0 |                          name.setLastName(lastName); | 
  | 148 | 0 |                          name.setDefault(true); | 
  | 149 |  |                           | 
  | 150 | 0 |                          entity.getNames().add(name); | 
  | 151 |  |                  } | 
  | 152 |  |                   | 
  | 153 | 0 |                  KNSServiceLocator.getBusinessObjectService().save(entity); | 
  | 154 |  |                   | 
  | 155 | 0 |                  String emailAddress = userElement.getChildTextTrim(EMAIL_ELEMENT, NAMESPACE); | 
  | 156 | 0 |                  if (!StringUtils.isBlank(emailAddress)) { | 
  | 157 | 0 |                          Long emailId = sas.getNextAvailableSequenceNumber( | 
  | 158 |  |                                          "KRIM_ENTITY_EMAIL_ID_S", KimEntityEmailImpl.class); | 
  | 159 | 0 |                          KimEntityEmailImpl email = new KimEntityEmailImpl(); | 
  | 160 | 0 |                          email.setActive(true); | 
  | 161 | 0 |                          email.setEntityEmailId("" + emailId); | 
  | 162 | 0 |                          email.setEntityTypeCode("PERSON"); | 
  | 163 |  |                           | 
  | 164 | 0 |                          email.setEmailTypeCode("WRK"); | 
  | 165 | 0 |                          email.setEmailAddress(emailAddress); | 
  | 166 | 0 |                          email.setDefault(true); | 
  | 167 | 0 |                          email.setEntityId(entity.getEntityId()); | 
  | 168 | 0 |                          KNSServiceLocator.getBusinessObjectService().save(email); | 
  | 169 |  |                  } | 
  | 170 |  |                   | 
  | 171 | 0 |                  return entity; | 
  | 172 |  |      } | 
  | 173 |  |       | 
  | 174 |  |      protected KimPrincipalImpl constructPrincipal(Element userElement, String entityId) { | 
  | 175 | 0 |              String principalId = userElement.getChildTextTrim(WORKFLOW_ID_ELEMENT, NAMESPACE); | 
  | 176 | 0 |              if (principalId == null) { | 
  | 177 | 0 |                      principalId = userElement.getChildTextTrim(PRINCIPAL_ID_ELEMENT, NAMESPACE); | 
  | 178 |  |              } | 
  | 179 | 0 |              String principalName = userElement.getChildTextTrim(AUTHENTICATION_ID_ELEMENT, NAMESPACE); | 
  | 180 | 0 |              if (principalName == null) { | 
  | 181 | 0 |                      principalName = userElement.getChildTextTrim(PRINCIPAL_NAME_ELEMENT, NAMESPACE); | 
  | 182 |  |              } | 
  | 183 |  |               | 
  | 184 | 0 |                  KimPrincipalImpl principal = new KimPrincipalImpl(); | 
  | 185 | 0 |                  principal.setActive(true); | 
  | 186 | 0 |                  principal.setPrincipalId(principalId); | 
  | 187 | 0 |                  principal.setPrincipalName(principalName); | 
  | 188 | 0 |                  principal.setEntityId(entityId); | 
  | 189 | 0 |                  KNSServiceLocator.getBusinessObjectService().save(principal); | 
  | 190 |  |                   | 
  | 191 | 0 |                  return principal; | 
  | 192 |  |      } | 
  | 193 |  |   | 
  | 194 |  |  } |