Author: fdesbois Date: 2009-08-26 16:02:12 +0200 (Wed, 26 Aug 2009) New Revision: 3 Added: trunk/billy-business/src/main/java/org/chorem/billy/business/ClientHelper.java Modified: trunk/billy-business/src/main/java/org/chorem/billy/external/ImportXPIL.java trunk/billy-business/src/main/java/org/chorem/billy/impl/ServiceClientImpl.java Log: First implementation for getting xPIL clients from ChoReg Added: trunk/billy-business/src/main/java/org/chorem/billy/business/ClientHelper.java =================================================================== --- trunk/billy-business/src/main/java/org/chorem/billy/business/ClientHelper.java (rev 0) +++ trunk/billy-business/src/main/java/org/chorem/billy/business/ClientHelper.java 2009-08-26 14:02:12 UTC (rev 3) @@ -0,0 +1,67 @@ +/** + * *##% Billy Business + * Copyright (C) 2009 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. ##%* + */ +package org.chorem.billy.business; + +import org.chorem.billy.ContextUtilBilly; +import org.chorem.billy.dto.Client; +import org.chorem.billy.dto.ClientOrganization; +import org.chorem.billy.dto.SubDivision; +import org.chorem.billy.persistence.ClientEntity; +import org.chorem.billy.persistence.ClientOrganizationEntity; +import org.chorem.billy.persistence.SubDivisionEntity; +import org.chorem.exceptions.ConvertException; +import org.chorem.utils.Convert; + +/** + * ClientHelper.java + * + * Created on 2009-08-26 + * + * @author fdesbois + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ +public class ClientHelper { + + public static ClientOrganization getClientOrganization(ClientOrganizationEntity entity) throws ConvertException { + ClientOrganization result = Convert.toDTO(entity, ClientOrganizationEntity.class, ClientOrganization.class); + + result.setId(ContextUtilBilly.convertId(entity.getTopiaId())); + + for (SubDivisionEntity divisionEntity : entity.getSubDivisionEntity()) { + SubDivision sub = Convert.toDTO(divisionEntity, SubDivisionEntity.class, SubDivision.class); + sub.setId(ContextUtilBilly.convertId(divisionEntity.getTopiaId())); + + for (ClientEntity clientEntity : divisionEntity.getClientEntity()) { + Client client = Convert.toDTO(clientEntity, ClientEntity.class, Client.class); + client.setId(ContextUtilBilly.convertId(clientEntity.getTopiaId())); + sub.addClient(client); + client.setSubDivision(sub); + } + + result.addSubDivision(sub); + sub.setClientOrganization(result); + } + + return result; + } + +} Property changes on: trunk/billy-business/src/main/java/org/chorem/billy/business/ClientHelper.java ___________________________________________________________________ Added: svn:keywords + Author Date Revision Modified: trunk/billy-business/src/main/java/org/chorem/billy/external/ImportXPIL.java =================================================================== --- trunk/billy-business/src/main/java/org/chorem/billy/external/ImportXPIL.java 2009-08-26 12:00:23 UTC (rev 2) +++ trunk/billy-business/src/main/java/org/chorem/billy/external/ImportXPIL.java 2009-08-26 14:02:12 UTC (rev 3) @@ -21,15 +21,22 @@ import java.io.InputStream; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.chorem.billy.BillyException; import org.chorem.billy.ContextUtilBilly; +import org.chorem.billy.dto.Client; +import org.chorem.billy.dto.ClientOrganization; +import org.chorem.billy.dto.SubDivision; import org.chorem.choreg.ChoremRegistry; import org.chorem.choreg.ChoremRegistryImpl; import org.chorem.choreg.FormatType; +import org.chorem.oasis.ciq.api.SimplifiedNameElement; import org.chorem.oasis.ciq.api.XNLHelper; import org.chorem.oasis.ciq.api.XPILHelper; import urn.oasis.names.tc.ciq.xnl.PartyNameType; +import urn.oasis.names.tc.ciq.xpil.MembershipsDocument.Memberships; import urn.oasis.names.tc.ciq.xpil.PartyDocument; import urn.oasis.names.tc.ciq.xpil.PartyType; @@ -56,46 +63,130 @@ choreg = ChoremRegistryImpl.getInstance(); } - /*public List<Reference> findAll(String type) throws CashException { - List<Reference> references = new ArrayList<Reference>(); + /** + * <pre> + * Two types of document PartyDocument for XPIL : + * - Person Type include personName, personSourceId, membershipOrganisations with subDivisionNames + * - Organisation Type include organisationName, organisationSourceId, subDivisionNames + * </pre> + * Person document includes organisationSourceId in membershipOrganisations. + * Two steps : Check all files and use maps to store data from PartyDocument. Then routing maps for convert PartyDocument into DTO objects. + * @return list of ClientOrganization which contains SubDivisions and Persons (Clients) + * @throws BillyException + */ + public List<ClientOrganization> findAll() throws BillyException { + List<ClientOrganization> results = new ArrayList<ClientOrganization>(); try { - List<InputStream> results = choreg.findAll(null, FormatType.OASIS_CIQ_XPIL, InputStream.class, null); + List<InputStream> streams = choreg.findAll(null, FormatType.OASIS_CIQ_XPIL, InputStream.class, null); - for (InputStream input : results) { - PartyDocument doc = PartyDocument.Factory.parse(input); + /* First : Creation of two maps */ + + // Key = organizationId, value = List persons PartyDocument + Map<String, List<PartyDocument>> persons = new HashMap<String, List<PartyDocument>>(); + // Key = organizationId, value = organization PartyDocument + Map<String, PartyDocument> organizations = new HashMap<String, PartyDocument>(); + + for (InputStream stream : streams) { + PartyDocument doc = PartyDocument.Factory.parse(stream); PartyType party = doc.getParty(); - PartyNameType partyName = doc.getParty().getPartyName(); + PartyNameType partyName = party.getPartyName(); + + Memberships memberships = party.getMemberships(); + if (memberships != null && XNLHelper.hasPersonNames(partyName)) { + // PERSON FILE - Reference ref = new Reference(); - ref.setSource(XPILHelper.getMainIdentifier(party)); + for (Memberships.Membership membership : memberships.getMembershipArray()) { + // ORGANIZATION + Memberships.Membership.MembershipElement element = membership.getMembershipElementArray(0); + String organizationId = element.getStringValue(); + List<PartyDocument> partys = persons.get(organizationId); + if (partys == null) { + partys = new ArrayList<PartyDocument>(); + persons.put(organizationId, partys); + } + partys.add(doc); + } + } else if (XNLHelper.hasOrganisationNames(partyName)) { + // ORGANIZATION FILE + String organizationId = XPILHelper.getMainIdentifier(party); + organizations.put(organizationId, doc); + } + } - if (!BONZOMS_SERVICE_ORGANIZATION.equals(type) && XNLHelper.hasPersonNames(partyName)) { - ref.setType(type); - ref.setName(XNLHelper.getPersonSimpleName(partyName)); - references.add(ref); + /* Second : Interpret map data to convert into DTO object */ + for (String organizationId : organizations.keySet()) { + PartyType party = organizations.get(organizationId).getParty(); + PartyNameType partyName = party.getPartyName(); + + // Create clientOrganization DTO + ClientOrganization organization = new ClientOrganization(); + organization.setSource(organizationId); + organization.setName(XNLHelper.getOrganisationSimpleName(partyName)); + + // Add subdivisions for organization + for (SimplifiedNameElement name : XNLHelper.getSubDivisionNames(partyName)) { + // Create subdivision DTO + SubDivision division = new SubDivision(); + division.setName(name.getValue()); + division.setClientOrganization(organization); + organization.addSubDivision(division); } - if (!BONZOMS_SERVICE_PERSON.equals(type) && XNLHelper.hasOrganisationNames(partyName)) { - ref.setType(type); - ref.setName(XNLHelper.getOrganisationSimpleName(partyName)); - references.add(ref); + + // Get personDocuments from Map with key = organizationId + List<PartyDocument> personDocuments = persons.get(organizationId); + + // Add persons to subdivision from organization + for (PartyDocument personDoc : personDocuments) { + + PartyType personParty = personDoc.getParty(); + PartyNameType personName = personParty.getPartyName(); + + // Create client (person) DTO + Client client = new Client(); + String personId = XPILHelper.getMainIdentifier(party); + client.setPersonSource(personId); + client.setPersonName(XNLHelper.getPersonSimpleName(personName)); + + Memberships.Membership.Organisation orga = getMembershipOrganisation(personParty, organizationId); + // Add to divisions from organization + for (Memberships.Membership.Organisation.SubDivisionName sub : orga.getSubDivisionNameArray()) { + SubDivision division = getSubDivisionFromOrganization(organization, sub.getStringValue()); + if (division != null) { + //client.addDivision(division); + division.addClient(client); + } + } + } - + + results.add(organization); } } catch (Exception eee) { - ContextUtilCash.serviceException(null, "An error occured when retrieve XPIL references from ChoremRegistry", eee); + ContextUtilBilly.serviceException(null, "An error occured when retrieve XPIL clients from ChoremRegistry", eee); } - return references; + return results; } - public List<String> findReferenceTypes() throws CashException { - List<String> results = new ArrayList<String>(); - try { - results = choreg.findFormatSupportedServiceTitles(FormatType.OASIS_CIQ_XPIL); - } catch (Exception eee) { - ContextUtilCash.serviceException(null, "An error occured when retrieve XPIL reference types from ChoremRegistry", eee); + private SubDivision getSubDivisionFromOrganization(ClientOrganization organization, String subDivisionName) { + for (SubDivision division : organization.getSubDivision()) { + if (division.getName().equals(subDivisionName)) { + return division; + } } - return results; - }*/ + return null; + } + + private Memberships.Membership.Organisation getMembershipOrganisation(PartyType partyType, String organizationId) { + + for (Memberships.Membership membership : partyType.getMemberships().getMembershipArray()) { + Memberships.Membership.MembershipElement element = membership.getMembershipElementArray(0); + if (element.getStringValue().equals(organizationId)) { + return membership.getOrganisation(); + } + } + return null; + + } } Modified: trunk/billy-business/src/main/java/org/chorem/billy/impl/ServiceClientImpl.java =================================================================== --- trunk/billy-business/src/main/java/org/chorem/billy/impl/ServiceClientImpl.java 2009-08-26 12:00:23 UTC (rev 2) +++ trunk/billy-business/src/main/java/org/chorem/billy/impl/ServiceClientImpl.java 2009-08-26 14:02:12 UTC (rev 3) @@ -24,8 +24,15 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.billy.BillyException; +import org.chorem.billy.BillyModelDAOHelper; import org.chorem.billy.ContextUtilBilly; +import org.chorem.billy.business.ClientHelper; +import org.chorem.billy.dto.Client; import org.chorem.billy.dto.ClientOrganization; +import org.chorem.billy.dto.SubDivision; +import org.chorem.billy.external.ImportXPIL; +import org.chorem.billy.persistence.ClientOrganizationEntity; +import org.chorem.billy.persistence.ClientOrganizationEntityDAO; import org.chorem.billy.services.ServiceClient; import org.chorem.billy.services.ServiceClientAbstract; import org.nuiton.topia.TopiaContext; @@ -47,6 +54,8 @@ private TopiaContext context; + private List<ClientOrganization> clientsImportedCache; + public ServiceClientImpl() { this.context = ContextUtilBilly.getRootContext(); } @@ -63,6 +72,15 @@ try { transaction = context.beginTransaction(); + ClientOrganizationEntityDAO dao = BillyModelDAOHelper.getClientOrganizationEntityDAO(transaction); + + for (ClientOrganizationEntity entity : dao.findAll()) { + ClientOrganization client = ClientHelper.getClientOrganization(entity); + results.add(client); + } + + results = importXpilReferences(results); + transaction.closeContext(); } catch (Exception eee) { ContextUtilBilly.serviceException(transaction, "Unable to get all clients", eee); @@ -70,4 +88,106 @@ return results; } + private List<ClientOrganization> importXpilReferences(List<ClientOrganization> input) throws BillyException { + try { + ImportXPIL xpil = new ImportXPIL(); + + if (log.isInfoEnabled()) { + log.info("ImportXPIL..."); + } + + if (clientsImportedCache == null) { + if (log.isDebugEnabled()) { + log.debug("New imports, create Cache"); + } + clientsImportedCache = xpil.findAll(); + } + + + for (ClientOrganization organization : clientsImportedCache) { + + ClientOrganization organizationFind = getClientOrganization(input, organization); + if (organizationFind == null) { + //organizationFind = organization; + input.add(organization); + if (log.isDebugEnabled()) { + log.debug("ClientOrganization : " + organization.getSource()); + } + } else { + // TODO MAJ Organization parameter if needed + + for (SubDivision division : organization.getSubDivision()) { + //division. + SubDivision divisionFind = getDivision(organizationFind, division); + + if (divisionFind == null) { + //divisionFind = division; + organizationFind.addSubDivision(division); + if (log.isDebugEnabled()) { + log.debug("SubDivision : " + division.getName()); + } + } else { + // TODO MAJ SubDivision parameter if needed + + for (Client client : division.getClient()) { + //division. + Client clientFind = getClient(divisionFind, client); + if (clientFind == null) { + divisionFind.addClient(client); + if (log.isDebugEnabled()) { + log.debug("Client : " + client.getPersonSource()); + } + } + } + } + } + + } + + } + + /*ImportReferenceUrl importUrl = new ImportReferenceUrl(); + for (Reference reference : input) { + importUrl.setReferenceUrl(reference); + }*/ + + if (log.isInfoEnabled()) { + log.info("...Clients Imported"); + } + //} + } catch (NoClassDefFoundError eee) { + if (log.isWarnEnabled()) { + log.warn("No ChoremRegistry available"); + } + } + return input; + } + + private ClientOrganization getClientOrganization(List<ClientOrganization> input, ClientOrganization organizationFromXpil) { + for (ClientOrganization in : input) { + if (organizationFromXpil.getSource().equals(in.getSource())) { + return in; + } + } + return null; + } + + private SubDivision getDivision(ClientOrganization input, SubDivision subDivisionFromXpil) { + for (SubDivision in : input.getSubDivision()) { + if (in.getName().equals(subDivisionFromXpil.getName())) { + return in; + } + } + return null; + } + + private Client getClient(SubDivision input, Client clientFromXpil) { + for (Client in : input.getClient()) { + if (clientFromXpil.getPersonSource().equals(in.getPersonSource())) { + return in; + } + } + return null; + } + }