View Javadoc
1   /*
2    * Copyright 2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * 	http://www.osedu.org/licenses/ECL-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.ap.service.mock;
17  
18  
19  import java.util.ArrayList;
20  import java.util.Date;
21  import java.util.LinkedHashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.kuali.student.ap.academicplan.constants.AcademicPlanServiceConstants;
26  import org.kuali.student.ap.academicplan.dto.LearningPlanInfo;
27  import org.kuali.student.ap.academicplan.dto.PlanItemInfo;
28  import org.kuali.student.ap.academicplan.service.AcademicPlanService;
29  import org.kuali.student.common.mock.MockService;
30  import org.kuali.student.common.util.UUIDHelper;
31  import org.kuali.student.r2.common.dto.ContextInfo;
32  import org.kuali.student.r2.common.dto.MetaInfo;
33  import org.kuali.student.r2.common.dto.StatusInfo;
34  import org.kuali.student.r2.common.dto.ValidationResultInfo;
35  import org.kuali.student.r2.common.exceptions.AlreadyExistsException;
36  import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
37  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
38  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
39  import org.kuali.student.r2.common.exceptions.MissingParameterException;
40  import org.kuali.student.r2.common.exceptions.OperationFailedException;
41  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
42  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
43  
44  import javax.jws.WebParam;
45  
46  
47  public class AcademicPlanServiceMockImpl implements MockService, AcademicPlanService
48  {
49  	// cache variable
50  	// The LinkedHashMap is just so the values come back in a predictable order
51  	private Map<String, LearningPlanInfo> learningPlanMap = new LinkedHashMap<String, LearningPlanInfo>();
52  	private Map<String, PlanItemInfo> planItemMap = new LinkedHashMap<String, PlanItemInfo>();
53  
54      @Override
55  	public void clear()
56  	{
57  		this.learningPlanMap.clear ();
58  		this.planItemMap.clear ();
59  	}
60  
61  	
62  	@Override
63  	public LearningPlanInfo getLearningPlan(String learningPlanId, ContextInfo context)
64  		throws DoesNotExistException
65  		      ,InvalidParameterException
66  		      ,MissingParameterException
67  		      ,OperationFailedException
68  	{
69  		// GET_BY_ID
70  		if (!this.learningPlanMap.containsKey(learningPlanId)) {
71  		   throw new DoesNotExistException(learningPlanId);
72  		}
73  		return new LearningPlanInfo(this.learningPlanMap.get (learningPlanId));
74  	}
75  	
76  	@Override
77  	public List<LearningPlanInfo> getLearningPlansByIds(List<String> learningPlanIds, ContextInfo context)
78  		throws InvalidParameterException
79  		      ,MissingParameterException
80  		      ,OperationFailedException
81  		      ,PermissionDeniedException
82  	{
83  		// GET_BY_IDS
84  		List<LearningPlanInfo> list = new ArrayList<LearningPlanInfo> ();
85  		for (String id: learningPlanIds) {
86              try {
87                  list.add (this.getLearningPlan(id, context));
88              } catch (DoesNotExistException e) {
89                  throw new InvalidParameterException("plan id="+id+" does not exist",e);
90              }
91          }
92  		return list;
93  	}
94  	
95  	@Override
96  	public PlanItemInfo getPlanItem(String planItemId, ContextInfo context)
97  		throws DoesNotExistException
98  		      ,InvalidParameterException
99  		      ,MissingParameterException
100 		      ,OperationFailedException
101 	{
102 		// GET_BY_ID
103 		if (!this.planItemMap.containsKey(planItemId)) {
104 		   throw new DoesNotExistException(planItemId);
105 		}
106 		return new PlanItemInfo(this.planItemMap.get (planItemId));
107 	}
108 	
109 	@Override
110 	public List<PlanItemInfo> getPlanItemsByIds(List<String> planItemIds, ContextInfo context)
111 		throws InvalidParameterException
112 		      ,MissingParameterException
113 		      ,OperationFailedException
114 	{
115 		// GET_BY_IDS
116 		List<PlanItemInfo> list = new ArrayList<PlanItemInfo> ();
117 		for (String id: planItemIds) {
118             try {
119                 list.add (this.getPlanItem(id, context));
120             } catch (DoesNotExistException e) {
121                 throw new InvalidParameterException("plan item id="+id+" does not exist",e);
122             }
123 		}
124 		return list;
125 	}
126 	
127 	@Override
128 	public List<PlanItemInfo> getPlanItemsInPlanByType(String learningPlanId, String planItemTypeKey, ContextInfo context)
129 		throws InvalidParameterException
130 		      ,MissingParameterException
131 		      ,OperationFailedException
132 	{
133 		// GET_IDS_BY_TYPE
134 		List<PlanItemInfo> list = new ArrayList<PlanItemInfo> ();
135 		for (PlanItemInfo info: planItemMap.values ()) {
136 			if (learningPlanId.equals(info.getTypeKey())) {
137 				if (planItemTypeKey.equals(info.getTypeKey())) {
138 				    list.add (info);
139 				}
140 			}
141 		}
142 		return list;
143 	}
144 
145     @Override
146 	public List<PlanItemInfo> getPlanItemsInPlanByCategory(String learningPlanId, AcademicPlanServiceConstants.ItemCategory category, ContextInfo context)
147 		throws InvalidParameterException
148 		      ,MissingParameterException
149 		      ,OperationFailedException
150 	{
151 		// GET_INFOS_BY_OTHER
152 		List<PlanItemInfo> list = new ArrayList<PlanItemInfo> ();
153 		for (PlanItemInfo info: planItemMap.values ()) {
154 			if (learningPlanId.equals(info.getLearningPlanId())) {
155 				if (category.equals(info.getCategory())) {
156 				    list.add (new PlanItemInfo(info));
157 				}
158 			}
159 		}
160 		return list;
161 	}
162 	
163 	@Override
164 	public List<PlanItemInfo> getPlanItemsInPlan(String learningPlanId, ContextInfo context)
165 		throws InvalidParameterException
166 		      ,MissingParameterException
167 		      ,OperationFailedException
168 	{
169         // GET_INFOS_BY_OTHER
170         List<PlanItemInfo> list = new ArrayList<PlanItemInfo> ();
171         for (PlanItemInfo info: planItemMap.values ()) {
172             if (learningPlanId.equals(info.getLearningPlanId())) {
173                 list.add (new PlanItemInfo(info));
174             }
175         }
176         return list;
177 	}
178 
179     @Override
180 	public List<PlanItemInfo> getPlanItemsInPlanByTermIdByCategory(String learningPlanId, String termId, org.kuali.student.ap.academicplan.constants.AcademicPlanServiceConstants.ItemCategory category, ContextInfo context)
181 		throws InvalidParameterException
182 		      ,MissingParameterException
183 		      ,OperationFailedException
184 	{
185 		// GET_INFOS_BY_OTHER
186 		List<PlanItemInfo> list = new ArrayList<PlanItemInfo> ();
187 		for (PlanItemInfo info: planItemMap.values ()) {
188 			if (learningPlanId.equals(info.getLearningPlanId())) {
189                 for (String itemTermId : info.getPlanTermIds()) {
190                     if (termId.equals(itemTermId)) {
191                         if (category.equals(info.getCategory())) {
192                             list.add(new PlanItemInfo(info));
193                         }
194                     }
195                 }
196 			}
197 		}
198 		return list;
199 	}
200 
201     @Override
202     public List<PlanItemInfo> getPlanItemsByPlanTermAndCategories(
203             @WebParam(name = "learningPlanId") String learningPlanId, @WebParam(name = "termId") String termId,
204             @WebParam(
205                     name = "categories") List<AcademicPlanServiceConstants.ItemCategory> categories,
206             @WebParam(name = "context") ContextInfo context)
207             throws InvalidParameterException, MissingParameterException, OperationFailedException,
208                    PermissionDeniedException {
209         List<PlanItemInfo> planItemDtos = new ArrayList<PlanItemInfo>();
210         for (AcademicPlanServiceConstants.ItemCategory category : categories) {
211             planItemDtos.addAll(getPlanItemsInPlanByTermIdByCategory(learningPlanId, termId,
212                     category, context));
213         }
214         return planItemDtos;
215     }
216 
217     @Override
218 	public List<PlanItemInfo> getPlanItemsInPlanByRefObjectIdByRefObjectType(String learningPlanId, String refObjectId, String refObjectType, ContextInfo context)
219 		throws InvalidParameterException
220 		      ,MissingParameterException
221 		      ,OperationFailedException
222 	{
223 		// GET_INFOS_BY_OTHER
224 		List<PlanItemInfo> list = new ArrayList<PlanItemInfo> ();
225 		for (PlanItemInfo info: planItemMap.values ()) {
226 			if (learningPlanId.equals(info.getLearningPlanId())) {
227 				if (refObjectId.equals(info.getRefObjectId())) {
228 					if (refObjectType.equals(info.getRefObjectType())) {
229 					    list.add (new PlanItemInfo(info));
230 					}
231 				}
232 			}
233 		}
234 		return list;
235 	}
236 	
237 	@Override
238 	public List<LearningPlanInfo> getLearningPlansForStudentByType(String studentId, String planTypeKey, ContextInfo context)
239 		throws InvalidParameterException
240 		      ,MissingParameterException
241 		      ,OperationFailedException
242 	{
243 		// GET_IDS_BY_TYPE
244 		List<LearningPlanInfo> list = new ArrayList<LearningPlanInfo> ();
245 		for (LearningPlanInfo info: learningPlanMap.values ()) {
246 			if (studentId.equals(info.getStudentId())) {
247 				if (planTypeKey.equals(info.getTypeKey())) {
248 				    list.add (info);
249 				}
250 			}
251 		}
252 		return list;
253 	}
254 	
255 	@Override
256 	public LearningPlanInfo createLearningPlan(LearningPlanInfo learningPlan, ContextInfo context)
257 		throws AlreadyExistsException
258 		      ,DataValidationErrorException
259 		      ,InvalidParameterException
260 		      ,MissingParameterException
261 		      ,OperationFailedException
262 		      ,PermissionDeniedException
263 	{
264 		// CREATE
265 		LearningPlanInfo copy = new LearningPlanInfo(learningPlan);
266 		if (copy.getId() == null) {
267 		   copy.setId(UUIDHelper.genStringUUID());
268 		}
269 		copy.setMeta(newMeta(context));
270 		learningPlanMap.put(copy.getId(), copy);
271 		return new LearningPlanInfo(copy);
272 	}
273 	
274 	@Override
275 	public PlanItemInfo createPlanItem(PlanItemInfo planItem, ContextInfo context)
276 		throws AlreadyExistsException
277 		      ,DataValidationErrorException
278 		      ,InvalidParameterException
279 		      ,MissingParameterException
280 		      ,OperationFailedException
281 		      ,PermissionDeniedException
282 	{
283 		// CREATE
284 		PlanItemInfo copy = new PlanItemInfo(planItem);
285 		if (copy.getId() == null) {
286 		   copy.setId(UUIDHelper.genStringUUID());
287 		}
288 		copy.setMeta(newMeta(context));
289 		planItemMap.put(copy.getId(), copy);
290 		return new PlanItemInfo(copy);
291 	}
292 	
293 	@Override
294 	public LearningPlanInfo updateLearningPlan(String learningPlanId, LearningPlanInfo learningPlan, ContextInfo context)
295 		throws DataValidationErrorException
296 		      ,InvalidParameterException
297 		      ,MissingParameterException
298 		      ,OperationFailedException
299 		      ,PermissionDeniedException
300 		      ,DoesNotExistException
301 		      ,VersionMismatchException
302 	{
303 		// UPDATE
304 		if (!learningPlanId.equals (learningPlan.getId())) {
305 		    throw new InvalidParameterException ("The id parameter does not match the id on the info object");
306 		}
307 		LearningPlanInfo copy = new LearningPlanInfo(learningPlan);
308 		LearningPlanInfo old = this.getLearningPlan(learningPlan.getId(), context);
309 		if (!old.getMeta().getVersionInd().equals(copy.getMeta().getVersionInd())) {
310 		    throw new VersionMismatchException(old.getMeta().getVersionInd());
311 		}
312 		copy.setMeta(updateMeta(copy.getMeta(), context));
313 		this.learningPlanMap .put(learningPlan.getId(), copy);
314 		return new LearningPlanInfo(copy);
315 	}
316 	
317 	@Override
318 	public PlanItemInfo updatePlanItem(String planItemId, PlanItemInfo planItem, ContextInfo context)
319 		throws DataValidationErrorException
320 		      ,InvalidParameterException
321 		      ,MissingParameterException
322 		      ,OperationFailedException
323 		      ,PermissionDeniedException
324 		      ,DoesNotExistException
325 		      ,VersionMismatchException
326 	{
327 		// UPDATE
328 		if (!planItemId.equals (planItem.getId())) {
329 		    throw new InvalidParameterException ("The id parameter does not match the id on the info object");
330 		}
331 		PlanItemInfo copy = new PlanItemInfo(planItem);
332 		PlanItemInfo old = this.getPlanItem(planItem.getId(), context);
333 		if (!old.getMeta().getVersionInd().equals(copy.getMeta().getVersionInd())) {
334 		    throw new VersionMismatchException(old.getMeta().getVersionInd());
335 		}
336 		copy.setMeta(updateMeta(copy.getMeta(), context));
337 		this.planItemMap .put(planItem.getId(), copy);
338 		return new PlanItemInfo(copy);
339 	}
340 	
341 	@Override
342 	public StatusInfo deleteLearningPlan(String learningPlanId, ContextInfo context)
343 		throws DoesNotExistException
344 		      ,InvalidParameterException
345 		      ,MissingParameterException
346 		      ,OperationFailedException
347 		      ,PermissionDeniedException
348 	{
349         List<String> delItemIdList = new ArrayList<String>();
350         for (PlanItemInfo info: planItemMap.values ()) {
351             if (learningPlanId.equals(info.getLearningPlanId())) {
352                 delItemIdList.add(info.getId());
353             }
354         }
355         for (String delId :delItemIdList) {
356             if (this.planItemMap.remove(delId) == null) {
357                 throw new OperationFailedException(delId);
358             }
359         }
360 
361         // DELETE
362 		if (this.learningPlanMap.remove(learningPlanId) == null) {
363 		   throw new OperationFailedException(learningPlanId);
364 		}
365 		return newStatus();
366 	}
367 	
368 	@Override
369 	public StatusInfo deletePlanItem(String planItemId, ContextInfo context)
370 		throws DoesNotExistException
371 		      ,InvalidParameterException
372 		      ,MissingParameterException
373 		      ,OperationFailedException
374 		      ,PermissionDeniedException
375 	{
376 		// DELETE
377 		if (this.planItemMap.remove(planItemId) == null) {
378 		   throw new OperationFailedException(planItemId);
379 		}
380 		return newStatus();
381 	}
382 	
383 	@Override
384 	public List<ValidationResultInfo> validateLearningPlan(String validationType, LearningPlanInfo learningPlanInfo, ContextInfo context)
385 		throws DoesNotExistException
386 		      ,InvalidParameterException
387 		      ,MissingParameterException
388 		      ,OperationFailedException
389 	{
390 		// VALIDATE
391 		return new ArrayList<ValidationResultInfo> ();
392 	}
393 	
394 	@Override
395     public List<ValidationResultInfo> validatePlanItem(String validationType, PlanItemInfo planItemInfo, ContextInfo context)
396             throws DoesNotExistException, InvalidParameterException, MissingParameterException,
397                    OperationFailedException, PermissionDeniedException
398 	{
399 		// VALIDATE
400 		return new ArrayList<ValidationResultInfo> ();
401 	}
402 	
403 	private StatusInfo newStatus() {
404 	     StatusInfo status = new StatusInfo();
405 	     status.setSuccess(Boolean.TRUE);
406 	     return status;
407 	}
408 	
409 	private MetaInfo newMeta(ContextInfo context) {
410 	     MetaInfo meta = new MetaInfo();
411 	     meta.setCreateId(context.getPrincipalId());
412 	     meta.setCreateTime(new Date());
413 	     meta.setUpdateId(context.getPrincipalId());
414 	     meta.setUpdateTime(meta.getCreateTime());
415 	     meta.setVersionInd("0");
416 	     return meta;
417 	}
418 	
419 	private MetaInfo updateMeta(MetaInfo old, ContextInfo context) {
420 	     MetaInfo meta = new MetaInfo(old);
421 	     meta.setUpdateId(context.getPrincipalId());
422 	     meta.setUpdateTime(new Date());
423 	     meta.setVersionInd((Integer.parseInt(meta.getVersionInd()) + 1) + "");
424 	     return meta;
425 	}
426 
427 }
428