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.quadrige3.core.dao.system.rule;
8   
9   /*-
10   * #%L
11   * Quadrige3 Core :: Client API
12   * %%
13   * Copyright (C) 2017 - 2019 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  import fr.ifremer.quadrige3.core.dao.technical.Assert;
30  import fr.ifremer.quadrige3.core.vo.system.rule.RuleGroupVO;
31  import org.hibernate.SessionFactory;
32  import org.hibernate.type.StringType;
33  import org.springframework.beans.factory.annotation.Autowired;
34  import org.springframework.context.annotation.Lazy;
35  import org.springframework.stereotype.Repository;
36  
37  import java.sql.Timestamp;
38  import java.util.Collection;
39  import java.util.List;
40  
41  /**
42   * @see RuleGroup
43   */
44  @Repository("ruleGroupDao")
45  @Lazy
46  public class RuleGroupDaoImpl extends RuleGroupDaoBase implements RuleGroupExtendDao {
47      /**
48       * Constructor used by Spring
49       */
50      @Autowired
51      public RuleGroupDaoImpl(SessionFactory sessionFactory) {
52          super();
53          setSessionFactory(sessionFactory);
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      protected RuleGroupVO handleSave(RuleGroupVO source, Timestamp updateDt) {
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          RuleGroup entity = null;
69          boolean isNew = false;
70          if (source.getRuleGroupId() != null) {
71              entity = get(source.getRuleGroupId());
72          }
73          if (entity == null) {
74              entity = RuleGroup.Factory.newInstance();
75              entity.setRule(parent);
76              parent.getRuleGroups().clear();
77              parent.addRuleGroups(entity);
78              isNew = true;
79          }
80  
81          // Check update_dt :
82          // NOT NEED on a client database
83  
84          // Update update_dt
85          // NOT NEED on a client database
86  
87          // VO -> Entity
88          ruleGroupVOToEntity(source, entity, true);
89  
90          // Save entity
91          if (isNew) {
92              getSession().save(entity);
93              source.setRuleGroupId(entity.getRuleGroupId());
94          } else {
95              getSession().update(entity);
96          }
97  
98          return source;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     protected void handleRemoveByIds(Collection<Integer> ruleGroupIds) {
106         ruleGroupIds.forEach(this::remove);
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     public void toRuleGroupVO(
113             RuleGroup source,
114             RuleGroupVO target) {
115         super.toRuleGroupVO(source, target);
116 
117         // Rule
118         if (source.getRule() == null) {
119             target.setRuleCd(null);
120         } else {
121             target.setRuleCd(source.getRule().getRuleCd());
122         }
123     }
124 
125     /**
126      * Retrieves the entity object that is associated with the specified value object
127      * from the object store. If no such entity object exists in the object store,
128      * a new, blank entity is created
129      */
130     private RuleGroup loadRuleGroupFromRuleGroupVO(RuleGroupVO ruleGroupVO) {
131         RuleGroup ruleGroup = null;
132         if (ruleGroupVO.getRuleGroupId() != null) {
133             ruleGroup = this.get(ruleGroupVO.getRuleGroupId());
134         }
135         if (ruleGroup == null) {
136             ruleGroup = RuleGroup.Factory.newInstance();
137         }
138         return ruleGroup;
139     }
140 
141     /**
142      * {@inheritDoc}
143      */
144     public RuleGroup ruleGroupVOToEntity(RuleGroupVO ruleGroupVO) {
145         RuleGroup entity = this.loadRuleGroupFromRuleGroupVO(ruleGroupVO);
146         this.ruleGroupVOToEntity(ruleGroupVO, entity, true);
147         return entity;
148     }
149 
150     /**
151      * {@inheritDoc}
152      */
153     @Override
154     public void ruleGroupVOToEntity(
155             RuleGroupVO source,
156             RuleGroup target,
157             boolean copyIfNull) {
158         super.ruleGroupVOToEntity(source, target, copyIfNull);
159 
160         // Id
161         if (copyIfNull || source.getRuleGroupId() != null) {
162             target.setRuleGroupId(source.getRuleGroupId());
163         }
164 
165         // Rule
166         if (copyIfNull || source.getRuleCd() != null) {
167             if (source.getRuleCd() == null) {
168                 target.setRule(null);
169             } else {
170                 target.setRule(load(RuleImpl.class, source.getRuleCd()));
171             }
172         }
173     }
174 
175     @Override
176     public List<RuleGroup> getByCode(String code) {
177         return queryListTyped("ruleGroupsByCode", "code", StringType.INSTANCE, code);
178     }
179 }