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