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.config.SumarisConfiguration;
28  import net.sumaris.core.dao.data.MeasurementDao;
29  import net.sumaris.core.dao.data.SampleDao;
30  import net.sumaris.core.util.Beans;
31  import net.sumaris.core.model.data.IMeasurementEntity;
32  import net.sumaris.core.model.data.SampleMeasurement;
33  import net.sumaris.core.vo.data.MeasurementVO;
34  import net.sumaris.core.vo.data.SampleVO;
35  import net.sumaris.core.vo.referential.ReferentialVO;
36  import org.apache.commons.collections4.CollectionUtils;
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.List;
43  import java.util.Objects;
44  import java.util.stream.Collectors;
45  
46  @Service("sampleService")
47  public class SampleServiceImpl implements SampleService {
48  
49  	private static final Logger log = LoggerFactory.getLogger(SampleServiceImpl.class);
50  
51  	@Autowired
52  	protected SumarisConfiguration config;
53  
54  	@Autowired
55  	protected SampleDao sampleDao;
56  
57  	@Autowired
58  	protected MeasurementDao measurementDao;
59  
60  	@Override
61  	public List<SampleVO> getAllByOperationId(int tripId) {
62  		return sampleDao.getAllByOperationId(tripId);
63  	}
64  
65  	@Override
66  	public List<SampleVO> getAllByLandingId(int landingId) {
67  		return sampleDao.getAllByLandingId(landingId);
68  	}
69  
70  	@Override
71  	public SampleVO get(int saleId) {
72  		return sampleDao.get(saleId);
73  	}
74  
75  	@Override
76  	public List<SampleVO> saveByOperationId(int operationId, List<SampleVO> sources) {
77  
78  		List<SampleVO> result = sampleDao.saveByOperationId(operationId, sources);
79  
80  		// Save measurements
81  		saveMeasurements(result);
82  
83  		return result;
84  	}
85  
86  	@Override
87  	public List<SampleVO> saveByLandingId(int landingId, List<SampleVO> sources) {
88  		List<SampleVO> result = sampleDao.saveByLandingId(landingId, sources);
89  
90  		// Save measurements
91  		saveMeasurements(result);
92  
93  		return result;
94  	}
95  
96  	@Override
97  	public SampleVOf="../../../../../net/sumaris/core/vo/data/SampleVO.html#SampleVO">SampleVO save(SampleVO sample) {
98  		Preconditions.checkNotNull(sample);
99  		Preconditions.checkArgument((sample.getOperation() != null && sample.getOperation().getId() != null) || sample.getOperationId() != null, "Missing sample.operation or sample.operationId");
100 		Preconditions.checkNotNull(sample.getRecorderDepartment(), "Missing sample.recorderDepartment");
101 		Preconditions.checkNotNull(sample.getRecorderDepartment().getId(), "Missing sample.recorderDepartment.id");
102 
103 		return sampleDao.save(sample);
104 	}
105 
106 	@Override
107 	public List<SampleVO> save(List<SampleVO> sales) {
108 		Preconditions.checkNotNull(sales);
109 
110 		return sales.stream()
111 				.map(this::save)
112 				.collect(Collectors.toList());
113 	}
114 
115 	@Override
116 	public void delete(int id) {
117 		sampleDao.delete(id);
118 	}
119 
120 	@Override
121 	public void delete(List<Integer> ids) {
122 		Preconditions.checkNotNull(ids);
123 		ids.stream()
124 				.filter(Objects::nonNull)
125 				.forEach(this::delete);
126 	}
127 
128 
129 	/**
130 	 * Transform a samples (with children) into a falt list, sorted with parent always before children
131 	 * @param sample
132 	 * @param result
133 	 */
134 	@Override
135 	public void treeToList(final SampleVO sample, final List<SampleVO> result) {
136 		if (sample == null) return;
137 
138 		// Add the batch itself
139 		if (!result.contains(sample)) result.add(sample);
140 
141 		// Process children
142 		if (CollectionUtils.isNotEmpty(sample.getChildren())) {
143 			// Recursive call
144 			sample.getChildren().forEach(child -> {
145 				fillDefaultProperties(sample, child);
146 				// Link to parent
147 				child.setParent(sample);
148 				treeToList(child, result);
149 			});
150 		}
151 
152 	}
153 	/* -- protected methods -- */
154 
155 	protected void saveMeasurements(List<SampleVO> result) {
156 		result.forEach(savedSample -> {
157 			if (savedSample.getMeasurementValues() != null) {
158 				measurementDao.saveSampleMeasurementsMap(savedSample.getId(), savedSample.getMeasurementValues());
159 			}
160 			else {
161 				List<MeasurementVO> measurements = Beans.getList(savedSample.getMeasurements());
162 				measurements.forEach(m -> fillDefaultProperties(savedSample, m, SampleMeasurement.class));
163 				measurements = measurementDao.saveSampleMeasurements(savedSample.getId(), measurements);
164 				savedSample.setMeasurements(measurements);
165 			}
166 		});
167 	}
168 
169 	protected void fillDefaultProperties(SampleVO parent, MeasurementVO measurement, Class<? extends IMeasurementEntity> entityClass) {
170 		if (measurement == null) return;
171 
172 		// Copy recorder department from the parent
173 		if (measurement.getRecorderDepartment() == null || measurement.getRecorderDepartment().getId() == null) {
174 			measurement.setRecorderDepartment(parent.getRecorderDepartment());
175 		}
176 
177 		measurement.setEntityName(entityClass.getSimpleName());
178 	}
179 
180 	protected void fillDefaultProperties(SampleVO../../../../../net/sumaris/core/vo/data/SampleVO.html#SampleVO">SampleVO parent, SampleVO sample) {
181 		if (sample == null) return;
182 
183 		// Copy recorder department from the parent
184 		if (sample.getRecorderDepartment() == null || sample.getRecorderDepartment().getId() == null) {
185 			sample.setRecorderDepartment(parent.getRecorderDepartment());
186 		}
187 
188 		// Fill matrix
189 		if (sample.getMatrix() == null || sample.getMatrix().getId() == null) {
190 			ReferentialVO matrix = new ReferentialVO();
191 			matrix.setId(config.getMatrixIdIndividual());
192 			sample.setMatrix(matrix);
193 		}
194 
195 		// Fill sample (use operation end date time)
196 		if (sample.getSampleDate() == null) {
197 			sample.setSampleDate(parent.getSampleDate());
198 		}
199 
200 		sample.setParentId(parent.getId());
201 		sample.setOperationId(parent.getOperationId());
202 	}
203 }