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.technical.Assert;
25  import fr.ifremer.quadrige3.core.vo.system.rule.RuleParameterVO;
26  import org.hibernate.SessionFactory;
27  import org.springframework.beans.factory.annotation.Autowired;
28  import org.springframework.context.annotation.Lazy;
29  import org.springframework.stereotype.Repository;
30  
31  import java.sql.Timestamp;
32  import java.util.Collection;
33  
34  /**
35   * @see RuleParameter
36   */
37  @Repository("ruleParameterDao")
38  @Lazy
39  public class RuleParameterDaoImpl
40          extends RuleParameterDaoBase
41  {
42      /**
43       * Constructor used by Spring
44       */
45      @Autowired
46      public RuleParameterDaoImpl(SessionFactory sessionFactory) {
47          super();
48          setSessionFactory(sessionFactory);
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      protected RuleParameterVO handleSave(RuleParameterVO source, Timestamp updateDt) {
55          Assert.notNull(source);
56          Assert.notNull(source.getRuleCd());
57  
58          // Load parent
59          Rule parent = get(RuleImpl.class, source.getRuleCd());
60  
61          // Load entity
62          RuleParameter entity = null;
63          boolean isNew = false;
64          if (source.getRuleParId() != null) {
65              entity = get(source.getRuleParId());
66          }
67          if (entity == null) {
68              entity = RuleParameter.Factory.newInstance();
69              parent.addRuleParameters(entity);
70              entity.setRule(parent);
71              isNew = true;
72          }
73  
74          // Check update_dt :
75          // NOT NEED on a client database
76  
77          // Update update_dt
78          // NOT NEED on a client database
79  
80          // VO -> Entity
81          ruleParameterVOToEntity(source, entity, true);
82  
83          // Save entity
84          if (isNew) {
85              getSession().save(entity);
86              // Affect new Id
87              source.setRuleParId(entity.getRuleParId());
88          } else {
89              getSession().update(entity);
90          }
91  
92          return source;
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      protected void handleRemoveByIds(Collection<Integer> ruleParIds) {
99          for (Integer ruleParId: ruleParIds) {
100             remove(ruleParId);
101         }
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     public void toRuleParameterVO(
108             RuleParameter source,
109             RuleParameterVO target)
110     {
111         super.toRuleParameterVO(source, target);
112 
113         // Rule
114         if (source.getRule() == null) {
115             target.setRuleCd(null);
116         }
117         else {
118             target.setRuleCd(source.getRule().getRuleCd());
119         }
120 
121         // Function
122         if (source.getFunctionParameter() == null) {
123             target.setFunctionParId(null);
124         }
125         else {
126             target.setFunctionParId(source.getFunctionParameter().getFunctionParId());
127         }
128 
129     }
130 
131     /**
132      * Retrieves the entity object that is associated with the specified value object
133      * from the object store. If no such entity object exists in the object store,
134      * a new, blank entity is created
135      */
136     private RuleParameter loadRuleParameterFromRuleParameterVO(RuleParameterVO ruleParameterVO)
137     {
138         RuleParameter ruleParameter = null;
139         if (ruleParameterVO.getRuleParId() != null) {
140             ruleParameter = this.get(ruleParameterVO.getRuleParId());
141         }
142         if (ruleParameter == null) {
143             ruleParameter = RuleParameter.Factory.newInstance();
144         }
145         return ruleParameter;
146     }
147 
148     /**
149      * {@inheritDoc}
150      */
151     public RuleParameter ruleParameterVOToEntity(RuleParameterVO ruleParameterVO)
152     {
153         RuleParameter entity = this.loadRuleParameterFromRuleParameterVO(ruleParameterVO);
154         this.ruleParameterVOToEntity(ruleParameterVO, entity, true);
155         return entity;
156     }
157 
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     public void ruleParameterVOToEntity(
163             RuleParameterVO source,
164             RuleParameter target,
165             boolean copyIfNull)
166     {
167         super.ruleParameterVOToEntity(source, target, copyIfNull);
168 
169         // Id
170         if (copyIfNull || source.getRuleParId() != null) {
171             target.setRuleParId(source.getRuleParId());
172         }
173 
174         // Rule
175         if (copyIfNull || source.getRuleCd() != null) {
176             if (source.getRuleCd() == null) {
177                 target.setRule(null);
178             }
179             else {
180                 target.setRule(load(RuleImpl.class, source.getRuleCd()));
181             }
182         }
183 
184         // Function parameter
185         if (copyIfNull || source.getFunctionParId() != null) {
186             if (source.getFunctionParId() == null) {
187                 target.setFunctionParameter(null);
188             }
189             else {
190                 target.setFunctionParameter(load(FunctionParameterImpl.class, source.getFunctionParId()));
191             }
192         }
193     }
194 }