1 package net.sumaris.core.service.referential;
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.referential.ReferentialDao;
28 import net.sumaris.core.dao.technical.SortDirection;
29 import net.sumaris.core.exception.DataNotFoundException;
30 import net.sumaris.core.model.referential.IItemReferentialEntity;
31 import net.sumaris.core.vo.filter.ReferentialFilterVO;
32 import net.sumaris.core.vo.referential.ReferentialTypeVO;
33 import net.sumaris.core.vo.referential.ReferentialVO;
34 import org.nuiton.i18n.I18n;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Service;
39
40 import java.util.List;
41 import java.util.stream.Collectors;
42
43 @Service("referentialService")
44 public class ReferentialServiceImpl implements ReferentialService {
45
46 private static final Logger log = LoggerFactory.getLogger(ReferentialServiceImpl.class);
47
48 @Autowired
49 protected ReferentialDao referentialDao;
50
51 @Override
52 public List<ReferentialTypeVO> getAllTypes() {
53 return referentialDao.getAllTypes();
54 }
55
56 @Override
57 public List<ReferentialVO> getAllLevels(final String entityName) {
58 return referentialDao.getAllLevels(entityName);
59 }
60
61 @Override
62 public ReferentialVO getLevelById(String entityName, int levelId) {
63 return referentialDao.getLevelById(entityName, levelId);
64 }
65
66 @Override
67 public List<ReferentialVO> findByFilter(String entityName, ReferentialFilterVO filter, int offset, int size, String sortAttribute, SortDirection sortDirection) {
68 return referentialDao.findByFilter(entityName, filter != null ? filter : new ReferentialFilterVO(), offset, size, sortAttribute,
69 sortDirection);
70 }
71
72 @Override
73 public List<ReferentialVO> findByFilter(String entityName, ReferentialFilterVO filter, int offset, int size) {
74 return findByFilter(entityName, filter != null ? filter : new ReferentialFilterVO(), offset, size,
75 IItemReferentialEntity.Fields.LABEL,
76 SortDirection.ASC);
77 }
78
79 @Override
80 public ReferentialVO findByUniqueLabel(String entityName, String label) {
81 Preconditions.checkNotNull(entityName);
82 Preconditions.checkNotNull(label);
83 return referentialDao.findByUniqueLabel(entityName, label);
84 }
85
86 @Override
87 public Integer getIdByUniqueLabel(Class<? extends IItemReferentialEntity> entityClass, String label) {
88 Preconditions.checkNotNull(entityClass);
89 Preconditions.checkNotNull(label);
90 ReferentialVO entity = referentialDao.findByUniqueLabel(entityClass.getSimpleName(), label);
91 if (entity == null) throw new DataNotFoundException(I18n.t("sumaris.error.entity.notfoundByLabel", entityClass.getSimpleName(), label));
92 return entity.getId();
93 }
94
95 @Override
96 public void delete(final String entityName, int id) {
97 referentialDao.delete(entityName, id);
98 }
99
100 @Override
101 public void delete(final String entityName, List<Integer> ids) {
102 Preconditions.checkNotNull(entityName);
103 Preconditions.checkNotNull(ids);
104
105 ids.stream().forEach(id -> delete(entityName, id));
106 }
107
108 @Override
109 public Long count(String entityName) {
110 Preconditions.checkNotNull(entityName);
111 return referentialDao.count(entityName);
112 }
113
114 @Override
115 public Long countByLevelId(String entityName, Integer... levelIds) {
116 Preconditions.checkNotNull(entityName);
117 return referentialDao.countByLevelId(entityName, levelIds);
118 }
119
120 @Override
121 public ReferentialVO/../../../../net/sumaris/core/vo/referential/ReferentialVO.html#ReferentialVO">ReferentialVO save(ReferentialVO source) {
122 Preconditions.checkNotNull(source);
123 Preconditions.checkNotNull(source.getStatusId(), "Missing statusId");
124 Preconditions.checkNotNull(source.getEntityName(), "Missing entityName");
125
126 return referentialDao.save(source);
127 }
128
129 @Override
130 public List<ReferentialVO> save(List<ReferentialVO> beans) {
131 Preconditions.checkNotNull(beans);
132
133 return beans.stream()
134 .map(this::save)
135 .collect(Collectors.toList());
136 }
137 }