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