1 | |
package org.kuali.student.r2.core.population.service; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.Date; |
5 | |
import org.kuali.rice.core.api.criteria.QueryByCriteria; |
6 | |
import org.kuali.student.r2.common.dto.ContextInfo; |
7 | |
import org.kuali.student.r2.common.dto.MetaInfo; |
8 | |
import org.kuali.student.r2.common.dto.StatusInfo; |
9 | |
import org.kuali.student.r2.common.dto.ValidationResultInfo; |
10 | |
import org.kuali.student.r2.common.exceptions.DataValidationErrorException; |
11 | |
import org.kuali.student.r2.common.exceptions.DoesNotExistException; |
12 | |
import org.kuali.student.r2.common.exceptions.InvalidParameterException; |
13 | |
import org.kuali.student.r2.common.exceptions.MissingParameterException; |
14 | |
import org.kuali.student.r2.common.exceptions.OperationFailedException; |
15 | |
import org.kuali.student.r2.common.exceptions.PermissionDeniedException; |
16 | |
import org.kuali.student.r2.common.exceptions.ReadOnlyException; |
17 | |
import org.kuali.student.r2.common.exceptions.VersionMismatchException; |
18 | |
import org.kuali.student.r2.common.util.constants.PopulationServiceConstants; |
19 | |
import org.kuali.student.r2.core.population.dto.PopulationInfo; |
20 | |
import org.kuali.student.r2.core.population.dto.PopulationRuleInfo; |
21 | |
|
22 | |
import org.kuali.student.common.util.UUIDHelper; |
23 | |
|
24 | |
import java.util.HashMap; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
|
28 | 0 | public class PopulationServiceMockImpl implements PopulationService { |
29 | |
|
30 | 0 | private Map<String, PopulationInfo> populations = new HashMap<String, PopulationInfo>(); |
31 | 0 | private Map<String, PopulationRuleInfo> populationRules = new HashMap<String, PopulationRuleInfo>(); |
32 | 0 | private Map<String, String> populationToRule = new HashMap<String, String>(); |
33 | |
|
34 | |
private MetaInfo newMeta(ContextInfo context) { |
35 | 0 | MetaInfo meta = new MetaInfo(); |
36 | 0 | meta.setCreateId(context.getPrincipalId()); |
37 | 0 | meta.setCreateTime(new Date()); |
38 | 0 | meta.setUpdateId(context.getPrincipalId()); |
39 | 0 | meta.setUpdateTime(meta.getCreateTime()); |
40 | 0 | meta.setVersionInd("0"); |
41 | 0 | return meta; |
42 | |
} |
43 | |
|
44 | |
private StatusInfo newStatus() { |
45 | 0 | StatusInfo status = new StatusInfo(); |
46 | 0 | status.setSuccess(Boolean.TRUE); |
47 | 0 | return status; |
48 | |
} |
49 | |
|
50 | |
private MetaInfo updateMeta(MetaInfo old, ContextInfo context) { |
51 | 0 | MetaInfo meta = new MetaInfo(old); |
52 | 0 | meta.setUpdateId(context.getPrincipalId()); |
53 | 0 | meta.setUpdateTime(new Date()); |
54 | 0 | meta.setVersionInd((Integer.parseInt(meta.getVersionInd()) + 1) + ""); |
55 | 0 | return meta; |
56 | |
} |
57 | |
|
58 | |
@Override |
59 | |
public StatusInfo applyPopulationRuleToPopulation(String populationRuleId, String populationKey, ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
60 | 0 | this.populationToRule.put(populationKey, populationRuleId); |
61 | 0 | return newStatus(); |
62 | |
} |
63 | |
|
64 | |
@Override |
65 | |
public PopulationInfo createPopulation(PopulationInfo populationInfo, ContextInfo contextInfo) throws DataValidationErrorException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, ReadOnlyException { |
66 | 0 | PopulationInfo copy = new PopulationInfo(populationInfo); |
67 | 0 | copy.setMeta(newMeta(contextInfo)); |
68 | 0 | copy.setId(UUIDHelper.genStringUUID()); |
69 | 0 | populations.put(copy.getId(), copy); |
70 | 0 | return new PopulationInfo(copy); |
71 | |
} |
72 | |
|
73 | |
@Override |
74 | |
public PopulationRuleInfo createPopulationRule(PopulationRuleInfo populationRuleInfo, ContextInfo contextInfo) throws DataValidationErrorException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, ReadOnlyException { |
75 | 0 | PopulationRuleInfo copy = new PopulationRuleInfo(populationRuleInfo); |
76 | 0 | copy.setId(UUIDHelper.genStringUUID()); |
77 | 0 | copy.setMeta(newMeta(contextInfo)); |
78 | 0 | populationRules.put(copy.getId(), copy); |
79 | 0 | return new PopulationRuleInfo(copy); |
80 | |
} |
81 | |
|
82 | |
@Override |
83 | |
public StatusInfo deletePopulation(String populationKey, ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
84 | 0 | if (this.populations.remove(populationKey) == null) { |
85 | 0 | throw new DoesNotExistException(populationKey); |
86 | |
} |
87 | 0 | return newStatus(); |
88 | |
} |
89 | |
|
90 | |
@Override |
91 | |
public StatusInfo deletePopulationRule(String populationRuleId, ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
92 | 0 | if (this.populationRules.remove(populationRuleId) == null) { |
93 | 0 | throw new DoesNotExistException(populationRuleId); |
94 | |
} |
95 | 0 | return newStatus(); |
96 | |
} |
97 | |
|
98 | |
@Override |
99 | |
public List<String> getMembers(String populationKey, ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
100 | 0 | PopulationRuleInfo rule = this.getPopulationRuleForPopulation(populationKey, contextInfo); |
101 | 0 | return new ArrayList(rule.getPersonIds()); |
102 | |
} |
103 | |
|
104 | |
@Override |
105 | |
public PopulationInfo getPopulation(String populationKey, ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
106 | 0 | PopulationInfo info = this.populations.get(populationKey); |
107 | 0 | if (info == null) { |
108 | 0 | throw new DoesNotExistException(populationKey); |
109 | |
} |
110 | 0 | return new PopulationInfo(info); |
111 | |
} |
112 | |
|
113 | |
@Override |
114 | |
public List<String> getPopulationKeysByType(String populationTypeId, ContextInfo contextInfo) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
115 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
116 | |
} |
117 | |
|
118 | |
@Override |
119 | |
public PopulationRuleInfo getPopulationRule(String populationRuleId, ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
120 | 0 | PopulationRuleInfo info = this.populationRules.get(populationRuleId); |
121 | 0 | if (info == null) { |
122 | 0 | throw new DoesNotExistException(populationRuleId); |
123 | |
} |
124 | 0 | return new PopulationRuleInfo(info); |
125 | |
} |
126 | |
|
127 | |
@Override |
128 | |
public PopulationRuleInfo getPopulationRuleForPopulation(String populationKey, ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
129 | 0 | String ruleId = this.populationToRule.get(populationKey); |
130 | 0 | if (ruleId == null) { |
131 | 0 | throw new DoesNotExistException (populationKey); |
132 | |
} |
133 | 0 | PopulationRuleInfo rule = this.getPopulationRule(ruleId, contextInfo); |
134 | 0 | return rule; |
135 | |
} |
136 | |
|
137 | |
@Override |
138 | |
public List<String> getPopulationRuleIdsByType(String populationTypeKey, ContextInfo contextInfo) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
139 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
140 | |
} |
141 | |
|
142 | |
@Override |
143 | |
public List<PopulationRuleInfo> getPopulationRulesByIds(List<String> populationRuleIds, ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
144 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
145 | |
} |
146 | |
|
147 | |
@Override |
148 | |
public List<PopulationInfo> getPopulationsByIds(List<String> populationKeys, ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
149 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
150 | |
} |
151 | |
|
152 | |
@Override |
153 | |
public List<PopulationInfo> getPopulationsForPopulationRule(String populationRuleId, ContextInfo contextInfo) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
154 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
155 | |
} |
156 | |
|
157 | |
@Override |
158 | |
public Boolean isMember(String personId, String populationKey, ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
159 | 0 | if (populationKey.equals(PopulationServiceConstants.EVERYONE_POPULATION_KEY)) { |
160 | 0 | return true; |
161 | |
} |
162 | 0 | PopulationRuleInfo rule = this.getPopulationRuleForPopulation(populationKey, contextInfo); |
163 | 0 | if (rule.getPersonIds().contains(personId)) { |
164 | 0 | return true; |
165 | |
} |
166 | |
|
167 | 0 | return false; |
168 | |
} |
169 | |
|
170 | |
@Override |
171 | |
public StatusInfo removePopulationRuleFromPopulation(String populationRuleId, String populationKey, ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
172 | 0 | this.populationToRule.remove(populationKey); |
173 | 0 | return newStatus (); |
174 | |
} |
175 | |
|
176 | |
@Override |
177 | |
public List<String> searchForPopulationKeys(QueryByCriteria criteria, ContextInfo contextInfo) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
178 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
179 | |
} |
180 | |
|
181 | |
@Override |
182 | |
public List<String> searchForPopulationRuleIds(QueryByCriteria criteria, ContextInfo contextInfo) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
183 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
184 | |
} |
185 | |
|
186 | |
@Override |
187 | |
public List<PopulationRuleInfo> searchForPopulationRules(QueryByCriteria criteria, ContextInfo contextInfo) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
188 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
189 | |
} |
190 | |
|
191 | |
@Override |
192 | |
public List<PopulationInfo> searchForPopulations(QueryByCriteria criteria, ContextInfo contextInfo) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
193 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
194 | |
} |
195 | |
|
196 | |
@Override |
197 | |
public PopulationInfo updatePopulation(String populationKey, PopulationInfo populationInfo, ContextInfo contextInfo) throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, ReadOnlyException, VersionMismatchException { |
198 | 0 | PopulationInfo copy = new PopulationInfo(populationInfo); |
199 | 0 | PopulationInfo old = this.getPopulation(populationKey, contextInfo); |
200 | 0 | if (!old.getMeta().getVersionInd().equals(copy.getMeta().getVersionInd())) { |
201 | 0 | throw new VersionMismatchException(old.getMeta().getVersionInd()); |
202 | |
} |
203 | 0 | copy.setMeta(updateMeta(copy.getMeta(), contextInfo)); |
204 | 0 | this.populations.put(populationInfo.getId(), copy); |
205 | 0 | return new PopulationInfo(copy); |
206 | |
} |
207 | |
|
208 | |
@Override |
209 | |
public PopulationRuleInfo updatePopulationRule(String populationRuleId, PopulationRuleInfo populationRuleInfo, ContextInfo contextInfo) throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, ReadOnlyException, VersionMismatchException { |
210 | 0 | PopulationRuleInfo copy = new PopulationRuleInfo(populationRuleInfo); |
211 | 0 | PopulationRuleInfo old = this.getPopulationRule(populationRuleId, contextInfo); |
212 | 0 | if (!old.getMeta().getVersionInd().equals(copy.getMeta().getVersionInd())) { |
213 | 0 | throw new VersionMismatchException(old.getMeta().getVersionInd()); |
214 | |
} |
215 | 0 | copy.setMeta(updateMeta(copy.getMeta(), contextInfo)); |
216 | 0 | this.populationRules.put(populationRuleInfo.getId(), copy); |
217 | 0 | return new PopulationRuleInfo(copy); |
218 | |
} |
219 | |
|
220 | |
@Override |
221 | |
public List<ValidationResultInfo> validatePopulation(String validationTypeId, PopulationInfo populationInfo, ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
222 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
223 | |
} |
224 | |
|
225 | |
@Override |
226 | |
public List<ValidationResultInfo> validatePopulationRule(String validationTypeKey, PopulationRuleInfo populationInfo, ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
227 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
228 | |
} |
229 | |
|
230 | |
} |