Author: fdesbois Date: 2009-08-27 13:02:41 +0200 (Thu, 27 Aug 2009) New Revision: 4 Added: trunk/billy-business/src/main/java/org/chorem/billy/dto/ trunk/billy-business/src/main/java/org/chorem/billy/dto/Client.java trunk/billy-business/src/test/java/org/chorem/billy/ trunk/billy-business/src/test/java/org/chorem/billy/impl/ trunk/billy-business/src/test/java/org/chorem/billy/impl/ServiceClientImplTest.java trunk/billy-business/src/test/java/org/chorem/billy/test/ trunk/billy-business/src/test/java/org/chorem/billy/test/ClientMock.java Modified: trunk/billy-business/src/main/java/org/chorem/billy/ContextUtilBilly.java trunk/billy-business/src/main/java/org/chorem/billy/business/ClientHelper.java trunk/billy-business/src/main/java/org/chorem/billy/external/ImportXPIL.java trunk/billy-business/src/main/java/org/chorem/billy/impl/ServiceClientImpl.java trunk/billy-business/src/test/resources/TopiaContextBilly.properties Log: - Change model for clients - Corrections for ImportXpil with new model - Implementation for createUpdateClient and deleteClient - Tests for Client CRUD Modified: trunk/billy-business/src/main/java/org/chorem/billy/ContextUtilBilly.java =================================================================== --- trunk/billy-business/src/main/java/org/chorem/billy/ContextUtilBilly.java 2009-08-26 14:02:12 UTC (rev 3) +++ trunk/billy-business/src/main/java/org/chorem/billy/ContextUtilBilly.java 2009-08-27 11:02:41 UTC (rev 4) @@ -61,7 +61,7 @@ log.info("Initialize of TopiaContext..."); } try { - Properties config = loadFileProperties("TopiaContextCash.properties"); + Properties config = loadFileProperties("TopiaContextBilly.properties"); config.setProperty("topia.persistence.classes", BillyModelDAOHelper.getImplementationClassesAsString()); rootContext = TopiaContextFactory.getContext(config); Modified: trunk/billy-business/src/main/java/org/chorem/billy/business/ClientHelper.java =================================================================== --- trunk/billy-business/src/main/java/org/chorem/billy/business/ClientHelper.java 2009-08-26 14:02:12 UTC (rev 3) +++ trunk/billy-business/src/main/java/org/chorem/billy/business/ClientHelper.java 2009-08-27 11:02:41 UTC (rev 4) @@ -18,15 +18,19 @@ */ package org.chorem.billy.business; +import java.util.HashMap; +import java.util.Map; +import org.apache.commons.logging.Log; +import org.chorem.billy.BillyModelDAOHelper; 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.dto.ClientPerson; 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; +import org.chorem.billy.persistence.ClientEntityDAO; +import org.chorem.exceptions.EntityException; +import org.chorem.utils.ServiceHelper; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; /** * ClientHelper.java @@ -41,12 +45,43 @@ */ public class ClientHelper { - public static ClientOrganization getClientOrganization(ClientOrganizationEntity entity) throws ConvertException { - ClientOrganization result = Convert.toDTO(entity, ClientOrganizationEntity.class, ClientOrganization.class); + + public static ClientEntity createUpdateClient(ClientPerson person, TopiaContext transaction, Log log) + throws TopiaException, EntityException { + + Client client = person.getClient(); + Map<String, Object> existParams = new HashMap<String, Object>(); + + existParams.put(ClientEntity.NAME, client.getName()); + // TODO Add siret + existParams.put(ClientEntity.SUB_DIVISION_NAME, person.getSubDivisionName()); + existParams.put(ClientEntity.PERSON_NAME, person.getName()); + // TODO Add email + + Map<String, Object> saveParams = new HashMap<String, Object>(existParams); + + saveParams.put(ClientEntity.SIRET, client.getSiret()); + saveParams.put(ClientEntity.SOURCE, client.getSource()); + saveParams.put(ClientEntity.PERSON_EMAIL, person.getEmail()); + saveParams.put(ClientEntity.PERSON_SOURCE, person.getSource()); + + ClientEntityDAO dao = BillyModelDAOHelper.getClientEntityDAO(transaction); + + ClientEntity entity = ServiceHelper.createUpdateEntity(dao, person.getId(), existParams, saveParams, log); + + String id = ContextUtilBilly.convertId(entity.getTopiaId()); + person.setId(id); + + return entity; + } + + /*public static Client getClientOrganization(ClientEntity entity) throws ConvertException { + Client result = Convert.toDTO(entity, ClientEntity.class, ClientOrganization.class); + result.setId(ContextUtilBilly.convertId(entity.getTopiaId())); - for (SubDivisionEntity divisionEntity : entity.getSubDivisionEntity()) { + for (SubDivisionEntity divisionEntity : entity.getSubDivisionEntity()) { SubDivision sub = Convert.toDTO(divisionEntity, SubDivisionEntity.class, SubDivision.class); sub.setId(ContextUtilBilly.convertId(divisionEntity.getTopiaId())); @@ -62,6 +97,6 @@ } return result; - } + }*/ } Added: trunk/billy-business/src/main/java/org/chorem/billy/dto/Client.java =================================================================== --- trunk/billy-business/src/main/java/org/chorem/billy/dto/Client.java (rev 0) +++ trunk/billy-business/src/main/java/org/chorem/billy/dto/Client.java 2009-08-27 11:02:41 UTC (rev 4) @@ -0,0 +1,121 @@ +package org.chorem.billy.dto; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.util.List; +import java.util.Map; +import org.apache.commons.lang.builder.ToStringBuilder; + +// FIXME, must be generated by BeanGenerator in ToPIA-persistence +public class Client implements java.io.Serializable { + + public Map<String, List<ClientPerson>> services; + public String name; + public String source; + public String siret; + public String viewUrl; + public String formUrl; + protected final PropertyChangeSupport pcs; + + /** + * Default constructor of Client. + */ + public Client() { + pcs = new PropertyChangeSupport(this); + } + + public void addPropertyChangeListener(PropertyChangeListener listener) { + pcs.addPropertyChangeListener(listener); + } + + public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { + pcs.addPropertyChangeListener(propertyName, listener); + } + + public void removePropertyChangeListener(PropertyChangeListener listener) { + pcs.removePropertyChangeListener(listener); + } + + public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { + pcs.removePropertyChangeListener(propertyName, listener); + } + + public Map<String, List<ClientPerson>> getServices() { + return services; + } + + public String getName() { + return name; + } + + public String getSource() { + return source; + } + + public String getSiret() { + return siret; + } + + public String getViewUrl() { + return viewUrl; + } + + public String getFormUrl() { + return formUrl; + } + + public void setServices(Map<String, List<ClientPerson>> newValue) { + Map<String, List<ClientPerson>> oldValue = getServices(); + this.services = newValue; + firePropertyChange("services", oldValue, newValue); + } + + public void setName(String newValue) { + String oldValue = getName(); + this.name = newValue; + firePropertyChange("name", oldValue, newValue); + } + + public void setSource(String newValue) { + String oldValue = getSource(); + this.source = newValue; + firePropertyChange("source", oldValue, newValue); + } + + public void setSiret(String newValue) { + String oldValue = getSiret(); + this.siret = newValue; + firePropertyChange("siret", oldValue, newValue); + } + + public void setViewUrl(String newValue) { + String oldValue = getViewUrl(); + this.viewUrl = newValue; + firePropertyChange("viewUrl", oldValue, newValue); + } + + public void setFormUrl(String newValue) { + String oldValue = getFormUrl(); + this.formUrl = newValue; + firePropertyChange("formUrl", oldValue, newValue); + } + + + @Override + public String toString() { + String result = new ToStringBuilder(this). + append("services", this.services). + append("name", this.name). + append("source", this.source). + append("siret", this.siret). + append("viewUrl", this.viewUrl). + append("formUrl", this.formUrl). + toString(); + return result; + } + + protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { + pcs.firePropertyChange(propertyName, oldValue, newValue); + } + +} //Client 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 14:02:12 UTC (rev 3) +++ trunk/billy-business/src/main/java/org/chorem/billy/external/ImportXPIL.java 2009-08-27 11:02:41 UTC (rev 4) @@ -24,14 +24,16 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; 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.billy.dto.ClientPerson; import org.chorem.choreg.ChoremRegistry; import org.chorem.choreg.ChoremRegistryImpl; import org.chorem.choreg.FormatType; +import org.chorem.choreg.exceptions.NoServiceException; import org.chorem.oasis.ciq.api.SimplifiedNameElement; import org.chorem.oasis.ciq.api.XNLHelper; import org.chorem.oasis.ciq.api.XPILHelper; @@ -59,6 +61,8 @@ private static ChoremRegistry choreg; + private static Log log = LogFactory.getLog(ImportXPIL.class); + public ImportXPIL() { choreg = ChoremRegistryImpl.getInstance(); } @@ -74,18 +78,20 @@ * @return list of ClientOrganization which contains SubDivisions and Persons (Clients) * @throws BillyException */ - public List<ClientOrganization> findAll() throws BillyException { - List<ClientOrganization> results = new ArrayList<ClientOrganization>(); + public List<Client> findAll() throws BillyException { + List<Client> results = new ArrayList<Client>(); try { List<InputStream> streams = choreg.findAll(null, FormatType.OASIS_CIQ_XPIL, InputStream.class, null); /* First : Creation of two maps */ // Key = organizationId, value = List persons PartyDocument - Map<String, List<PartyDocument>> persons = new HashMap<String, List<PartyDocument>>(); + //Map<String, List<PartyDocument>> persons = new HashMap<String, List<PartyDocument>>(); // Key = organizationId, value = organization PartyDocument - Map<String, PartyDocument> organizations = new HashMap<String, PartyDocument>(); + //Map<String, PartyDocument> organizations = new HashMap<String, PartyDocument>(); + Map<String, Client> clients = new HashMap<String, Client>(); + for (InputStream stream : streams) { PartyDocument doc = PartyDocument.Factory.parse(stream); PartyType party = doc.getParty(); @@ -99,27 +105,118 @@ // ORGANIZATION Memberships.Membership.MembershipElement element = membership.getMembershipElementArray(0); String organizationId = element.getStringValue(); - List<PartyDocument> partys = persons.get(organizationId); + + Client client = clients.get(organizationId); + if (client == null) { + client = new Client(); + client.setSource(organizationId); + clients.put(organizationId, client); + results.add(client); + } + + Map<String, List<ClientPerson>> services = client.getServices(); + if (services == null) { + services = new HashMap<String, List<ClientPerson>>(); + client.setServices(services); + } + + Memberships.Membership.Organisation orga = membership.getOrganisation(); + + for (Memberships.Membership.Organisation.SubDivisionName sub : orga.getSubDivisionNameArray()) { + //SubDivision division = getSubDivisionFromOrganization(organization, sub.getStringValue()); + + String subDivisionName = sub.getStringValue(); + List<ClientPerson> persons = services.get(subDivisionName); + if (persons == null) { + persons = new ArrayList<ClientPerson>(); + services.put(subDivisionName, persons); + } + + ClientPerson person = new ClientPerson(); + String personId = XPILHelper.getMainIdentifier(party); + person.setSource(personId); + person.setName(XNLHelper.getPersonSimpleName(partyName)); + person.setSubDivisionName(subDivisionName); + person.setClient(client); + + persons.add(person); + + } + + /*List<PartyDocument> partys = persons.get(organizationId); if (partys == null) { partys = new ArrayList<PartyDocument>(); persons.put(organizationId, partys); } - partys.add(doc); + partys.add(doc);*/ } } else if (XNLHelper.hasOrganisationNames(partyName)) { // ORGANIZATION FILE String organizationId = XPILHelper.getMainIdentifier(party); - organizations.put(organizationId, doc); + + /*Client client = clients.get(organizationId); + if (client == null) { + client = new Client(); + client.setSource(organizationId); + clients.put(organizationId, client); + } + + + Map<String, List<ClientPerson>> services = client.getServices(); + if (services == null) { + services = new HashMap<String, List<ClientPerson>>(); + client.setServices(services); + }*/ + Client client = clients.get(organizationId); + if (client == null) { + client = new Client(); + client.setSource(organizationId); + clients.put(organizationId, client); + results.add(client); + } + + Map<String, List<ClientPerson>> services = client.getServices(); + if (services == null) { + services = new HashMap<String, List<ClientPerson>>(); + client.setServices(services); + } + + client.setName(XNLHelper.getOrganisationSimpleName(partyName)); + + // Add subdivisions for organization + for (SimplifiedNameElement name : XNLHelper.getSubDivisionNames(partyName)) { + // Create subdivision DTO + //SubDivision division = new SubDivision(); + String subDivisionName = name.getValue(); + List<ClientPerson> persons = services.get(subDivisionName); + if (persons == null) { + persons = new ArrayList<ClientPerson>(); + services.put(subDivisionName, persons); + } + + //division.setName(name.getValue()); + //division.setClientOrganization(organization); + //organization.addSubDivision(division); + } + //clients.put(organizationId, organization); + + + //organizations.put(organizationId, doc); } } + /*for (String clientId : clients.keySet()) { + Client client = clients.get(clientId); + results.add(client); + }*/ + //results = clients. /* Second : Interpret map data to convert into DTO object */ - for (String organizationId : organizations.keySet()) { + /*for (String organizationId : organizations.keySet()) { PartyType party = organizations.get(organizationId).getParty(); PartyNameType partyName = party.getPartyName(); // Create clientOrganization DTO - ClientOrganization organization = new ClientOrganization(); + Client organization = new Client(); organization.setSource(organizationId); organization.setName(XNLHelper.getOrganisationSimpleName(partyName)); @@ -160,14 +257,18 @@ } results.add(organization); + }*/ + + } catch (NoServiceException eee) { + if (log.isInfoEnabled()) { + log.info("No service available for xPIL format to retrieve existing Organisations and Persons clients for Billy"); } - } catch (Exception eee) { ContextUtilBilly.serviceException(null, "An error occured when retrieve XPIL clients from ChoremRegistry", eee); } return results; } - +/* private SubDivision getSubDivisionFromOrganization(ClientOrganization organization, String subDivisionName) { for (SubDivision division : organization.getSubDivision()) { if (division.getName().equals(subDivisionName)) { @@ -187,6 +288,6 @@ } 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 14:02:12 UTC (rev 3) +++ trunk/billy-business/src/main/java/org/chorem/billy/impl/ServiceClientImpl.java 2009-08-27 11:02:41 UTC (rev 4) @@ -20,7 +20,9 @@ package org.chorem.billy.impl; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.billy.BillyException; @@ -28,13 +30,13 @@ 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.dto.ClientPerson; import org.chorem.billy.external.ImportXPIL; -import org.chorem.billy.persistence.ClientOrganizationEntity; -import org.chorem.billy.persistence.ClientOrganizationEntityDAO; +import org.chorem.billy.persistence.ClientEntity; +import org.chorem.billy.persistence.ClientEntityDAO; import org.chorem.billy.services.ServiceClient; import org.chorem.billy.services.ServiceClientAbstract; +import org.chorem.utils.ServiceHelper; import org.nuiton.topia.TopiaContext; /** @@ -54,31 +56,117 @@ private TopiaContext context; - private List<ClientOrganization> clientsImportedCache; + private List<Client> clientsImportedCache; public ServiceClientImpl() { this.context = ContextUtilBilly.getRootContext(); } @Override - public void createUpdateClient(ClientOrganization clientOrganization) throws BillyException { - throw new UnsupportedOperationException("Not supported yet."); + public void createUpdateClient(Client client) throws BillyException { + TopiaContext transaction = null; + try { + transaction = context.beginTransaction(); + + Map<String, List<ClientPerson>> services = client.getServices(); + + for (String subDivisionName : services.keySet()) { + + List<ClientPerson> persons = services.get(subDivisionName); + + for (ClientPerson person : persons) { + + ClientHelper.createUpdateClient(person, transaction, log); + + /* Map<String, Object> existParams = new HashMap<String, Object>(); + + existParams.put(ClientEntity.NAME, client.getName()); + // TODO Add siret + existParams.put(ClientEntity.SUB_DIVISION_NAME, subDivisionName); + existParams.put(ClientEntity.PERSON_NAME, person.getName()); + // TODO Add email + + Map<String, Object> saveParams = new HashMap<String, Object>(existParams); + + saveParams.put(ClientEntity.SIRET, client.getSiret()); + saveParams.put(ClientEntity.SOURCE, client.getSource()); + saveParams.put(ClientEntity.PERSON_EMAIL, person.getEmail()); + saveParams.put(ClientEntity.PERSON_SOURCE, person.getSource()); + + ClientEntityDAO dao = BillyModelDAOHelper.getClientEntityDAO(transaction); + + ClientEntity entity = ServiceHelper.createUpdateEntity(dao, person.getId(), existParams, saveParams, log); + + String id = ContextUtilBilly.convertId(entity.getTopiaId()); + person.setId(id);*/ + } + } + + transaction.commitTransaction(); + transaction.closeContext(); + } catch (Exception eee) { + ContextUtilBilly.serviceException(transaction, "Unable to create update the client", eee); + } } @Override - public List<ClientOrganization> getClients() throws BillyException { + public List<Client> getClients() throws BillyException { TopiaContext transaction = null; - List<ClientOrganization> results = new ArrayList<ClientOrganization>(); + List<Client> results = new ArrayList<Client>(); try { transaction = context.beginTransaction(); - ClientOrganizationEntityDAO dao = BillyModelDAOHelper.getClientOrganizationEntityDAO(transaction); + ClientEntityDAO dao = BillyModelDAOHelper.getClientEntityDAO(transaction); - for (ClientOrganizationEntity entity : dao.findAll()) { - ClientOrganization client = ClientHelper.getClientOrganization(entity); - results.add(client); + Map<String, Client> clients = new HashMap<String, Client>(); + + for (ClientEntity entity : dao.findAll()) { + + String siret = entity.getName(); // FIXME Change name by siret + + Client client = clients.get(siret); + if (client == null) { + client = new Client(); + client.setName(entity.getName()); + client.setSiret(entity.getSiret()); + client.setSource(entity.getSource()); + clients.put(siret, client); + results.add(client); + } + + Map<String, List<ClientPerson>> services = client.getServices(); + if (services == null) { + services = new HashMap<String, List<ClientPerson>>(); + client.setServices(services); + } + + List<ClientPerson> persons = services.get(entity.getSubDivisionName()); + + if (persons == null) { + persons = new ArrayList<ClientPerson>(); + services.put(entity.getSubDivisionName(), persons); + } + + ClientPerson person = new ClientPerson(); + person.setName(entity.getPersonName()); + person.setSource(entity.getSource()); + person.setEmail(entity.getPersonEmail()); + + String topiaId = entity.getTopiaId(); // specifique à un triplet Client / Service / ClientPerson + person.setId(ContextUtilBilly.convertId(topiaId)); + person.setSubDivisionName(entity.getSubDivisionName()); + person.setClient(client); + + persons.add(person); + + //Client client = ClientHelper.getClient(entity); + //results.add(client); } + /*for (String key : clients.keySet()) { + results.add(clients.get(key)); + }*/ + results = importXpilReferences(results); transaction.closeContext(); @@ -88,8 +176,57 @@ return results; } - private List<ClientOrganization> importXpilReferences(List<ClientOrganization> input) throws BillyException { + @Override + public void deleteClientPerson(String personId) throws BillyException { + TopiaContext transaction = null; try { + transaction = context.beginTransaction(); + + ClientEntityDAO dao = BillyModelDAOHelper.getClientEntityDAO(transaction); + + ServiceHelper.deleteEntity(dao, personId, log); + + transaction.commitTransaction(); + transaction.closeContext(); + } catch (Exception eee) { + ContextUtilBilly.serviceException(transaction, "Unable to delete person with id = " + personId, eee); + } + } + + @Override + public void deleteClient(Client client) throws BillyException { + TopiaContext transaction = null; + try { + transaction = context.beginTransaction(); + + ClientEntityDAO dao = BillyModelDAOHelper.getClientEntityDAO(transaction); + + int count = 0; + + if (client.getServices() != null) { + for (String subDivisionName : client.getServices().keySet()) { + List<ClientPerson> persons = client.getServices().get(subDivisionName); + for (ClientPerson person : persons) { + + ServiceHelper.deleteEntity(dao, person.getId(), log); + count++; + } + } + } + + if (log.isInfoEnabled()) { + log.info("Suppress " + count + " persons for client '" + client.getName() + "'"); + } + + transaction.commitTransaction(); + transaction.closeContext(); + } catch (Exception eee) { + ContextUtilBilly.serviceException(transaction, "Unable to delete client '" + client.getName() + "'", eee); + } + } + + private List<Client> importXpilReferences(List<Client> input) throws BillyException { + try { ImportXPIL xpil = new ImportXPIL(); if (log.isInfoEnabled()) { @@ -103,9 +240,54 @@ clientsImportedCache = xpil.findAll(); } + int nbPersonsImported = 0; + int nbPersonsFromXpil = 0; - for (ClientOrganization organization : clientsImportedCache) { + for (Client client : clientsImportedCache) { + Client clientFind = getClient(input, client); + if (clientFind == null) { // new Client from xPIL + input.add(client); + if (log.isDebugEnabled()) { + log.debug("Add Client : " + client.getSource() + " _ " + client.getSiret()); + } + } else { // Client already exist + + Map<String, List<ClientPerson>> servicesFind = clientFind.getServices(); + + for (String serviceName : client.getServices().keySet()) { + + List<ClientPerson> persons = client.getServices().get(serviceName); + + List<ClientPerson> personsFind = servicesFind.get(serviceName); + if (personsFind == null) { // new Service with List persons from xPIL + + servicesFind.put(serviceName, persons); + if (log.isDebugEnabled()) { + log.debug("Add Service : " + serviceName + " with " + persons.size() + " persons"); + } + } else { // Service already exist + + for (ClientPerson person : persons) { + + ClientPerson personFind = getClientPerson(personsFind, person); + if (personFind == null) { // new ClientPerson from xPIL + personsFind.add(person); + if (log.isDebugEnabled()) { + log.debug("Add Person : " + person.getSource() + " _ " + person.getEmail()); + } + nbPersonsImported++; + } + } + } + + nbPersonsFromXpil += persons.size(); + } + } + } + + /*for (ClientOrganization organization : clientsImportedCache) { + ClientOrganization organizationFind = getClientOrganization(input, organization); if (organizationFind == null) { //organizationFind = organization; @@ -144,7 +326,7 @@ } - } + }*/ /*ImportReferenceUrl importUrl = new ImportReferenceUrl(); for (Reference reference : input) { @@ -152,7 +334,13 @@ }*/ if (log.isInfoEnabled()) { - log.info("...Clients Imported"); + if (nbPersonsFromXpil != 0) { + log.info("...clients imported"); + log.info("Persons from xPIL = " + nbPersonsFromXpil); + log.info("Persons imported from client organisations = " + nbPersonsImported); + } else { + log.info("...no clients imported. Probably no service available"); + } } //} } catch (NoClassDefFoundError eee) { @@ -163,7 +351,27 @@ return input; } - private ClientOrganization getClientOrganization(List<ClientOrganization> input, ClientOrganization organizationFromXpil) { + private Client getClient(List<Client> input, Client client) { + for (Client in : input) { + if (client.getSource().equals(in.getSource())) { + return in; + } + // TODO siret comparison + } + return null; + } + + private ClientPerson getClientPerson(List<ClientPerson> input, ClientPerson person) { + for (ClientPerson in : input) { + if (person.getSource().equals(in.getSource())) { + return in; + } + // TODO email comparison + } + return null; + } + + /*private ClientOrganization getClientOrganization(List<ClientOrganization> input, ClientOrganization organizationFromXpil) { for (ClientOrganization in : input) { if (organizationFromXpil.getSource().equals(in.getSource())) { return in; @@ -188,6 +396,6 @@ } } return null; - } + }*/ } Added: trunk/billy-business/src/test/java/org/chorem/billy/impl/ServiceClientImplTest.java =================================================================== --- trunk/billy-business/src/test/java/org/chorem/billy/impl/ServiceClientImplTest.java (rev 0) +++ trunk/billy-business/src/test/java/org/chorem/billy/impl/ServiceClientImplTest.java 2009-08-27 11:02:41 UTC (rev 4) @@ -0,0 +1,191 @@ +/** + * *##% 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.impl; + +import java.util.List; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.billy.BillyException; +import org.chorem.billy.ContextUtilBilly; +import org.chorem.billy.dto.Client; +import org.chorem.billy.dto.ClientPerson; +import org.chorem.billy.test.ClientMock; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * ServiceClientImplTest.java + * + * Created on 2009-08-27 + * + * @author fdesbois + * @version $Revision: 3 $ + * + * Last update: $Date: 2009-08-26 16:02:12 +0200 (Wed, 26 Aug 2009) $ + * by : $Author: fdesbois $ + */ +public class ServiceClientImplTest { + + private static final Log log = LogFactory.getLog(ServiceClientImplTest.class); + + public ServiceClientImplTest() { + } + + @BeforeClass + public static void setUpClass() throws Exception { + } + + @AfterClass + public static void tearDownClass() throws Exception { + ContextUtilBilly.closeRootContext(); + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of createUpdateClient method, of class ServiceClientImpl. + */ + @Test + public void testCreateUpdateClient() throws Exception { + log.info("createUpdateClient"); + + ServiceClientImpl instance = new ServiceClientImpl(); + + ClientMock psychoville = new ClientMock(ClientMock.PYSCHOVILLE); + Client client = psychoville.execute(); + + try { + instance.createUpdateClient(client); + + assertEquals(client.getServices().size(), 2); + + for (String key : client.getServices().keySet()) { + List<ClientPerson> persons = client.getServices().get(key); + for (ClientPerson person : persons) { + log.debug("Person : '" + person.getName() + "' from '" + person.getClient().getName() + "' in service '" + + person.getSubDivisionName() + "'"); + + assertNotNull(person.getId()); + } + } + + } catch (BillyException eee) { + fail("BillyException : " + eee.getMessage()); + } + + try { + instance.deleteClient(client); + } catch (BillyException eee) { + fail("BillyException : " + eee.getMessage()); + } + } + + /** + * Test of getClients method, of class ServiceClientImpl. + */ + @Test + public void testGetClients() throws Exception { + log.info("getClients"); + + ServiceClientImpl instance = new ServiceClientImpl(); + + ClientMock psychoville = new ClientMock(ClientMock.PYSCHOVILLE); + Client client1 = psychoville.execute(); + ClientMock gotham = new ClientMock(ClientMock.GOTHAM_CITY); + Client client2 = gotham.execute(); + + try { + instance.createUpdateClient(client1); + instance.createUpdateClient(client2); + } catch (BillyException eee) { + fail("BillyException : " + eee.getMessage()); + } + + try { + List<Client> clients = instance.getClients(); + + assertEquals(clients.size(),2); + + int nbPersonsGotham = 0; + int nbPersonsPsychoville = 0; + + for (Client client : clients) { + + assertNotNull(client.getName()); + + if (client.getName().equals(ClientMock.GOTHAM_CITY)) { + + assertEquals(client.getServices().size(), gotham.getNbServices()); + + List<ClientPerson> clowns = client.getServices().get(ClientMock.SERVICE_CLOWN); + assertNotNull(clowns); + assertEquals(clowns.size(), gotham.getNbPersons(ClientMock.SERVICE_CLOWN)); + + List<ClientPerson> heros = client.getServices().get(ClientMock.SERVICE_HEROS); + assertNotNull(heros); + assertEquals(heros.size(), gotham.getNbPersons(ClientMock.SERVICE_HEROS)); + + nbPersonsGotham += clowns.size() + heros.size(); + } + + if (client.getName().equals(ClientMock.PYSCHOVILLE)) { + + assertEquals(client.getServices().size(), gotham.getNbServices()); + + List<ClientPerson> clowns = client.getServices().get(ClientMock.SERVICE_CLOWN); + assertNotNull(clowns); + assertEquals(clowns.size(), psychoville.getNbPersons(ClientMock.SERVICE_CLOWN)); + + List<ClientPerson> psychos = client.getServices().get(ClientMock.SERVICE_PSYCHO); + assertNotNull(psychos); + assertEquals(psychos.size(), psychoville.getNbPersons(ClientMock.SERVICE_PSYCHO)); + + nbPersonsPsychoville += clowns.size() + psychos.size(); + } + } + + assertEquals(nbPersonsGotham, gotham.getNbPersons()); + assertEquals(nbPersonsPsychoville, psychoville.getNbPersons()); + + + } catch (BillyException eee) { + fail("BillyException : " + eee.getMessage()); + } + + try { + instance.deleteClient(client1); + instance.deleteClient(client2); + } catch (BillyException eee) { + fail("BillyException : " + eee.getMessage()); + } + + } + +} \ No newline at end of file Added: trunk/billy-business/src/test/java/org/chorem/billy/test/ClientMock.java =================================================================== --- trunk/billy-business/src/test/java/org/chorem/billy/test/ClientMock.java (rev 0) +++ trunk/billy-business/src/test/java/org/chorem/billy/test/ClientMock.java 2009-08-27 11:02:41 UTC (rev 4) @@ -0,0 +1,131 @@ +/** + * *##% 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.test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.chorem.billy.dto.Client; +import org.chorem.billy.dto.ClientPerson; + +/** + * ServiceClientImplTest.java + * + * Created on 2009-08-27 + * + * @author fdesbois + * @version $Revision: 3 $ + * + * Last update: $Date: 2009-08-26 16:02:12 +0200 (Wed, 26 Aug 2009) $ + * by : $Author: fdesbois $ + */ +public class ClientMock { + + + public static final String PYSCHOVILLE = "Psychoville"; + public static final String GOTHAM_CITY = "Gotham City"; + + public static final String SERVICE_CLOWN = "Clown"; + public static final String SERVICE_PSYCHO = "Psycho"; + public static final String SERVICE_HEROS = "Heros"; + + private Client client; + + private String name; + + public ClientMock(String name) { + this.name = name; + } + + public Client execute() throws IllegalArgumentException { + if (this.name.equals(PYSCHOVILLE)) { + return executePsychoville(); + } else if (this.name.equals(GOTHAM_CITY)) { + return executeGotham(); + } + throw new IllegalArgumentException("Illegal ClientMock name"); + } + + public int getNbServices() { + return client.getServices().size(); + } + + public int getNbPersons(String service) { + return client.getServices().get(service).size(); + } + + public int getNbPersons() { + int count = 0; + for (String key : client.getServices().keySet()) { + count += client.getServices().get(key).size(); + } + return count; + } + + private Client executePsychoville() { + client = new Client(); + client.setName(this.name); + Map<String, List<ClientPerson>> services = new HashMap<String, List<ClientPerson>>(); + services.put(SERVICE_CLOWN, new ArrayList<ClientPerson>()); + services.put(SERVICE_PSYCHO, new ArrayList<ClientPerson>()); + client.setServices(services); + + List<ClientPerson> clowns = services.get(SERVICE_CLOWN); + ClientPerson clown1 = addClientPerson(client, SERVICE_CLOWN, "Mr. Jelly"); + ClientPerson clown2 = addClientPerson(client, SERVICE_CLOWN, "Mr. Jolly"); + + List<ClientPerson> psychos = services.get(SERVICE_PSYCHO); + ClientPerson psycho1 = addClientPerson(client, SERVICE_PSYCHO, "Mr. Lomax"); + ClientPerson psycho2 = addClientPerson(client, SERVICE_PSYCHO, "David"); + ClientPerson psycho3 = addClientPerson(client, SERVICE_PSYCHO, "Joy Aston"); + + return client; + } + + private Client executeGotham() { + client = new Client(); + client.setName(this.name); + Map<String, List<ClientPerson>> services = new HashMap<String, List<ClientPerson>>(); + services.put(SERVICE_CLOWN, new ArrayList<ClientPerson>()); + services.put(SERVICE_HEROS, new ArrayList<ClientPerson>()); + client.setServices(services); + + List<ClientPerson> clowns = services.get(SERVICE_CLOWN); + ClientPerson clown1 = addClientPerson(client, SERVICE_CLOWN, "Joker"); + ClientPerson clown2 = addClientPerson(client, SERVICE_CLOWN, "Mystery Men"); + + List<ClientPerson> heros = services.get(SERVICE_HEROS); + ClientPerson hero1 = addClientPerson(client, SERVICE_HEROS, "Batman"); + + return client; + } + + + private ClientPerson addClientPerson(Client client, String subDivisionName, String personName) { + ClientPerson person = new ClientPerson(); + person.setName(personName); + person.setClient(client); + person.setSubDivisionName(subDivisionName); + client.getServices().get(subDivisionName).add(person); + return person; + } + +} Modified: trunk/billy-business/src/test/resources/TopiaContextBilly.properties =================================================================== --- trunk/billy-business/src/test/resources/TopiaContextBilly.properties 2009-08-26 14:02:12 UTC (rev 3) +++ trunk/billy-business/src/test/resources/TopiaContextBilly.properties 2009-08-27 11:02:41 UTC (rev 4) @@ -1,6 +1,6 @@ # Propri\u00C3\u00A9t\u00C3\u00A9s par d\u00C3\u00A9faut pour une base de donn\u00C3\u00A9es de type H2 hibernate.hbm2ddl.auto=create -hibernate.show_sql=true +hibernate.show_sql=false hibernate.dialect=org.hibernate.dialect.H2Dialect hibernate.connection.username=sa