Coverage Report - org.kuali.rice.kew.xml.UserXmlParser
 
Classes in this File Line Coverage Branch Coverage Complexity
UserXmlParser
0%
0/96
0%
0/20
5
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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.api.util.xml.XmlException;
 24  
 import org.kuali.rice.core.api.util.xml.XmlHelper;
 25  
 import org.kuali.rice.kim.api.identity.CodedAttribute;
 26  
 import org.kuali.rice.kim.api.identity.email.EntityEmail;
 27  
 import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
 28  
 import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
 29  
 import org.kuali.rice.kim.impl.identity.entity.EntityBo;
 30  
 import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
 31  
 import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
 32  
 import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
 33  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 34  
 import org.kuali.rice.krad.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.Collections;
 42  
 import java.util.Iterator;
 43  
 import java.util.List;
 44  
 
 45  
 /**
 46  
  * Parses users from XML.
 47  
  * 
 48  
  * This is really meant for use only in the unit tests and was written to help ease
 49  
  * transition over to KIM.  There are numerous unit tests which took advantage of
 50  
  * the ability to import "users" from XML in KEW.  KIM does not provide XML
 51  
  * import capabilities in the initial implementation so this class provides that.
 52  
  *
 53  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 54  
  *
 55  
  */
 56  0
 public class UserXmlParser {
 57  
     
 58  0
     private static final Namespace NAMESPACE = Namespace.getNamespace("", "ns:workflow/User");
 59  
 
 60  
     private static final String USERS_ELEMENT = "users";
 61  
     private static final String USER_ELEMENT = "user";
 62  
     private static final String WORKFLOW_ID_ELEMENT = "workflowId";
 63  
     private static final String AUTHENTICATION_ID_ELEMENT = "authenticationId";
 64  
     private static final String PRINCIPAL_ID_ELEMENT = "principalId";
 65  
     private static final String PRINCIPAL_NAME_ELEMENT = "principalName";
 66  
     private static final String EMPL_ID_ELEMENT = "emplId";
 67  
     private static final String EMAIL_ELEMENT = "emailAddress";
 68  
     private static final String GIVEN_NAME_ELEMENT = "givenName";
 69  
     private static final String LAST_NAME_ELEMENT = "lastName";    
 70  
     private static final String TYPE_ELEMENT = "type";
 71  
     
 72  
     public void parseUsers(InputStream input) throws IOException, XmlException {
 73  
         try {
 74  0
             Document doc = XmlHelper.trimSAXXml(input);
 75  0
             Element root = doc.getRootElement();
 76  0
             parseUsers(root);
 77  0
         } catch (JDOMException e) {
 78  0
             throw new XmlException("Parse error.", e);
 79  0
         } catch (SAXException e){
 80  0
             throw new XmlException("Parse error.",e);
 81  0
         } catch(ParserConfigurationException e){
 82  0
             throw new XmlException("Parse error.",e);
 83  0
         }
 84  0
     }
 85  
 
 86  
     public void parseUsers(Element root) throws XmlException {
 87  0
             for (Iterator usersElementIt = root.getChildren(USERS_ELEMENT, NAMESPACE).iterator(); usersElementIt.hasNext();) {
 88  0
                     Element usersElement = (Element) usersElementIt.next();
 89  0
                     for (Iterator iterator = usersElement.getChildren(USER_ELEMENT, NAMESPACE).iterator(); iterator.hasNext();) {
 90  0
                             Element userElement = (Element) iterator.next();
 91  0
                             EntityBo entity = constructEntity(userElement);
 92  0
                             constructPrincipal(userElement, entity.getId());
 93  0
                     }
 94  0
             }
 95  0
     }
 96  
     
 97  
     protected EntityBo constructEntity(Element userElement) {
 98  0
         SequenceAccessorService sas = KRADServiceLocator.getSequenceAccessorService();
 99  
             
 100  0
             String firstName = userElement.getChildTextTrim(GIVEN_NAME_ELEMENT, NAMESPACE);
 101  0
         String lastName = userElement.getChildTextTrim(LAST_NAME_ELEMENT, NAMESPACE);
 102  0
         String emplId = userElement.getChildTextTrim(EMPL_ID_ELEMENT, NAMESPACE);
 103  0
         String entityTypeCode = userElement.getChildTextTrim(TYPE_ELEMENT, NAMESPACE);
 104  0
         if (StringUtils.isBlank(entityTypeCode)) {
 105  0
                 entityTypeCode = "PERSON";
 106  
         }
 107  
             
 108  0
         Long entityId = sas.getNextAvailableSequenceNumber("KRIM_ENTITY_ID_S", 
 109  
                         EntityEmploymentBo.class);
 110  
         
 111  
         // if they define an empl id, let's set that up
 112  0
         EntityEmploymentBo emplInfo = null;
 113  0
         if (!StringUtils.isBlank(emplId)) {
 114  0
                 emplInfo = new EntityEmploymentBo();
 115  0
                 emplInfo.setActive(true);
 116  0
                 emplInfo.setEmployeeId(emplId);
 117  0
                 emplInfo.setPrimary(true);
 118  0
                 emplInfo.setEntityId("" + entityId);
 119  0
                 emplInfo.setId(emplId);
 120  0
                 emplInfo.setEntityAffiliationId(null);
 121  
         }
 122  
         
 123  
             
 124  0
                 EntityBo entity = new EntityBo();
 125  0
                 entity.setActive(true);
 126  0
                 entity.setId("" + entityId);
 127  0
                 List<EntityEmploymentBo> emplInfos = new ArrayList<EntityEmploymentBo>();
 128  0
                 if (emplInfo != null) {
 129  0
                         emplInfos.add(emplInfo);
 130  
                 }
 131  0
                 entity.setEmploymentInformation(emplInfos);
 132  
                 
 133  0
                 EntityTypeContactInfoBo entityType = new EntityTypeContactInfoBo();
 134  
                 //identity.getEntityTypes().add(entityType);
 135  0
                 entityType.setEntityTypeCode(entityTypeCode);
 136  0
                 entityType.setEntityId(entity.getId());
 137  0
                 entityType.setActive(true);
 138  0
                 entityType.setVersionNumber(new Long(1));
 139  0
                 String emailAddress = userElement.getChildTextTrim(EMAIL_ELEMENT, NAMESPACE);
 140  0
                 if (!StringUtils.isBlank(emailAddress)) {
 141  0
                         Long emailId = sas.getNextAvailableSequenceNumber(
 142  
                                         "KRIM_ENTITY_EMAIL_ID_S", EntityEmailBo.class);
 143  0
                         EntityEmail.Builder email = EntityEmail.Builder.create();
 144  0
                         email.setActive(true);
 145  0
                         email.setId("" + emailId);
 146  0
                         email.setEntityTypeCode(entityTypeCode);
 147  
                         // must be in krim_email_typ_t.email_typ_cd:
 148  0
                         email.setEmailType(CodedAttribute.Builder.create("WRK"));
 149  0
                         email.setVersionNumber(new Long(1));
 150  0
                         email.setEmailAddress(emailAddress);
 151  0
                         email.setDefaultValue(true);
 152  0
                         email.setEntityId(entity.getId());
 153  0
                         List<EntityEmailBo> emailAddresses = new ArrayList<EntityEmailBo>(1);
 154  0
                         emailAddresses.add(EntityEmailBo.from(email.build()));
 155  0
                         entityType.setEmailAddresses(emailAddresses);
 156  
                         //email = (KimEntityEmailImpl)KRADServiceLocatorInternal.getBusinessObjectService().save(email);
 157  
                 }
 158  0
                 List<EntityTypeContactInfoBo> entityTypes = new ArrayList<EntityTypeContactInfoBo>(1);
 159  0
                 entityTypes.add(entityType);
 160  0
                 entity.setEntityTypeContactInfos(entityTypes);
 161  
                 
 162  0
                 if (!StringUtils.isBlank(firstName) || !StringUtils.isBlank(lastName)) {
 163  0
                         Long entityNameId = sas.getNextAvailableSequenceNumber(
 164  
                                         "KRIM_ENTITY_NM_ID_S", EntityNameBo.class);
 165  0
                         EntityNameBo name = new EntityNameBo();
 166  0
                         name.setActive(true);
 167  0
                         name.setId("" + entityNameId);
 168  0
                         name.setEntityId(entity.getId());
 169  
                         // must be in krim_ent_nm_typ_t.ent_nm_typ_cd
 170  0
                         name.setNameCode("PRFR");
 171  0
                         name.setFirstName(firstName);
 172  0
                         name.setMiddleName("");
 173  0
                         name.setLastName(lastName);
 174  0
                         name.setDefaultValue(true);
 175  
                         
 176  0
                         entity.setNames(Collections.singletonList(name));
 177  
                 }
 178  
 
 179  0
                 entity =  KRADServiceLocator.getBusinessObjectService().save(entity);
 180  
                 
 181  0
                 return entity;
 182  
     }
 183  
     
 184  
     protected PrincipalBo constructPrincipal(Element userElement, String entityId) {
 185  0
             String principalId = userElement.getChildTextTrim(WORKFLOW_ID_ELEMENT, NAMESPACE);
 186  0
             if (principalId == null) {
 187  0
                     principalId = userElement.getChildTextTrim(PRINCIPAL_ID_ELEMENT, NAMESPACE);
 188  
             }
 189  0
             String principalName = userElement.getChildTextTrim(AUTHENTICATION_ID_ELEMENT, NAMESPACE);
 190  0
             if (principalName == null) {
 191  0
                     principalName = userElement.getChildTextTrim(PRINCIPAL_NAME_ELEMENT, NAMESPACE);
 192  
             }
 193  
             
 194  0
                 PrincipalBo principal = new PrincipalBo();
 195  0
                 principal.setActive(true);
 196  0
                 principal.setPrincipalId(principalId);
 197  0
                 principal.setPrincipalName(principalName);
 198  0
                 principal.setEntityId(entityId);
 199  0
                 principal = (PrincipalBo) KRADServiceLocator.getBusinessObjectService().save(principal);
 200  
                 
 201  0
                 return principal;
 202  
     }
 203  
 
 204  
 }