View Javadoc
1   package net.sumaris.core.service.administration;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core
6    * %%
7    * Copyright (C) 2018 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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 	/* -- protected methods -- */
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 }