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 net.sumaris.core.dao.data.MeasurementDao;
28  import net.sumaris.core.dao.data.VesselDao;
29  import net.sumaris.core.dao.data.VesselSnapshotDao;
30  import net.sumaris.core.dao.technical.SortDirection;
31  import net.sumaris.core.model.data.VesselPhysicalMeasurement;
32  import net.sumaris.core.util.Beans;
33  import net.sumaris.core.util.DataBeans;
34  import net.sumaris.core.vo.data.*;
35  import net.sumaris.core.vo.filter.VesselFilterVO;
36  import org.apache.commons.lang3.StringUtils;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  import org.springframework.beans.factory.annotation.Autowired;
40  import org.springframework.stereotype.Service;
41  
42  import java.util.ArrayList;
43  import java.util.Date;
44  import java.util.List;
45  import java.util.Objects;
46  import java.util.stream.Collectors;
47  
48  @Service("vesselService")
49  public class VesselServiceImpl implements VesselService {
50  
51  	private static final Logger log = LoggerFactory.getLogger(VesselServiceImpl.class);
52  
53  	@Autowired
54  	protected VesselDao vesselDao;
55  
56  	@Autowired
57  	protected VesselSnapshotDao vesselSnapshotDao;
58  
59  	@Autowired
60  	protected MeasurementDao measurementDao;
61  
62  
63  	@Override
64  	public List<VesselSnapshotVO> findSnapshotByFilter(VesselFilterVO filter, int offset, int size, String sortAttribute, SortDirection sortDirection) {
65  		return vesselSnapshotDao.findByFilter(filter, offset, size, sortAttribute, sortDirection);
66  	}
67  
68  	@Override
69  	public List<VesselFeaturesVO> getFeaturesByVesselId(int vesselId, int offset, int size, String sortAttribute, SortDirection sortDirection) {
70  		return vesselDao.getFeaturesByVesselId(vesselId, offset, size, sortAttribute, sortDirection);
71  	}
72  
73  	@Override
74  	public List<VesselRegistrationVO> getRegistrationsByVesselId(int vesselId, int offset, int size, String sortAttribute, SortDirection sortDirection) {
75  		return vesselDao.getRegistrationsByVesselId(vesselId, offset, size, sortAttribute, sortDirection);
76  	}
77  
78  	@Override
79  	public VesselSnapshotVO getSnapshotByIdAndDate(int vesselId, Date date) {
80  		return vesselSnapshotDao.getByIdAndDate(vesselId, date);
81  	}
82  
83  	@Override
84  	public List<VesselVO> findVesselsByFilter(VesselFilterVO filter, int offset, int size, String sortAttribute, SortDirection sortDirection) {
85  		return vesselDao.findByFilter(filter, offset, size, sortAttribute, sortDirection);
86  	}
87  
88  	@Override
89  	public Long countVesselsByFilter(VesselFilterVO filter) {
90  		return vesselDao.countByFilter(filter);
91  	}
92  
93  	@Override
94  	public VesselVO getVesselById(int vesselId) {
95  		return vesselDao.get(vesselId);
96  	}
97  
98  	@Override
99  	public VesselVOf="../../../../../net/sumaris/core/vo/data/VesselVO.html#VesselVO">VesselVO save(VesselVO source) {
100 		return save(source, true);
101 	}
102 
103 	@Override
104 	public VesselVOf="../../../../../net/sumaris/core/vo/data/VesselVO.html#VesselVO">VesselVO save(VesselVO source, boolean checkUpdateDate) {
105 		Preconditions.checkNotNull(source);
106 		Preconditions.checkNotNull(source.getRecorderDepartment(), "Missing recorderDepartment");
107 		Preconditions.checkNotNull(source.getRecorderDepartment().getId(), "Missing recorderDepartment.id");
108 		Preconditions.checkNotNull(source.getVesselType(), "Missing vesselId or vesselTypeId");
109 
110 		if (source.getFeatures() != null) {
111 			Preconditions.checkNotNull(source.getFeatures().getBasePortLocation().getId(), "Missing basePortLocation.id");
112 			Preconditions.checkNotNull(source.getFeatures().getStartDate(), "Missing start date");
113 			Preconditions.checkArgument(StringUtils.isNotBlank(source.getFeatures().getExteriorMarking()), "Missing exterior marking");
114 		}
115 
116 		if (source.getRegistration() != null) {
117 			Preconditions.checkArgument(StringUtils.isNotBlank(source.getRegistration().getRegistrationCode()), "Missing registration code");
118 			Preconditions.checkNotNull(source.getRegistration().getRegistrationLocation().getId(), "Missing registration location");
119 		}
120 
121 		VesselVO savedVessel = vesselDao.save(source, checkUpdateDate);
122 
123 		if (savedVessel.getFeatures() != null) {
124 			VesselFeaturesVO savedVesselFeatures = savedVessel.getFeatures();
125 			// Save measurements
126 			if (savedVesselFeatures.getMeasurementValues() != null) {
127 				measurementDao.saveVesselPhysicalMeasurementsMap(savedVesselFeatures.getId(), savedVesselFeatures.getMeasurementValues());
128 			} else {
129 				List<MeasurementVO> measurements = Beans.getList(savedVesselFeatures.getMeasurements());
130 				measurements.forEach(m -> fillDefaultProperties(savedVesselFeatures, m));
131 				measurements = measurementDao.saveVesselPhysicalMeasurements(savedVesselFeatures.getId(), measurements);
132 				savedVesselFeatures.setMeasurements(measurements);
133 			}
134 		}
135 
136 		return savedVessel;
137 	}
138 
139 	@Override
140 	public List<VesselVO> save(List<VesselVO> sources) {
141 		Preconditions.checkNotNull(sources);
142 
143 		// special case if a vessel is saved twice: it means a features or registration change
144 		if (sources.size() == 2 && Objects.equals(sources.get(0).getId(), sources.get(1).getId())) {
145 			List<VesselVO> result = new ArrayList<>();
146 			// save the first vo normally
147 			result.add(save(sources.get(0)));
148 			// save the second without checking update date
149 			result.add(save(sources.get(1), false));
150 			return result;
151 		}
152 
153 		return sources.stream()
154 				.map(this::save)
155 				.collect(Collectors.toList());
156 	}
157 
158 	@Override
159 	public void delete(int id) {
160 		vesselDao.delete(id);
161 	}
162 
163 	@Override
164 	public void delete(List<Integer> ids) {
165 		Preconditions.checkNotNull(ids);
166 		ids.stream()
167 				.filter(Objects::nonNull)
168 				.forEach(this::delete);
169 	}
170 
171 	/* protected methods */
172 
173 	void fillDefaultProperties(VesselFeaturesVO parent, MeasurementVO measurement) {
174 		if (measurement == null) return;
175 
176 		// Set default value for recorder department and person
177 		DataBeans.setDefaultRecorderDepartment(measurement, parent.getRecorderDepartment());
178 		DataBeans.setDefaultRecorderPerson(measurement, parent.getRecorderPerson());
179 
180 		measurement.setEntityName(VesselPhysicalMeasurement.class.getSimpleName());
181 	}
182 
183 }