1 package fr.ifremer.dali.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 com.google.common.collect.ImmutableList;
27 import fr.ifremer.dali.dao.referential.DaliReferentialDao;
28 import fr.ifremer.dali.dao.technical.Daos;
29 import fr.ifremer.dali.dto.DaliBeanFactory;
30 import fr.ifremer.dali.dto.referential.DepartmentDTO;
31 import fr.ifremer.quadrige3.core.dao.administration.user.DepartmentDaoImpl;
32 import fr.ifremer.quadrige3.core.service.technical.CacheService;
33 import org.apache.commons.collections4.CollectionUtils;
34 import org.hibernate.Query;
35 import org.hibernate.SessionFactory;
36 import org.hibernate.type.IntegerType;
37 import org.hibernate.type.StringType;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.cache.Cache;
40 import org.springframework.dao.DataRetrievalFailureException;
41 import org.springframework.stereotype.Repository;
42
43 import javax.annotation.Resource;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Iterator;
47 import java.util.List;
48
49
50
51
52
53 @Repository("daliDepartmentDao")
54 public class DaliDepartmentDaoImpl extends DepartmentDaoImpl implements DaliDepartmentDao {
55
56 @Resource
57 protected CacheService cacheService;
58
59 @Resource(name = "daliReferentialDao")
60 protected DaliReferentialDao referentialDao;
61
62
63
64
65
66
67 @Autowired
68 public DaliDepartmentDaoImpl(SessionFactory sessionFactory) {
69 super(sessionFactory);
70 }
71
72
73 @Override
74 public List<DepartmentDTO> getAllDepartments(List<String> statusCodes) {
75
76 Cache cacheById = cacheService.getCache(DEPARTMENT_BY_ID_CACHE);
77
78 Iterator<Object[]> rows = Daos.queryIteratorWithStatus(createQuery("allDepartments"), statusCodes);
79
80 List<DepartmentDTO> result = new ArrayList<>();
81 if (rows != null) {
82 while (rows.hasNext()) {
83 Object[] row = rows.next();
84 DepartmentDTO department = toDepartmentDTO(Arrays.asList(row).iterator(), true);
85 result.add(department);
86
87 cacheById.put(department.getId(), department);
88 }
89 }
90
91 return ImmutableList.copyOf(result);
92 }
93
94
95 @Override
96 public DepartmentDTO getDepartmentById(int departmentId) {
97 return getDepartmentById(departmentId, false);
98 }
99
100 private DepartmentDTO getDepartmentById(int departmentId, boolean fetchParent) {
101
102 Object[] row = queryUnique("departmentById",
103 "departmentId", IntegerType.INSTANCE, departmentId);
104
105 if (row == null) {
106 throw new DataRetrievalFailureException("can't load department with id = " + departmentId);
107 }
108
109 return toDepartmentDTO(Arrays.asList(row).iterator(), fetchParent);
110 }
111
112
113 @SuppressWarnings("unchecked")
114 @Override
115 public List<DepartmentDTO> getDepartmentsByIds(List<Integer> departmentIds) {
116
117 List<DepartmentDTO> result = new ArrayList<>();
118 if (CollectionUtils.isEmpty(departmentIds))
119 return result;
120
121 Iterator<Object[]> it = createQuery("departmentsByIds")
122 .setParameterList("departmentIds", departmentIds)
123 .iterate();
124
125 while (it.hasNext()) {
126 Object[] row = it.next();
127 result.add(toDepartmentDTO(Arrays.asList(row).iterator(), false));
128 }
129
130 return result;
131 }
132
133
134 @Override
135 public List<DepartmentDTO> findDepartmentsByCodeAndName(String code, String name, boolean strictName, Integer parentId, List<String> statusCodes) {
136 Query query = createQuery("departmentsByCodeAndName",
137 "departmentCode", StringType.INSTANCE, strictName ? null : code,
138 "departmentName", StringType.INSTANCE, strictName ? null : name,
139 "strictDepartmentCode", StringType.INSTANCE, strictName ? code : null,
140 "strictDepartmentName", StringType.INSTANCE, strictName ? name : null,
141 "parentId", IntegerType.INSTANCE, parentId);
142
143 Iterator<Object[]> it = Daos.queryIteratorWithStatus(query, statusCodes);
144
145 List<DepartmentDTO> result = new ArrayList<>();
146 while (it.hasNext()) {
147 Object[] source = it.next();
148 result.add(toDepartmentDTO(Arrays.asList(source).iterator(), true));
149 }
150
151 return ImmutableList.copyOf(result);
152 }
153
154
155 private DepartmentDTO toDepartmentDTO(Iterator<Object> source, boolean fetchParent) {
156 DepartmentDTO result = DaliBeanFactory.newDepartmentDTO();
157 result.setId((Integer) source.next());
158 result.setCode((String) source.next());
159 result.setName((String) source.next());
160 result.setDescription((String) source.next());
161 result.setEmail((String) source.next());
162 result.setAddress((String) source.next());
163 result.setPhone((String) source.next());
164 Integer parentId = (Integer) source.next();
165 if (parentId != null && fetchParent) {
166 result.setParentDepartment(getDepartmentById(parentId));
167 }
168 result.setStatus(referentialDao.getStatusByCode((String) source.next()));
169 result.setComment((String) source.next());
170 result.setCreationDate(Daos.convertToDate(source.next()));
171 result.setUpdateDate(Daos.convertToDate(source.next()));
172 return result;
173 }
174
175 }