View Javadoc
1   package fr.ifremer.tutti.persistence.service.util;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import fr.ifremer.adagio.core.dao.data.batch.Batch;
26  import fr.ifremer.adagio.core.dao.data.batch.CatchBatch;
27  import fr.ifremer.adagio.core.dao.data.batch.CatchBatchImpl;
28  import fr.ifremer.adagio.core.dao.data.sample.Sample;
29  import fr.ifremer.adagio.core.dao.data.survey.fishingTrip.FishingTrip;
30  import fr.ifremer.adagio.core.dao.data.survey.scientificCruise.ScientificCruise;
31  import fr.ifremer.adagio.core.dao.technical.synchronization.SynchronizationStatus;
32  import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch;
33  import fr.ifremer.tutti.persistence.entities.data.IndividualObservationBatch;
34  import fr.ifremer.tutti.persistence.service.AbstractPersistenceService;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.hibernate.LazyInitializationException;
38  import org.hibernate.type.IntegerType;
39  import org.hibernate.type.StringType;
40  import org.springframework.dao.DataIntegrityViolationException;
41  import org.springframework.stereotype.Component;
42  
43  /**
44   * Helper around SynchronizationStatus.
45   *
46   * Created on 4/22/14.
47   *
48   * @author Tony Chemit - chemit@codelutin.com
49   * @since 3.5
50   */
51  @Component("synchronizationStatusHelper")
52  public class SynchronizationStatusHelper extends AbstractPersistenceService {
53  
54      /** Logger. */
55      private static final Log log = LogFactory.getLog(SynchronizationStatusHelper.class);
56  
57      /**
58       * A internal state to skip any propagation to parent objects.
59       *
60       * This is used while generic format import.
61       *
62       * @since 3.15
63       */
64      private static boolean propagateToParents = true;
65  
66      public static void propagateDirtyStatusToParents() {
67          SynchronizationStatusHelper.propagateToParents = true;
68      }
69  
70      public static void doNotPropagateDirtyStatusToParents() {
71          SynchronizationStatusHelper.propagateToParents = false;
72      }
73  
74      public void setDirty(ScientificCruise scientificCruise) {
75  
76          String synchronizationStatus = getDirtyValue();
77          if (log.isInfoEnabled()) {
78              log.info("SetDirty scientificCruise: " + scientificCruise.getId());
79          }
80          scientificCruise.setSynchronizationStatus(synchronizationStatus);
81  
82      }
83  
84      public void setDirty(FishingTrip fishingTrip) {
85  
86          String synchronizationStatus = getDirtyValue();
87          fishingTrip.setSynchronizationStatus(synchronizationStatus);
88          if (log.isInfoEnabled()) {
89              log.info("SetDirty fishingTrip: " + fishingTrip.getId());
90          }
91  
92          if (propagateToParents) {
93              setDirty(fishingTrip.getScientificCruise());
94          }
95  
96      }
97  
98      public void setDirty(CatchBatch catchBatch) {
99  
100         String synchronizationStatus = getDirtyValue();
101 
102         if (log.isInfoEnabled()) {
103             log.info("SetDirty catchBatch: " + catchBatch.getId());
104         }
105         catchBatch.setSynchronizationStatus(synchronizationStatus);
106 
107         if (propagateToParents) {
108             try {
109                 setDirty(catchBatch.getFishingOperation().getFishingTrip());
110             } catch (LazyInitializationException e) {
111 
112                 // We need to have a loaded catchBatch, since it can come from a cache, prefer to reload it here
113                 CatchBatchImpl loadedCatchBatch = load(CatchBatchImpl.class, catchBatch.getId());
114                 loadedCatchBatch.setSynchronizationStatus(synchronizationStatus);
115             }
116         }
117 
118     }
119 
120     public void setDirty(Sample sample) {
121 
122         String synchronizationStatus = getDirtyValue();
123         sample.setSynchronizationStatus(synchronizationStatus);
124         if (log.isInfoEnabled()) {
125             log.info("SetDirty sample: " + sample.getId());
126         }
127 
128         if (propagateToParents) {
129             Batch batch = sample.getBatch();
130             if (batch != null && batch instanceof CatchBatch) {
131                 setDirty((CatchBatch) batch);
132             } else if (sample.getFishingOperation() != null) {
133                 setDirty(sample.getFishingOperation().getFishingTrip());
134             }
135         }
136 
137     }
138 
139     public void setDirty(fr.ifremer.tutti.persistence.entities.data.CatchBatch bean) {
140         bean.setSynchronizationStatus(getDirtyValue());
141     }
142 
143     public void setDirty(IndividualObservationBatch bean) {
144         bean.setSynchronizationStatus(getDirtyValue());
145     }
146 
147     public void setDirty(AccidentalBatch bean) {
148         bean.setSynchronizationStatus(getDirtyValue());
149     }
150 
151     public void setCruiseReadyToSynch(Integer cruiseId) {
152 
153         String oldStatus = getDirtyValue();
154         String newStatus = SynchronizationStatus.READY_TO_SYNCHRONIZE.getValue();
155 
156         if (log.isInfoEnabled()) {
157             log.info("setCruiseReadyToSynch cruise: " + cruiseId);
158         }
159 
160         // update scientificCruise
161         int scientificCruiseQueryUpdate = queryUpdate("updateScientificCruiseSynchronizationStatus",
162                                                       "cruiseId", IntegerType.INSTANCE, cruiseId,
163                                                       "oldStatus", StringType.INSTANCE, oldStatus,
164                                                       "newStatus", StringType.INSTANCE, newStatus);
165 
166         if (scientificCruiseQueryUpdate == 0) {
167             throw new DataIntegrityViolationException("Could not attach update scientific cruise, was not found.");
168         }
169 
170         // update fishingTrip
171         int fishingTripQueryUpdate = queryUpdate("updateFishingTripSynchronizationStatus",
172                                                  "cruiseId", IntegerType.INSTANCE, cruiseId,
173                                                  "oldStatus", StringType.INSTANCE, oldStatus,
174                                                  "newStatus", StringType.INSTANCE, newStatus);
175 
176         if (log.isInfoEnabled()) {
177             log.info("Nb fishingTrip  updated: " + fishingTripQueryUpdate);
178         }
179 
180         // update catchBatch
181         int catchBatchQueryUpdate = queryUpdate("updateCatchBatchSynchronizationStatus",
182                                                 "cruiseId", IntegerType.INSTANCE, cruiseId,
183                                                 "oldStatus", StringType.INSTANCE, oldStatus,
184                                                 "newStatus", StringType.INSTANCE, newStatus);
185         if (log.isInfoEnabled()) {
186             log.info("Nb catchBatch  updated: " + catchBatchQueryUpdate);
187         }
188 
189         // update sample
190         int sampleQueryUpdate = queryUpdate("updateSampleSynchronizationStatus",
191                                             "cruiseId", IntegerType.INSTANCE, cruiseId,
192                                             "oldStatus", StringType.INSTANCE, oldStatus,
193                                             "newStatus", StringType.INSTANCE, newStatus);
194 
195         if (log.isInfoEnabled()) {
196             log.info("Nb sample updated: " + sampleQueryUpdate);
197         }
198     }
199 
200     protected String getDirtyValue() {
201         return SynchronizationStatus.DIRTY.getValue();
202     }
203 }