001    /**
002     * Copyright 2010 The Kuali Foundation Licensed under the
003     * Educational Community License, Version 2.0 (the "License"); you may
004     * not use this file except in compliance with the License. You may
005     * obtain a copy of the License at
006     *
007     * http://www.osedu.org/licenses/ECL-2.0
008     *
009     * Unless required by applicable law or agreed to in writing,
010     * software distributed under the License is distributed on an "AS IS"
011     * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012     * or implied. See the License for the specific language governing
013     * permissions and limitations under the License.
014     */
015    
016    package org.kuali.rice.student.kew.xml;
017    
018    import java.security.GeneralSecurityException;
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.apache.log4j.Logger;
024    import org.jdom.Element;
025    import org.jdom.Namespace;
026    import org.kuali.rice.kew.xml.UserXmlParser;
027    import org.kuali.rice.kim.bo.entity.impl.KimEntityAffiliationImpl;
028    import org.kuali.rice.kim.bo.entity.impl.KimEntityEmailImpl;
029    import org.kuali.rice.kim.bo.entity.impl.KimEntityEmploymentInformationImpl;
030    import org.kuali.rice.kim.bo.entity.impl.KimEntityEntityTypeImpl;
031    import org.kuali.rice.kim.bo.entity.impl.KimEntityImpl;
032    import org.kuali.rice.kim.bo.entity.impl.KimEntityNameImpl;
033    import org.kuali.rice.kim.bo.entity.impl.KimPrincipalImpl;
034    import org.kuali.rice.kns.service.KNSServiceLocator;
035    import org.kuali.rice.kns.service.SequenceAccessorService;
036    
037    /**
038     * Adds password to the User xml ingestion
039     *
040     */
041    public class KSUserXmlParser extends UserXmlParser {
042            
043        protected final Logger LOG = Logger.getLogger(getClass());
044            
045        private static final Namespace NAMESPACE = Namespace.getNamespace("", "ns:workflow/User");
046    
047        private static final String WORKFLOW_ID_ELEMENT = "workflowId";
048        private static final String AUTHENTICATION_ID_ELEMENT = "authenticationId";
049        private static final String PRINCIPAL_ID_ELEMENT = "principalId";
050        private static final String PRINCIPAL_NAME_ELEMENT = "principalName";
051        private static final String EMPL_ID_ELEMENT = "emplId";
052        private static final String EMAIL_ELEMENT = "emailAddress";
053        private static final String GIVEN_NAME_ELEMENT = "givenName";
054        private static final String LAST_NAME_ELEMENT = "lastName";    
055        private static final String TYPE_ELEMENT = "type";
056        private static final String PASSWORD_ELEMENT = "password";
057        private static final String HASH_SUFFIX = "(&^HSH#&)";
058    
059            private static final String AFFILIATION_CD_ELEMENT = "affiliationTypeCode";
060            private static final String ACTIVE_ELEMENT = "active";
061            private static final String CAMPUS_CD_ELEMENT = "campusCode";
062            
063        @Override
064        protected KimEntityImpl constructEntity(Element userElement) {
065            SequenceAccessorService sas = KNSServiceLocator.getSequenceAccessorService();
066            
067            String firstName = userElement.getChildTextTrim(GIVEN_NAME_ELEMENT, NAMESPACE);
068            String lastName = userElement.getChildTextTrim(LAST_NAME_ELEMENT, NAMESPACE);
069            String emplId = userElement.getChildTextTrim(EMPL_ID_ELEMENT, NAMESPACE);
070            String entityTypeCode = userElement.getChildTextTrim(TYPE_ELEMENT, NAMESPACE);
071            if (StringUtils.isBlank(entityTypeCode)) {
072                    entityTypeCode = "PERSON";
073            }
074            
075            Long entityId = sas.getNextAvailableSequenceNumber("KRIM_ENTITY_ID_S", 
076                            KimEntityEmploymentInformationImpl.class);
077            
078            // if they define an empl id, let's set that up
079            KimEntityEmploymentInformationImpl emplInfo = null;
080            if (!StringUtils.isBlank(emplId)) {
081                    emplInfo = new KimEntityEmploymentInformationImpl();
082                    emplInfo.setActive(true);
083                    emplInfo.setEmployeeId(emplId);
084                    emplInfo.setPrimary(true);
085                    emplInfo.setEntityId("" + entityId);
086                    emplInfo.setEntityEmploymentId(emplId);
087            }
088            
089            
090                    KimEntityImpl entity = new KimEntityImpl();
091                    
092                    //Set active on the entity
093                    String active = userElement.getChildTextTrim(ACTIVE_ELEMENT, NAMESPACE);
094                    if("false".equals(active)){
095                            entity.setActive(false);
096                    }else{
097                            entity.setActive(true);
098                    }
099                    
100                    
101                    entity.setEntityId("" + entityId);
102                    List<KimEntityEmploymentInformationImpl> emplInfos = new ArrayList<KimEntityEmploymentInformationImpl>();
103                    if (emplInfo != null) {
104                            emplInfos.add(emplInfo);
105                    }
106                    entity.setEmploymentInformation(emplInfos);
107                    
108                    //Add affiliations
109                    String affiliationTypeCode = userElement.getChildTextTrim(AFFILIATION_CD_ELEMENT, NAMESPACE);
110                    if (!StringUtils.isBlank(affiliationTypeCode)) {
111                            if(entity.getAffiliations()==null){
112                                    entity.setAffiliations(new ArrayList<KimEntityAffiliationImpl>());
113                            }
114                            KimEntityAffiliationImpl affiliation = new KimEntityAffiliationImpl();
115                            
116                            Long affiliationId = sas.getNextAvailableSequenceNumber(
117                                            "KRIM_ENTITY_AFLTN_ID_S", KimEntityAffiliationImpl.class);
118                            affiliation.setEntityAffiliationId(""+affiliationId);
119                            affiliation.setAffiliationTypeCode(affiliationTypeCode);
120                            affiliation.setActive(true);
121                            affiliation.setDefault(true);
122                            affiliation.setEntityId(entity.getEntityId());
123                            String campusCode = userElement.getChildTextTrim(CAMPUS_CD_ELEMENT, NAMESPACE);
124                            if(!StringUtils.isBlank(campusCode)){
125                                    affiliation.setCampusCode(campusCode);
126                            }
127                            entity.getAffiliations().add(affiliation);
128                    }               
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                            // must be in krim_ent_nm_typ_t.ent_nm_typ_cd
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                            // must be in krim_email_typ_t.email_typ_cd:
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        @Override
175            protected KimPrincipalImpl constructPrincipal(Element userElement, String entityId) {
176            String principalId = userElement.getChildTextTrim(WORKFLOW_ID_ELEMENT, NAMESPACE);
177            if (principalId == null) {
178                    principalId = userElement.getChildTextTrim(PRINCIPAL_ID_ELEMENT, NAMESPACE);
179            }
180            String principalName = userElement.getChildTextTrim(AUTHENTICATION_ID_ELEMENT, NAMESPACE);
181            if (principalName == null) {
182                    principalName = userElement.getChildTextTrim(PRINCIPAL_NAME_ELEMENT, NAMESPACE);
183            }
184            String password= userElement.getChildTextTrim(PASSWORD_ELEMENT, NAMESPACE);
185            
186            
187                    KimPrincipalImpl principal = new KimPrincipalImpl();
188                    principal.setActive(true);
189                    principal.setPrincipalId(principalId);
190                    principal.setPrincipalName(principalName);
191                    principal.setEntityId(entityId);
192                    try {
193                            principal.setPassword(KNSServiceLocator.getEncryptionService().hash(password)+HASH_SUFFIX);
194                    } catch (GeneralSecurityException e) {
195                            LOG.warn("Error hashing password.",e);
196                    }
197                    KNSServiceLocator.getBusinessObjectService().save(principal);
198                    
199                    return principal;
200        }
201    }