1
2
3
4
5
6
7 package fr.ifremer.quadrige3.core.dao.system.rule;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
43
44 @Repository("ruleGroupDao")
45 @Lazy
46 public class RuleGroupDaoImpl extends RuleGroupDaoBase implements RuleGroupExtendDao {
47
48
49
50 @Autowired
51 public RuleGroupDaoImpl(SessionFactory sessionFactory) {
52 super();
53 setSessionFactory(sessionFactory);
54 }
55
56
57
58
59 @Override
60 protected RuleGroupVO handleSave(RuleGroupVO source, Timestamp updateDt) {
61 Assert.notNull(source);
62 Assert.notNull(source.getRuleCd());
63
64
65 Rule parent = get(RuleImpl.class, source.getRuleCd());
66
67
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
82
83
84
85
86
87
88 ruleGroupVOToEntity(source, entity, true);
89
90
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
103
104 @Override
105 protected void handleRemoveByIds(Collection<Integer> ruleGroupIds) {
106 ruleGroupIds.forEach(this::remove);
107 }
108
109
110
111
112 public void toRuleGroupVO(
113 RuleGroup source,
114 RuleGroupVO target) {
115 super.toRuleGroupVO(source, target);
116
117
118 if (source.getRule() == null) {
119 target.setRuleCd(null);
120 } else {
121 target.setRuleCd(source.getRule().getRuleCd());
122 }
123 }
124
125
126
127
128
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
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
152
153 @Override
154 public void ruleGroupVOToEntity(
155 RuleGroupVO source,
156 RuleGroup target,
157 boolean copyIfNull) {
158 super.ruleGroupVOToEntity(source, target, copyIfNull);
159
160
161 if (copyIfNull || source.getRuleGroupId() != null) {
162 target.setRuleGroupId(source.getRuleGroupId());
163 }
164
165
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 }