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.HashMultimap;
29  import com.google.common.collect.ImmutableSet;
30  import com.google.common.collect.Lists;
31  import com.google.common.collect.Multimap;
32  import fr.ifremer.adagio.core.dao.referential.location.LocationClassificationId;
33  import fr.ifremer.adagio.core.dao.referential.location.LocationExtendDao;
34  import fr.ifremer.adagio.core.dao.referential.location.LocationLevelId;
35  import fr.ifremer.adagio.core.service.referential.location.LocationService;
36  import fr.ifremer.tutti.persistence.entities.referential.TuttiLocation;
37  import fr.ifremer.tutti.persistence.entities.referential.TuttiLocations;
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  import org.hibernate.type.IntegerType;
41  import org.springframework.stereotype.Service;
42  
43  import javax.annotation.Resource;
44  import java.util.Collections;
45  import java.util.Iterator;
46  import java.util.List;
47  import java.util.stream.Collectors;
48  
49  /**
50   * Created on 11/3/14.
51   *
52   * @author Tony Chemit - chemit@codelutin.com
53   * @since 3.8
54   */
55  @Service("locationPersistenceService")
56  public class LocationPersistenceServiceImpl extends ReferentialPersistenceServiceSupport implements LocationPersistenceService {
57  
58      /** Logger. */
59      private static final Log log = LogFactory.getLog(LocationPersistenceServiceImpl.class);
60  
61      @Resource(name = "locationDao")
62      protected LocationExtendDao locationDao;
63  
64      @Resource(name = "locationService")
65      protected LocationService locationService;
66  
67      @Override
68      public List<TuttiLocation> getAllProgramZone() {
69          Iterator<Object[]> list = queryListWithStatus(
70                  "allLocationsByLevelAndClassificiation",
71                  "locationClassificationId", IntegerType.INSTANCE, LocationClassificationId.SECTOR.getValue(),
72                  "locationLevelId", IntegerType.INSTANCE, LocationLevelId.SCIENTIFIC_CRUISE_PROGRAM.getValue());
73  
74          List<TuttiLocation> result = Lists.newArrayList();
75          loadLocations(list, result);
76          return Collections.unmodifiableList(result);
77      }
78  
79      @Override
80      public List<TuttiLocation> getAllCountry() {
81          Iterator<Object[]> list = queryListWithStatus(
82                  "allLocationsByLevelAndClassificiation",
83                  "locationClassificationId", IntegerType.INSTANCE, LocationClassificationId.TERRITORIAL.getValue(),
84                  "locationLevelId", IntegerType.INSTANCE, LocationLevelId.PAYS_ISO3.getValue());
85  
86          List<TuttiLocation> result = Lists.newArrayList();
87          loadLocations(list, result);
88          return Collections.unmodifiableList(result);
89      }
90  
91      @Override
92      public List<TuttiLocation> getAllHarbour() {
93          Iterator<Object[]> list = queryListWithStatus(
94                  "allLocationsByLevelAndClassificiation",
95                  "locationClassificationId", IntegerType.INSTANCE, LocationClassificationId.TERRITORIAL.getValue(),
96                  "locationLevelId", IntegerType.INSTANCE, LocationLevelId.PORT.getValue());
97  
98          List<TuttiLocation> result = Lists.newArrayList();
99          loadLocations(list, result);
100         return Collections.unmodifiableList(result);
101     }
102 
103     @Override
104     public List<TuttiLocation> getAllHarbourWithObsoletes() {
105         Iterator<Object[]> list = queryListWithStatus2(
106                 "allLocationsByLevelAndClassificiationWithObsoletes",
107                 "locationClassificationId", IntegerType.INSTANCE, LocationClassificationId.TERRITORIAL.getValue(),
108                 "locationLevelId", IntegerType.INSTANCE, LocationLevelId.PORT.getValue());
109 
110         List<TuttiLocation> result = Lists.newArrayList();
111         loadLocations(list, result);
112         return Collections.unmodifiableList(result);
113     }
114 
115     @Override
116     public ImmutableSet<Integer> getAllFishingOperationStratasAndSubstratasIdsForProgram(String zoneId) {
117         Preconditions.checkNotNull(zoneId);
118 
119         ImmutableSet.Builder<Integer> resultBuilder = ImmutableSet.builder();
120 
121         List<TuttiLocation> stratas = getAllFishingOperationStrata(zoneId);
122 
123         if (log.isInfoEnabled()) {
124             log.info("found " + stratas.size() + " stratas for zone: " + zoneId);
125         }
126 
127         stratas.forEach(strata -> {
128 
129             resultBuilder.add(strata.getIdAsInt());
130 
131             List<TuttiLocation> subStrats = getAllFishingOperationSubStrata(zoneId, strata.getId());
132 
133             if (log.isInfoEnabled()) {
134                 log.info("found " + subStrats.size() + " substratas for strata: " + strata);
135             }
136 
137             subStrats.forEach(subStrata -> resultBuilder.add(subStrata.getIdAsInt()));
138 
139         });
140 
141         return resultBuilder.build();
142     }
143 
144     @Override
145     public Multimap<TuttiLocation, TuttiLocation> getAllFishingOperationStratasAndSubstratas(final String zoneId) {
146         Preconditions.checkNotNull(zoneId);
147 
148         Multimap<TuttiLocation, TuttiLocation> result = HashMultimap.create();
149 
150         List<TuttiLocation> stratas = getAllFishingOperationStrata(zoneId);
151 
152         if (log.isInfoEnabled()) {
153             log.info("stratas : " + stratas.stream().map(TuttiLocation::getLabel).collect(Collectors.toSet()));
154         }
155 
156         stratas.forEach(strata -> {
157             List<TuttiLocation> allFishingOperationSubStrata = getAllFishingOperationSubStrata(zoneId, strata.getId());
158 
159             if (allFishingOperationSubStrata.isEmpty()) {
160                 result.put(strata, null);
161 
162             } else {
163                 result.putAll(strata, allFishingOperationSubStrata);
164             }
165         });
166 
167         if (log.isInfoEnabled()) {
168             log.info("stratas in result : " + result.keySet().stream().map(TuttiLocation::getLabel).collect(Collectors.toSet()));
169         }
170 
171         return result;
172     }
173 
174     @Override
175     public List<TuttiLocation> getAllFishingOperationStrata(String zoneId) {
176         Preconditions.checkNotNull(zoneId);
177         List<TuttiLocation> result = getFishingOperationLocationsByParent(
178                 LocationLevelId.SCIENTIFIC_CRUISE_STRATA.getValue(),
179                 Integer.valueOf(zoneId),
180                 LocationLevelId.SCIENTIFIC_CRUISE_PROGRAM.getValue());
181         return Collections.unmodifiableList(result);
182     }
183 
184     @Override
185     public List<TuttiLocation> getAllFishingOperationStrataWithObsoletes(String zoneId) {
186         Preconditions.checkNotNull(zoneId);
187         List<TuttiLocation> result = getFishingOperationLocationsByParentWithObsoletes(
188                 LocationLevelId.SCIENTIFIC_CRUISE_STRATA.getValue(),
189                 Integer.valueOf(zoneId),
190                 LocationLevelId.SCIENTIFIC_CRUISE_PROGRAM.getValue());
191         return Collections.unmodifiableList(result);
192     }
193 
194     @Override
195     public List<TuttiLocation> getAllFishingOperationSubStrata(String zoneId,
196                                                                String strataId) {
197 
198         String parentId;
199         Integer parentLocationLevelId;
200 
201         if (strataId != null) {
202 
203             // use strata as parent
204             parentId = strataId;
205             parentLocationLevelId = LocationLevelId.SCIENTIFIC_CRUISE_STRATA.getValue();
206 
207         } else {
208 
209             // use zoneId as parent
210             parentId = zoneId;
211             parentLocationLevelId = LocationLevelId.SCIENTIFIC_CRUISE_PROGRAM.getValue();
212         }
213 
214         Preconditions.checkNotNull(parentId);
215         List<TuttiLocation> result = getFishingOperationLocationsByParent(
216                 LocationLevelId.SCIENTIFIC_CRUISE_SUB_STRATA.getValue(),
217                 Integer.valueOf(parentId),
218                 parentLocationLevelId);
219         return Collections.unmodifiableList(result);
220     }
221 
222     @Override
223     public List<TuttiLocation> getAllFishingOperationSubStrataWithObsoletes(String zoneId, String strataId) {
224 
225         String parentId;
226         Integer parentLocationLevelId;
227 
228         if (strataId != null) {
229 
230             // use strata as parent
231             parentId = strataId;
232             parentLocationLevelId = LocationLevelId.SCIENTIFIC_CRUISE_STRATA.getValue();
233 
234         } else {
235 
236             // use zoneId as parent
237             parentId = zoneId;
238             parentLocationLevelId = LocationLevelId.SCIENTIFIC_CRUISE_PROGRAM.getValue();
239         }
240 
241         Preconditions.checkNotNull(parentId);
242         List<TuttiLocation> result = getFishingOperationLocationsByParentWithObsoletes(
243                 LocationLevelId.SCIENTIFIC_CRUISE_SUB_STRATA.getValue(),
244                 Integer.valueOf(parentId),
245                 parentLocationLevelId);
246         return Collections.unmodifiableList(result);
247 
248     }
249 
250     @Override
251     public List<TuttiLocation> getAllFishingOperationLocation(String zoneId,
252                                                               String strataId,
253                                                               String subStrataId) {
254 
255         String parentId;
256         Integer parentLocationLevelId;
257 
258         if (subStrataId != null) {
259 
260             // use substrata as parent
261             parentId = subStrataId;
262             parentLocationLevelId = LocationLevelId.SCIENTIFIC_CRUISE_SUB_STRATA.getValue();
263 
264         } else if (strataId != null) {
265 
266             // use strata as parent
267             parentId = strataId;
268             parentLocationLevelId = LocationLevelId.SCIENTIFIC_CRUISE_STRATA.getValue();
269 
270         } else {
271 
272             // use zoneId as parent
273             parentId = zoneId;
274             parentLocationLevelId = LocationLevelId.SCIENTIFIC_CRUISE_PROGRAM.getValue();
275         }
276 
277         Preconditions.checkNotNull(parentId);
278         List<TuttiLocation> result = getFishingOperationLocationsByParent(
279                 LocationLevelId.SCIENTIFIC_CRUISE_LOCALITE.getValue(),
280                 Integer.valueOf(parentId),
281                 parentLocationLevelId);
282         return Collections.unmodifiableList(result);
283     }
284 
285     @Override
286     public List<TuttiLocation> getAllFishingOperationLocationWithObsoletes(String zoneId, String strataId, String subStrataId) {
287 
288         String parentId;
289         Integer parentLocationLevelId;
290 
291         if (subStrataId != null) {
292 
293             // use substrata as parent
294             parentId = subStrataId;
295             parentLocationLevelId = LocationLevelId.SCIENTIFIC_CRUISE_SUB_STRATA.getValue();
296 
297         } else if (strataId != null) {
298 
299             // use strata as parent
300             parentId = strataId;
301             parentLocationLevelId = LocationLevelId.SCIENTIFIC_CRUISE_STRATA.getValue();
302 
303         } else {
304 
305             // use zoneId as parent
306             parentId = zoneId;
307             parentLocationLevelId = LocationLevelId.SCIENTIFIC_CRUISE_PROGRAM.getValue();
308         }
309 
310         Preconditions.checkNotNull(parentId);
311         List<TuttiLocation> result = getFishingOperationLocationsByParentWithObsoletes(
312                 LocationLevelId.SCIENTIFIC_CRUISE_LOCALITE.getValue(),
313                 Integer.valueOf(parentId),
314                 parentLocationLevelId);
315         return Collections.unmodifiableList(result);
316 
317     }
318 
319     @Override
320     public TuttiLocation getLocation(String id) {
321         Object[] source = queryUnique(
322                 "locationById",
323                 "locationId", IntegerType.INSTANCE, Integer.valueOf(id)
324         );
325         TuttiLocation target;
326         if (source == null) {
327             target = null;
328         } else {
329             target = loadLocation(source);
330         }
331         return target;
332     }
333 
334     @Override
335     public String getLocationLabelByLatLong(Float latitude, Float longitude) {
336         return locationService.getLocationLabelByLatLong(latitude, longitude);
337     }
338 
339     @Override
340     public Integer getLocationIdByLatLong(Float latitude, Float longitude) {
341         return locationService.getLocationIdByLatLong(latitude, longitude);
342     }
343 
344     protected List<TuttiLocation> getFishingOperationLocationsByParent(Integer locationLevelId, Integer parentId, Integer parentLocationLevelId) {
345         Iterator<Object[]> sources = queryListWithStatus(
346                 "allFishingOperationLocationByParent",
347                 "parentId", IntegerType.INSTANCE, parentId,
348                 "parentLocationLevelId", IntegerType.INSTANCE, parentLocationLevelId,
349                 "locationClassificationId", IntegerType.INSTANCE, LocationClassificationId.SECTOR.getValue(),
350                 "locationLevelId", IntegerType.INSTANCE, locationLevelId
351         );
352         List<TuttiLocation> result = Lists.newArrayList();
353         loadLocations(sources, result);
354         return result;
355     }
356 
357     protected List<TuttiLocation> getFishingOperationLocationsByParentWithObsoletes(Integer locationLevelId, Integer parentId, Integer parentLocationLevelId) {
358         Iterator<Object[]> sources = queryListWithStatus2(
359                 "allFishingOperationLocationByParentWithObsoletes",
360                 "parentId", IntegerType.INSTANCE, parentId,
361                 "parentLocationLevelId", IntegerType.INSTANCE, parentLocationLevelId,
362                 "locationClassificationId", IntegerType.INSTANCE, LocationClassificationId.SECTOR.getValue(),
363                 "locationLevelId", IntegerType.INSTANCE, locationLevelId
364         );
365         List<TuttiLocation> result = Lists.newArrayList();
366         loadLocations(sources, result);
367         return result;
368     }
369 
370     protected TuttiLocation loadLocation(Object... source) {
371         TuttiLocation target = TuttiLocations.newTuttiLocation();
372         target.setId((Integer) source[0]);
373         target.setLabel((String) source[1]);
374         target.setName((String) source[2]);
375         String statusCode = (String) source[3];
376         setStatus(statusCode, target);
377         return target;
378     }
379 
380     protected void loadLocations(Iterator<Object[]> list, List<TuttiLocation> result) {
381         while (list.hasNext()) {
382             Object[] source = list.next();
383             TuttiLocation target = loadLocation(source);
384             result.add(target);
385         }
386     }
387 
388 }