r462 - in trunk/tutti-persistence/src: main/java/fr/ifremer/tutti/persistence test/java/fr/ifremer/tutti/persistence/service
Author: tchemit Date: 2013-02-25 15:30:44 +0100 (Mon, 25 Feb 2013) New Revision: 462 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/462 Log: remove dev persistence implementation Removed: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceDevImpl.java Modified: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceReadTest.java Deleted: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceDevImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceDevImpl.java 2013-02-25 14:11:54 UTC (rev 461) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceDevImpl.java 2013-02-25 14:30:44 UTC (rev 462) @@ -1,1470 +0,0 @@ -package fr.ifremer.tutti.persistence; - -/* - * #%L - * Tutti :: Persistence Dev (impl) - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2012 Ifremer - * %% - * 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 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>. - * #L% - */ - - -import com.google.common.base.Function; -import com.google.common.base.Preconditions; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import fr.ifremer.tutti.persistence.entities.AbstractTuttiEntity; -import fr.ifremer.tutti.persistence.entities.IdAware; -import fr.ifremer.tutti.persistence.entities.TuttiEntities; -import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch; -import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; -import fr.ifremer.tutti.persistence.entities.data.CatchBatch; -import fr.ifremer.tutti.persistence.entities.data.Cruise; -import fr.ifremer.tutti.persistence.entities.data.FishingOperation; -import fr.ifremer.tutti.persistence.entities.data.FishingOperationAware; -import fr.ifremer.tutti.persistence.entities.data.MacroWasteBatch; -import fr.ifremer.tutti.persistence.entities.data.PlanktonBatch; -import fr.ifremer.tutti.persistence.entities.data.Program; -import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; -import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; -import fr.ifremer.tutti.persistence.entities.protocol.SpeciesProtocol; -import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol; -import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; -import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; -import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType; -import fr.ifremer.tutti.persistence.entities.referential.Country; -import fr.ifremer.tutti.persistence.entities.referential.FishingOperationLocation; -import fr.ifremer.tutti.persistence.entities.referential.Gear; -import fr.ifremer.tutti.persistence.entities.referential.Person; -import fr.ifremer.tutti.persistence.entities.referential.Species; -import fr.ifremer.tutti.persistence.entities.referential.Vessel; -import fr.ifremer.tutti.persistence.entities.referential.Zone; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.util.ApplicationConfig; -import org.nuiton.util.beans.Binder; -import org.nuiton.util.beans.BinderFactory; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.UUID; - -/** - * Persistence service implementation for dev purpose (no real db). - * - * @author tchemit <chemit@codelutin.com> - * @since 0.1 - */ -public class TuttiPersistenceDevImpl implements TuttiPersistence { - - /** Logger. */ - private static final Log log = - LogFactory.getLog(TuttiPersistenceDevImpl.class); - - public enum CaracteristicEnum { - All, - SizeCategory, - SexCategory, - SortedUnsortedCategory, - MaturityCategory, - MacroWasteCategory, - MacroWasteSizeCategory - } - - public static final List<String> FILE_KEYS = Lists.newArrayList( - "fr.ifremer.tutti.persistence.entities.data.Attachment", - "fr.ifremer.tutti.persistence.entities.data.AccidentalBatch", - "fr.ifremer.tutti.persistence.entities.data.BenthosBatch", - "fr.ifremer.tutti.persistence.entities.data.FishingOperation", - "fr.ifremer.tutti.persistence.entities.data.MacroWasteBatch", - "fr.ifremer.tutti.persistence.entities.data.PlanktonBatch", - "fr.ifremer.tutti.persistence.entities.data.Program", - "fr.ifremer.tutti.persistence.entities.data.Cruise", - "fr.ifremer.tutti.persistence.entities.data.CatchBatch", - "fr.ifremer.tutti.persistence.entities.data.SpeciesBatch", - "fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency", - "fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol", - "fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue", - "fr.ifremer.tutti.persistence.entities.referential.Country", - "fr.ifremer.tutti.persistence.entities.referential.FishingOperationLocation#Strata", - "fr.ifremer.tutti.persistence.entities.referential.FishingOperationLocation#SubStrata", - "fr.ifremer.tutti.persistence.entities.referential.FishingOperationLocation#Localite", - "fr.ifremer.tutti.persistence.entities.referential.Gear#Scientific", - "fr.ifremer.tutti.persistence.entities.referential.Gear#Fishing", - "fr.ifremer.tutti.persistence.entities.referential.Person", - "fr.ifremer.tutti.persistence.entities.referential.Species", - "fr.ifremer.tutti.persistence.entities.referential.Vessel#Scientific", - "fr.ifremer.tutti.persistence.entities.referential.Vessel#Fishing", - "fr.ifremer.tutti.persistence.entities.referential.Zone" - ); - - static { - for (CaracteristicEnum caracteristicEnum : - CaracteristicEnum.values()) { - FILE_KEYS.add(getKey(Caracteristic.class, caracteristicEnum.name())); - } - - if (log.isInfoEnabled()) { - log.info("Found " + FILE_KEYS.size() + " files."); - } - } - - public static final String SKIP_FIXTURES_OPTION = "LoadFixtures"; - - public static final Function<TuttiProtocol, String> GET_PROTOCOL_NAME = new Function<TuttiProtocol, String>() { - - @Override - public String apply(TuttiProtocol input) { - return input.getName(); - } - }; - - /** - * Persistence config. - * - * @since 0.1 - */ - protected final ApplicationConfig config; - - /** - * Cache of data. - * - * @since 0.1 - */ - protected final ArrayListMultimap<String, IdAware> cache = ArrayListMultimap.create(); - - /** - * Cache of binder. - * - * @since 0.2 - */ - protected final Map<String, Binder<? extends IdAware, ? extends IdAware>> binderCache = Maps.newHashMap(); - - - protected final File storageDirectory; - - - public static File getStorageDirectory(ApplicationConfig config) { - return new File(config.getOptionAsFile("tutti.data.directory"), "devStorage"); - } - - public TuttiPersistenceDevImpl(ApplicationConfig config) { - - Preconditions.checkNotNull(config, " No config property setted"); - - this.config = config; - - this.storageDirectory = getStorageDirectory(this.config); - } - - //------------------------------------------------------------------------// - //-- Technical methods --// - //------------------------------------------------------------------------// - - @Override - public String getImplementationName() { - return "Persistence Dev implementation"; - } - - @Override - public void init() { - - try { - - FileUtils.forceMkdir(storageDirectory); - } catch (IOException e) { - throw new RuntimeException("Could not create directory", e); - } - - cache.clear(); - - boolean loadFixtures = - config.getOptionAsBoolean(SKIP_FIXTURES_OPTION); - if (!loadFixtures) { - - if (log.isInfoEnabled()) { - log.info("Load default fixtures..."); - } - TuttiPersistenceDevFixtures fixtures = - new TuttiPersistenceDevFixtures(); - - for (String entityType : FILE_KEYS) { - try { - loadEntities(entityType, fixtures); - } catch (IOException e) { - throw new RuntimeException("Could not load entities " + entityType, e); - } - } - } - } - - @Override - public void close() throws IOException { - - if (log.isInfoEnabled()) { - log.info("Close persistence driver " + getImplementationName()); - } - binderCache.clear(); - - for (String entityType : cache.keySet()) { - persistToFile(entityType); - } - } - - //------------------------------------------------------------------------// - //-- Referential methods --// - //------------------------------------------------------------------------// - - @Override - public List<Species> getAllSpecies() { - List<Species> result = getDataInNewList(Species.class.getName()); - return result; - } - - @Override - public List<Species> getAllReferentSpecies() { - throw new IllegalStateException("method 'getAllReferentSpecies' not implemented"); - } - - @Override - public List<Species> getAllSpecies(TuttiProtocol protocol) { - List<Species> result = getAllSpecies(); - if (protocol != null && !protocol.isSpeciesEmpty()) { - Map<String, Species> map = TuttiEntities.splitById(result); - for (SpeciesProtocol speciesProtocol : protocol.getSpecies()) { - map.get(speciesProtocol.getSpeciesReferenceTaxonId()).setSurveyCode(speciesProtocol.getSpeciesSurveyCode()); - } - } - return result; - } - - @Override - public List<Caracteristic> getAllCaracteristic() { - List<Caracteristic> result = getAllCaracteristic(CaracteristicEnum.All); - return result; - } - - @Override - public List<Zone> getAllProgramZone() { - List<Zone> result = getDataInNewList(Zone.class.getName()); - return result; - } - - @Override - public List<Country> getAllCountry() { - List<Country> result = getDataInNewList(Country.class.getName()); - return result; - } - - @Override - public List<FishingOperationLocation> getAllFishingOperationStrata(String zoneId) { - List<FishingOperationLocation> result = getDataInNewList(getKey(FishingOperationLocation.class, "Strata")); - return result; - } - - @Override - public List<FishingOperationLocation> getAllFishingOperationSubStrata(String zoneId, String strataId) { - List<FishingOperationLocation> result = getDataInNewList(getKey(FishingOperationLocation.class, "SubStrata")); - return result; - } - - @Override - public List<FishingOperationLocation> getAllFishingOperationLocation(String zoneId, String strataId, String subStrataId) { - List<FishingOperationLocation> result = getDataInNewList(getKey(FishingOperationLocation.class, "Localite")); - return result; - } - - @Override - public List<Vessel> getAllScientificVessel() { - List<Vessel> result = getDataInNewList(getKey(Vessel.class, "Scientific")); - return result; - } - - @Override - public List<Vessel> getAllFishingVessel() { - List<Vessel> result = getDataInNewList(getKey(Vessel.class, "Fishing")); - return result; - } - - @Override - public List<Gear> getAllScientificGear() { - List<Gear> result = getDataInNewList(getKey(Gear.class, "Scientific")); - return result; - } - - @Override - public List<Gear> getAllFishingGear() { - List<Gear> result = getDataInNewList(getKey(Gear.class, "Fishing")); - return result; - } - - @Override - public List<Person> getAllPerson() { - List<Person> result = getDataInNewList(Person.class.getName()); - return result; - } - - @Override - public Caracteristic getSizeCategoryCaracteristic() { - return getCaracteristic(CaracteristicEnum.SizeCategory); - } - - @Override - public Caracteristic getSexCaracteristic() { - return getCaracteristic(CaracteristicEnum.SexCategory); - } - - @Override - public Caracteristic getSortedUnsortedCaracteristic() { - return getCaracteristic(CaracteristicEnum.SortedUnsortedCategory); - } - - @Override - public Caracteristic getMaturityCaracteristic() { - return getCaracteristic(CaracteristicEnum.MaturityCategory); - } - - @Override - public Caracteristic getMacroWasteCategoryCaracteristic() { - return getCaracteristic(CaracteristicEnum.MacroWasteCategory); - } - - @Override - public Caracteristic getMacroWasteSizeCategoryCaracteristic() { - return getCaracteristic(CaracteristicEnum.MacroWasteSizeCategory); - } - - @Override - public Vessel getVessel(String vesselCode) { - return getBean(Vessel.class.getName(), vesselCode); - } - - @Override - public Person getPerson(Integer personId) { - return getBean(Person.class.getName(), personId.toString()); - } - - @Override - public Gear getGear(Integer gearCode) { - return getBean(Gear.class.getName(), gearCode.toString()); - } - - @Override - public boolean isSortedQualitativeValue(CaracteristicQualitativeValue value) { - return value != null && value.getId().equals("311"); - } - - @Override - public List<Species> importTemporarySpecies(List<Species> species) { - throw new IllegalStateException("method 'importTemporarySpecies' not implemented"); - } - - @Override - public List<Vessel> importTemporaryVessel(List<Vessel> vessels) { - throw new IllegalStateException("method 'importTemporaryVessel' not implemented"); - } - - @Override - public List<Person> importTemporaryPerson(List<Person> persons) { - throw new IllegalStateException("method 'importTemporaryPerson' not implemented"); - } - - @Override - public List<Gear> importTemporaryGear(List<Gear> gears) { - throw new IllegalStateException("method 'importTemporaryGear' not implemented"); - } - - //------------------------------------------------------------------------// - //-- Program methods --// - //------------------------------------------------------------------------// - - @Override - public List<Program> getAllProgram() { - List<Program> result = getDataInNewList(Program.class.getName()); - return result; - } - - @Override - public Program getProgram(String id) { - Program result = getBean(Program.class.getName(), id); - return result; - } - - @Override - public Program createProgram(Program bean) { - Program result = create(Program.class.getName(), bean); - return result; - } - - @Override - public Program saveProgram(Program bean) { - Program result = save(Program.class.getName(), bean); - return result; - } - - //------------------------------------------------------------------------// - //-- Cruise methods --// - //------------------------------------------------------------------------// - - @Override - public List<Cruise> getAllCruise(final String programId) { - List<Cruise> result = getFilteredDataList(Cruise.class.getName(), new Predicate<Cruise>() { - @Override - public boolean apply(Cruise input) { - return programId.equals(input.getProgram().getId()); - } - }); - return result; - } - - @Override - public Cruise getCruise(String id) { - Cruise result = getBean(Cruise.class.getName(), id); - return result; - } - - @Override - public Cruise createCruise(Cruise bean) { - Cruise result = create(Cruise.class.getName(), bean); - return result; - } - - @Override - public Cruise saveCruise(Cruise bean) { - Cruise result = save(Cruise.class.getName(), bean); - return result; - } - - //------------------------------------------------------------------------// - //-- Protocol methods --// - //------------------------------------------------------------------------// - - @Override - public List<String> getAllProtocolNames() { - throw new IllegalStateException("method 'getAllProtocolNames' not implemented"); - } - - @Override - public List<TuttiProtocol> getAllProtocol() { - throw new IllegalStateException("method 'getAllProtocol' not implemented"); - } - - @Override - public boolean isProtocolExist(String id) { - throw new IllegalStateException("method 'isProtocolExist' not implemented"); - } - - @Override - public TuttiProtocol getProtocol(String id) { - throw new IllegalStateException("method 'getProtocol' not implemented"); - } - - @Override - public TuttiProtocol createProtocol(TuttiProtocol bean) { - throw new IllegalStateException("method 'createProtocol' not implemented"); - } - - @Override - public TuttiProtocol saveProtocol(TuttiProtocol bean) { - throw new IllegalStateException("method 'saveProtocol' not implemented"); - } - - @Override - public void deleteProtocol(String id) { - throw new IllegalStateException("method 'deleteProtocol' not implemented"); - } - - //------------------------------------------------------------------------// - //-- FishingOperation methods --// - //------------------------------------------------------------------------// - - @Override - public List<FishingOperation> getAllFishingOperation(final String cruiseId) { - List<FishingOperation> result = getFilteredDataList(FishingOperation.class.getName(), new Predicate<FishingOperation>() { - @Override - public boolean apply(FishingOperation input) { - return cruiseId.equals(input.getCruise().getId()); - } - }); - return result; - } - - @Override - public FishingOperation getFishingOperation(String id) { - List<FishingOperation> data = getData(FishingOperation.class.getName()); - FishingOperation result = TuttiEntities.findById(data, id); - return result; - } - - @Override - public FishingOperation createFishingOperation(FishingOperation bean) { - FishingOperation result = create(FishingOperation.class.getName(), bean); - return result; - } - - @Override - public FishingOperation saveFishingOperation(FishingOperation bean) { - FishingOperation result = save(FishingOperation.class.getName(), bean); - return result; - } - - //------------------------------------------------------------------------// - //-- CatchBatch methods --// - //------------------------------------------------------------------------// - - @Override - public CatchBatch getCatchBatchFromFishingOperation(String id) { - List<CatchBatch> data = getData(CatchBatch.class.getName()); - CatchBatch result = null; - for (CatchBatch catchBatch : data) { - if (id.equals(catchBatch.getFishingOperation().getId())) { - result = catchBatch; - break; - } - } - return result; - } - - @Override - public CatchBatch createCatchBatch(CatchBatch bean) { - CatchBatch result = create(CatchBatch.class.getName(), bean); - return result; - } - - @Override - public CatchBatch saveCatchBatch(CatchBatch bean) { - CatchBatch result = save(CatchBatch.class.getName(), bean); - return result; - } - - //------------------------------------------------------------------------// - //-- Species Batch methods --// - //------------------------------------------------------------------------// - - @Override - public List<SpeciesBatch> getAllRootSpeciesBatch(String fishingOperationId) { - List<SpeciesBatch> result = getAllTraitFilterBatches( - SpeciesBatch.class.getName(), fishingOperationId, new Predicate<SpeciesBatch>() { - - @Override - public boolean apply(SpeciesBatch input) { - return input.getParentBatch() == null; - } - }); - return result; - } - - private SpeciesBatch getSpeciesBatch(String id) { - SpeciesBatch result = getBean(SpeciesBatch.class.getName(), id); - return result; - } - - @Override - public SpeciesBatch createSpeciesBatch(SpeciesBatch bean, - String parentBatchId) { - - boolean withParent = parentBatchId != null; - - SpeciesBatch result = create(SpeciesBatch.class.getName(), bean, !withParent); - - if (withParent) { - - // link to parent - SpeciesBatch parentBatch = getSpeciesBatch(parentBatchId); - result.setParentBatch(parentBatch); - - // add to parent childs - List<SpeciesBatch> childBatchs = parentBatch.getChildBatchs(); - if (childBatchs == null) { - childBatchs = Lists.newArrayList(); - parentBatch.setChildBatchs(childBatchs); - } - childBatchs.add(result); - - // now persist species batches - persistToFile(SpeciesBatch.class.getName()); - } - return result; - } - - @Override - public SpeciesBatch saveSpeciesBatch(SpeciesBatch bean) { - SpeciesBatch result = save(SpeciesBatch.class.getName(), bean); - return result; - } - - @Override - public void deleteSpeciesBatch(String id) { - remove(SpeciesBatch.class.getName(), id); - } - - @Override - public void deleteSpeciesSubBatch(String id) { - Preconditions.checkNotNull(id, "Can't save a bean with null id"); - SpeciesBatch bean = getBean(SpeciesBatch.class.getName(), id); - Preconditions.checkNotNull( - bean, "SpeciesBatch with id: " + id + " does not exist"); - - Set<SpeciesBatch> collected = Sets.newHashSet(); - collectChilds(bean, collected); - for (SpeciesBatch toDelete : collected) { - cache.remove(SpeciesBatch.class, toDelete); - } - bean.setChildBatchs(null); - persistToFile(SpeciesBatch.class.getName()); - } - - @Override - public void changeSpeciesBatchSpecies(String batchId, Species species) { - throw new IllegalStateException("method 'changeSpeciesBatchSpecies' not implemented"); - } - - protected void collectChilds(SpeciesBatch bean, Set<SpeciesBatch> collected) { - if (!bean.isChildBatchsEmpty()) { - for (SpeciesBatch batch : bean.getChildBatchs()) { - collected.add(batch); - collectChilds(batch, collected); - } - } - } - - @Override - public List<SpeciesBatchFrequency> getAllSpeciesBatchFrequency(final String speciesBatchId) { - List<SpeciesBatchFrequency> result = getFilteredDataList(SpeciesBatchFrequency.class.getName(), new Predicate<SpeciesBatchFrequency>() { - @Override - public boolean apply(SpeciesBatchFrequency input) { - return speciesBatchId.equals(input.getBatch().getId()); - } - }); - return result; - } - - @Override - public List<SpeciesBatchFrequency> saveSpeciesBatchFrequency(String speciesBatchId, - List<SpeciesBatchFrequency> frequencies) { - - // get existing data - List<SpeciesBatchFrequency> existingData = - getAllSpeciesBatchFrequency(speciesBatchId); - - List<SpeciesBatchFrequency> result = - persistList(SpeciesBatchFrequency.class.getName(), - existingData, - frequencies); - return result; - } - - //------------------------------------------------------------------------// - //-- Benthos Batch methods --// - //------------------------------------------------------------------------// - - @Override - public List<BenthosBatch> getAllBenthosBatch(String fishingOperationId) { - List<BenthosBatch> result = getAllTraitFilterBatches( - BenthosBatch.class.getName(), fishingOperationId, null); - return result; - } - - @Override - public BenthosBatch getBenthosBatch(String id) { - BenthosBatch result = getBean(BenthosBatch.class.getName(), id); - return result; - } - - @Override - public BenthosBatch createBenthosBatch(BenthosBatch bean) { - BenthosBatch result = create(BenthosBatch.class.getName(), bean); - return result; - } - - @Override - public BenthosBatch saveBenthosBatch(BenthosBatch bean) { - BenthosBatch result = save(BenthosBatch.class.getName(), bean); - return result; - } - - @Override - public void deleteBenthosBatch(String id) { - remove(BenthosBatch.class.getName(), id); - } - - //------------------------------------------------------------------------// - //-- Plancton Batch methods --// - //------------------------------------------------------------------------// - - @Override - public List<PlanktonBatch> getAllPlanktonBatch(String fishingOperationId) { - List<PlanktonBatch> result = getAllTraitFilterBatches( - PlanktonBatch.class.getName(), fishingOperationId, null); - return result; - } - - @Override - public PlanktonBatch getPlanktonBatch(String id) { - PlanktonBatch result = getBean(PlanktonBatch.class.getName(), id); - return result; - } - - @Override - public PlanktonBatch createPlanktonBatch(PlanktonBatch bean) { - PlanktonBatch result = create(PlanktonBatch.class.getName(), bean); - return result; - } - - @Override - public PlanktonBatch savePlanktonBatch(PlanktonBatch bean) { - PlanktonBatch result = save(PlanktonBatch.class.getName(), bean); - return result; - } - - @Override - public void deletePlanktonBatch(String id) { - remove(PlanktonBatch.class.getName(), id); - } - - //------------------------------------------------------------------------// - //-- Macrodechet Batch methods --// - //------------------------------------------------------------------------// - - @Override - public List<MacroWasteBatch> getAllMacroWasteBatch(String fishingOperationId) { - List<MacroWasteBatch> result = getAllTraitFilterBatches( - MacroWasteBatch.class.getName(), fishingOperationId, null); - return result; - } - - @Override - public MacroWasteBatch getMacroWasteBatch(String id) { - MacroWasteBatch result = getBean(MacroWasteBatch.class.getName(), id); - return result; - } - - @Override - public MacroWasteBatch createMacroWasteBatch(MacroWasteBatch bean) { - MacroWasteBatch result = create(MacroWasteBatch.class.getName(), bean); - return result; - } - - @Override - public MacroWasteBatch saveMacroWasteBatch(MacroWasteBatch bean) { - MacroWasteBatch result = save(MacroWasteBatch.class.getName(), bean); - return result; - } - - @Override - public void deleteMacroWasteBatch(String id) { - remove(MacroWasteBatch.class.getName(), id); - } - - //------------------------------------------------------------------------// - //-- Accidental Batch methods --// - //------------------------------------------------------------------------// - - @Override - public List<AccidentalBatch> getAllAccidentalBatch(String fishingOperationId) { - List<AccidentalBatch> result = getAllTraitFilterBatches( - AccidentalBatch.class.getName(), fishingOperationId, null); - return result; - } - - @Override - public AccidentalBatch getAccidentalBatch(String id) { - AccidentalBatch result = getBean(AccidentalBatch.class.getName(), id); - return result; - } - - @Override - public AccidentalBatch createAccidentalBatch(AccidentalBatch bean) { - AccidentalBatch result = create(AccidentalBatch.class.getName(), bean); - return result; - } - - @Override - public AccidentalBatch saveAccidentalBatch(AccidentalBatch bean) { - AccidentalBatch result = save(AccidentalBatch.class.getName(), bean); - return result; - } - - @Override - public void deleteAccidentalBatch(String id) { - remove(AccidentalBatch.class.getName(), id); - } - - //------------------------------------------------------------------------// - //-- Internal methods --// - //------------------------------------------------------------------------// - - protected <B> Class<B> getType(String type) { - String typeName = type; - int i = type.indexOf('#'); - if (i > -1) { - typeName = type.substring(0, i); - } - try { - Class<B> result = (Class<B>) Class.forName(typeName); - return result; - } catch (Exception e) { - throw new RuntimeException("No class named " + typeName, e); - } - } - - protected <B extends IdAware> Binder<B, B> getBinder(String type) { - - Binder<B, B> result = (Binder<B, B>) binderCache.get(type); - if (result == null) { - - Class<B> binderType = getType(type); - - result = BinderFactory.newBinder(binderType); - binderCache.put(type, result); - } - return result; - } - - protected <B extends IdAware> void persist(String beanType, - B source, - B target, - boolean synchFile) { - - getBinder(beanType).copyExcluding(source, target, AbstractTuttiEntity.PROPERTY_ID); - if (source.getId() == null) { - target.setId(UUID.randomUUID().toString()); - } - if (!cache.containsEntry(beanType, target)) { - cache.put(beanType, target); - } - if (synchFile) { - persistToFile(beanType); - } - } - - protected <B extends IdAware> List<B> persistList(String beanType, - List<B> existingData, - List<B> newData) { - - Map<B, B> toPersist = Maps.newHashMap(); - - // mark all old entry to be dettach - TuttiEntities.fillEntries(toPersist, - existingData, - TuttiEntities.<B, B>newConstantFunction(null)); - - // index them by id - Map<String, B> existingById = TuttiEntities.splitById(existingData); - - for (B source : newData) { - - B target; - - if (TuttiEntities.isNew(source)) { - - // must create it - Class<B> type = getType(beanType); - target = TuttiEntities.newEntity(type); - } else { - - // get existing data - target = existingById.get(source.getId()); - } - - // store it to be persist in one time - toPersist.put(target, source); - } - - // persist (entry with a value) and dettach (entry with no value) - - List<B> result = Lists.newArrayList(); - - for (Map.Entry<B, B> entry : toPersist.entrySet()) { - B source = entry.getValue(); - B target = entry.getKey(); - if (source == null) { - - // dettach entity - cache.remove(beanType, target); - } else { - - // persist entity - persist(beanType, source, target, false); - - // store it back in result - result.add(target); - } - } - persistToFile(beanType); - - return result; - } - - protected <B extends IdAware> List<B> loadEntities(String entityType, - TuttiPersistenceDevFixtures fix) throws IOException { - File storageFile = getStorageFile(entityType); - - List<B> result; - - if (storageFile.exists()) { - if (log.isDebugEnabled()) { - log.debug("Load data from file: " + storageFile); - } - - ObjectInputStream oos = - new ObjectInputStream(new FileInputStream(storageFile)); - try { - - result = (List<B>) oos.readObject(); - oos.close(); - } catch (ClassNotFoundException e) { - throw new RuntimeException("Could not find a class ", e); - } finally { - IOUtils.closeQuietly(oos); - } - - cache.putAll(entityType, result); - } else { - - result = fix.getData(entityType); - - cache.putAll(entityType, result); - - // persist it right now - persistToFile(entityType); - } - - return result; - } - - public static String getKey(Class<?> type, String context) { - String key = type.getName(); - if (context != null) { - key += '#' + context; - } - return key; - } - - protected <B extends IdAware> List<B> loadEntities(String entityType, - List<B> defaultList) throws IOException { - File storageFile = getStorageFile(entityType); - - List<B> result; - - if (storageFile.exists()) { - if (log.isDebugEnabled()) { - log.debug("Load data from file: " + storageFile); - } - - ObjectInputStream oos = - new ObjectInputStream(new FileInputStream(storageFile)); - try { - - result = (List<B>) oos.readObject(); - oos.close(); - } catch (ClassNotFoundException e) { - throw new RuntimeException("Could not find a class ", e); - } finally { - IOUtils.closeQuietly(oos); - } - - } else { - result = defaultList; - - // persist it right now - persistToFile(entityType); - } - - cache.putAll(entityType, result); - return result; - } - - protected <B> File getStorageFile(String entityType) { - File result = new File(storageDirectory, entityType + ".ser"); - return result; - } - - public <B extends IdAware> void persistToFile(String entityType) { - - List<B> entities = getDataInNewList(entityType); - File storageFile = getStorageFile(entityType); - - if (log.isDebugEnabled()) { - log.debug("Persist storage to file: " + storageFile); - } - ObjectOutputStream oos = null; - try { - oos = new ObjectOutputStream(new FileOutputStream(storageFile)); - oos.writeObject(entities); - - oos.close(); - } catch (IOException e) { - throw new RuntimeException( - "Could not persist to file: " + storageFile, e); - } finally { - if (oos != null) { - IOUtils.closeQuietly(oos); - } - } - } - - protected <B extends IdAware> List<B> getData(String entityType) { - List<B> result = (List<B>) cache.get(entityType); - return result; - } - - protected <B extends IdAware> List<B> getDataInNewList(String entityType) { - List<B> result = getData(entityType); - return Lists.newArrayList(result); - } - - protected <B extends IdAware, E> List<E> getTransformedDataList(String entityType, Function<B, E> function) { - List<B> result = getData(entityType); - return Lists.newArrayList(Iterables.transform(result, function)); - } - - protected <B extends IdAware> List<B> getFilteredDataList(String entityType, Predicate<B> predicate) { - List<B> result = getData(entityType); - return Lists.newArrayList(Iterables.filter(result, predicate)); - } - - protected <B extends FishingOperationAware> List<B> getAllTraitFilterBatches( - String beanType, - String traitId, - Predicate<B> extraPredicate) { - Predicate<B> predicate = TuttiEntities.newTraitIdPredicate(traitId); - if (extraPredicate != null) { - predicate = Predicates.and(predicate, extraPredicate); - } - List<B> result = getFilteredDataList(beanType, predicate); - return result; - } - - protected <B extends IdAware> B create(String type, B bean) { - return create(type, bean, true); - } - - public <B extends IdAware> B create(String type, - B bean, - boolean sychronize) { - Preconditions.checkNotNull(bean, "Can't persist a null bean"); - B result = TuttiEntities.newEntity(bean); - persist(type, bean, result, sychronize); - return result; - } - - protected <B extends IdAware> B save(String type, B bean) { - Preconditions.checkNotNull(bean, "Can't persist a null bean"); - String id = bean.getId(); - Preconditions.checkNotNull(id, "Can't save a bean with null id"); - B result = getBean(type, id); - Preconditions.checkNotNull( - result, "Can't save a not persisted bean with id: " + id); - persist(type, bean, result, true); - return result; - } - - protected <B extends IdAware> void remove(String type, String id) { - Preconditions.checkNotNull(id, "Can't delete a null id"); - B bean = getBean(type, id); - //FIXME tchemit Should never happen! - if (bean == null) { - if (log.isWarnEnabled()) { - log.warn("No bean with id: " + id); - } - } -// Preconditions.checkNotNull(bean, "Can't delete a null bean"); - cache.remove(type, bean); - persistToFile(type); - } - - protected <B extends IdAware> B getBean(String type, String id) { - List<B> data = getData(type); - B result = TuttiEntities.findById(data, id); - return result; - } - - protected List<Caracteristic> getAllCaracteristic(CaracteristicEnum categoryId) { - List<Caracteristic> result = getDataInNewList(getKey(Caracteristic.class, categoryId.name())); - return result; - } - - protected Caracteristic getCaracteristic(CaracteristicEnum categoryId) { - List<Caracteristic> data = getData(getKey(Caracteristic.class, categoryId.name())); - Caracteristic result = data.get(0); - return result; - } - - /** - * TODO - * - * @author tchemit <chemit@codelutin.com> - * @since 0.1 - */ - public static class TuttiPersistenceDevFixtures { - - protected final ArrayListMultimap<String, IdAware> cache; - - protected void putInCache(Class<?> type, IdAware b) { - putInCache(type, null, b); - } - - protected void putInCache(Class<?> type, String context, IdAware b) { - String key = getKey(type, context); - cache.put(key, b); - } - - public TuttiPersistenceDevFixtures() { - - // inject default datas (only referential) - - cache = ArrayListMultimap.create(); - - Zone z; - z = new Zone(); - z.setId(UUID.randomUUID().toString()); - z.setName("zone1"); - putInCache(Zone.class, z); - - FishingOperationLocation l; - FishingOperationLocation sl; - FishingOperationLocation ssl; - - l = new FishingOperationLocation(); - l.setId(UUID.randomUUID().toString()); - l.setName("zone1 - strata1"); - l.setZone(z); - putInCache(FishingOperationLocation.class, "Strata", l); - - sl = new FishingOperationLocation(); - sl.setId(UUID.randomUUID().toString()); - sl.setName("strat1 - substrata1"); - sl.setParent(l); - putInCache(FishingOperationLocation.class, "SubStrata", sl); - - ssl = new FishingOperationLocation(); - ssl.setId(UUID.randomUUID().toString()); - ssl.setName("substrata1 - localite1"); - ssl.setLocalite(true); - ssl.setParent(sl); - putInCache(FishingOperationLocation.class, "Localite", ssl); - - ssl = new FishingOperationLocation(); - ssl.setId(UUID.randomUUID().toString()); - ssl.setName("substrata1 - localite2"); - ssl.setLocalite(true); - ssl.setParent(sl); - putInCache(FishingOperationLocation.class, "Localite", ssl); - - sl = new FishingOperationLocation(); - sl.setId(UUID.randomUUID().toString()); - sl.setName("strat1 - substrata2"); - sl.setParent(l); - putInCache(FishingOperationLocation.class, "SubStrata", sl); - - ssl = new FishingOperationLocation(); - ssl.setId(UUID.randomUUID().toString()); - ssl.setName("substrata2 - localite1"); - ssl.setLocalite(true); - ssl.setParent(sl); - putInCache(FishingOperationLocation.class, "Localite", ssl); - - ssl = new FishingOperationLocation(); - ssl.setId(UUID.randomUUID().toString()); - ssl.setName("substrata2 - localite2"); - ssl.setLocalite(true); - ssl.setParent(sl); - putInCache(FishingOperationLocation.class, "Localite", ssl); - - l = new FishingOperationLocation(); - l.setId(UUID.randomUUID().toString()); - l.setName("zone1 - strata2"); - l.setZone(z); - putInCache(FishingOperationLocation.class, "Strata", l); - - ssl = new FishingOperationLocation(); - ssl.setId(UUID.randomUUID().toString()); - ssl.setName("strata2 - localite1"); - ssl.setParent(l); - ssl.setLocalite(true); - putInCache(FishingOperationLocation.class, "Localite", ssl); - - ssl = new FishingOperationLocation(); - ssl.setId(UUID.randomUUID().toString()); - ssl.setName("strata2 - localite2"); - ssl.setParent(l); - ssl.setLocalite(true); - putInCache(FishingOperationLocation.class, "Localite", ssl); - - z = new Zone(); - z.setId(UUID.randomUUID().toString()); - z.setName("zone2"); - putInCache(Zone.class, z); - - l = new FishingOperationLocation(); - l.setId(UUID.randomUUID().toString()); - l.setName("zone2 - strata1"); - l.setZone(z); - putInCache(FishingOperationLocation.class, "Strata", l); - - l = new FishingOperationLocation(); - l.setId(UUID.randomUUID().toString()); - l.setName("zone2 - strata2"); - l.setZone(z); - putInCache(FishingOperationLocation.class, "Strata", l); - - Country c; - c = new Country(); - c.setId(UUID.randomUUID().toString()); - c.setName("France"); - putInCache(Country.class, c); - - c = new Country(); - c.setId(UUID.randomUUID().toString()); - c.setName("Espagne"); - putInCache(Country.class, c); - - Vessel v; - - v = new Vessel(); - v.setId(UUID.randomUUID().toString()); - v.setName("THALASSA"); - v.setScientificVessel(true); - v.setRegistrationCode("RegistrationCode1"); - putInCache(Vessel.class, "Scientific", v); - - v = new Vessel(); - v.setId(UUID.randomUUID().toString()); - v.setName("THALASSA-II"); - v.setScientificVessel(true); - v.setRegistrationCode("RegistrationCode2"); - putInCache(Vessel.class, "Scientific", v); - - v = new Vessel(); - v.setId(UUID.randomUUID().toString()); - v.setName("Fishing1"); - v.setScientificVessel(false); - v.setRegistrationCode("RegistrationCode1"); - putInCache(Vessel.class, "Fishing", v); - - v = new Vessel(); - v.setId(UUID.randomUUID().toString()); - v.setName("Fishing2"); - v.setScientificVessel(false); - v.setRegistrationCode("RegistrationCode1"); - putInCache(Vessel.class, "Fishing", v); - - - Gear g; - - g = new Gear(); - g.setId(UUID.randomUUID().toString()); - g.setName("Gear-Scientific-1"); - putInCache(Gear.class, "Scientific", g); - - g = new Gear(); - g.setId(UUID.randomUUID().toString()); - g.setName("Gear-Scientific-2"); - putInCache(Gear.class, "Scientific", g); - - g = new Gear(); - g.setId(UUID.randomUUID().toString()); - g.setName("Gear-Fishing-1"); - putInCache(Gear.class, "Fishing", g); - - g = new Gear(); - g.setId(UUID.randomUUID().toString()); - g.setName("Gear-Fishing-2"); - putInCache(Gear.class, "Fishing", g); - - Person u; - - u = new Person(); - u.setId(UUID.randomUUID().toString()); - u.setName("Vincent Badts"); - u.setFirstName("Vincent"); - u.setLastName("Badts"); - putInCache(Person.class, u); - - u = new Person(); - u.setId(UUID.randomUUID().toString()); - u.setName("Jean claude Mahet"); - u.setFirstName("Jean claude"); - u.setLastName("Mahet"); - putInCache(Person.class, u); - - u = new Person(); - u.setId(UUID.randomUUID().toString()); - u.setName("Luisa Metral"); - u.setFirstName("Luisa"); - u.setLastName("Metral"); - putInCache(Person.class, u); - - u = new Person(); - u.setId(UUID.randomUUID().toString()); - u.setName("Jean Hervé Bourdeix"); - u.setFirstName("Jean Hervé"); - u.setLastName("Bourdeix"); - putInCache(Person.class, u); - - u = new Person(); - u.setId(UUID.randomUUID().toString()); - u.setName("Ysabelle Cheret"); - u.setFirstName("Ysabelle"); - u.setLastName("Cheret"); - putInCache(Person.class, u); - - u = new Person(); - u.setId(UUID.randomUUID().toString()); - u.setName("Blandine Brisset"); - u.setFirstName("Blandine"); - u.setLastName("Brisset"); - putInCache(Person.class, u); - - Caracteristic ca; - - Species sp; - - sp = new Species(); - sp.setId(UUID.randomUUID().toString()); - sp.setRefTaxCode("BAR"); - sp.setName("Bar"); - putInCache(Species.class, sp); - - sp = new Species(); - sp.setId(UUID.randomUUID().toString()); - sp.setRefTaxCode("CHIN"); - sp.setName("Chinchard"); - putInCache(Species.class, sp); - - sp = new Species(); - sp.setId(UUID.randomUUID().toString()); - sp.setRefTaxCode("FLE"); - sp.setName("Flet"); - putInCache(Species.class, sp); - - sp = new Species(); - sp.setId(UUID.randomUUID().toString()); - sp.setRefTaxCode("LIM"); - sp.setName("Limande"); - putInCache(Species.class, sp); - - ca = createQualitativeCaracteristic("MacroWaste", CaracteristicEnum.MacroWasteCategory.name()); - - createCaracteristicQualitativeValue(ca, "Bottle"); - createCaracteristicQualitativeValue(ca, "Sheet"); - createCaracteristicQualitativeValue(ca, "Bag"); - createCaracteristicQualitativeValue(ca, "Caps/ lids"); - createCaracteristicQualitativeValue(ca, "Fishing line (monofilament)"); - createCaracteristicQualitativeValue(ca, "Fishing line (entangled)"); - createCaracteristicQualitativeValue(ca, "Synthetic rope"); - createCaracteristicQualitativeValue(ca, "Cable ties"); - createCaracteristicQualitativeValue(ca, "Strapping band"); - createCaracteristicQualitativeValue(ca, "Crates abd containers"); - createCaracteristicQualitativeValue(ca, "Boots"); - createCaracteristicQualitativeValue(ca, "Balloons"); - createCaracteristicQualitativeValue(ca, "bobbins (fishing)"); - createCaracteristicQualitativeValue(ca, "tyre"); - createCaracteristicQualitativeValue(ca, "glove"); - createCaracteristicQualitativeValue(ca, "diapers"); - createCaracteristicQualitativeValue(ca, "cotton buds"); - createCaracteristicQualitativeValue(ca, "cigarette butts"); - createCaracteristicQualitativeValue(ca, "condoms"); - createCaracteristicQualitativeValue(ca, "syringes"); - createCaracteristicQualitativeValue(ca, "sanitary towels / tampon"); - createCaracteristicQualitativeValue(ca, "jar"); - createCaracteristicQualitativeValue(ca, "piece"); - - ca = createQualitativeCaracteristic("MacroWaste Size category", - CaracteristicEnum.MacroWasteSizeCategory.name()); - createCaracteristicQualitativeValue(ca, "<5*5cm = 25cm²"); - createCaracteristicQualitativeValue(ca, "<10*10cm = 100cm²"); - createCaracteristicQualitativeValue(ca, "<20*20cm = 400cm²"); - createCaracteristicQualitativeValue(ca, "<50*50cm = 2500cm²"); - createCaracteristicQualitativeValue(ca, "<100*100cm = 1m²"); - createCaracteristicQualitativeValue(ca, ">100*100cm = 1m²"); - - ca = createQualitativeCaracteristic("Sorted / Unsorted Category", CaracteristicEnum.SortedUnsortedCategory.name()); - - createCaracteristicQualitativeValue(ca, "Vrac"); - createCaracteristicQualitativeValue(ca, "Hors Vrac"); - - ca = createQualitativeCaracteristic("Species Sex Category", CaracteristicEnum.SexCategory.name()); - - createCaracteristicQualitativeValue(ca, "Male"); - createCaracteristicQualitativeValue(ca, "Femelle"); - createCaracteristicQualitativeValue(ca, "Indéterminé"); - - ca = createQualitativeCaracteristic("Species Maturity Category", CaracteristicEnum.MaturityCategory.name()); - - createCaracteristicQualitativeValue(ca, "0"); - createCaracteristicQualitativeValue(ca, "1"); - createCaracteristicQualitativeValue(ca, "2"); - - ca = createQualitativeCaracteristic("Species Size Category", CaracteristicEnum.SizeCategory.name()); - - createCaracteristicQualitativeValue(ca, "Petit"); - createCaracteristicQualitativeValue(ca, "Gros"); - } - - protected void createCaracteristicQualitativeValue(Caracteristic ca, String name) { - CaracteristicQualitativeValue cqv = new CaracteristicQualitativeValue(); - cqv.setId(UUID.randomUUID().toString()); - cqv.setName(name); - ca.addQualitativeValue(cqv); - putInCache(CaracteristicQualitativeValue.class, name, cqv); - } - - protected Caracteristic createQualitativeCaracteristic(String name, String category) { - Caracteristic ca = new Caracteristic(); - ca.setId(UUID.randomUUID().toString()); - ca.setName(name); - ca.setCategory(category); - ca.setCaracteristicType(CaracteristicType.QUALITATIVE); - ca.setQualitativeValue(Lists.<CaracteristicQualitativeValue>newArrayList()); - putInCache(Caracteristic.class, category, ca); - return ca; - } - - protected Caracteristic createNumberCaracteristic(String name, String category) { - Caracteristic ca = new Caracteristic(); - ca.setId(UUID.randomUUID().toString()); - ca.setName(name); - ca.setCategory(category); - ca.setCaracteristicType(CaracteristicType.NUMBER); - putInCache(Caracteristic.class, category, ca); - return ca; - } - - public <B extends IdAware> List<B> getData(String entityType) { - List<B> result = (List<B>) cache.get(entityType); - return result; - } - } -} Modified: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceReadTest.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceReadTest.java 2013-02-25 14:11:54 UTC (rev 461) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceReadTest.java 2013-02-25 14:30:44 UTC (rev 462) @@ -24,11 +24,9 @@ * #L% */ -import com.google.common.base.Preconditions; import fr.ifremer.adagio.core.service.technical.CacheService; import fr.ifremer.tutti.persistence.DatabaseFixtures; import fr.ifremer.tutti.persistence.DatabaseResource; -import fr.ifremer.tutti.persistence.TuttiPersistenceDevImpl; import fr.ifremer.tutti.persistence.entities.IdAware; import fr.ifremer.tutti.persistence.entities.TuttiEntities; import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; @@ -42,20 +40,16 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.NonUniqueResultException; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Ignore; import org.junit.Test; -import org.nuiton.util.ApplicationConfig; import java.io.IOException; import java.util.List; -import static fr.ifremer.tutti.persistence.TuttiPersistenceDevImpl.CaracteristicEnum; - /** * To test {@link ReferentialPersistenceService} for read operation. * @@ -71,8 +65,6 @@ private static final Log log = LogFactory.getLog(ReferentialPersistenceServiceReadTest.class); - protected static TuttiPersistenceDevImpl storage; - protected ReferentialPersistenceService service; protected CacheService cacheService; @@ -82,43 +74,9 @@ @BeforeClass public static void beforeClass() throws IOException { - // create a storage - - ApplicationConfig applicationConfig = dbResource.getConfig().getConfig(); - - applicationConfig.setOption( - TuttiPersistenceDevImpl.SKIP_FIXTURES_OPTION, Boolean.TRUE.toString()); - - storage = new TuttiPersistenceDevImpl(applicationConfig) { - - @Override - public <B extends IdAware> B create(String type, - B bean, - boolean sychronize) { - Preconditions.checkNotNull(bean, "Can't persist a null bean"); - B result = TuttiEntities.newEntity(bean); - result.setId(bean.getId()); - if (log.isDebugEnabled()) { - log.debug("Will persist [" + type + ":" + bean.getId() + "]"); - } - super.persist(type, bean, result, sychronize); - return result; - } - }; - - if (log.isInfoEnabled()) { - log.info("Will init persistence driver " + storage.getImplementationName()); - } - - storage.init(); - TuttiPersistenceServiceLocator.instance().getCacheService().clearAllCaches(); } - @AfterClass - public static void afterClass() throws IOException { - storage.close(); - } @Before public void setUp() throws Exception { @@ -134,18 +92,12 @@ public void getAllProgramZone() { List<Zone> result = service.getAllProgramZone(); assertResultList(result, fixtures.refNbProgramZone()); - - persistList(Zone.class, result); - assertSize(result, storage.getAllProgramZone()); } @Test public void getAllCountry() { List<Country> result = service.getAllCountry(); assertResultList(result, fixtures.refNbCountry()); - - persistList(Country.class, result); - assertSize(result, storage.getAllCountry()); } @Test @@ -154,9 +106,6 @@ List<FishingOperationLocation> result = service.getAllFishingOperationStrata(zoneId); assertResultList(result, fixtures.refNbStrata()); - - persistList(FishingOperationLocation.class, "Strata", result); - assertSize(result, storage.getAllFishingOperationStrata(zoneId)); } @Test @@ -166,9 +115,6 @@ service.getAllFishingOperationSubStrata(zoneId, null); assertResultList(result, 0); - persistList(FishingOperationLocation.class, "SubStrata", result); - assertSize(result, storage.getAllFishingOperationSubStrata(zoneId, null)); - // try with a strataId String strataId = dbResource.getFixtures().strataId(); result = service.getAllFishingOperationLocation(zoneId, strataId, null); @@ -182,9 +128,6 @@ service.getAllFishingOperationLocation(zoneId, null, null); assertResultList(result, fixtures.refNbLocalite()); - persistList(FishingOperationLocation.class, "Localite", result); - assertSize(result, storage.getAllFishingOperationLocation(zoneId, null, null)); - // try with a strataId String strataId = dbResource.getFixtures().strataId(); result = service.getAllFishingOperationLocation(zoneId, strataId, null); @@ -200,8 +143,6 @@ public void getAllScientificVessel() { List<Vessel> result = service.getAllScientificVessel(); assertResultList(result, fixtures.refNbScientificVessel()); - persistList(Vessel.class, "Scientific", result); - assertSize(result, storage.getAllScientificVessel()); } @Test @@ -226,45 +167,30 @@ //TODO-tc We should not test cache performance like this, depends to much of the computer usage,... // float reduceFactor = delta2 * 100 / delta1; // Assert.assertTrue("The cache on getAllFishingVessel() should have speed up more than factor 10. Make sure EhCache is well configured.", reduceFactor < 0.1); - - persistList(Vessel.class, "Fishing", result); - assertSize(result, storage.getAllFishingVessel()); } @Test public void getAllScientificGear() { List<Gear> result = service.getAllScientificGear(); assertResultList(result, fixtures.refNbScientificGear()); - - persistList(Gear.class, "Scientific", result); - assertSize(result, storage.getAllScientificGear()); } @Test public void getAllFishingGear() { List<Gear> result = service.getAllFishingGear(); assertResultList(result, fixtures.refNbFishingGear()); - - persistList(Gear.class, "Fishing", result); - assertSize(result, storage.getAllFishingGear()); } @Test public void getAllPerson() { List<Person> result = service.getAllPerson(); assertResultList(result, fixtures.refNbPerson()); - - persistList(Person.class, result); - assertSize(result, storage.getAllPerson()); } @Test public void getAllSpecies() { List<Species> result = service.getAllSpecies(); assertResultList(result, fixtures.refNbSpecies()); - - persistList(Species.class, result); - assertSize(result, storage.getAllSpecies()); } @Test @@ -301,51 +227,42 @@ public void getAllCaracteristic() { List<Caracteristic> result = service.getAllCaracteristic(); assertResultList(result, fixtures.refNbCaracteristic()); - - persistList(Caracteristic.class, CaracteristicEnum.All.name(), result); - assertSize(result, storage.getAllCaracteristic()); } @Test public void getSizeCategoryCaracteristic() { Caracteristic result = service.getSizeCategoryCaracteristic(); assertCaracteristicQualitative(result, 6); - persist(Caracteristic.class, CaracteristicEnum.SizeCategory.name(), result); } @Test public void getSexCaracteristic() { Caracteristic result = service.getSexCaracteristic(); assertCaracteristicQualitative(result, 4); - persist(Caracteristic.class, CaracteristicEnum.SexCategory.name(), result); } @Test public void getSortedUnsortedCaracteristic() { Caracteristic result = service.getSortedUnsortedCaracteristic(); assertCaracteristicQualitative(result, 2); - persist(Caracteristic.class, CaracteristicEnum.SortedUnsortedCategory.name(), result); } @Test public void getMaturityCaracteristic() { Caracteristic result = service.getMaturityCaracteristic(); assertCaracteristicQualitative(result, 6); - persist(Caracteristic.class, CaracteristicEnum.MaturityCategory.name(), result); } @Test public void getMacroWasteCategoryCaracteristic() { Caracteristic result = service.getMacroWasteCategoryCaracteristic(); assertCaracteristicQualitative(result, 21); - persist(Caracteristic.class, CaracteristicEnum.MacroWasteCategory.name(), result); } @Test public void getMacroWasteSizeCategoryCaracteristic() { Caracteristic result = service.getMacroWasteSizeCategoryCaracteristic(); assertCaracteristicQualitative(result, 6); - persist(Caracteristic.class, CaracteristicEnum.MacroWasteSizeCategory.name(), result); } @Test @@ -363,28 +280,6 @@ System.out.println(obj); } - protected <S extends IdAware> void persistList(Class<S> type, List<S> result) { - persistList(type, null, result); - } - - protected <S extends IdAware> void persistList(Class<S> type, String context, List<S> result) { - - String key = TuttiPersistenceDevImpl.getKey(type, context); - for (S s : result) { - storage.create(key, s, false); - } - storage.persistToFile(key); - } - - protected <S extends IdAware> void persist(Class<S> type, S result) { - persist(type, null, result); - } - - protected <S extends IdAware> void persist(Class<S> type, String context, S result) { - String key = TuttiPersistenceDevImpl.getKey(type, context); - storage.create(key, result, true); - } - protected void assertSize(List<?> expectedList, List<?> storageList) { Assert.assertNotNull(expectedList); Assert.assertNotNull(storageList);
participants (1)
-
tchemit@users.forge.codelutin.com