1 package fr.ifremer.quadrige3.core.dao.system.rule;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
36
37 @Repository("ruleParameterDao")
38 @Lazy
39 public class RuleParameterDaoImpl
40 extends RuleParameterDaoBase
41 {
42
43
44
45 @Autowired
46 public RuleParameterDaoImpl(SessionFactory sessionFactory) {
47 super();
48 setSessionFactory(sessionFactory);
49 }
50
51
52
53
54 protected RuleParameterVO handleSave(RuleParameterVO source, Timestamp updateDt) {
55 Assert.notNull(source);
56 Assert.notNull(source.getRuleCd());
57
58
59 Rule parent = get(RuleImpl.class, source.getRuleCd());
60
61
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
75
76
77
78
79
80
81 ruleParameterVOToEntity(source, entity, true);
82
83
84 if (isNew) {
85 getSession().save(entity);
86
87 source.setRuleParId(entity.getRuleParId());
88 } else {
89 getSession().update(entity);
90 }
91
92 return source;
93 }
94
95
96
97
98 protected void handleRemoveByIds(Collection<Integer> ruleParIds) {
99 for (Integer ruleParId: ruleParIds) {
100 remove(ruleParId);
101 }
102 }
103
104
105
106
107 public void toRuleParameterVO(
108 RuleParameter source,
109 RuleParameterVO target)
110 {
111 super.toRuleParameterVO(source, target);
112
113
114 if (source.getRule() == null) {
115 target.setRuleCd(null);
116 }
117 else {
118 target.setRuleCd(source.getRule().getRuleCd());
119 }
120
121
122 if (source.getFunctionParameter() == null) {
123 target.setFunctionParId(null);
124 }
125 else {
126 target.setFunctionParId(source.getFunctionParameter().getFunctionParId());
127 }
128
129 }
130
131
132
133
134
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
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
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
170 if (copyIfNull || source.getRuleParId() != null) {
171 target.setRuleParId(source.getRuleParId());
172 }
173
174
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
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 }