Author: tchemit Date: 2014-01-18 16:14:32 +0100 (Sat, 18 Jan 2014) New Revision: 1504 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/1504 Log: fixes #4149: [TECH] Mise ?\195?\160 jour de r?\195?\169f?\195?\169rentiel - Erreur sur les donn?\195?\169es suite ?\195?\160 la mise ?\195?\160 jour Added: trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/ trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/DatabaseSanityService.java trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/DatabaseSanityServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/ trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTask.java trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTaskVesselFeatures.java trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTaskVesselRegistrationPeriod.java trunk/tutti-persistence/src/main/resources/META-INF/services/fr.ifremer.adagio.core.service.technical.sanity.task.DatabaseSanityTask Removed: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/DatabaseCheckPersistenceService.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/DatabaseCheckPersistenceServiceImpl.java Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/TuttiPersistenceServiceLocator.java Added: trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/DatabaseSanityService.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/DatabaseSanityService.java (rev 0) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/DatabaseSanityService.java 2014-01-18 15:14:32 UTC (rev 1504) @@ -0,0 +1,41 @@ +package fr.ifremer.adagio.core.service.technical.sanity; + +/* + * #%L + * Tutti :: Persistence + * $Id$ + * $HeadURL:$ + * %% + * Copyright (C) 2012 - 2014 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 org.springframework.transaction.annotation.Transactional; + +/** + * To check that a base is sane and try to clean it if possible. + * <p/> + * Created on 1/18/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 3.0.1 + */ +@Transactional(readOnly = false) +public interface DatabaseSanityService { + + void sanity(); +} Property changes on: trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/DatabaseSanityService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/DatabaseSanityServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/DatabaseSanityServiceImpl.java (rev 0) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/DatabaseSanityServiceImpl.java 2014-01-18 15:14:32 UTC (rev 1504) @@ -0,0 +1,79 @@ +package fr.ifremer.adagio.core.service.technical.sanity; + +/* + * #%L + * Tutti :: Persistence + * $Id$ + * $HeadURL:$ + * %% + * Copyright (C) 2012 - 2014 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.collect.Sets; +import fr.ifremer.adagio.core.service.technical.CacheService; +import fr.ifremer.adagio.core.service.technical.sanity.task.DatabaseSanityTask; +import org.hibernate.SessionFactory; +import org.hibernate.classic.Session; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.ServiceLoader; +import java.util.Set; + +/** + * Default implementation of the {@link DatabaseSanityService}. + * <p/> + * Created on 1/18/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 3.0.1 + */ +@Service("databaseSanityService") +public class DatabaseSanityServiceImpl implements DatabaseSanityService { + + /** + * Session factory. + */ + @Autowired + protected SessionFactory sessionFactory; + + /** + * Cache service. + */ + @Resource(name = "cacheService") + protected CacheService cacheService; + + @Override + public void sanity() { + + Session currentSession = sessionFactory.getCurrentSession(); + + Set<String> cacheIds = Sets.newHashSet(); + + for (DatabaseSanityTask checker : ServiceLoader.load(DatabaseSanityTask.class)) { + Set<String> ids = checker.sanity(currentSession); + cacheIds.addAll(ids); + } + + // clear caches + for (String cacheId : cacheIds) { + cacheService.clearCache(cacheId); + } + } +} Property changes on: trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/DatabaseSanityServiceImpl.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTask.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTask.java (rev 0) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTask.java 2014-01-18 15:14:32 UTC (rev 1504) @@ -0,0 +1,46 @@ +package fr.ifremer.adagio.core.service.technical.sanity.task; + +/* + * #%L + * Tutti :: Persistence + * $Id$ + * $HeadURL:$ + * %% + * Copyright (C) 2012 - 2014 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 org.hibernate.classic.Session; + +import java.util.Set; + +/** + * Created on 1/18/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 3.0.1 + */ +public interface DatabaseSanityTask { + + /** + * Sanity the database and return the set of cache id to refresh. + * + * @param currentSession database session + * @return the set of cache ids to clean. + */ + Set<String> sanity(Session currentSession); +} Property changes on: trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTask.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTaskVesselFeatures.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTaskVesselFeatures.java (rev 0) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTaskVesselFeatures.java 2014-01-18 15:14:32 UTC (rev 1504) @@ -0,0 +1,99 @@ +package fr.ifremer.adagio.core.service.technical.sanity.task; + +/* + * #%L + * Tutti :: Persistence + * $Id$ + * $HeadURL:$ + * %% + * Copyright (C) 2012 - 2014 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.collect.Lists; +import com.google.common.collect.Sets; +import fr.ifremer.adagio.core.dao.data.vessel.feature.physical.VesselFeaturesImpl; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.FlushMode; +import org.hibernate.Query; +import org.hibernate.classic.Session; + +import java.util.List; +import java.util.Set; + +/** + * Check and clean vessel features. + * <p/> + * Created on 1/18/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 3.0.1 + */ +public class DatabaseSanityTaskVesselFeatures implements DatabaseSanityTask { + + /** Logger. */ + private static final Log log = LogFactory.getLog(DatabaseSanityTaskVesselFeatures.class); + + @Override + public Set<String> sanity(Session currentSession) { + + Set<String> cacheIds = Sets.newHashSet(); + + // get all VESSEL_FEATURES with more than one unclosed period + + Query query = currentSession.createQuery("SELECT vessel.code FROM " + VesselFeaturesImpl.class.getName() + " WHERE endDateTime IS NULL GROUP BY vessel.code HAVING COUNT(id)>1"); + List<String> vesselCodes = query.list(); + + if (CollectionUtils.isEmpty(vesselCodes)) { + if (log.isInfoEnabled()) { + log.info("vesselFeatures are sane"); + } + return cacheIds; + } + + for (String vesselCode : vesselCodes) { + + // Get all features having a null endDatetime + Query query1 = currentSession.createQuery("SELECT id FROM " + VesselFeaturesImpl.class.getName() + " WHERE endDateTime IS NULL AND vessel.code = :vesselCode ORDER BY startDateTime DESC"); + query1.setString("vesselCode", vesselCode); + + List<Integer> vesselFeaturesIds = Lists.<Integer>newArrayList(query1.list()); + + // remove first id (this one will stay on db) + vesselFeaturesIds.remove(0); + + // remove all others ids + if (log.isInfoEnabled()) { + log.info(String.format("Remove %d bad vesselFeature with vessel: %s", + vesselFeaturesIds.size(), vesselCode)); + } + currentSession.createQuery("DELETE FROM " + VesselFeaturesImpl.class.getName() + " WHERE id in :id"). + setParameterList("id", vesselFeaturesIds).executeUpdate(); + } + + currentSession.setFlushMode(FlushMode.COMMIT); + currentSession.flush(); + + // clean vessel caches + cacheIds.add("fishingVessels"); + cacheIds.add("vesselByCode"); + + return cacheIds; + } +} Property changes on: trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTaskVesselFeatures.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTaskVesselRegistrationPeriod.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTaskVesselRegistrationPeriod.java (rev 0) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTaskVesselRegistrationPeriod.java 2014-01-18 15:14:32 UTC (rev 1504) @@ -0,0 +1,105 @@ +package fr.ifremer.adagio.core.service.technical.sanity.task; + +/* + * #%L + * Tutti :: Persistence + * $Id$ + * $HeadURL:$ + * %% + * Copyright (C) 2012 - 2014 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.collect.Lists; +import com.google.common.collect.Sets; +import fr.ifremer.adagio.core.dao.data.vessel.VesselRegistrationPeriod; +import fr.ifremer.adagio.core.dao.data.vessel.VesselRegistrationPeriodImpl; +import fr.ifremer.adagio.core.service.technical.CacheService; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.FlushMode; +import org.hibernate.Query; +import org.hibernate.classic.Session; + +import java.util.List; +import java.util.Set; + +/** + * Check that vessel registration periods are sane. + * <p/> + * Created on 1/18/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 3.0.1 + */ +public class DatabaseSanityTaskVesselRegistrationPeriod implements DatabaseSanityTask { + + /** Logger. */ + private static final Log log = LogFactory.getLog(DatabaseSanityTaskVesselRegistrationPeriod.class); + + @Override + public Set<String> sanity(Session currentSession) { + + Set<String> cacheIds = Sets.newHashSet(); + + // get all VESSEL_REGISTRATION_PERIOD with more than one unclosed period + Query query = currentSession.createQuery( + "SELECT vesselRegistrationPeriodPk.vessel.code FROM " + + VesselRegistrationPeriodImpl.class.getName() + " WHERE endDateTime IS NULL GROUP BY vesselRegistrationPeriodPk.vessel.code HAVING COUNT(*)>1"); + List<String> vesselCodes = query.list(); + + if (CollectionUtils.isEmpty(vesselCodes)) { + if (log.isInfoEnabled()) { + log.info("vesselRegistrationPeriods are sane"); + } + return cacheIds ; + } + + for (String vesselCode : vesselCodes) { + + // Get all features having a null endDatetime + Query query1 = currentSession.createQuery( + "SELECT vesselRegistrationPeriodPk FROM " + VesselRegistrationPeriodImpl.class.getName() + + " WHERE endDateTime IS NULL AND vesselRegistrationPeriodPk.vessel.code = :vesselCode ORDER BY vesselRegistrationPeriodPk.startDateTime DESC") + .setString("vesselCode", vesselCode); + + List<VesselRegistrationPeriod> vesselRegistrationPeriods = Lists.<VesselRegistrationPeriod>newArrayList(query1.list()); + + // remove first id (this one will stay on db) + vesselRegistrationPeriods.remove(0); + + // remove all others ids + if (log.isWarnEnabled()) { + log.warn(String.format("Remove %d bad VesselRegistrationPeriod for vessel: %s", + vesselRegistrationPeriods.size(), vesselCode)); + } + currentSession.createQuery("DELETE FROM " + VesselRegistrationPeriodImpl.class.getName() + " WHERE vesselRegistrationPeriodPk in :vesselRegistrationPeriodPk"). + setParameterList("vesselRegistrationPeriodPk", vesselRegistrationPeriods).executeUpdate(); + } + + currentSession.setFlushMode(FlushMode.COMMIT); + currentSession.flush(); + + // clean vessel caches + // clean vessel caches + cacheIds.add("fishingVessels"); + cacheIds.add("vesselByCode"); + + return cacheIds; + } +} Property changes on: trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/sanity/task/DatabaseSanityTaskVesselRegistrationPeriod.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java 2014-01-18 15:13:52 UTC (rev 1503) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java 2014-01-18 15:14:32 UTC (rev 1504) @@ -30,6 +30,7 @@ import fr.ifremer.adagio.core.dao.technical.DatabaseSchemaUpdateException; import fr.ifremer.adagio.core.dao.technical.VersionNotFoundException; import fr.ifremer.adagio.core.service.technical.CacheService; +import fr.ifremer.adagio.core.service.technical.sanity.DatabaseSanityService; import fr.ifremer.shared.application.ApplicationTechnicalException; import fr.ifremer.tutti.persistence.entities.CaracteristicMap; import fr.ifremer.tutti.persistence.entities.TuttiEntities; @@ -64,7 +65,6 @@ import fr.ifremer.tutti.persistence.service.BenthosBatchPersistenceService; import fr.ifremer.tutti.persistence.service.CatchBatchPersistenceService; import fr.ifremer.tutti.persistence.service.CruisePersistenceService; -import fr.ifremer.tutti.persistence.service.DatabaseCheckPersistenceService; import fr.ifremer.tutti.persistence.service.FishingOperationPersistenceService; import fr.ifremer.tutti.persistence.service.IndividualObservationBatchPersistenceService; import fr.ifremer.tutti.persistence.service.MarineLitterBatchPersistenceService; @@ -137,7 +137,7 @@ protected AttachmentPersistenceService attachmentService; @Autowired - protected DatabaseCheckPersistenceService databaseCheckPersistenceService; + protected DatabaseSanityService databaseSanityService; @Autowired protected CacheService cacheService; @@ -228,7 +228,6 @@ individualObservationBatchService.init(); protocolService.init(); attachmentService.init(); - databaseCheckPersistenceService.init(); TuttiEnumerationFile enumerationFile = getEnumerationFile(); @@ -236,8 +235,8 @@ if (CHECK) { - // check database is sane - databaseCheckPersistenceService.check(); + // sanity database + databaseSanityService.sanity(); } } @@ -265,7 +264,6 @@ individualObservationBatchService.close(); protocolService.close(); attachmentService.close(); - databaseCheckPersistenceService.close(); TuttiPersistenceServiceLocator.close(); } } Deleted: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/DatabaseCheckPersistenceService.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/DatabaseCheckPersistenceService.java 2014-01-18 15:13:52 UTC (rev 1503) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/DatabaseCheckPersistenceService.java 2014-01-18 15:14:32 UTC (rev 1504) @@ -1,44 +0,0 @@ -package fr.ifremer.tutti.persistence.service; - -/* - * #%L - * Tutti :: Persistence - * $Id$ - * $HeadURL:$ - * %% - * Copyright (C) 2012 - 2014 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 fr.ifremer.tutti.persistence.TuttiPersistenceServiceImplementor; -import org.springframework.transaction.annotation.Transactional; - -/** - * To fix some errors in db. - * - * Created on 1/15/14. - * - * @author Tony Chemit <chemit@codelutin.com> - * @since 3.0 - */ -@Transactional(readOnly = true) -public interface DatabaseCheckPersistenceService extends TuttiPersistenceServiceImplementor { - - @Transactional(readOnly = false) - public void check(); - -} Deleted: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/DatabaseCheckPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/DatabaseCheckPersistenceServiceImpl.java 2014-01-18 15:13:52 UTC (rev 1503) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/DatabaseCheckPersistenceServiceImpl.java 2014-01-18 15:14:32 UTC (rev 1504) @@ -1,98 +0,0 @@ -package fr.ifremer.tutti.persistence.service; - -/* - * #%L - * Tutti :: Persistence - * $Id$ - * $HeadURL:$ - * %% - * Copyright (C) 2012 - 2014 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.collect.Lists; -import fr.ifremer.adagio.core.dao.data.vessel.feature.physical.VesselFeaturesImpl; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.hibernate.FlushMode; -import org.hibernate.Query; -import org.hibernate.classic.Session; -import org.springframework.stereotype.Service; - -import java.util.List; - -/** - * To fix some errors in db. - * <p/> - * Created on 1/15/14. - * - * @author Tony Chemit <chemit@codelutin.com> - * @since 3.0 - */ -@Service("databaseCheckPersistenceService") -public class DatabaseCheckPersistenceServiceImpl extends AbstractPersistenceService - implements DatabaseCheckPersistenceService { - - /** Logger. */ - private static final Log log = - LogFactory.getLog(DatabaseCheckPersistenceServiceImpl.class); - - @Override - public void check() { - checkVesselFeatures(); - } - - public void checkVesselFeatures() { - - // get all VESSEL_FEATURES with more than one unclosed period - Session currentSession = getCurrentSession(); - - Query query = currentSession.createQuery("SELECT vessel.code FROM " + VesselFeaturesImpl.class.getName() + " WHERE endDateTime IS NULL GROUP BY vessel.code HAVING COUNT(id)>1"); - List<String> vesselCodes = query.list(); - - if (CollectionUtils.isEmpty(vesselCodes)) { - if (log.isInfoEnabled()) { - log.info("vesselFeatures are sane"); - } - return; - } - - for (String vesselCode : vesselCodes) { - - // Get all features having a null endDatetime - Query query1 = currentSession.createQuery("SELECT id FROM " + VesselFeaturesImpl.class.getName() + " WHERE endDateTime IS NULL AND vessel.code = :vesselCode ORDER BY startDateTime DESC"); - query1.setString("vesselCode", vesselCode); - - List<Integer> vesselFeaturesIds = Lists.<Integer>newArrayList(query1.list()); - - // remove first id (this one will stay on db) - vesselFeaturesIds.remove(0); - - // remove all others ids - if (log.isInfoEnabled()) { - log.info("Remove vesselFeature with id: " + vesselFeaturesIds); - } - currentSession.createQuery("DELETE FROM " + VesselFeaturesImpl.class.getName() + " WHERE id in :id"). - setParameterList("id", vesselFeaturesIds).executeUpdate(); - } - - getCurrentSession().setFlushMode(FlushMode.COMMIT); - getCurrentSession().flush(); - } - -} Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/TuttiPersistenceServiceLocator.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/TuttiPersistenceServiceLocator.java 2014-01-18 15:13:52 UTC (rev 1503) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/TuttiPersistenceServiceLocator.java 2014-01-18 15:14:32 UTC (rev 1504) @@ -25,6 +25,7 @@ */ import fr.ifremer.adagio.core.service.ServiceLocator; +import fr.ifremer.adagio.core.service.technical.sanity.DatabaseSanityService; import fr.ifremer.adagio.core.service.technical.synchro.ReferentialSynchroService; import fr.ifremer.tutti.persistence.TuttiPersistence; import fr.ifremer.tutti.persistence.TuttiPersistenceServiceImplementor; @@ -122,6 +123,13 @@ return service; } + //TODO Move this to adagio + public static DatabaseSanityService getDatabaseSanityService() { + DatabaseSanityService service = instance().getService( + "databaseSanityService", DatabaseSanityService.class); + return service; + } + public static AttachmentPersistenceService getAttachmentPersistenceService() { return getPersistenceService("attachmentPersistenceService", AttachmentPersistenceService.class); Added: trunk/tutti-persistence/src/main/resources/META-INF/services/fr.ifremer.adagio.core.service.technical.sanity.task.DatabaseSanityTask =================================================================== --- trunk/tutti-persistence/src/main/resources/META-INF/services/fr.ifremer.adagio.core.service.technical.sanity.task.DatabaseSanityTask (rev 0) +++ trunk/tutti-persistence/src/main/resources/META-INF/services/fr.ifremer.adagio.core.service.technical.sanity.task.DatabaseSanityTask 2014-01-18 15:14:32 UTC (rev 1504) @@ -0,0 +1,2 @@ +fr.ifremer.adagio.core.service.technical.sanity.task.DatabaseSanityTaskVesselFeatures +fr.ifremer.adagio.core.service.technical.sanity.task.DatabaseSanityTaskVesselRegistrationPeriod \ No newline at end of file