View Javadoc
1   // Generated by: hibernate/SpringHibernateDaoImpl.vsl in andromda-spring-cartridge.
2   // license-header java merge-point
3   /**
4    * This is only generated once! It will never be overwritten.
5    * You can (and have to!) safely modify it by hand.
6    */
7   package fr.ifremer.quadrige2.core.dao.system.rule;
8   
9   /*-
10   * #%L
11   * Quadrige2 Core :: Quadrige2 Server Core
12   * %%
13   * Copyright (C) 2017 Ifremer
14   * %%
15   * This program is free software: you can redistribute it and/or modify
16   * it under the terms of the GNU Affero General Public License as published by
17   * the Free Software Foundation, either version 3 of the License, or
18   * (at your option) any later version.
19   * 
20   * This program is distributed in the hope that it will be useful,
21   * but WITHOUT ANY WARRANTY; without even the implied warranty of
22   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   * GNU General Public License for more details.
24   * 
25   * You should have received a copy of the GNU Affero General Public License
26   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27   * #L%
28   */
29  
30  import com.google.common.base.Preconditions;
31  import fr.ifremer.quadrige2.core.dao.system.synchronization.DeletedItemHistoryExtendDao;
32  import fr.ifremer.quadrige2.core.dao.technical.Beans;
33  import fr.ifremer.quadrige2.core.dao.technical.Daos;
34  import fr.ifremer.quadrige2.core.dao.technical.Dates;
35  import fr.ifremer.quadrige2.core.exception.BadUpdateDtException;
36  import fr.ifremer.quadrige2.core.vo.system.rule.RuleParameterVO;
37  import fr.ifremer.quadrige2.core.vo.system.rule.RulePmfmVO;
38  import fr.ifremer.quadrige2.core.vo.system.rule.RuleVO;
39  import org.apache.commons.collections4.CollectionUtils;
40  import org.apache.commons.collections4.MapUtils;
41  import org.apache.commons.lang3.ArrayUtils;
42  import org.hibernate.SessionFactory;
43  import org.nuiton.i18n.I18n;
44  import org.springframework.beans.factory.annotation.Autowired;
45  import org.springframework.context.annotation.Lazy;
46  import org.springframework.stereotype.Repository;
47  
48  import javax.annotation.Resource;
49  import java.sql.Timestamp;
50  import java.util.Collection;
51  import java.util.Map;
52  import java.util.Objects;
53  
54  /**
55   * @see Rule
56   */
57  @Repository("ruleDao")
58  @Lazy
59  public class RuleDaoImpl
60      extends RuleDaoBase
61  {
62  
63      @Resource
64      private RuleParameterDao ruleParameterDao;
65  
66      @Resource
67      private RulePmfmDao rulePmfmDao;
68  
69      @Resource(name = "deletedItemHistoryDao")
70      private DeletedItemHistoryExtendDao deletedItemHistoryDao;
71  
72      /**
73       * Constructor used by Spring
74       */
75  	@Autowired
76  	public RuleDaoImpl(SessionFactory sessionFactory) {
77  		super();
78  		setSessionFactory(sessionFactory);
79  	}
80  
81      /** {@inheritDoc} */
82      @Override
83      public void remove(Collection<Rule> entities) {
84          Preconditions.checkNotNull(entities);
85          Preconditions.checkArgument(entities.size() > 0);
86  
87          for (Rule entity : entities) {
88              remove(entity);
89          }
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public void remove(Rule entity) {
95  
96          // Remove rule parameters
97          if (CollectionUtils.isNotEmpty(entity.getRuleParameters())) {
98              ruleParameterDao.remove(entity.getRuleParameters());
99              entity.getRuleParameters().clear();
100         }
101 
102         getSession().flush();
103 
104         // Remove rule pmfm
105         if (CollectionUtils.isNotEmpty(entity.getRulePmfms())) {
106             rulePmfmDao.remove(entity.getRulePmfms());
107             entity.getRulePmfms().clear();
108         }
109 
110         getSession().flush();
111 
112         // Insert into DeleteItemHistory
113         deletedItemHistoryDao.insertDeletedItem(RuleImpl.class, entity);
114 
115         super.remove(entity);
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     protected RuleVO handleSave(RuleVO source, Timestamp updateDt) {
122         Preconditions.checkNotNull(source);
123         Preconditions.checkNotNull(source.getRuleCd());
124 
125         // Load parent
126         RuleList parent = get(RuleListImpl.class, source.getRuleListCd());
127 
128         // Load entity
129         Rule entity = get(source.getRuleCd());
130         boolean isNew = false;
131         if (entity == null) {
132             entity = Rule.Factory.newInstance();
133             parent.addRules(entity);
134             entity.setRuleList(parent);
135             isNew = true;
136         }
137 
138         // Check update_dt
139         if (!isNew) {
140             if (entity.getUpdateDt() != null) {
141                 Timestamp serverUpdateDtNoMillisecond = Dates.resetMillisecond(entity.getUpdateDt());
142                 if (source.getUpdateDt() == null) {
143                     throw new BadUpdateDtException(I18n.t("quadrige2.dao.rule.badUpdateDt", source.getRuleListCd(),
144                             serverUpdateDtNoMillisecond,
145                             "null"));
146                 }
147                 Timestamp sourceUpdateDtNoMillisecond = Dates.resetMillisecond(source.getUpdateDt());
148                 if (!Objects.equals(sourceUpdateDtNoMillisecond, serverUpdateDtNoMillisecond)) {
149                     throw new BadUpdateDtException(I18n.t("quadrige2.dao.rule.badUpdateDt", source.getRuleCd(), serverUpdateDtNoMillisecond,
150                             sourceUpdateDtNoMillisecond));
151                 }
152             }
153         }
154 
155         // update update_dt
156         Timestamp newUpdateDt = updateDt;
157         if (newUpdateDt == null) {
158             newUpdateDt = getDatabaseCurrentTimestamp();
159         }
160         source.setUpdateDt(newUpdateDt);
161 
162         // VO -> Entity
163         ruleVOToEntity(source, entity, true, false /*children not need*/);
164 
165         // Save entity
166         if (isNew) {
167             getSession().save(entity);
168         } else {
169             getSession().update(entity);
170         }
171 
172         // Keep trace of items to remove
173         Map<Integer, RuleParameter> ruleParametersToRemove = Beans.splitByProperty(entity.getRuleParameters(), "ruleParId");
174         Map<Integer, RulePmfm> rulePmfmsToRemove = Beans.splitByProperty(entity.getRulePmfms(), "rulePmfmId");
175 
176         // Save rule parameters
177         if (ArrayUtils.isNotEmpty(source.getRuleParameterVOs())) {
178             for (RuleParameterVO ruleParameterVO : source.getRuleParameterVOs()) {
179                 ruleParameterVO.setRuleCd(entity.getRuleCd());
180                 ruleParameterVO = ruleParameterDao.save(ruleParameterVO, newUpdateDt);
181                 ruleParametersToRemove.remove(ruleParameterVO.getRuleParId());
182             }
183         }
184 
185         // Save rule pmfm
186         if (ArrayUtils.isNotEmpty(source.getRulePmfmVOs())) {
187             for (RulePmfmVO rulePmfmVO : source.getRulePmfmVOs()) {
188                 rulePmfmVO.setRuleCd(entity.getRuleCd());
189                 rulePmfmVO = rulePmfmDao.save(rulePmfmVO, newUpdateDt);
190                 rulePmfmsToRemove.remove(rulePmfmVO.getRulePmfmId());
191             }
192         }
193 
194         // Remove unused rule parameters
195         if (MapUtils.isNotEmpty(ruleParametersToRemove)) {
196             ruleParameterDao.remove(ruleParametersToRemove.values());
197             entity.getRuleParameters().removeAll(ruleParametersToRemove.values());
198         }
199 
200         // Remove unused rule pmfm
201         if (MapUtils.isNotEmpty(rulePmfmsToRemove)) {
202             rulePmfmDao.remove(rulePmfmsToRemove.values());
203             entity.getRulePmfms().removeAll(rulePmfmsToRemove.values());
204         }
205 
206         return source;
207     }
208 
209     /**
210      * {@inheritDoc}
211      */
212     protected void handleRemoveByCds(String[] ruleCds) {
213         for (String ruleCd: ruleCds) {
214             remove(ruleCd);
215         }
216     }
217 
218     /**
219      * {@inheritDoc}
220      */
221     public void toRuleVO(
222         Rule source,
223         RuleVO target)
224     {
225         // base attributes
226         super.toRuleVO(source, target);
227 
228         // Parent
229         if (source.getRuleList() == null) {
230             target.setRuleListCd(null);
231         }
232         else {
233             target.setRuleListCd(source.getRuleList().getRuleListCd());
234         }
235 
236         // Function
237         if (source.getFunction() == null) {
238             target.setFunctionId(null);
239         } else {
240             target.setFunctionId(source.getFunction().getFunctionId());
241         }
242 
243         // Rules parameters
244         if (CollectionUtils.isEmpty(source.getRuleParameters())) {
245             target.setRuleParameterVOs(null);
246         }
247         else {
248             target.setRuleParameterVOs(ruleParameterDao.toRuleParameterVOArray(source.getRuleParameters()));
249         }
250 
251         // Rules pmfm
252         if (CollectionUtils.isEmpty(source.getRulePmfms())) {
253             target.setRulePmfmVOs(null);
254         }
255         else {
256             target.setRulePmfmVOs(rulePmfmDao.toRulePmfmVOArray(source.getRulePmfms()));
257         }
258     }
259 
260     /**
261      * Retrieves the entity object that is associated with the specified value object
262      * from the object store. If no such entity object exists in the object store,
263      * a new, blank entity is created
264      */
265     private Rule loadRuleFromRuleVO(RuleVO ruleVO)
266     {
267         Rule rule = null;
268         if (ruleVO.getRuleCd() != null) {
269             rule = this.get(ruleVO.getRuleCd());
270         }
271         if (rule == null)
272         {
273             rule = Rule.Factory.newInstance();
274         }
275         return rule;
276     }
277 
278     /**
279      * {@inheritDoc}
280      */
281     public Rule ruleVOToEntity(RuleVO ruleVO)
282     {
283         Rule entity = this.loadRuleFromRuleVO(ruleVO);
284         this.ruleVOToEntity(ruleVO, entity, true);
285         return entity;
286     }
287 
288     /**
289      * {@inheritDoc}
290      */
291     @Override
292     public void ruleVOToEntity(
293         RuleVO source,
294         Rule target,
295         boolean copyIfNull)
296     {
297         this.ruleVOToEntity(source, target, copyIfNull, false/*withChildrenEntities*/);
298     }
299 
300 
301     /* -- protected methods -- */
302 
303     protected void ruleVOToEntity(
304             RuleVO source,
305             Rule target,
306             boolean copyIfNull,
307             boolean withChildrenEntities)
308     {
309         // Base attributes
310         super.ruleVOToEntity(source, target, copyIfNull);
311 
312         // Code
313         if (copyIfNull || source.getRuleCd() != null)
314         {
315             target.setRuleCd(source.getRuleCd());
316         }
317 
318         // Rule list
319         if (copyIfNull || source.getRuleListCd() != null) {
320             if (source.getRuleListCd() == null) {
321                 target.setRuleList(null);
322             }
323             else {
324                 target.setRuleList(load(RuleListImpl.class, source.getRuleListCd()));
325             }
326         }
327 
328         // Function
329         if (copyIfNull || source.getFunctionId() != null)
330         {
331             if (source.getFunctionId() == null) {
332                 target.setFunction(null);
333             }
334             else {
335                 target.setFunction(load(FunctionImpl.class, source.getFunctionId()));
336             }
337         }
338 
339         if (withChildrenEntities) {
340 
341             // Rules parameters
342             if (copyIfNull || ArrayUtils.isNotEmpty(source.getRuleParameterVOs())) {
343                 if (ArrayUtils.isEmpty(source.getRuleParameterVOs())) {
344                     target.getRuleParameters().clear();
345                 } else {
346                     Daos.replaceEntities(target.getRuleParameters(),
347                             source.getRuleParameterVOs(),
348                             vo -> ruleParameterDao.ruleParameterVOToEntity(vo));
349                 }
350             }
351 
352             // Rules pmfm
353             if (copyIfNull || ArrayUtils.isNotEmpty(source.getRulePmfmVOs())) {
354                 if (ArrayUtils.isEmpty(source.getRulePmfmVOs())) {
355                     target.getRulePmfms().clear();
356                 } else {
357                     Daos.replaceEntities(target.getRulePmfms(),
358                             source.getRulePmfmVOs(),
359                             vo -> rulePmfmDao.rulePmfmVOToEntity(vo));
360                 }
361             }
362         }
363     }
364 }