View Javadoc
1   package fr.ifremer.quadrige3.core.service.administration.program;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Client Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   *
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   *
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.collect.Lists;
27  import com.google.common.collect.Sets;
28  import fr.ifremer.quadrige3.core.config.QuadrigeCoreConfiguration;
29  import fr.ifremer.quadrige3.core.dao.administration.program.Program;
30  import fr.ifremer.quadrige3.core.dao.administration.program.ProgramDao;
31  import fr.ifremer.quadrige3.core.dao.administration.program.ProgramStrategyJdbcDao;
32  import fr.ifremer.quadrige3.core.dao.administration.user.PrivilegeCode;
33  import fr.ifremer.quadrige3.core.dao.technical.Assert;
34  import fr.ifremer.quadrige3.core.dao.technical.Beans;
35  import fr.ifremer.quadrige3.core.service.administration.user.UserService;
36  import fr.ifremer.quadrige3.core.vo.administration.program.ProgramVO;
37  import org.apache.commons.collections4.CollectionUtils;
38  import org.springframework.context.annotation.Lazy;
39  import org.springframework.stereotype.Service;
40  
41  import javax.annotation.Resource;
42  import java.util.Collection;
43  import java.util.List;
44  import java.util.Set;
45  import java.util.stream.Collectors;
46  
47  /**
48   * <p>
49   * ProgramServiceImpl class.
50   * </p>
51   *
52   */
53  @Service("programService")
54  @Lazy
55  public class ProgramServiceImpl implements ProgramService {
56  
57  	@Resource
58  	private ProgramDao programDao;
59  	@Resource
60  	private ProgramStrategyJdbcDao programStrategyJdbcDao;
61  	@Resource
62  	private QuadrigeCoreConfiguration configuration;
63  
64  	@Resource
65  	private UserService userService;
66  
67  	@Override
68  	public List<String> getAllProgramCodes() {
69  
70  		return programStrategyJdbcDao.getAllProgramCodes();
71  	}
72  
73  	/** {@inheritDoc} */
74  	@Override
75  	public List<ProgramVO> save(List<ProgramVO> programs) {
76  		List<ProgramVO> result = Lists.newArrayList();
77  
78  		if (CollectionUtils.isNotEmpty(programs)) {
79  			for (ProgramVO source : programs) {
80  				ProgramVO target = save(source);
81  				result.add(target);
82  			}
83  		}
84  
85  		return result;
86  	}
87  
88  	/** {@inheritDoc} */
89  	@Override
90  	public ProgramVO save(ProgramVO source) {
91  		Assert.notNull(source);
92  		Assert.notNull(source.getProgCd());
93  
94  		// Load (or create) the entity
95  		Program entity = programDao.get(source.getProgCd());
96  		boolean isNew = false;
97  		if (entity == null) {
98  			entity = Program.Factory.newInstance();
99  			isNew = true;
100 		}
101 
102 		// VO -> Entity
103 		programDao.programVOToEntity(source, entity, true);
104 
105 		// Save it
106 		ProgramVO target;
107 		if (isNew) {
108 			target = (ProgramVO) programDao.create(ProgramDao.TRANSFORM_PROGRAMVO, entity);
109 		} else {
110 			programDao.update(entity);
111 			target = programDao.toProgramVO(entity);
112 		}
113 
114 		return target;
115 	}
116 
117 	/** {@inheritDoc} */
118 	@Override
119 	public ProgramVO getByCode(String progCd) {
120 		return (ProgramVO) programDao.get(ProgramDao.TRANSFORM_PROGRAMVO, progCd);
121 	}
122 
123 	@Override
124 	public Set<String> getReadableProgramCodesByQuserId(int quserId) {
125 		List<String> programCodes = isAdmin(quserId)
126 			? getAllProgramCodes()
127 			: Beans.transformCollection(programStrategyJdbcDao.getReadableProgramsByUserId(quserId), ProgramVO::getProgCd);
128 		if (CollectionUtils.isEmpty(programCodes)) return Sets.newHashSet();
129 		return filterProgramCodes(programCodes);
130 	}
131 
132 	/** {@inheritDoc} */
133 	@Override
134     public Set<String> getWritableProgramCodesByQuserId(int quserId) {
135         List<String> programCodes = isAdmin(quserId)
136             ? getAllProgramCodes()
137             : Beans.transformCollection(programStrategyJdbcDao.getWritableProgramsByUserId(quserId), ProgramVO::getProgCd);
138         if (CollectionUtils.isEmpty(programCodes)) return Sets.newHashSet();
139         return filterProgramCodes(programCodes);
140     }
141 
142 	/** {@inheritDoc} */
143 	@Override
144 	public Set<String> getManagedProgramCodesByQuserId(int quserId) {
145 		List<String> programCodes = isAdmin(quserId)
146 			? getAllProgramCodes()
147 			: Beans.transformCollection(programStrategyJdbcDao.getManagedProgramsByUserId(null, quserId), ProgramVO::getProgCd);
148 		if (CollectionUtils.isEmpty(programCodes)) return Sets.newHashSet();
149 		return filterProgramCodes(programCodes);
150 	}
151 
152 	protected Set<String> filterProgramCodes(Collection<String> programCodes) {
153 		// Filter by configuration
154 		Set<String> includeProgramCodes = configuration.getSynchroProgramCodeIncludes();
155 
156 		return programCodes.stream()
157 			.filter(programCode -> CollectionUtils.isEmpty(includeProgramCodes) || includeProgramCodes.contains(programCode))
158 			.collect(Collectors.toSet());
159 	}
160 
161 	protected boolean isAdmin(int userId) {
162 		return userService.hasPrivilege(userId, PrivilegeCode.REFERENTIAL_ADMINISTRATOR.getValue());
163 	}
164 }