View Javadoc
1   package fr.ifremer.quadrige2.core.dao.administration.strategy;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Server Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (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 Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  import com.google.common.base.Function;
26  import com.google.common.base.Preconditions;
27  import com.google.common.collect.Lists;
28  import fr.ifremer.quadrige2.core.dao.referential.AcquisitionLevel;
29  import fr.ifremer.quadrige2.core.dao.referential.AcquisitionLevelImpl;
30  import fr.ifremer.quadrige2.core.dao.referential.PrecisionTypeImpl;
31  import fr.ifremer.quadrige2.core.dao.referential.pmfm.PmfmImpl;
32  import fr.ifremer.quadrige2.core.exception.BadUpdateDtException;
33  import fr.ifremer.quadrige2.core.dao.technical.Daos;
34  import fr.ifremer.quadrige2.core.dao.technical.Dates;
35  import fr.ifremer.quadrige2.core.vo.administration.strategy.PmfmStrategyVO;
36  import org.apache.commons.collections4.CollectionUtils;
37  import org.apache.commons.lang3.ArrayUtils;
38  import org.hibernate.SessionFactory;
39  import org.nuiton.i18n.I18n;
40  import org.springframework.beans.factory.annotation.Autowired;
41  import org.springframework.context.annotation.Lazy;
42  import org.springframework.stereotype.Repository;
43  
44  import javax.annotation.Nullable;
45  import java.sql.Timestamp;
46  import java.util.List;
47  import java.util.Objects;
48  
49  /**
50   * <p>
51   * PmfmStrategyDaoImpl class.
52   * </p>
53   * 
54   * @see PmfmStrategy
55   */
56  @Repository("pmfmStrategyDao")
57  @Lazy
58  public class PmfmStrategyDaoImpl
59  		extends PmfmStrategyDaoBase
60  {
61  	/**
62  	 * Constructor used by Spring
63  	 * 
64  	 * @param sessionFactory
65  	 *            a {@link org.hibernate.SessionFactory} object.
66  	 */
67  	@Autowired
68  	public PmfmStrategyDaoImpl(SessionFactory sessionFactory) {
69  		super();
70  		setSessionFactory(sessionFactory);
71  	}
72  
73  	/** {@inheritDoc} */
74  	public void toPmfmStrategyVO(
75  			PmfmStrategy source,
76  			PmfmStrategyVO target)
77  	{
78  		super.toPmfmStrategyVO(source, target);
79  
80  		// Strategy
81  		if (source.getStrategy() == null) {
82  			target.setStratId(null);
83  		}
84  		else {
85  			target.setStratId(source.getStrategy().getStratId());
86  		}
87  
88  		// Pmfm
89  		if (source.getPmfm() == null) {
90  			target.setPmfmId(null);
91  		}
92  		else {
93  			target.setPmfmId(source.getPmfm().getPmfmId());
94  		}
95  
96  		// Precision type
97  		if (source.getPrecisionType() == null) {
98  			target.setPrecTypeId(null);
99  		}
100 		else {
101 			target.setPrecTypeId(source.getPrecisionType().getPrecTypeId());
102 		}
103 
104 		// Acquisition levels
105 		if (CollectionUtils.isEmpty(source.getAcquisitionLevels())) {
106 			target.setAcquisLevelCds(null);
107 		}
108 		else {
109 			List<String> acquisLevelCds = Lists.transform(Lists.newArrayList(source.getAcquisitionLevels()),
110 					new Function<AcquisitionLevel, String>() {
111 						@Nullable
112 						@Override
113 						public String apply(AcquisitionLevel source) {
114 							return source.getAcquisLevelCd();
115 						}
116 					});
117 			target.setAcquisLevelCds(acquisLevelCds.toArray(new String[acquisLevelCds.size()]));
118 		}
119 
120 		// UI functions
121 		if (CollectionUtils.isEmpty(source.getUiFunctions())) {
122 			target.setUiFunctionCds(null);
123 		}
124 		else {
125 			List<String> uiFunctionCds = Lists.transform(Lists.newArrayList(source.getUiFunctions()),
126 					new Function<UiFunction, String>() {
127 						@Nullable
128 						@Override
129 						public String apply(UiFunction source) {
130 							return source.getUiFunctionCd();
131 						}
132 					});
133 			target.setUiFunctionCds(uiFunctionCds.toArray(new String[uiFunctionCds.size()]));
134 		}
135 	}
136 
137 	/**
138 	 * Retrieves the entity object that is associated with the specified value object
139 	 * from the object store. If no such entity object exists in the object store,
140 	 * a new, blank entity is created
141 	 */
142 	private PmfmStrategy loadPmfmStrategyFromPmfmStrategyVO(PmfmStrategyVO pmfmStrategyVO)
143 	{
144 		PmfmStrategy pmfmStrategy = null;
145 		if (pmfmStrategyVO.getPmfmStratId() != null) {
146 			pmfmStrategy = this.get(pmfmStrategyVO.getPmfmStratId());
147 		}
148 		if (pmfmStrategy == null)
149 		{
150 			pmfmStrategy = PmfmStrategy.Factory.newInstance();
151 		}
152 		return pmfmStrategy;
153 	}
154 
155 	/** {@inheritDoc} */
156 	public PmfmStrategy pmfmStrategyVOToEntity(PmfmStrategyVO pmfmStrategyVO)
157 	{
158 		PmfmStrategy entity = this.loadPmfmStrategyFromPmfmStrategyVO(pmfmStrategyVO);
159 		this.pmfmStrategyVOToEntity(pmfmStrategyVO, entity, true);
160 		return entity;
161 	}
162 
163 	/** {@inheritDoc} */
164 	@Override
165 	public void pmfmStrategyVOToEntity(
166 			PmfmStrategyVO source,
167 			PmfmStrategy target,
168 			boolean copyIfNull)
169 	{
170 		super.pmfmStrategyVOToEntity(source, target, copyIfNull);
171 
172 		// Strategy
173 		if (copyIfNull || source.getStratId() != null || source.getStrategyVO() != null) {
174 			if (source.getStratId() == null && source.getStrategyVO() == null) {
175 				target.setStrategy(null);
176 			}
177 			else {
178 				Integer stratId = source.getStratId();
179 				if (stratId == null) {
180 					stratId = source.getStrategyVO().getStratId();
181 				}
182 				target.setStrategy(load(StrategyImpl.class, stratId));
183 			}
184 		}
185 
186 		// Pmfm
187 		if (copyIfNull || source.getPmfmId() != null) {
188 			if (source.getPmfmId() == null) {
189 				target.setPmfm(null);
190 			}
191 			else {
192 				target.setPmfm(load(PmfmImpl.class, source.getPmfmId()));
193 			}
194 		}
195 
196 		// Precision type
197 		if (copyIfNull || source.getPrecTypeId() != null) {
198 			if (source.getPrecTypeId() == null) {
199 				target.setPrecisionType(null);
200 			}
201 			else {
202 				target.setPrecisionType(load(PrecisionTypeImpl.class, source.getPrecTypeId()));
203 			}
204 		}
205 
206 		// Acquisition levels
207 		if (copyIfNull || ArrayUtils.isNotEmpty(source.getAcquisLevelCds())) {
208 			if (ArrayUtils.isEmpty(source.getAcquisLevelCds())) {
209 				target.getAcquisitionLevels().clear();
210 			}
211 			else {
212 				Daos.replaceEntities(
213 						target.getAcquisitionLevels(),
214 						source.getAcquisLevelCds(),
215 						acquisLevelCd -> load(AcquisitionLevelImpl.class, acquisLevelCd));
216 			}
217 		}
218 
219 		// UI functions
220 		if (copyIfNull || ArrayUtils.isNotEmpty(source.getUiFunctionCds())) {
221 			if (ArrayUtils.isEmpty(source.getUiFunctionCds())) {
222 				target.getUiFunctions().clear();
223 			}
224 			else {
225 				Daos.replaceEntities(
226 						target.getUiFunctions(),
227 						source.getUiFunctionCds(),
228 						uiFunctionCd -> load(UiFunctionImpl.class, uiFunctionCd));
229 			}
230 		}
231 
232 	}
233 
234 	/** {@inheritDoc} */
235 	@Override
236 	protected PmfmStrategyVO handleSave(PmfmStrategyVO source, Timestamp updateDt) throws Exception {
237 		Preconditions.checkNotNull(source);
238 		Preconditions.checkNotNull(source.getStratId());
239 
240 		// Load parent
241 		Strategy parent = get(StrategyImpl.class, source.getStratId());
242 
243 		// Load entity
244 		PmfmStrategy entity = null;
245 		boolean isNew = false;
246 		if (source.getPmfmStratId() != null) {
247 			entity = get(source.getPmfmStratId());
248 		}
249 		if (entity == null) {
250 			entity = PmfmStrategy.Factory.newInstance();
251 			parent.addPmfmStrategies(entity);
252 			entity.setStrategy(parent);
253 			isNew = true;
254 		}
255 
256 		// Check update_dt
257 		if (!isNew) {
258 			if (entity.getUpdateDt() != null) {
259 				Timestamp serverUpdateDtNoMillisecond = Dates.resetMillisecond(entity.getUpdateDt());
260 				Timestamp sourceUpdateDtNoMillisecond = Dates.resetMillisecond(source.getUpdateDt());
261 				if (!Objects.equals(sourceUpdateDtNoMillisecond, serverUpdateDtNoMillisecond)) {
262 					throw new BadUpdateDtException(I18n.t("quadrige2.dao.pmfmStrategy.badUpdateDt", source.getStratId(), serverUpdateDtNoMillisecond,
263 							sourceUpdateDtNoMillisecond));
264 				}
265 			}
266 		}
267 
268 		// update update_dt
269 		Timestamp newUpdateDt = updateDt;
270 		if (newUpdateDt == null) {
271 			newUpdateDt = getDatabaseCurrentTimestamp();
272 		}
273 		source.setUpdateDt(newUpdateDt);
274 
275 		// VO -> Entity
276 		pmfmStrategyVOToEntity(source, entity, true);
277 
278 		// Save entity
279 		if (isNew) {
280 			Integer pmfmStratId = (Integer) getSession().save(entity);
281 			source.setPmfmStratId(pmfmStratId);
282 		} else {
283 			getSession().update(entity);
284 		}
285 
286 		return source;
287 	}
288 }