This is an automated email from the git hooks/post-receive script. New commit to branch feature/7017 in repository observe. See http://git.codelutin.com/observe.git commit f79c35bd0cc77b14bbeca18559fc5ba6a74ee097 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon May 4 15:55:19 2015 +0200 - Amélioration du cache de services - Ajout API pour créer un modèle de sélection refs #7017 --- .../observe/services/ObserveServiceFactory.java | 148 ++++------------ .../ird/observe/services/ObserveServicesCache.java | 191 +++++++++++++++++++++ .../fr/ird/observe/services/data/TripService.java | 3 + .../observe/services/model/DataSelectionModel.java | 103 ++--------- .../ObserveServiceFactoryProviderTopia.java | 11 +- .../ird/observe/services/data/TripServiceImpl.java | 88 ++++++++++ .../main/java/fr/ird/observe/ObserveContext.java | 17 +- .../java/fr/ird/observe/ui/admin/AdminUIModel.java | 25 ++- .../ui/admin/consolidate/ConsolidateUIHandler.java | 2 +- .../ird/observe/ui/admin/export/ExportModel.java | 3 - .../ird/observe/ui/storage/StorageUIHandler.java | 63 +++---- 11 files changed, 381 insertions(+), 273 deletions(-) diff --git a/observe-services-api/src/main/java/fr/ird/observe/services/ObserveServiceFactory.java b/observe-services-api/src/main/java/fr/ird/observe/services/ObserveServiceFactory.java index 345052d..25dcb33 100644 --- a/observe-services-api/src/main/java/fr/ird/observe/services/ObserveServiceFactory.java +++ b/observe-services-api/src/main/java/fr/ird/observe/services/ObserveServiceFactory.java @@ -1,21 +1,14 @@ package fr.ird.observe.services; import com.google.common.base.Preconditions; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; import fr.ird.observe.ObserveApplicationContext; -import fr.ird.observe.ObserveTechnicalException; import fr.ird.observe.db.DataSource; -import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import java.io.Closeable; import java.util.HashSet; import java.util.ServiceLoader; import java.util.Set; -import java.util.concurrent.ExecutionException; /** * Created on 4/24/15. @@ -23,7 +16,7 @@ import java.util.concurrent.ExecutionException; * @author Tony Chemit - chemit@codelutin.com * @since 4.0 */ -public class ObserveServiceFactory implements Closeable { +public class ObserveServiceFactory { /** Logger. */ private static final Log log = LogFactory.getLog(ObserveServiceFactory.class); @@ -32,6 +25,34 @@ public class ObserveServiceFactory implements Closeable { protected static Set<ObserveServiceFactoryProvider> providers; + public ObserveServiceFactory(ObserveApplicationContext applicationContext) { + this.applicationContext = applicationContext; + if (log.isInfoEnabled()) { + log.info("Init new ServiceFactory for applicationContext: " + applicationContext); + } + } + + public <S extends ObserveService> S newService(DataSource dataSource, Class<S> serviceType) { + + Preconditions.checkNotNull(dataSource, "dataSource can't be null."); + Preconditions.checkNotNull(serviceType, "serviceType can't be null."); + Preconditions.checkNotNull(dataSource, "dataSource can't be null."); + + ObserveServiceFactoryProvider provider = getProvider(dataSource); + + if (log.isDebugEnabled()) { + log.debug("Using provider: " + provider); + } + ObserveServiceContext serviceContext = new ObserveServiceContext(applicationContext, dataSource); + + S service = provider.newService(serviceType, serviceContext); + if (log.isInfoEnabled()) { + log.info("New service created: " + service + " for dataSource: " + toString(dataSource)); + } + return service; + + } + protected static Set<ObserveServiceFactoryProvider> getProviders() { if (providers == null) { @@ -63,114 +84,7 @@ public class ObserveServiceFactory implements Closeable { } - protected final LoadingCache<ServiceKey<? extends ObserveService>, ObserveService> services; - - public static ObserveServiceFactory newFactory(ObserveApplicationContext applicationContext) { - return new ObserveServiceFactory(applicationContext); - } - - protected ObserveServiceFactory(ObserveApplicationContext applicationContext) { - - this.applicationContext = applicationContext; - this.services = CacheBuilder.newBuilder().build(new CacheLoader<ServiceKey<? extends ObserveService>, ObserveService>() { - - @Override - public ObserveService load(ServiceKey<? extends ObserveService> key) throws Exception { - Preconditions.checkNotNull(key, "service key can't be null"); - - Class<? extends ObserveService> serviceType = key.serviceType; - - ObserveServiceFactoryProvider provider = getProvider(key.dataSource); - - ObserveServiceContext serviceContext = new ObserveServiceContext(ObserveServiceFactory.this.applicationContext, key.dataSource); - ObserveService service = provider.newService(serviceType, serviceContext); - - if (log.isDebugEnabled()) { - log.debug("New service " + service); - } - - return service; - } - }); - - } - - public <S extends ObserveService> S getService(DataSource dataSource, Class<S> serviceType) { - - Preconditions.checkNotNull(dataSource, "dataSource can't be null."); - Preconditions.checkNotNull(serviceType, "serviceType can't be null."); - Preconditions.checkNotNull(dataSource, "dataSource can't be null."); - - ServiceKey<S> key = new ServiceKey<S>(dataSource, serviceType); - - try { - - S s = (S) services.get(key); - return s; - - } catch (ExecutionException e) { - throw new ObserveTechnicalException("Could not get service: " + serviceType, e); - } - - } - - static class ServiceKey<S extends ObserveService> { - - final DataSource dataSource; - - final Class<S> serviceType; - - ServiceKey(DataSource dataSource, Class<S> serviceType) { - - Preconditions.checkNotNull(dataSource, "dataSource can't be null."); - Preconditions.checkNotNull(serviceType, "serviceType can't be null."); - this.dataSource = dataSource; - this.serviceType = serviceType; - - } - - @Override - public boolean equals(Object o) { - - if (this == o) return true; - if (!(o instanceof ServiceKey)) return false; - - ServiceKey<?> that = (ServiceKey<?>) o; - - return dataSource.equals(that.dataSource) - && serviceType.equals(that.serviceType); - - } - - @Override - public int hashCode() { - - int result = dataSource.hashCode(); - result = 31 * result + serviceType.hashCode(); - return result; - - } - } - - public void unregisterDataSource(DataSource dataSource) { - //FIXME Remove all service using this dataSource + protected String toString(DataSource dataSource) { + return dataSource.getClass().getName() + "#" + System.identityHashCode(dataSource); } - - public void clear() { - services.invalidateAll(); - } - - @Override - public void close() { - - for (ObserveService service : services.asMap().values()) { - if (log.isDebugEnabled()) { - log.debug("Close service " + service); - } - IOUtils.closeQuietly(service); - } - services.invalidateAll(); - - } - } diff --git a/observe-services-api/src/main/java/fr/ird/observe/services/ObserveServicesCache.java b/observe-services-api/src/main/java/fr/ird/observe/services/ObserveServicesCache.java new file mode 100644 index 0000000..788589f --- /dev/null +++ b/observe-services-api/src/main/java/fr/ird/observe/services/ObserveServicesCache.java @@ -0,0 +1,191 @@ +package fr.ird.observe.services; + +import com.google.common.base.Preconditions; +import com.google.common.base.Predicate; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.collect.Iterables; +import fr.ird.observe.ObserveTechnicalException; +import fr.ird.observe.db.DataSource; +import fr.ird.observe.db.event.DataSourceEvent; +import fr.ird.observe.db.event.DataSourceListener; +import fr.ird.observe.db.event.DataSourceListenerAdapter; +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.Closeable; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutionException; + +/** + * Created on 5/4/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 4.0 + */ +public class ObserveServicesCache implements Closeable { + + /** Logger. */ + private static final Log log = LogFactory.getLog(ObserveServicesCache.class); + + protected final ObserveServiceFactory factory; + + protected final LoadingCache<ServiceKey<? extends ObserveService>, ObserveService> cache; + + protected final Set<DataSource> usedDataSource; + + protected final DataSourceListener dataSourceListener; + + public ObserveServicesCache(ObserveServiceFactory factory) { + + this.factory = factory; + + this.usedDataSource = new HashSet<DataSource>(); + + this.cache = CacheBuilder.newBuilder().build(new CacheLoader<ServiceKey<? extends ObserveService>, ObserveService>() { + + @Override + public ObserveService load(ServiceKey<? extends ObserveService> key) throws Exception { + + Preconditions.checkNotNull(key, "service key can't be null"); + + boolean add = usedDataSource.add(key.dataSource); + if (add) { + registerDataSource(key.dataSource); + } + ObserveService service = ObserveServicesCache.this.factory.newService(key.dataSource, key.serviceType); + + if (log.isInfoEnabled()) { + log.info("Adding service " + service + "in cache"); + } + return service; + + } + }); + this.dataSourceListener = new DataSourceListenerAdapter() { + + @Override + public void onClosed(DataSourceEvent event) { + + // remove any cache entry using this dataSource + DataSource source = event.getSource(); + unregisterDataSource(source); + + } + }; + } + + public <S extends ObserveService> S getService(DataSource dataSource, Class<S> serviceType) { + + Preconditions.checkNotNull(dataSource, "dataSource can't be null."); + Preconditions.checkNotNull(serviceType, "serviceType can't be null."); + + ServiceKey<S> key = new ServiceKey<S>(dataSource, serviceType); + + try { + + S s = (S) cache.get(key); + return s; + + } catch (ExecutionException e) { + throw new ObserveTechnicalException("Could not get service: " + serviceType, e); + } + + } + + @Override + public void close() { + + removeDataSource(cache.asMap().keySet()); + usedDataSource.clear(); + + } + + protected void registerDataSource(DataSource dataSource) { + + if (log.isInfoEnabled()) { + log.info("Register new dataSource: " + factory.toString(dataSource)); + } + usedDataSource.add(dataSource); + dataSource.addDataSourceListener(dataSourceListener); + + } + + protected void unregisterDataSource(final DataSource dataSource) { + + if (log.isInfoEnabled()) { + log.info("Unregister new dataSource: " + factory.toString(dataSource)); + } + + Iterable<ServiceKey<?>> keysToRemove = Iterables.filter(cache.asMap().keySet(), new Predicate<ServiceKey<? extends ObserveService>>() { + + @Override + public boolean apply(ServiceKey<? extends ObserveService> input) { + return input.dataSource == dataSource; + } + }); + removeDataSource(keysToRemove); + usedDataSource.remove(dataSource); + + } + + protected synchronized void removeDataSource(Iterable<ServiceKey<?>> keysToRemove) { + + Map<ServiceKey<? extends ObserveService>, ObserveService> map = cache.asMap(); + + for (ServiceKey<? extends ObserveService> serviceKey : keysToRemove) { + ObserveService service = map.get(serviceKey); + if (log.isInfoEnabled()) { + log.info("Close service: " + service); + } + IOUtils.closeQuietly(service); + } + cache.invalidateAll(keysToRemove); + + } + + + static class ServiceKey<S extends ObserveService> { + + protected final DataSource dataSource; + + protected final Class<S> serviceType; + + ServiceKey(DataSource dataSource, Class<S> serviceType) { + + Preconditions.checkNotNull(dataSource, "dataSource can't be null."); + Preconditions.checkNotNull(serviceType, "serviceType can't be null."); + + this.dataSource = dataSource; + this.serviceType = serviceType; + + } + + @Override + public boolean equals(Object o) { + + if (this == o) return true; + if (!(o instanceof ServiceKey)) return false; + + ServiceKey<?> that = (ServiceKey<?>) o; + // On doit utiliser une égalité de référence pour la dataSource + return dataSource == that.dataSource && serviceType.equals(that.serviceType); + + } + + @Override + public int hashCode() { + + int result = dataSource.hashCode(); + result = 31 * result + serviceType.hashCode(); + return result; + + } + } + + +} diff --git a/observe-services-api/src/main/java/fr/ird/observe/services/data/TripService.java b/observe-services-api/src/main/java/fr/ird/observe/services/data/TripService.java index 9f0933b..db47799 100644 --- a/observe-services-api/src/main/java/fr/ird/observe/services/data/TripService.java +++ b/observe-services-api/src/main/java/fr/ird/observe/services/data/TripService.java @@ -5,6 +5,7 @@ import fr.ird.observe.entities.referentiel.Ocean; import fr.ird.observe.entities.referentiel.Program; import fr.ird.observe.services.Commit; import fr.ird.observe.services.ObserveService; +import fr.ird.observe.services.model.DataSelectionModel; import fr.ird.observe.tripMap.TripMapPoint; import java.util.List; @@ -17,6 +18,8 @@ import java.util.List; */ public interface TripService extends ObserveService { + DataSelectionModel loadDataSelectionModel(); + List<Program> getPossibleProgramsForTrip(String tripId); Trip getTrip(String tripId); diff --git a/observe-services-api/src/main/java/fr/ird/observe/services/model/DataSelectionModel.java b/observe-services-api/src/main/java/fr/ird/observe/services/model/DataSelectionModel.java index c3ea1f0..f03fc98 100644 --- a/observe-services-api/src/main/java/fr/ird/observe/services/model/DataSelectionModel.java +++ b/observe-services-api/src/main/java/fr/ird/observe/services/model/DataSelectionModel.java @@ -21,18 +21,9 @@ */ package fr.ird.observe.services.model; -import fr.ird.observe.db.DataSource; import fr.ird.observe.entities.Entities; -import fr.ird.observe.entities.Trip; import fr.ird.observe.entities.Trips; -import fr.ird.observe.entities.longline.TripLongline; import fr.ird.observe.entities.referentiel.Program; -import fr.ird.observe.entities.referentiel.Programs; -import fr.ird.observe.entities.seine.TripSeine; -import fr.ird.observe.services.ObserveServiceFactory; -import fr.ird.observe.services.data.longline.TripLonglineService; -import fr.ird.observe.services.data.seine.TripSeineService; -import fr.ird.observe.services.referential.ReferentialService; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -60,78 +51,6 @@ import java.util.TreeMap; */ public class DataSelectionModel implements Serializable { - public static void populate(DataSelectionModel model, ObserveServiceFactory services, DataSource dataSource) throws Exception { - -// source.checkState(DataSourceState.OPEN); - - Map<String, List<String>> datas = new HashMap<String, List<String>>(); - - TripSeineService tripSeineService = services.getService(dataSource, TripSeineService.class); - TripLonglineService tripLonglineService = services.getService(dataSource, TripLonglineService.class); - - List<Program> programs = services.getService(dataSource, ReferentialService.class).getAllProgramStub(); - for (Program program : programs) { - - if (Programs.isProgramSeine(program)) { - List<TripSeine> trips = tripSeineService.getTripSeineStubByProgram(program.getTopiaId()); - populate(model, program, trips, datas); - } - - if (Programs.isProgramLongline(program)) { - List<TripLongline> trips = tripLonglineService.getTripLonglineStubByProgram(program.getTopiaId()); - populate(model, program, trips, datas); - } - - } - model.setDatas(datas); - - } - - protected static <T extends Trip> void populate(DataSelectionModel model, - Program program, - List<T> trips, - Map<String, List<String>> datas) { - - model.cacheEntity(program); - - if (!trips.isEmpty()) { - List<String> tripIds = new ArrayList<String>(); - - if (model.isUseOpenData()) { - - // ajout de toutes les marées ouvertes ou non - for (T trip : trips) { - trip.setProgram(program); - model.cacheEntity(trip); - tripIds.add(trip.getTopiaId()); - } - - } else { - - // ajout de toutes les marées non ouvertes - for (T trip : trips) { - trip.setProgram(program); - if (!trip.isOpen()) { - model.cacheEntity(trip); - tripIds.add(trip.getTopiaId()); - } - } - - } - - if (!tripIds.isEmpty()) { - - datas.put(program.getTopiaId(), tripIds); - if (log.isDebugEnabled()) { - log.debug("Add program " + program.getLabel1() + " with " + tripIds.size() + " trip(s)."); - } - - } - - } - - } - public static final String PROPERTY_USE_REFERENTIEL = "useReferentiel"; public static final String PROPERTY_USE_DATA = "useData"; @@ -187,10 +106,6 @@ public class DataSelectionModel implements Serializable { protected final PropertyChangeSupport pcs; - public void populate() throws Exception { - throw new UnsupportedOperationException(); - } - public DataSelectionModel() { pcs = new PropertyChangeSupport(this); Class<?>[] classes = TopiaEntityHelper.getContracts(Entities.REFERENCE_ENTITIES); @@ -227,7 +142,7 @@ public class DataSelectionModel implements Serializable { return true; } - protected void cacheEntity(TopiaEntity entity) { + public void cacheEntity(TopiaEntity entity) { entityCache.put(entity.getTopiaId(), entity); } @@ -492,20 +407,22 @@ public class DataSelectionModel implements Serializable { } protected void checkReferentielType(Class<?> type) { + if (!referentiel.contains(type)) { - throw new IllegalArgumentException( - "given <" + type + "> is not in referentiel universe : " + - referentiel); + throw new IllegalArgumentException("given <" + type + "> is not in referentiel universe : " + referentiel); } + } public void destroy() { + removeAll(); // suppression de tous les listeners PropertyChangeListener[] listeners = pcs.getPropertyChangeListeners(); for (PropertyChangeListener l : listeners) { removePropertyChangeListener(l); } + } @Override @@ -513,4 +430,12 @@ public class DataSelectionModel implements Serializable { super.finalize(); destroy(); } + + public void copyDataTo(DataSelectionModel dataSelectionModel) { + + dataSelectionModel.setDatas(datas); + dataSelectionModel.entityCache.clear(); + dataSelectionModel.entityCache.putAll((Map) entityCache); + + } } diff --git a/observe-services/src/main/java/fr/ird/observe/services/ObserveServiceFactoryProviderTopia.java b/observe-services/src/main/java/fr/ird/observe/services/ObserveServiceFactoryProviderTopia.java index da22f6a..3ee2edc 100644 --- a/observe-services/src/main/java/fr/ird/observe/services/ObserveServiceFactoryProviderTopia.java +++ b/observe-services/src/main/java/fr/ird/observe/services/ObserveServiceFactoryProviderTopia.java @@ -25,8 +25,9 @@ import java.util.Set; */ public class ObserveServiceFactoryProviderTopia implements ObserveServiceFactoryProvider { + /** Logger. */ private static final Log log = LogFactory.getLog(ObserveServiceFactoryProviderTopia.class); - + @Override public boolean acceptDataSource(DataSource dataSource) { @@ -81,7 +82,7 @@ public class ObserveServiceFactoryProviderTopia implements ObserveServiceFactory // Instanciate transactional proxied service ServiceInvocationHandler invocationHandler = new ServiceInvocationHandler(serviceContext, service); - S proxyService = (S) Proxy.newProxyInstance(ObserveServiceFactory.class.getClassLoader(), new Class[]{serviceType}, invocationHandler); + S proxyService = (S) Proxy.newProxyInstance(ObserveServiceFactoryProviderTopia.class.getClassLoader(), new Class[]{serviceType}, invocationHandler); return proxyService; @@ -96,6 +97,7 @@ public class ObserveServiceFactoryProviderTopia implements ObserveServiceFactory private final Set<String> methodNamesToByPass; protected ServiceInvocationHandler(ObserveServiceContext serviceContext, ObserveService target) { + this.serviceContext = serviceContext; this.target = target; this.methodNamesToByPass = Sets.newHashSet( @@ -104,8 +106,9 @@ public class ObserveServiceFactoryProviderTopia implements ObserveServiceFactory "finalize", "toString", "clone", - "getClass" - ); + "getClass", + "close"); + } @Override diff --git a/observe-services/src/main/java/fr/ird/observe/services/data/TripServiceImpl.java b/observe-services/src/main/java/fr/ird/observe/services/data/TripServiceImpl.java index d419ecb..d7ed97b 100644 --- a/observe-services/src/main/java/fr/ird/observe/services/data/TripServiceImpl.java +++ b/observe-services/src/main/java/fr/ird/observe/services/data/TripServiceImpl.java @@ -13,9 +13,18 @@ import fr.ird.observe.entities.referentiel.Programs; import fr.ird.observe.entities.seine.TripSeine; import fr.ird.observe.entities.seine.TripSeineDAO; import fr.ird.observe.services.AbstractObserveService; +import fr.ird.observe.services.data.longline.TripLonglineService; +import fr.ird.observe.services.data.seine.TripSeineService; +import fr.ird.observe.services.model.DataSelectionModel; +import fr.ird.observe.services.referential.ReferentialService; import fr.ird.observe.tripMap.TripMapPoint; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Created on 4/26/15. @@ -25,6 +34,39 @@ import java.util.List; */ public class TripServiceImpl extends AbstractObserveService implements TripService { + /** Logger. */ + private static final Log log = LogFactory.getLog(TripServiceImpl.class); + + @Override + public DataSelectionModel loadDataSelectionModel() { + + DataSelectionModel result = new DataSelectionModel(); + + Map<String, List<String>> datas = new HashMap<String, List<String>>(); + + TripSeineService tripSeineService = newService(TripSeineService.class); + TripLonglineService tripLonglineService = newService(TripLonglineService.class); + + List<Program> programs = newService(ReferentialService.class).getAllProgramStub(); + for (Program program : programs) { + + if (Programs.isProgramSeine(program)) { + List<TripSeine> trips = tripSeineService.getTripSeineStubByProgram(program.getTopiaId()); + populate(result, program, trips, datas); + } + + if (Programs.isProgramLongline(program)) { + List<TripLongline> trips = tripLonglineService.getTripLonglineStubByProgram(program.getTopiaId()); + populate(result, program, trips, datas); + } + + } + result.setDatas(datas); + + return result; + + } + @Override public List<Program> getPossibleProgramsForTrip(String tripId) { @@ -121,4 +163,50 @@ public class TripServiceImpl extends AbstractObserveService implements TripServi } } + + protected static <T extends Trip> void populate(DataSelectionModel model, + Program program, + List<T> trips, + Map<String, List<String>> datas) { + + model.cacheEntity(program); + + if (!trips.isEmpty()) { + List<String> tripIds = new ArrayList<String>(); + + if (model.isUseOpenData()) { + + // ajout de toutes les marées ouvertes ou non + for (T trip : trips) { + trip.setProgram(program); + model.cacheEntity(trip); + tripIds.add(trip.getTopiaId()); + } + + } else { + + // ajout de toutes les marées non ouvertes + for (T trip : trips) { + trip.setProgram(program); + if (!trip.isOpen()) { + model.cacheEntity(trip); + tripIds.add(trip.getTopiaId()); + } + } + + } + + if (!tripIds.isEmpty()) { + + datas.put(program.getTopiaId(), tripIds); + if (log.isDebugEnabled()) { + log.debug("Add program " + program.getLabel1() + " with " + tripIds.size() + " trip(s)."); + } + + } + + } + + } + } diff --git a/observe-swing/src/main/java/fr/ird/observe/ObserveContext.java b/observe-swing/src/main/java/fr/ird/observe/ObserveContext.java index 0d05c39..8eafd51 100644 --- a/observe-swing/src/main/java/fr/ird/observe/ObserveContext.java +++ b/observe-swing/src/main/java/fr/ird/observe/ObserveContext.java @@ -30,6 +30,7 @@ import fr.ird.observe.db.event.DataSourceListenerAdapter; import fr.ird.observe.db.impl.H2DataSource; import fr.ird.observe.services.ObserveService; import fr.ird.observe.services.ObserveServiceFactory; +import fr.ird.observe.services.ObserveServicesCache; import fr.ird.observe.services.data.OpenableService; import fr.ird.observe.ui.ObserveMainUI; import fr.ird.observe.ui.ObserveMainUIHandler; @@ -237,22 +238,16 @@ public class ObserveContext extends ObserveApplicationContext { } public <S extends ObserveService> S getService(Class<S> serviceType) { - S service = services.getService(getDataSource(), serviceType); + S service = servicesCache.getService(getDataSource(), serviceType); return service; } public static <S extends ObserveService> S getService(DataSource dataSource, Class<S> serviceType) { - S service = get().services.getService(dataSource, serviceType); + S service = get().servicesCache.getService(dataSource, serviceType); return service; } - public ObserveServiceFactory getServices() { - return services; - } - - public void initStorage(ObserveConfig config, - ObserveMainUI mainUI, - boolean askToCreate) { + public void initStorage(ObserveConfig config, ObserveMainUI mainUI, boolean askToCreate) { try { if (config.isLocalStorageExist()) { @@ -548,6 +543,8 @@ public class ObserveContext extends ObserveApplicationContext { actionMap.put(actionId, action); } - private final ObserveServiceFactory services = ObserveServiceFactory.newFactory(this); + private final ObserveServiceFactory serviceFactory = new ObserveServiceFactory(this); + + protected final ObserveServicesCache servicesCache = new ObserveServicesCache(serviceFactory); } diff --git a/observe-swing/src/main/java/fr/ird/observe/ui/admin/AdminUIModel.java b/observe-swing/src/main/java/fr/ird/observe/ui/admin/AdminUIModel.java index 28e36fc..79238c5 100644 --- a/observe-swing/src/main/java/fr/ird/observe/ui/admin/AdminUIModel.java +++ b/observe-swing/src/main/java/fr/ird/observe/ui/admin/AdminUIModel.java @@ -34,6 +34,7 @@ import fr.ird.observe.db.impl.PGDataSource; import fr.ird.observe.entities.Trip; import fr.ird.observe.entities.Trips; import fr.ird.observe.report.model.Report; +import fr.ird.observe.services.data.TripService; import fr.ird.observe.services.model.DataSelectionModel; import fr.ird.observe.ui.admin.consolidate.ConsolidateModel; import fr.ird.observe.ui.admin.export.ExportModel; @@ -512,7 +513,6 @@ public class AdminUIModel extends WizardExtModel<AdminStep> { availableIncomingModes.clear(); storageHandler = ui.getContextValue(StorageUIHandler.class); -// dataService = ui.getContextValue(DataService.class); config = ui.getContextValue(ObserveConfig.class); dataContext = ui.getContextValue(DataContext.class); @@ -1275,31 +1275,28 @@ public class AdminUIModel extends WizardExtModel<AdminStep> { } public void populateSelectionModel(DataSource dataSource) { + try { - DataSelectionModel.populate(getSelectionDataModel(), ObserveContext.get().getServices(), dataSource); - } catch (Exception e) { - if (log.isErrorEnabled()) { - log.error("could not populate selected model", e); - } + + TripService service = ObserveContext.getService(dataSource, TripService.class); + DataSelectionModel dataSelectionModel = service.loadDataSelectionModel(); + dataSelectionModel.copyDataTo(getSelectionDataModel()); + } finally { // on notifie que le modèle de sélection a changé // (il faut donc recalculé l'arbre de sélection) firePropertyChange(SELECTION_MODEL_CHANGED_PROPERTY_NAME, getSelectionDataModel()); + } + } protected void computeExistingTrips(DataSource dataSource) { - DataSelectionModel selectionModel = new DataSelectionModel(); + TripService service = ObserveContext.getService(dataSource, TripService.class); + DataSelectionModel selectionModel = service.loadDataSelectionModel(); selectionModel.setUseOpenData(true); - try { - DataSelectionModel.populate(selectionModel, ObserveContext.get().getServices(), dataSource); - } catch (Exception e) { - if (log.isErrorEnabled()) { - log.error("could not populate selected model", e); - } - } List<String> existingTripIds = new ArrayList<String>(); diff --git a/observe-swing/src/main/java/fr/ird/observe/ui/admin/consolidate/ConsolidateUIHandler.java b/observe-swing/src/main/java/fr/ird/observe/ui/admin/consolidate/ConsolidateUIHandler.java index f153528..77aaa3d 100644 --- a/observe-swing/src/main/java/fr/ird/observe/ui/admin/consolidate/ConsolidateUIHandler.java +++ b/observe-swing/src/main/java/fr/ird/observe/ui/admin/consolidate/ConsolidateUIHandler.java @@ -129,7 +129,7 @@ public class ConsolidateUIHandler extends AdminTabUIHandler { Set<String> tripIds = model.getSelectionDataModel().getSelectedData(); - ComputeDataService service = ObserveContext.get().getServices().getService(getStepModel().getSource(), ComputeDataService.class); + ComputeDataService service = ObserveContext.getService(getStepModel().getSource(), ComputeDataService.class); try { diff --git a/observe-swing/src/main/java/fr/ird/observe/ui/admin/export/ExportModel.java b/observe-swing/src/main/java/fr/ird/observe/ui/admin/export/ExportModel.java index b6bf549..959dd3f 100644 --- a/observe-swing/src/main/java/fr/ird/observe/ui/admin/export/ExportModel.java +++ b/observe-swing/src/main/java/fr/ird/observe/ui/admin/export/ExportModel.java @@ -27,7 +27,6 @@ import fr.ird.observe.entities.Trip; import fr.ird.observe.entities.longline.TripLongline; import fr.ird.observe.entities.referentiel.Program; import fr.ird.observe.entities.seine.TripSeine; -import fr.ird.observe.services.ObserveServiceFactory; import fr.ird.observe.services.model.DataSelectionModel; import fr.ird.observe.ui.admin.AdminActionModel; import fr.ird.observe.ui.admin.AdminStep; @@ -65,8 +64,6 @@ public class ExportModel extends AdminActionModel { protected DataSource centralSource; - protected ObserveServiceFactory centralServiceFactory; - public ExportModel() { super(AdminStep.EXPORT_DATA); } diff --git a/observe-swing/src/main/java/fr/ird/observe/ui/storage/StorageUIHandler.java b/observe-swing/src/main/java/fr/ird/observe/ui/storage/StorageUIHandler.java index b873656..9f98482 100644 --- a/observe-swing/src/main/java/fr/ird/observe/ui/storage/StorageUIHandler.java +++ b/observe-swing/src/main/java/fr/ird/observe/ui/storage/StorageUIHandler.java @@ -45,6 +45,7 @@ import fr.ird.observe.db.util.SecurityModel; import fr.ird.observe.entities.referentiel.Program; import fr.ird.observe.entities.seine.TripSeine; import fr.ird.observe.services.ReplicationService; +import fr.ird.observe.services.data.TripService; import fr.ird.observe.services.model.DataSelectionModel; import fr.ird.observe.ui.ObserveMainUI; import fr.ird.observe.ui.UIHelper; @@ -1077,10 +1078,8 @@ public class StorageUIHandler { try { source.doOpen(); - Decorator<Program> pDecorator = - getDecoratorService().getDecoratorByType(Program.class); - Decorator<TripSeine> mDecorator = - getDecoratorService().getDecoratorByType(TripSeine.class); + Decorator<Program> pDecorator = getDecoratorService().getDecoratorByType(Program.class); + Decorator<TripSeine> mDecorator = getDecoratorService().getDecoratorByType(TripSeine.class); Map<String, List<String>> dataByProgram = dataModel.getSelectedDataByProgram(); @@ -1171,43 +1170,36 @@ public class StorageUIHandler { StorageUIModel model = ui.getModel(); - try { - - Preconditions.checkState(source != null, "Can't select data on a null dataSource"); + Preconditions.checkState(source != null, "Can't select data on a null dataSource"); - DataSelectionModel dataModel = new DataSelectionModel(); - dataModel.setUseData(true); - dataModel.setUseOpenData(true); - dataModel.setUseReferentiel(false); - DataSelectionModel.populate(dataModel, ObserveContext.get().getServices(), source); + TripService service = ObserveContext.getService(source, TripService.class); + DataSelectionModel dataModel = service.loadDataSelectionModel(); + dataModel.setUseData(true); + dataModel.setUseOpenData(true); + dataModel.setUseReferentiel(false); - // positionnement du model de selection de données - // dans le model du wizard - model.setSelectDataModel(dataModel); + // positionnement du model de selection de données dans le model du wizard + model.setSelectDataModel(dataModel); - // initialisation de l'ui dedié - ui.getSELECT_DATA().initTree(source); - - } catch (Exception e) { - throw new RuntimeException("Could not grab data to select", e); - } + // initialisation de l'ui dedié + ui.getSELECT_DATA().initTree(source); } - protected void checkImportDbVersion(StorageUIModel model, DataSource dataSource) throws DataSourceException { - try { - dataSource.doOpen(); - - Version importServiceDbVersion = dataSource.getDbVersion(); - Version currentDbVersion = model.getDbVersion(); - if (importServiceDbVersion.before(currentDbVersion)) { - throw new IllegalStateException("Import db version (" + importServiceDbVersion + ") is not compatible with the current database version (" + currentDbVersion + ")"); - } - } finally { - dataSource.doClose(false); - } - - } +// protected void checkImportDbVersion(StorageUIModel model, DataSource dataSource) throws DataSourceException { +// try { +// dataSource.doOpen(); +// +// Version importServiceDbVersion = dataSource.getDbVersion(); +// Version currentDbVersion = model.getDbVersion(); +// if (importServiceDbVersion.before(currentDbVersion)) { +// throw new IllegalStateException("Import db version (" + importServiceDbVersion + ") is not compatible with the current database version (" + currentDbVersion + ")"); +// } +// } finally { +// dataSource.doClose(false); +// } +// +// } protected void initSelectData(StorageUI ui) { @@ -1238,6 +1230,7 @@ public class StorageUIHandler { log.error(e, e); } } + } protected void updateSecurity(StorageUIModel model, RolesTableModel roleModel) { -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.