1
2
3
4
5
6 package fr.ifremer.quadrige2.core.dao.administration.user;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 import com.google.common.base.Preconditions;
32 import fr.ifremer.quadrige2.core.exception.Quadrige2TechnicalException;
33 import fr.ifremer.quadrige2.core.dao.referential.StatusImpl;
34 import fr.ifremer.quadrige2.core.vo.administration.user.DepartmentVO;
35 import org.hibernate.SessionFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.context.annotation.Lazy;
38 import org.springframework.stereotype.Repository;
39
40
41
42
43
44
45
46
47 @Repository("departmentDao")
48 @Lazy
49 public class DepartmentDaoImpl
50 extends DepartmentDaoBase
51 implements DepartmentExtendDao {
52
53
54
55
56
57
58
59 @Autowired
60 public DepartmentDaoImpl(SessionFactory sessionFactory) {
61 super();
62 setSessionFactory(sessionFactory);
63 }
64
65
66 @Override
67 public DepartmentVO save(DepartmentVO bean) {
68 Preconditions.checkNotNull(bean);
69
70 Department entity = null;
71 if (bean.getDepId() != null) {
72 entity = load(bean.getDepId());
73 }
74
75
76 if (entity == null) {
77 entity = Department.Factory.newInstance();
78
79
80 toEntity(bean, entity);
81
82
83 entity = create(entity);
84 } else {
85 entity = load(bean.getDepId());
86
87
88 toEntity(bean, entity);
89
90
91 update(entity);
92 }
93
94
95 bean.setDepId(entity.getDepId());
96
97
98 if (entity.getParentDepartment() == null) {
99 bean.setParentDepartmentId(null);
100 } else {
101 bean.setParentDepartmentId(entity.getParentDepartment().getDepId());
102 }
103
104 return bean;
105 }
106
107
108 @Override
109 public Department departmentVOToEntity(DepartmentVO source) {
110 Department target;
111 if (source.getDepId() != null && source.getDepId() > 0) {
112 target = load(source.getDepId());
113 }
114 else {
115 target = Department.Factory.newInstance();
116 }
117 toEntity(source, target);
118
119 return target;
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134 protected void toEntity(DepartmentVO source, Department target) {
135 try {
136 target.setDepId(source.getDepId());
137 target.setDepCd(source.getDepCd());
138 target.setDepNm(source.getDepNm());
139 target.setDepLdapPresent(source.getDepLdapPresent());
140 target.setDepCreationDt(source.getDepCreationDt());
141 target.setUpdateDt(source.getUpdateDt());
142 target.setStatus(load(StatusImpl.class, source.getStatusCd()));
143 target.setDepEMail(source.getDepEMail());
144 target.setDepAddress(source.getDepAddress());
145 target.setDepPhone(source.getDepPhone());
146
147
148 if (source.getParentDepartmentId() == null) {
149 target.setParentDepartment(null);
150 } else {
151
152 Department parentDepartment = load(source.getParentDepartmentId());
153 target.setParentDepartment(parentDepartment);
154 }
155
156 } catch (Exception e) {
157 throw new Quadrige2TechnicalException("Error while transform DepartmentVO bean into Department entity", e);
158 }
159 }
160 }