View Javadoc
1   package net.sumaris.core.service.data;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core
6    * %%
7    * Copyright (C) 2018 SUMARiS Consortium
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  
26  import com.google.common.base.Preconditions;
27  import com.google.common.collect.ImmutableList;
28  import net.sumaris.core.dao.data.MeasurementDao;
29  import net.sumaris.core.dao.data.SaleDao;
30  import net.sumaris.core.dao.data.TripDao;
31  import net.sumaris.core.dao.technical.SortDirection;
32  import net.sumaris.core.model.data.VesselUseMeasurement;
33  import net.sumaris.core.util.Beans;
34  import net.sumaris.core.util.DataBeans;
35  import net.sumaris.core.vo.data.*;
36  import net.sumaris.core.vo.filter.TripFilterVO;
37  import org.apache.commons.collections4.CollectionUtils;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  import org.springframework.beans.factory.annotation.Autowired;
41  import org.springframework.stereotype.Service;
42  
43  import java.util.List;
44  import java.util.Objects;
45  import java.util.stream.Collectors;
46  
47  @Service("tripService")
48  public class TripServiceImpl implements TripService {
49  
50      private static final Logger log = LoggerFactory.getLogger(TripServiceImpl.class);
51  
52      @Autowired
53      protected TripDao tripDao;
54  
55      @Autowired
56      protected SaleDao saleDao;
57  
58      @Autowired
59      protected SaleService saleService;
60  
61      @Autowired
62      protected OperationService operationService;
63  
64      @Autowired
65      protected PhysicalGearService physicalGearService;
66  
67      @Autowired
68      protected MeasurementDao measurementDao;
69  
70  
71      @Override
72      public List<TripVO> getAllTrips(int offset, int size) {
73          return findByFilter(null, offset, size, null, null, DataFetchOptions.builder().build());
74      }
75  
76      @Override
77      public List<TripVO> findByFilter(TripFilterVO filter, int offset, int size) {
78          return findByFilter(filter, offset, size, null, null, DataFetchOptions.builder().build());
79      }
80  
81      @Override
82      public List<TripVO> findByFilter(TripFilterVO filter, int offset, int size, String sortAttribute,
83                                       SortDirection sortDirection, DataFetchOptions fieldOptions) {
84          if (filter == null) {
85              return tripDao.findAll(offset, size, sortAttribute, sortDirection, fieldOptions);
86          }
87  
88          return tripDao.findAll(filter, offset, size, sortAttribute, sortDirection, fieldOptions);
89      }
90  
91      @Override
92      public Long countByFilter(TripFilterVO filter) {
93          return tripDao.countByFilter(filter);
94      }
95  
96      @Override
97      public TripVO get(int tripId) {
98          return tripDao.get(tripId);
99      }
100 
101     @Override
102     public TripVOa/TripVO.html#TripVO">TripVO save(final TripVO source, final boolean withOperation) {
103         Preconditions.checkNotNull(source);
104         Preconditions.checkNotNull(source.getProgram(), "Missing program");
105         Preconditions.checkArgument(source.getProgram().getId() != null || source.getProgram().getLabel() != null, "Missing program.id or program.label");
106         Preconditions.checkNotNull(source.getDepartureDateTime(), "Missing departureDateTime");
107         Preconditions.checkNotNull(source.getDepartureLocation(), "Missing departureLocation");
108         Preconditions.checkNotNull(source.getDepartureLocation().getId(), "Missing departureLocation.id");
109         Preconditions.checkNotNull(source.getRecorderDepartment(), "Missing recorderDepartment");
110         Preconditions.checkNotNull(source.getRecorderDepartment().getId(), "Missing recorderDepartment.id");
111         Preconditions.checkNotNull(source.getVesselSnapshot(), "Missing vesselSnapshot");
112         Preconditions.checkNotNull(source.getVesselSnapshot().getId(), "Missing vesselSnapshot.id");
113         Preconditions.checkArgument(Objects.isNull(source.getSale()) || CollectionUtils.isEmpty(source.getSales()), "Must not have both 'sales' and 'sale' attributes");
114 
115         // Reset control date
116         source.setControlDate(null);
117 
118         // Save
119         TripVO savedTrip = tripDao.save(source);
120 
121         // Save sales
122         if (CollectionUtils.isNotEmpty(source.getSales())) {
123             List<SaleVO> sales = Beans.getList(source.getSales());
124             sales.forEach(g -> fillDefaultProperties(savedTrip, g));
125             sales = saleService.saveAllByTripId(savedTrip.getId(), sales);
126             savedTrip.setSales(sales);
127         } else if (source.getSale() != null) {
128             SaleVO sale = source.getSale();
129             fillDefaultProperties(savedTrip, sale);
130             List<SaleVO> sales = saleService.saveAllByTripId(savedTrip.getId(), ImmutableList.of(sale));
131             savedTrip.setSale(sales.get(0));
132         } else {
133             // Remove all
134             saleService.saveAllByTripId(savedTrip.getId(), ImmutableList.of());
135         }
136 
137         // Save physical gears
138         List<PhysicalGearVO> gears = Beans.getList(source.getGears());
139         gears.forEach(g -> fillDefaultProperties(savedTrip, g));
140         gears = physicalGearService.save(savedTrip.getId(), gears);
141         savedTrip.setGears(gears);
142 
143         // Save operations (only if asked)
144         if (withOperation) {
145             List<OperationVO> operations = Beans.getList(source.getOperations());
146             operations = operationService.saveAllByTripId(savedTrip.getId(), operations);
147             savedTrip.setOperations(operations);
148         }
149 
150         // Save measurements
151         if (source.getMeasurementValues() != null) {
152             measurementDao.saveTripMeasurementsMap(source.getId(), source.getMeasurementValues());
153         }
154         else {
155             List<MeasurementVO> measurements = Beans.getList(source.getMeasurements());
156             measurements.forEach(m -> fillDefaultProperties(savedTrip, m));
157             measurements = measurementDao.saveTripVesselUseMeasurements(savedTrip.getId(), measurements);
158             savedTrip.setMeasurements(measurements);
159         }
160 
161         return savedTrip;
162     }
163 
164     @Override
165     public List<TripVO> save(List<TripVO> trips, final boolean withOperation) {
166         Preconditions.checkNotNull(trips);
167 
168         return trips.stream()
169                 .map(t -> save(t, withOperation))
170                 .collect(Collectors.toList());
171     }
172 
173     @Override
174     public void delete(int id) {
175         tripDao.delete(id);
176     }
177 
178     @Override
179     public void delete(List<Integer> ids) {
180         Preconditions.checkNotNull(ids);
181         ids.stream()
182                 .filter(Objects::nonNull)
183                 .forEach(this::delete);
184     }
185 
186     @Override
187     public TripVO="../../../../../net/sumaris/core/vo/data/TripVO.html#TripVO">TripVO control(TripVO trip) {
188         Preconditions.checkNotNull(trip);
189         Preconditions.checkNotNull(trip.getId());
190         Preconditions.checkArgument(trip.getControlDate() == null);
191 
192         return tripDao.control(trip);
193     }
194 
195     @Override
196     public TripVO"../../../../../net/sumaris/core/vo/data/TripVO.html#TripVO">TripVO validate(TripVO trip) {
197         Preconditions.checkNotNull(trip);
198         Preconditions.checkNotNull(trip.getId());
199         Preconditions.checkNotNull(trip.getControlDate());
200         Preconditions.checkArgument(trip.getValidationDate() == null);
201 
202         return tripDao.validate(trip);
203     }
204 
205     @Override
206     public TripVO./../../../../net/sumaris/core/vo/data/TripVO.html#TripVO">TripVO unvalidate(TripVO trip) {
207         Preconditions.checkNotNull(trip);
208         Preconditions.checkNotNull(trip.getId());
209         Preconditions.checkNotNull(trip.getControlDate());
210         Preconditions.checkNotNull(trip.getValidationDate());
211 
212         return tripDao.unvalidate(trip);
213     }
214 
215     @Override
216     public TripVO="../../../../../net/sumaris/core/vo/data/TripVO.html#TripVO">TripVO qualify(TripVO trip) {
217         Preconditions.checkNotNull(trip);
218         Preconditions.checkNotNull(trip.getId());
219         Preconditions.checkNotNull(trip.getControlDate());
220         Preconditions.checkNotNull(trip.getValidationDate());
221         Preconditions.checkNotNull(trip.getQualityFlagId());
222 
223         return tripDao.qualify(trip);
224     }
225 
226     /* protected methods */
227 
228     void fillDefaultProperties(TripVO parent, SaleVO sale) {
229         if (sale == null) return;
230 
231         // Set default values from parent
232         DataBeans.setDefaultRecorderDepartment(sale, parent.getRecorderDepartment());
233         DataBeans.setDefaultRecorderPerson(sale, parent.getRecorderPerson());
234         DataBeans.setDefaultVesselFeatures(sale, parent.getVesselSnapshot());
235 
236         sale.setTripId(parent.getId());
237     }
238 
239     void fillDefaultProperties(TripVO parent, PhysicalGearVO gear) {
240         if (gear == null) return;
241 
242         // Copy program
243         gear.setProgram(parent.getProgram());
244 
245         // Copy recorder department from the parent trip
246         DataBeans.setDefaultRecorderDepartment(gear, parent.getRecorderDepartment());
247         DataBeans.setDefaultRecorderPerson(gear, parent.getRecorderPerson());
248 
249         gear.setTripId(parent.getId());
250     }
251 
252     void fillDefaultProperties(TripVO parent, MeasurementVO measurement) {
253         if (measurement == null) return;
254 
255         // Set default value for recorder department and person
256         DataBeans.setDefaultRecorderDepartment(measurement, parent.getRecorderDepartment());
257         DataBeans.setDefaultRecorderPerson(measurement, parent.getRecorderPerson());
258 
259         measurement.setEntityName(VesselUseMeasurement.class.getSimpleName());
260     }
261 }