View Javadoc
1   package fr.ifremer.quadrige2.core.service.administration.program;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Server 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.base.Preconditions;
27  import com.google.common.collect.Lists;
28  import com.google.common.collect.Sets;
29  import fr.ifremer.quadrige2.core.exception.Exceptions;
30  import fr.ifremer.quadrige2.core.config.Quadrige2Configuration;
31  import fr.ifremer.quadrige2.core.dao.administration.program.ProgramDao;
32  import fr.ifremer.quadrige2.core.dao.administration.program.ProgramStrategyJdbcDao;
33  import fr.ifremer.quadrige2.core.exception.BadUpdateDtException;
34  import fr.ifremer.quadrige2.core.exception.DataLockedException;
35  import fr.ifremer.quadrige2.core.vo.administration.program.ProgramVO;
36  import org.apache.commons.collections4.CollectionUtils;
37  import org.springframework.context.annotation.Lazy;
38  import org.springframework.stereotype.Service;
39  
40  import javax.annotation.Resource;
41  import java.util.Collection;
42  import java.util.List;
43  import java.util.Set;
44  
45  /**
46   * <p>
47   * ProgramServiceImpl class.
48   * </p>
49   * 
50   */
51  @Service("programService")
52  @Lazy
53  public class ProgramServiceImpl implements ProgramService {
54  
55  	@Resource
56  	private ProgramDao programDao;
57  
58  	@Resource
59  	protected ProgramStrategyJdbcDao programStrategyJdbcDao;
60  
61  	@Resource
62  	protected Quadrige2Configuration config;
63  
64  	/** {@inheritDoc} */
65  	@Override
66  	public List<ProgramVO> save(List<ProgramVO> programs) {
67  		List<ProgramVO> result = Lists.newArrayList();
68  
69  		if (CollectionUtils.isNotEmpty(programs)) {
70  			for (ProgramVO source : programs) {
71  				ProgramVO target = save(source);
72  				result.add(target);
73  			}
74  		}
75  
76  		return result;
77  	}
78  
79  	/** {@inheritDoc} */
80  	@Override
81  	public ProgramVO save(ProgramVO source) {
82  		Preconditions.checkNotNull(source);
83  		Preconditions.checkNotNull(source.getProgCd());
84  
85  
86  		// Save program
87  		try {
88  			return programDao.save(source);
89  		} catch (RuntimeException e) {
90  			if (Exceptions.hasCause(e, BadUpdateDtException.class)) {
91  				throw new BadUpdateDtException(Exceptions.getCause(e, BadUpdateDtException.class).getMessage(), e);
92  			}
93  			if (Exceptions.hasCause(e, DataLockedException.class)) {
94  				throw new DataLockedException(Exceptions.getCause(e, DataLockedException.class).getMessage(), e);
95  			}
96  			throw e;
97  		}
98  	}
99  
100 	/** {@inheritDoc} */
101 	@Override
102 	public ProgramVO getByCode(String progCd) {
103 		return (ProgramVO) programDao.get(ProgramDao.TRANSFORM_PROGRAMVO, progCd);
104 	}
105 
106 	/** {@inheritDoc} */
107 	@Override
108 	public List<ProgramVO> getWritableProgramsByQuserId(int quserId) {
109 		List<ProgramVO> programs = programStrategyJdbcDao.getWritableProgramsByUserId(quserId);
110 		Set<String> programCodes = config.getSynchroProgramCodeIncludes();
111 
112 		if (CollectionUtils.isEmpty(programs) || CollectionUtils.isEmpty(programCodes)) {
113 			return programs;
114 		}
115 
116 		// Filter on programs to includes
117 		List<ProgramVO> result = Lists.newArrayList();
118 		for (ProgramVO programVO : programs) {
119 			if (programCodes.contains(programVO.getProgCd())) {
120 				result.add(programVO);
121 			}
122 		}
123 
124 		return result;
125 	}
126 
127 	/** {@inheritDoc} */
128 	@Override
129 	public Set<String> getWritableProgramCodesByQuserId(int quserId) {
130 		List<ProgramVO> writablePrograms = programStrategyJdbcDao.getWritableProgramsByUserId(quserId);
131 		Set<String> configProgramCodes = config.getSynchroProgramCodeIncludes();
132 
133 		if (CollectionUtils.isEmpty(writablePrograms)) {
134 			return null;
135 		}
136 
137 		// Filter on programs to includes
138 		Set<String> result = Sets.newHashSet();
139 		for (ProgramVO programVO : writablePrograms) {
140 			if (CollectionUtils.isEmpty(configProgramCodes)
141 					|| configProgramCodes.contains(programVO.getProgCd())) {
142 				result.add(programVO.getProgCd());
143 			}
144 		}
145 
146 		return result;
147 	}
148 
149 	/** {@inheritDoc} */
150 	@Override
151 	public boolean hasAccessRightOnProgram(int quserId) {
152 		return CollectionUtils.isNotEmpty(
153 				getWritableProgramsByQuserId(quserId)
154 				);
155 	}
156 
157 	/** {@inheritDoc} */
158 	@Override
159 	public boolean hasWritePermission(int quserId, Collection<String> progCds) {
160 		if (CollectionUtils.isEmpty(progCds)) return true; // make no sense
161 
162 		List<ProgramVO> writablePrograms = getWritableProgramsByQuserId(quserId);
163 		if (CollectionUtils.isEmpty(writablePrograms)) {
164 			return CollectionUtils.isEmpty(progCds);
165 		}
166 		List<String> writableProgramCodes = Lists.transform(writablePrograms, ProgramVO::getProgCd);
167 		for (String progCd : progCds) {
168 			if (!writableProgramCodes.contains(progCd)) {
169 				return false;
170 			}
171 		}
172 		return true;
173 	}
174 
175 	@Override
176 	public boolean hasSomeWritePermission(int quserId) {
177 		List<ProgramVO> writablePrograms = getWritableProgramsByQuserId(quserId);
178 		return CollectionUtils.isNotEmpty(writablePrograms);
179 	}
180 }