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 com.google.common.collect.ImmutableList;
28 import net.sumaris.core.dao.administration.user.DepartmentDao;
29 import net.sumaris.core.dao.administration.user.PersonDao;
30 import net.sumaris.core.dao.technical.SortDirection;
31 import net.sumaris.core.exception.DataNotFoundException;
32 import net.sumaris.core.model.referential.StatusEnum;
33 import net.sumaris.core.model.referential.UserProfileEnum;
34 import net.sumaris.core.vo.administration.user.DepartmentVO;
35 import net.sumaris.core.vo.administration.user.PersonVO;
36 import net.sumaris.core.vo.data.ImageAttachmentVO;
37 import net.sumaris.core.vo.filter.PersonFilterVO;
38 import org.apache.commons.lang3.StringUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.nuiton.i18n.I18n;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Service;
44
45 import java.util.List;
46 import java.util.stream.Collectors;
47
48 @Service("personService")
49 public class PersonServiceImpl implements PersonService {
50
51 private static final Logger log = LoggerFactory.getLogger(PersonServiceImpl.class);
52
53 @Autowired
54 protected PersonDao personDao;
55
56 @Autowired
57 protected DepartmentDao departmentDao;
58
59 @Override
60 public List<PersonVO> findByFilter(PersonFilterVO filter, int offset, int size, String sortAttribute, SortDirection sortDirection) {
61 if (filter == null) filter = new PersonFilterVO();
62 return personDao.findByFilter(filter, offset, size, sortAttribute, sortDirection);
63 }
64
65 @Override
66 public Long countByFilter(final PersonFilterVO filter) {
67 return personDao.countByFilter(filter);
68 }
69
70 @Override
71 public PersonVO get(final int personId) {
72 return personDao.get(personId);
73 }
74
75 @Override
76 public PersonVO getByPubkey(final String pubkey) {
77 Preconditions.checkNotNull(pubkey);
78 PersonVO person = personDao.getByPubkeyOrNull(pubkey);
79 if (person == null) {
80 throw new DataNotFoundException(I18n.t("sumaris.error.person.notFound"));
81 }
82 return person;
83 }
84
85 @Override
86 public boolean isExistsByEmailHash(final String hash) {
87 Preconditions.checkArgument(StringUtils.isNotBlank(hash));
88 return personDao.isExistsByEmailHash(hash);
89 }
90
91 @Override
92 public ImageAttachmentVO getAvatarByPubkey(final String pubkey) {
93 Preconditions.checkNotNull(pubkey);
94 return personDao.getAvatarByPubkey(pubkey);
95 }
96
97 @Override
98 public List<String> getEmailsByProfiles(UserProfileEnum... userProfiles) {
99 Preconditions.checkNotNull(userProfiles);
100
101 return personDao.getEmailsByProfiles(
102 ImmutableList.copyOf(userProfiles).stream().map(up -> up.id).collect(Collectors.toList()),
103 ImmutableList.of(StatusEnum.ENABLE.getId())
104 );
105 }
106
107 @Override
108 public PersonVOf="../../../../../net/sumaris/core/vo/administration/user/PersonVO.html#PersonVO">PersonVO save(PersonVO person) {
109 checkValid(person);
110
111
112 if (person.getDepartment().getLabel() == null) {
113 DepartmentVO department = departmentDao.get(person.getDepartment().getId());
114 person.setDepartment(department);
115 }
116
117 return personDao.save(person);
118 }
119
120 @Override
121 public List<PersonVO> save(List<PersonVO> persons) {
122 Preconditions.checkNotNull(persons);
123
124 return persons.stream()
125 .map(this::save)
126 .collect(Collectors.toList());
127 }
128
129 @Override
130 public void delete(int id) {
131 personDao.delete(id);
132 }
133
134 @Override
135 public void delete(List<Integer> ids) {
136 Preconditions.checkNotNull(ids);
137
138 ids.stream().forEach(id -> delete(id));
139 }
140
141
142
143 protected void checkValid(PersonVO person) {
144 Preconditions.checkNotNull(person);
145 Preconditions.checkNotNull(person.getEmail(), I18n.t("sumaris.error.validation.required", I18n.t("sumaris.model.person.email")));
146 Preconditions.checkNotNull(person.getFirstName(), I18n.t("sumaris.error.validation.required", I18n.t("sumaris.model.person.firstName")));
147 Preconditions.checkNotNull(person.getLastName(), I18n.t("sumaris.error.validation.required", I18n.t("sumaris.model.person.lastName")));
148 Preconditions.checkNotNull(person.getDepartment(), I18n.t("sumaris.error.validation.required", I18n.t("sumaris.model.person.department")));
149 Preconditions.checkNotNull(person.getDepartment().getId(), I18n.t("sumaris.error.validation.required", I18n.t("sumaris.model.person.department")));
150
151 }
152 }