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.referential.pmfm.FractionImpl;
32  import fr.ifremer.quadrige2.core.dao.referential.pmfm.MatrixImpl;
33  import fr.ifremer.quadrige2.core.dao.referential.pmfm.MethodImpl;
34  import fr.ifremer.quadrige2.core.dao.referential.pmfm.ParameterImpl;
35  import fr.ifremer.quadrige2.core.dao.technical.Dates;
36  import fr.ifremer.quadrige2.core.exception.BadUpdateDtException;
37  import fr.ifremer.quadrige2.core.vo.system.rule.RulePmfmVO;
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 java.sql.Timestamp;
45  import java.util.Collection;
46  import java.util.Objects;
47  
48  /**
49   * @see RulePmfm
50   */
51  @Repository("rulePmfmDao")
52  @Lazy
53  public class RulePmfmDaoImpl
54      extends RulePmfmDaoBase
55  {
56      /**
57       * Constructor used by Spring
58       */
59  	@Autowired
60  	public RulePmfmDaoImpl(SessionFactory sessionFactory) {
61  		super();
62  		setSessionFactory(sessionFactory);
63  	}
64  
65      /**
66       * {@inheritDoc}
67       */
68      protected RulePmfmVO handleSave(RulePmfmVO source, Timestamp updateDt)
69      {
70          Preconditions.checkNotNull(source);
71          Preconditions.checkNotNull(source.getRuleCd());
72  
73          // Load parent
74          Rule parent = get(RuleImpl.class, source.getRuleCd());
75  
76          // Load entity
77          RulePmfm entity = null;
78          boolean isNew = false;
79          if (source.getRulePmfmId() != null) {
80              entity = get(source.getRulePmfmId());
81          }
82          if (entity == null) {
83              entity = RulePmfm.Factory.newInstance();
84              parent.addRulePmfms(entity);
85              entity.setRule(parent);
86              isNew = true;
87          }
88  
89          // Check update_dt
90          if (!isNew) {
91              if (entity.getUpdateDt() != null) {
92                  Timestamp serverUpdateDtNoMillisecond = Dates.resetMillisecond(entity.getUpdateDt());
93                  if (source.getUpdateDt() == null) {
94                      throw new BadUpdateDtException(I18n.t("quadrige2.dao.rule.badUpdateDt", source.getRuleCd(),
95                              serverUpdateDtNoMillisecond,
96                              "null"));
97                  }
98                  Timestamp sourceUpdateDtNoMillisecond = Dates.resetMillisecond(source.getUpdateDt());
99                  if (!Objects.equals(sourceUpdateDtNoMillisecond, serverUpdateDtNoMillisecond)) {
100                     throw new BadUpdateDtException(I18n.t("quadrige2.dao.rule.badUpdateDt", source.getRuleCd(), serverUpdateDtNoMillisecond,
101                             sourceUpdateDtNoMillisecond));
102                 }
103             }
104         }
105 
106         // update update_dt
107         Timestamp newUpdateDt = updateDt;
108         if (newUpdateDt == null) {
109             newUpdateDt = getDatabaseCurrentTimestamp();
110         }
111         source.setUpdateDt(newUpdateDt);
112 
113         // VO -> Entity
114         rulePmfmVOToEntity(source, entity, true);
115 
116         // Save entity
117         if (isNew) {
118             getSession().save(entity);
119             // Affect new Id
120             source.setRulePmfmId(entity.getRulePmfmId());
121         } else {
122             getSession().update(entity);
123         }
124 
125         return source;
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     protected void handleRemoveByIds(Collection<Integer> rulePmfmIds) {
132         for (Integer rulePmfmId: rulePmfmIds) {
133             remove(rulePmfmId);
134         }
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     public void toRulePmfmVO(
141         RulePmfm source,
142         RulePmfmVO target)
143     {
144         super.toRulePmfmVO(source, target);
145 
146         // Rule
147         if (source.getRule() == null) {
148             target.setRuleCd(null);
149         }
150         else {
151             target.setRuleCd(source.getRule().getRuleCd());
152         }
153 
154         // Parameter
155         if (source.getParameter() == null) {
156             target.setParCd(null);
157         }
158         else {
159             target.setParCd(source.getParameter().getParCd());
160         }
161 
162         // Matrix
163         if (source.getMatrix() == null) {
164             target.setMatrixId(null);
165         }
166         else {
167             target.setMatrixId(source.getMatrix().getMatrixId());
168         }
169 
170 
171         // Fraction
172         if (source.getFraction() == null) {
173             target.setFractionId(null);
174         }
175         else {
176             target.setFractionId(source.getFraction().getFractionId());
177         }
178 
179         // Method
180         if (source.getMethod() == null) {
181             target.setMethodId(null);
182         }
183         else {
184             target.setMethodId(source.getMethod().getMethodId());
185         }
186     }
187 
188 
189     /**
190      * Retrieves the entity object that is associated with the specified value object
191      * from the object store. If no such entity object exists in the object store,
192      * a new, blank entity is created
193      */
194     private RulePmfm loadRulePmfmFromRulePmfmVO(RulePmfmVO rulePmfmVO)
195     {
196         RulePmfm rulePmfm = null;
197         if (rulePmfmVO.getRulePmfmId() != null)
198         {
199             rulePmfm = this.get(rulePmfmVO.getRulePmfmId());
200         }
201         if (rulePmfm == null)
202         {
203             rulePmfm = RulePmfm.Factory.newInstance();
204         }
205         return rulePmfm;
206     }
207 
208     /**
209      * {@inheritDoc}
210      */
211     public RulePmfm rulePmfmVOToEntity(RulePmfmVO rulePmfmVO)
212     {
213         RulePmfm entity = this.loadRulePmfmFromRulePmfmVO(rulePmfmVO);
214         this.rulePmfmVOToEntity(rulePmfmVO, entity, true);
215         return entity;
216     }
217 
218     /**
219      * {@inheritDoc}
220      */
221     @Override
222     public void rulePmfmVOToEntity(
223         RulePmfmVO source,
224         RulePmfm target,
225         boolean copyIfNull)
226     {
227         super.rulePmfmVOToEntity(source, target, copyIfNull);
228 
229         // Id
230         if (copyIfNull || source.getRulePmfmId() != null) {
231             target.setRulePmfmId(source.getRulePmfmId());
232         }
233 
234         // Rule
235         if (copyIfNull || source.getRuleCd() != null) {
236             if (source.getRuleCd() == null) {
237                 target.setRule(null);
238             }
239             else {
240                 target.setRule(load(RuleImpl.class, source.getRuleCd()));
241             }
242         }
243 
244         // Parameters
245         if (copyIfNull || source.getParCd() != null) {
246             if (source.getParCd() == null) {
247                 target.setParameter(null);
248             }
249             else {
250                 target.setParameter(load(ParameterImpl.class, source.getParCd()));
251             }
252         }
253 
254         // Matrix
255         if (copyIfNull || source.getMatrixId() != null) {
256             if (source.getMatrixId() == null) {
257                 target.setMatrix(null);
258             }
259             else {
260                 target.setMatrix(load(MatrixImpl.class, source.getMatrixId()));
261             }
262         }
263 
264         // Fraction
265         if (copyIfNull || source.getFractionId() != null) {
266             if (source.getFractionId() == null) {
267                 target.setFraction(null);
268             }
269             else {
270                 target.setFraction(load(FractionImpl.class, source.getFractionId()));
271             }
272         }
273 
274         // Method
275         if (copyIfNull || source.getMethodId() != null) {
276             if (source.getMethodId() == null) {
277                 target.setMethod(null);
278             }
279             else {
280                 target.setMethod(load(MethodImpl.class, source.getMethodId()));
281             }
282         }
283     }
284 }