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.ObservedLocationDao;
29  import net.sumaris.core.dao.technical.SortDirection;
30  import net.sumaris.core.model.data.ObservedLocationMeasurement;
31  import net.sumaris.core.util.Beans;
32  import net.sumaris.core.util.DataBeans;
33  import net.sumaris.core.vo.data.DataFetchOptions;
34  import net.sumaris.core.vo.data.MeasurementVO;
35  import net.sumaris.core.vo.data.ObservedLocationVO;
36  import net.sumaris.core.vo.filter.ObservedLocationFilterVO;
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("observedLocationService")
47  public class ObservedLocationServiceImpl implements ObservedLocationService {
48  
49  	private static final Logger log = LoggerFactory.getLogger(ObservedLocationServiceImpl.class);
50  
51  	@Autowired
52  	protected ObservedLocationDao observedLocationDao;
53  
54  	@Autowired
55  	protected MeasurementDao measurementDao;
56  
57  
58  	@Override
59  	public List<ObservedLocationVO> getAll(int offset, int size) {
60  		return findByFilter(null, offset, size, null, null, null);
61  	}
62  
63  	@Override
64  	public List<ObservedLocationVO> findByFilter(ObservedLocationFilterVO filter, int offset, int size) {
65  		return findByFilter(filter, offset, size, null, null, null);
66  	}
67  
68  	@Override
69  	public List<ObservedLocationVO> findByFilter(ObservedLocationFilterVO filter, int offset, int size, String sortAttribute,
70                                       SortDirection sortDirection, DataFetchOptions fetchOptions) {
71  		if (filter == null) {
72  			return observedLocationDao.getAll(offset, size, sortAttribute, sortDirection, fetchOptions);
73  		}
74  
75  		return observedLocationDao.findByFilter(filter, offset, size, sortAttribute, sortDirection, fetchOptions);
76  	}
77  
78  	@Override
79  	public Long countByFilter(ObservedLocationFilterVO filter) {
80  		return observedLocationDao.countByFilter(filter);
81  	}
82  
83  	@Override
84  	public ObservedLocationVO get(int observedLocationId) {
85  		return observedLocationDao.get(observedLocationId);
86  	}
87  
88  	@Override
89  	public ObservedLocationVOcationVO.html#ObservedLocationVO">ObservedLocationVO save(final ObservedLocationVO source, final boolean withObservedVessel) {
90  		Preconditions.checkNotNull(source);
91  		Preconditions.checkNotNull(source.getProgram(), "Missing program");
92  		Preconditions.checkArgument(source.getProgram().getId() != null || source.getProgram().getLabel() != null, "Missing program.id or program.label");
93  		Preconditions.checkNotNull(source.getStartDateTime(), "Missing startDateTime");
94  		Preconditions.checkNotNull(source.getLocation(), "Missing location");
95  		Preconditions.checkNotNull(source.getLocation().getId(), "Missing location.id");
96  		Preconditions.checkNotNull(source.getRecorderDepartment(), "Missing recorderDepartment");
97  		Preconditions.checkNotNull(source.getRecorderDepartment().getId(), "Missing recorderDepartment.id");
98  
99  		// Reset control date
100 		source.setControlDate(null);
101 
102 		// Save
103 		ObservedLocationVO savedObservedLocation = observedLocationDao.save(source);
104 
105 		// Save measurements
106 		if (savedObservedLocation.getMeasurementValues() != null) {
107 			measurementDao.saveObservedLocationMeasurementsMap(savedObservedLocation.getId(), savedObservedLocation.getMeasurementValues());
108 		}
109 		else {
110 			List<MeasurementVO> measurements = Beans.getList(savedObservedLocation.getMeasurements());
111 			measurements.forEach(m -> fillDefaultProperties(savedObservedLocation, m));
112 			measurements = measurementDao.saveObservedLocationMeasurements(savedObservedLocation.getId(), measurements);
113 			savedObservedLocation.setMeasurements(measurements);
114 		}
115 
116 		return savedObservedLocation;
117 	}
118 
119 	@Override
120 	public List<ObservedLocationVO> save(List<ObservedLocationVO> observedLocations, final boolean withObservedVessel) {
121 		Preconditions.checkNotNull(observedLocations);
122 
123 		return observedLocations.stream()
124 				.map(t -> save(t, withObservedVessel))
125 				.collect(Collectors.toList());
126 	}
127 
128 	@Override
129 	public void delete(int id) {
130 		observedLocationDao.delete(id);
131 	}
132 
133 	@Override
134 	public void delete(List<Integer> ids) {
135 		Preconditions.checkNotNull(ids);
136 		ids.stream()
137 				.filter(Objects::nonNull)
138 				.forEach(this::delete);
139 	}
140 
141 	@Override
142 	public ObservedLocationVO./../net/sumaris/core/vo/data/ObservedLocationVO.html#ObservedLocationVO">ObservedLocationVO control(ObservedLocationVO observedLocation) {
143 		Preconditions.checkNotNull(observedLocation);
144 		Preconditions.checkNotNull(observedLocation.getId());
145 		Preconditions.checkArgument(observedLocation.getControlDate() == null);
146 
147 		return observedLocationDao.control(observedLocation);
148 	}
149 
150 	@Override
151 	public ObservedLocationVO/../net/sumaris/core/vo/data/ObservedLocationVO.html#ObservedLocationVO">ObservedLocationVO validate(ObservedLocationVO observedLocation) {
152 		Preconditions.checkNotNull(observedLocation);
153 		Preconditions.checkNotNull(observedLocation.getId());
154 		Preconditions.checkNotNull(observedLocation.getControlDate());
155 		Preconditions.checkArgument(observedLocation.getValidationDate() == null);
156 
157 		return observedLocationDao.validate(observedLocation);
158 	}
159 
160 	@Override
161 	public ObservedLocationVO./net/sumaris/core/vo/data/ObservedLocationVO.html#ObservedLocationVO">ObservedLocationVO unvalidate(ObservedLocationVO observedLocation) {
162 		Preconditions.checkNotNull(observedLocation);
163 		Preconditions.checkNotNull(observedLocation.getId());
164 		Preconditions.checkNotNull(observedLocation.getControlDate());
165 		Preconditions.checkNotNull(observedLocation.getValidationDate());
166 
167 		return observedLocationDao.unvalidate(observedLocation);
168 	}
169 
170 	/* protected methods */
171 
172 	void fillDefaultProperties(ObservedLocationVO parent, MeasurementVO measurement) {
173 		if (measurement == null) return;
174 
175 		// Set default value for recorder department and person
176 		DataBeans.setDefaultRecorderDepartment(measurement, parent.getRecorderDepartment());
177 		DataBeans.setDefaultRecorderPerson(measurement, parent.getRecorderPerson());
178 
179 		measurement.setEntityName(ObservedLocationMeasurement.class.getSimpleName());
180 	}
181 }