Author: blavenier Date: 2013-02-09 01:52:52 +0100 (Sat, 09 Feb 2013) New Revision: 377 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/377 Log: Add : - importTemporaryGear - importTemporaryPerson - importTemporaryVessel - importTemporarySpecies Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/TuttiEnumerationFile.java trunk/tutti-persistence/src/main/resources/applicationContext-service-tutti.xml trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceTest.java Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceImpl.java 2013-02-08 23:17:53 UTC (rev 376) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceImpl.java 2013-02-09 00:52:52 UTC (rev 377) @@ -24,9 +24,33 @@ * #L% */ +import java.util.Date; +import java.util.Iterator; +import java.util.List; + +import javax.annotation.Resource; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.Query; +import org.hibernate.type.DateType; +import org.hibernate.type.IntegerType; +import org.hibernate.type.StringType; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.CachePut; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.dao.DataRetrievalFailureException; +import org.springframework.stereotype.Service; + import com.google.common.base.Preconditions; import com.google.common.collect.Lists; + +import fr.ifremer.adagio.core.dao.administration.user.PersonExtendDao; +import fr.ifremer.adagio.core.dao.data.vessel.VesselExtendDao; import fr.ifremer.adagio.core.dao.referential.StatusDao; +import fr.ifremer.adagio.core.dao.referential.gear.FishingGearExtendDao; +import fr.ifremer.adagio.core.dao.referential.location.LocationExtendDao; import fr.ifremer.adagio.core.dao.referential.taxon.TaxonNameExtendDao; import fr.ifremer.adagio.core.dao.referential.taxon.TaxonRefTaxVO; import fr.ifremer.tutti.persistence.entities.TuttiEntities; @@ -43,22 +67,7 @@ import fr.ifremer.tutti.persistence.entities.referential.Status; import fr.ifremer.tutti.persistence.entities.referential.Vessel; import fr.ifremer.tutti.persistence.entities.referential.Zone; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.hibernate.Query; -import org.hibernate.type.DateType; -import org.hibernate.type.IntegerType; -import org.hibernate.type.StringType; -import org.springframework.cache.annotation.CacheEvict; -import org.springframework.cache.annotation.Cacheable; -import org.springframework.dao.DataRetrievalFailureException; -import org.springframework.stereotype.Service; -import javax.annotation.Resource; -import java.util.Date; -import java.util.Iterator; -import java.util.List; - /** * Implementation of the {@link ReferentialPersistenceService} using a adagio * data-source. @@ -78,8 +87,19 @@ @Resource(name = "statusDao") protected StatusDao statusDao; + + @Resource(name = "personDao") + protected PersonExtendDao personDao; + @Resource(name = "fishingGearDao") + protected FishingGearExtendDao fishingGearDao; + @Resource(name = "vesselExtendDao") + protected VesselExtendDao vesselExtendDao; + + @Resource(name = "locationDao") + protected LocationExtendDao locationDao; + //------------------------------------------------------------------------// //-- ReferentialPersistenceService implentation --// //------------------------------------------------------------------------// @@ -302,6 +322,9 @@ Object[] source = queryUnique( "person", "personId", IntegerType.INSTANCE, personId); + + if (source == null) return null; + Person result = loadPerson(source); return result; } @@ -311,6 +334,9 @@ Object[] source = queryUnique( "gear", "gearId", IntegerType.INSTANCE, gearId); + + if (source == null) return null; + Gear result = loadGear(source); return result; } @@ -328,10 +354,21 @@ TaxonRefTaxVO[] sources = taxonNameDao.getAllTaxonNames(true); List<Species> result = Lists.newArrayListWithCapacity(sources.length); + List<Species> referenceTaxonsOnly = Lists.newArrayList(); for (TaxonRefTaxVO source : sources) { Species target = loadSpecies(source); + if (target.isReferenceTaxon()) { + + // Add to cache : + putToSpeciesReferentCache(Integer.valueOf(target.getReferenceTaxonId()), target); + referenceTaxonsOnly.add(target); + } result.add(target); } + + // Add to cache : + putToSpeciesReferentCache(referenceTaxonsOnly); + return result; } @@ -347,13 +384,17 @@ continue; } Species target = loadSpecies(source); + + // Add to cache : + putToSpeciesReferentCache(Integer.valueOf(target.getReferenceTaxonId()), target); + result.add(target); } return result; } @Override - //TODO manage Cacheable(value = "species") + @Cacheable(value = "species", key="#speciesId") public Species getSpeciesByReferenceTaxonId(Integer speciesId) { try { TaxonRefTaxVO source = taxonNameDao.getTaxonNameReferent( @@ -379,8 +420,14 @@ // Skip some protected PSFM if (!isProtectedCaracteristic(pmfmId)) { Caracteristic target = loadCaracteristic(source); + putToCaracteristicCache(pmfmId, target); + result.add(target); } + // If protected, load anyway just for the cache + else { + putToCaracteristicCache(pmfmId, loadCaracteristic(source)); + } } return result; } @@ -437,7 +484,7 @@ } @Override - //TODO manage Cacheable(value = "pmfms") + @Cacheable(value = "pmfms", key="#pmfmId") public Caracteristic getCaracteristic(Integer pmfmId) { Object[] source = queryUniqueWithStatus("pmfmById", "pmfmId", IntegerType.INSTANCE, pmfmId, @@ -447,7 +494,7 @@ } @Override - @CacheEvict(value = {"species", "referentSpecies"}) + @CacheEvict(value = {"species", "referentSpecies"}, allEntries=true ) public List<Species> importTemporarySpecies(List<Species> species) { List<Species> result = Lists.newArrayList(); for (Species source : species) { @@ -458,44 +505,149 @@ } @Override - @CacheEvict(value = "fishingVessels") + @CacheEvict(value = "fishingVessels", allEntries=true ) public List<Vessel> importTemporaryVessel(List<Vessel> vessels) { - //TODO - return null; + List<Vessel> result = Lists.newArrayList(); + + fr.ifremer.adagio.core.dao.referential.Status status = + statusDao.load(enumeration.STATUS_TEMPORARY_CODE); + Integer countryLocationId = + locationDao.getLocationIdByLabelAndLocationLevel( + enumeration.LOCATION_LABEL_DEFAULT_COUNTRY, + new Integer[]{enumeration.LOCATION_LEVEL_ID_COUNTRY}); + if (countryLocationId == null) { + throw new DataIntegrityViolationException("Default country location not found, with label=" + enumeration.LOCATION_LABEL_DEFAULT_COUNTRY); + } + + for (Vessel source : vessels) { + source = importTemporaryVessel(source, countryLocationId, status); + result.add(source); + } + return result; } @Override - @CacheEvict(value = "persons") + @CacheEvict(value = "persons", allEntries=true ) public List<Person> importTemporaryPerson(List<Person> persons) { - //TODO - return null; + List<Person> result = Lists.newArrayList(); + + fr.ifremer.adagio.core.dao.referential.Status status = + statusDao.load(enumeration.STATUS_TEMPORARY_CODE); + for (Person source : persons) { + source = importTemporaryPerson(source, status); + result.add(source); + } + return result; } @Override - @CacheEvict(value = "gears") + @CacheEvict(value = "gears", allEntries=true ) public List<Gear> importTemporaryGear(List<Gear> gears) { - //TODO - return null; + List<Gear> result = Lists.newArrayList(); + + fr.ifremer.adagio.core.dao.referential.Status status = + statusDao.load(enumeration.STATUS_TEMPORARY_CODE); + for (Gear source : gears) { + source = importTemporaryGear(source, status); + result.add(source); + } + return result; } //------------------------------------------------------------------------// //-- Internal methods --// //------------------------------------------------------------------------// + public Vessel importTemporaryVessel(Vessel source, Integer registrationLocationId, fr.ifremer.adagio.core.dao.referential.Status status) { + Preconditions.checkNotNull(source); + Preconditions.checkNotNull(source.getName()); + Preconditions.checkNotNull(source.getInternationalRegistrationCode()); + + Integer vesselTypeId = null; + if (source.isScientificVessel()) { + vesselTypeId = enumeration.VESSEL_TYPE_ID_SCIENTIFIC; + } + else { + vesselTypeId = enumeration.VESSEL_TYPE_ID_FISHING; + } + + fr.ifremer.adagio.core.dao.referential.vessel.Vessel target = + vesselExtendDao.createAsTemporary( + null, + source.getInternationalRegistrationCode(), + registrationLocationId, source.getName(), + vesselTypeId); + + // Fill the result bean + Vessel result = new Vessel(); + result.setId(target.getCode()); + result.setName(source.getName()); + result.setRegistrationCode(source.getRegistrationCode()); + result.setInternationalRegistrationCode(source.getInternationalRegistrationCode()); + result.setScientificVessel(source.isScientificVessel()); + setStatus(status, result); + return result; + } + + public Person importTemporaryPerson(Person source, fr.ifremer.adagio.core.dao.referential.Status status) { + Preconditions.checkNotNull(source); + Preconditions.checkNotNull(source.getFirstName()); + Preconditions.checkNotNull(source.getLastName()); + + fr.ifremer.adagio.core.dao.administration.user.Person target = personDao.createAsTemporary(source.getLastName(), source.getFirstName(), enumeration.DEPARTMENT_ID_UNKNOWN_RECORDER_DEPARTMENT); + + // Fill the result bean + Person result = new Person(); + result.setId(target.getId().toString()); + result.setLastName(source.getLastName()); + result.setFirstName(source.getFirstName()); + setStatus(status, result); + return result; + } + protected Species importTemporarySpecies(Species source) { Preconditions.checkNotNull(source); Preconditions.checkNotNull(source.getName()); TaxonRefTaxVO taxonName = new TaxonRefTaxVO(); taxonName.setName(source.getName()); - taxonName = taxonNameDao.createAsTemporary(taxonName, "Added by tutti (file import)."); + taxonName = taxonNameDao.createAsTemporary( + taxonName, + "Added by tutti (file import)."); // update the source Species result = loadSpecies(taxonName); + + // Add to cache + putToSpeciesReferentCache(Integer.valueOf(result.getId()), result); return result; } + + public Gear importTemporaryGear(Gear source, fr.ifremer.adagio.core.dao.referential.Status status) { + Preconditions.checkNotNull(source); + Preconditions.checkNotNull(source.getLabel()); + Preconditions.checkNotNull(source.getName()); + Integer gearClassificationId = null; + if (source.isScientificGear()) { + gearClassificationId = enumeration.GEAR_CLASSIFICIATION_ID_SCIENTIFIC; + } + else { + gearClassificationId = enumeration.GEAR_CLASSIFICIATION_ID_FISHING; + } + + fr.ifremer.adagio.core.dao.referential.gear.Gear target = fishingGearDao.createAsTemporary(source.getLabel(), source.getName(), gearClassificationId); + + // Fill the result bean + Gear result = new Gear(); + result.setId(target.getId().toString()); + result.setLabel(source.getLabel()); + result.setName(source.getName()); + setStatus(status, result); + return result; + } + protected List<FishingOperationLocation> getFishingOperationLocations(Integer locationLevelId) { Iterator<Object[]> sources = queryListWithStatus( "allFishingOperationLocation", @@ -685,4 +837,19 @@ return sampleCategory; } + @Cacheable(value = "pmfms", key = "#pmfmId") + protected Caracteristic putToCaracteristicCache(Integer pmfmId, Caracteristic caracteristic) { + return caracteristic; + } + + @CachePut(value = "referentSpecies") + protected List<Species> putToSpeciesReferentCache(List<Species> species) { + return species; + } + + @CachePut(value = "referentSpecies", key = "#speciesId") + protected Species putToSpeciesReferentCache(Integer speciesId, Species species) { + return species; + } + } Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/TuttiEnumerationFile.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/TuttiEnumerationFile.java 2013-02-08 23:17:53 UTC (rev 376) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/TuttiEnumerationFile.java 2013-02-09 00:52:52 UTC (rev 377) @@ -47,6 +47,9 @@ @Value("${LocationClassificationId.SECTOR}") public final Integer LOCATION_CLASSIFICATION_ID_SECTOR = null; + @Value("${LocationLabel.FRANCE}") + public final String LOCATION_LABEL_DEFAULT_COUNTRY = null; + @Value("${LocationLevelId.PAYS_ISO3}") public final Integer LOCATION_LEVEL_ID_COUNTRY = null; Modified: trunk/tutti-persistence/src/main/resources/applicationContext-service-tutti.xml =================================================================== --- trunk/tutti-persistence/src/main/resources/applicationContext-service-tutti.xml 2013-02-08 23:17:53 UTC (rev 376) +++ trunk/tutti-persistence/src/main/resources/applicationContext-service-tutti.xml 2013-02-09 00:52:52 UTC (rev 377) @@ -73,10 +73,12 @@ <bean id="tuttiSpeciesCache" parent="tuttiAbstractEternalCache"> <property name="cacheName" value="species" /> + <property name="maxElementsInMemory" value="20000"/> </bean> <bean id="tuttiReferentSpeciesCache" parent="tuttiAbstractEternalCache"> - <property name="cacheName" value="referentSpecies" /> + <property name="cacheName" value="referentSpecies" /> + <property name="maxElementsInMemory" value="10000"/> </bean> <bean id="tuttiGearsCache" parent="tuttiAbstractEternalCache"> Modified: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceTest.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceTest.java 2013-02-08 23:17:53 UTC (rev 376) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceTest.java 2013-02-09 00:52:52 UTC (rev 377) @@ -398,7 +398,6 @@ Assert.assertEquals(createdSp2, service.getSpeciesByReferenceTaxonId(createdSp2.getReferenceTaxonId())); } - @Ignore @Test public void importVessel() { List<Vessel> vessels = Lists.newArrayList(); @@ -418,7 +417,7 @@ v2.setId("Don't care" + timestamp2); v2.setName("Name" + timestamp2); v2.setInternationalRegistrationCode("Immat" + timestamp2); - v1.setScientificVessel(false); + v2.setScientificVessel(false); vessels.add(v2); Assert.assertNull(service.getVessel(v1.getId())); @@ -450,11 +449,10 @@ Assert.assertNotNull(createdV2.getId()); Assert.assertNotSame(v2.getId(), createdV2.getId()); Assert.assertEquals(createdV2, service.getVessel(createdV2.getId())); - Assert.assertFalse(allScientificVessel.contains(createdV1)); - Assert.assertTrue(allFishingVessel.contains(createdV1)); + Assert.assertFalse(allScientificVessel.contains(createdV2)); + Assert.assertTrue(allFishingVessel.contains(createdV2)); } - @Ignore @Test public void importPerson() { List<Person> persons = Lists.newArrayList(); @@ -479,9 +477,6 @@ p2.setDepartment("Don't care" + timestamp2); persons.add(p2); - Assert.assertNull(service.getPerson(Integer.valueOf(p1.getId()))); - Assert.assertNull(service.getPerson(Integer.valueOf(p2.getId()))); - List<Person> personList = service.importTemporaryPerson(persons); Assert.assertNotNull(personList); @@ -517,7 +512,6 @@ Assert.assertEquals(createdP2, service.getPerson(Integer.valueOf(createdP2.getId()))); } - @Ignore @Test public void importGear() { List<Gear> gears = Lists.newArrayList(); @@ -527,9 +521,9 @@ Gear g1 = new Gear(); g1.setId("Don't care" + timestamp1); g1.setDescription("Don't care" + timestamp1); - g1.setScientificGear(true); g1.setName("Name" + timestamp1); g1.setLabel("Label" + timestamp1); + g1.setScientificGear(true); gears.add(g1); // fishing gear @@ -539,7 +533,7 @@ g2.setDescription("Don't care" + timestamp2); g2.setName("Name" + timestamp2); g2.setLabel("Label" + timestamp2); - g1.setScientificGear(false); + g2.setScientificGear(false); gears.add(g2); @@ -567,9 +561,24 @@ Assert.assertNotNull(createdG2.getId()); Assert.assertNotSame(g2.getId(), createdG2.getId()); Assert.assertEquals(createdG2, service.getGear(Integer.valueOf(createdG2.getId()))); - Assert.assertFalse(allScientificGear.contains(createdG1)); - Assert.assertTrue(allFishingGear.contains(createdG1)); + Assert.assertFalse(allScientificGear.contains(createdG2)); + Assert.assertTrue(allFishingGear.contains(createdG2)); } + + @Test + @Ignore + public void testCaches() { + cacheService.clearAllCaches(); + + Object obj = service.getAllCaracteristic(); + System.out.println(obj); + + obj = service.getCaracteristic(1429); + System.out.println(obj); + + obj = service.getCaracteristic(1428); + System.out.println(obj); + } protected <S extends IdAware> void persistList(Class<S> type, List<S> result) { persistList(type, null, result);