View Javadoc
1   package fr.ifremer.tutti.persistence.service.referential;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import com.google.common.base.Preconditions;
28  import com.google.common.collect.Lists;
29  import fr.ifremer.adagio.core.dao.referential.StatusCode;
30  import fr.ifremer.adagio.core.dao.referential.gear.FishingGearExtendDao;
31  import fr.ifremer.adagio.core.dao.referential.gear.GearClassificationId;
32  import fr.ifremer.adagio.core.dao.referential.gear.GearClassificationImpl;
33  import fr.ifremer.tutti.persistence.entities.referential.Gear;
34  import fr.ifremer.tutti.persistence.entities.referential.Gears;
35  import org.hibernate.type.IntegerType;
36  import org.hibernate.type.StringType;
37  import org.nuiton.jaxx.application.ApplicationBusinessException;
38  import org.springframework.stereotype.Service;
39  
40  import javax.annotation.Resource;
41  import java.util.Collection;
42  import java.util.Collections;
43  import java.util.Iterator;
44  import java.util.List;
45  
46  /**
47   * Created on 11/3/14.
48   *
49   * @author Tony Chemit - chemit@codelutin.com
50   * @since 3.8
51   */
52  @Service("gearPersistenceService")
53  public class GearPersistenceServiceImpl extends ReferentialPersistenceServiceSupport implements GearPersistenceService {
54  
55      @Resource(name = "fishingGearDao")
56      protected FishingGearExtendDao fishingGearDao;
57  
58      @Override
59      public List<Gear> getAllScientificGear() {
60  
61          Iterator<Object[]> sources = queryListWithStatus(
62                  "allGears",
63                  "gearClassificiationId", IntegerType.INSTANCE, GearClassificationId.SCIENTIFIC_CRUISE.getValue());
64          List<Gear> result = Lists.newArrayList();
65          loadGears(sources, result);
66          return Collections.unmodifiableList(result);
67  
68      }
69  
70      @Override
71      public List<Gear> getAllFishingGear() {
72  
73          Iterator<Object[]> sources = queryListWithStatus(
74                  "allGears",
75                  "gearClassificiationId", IntegerType.INSTANCE, GearClassificationId.FAO.getValue());
76          List<Gear> result = Lists.newArrayList();
77          loadGears(sources, result);
78          return Collections.unmodifiableList(result);
79  
80      }
81  
82      @Override
83      public List<Gear> getAllGearWithObsoletes() {
84  
85          List<Gear> result = Lists.newArrayList();
86  
87          Iterator<Object[]> fishingSources = queryListWithStatus2(
88                  "allGearsWithObsoletes",
89                  "gearClassificiationId", IntegerType.INSTANCE, GearClassificationId.FAO.getValue());
90          loadGears(fishingSources, result);
91  
92          Iterator<Object[]> scientificSources = queryListWithStatus2(
93                  "allGearsWithObsoletes",
94                  "gearClassificiationId", IntegerType.INSTANCE, GearClassificationId.SCIENTIFIC_CRUISE.getValue());
95          loadGears(scientificSources, result);
96          return Collections.unmodifiableList(result);
97  
98      }
99  
100     @Override
101     public Gear getGear(Integer gearId) {
102 
103         Object[] source = queryUniqueWithStatus2(
104                 "gearById",
105                 "gearId", IntegerType.INSTANCE, gearId);
106 
107         return source == null ? null : loadGear(source);
108 
109     }
110 
111     protected void loadGears(Iterator<Object[]> sources, List<Gear> result) {
112         while (sources.hasNext()) {
113             Object[] source = sources.next();
114             Gear target = loadGear(source);
115             result.add(target);
116         }
117     }
118 
119     @Override
120     public boolean isTemporaryGearUsed(Integer id) {
121         Long count = queryUniqueTyped("countGearInGearPhysicalFeatures", "id", IntegerType.INSTANCE, id);
122         boolean result = count > 0;
123 
124         if (!result) {
125             count = queryUniqueTyped("countGearInGearUseFeatures", "id", IntegerType.INSTANCE, id);
126             result = count > 0;
127         }
128         return result;
129     }
130 
131     @Override
132     public List<Gear> addTemporaryGears(List<Gear> gears) {
133 
134         List<Gear> result = Lists.newArrayList();
135         for (Gear source : gears) {
136             Gear added = addTemporaryGear(source);
137             result.add(added);
138         }
139         return Collections.unmodifiableList(result);
140 
141     }
142 
143     @Override
144     public List<Gear> updateTemporaryGears(List<Gear> gears) {
145 
146         List<Gear> result = Lists.newArrayList();
147         for (Gear source : gears) {
148             Gear updated = updateTemporaryGear(source);
149             result.add(updated);
150         }
151         return Collections.unmodifiableList(result);
152 
153     }
154 
155     @Override
156     public List<Gear> linkTemporaryGears(List<Gear> gears) {
157 
158         List<Gear> result = Lists.newArrayList();
159         for (Gear source : gears) {
160             Gear linked = linkTemporaryGear(source);
161             result.add(linked);
162         }
163         return Collections.unmodifiableList(result);
164 
165     }
166 
167     @Override
168     public void replaceGear(Gear source, Gear target, boolean delete) {
169 
170         Preconditions.checkNotNull(source);
171         Preconditions.checkNotNull(target);
172         Preconditions.checkState(Gears.isTemporary(source));
173         Preconditions.checkState(!Gears.isTemporary(target));
174 
175         Integer sourceId = source.getIdAsInt();
176         Integer targetId = target.getIdAsInt();
177 
178         queryUpdate("replaceGearInGearPhysicalFeatures",
179                     "sourceId", IntegerType.INSTANCE, sourceId,
180                     "targetId", IntegerType.INSTANCE, targetId);
181 
182         queryUpdate("replaceGearInGearUseFeatures",
183                     "sourceId", IntegerType.INSTANCE, sourceId,
184                     "targetId", IntegerType.INSTANCE, targetId);
185 
186         //TODO Check doublon...
187 
188         if (delete) {
189 
190             deleteTemporaryGear(sourceId);
191 
192         }
193 
194     }
195 
196     @Override
197     public void deleteTemporaryGears(Collection<Integer> ids) {
198 
199         for (Integer id : ids) {
200             deleteTemporaryGear(id);
201         }
202 
203     }
204 
205     @Override
206     public void deleteTemporaryGear(Integer id) {
207 
208         Preconditions.checkNotNull(id);
209         if (id > 0) {
210             throw new ApplicationBusinessException(String.format("Can't delete a Gear with a positive id %d.", id));
211         }
212         Gear gear = getGear(id);
213         if (gear == null) {
214             throw new ApplicationBusinessException(String.format("Gear with id %d does not exists", id));
215         }
216 
217         fishingGearDao.remove(id);
218 
219     }
220 
221     protected Gear addTemporaryGear(Gear source) {
222 
223         Preconditions.checkNotNull(source);
224         Preconditions.checkNotNull(source.getLabel());
225         Preconditions.checkNotNull(source.getName());
226         Preconditions.checkArgument(source.getId() == null || Gears.isTemporaryId(source.getIdAsInt()));
227 
228         Integer gearClassificationId = getGearClassificationId(source);
229 
230         fr.ifremer.adagio.core.dao.referential.gear.Gear target = fishingGearDao.createAsTemporary(source.getLabel(), source.getName(), gearClassificationId);
231         Gear result = Gears.newGear();
232         result.setId(target.getId());
233 
234         // Fill the result bean
235         result.setLabel(source.getLabel());
236         result.setName(source.getName());
237         result.setScientificGear(source.isScientificGear());
238         setStatus(StatusCode.TEMPORARY.getValue(), result);
239 
240         return result;
241 
242     }
243 
244     protected Gear updateTemporaryGear(Gear source) {
245 
246         Preconditions.checkNotNull(source);
247         Preconditions.checkNotNull(source.getId());
248         Preconditions.checkNotNull(source.getLabel());
249         Preconditions.checkNotNull(source.getName());
250         Preconditions.checkArgument(Gears.isTemporaryId(source.getIdAsInt()));
251 
252         Gear result = getGear(source.getIdAsInt());
253 
254         // Fill the result bean
255         result.setLabel(source.getLabel());
256         result.setName(source.getName());
257         result.setScientificGear(source.isScientificGear());
258         setStatus(StatusCode.TEMPORARY.getValue(), result);
259 
260         fr.ifremer.adagio.core.dao.referential.gear.FishingGear toUpdate = fishingGearDao.load(source.getIdAsInt());
261         toUpdate.setLabel(result.getLabel());
262         toUpdate.setName(result.getName());
263         Integer gearClassificationId = getGearClassificationId(source);
264         toUpdate.setGearClassification(load(GearClassificationImpl.class, gearClassificationId));
265         fishingGearDao.update(toUpdate);
266 
267         return result;
268 
269     }
270 
271     private Integer getGearClassificationId(Gear source) {
272         Integer gearClassificationId;
273         if (source.isScientificGear()) {
274             gearClassificationId = GearClassificationId.SCIENTIFIC_CRUISE.getValue();
275         } else {
276             gearClassificationId = GearClassificationId.FAO.getValue();
277         }
278         return gearClassificationId;
279     }
280 
281     protected Gear linkTemporaryGear(Gear source) {
282 
283         Preconditions.checkNotNull(source);
284         Preconditions.checkNotNull(source.getId());
285         Preconditions.checkNotNull(source.getLabel());
286         Preconditions.checkNotNull(source.getName());
287         Preconditions.checkArgument(Gears.isTemporaryId(source.getIdAsInt()));
288 
289         Object[] row = queryUniqueWithStatus2(
290                 "gearByName",
291                 "gearName", StringType.INSTANCE, source.getName());
292 
293         return row == null ? null : loadGear(row);
294 
295     }
296 
297     protected Gear loadGear(Object... source) {
298 
299         Gear result = Gears.newGear();
300         result.setId(String.valueOf(source[0]));
301         result.setLabel((String) source[1]);
302         result.setName((String) source[2]);
303         Integer classification = (Integer) source[3];
304         boolean scientific = false;
305         if (classification != null) {
306             scientific = GearClassificationId.SCIENTIFIC_CRUISE.getValue().equals(classification);
307         }
308         result.setScientificGear(scientific);
309 
310         setStatus((String) source[4], result);
311         return result;
312 
313     }
314 }