View Javadoc
1   package fr.ifremer.quadrige3.core.dao.system.rule;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Client Core
6    * %%
7    * Copyright (C) 2017 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (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 Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import fr.ifremer.quadrige3.core.dao.referential.UnitImpl;
25  import fr.ifremer.quadrige3.core.dao.referential.pmfm.FractionImpl;
26  import fr.ifremer.quadrige3.core.dao.referential.pmfm.MatrixImpl;
27  import fr.ifremer.quadrige3.core.dao.referential.pmfm.MethodImpl;
28  import fr.ifremer.quadrige3.core.dao.referential.pmfm.ParameterImpl;
29  import fr.ifremer.quadrige3.core.dao.technical.Assert;
30  import fr.ifremer.quadrige3.core.vo.system.rule.RulePmfmVO;
31  import org.hibernate.SessionFactory;
32  import org.springframework.beans.factory.annotation.Autowired;
33  import org.springframework.context.annotation.Lazy;
34  import org.springframework.stereotype.Repository;
35  
36  import java.sql.Timestamp;
37  import java.util.Collection;
38  
39  /**
40   * @see RulePmfm
41   */
42  @Repository("rulePmfmDao")
43  @Lazy
44  public class RulePmfmDaoImpl
45          extends RulePmfmDaoBase
46  {
47      /**
48       * Constructor used by Spring
49       */
50      @Autowired
51      public RulePmfmDaoImpl(SessionFactory sessionFactory) {
52          super();
53          setSessionFactory(sessionFactory);
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      protected RulePmfmVO handleSave(RulePmfmVO source, Timestamp updateDt)
60      {
61          Assert.notNull(source);
62          Assert.notNull(source.getRuleCd());
63  
64          // Load parent
65          Rule parent = get(RuleImpl.class, source.getRuleCd());
66  
67          // Load entity
68          RulePmfm entity = null;
69          boolean isNew = false;
70          if (source.getRulePmfmId() != null) {
71              entity = get(source.getRulePmfmId());
72          }
73          if (entity == null) {
74              entity = RulePmfm.Factory.newInstance();
75              parent.addRulePmfms(entity);
76              entity.setRule(parent);
77              isNew = true;
78          }
79  
80          // Check update_dt :
81          // NOT NEED on a client database
82  
83          // Update update_dt
84          // NOT NEED on a client database
85  
86          // VO -> Entity
87          rulePmfmVOToEntity(source, entity, true);
88  
89          // Save entity
90          if (isNew) {
91              getSession().save(entity);
92              // Affect new Id
93              source.setRulePmfmId(entity.getRulePmfmId());
94          } else {
95              getSession().update(entity);
96          }
97  
98          return source;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     protected void handleRemoveByIds(Collection<Integer> rulePmfmIds) {
105         for (Integer rulePmfmId: rulePmfmIds) {
106             remove(rulePmfmId);
107         }
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     public void toRulePmfmVO(
114             RulePmfm source,
115             RulePmfmVO target)
116     {
117         super.toRulePmfmVO(source, target);
118 
119         // Rule
120         if (source.getRule() == null) {
121             target.setRuleCd(null);
122         }
123         else {
124             target.setRuleCd(source.getRule().getRuleCd());
125         }
126 
127         // Parameter
128         if (source.getParameter() == null) {
129             target.setParCd(null);
130         }
131         else {
132             target.setParCd(source.getParameter().getParCd());
133         }
134 
135         // Matrix
136         if (source.getMatrix() == null) {
137             target.setMatrixId(null);
138         }
139         else {
140             target.setMatrixId(source.getMatrix().getMatrixId());
141         }
142 
143         // Fraction
144         if (source.getFraction() == null) {
145             target.setFractionId(null);
146         }
147         else {
148             target.setFractionId(source.getFraction().getFractionId());
149         }
150 
151         // Method
152         if (source.getMethod() == null) {
153             target.setMethodId(null);
154         }
155         else {
156             target.setMethodId(source.getMethod().getMethodId());
157         }
158 
159         // Unit
160         if (source.getUnit() == null) {
161             target.setUnitId(null);
162         }
163         else {
164             target.setUnitId(source.getUnit().getUnitId());
165         }
166     }
167 
168 
169     /**
170      * Retrieves the entity object that is associated with the specified value object
171      * from the object store. If no such entity object exists in the object store,
172      * a new, blank entity is created
173      */
174     private RulePmfm loadRulePmfmFromRulePmfmVO(RulePmfmVO rulePmfmVO)
175     {
176         RulePmfm rulePmfm = null;
177         if (rulePmfmVO.getRulePmfmId() != null) {
178             rulePmfm = this.get(rulePmfmVO.getRulePmfmId());
179         }
180         if (rulePmfm == null) {
181             rulePmfm = RulePmfm.Factory.newInstance();
182         }
183         return rulePmfm;
184     }
185 
186     /**
187      * {@inheritDoc}
188      */
189     public RulePmfm rulePmfmVOToEntity(RulePmfmVO rulePmfmVO)
190     {
191         RulePmfm entity = this.loadRulePmfmFromRulePmfmVO(rulePmfmVO);
192         this.rulePmfmVOToEntity(rulePmfmVO, entity, true);
193         return entity;
194     }
195 
196     /**
197      * {@inheritDoc}
198      */
199     @Override
200     public void rulePmfmVOToEntity(
201             RulePmfmVO source,
202             RulePmfm target,
203             boolean copyIfNull)
204     {
205         super.rulePmfmVOToEntity(source, target, copyIfNull);
206 
207         // Id
208         if (copyIfNull || source.getRulePmfmId() != null) {
209             target.setRulePmfmId(source.getRulePmfmId());
210         }
211 
212         // Rule
213         if (copyIfNull || source.getRuleCd() != null) {
214             if (source.getRuleCd() == null) {
215                 target.setRule(null);
216             }
217             else {
218                 target.setRule(load(RuleImpl.class, source.getRuleCd()));
219             }
220         }
221 
222         // Parameters
223         if (copyIfNull || source.getParCd() != null) {
224             if (source.getParCd() == null) {
225                 target.setParameter(null);
226             }
227             else {
228                 target.setParameter(load(ParameterImpl.class, source.getParCd()));
229             }
230         }
231 
232         // Matrix
233         if (copyIfNull || source.getMatrixId() != null) {
234             if (source.getMatrixId() == null) {
235                 target.setMatrix(null);
236             }
237             else {
238                 target.setMatrix(load(MatrixImpl.class, source.getMatrixId()));
239             }
240         }
241 
242         // Fraction
243         if (copyIfNull || source.getFractionId() != null) {
244             if (source.getFractionId() == null) {
245                 target.setFraction(null);
246             }
247             else {
248                 target.setFraction(load(FractionImpl.class, source.getFractionId()));
249             }
250         }
251 
252         // Method
253         if (copyIfNull || source.getMethodId() != null) {
254             if (source.getMethodId() == null) {
255                 target.setMethod(null);
256             }
257             else {
258                 target.setMethod(load(MethodImpl.class, source.getMethodId()));
259             }
260         }
261 
262         // Unit
263         if (copyIfNull || source.getUnitId() != null) {
264             if (source.getUnitId() == null) {
265                 target.setUnit(null);
266             }
267             else {
268                 target.setUnit(load(UnitImpl.class, source.getUnitId()));
269             }
270         }
271     }
272 }