1 package net.sumaris.core.service.administration;
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.base.Preconditions;
27 import net.sumaris.core.dao.administration.user.DepartmentDao;
28 import net.sumaris.core.dao.data.ImageAttachmentDao;
29 import net.sumaris.core.dao.technical.SortDirection;
30 import net.sumaris.core.model.administration.user.Department;
31 import net.sumaris.core.vo.administration.user.DepartmentVO;
32 import net.sumaris.core.vo.data.ImageAttachmentVO;
33 import net.sumaris.core.vo.filter.DepartmentFilterVO;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.nuiton.i18n.I18n;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.dao.DataRetrievalFailureException;
39 import org.springframework.stereotype.Service;
40
41 import java.util.Arrays;
42 import java.util.List;
43 import java.util.Objects;
44 import java.util.stream.Collectors;
45
46 @Service("departmentService")
47 public class DepartmentServiceImpl implements DepartmentService {
48
49 private static final Logger log = LoggerFactory.getLogger(DepartmentServiceImpl.class);
50
51 @Autowired
52 protected DepartmentDao departmentDao;
53
54 @Autowired
55 protected ImageAttachmentDao imageAttachmentDao;
56
57 @Override
58 public List<DepartmentVO> findByFilter(DepartmentFilterVO filter, int offset, int size, String sortAttribute, SortDirection sortDirection) {
59 return departmentDao.findByFilter(filter == null ? new DepartmentFilterVO() : filter,
60 offset,
61 size,
62 sortAttribute,
63 sortDirection);
64 }
65
66
67 @Override
68 public DepartmentVO get(int departmentId) {
69 return departmentDao.get(departmentId);
70 }
71
72 @Override
73 public List<DepartmentVO> getByIds(int... ids) {
74 return Arrays.stream(ids)
75 .mapToObj(departmentDao::get)
76 .filter(Objects::nonNull)
77 .collect(Collectors.toList());
78 }
79
80 @Override
81 public ImageAttachmentVO getLogoByLabel(final String label) {
82 Preconditions.checkNotNull(label);
83 Department department = departmentDao.getByLabelOrNull(label);
84 if (department == null || department.getLogo() == null) {
85 throw new DataRetrievalFailureException(I18n.t("sumaris.error.department.logo.notFound"));
86 }
87 return imageAttachmentDao.get(department.getLogo().getId());
88 }
89
90 @Override
91 public DepartmentVO./../../../../net/sumaris/core/vo/administration/user/DepartmentVO.html#DepartmentVO">DepartmentVO save(DepartmentVO department) {
92 checkValid(department);
93
94 return departmentDao.save(department);
95 }
96
97 @Override
98 public void delete(int id) {
99 departmentDao.delete(id);
100 }
101
102
103
104 protected void checkValid(DepartmentVO department) {
105 Preconditions.checkNotNull(department);
106 Preconditions.checkNotNull(department.getLabel(), I18n.t("sumaris.error.validation.required", I18n.t("sumaris.model.department.label")));
107 Preconditions.checkNotNull(department.getName(), I18n.t("sumaris.error.validation.required", I18n.t("sumaris.model.department.name")));
108 Preconditions.checkNotNull(department.getSiteUrl(), I18n.t("sumaris.error.validation.required", I18n.t("sumaris.model.department.siteUrl")));
109 Preconditions.checkNotNull(department.getStatusId(), I18n.t("sumaris.error.validation.required", I18n.t("sumaris.model.department.status")));
110
111 }
112 }