| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 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 |  |   | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 | 0 |  public class KSUserXmlParser extends UserXmlParser { | 
  | 42 |  |           | 
  | 43 | 0 |      protected final Logger LOG = Logger.getLogger(getClass()); | 
  | 44 |  |           | 
  | 45 | 0 |      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 | 0 |          SequenceAccessorService sas = KNSServiceLocator.getSequenceAccessorService(); | 
  | 66 |  |               | 
  | 67 | 0 |              String firstName = userElement.getChildTextTrim(GIVEN_NAME_ELEMENT, NAMESPACE); | 
  | 68 | 0 |          String lastName = userElement.getChildTextTrim(LAST_NAME_ELEMENT, NAMESPACE); | 
  | 69 | 0 |          String emplId = userElement.getChildTextTrim(EMPL_ID_ELEMENT, NAMESPACE); | 
  | 70 | 0 |          String entityTypeCode = userElement.getChildTextTrim(TYPE_ELEMENT, NAMESPACE); | 
  | 71 | 0 |          if (StringUtils.isBlank(entityTypeCode)) { | 
  | 72 | 0 |                  entityTypeCode = "PERSON"; | 
  | 73 |  |          } | 
  | 74 |  |               | 
  | 75 | 0 |          Long entityId = sas.getNextAvailableSequenceNumber("KRIM_ENTITY_ID_S",  | 
  | 76 |  |                          KimEntityEmploymentInformationImpl.class); | 
  | 77 |  |           | 
  | 78 |  |           | 
  | 79 | 0 |          KimEntityEmploymentInformationImpl emplInfo = null; | 
  | 80 | 0 |          if (!StringUtils.isBlank(emplId)) { | 
  | 81 | 0 |                  emplInfo = new KimEntityEmploymentInformationImpl(); | 
  | 82 | 0 |                  emplInfo.setActive(true); | 
  | 83 | 0 |                  emplInfo.setEmployeeId(emplId); | 
  | 84 | 0 |                  emplInfo.setPrimary(true); | 
  | 85 | 0 |                  emplInfo.setEntityId("" + entityId); | 
  | 86 | 0 |                  emplInfo.setEntityEmploymentId(emplId); | 
  | 87 |  |          } | 
  | 88 |  |           | 
  | 89 |  |               | 
  | 90 | 0 |                  KimEntityImpl entity = new KimEntityImpl(); | 
  | 91 |  |                   | 
  | 92 |  |                   | 
  | 93 | 0 |                  String active = userElement.getChildTextTrim(ACTIVE_ELEMENT, NAMESPACE); | 
  | 94 | 0 |                  if("false".equals(active)){ | 
  | 95 | 0 |                          entity.setActive(false); | 
  | 96 |  |                  }else{ | 
  | 97 | 0 |                          entity.setActive(true); | 
  | 98 |  |                  } | 
  | 99 |  |                   | 
  | 100 |  |                   | 
  | 101 | 0 |                  entity.setEntityId("" + entityId); | 
  | 102 | 0 |                  List<KimEntityEmploymentInformationImpl> emplInfos = new ArrayList<KimEntityEmploymentInformationImpl>(); | 
  | 103 | 0 |                  if (emplInfo != null) { | 
  | 104 | 0 |                          emplInfos.add(emplInfo); | 
  | 105 |  |                  } | 
  | 106 | 0 |                  entity.setEmploymentInformation(emplInfos); | 
  | 107 |  |                   | 
  | 108 |  |                   | 
  | 109 | 0 |                  String affiliationTypeCode = userElement.getChildTextTrim(AFFILIATION_CD_ELEMENT, NAMESPACE); | 
  | 110 | 0 |                  if (!StringUtils.isBlank(affiliationTypeCode)) { | 
  | 111 | 0 |                          if(entity.getAffiliations()==null){ | 
  | 112 | 0 |                                  entity.setAffiliations(new ArrayList<KimEntityAffiliationImpl>()); | 
  | 113 |  |                          } | 
  | 114 | 0 |                          KimEntityAffiliationImpl affiliation = new KimEntityAffiliationImpl(); | 
  | 115 |  |                           | 
  | 116 | 0 |                          Long affiliationId = sas.getNextAvailableSequenceNumber( | 
  | 117 |  |                                          "KRIM_ENTITY_AFLTN_ID_S", KimEntityAffiliationImpl.class); | 
  | 118 | 0 |                          affiliation.setEntityAffiliationId(""+affiliationId); | 
  | 119 | 0 |                          affiliation.setAffiliationTypeCode(affiliationTypeCode); | 
  | 120 | 0 |                          affiliation.setActive(true); | 
  | 121 | 0 |                          affiliation.setDefault(true); | 
  | 122 | 0 |                          affiliation.setEntityId(entity.getEntityId()); | 
  | 123 | 0 |                          String campusCode = userElement.getChildTextTrim(CAMPUS_CD_ELEMENT, NAMESPACE); | 
  | 124 | 0 |                          if(!StringUtils.isBlank(campusCode)){ | 
  | 125 | 0 |                                  affiliation.setCampusCode(campusCode); | 
  | 126 |  |                          } | 
  | 127 | 0 |                          entity.getAffiliations().add(affiliation); | 
  | 128 |  |                  }                 | 
  | 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 |  |      @Override | 
  | 175 |  |          protected KimPrincipalImpl constructPrincipal(Element userElement, String entityId) { | 
  | 176 | 0 |              String principalId = userElement.getChildTextTrim(WORKFLOW_ID_ELEMENT, NAMESPACE); | 
  | 177 | 0 |              if (principalId == null) { | 
  | 178 | 0 |                      principalId = userElement.getChildTextTrim(PRINCIPAL_ID_ELEMENT, NAMESPACE); | 
  | 179 |  |              } | 
  | 180 | 0 |              String principalName = userElement.getChildTextTrim(AUTHENTICATION_ID_ELEMENT, NAMESPACE); | 
  | 181 | 0 |              if (principalName == null) { | 
  | 182 | 0 |                      principalName = userElement.getChildTextTrim(PRINCIPAL_NAME_ELEMENT, NAMESPACE); | 
  | 183 |  |              } | 
  | 184 | 0 |              String password= userElement.getChildTextTrim(PASSWORD_ELEMENT, NAMESPACE); | 
  | 185 |  |               | 
  | 186 |  |               | 
  | 187 | 0 |                  KimPrincipalImpl principal = new KimPrincipalImpl(); | 
  | 188 | 0 |                  principal.setActive(true); | 
  | 189 | 0 |                  principal.setPrincipalId(principalId); | 
  | 190 | 0 |                  principal.setPrincipalName(principalName); | 
  | 191 | 0 |                  principal.setEntityId(entityId); | 
  | 192 |  |                  try { | 
  | 193 | 0 |                          principal.setPassword(KNSServiceLocator.getEncryptionService().hash(password)+HASH_SUFFIX); | 
  | 194 | 0 |                  } catch (GeneralSecurityException e) { | 
  | 195 | 0 |                          LOG.warn("Error hashing password.",e); | 
  | 196 | 0 |                  } | 
  | 197 | 0 |                  KNSServiceLocator.getBusinessObjectService().save(principal); | 
  | 198 |  |                   | 
  | 199 | 0 |                  return principal; | 
  | 200 |  |      } | 
  | 201 |  |  } |