View Javadoc

1   /*
2    * Copyright 2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the
6    * License.  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
13   * implied.  See the License for the specific language governing
14   * permissions and limitations under the License.
15   */
16  
17  package org.kuali.student.enrollment.class2.courseregistration.service.impl;
18  
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.core.api.criteria.QueryByCriteria;
21  import org.kuali.student.common.mock.MockService;
22  import org.kuali.student.enrollment.courseoffering.infc.RegistrationGroup;
23  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
24  import org.kuali.student.enrollment.courseregistration.dto.*;
25  import org.kuali.student.enrollment.courseregistration.infc.RegistrationRequest;
26  import org.kuali.student.enrollment.courseregistration.infc.RegistrationRequestItem;
27  import org.kuali.student.enrollment.courseregistration.service.CourseRegistrationService;
28  import org.kuali.student.r2.common.dto.*;
29  import org.kuali.student.r2.common.exceptions.*;
30  import org.kuali.student.r2.common.infc.ValidationResult;
31  import org.kuali.student.r2.common.util.constants.LprServiceConstants;
32  
33  import java.math.BigDecimal;
34  import java.util.*;
35  
36  public class CourseRegistrationServiceMockImpl
37      extends AbstractCourseRegistrationService
38      implements CourseRegistrationService, MockService {
39  
40      private static final Logger LOGGER = Logger.getLogger(CourseRegistrationServiceMockImpl.class);
41  
42      private final Map<String, CourseRegistrationInfo> crMap   = new LinkedHashMap<String, CourseRegistrationInfo>();
43      private final Map<String, ActivityRegistrationInfo> arMap = new LinkedHashMap<String, ActivityRegistrationInfo>();
44      private final Map<String, RegistrationRequestInfo> rrMap  = new LinkedHashMap<String, RegistrationRequestInfo>();
45      private final Map<String, List<ActivityRegistrationInfo>> regMap        = new LinkedHashMap<String, List<ActivityRegistrationInfo>>(); /* cr, ar */
46      private final Map<String, List<RegistrationRequestItemInfo>> xactionMap = new LinkedHashMap<String, List<RegistrationRequestItemInfo>>(); /* cr, ritem */
47      private final Map<String, List<RegistrationRequestItemInfo>> studentMap = new LinkedHashMap<String, List<RegistrationRequestItemInfo>>(); /* stu, ritem */
48      
49      private final static String CREDIT_LIMIT = "15"; 
50      private static long id = 0;
51  
52      private CourseOfferingService coService;
53  
54      
55      @Override
56  	public void clear() {
57  		this.arMap.clear();
58  		this.crMap.clear();
59  		this.regMap.clear();
60  		this.rrMap.clear();
61  		this.studentMap.clear();
62  		this.xactionMap.clear();
63  	}
64  
65  	public CourseOfferingService getCourseOfferingService() {
66          return coService;
67      }
68  
69      public void setCourseOfferingService(CourseOfferingService coService) {
70          this.coService = coService;
71      }
72  
73      @Override
74      public CourseRegistrationInfo getCourseRegistration(String courseRegistrationId, ContextInfo contextInfo)
75          throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
76          
77          CourseRegistrationInfo crInfo = this.crMap.get(courseRegistrationId);
78          if (crInfo == null) {
79              throw new DoesNotExistException(courseRegistrationId + " does not exist");
80          }
81  
82          return (crInfo);
83      }
84  
85      public List<CourseRegistrationInfo> getCourseRegistrations(ContextInfo contextInfo)
86          throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
87  
88          return (Collections.unmodifiableList(new ArrayList<CourseRegistrationInfo>(this.crMap.values())));
89      }
90  
91      @Override
92      public List<CourseRegistrationInfo> getCourseRegistrationsByIds(List<String> courseRegistrationIds, ContextInfo contextInfo)
93          throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
94          
95          List<CourseRegistrationInfo> ret = new ArrayList<CourseRegistrationInfo>();
96          for (String id : courseRegistrationIds) {
97              ret.add(getCourseRegistration(id, contextInfo));
98          }
99          
100         return Collections.unmodifiableList(ret);
101     }
102 
103     @Override
104     public List<String> getCourseRegistrationIdsByType (String courseRegistrationTypeKey, ContextInfo contextInfo)
105         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
106         
107         List<String> ret = new ArrayList<String>();
108         for (CourseRegistrationInfo cr : getCourseRegistrations(contextInfo)) {
109             if (cr.getType().equals(courseRegistrationTypeKey)) {
110                 ret.add(cr.getId());
111             }
112         }
113 
114         return Collections.unmodifiableList(ret);
115     }
116     
117     @Override
118     public List<CourseRegistrationInfo> getCourseRegistrationsByStudent(String studentId, ContextInfo contextInfo) 
119         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
120         
121         List<CourseRegistrationInfo> ret = new ArrayList<CourseRegistrationInfo>();
122         for (CourseRegistrationInfo cr : getCourseRegistrations(contextInfo)) {
123             if (cr.getStudentId().equals(studentId)) {
124                 ret.add(cr);
125             }
126         }
127 
128         return Collections.unmodifiableList(ret);
129     }
130 
131     @Override
132     public List<CourseRegistrationInfo> getCourseRegistrationsByCourseOffering(String courseOfferingId, ContextInfo contextInfo) 
133         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
134         
135         List<CourseRegistrationInfo> ret = new ArrayList<CourseRegistrationInfo>();
136         for (CourseRegistrationInfo cr : getCourseRegistrations(contextInfo)) {
137             if (cr.getCourseOfferingId().equals(courseOfferingId)) {
138                 ret.add(cr);
139             }
140         }
141 
142         return Collections.unmodifiableList(ret);
143     }
144 
145     @Override
146     public List<CourseRegistrationInfo> getCourseRegistrationsByStudentAndCourseOffering(String studentId, String courseOfferingId, ContextInfo contextInfo) 
147         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
148         
149         List<CourseRegistrationInfo> ret = new ArrayList<CourseRegistrationInfo>();
150         for (CourseRegistrationInfo cr : getCourseRegistrationsByStudent(studentId, contextInfo)) {
151             if (cr.getCourseOfferingId().equals(courseOfferingId)) {
152                 ret.add(cr);
153             }
154         }
155 
156         return Collections.unmodifiableList(ret);
157     }
158 
159     @Override
160     public List<CourseRegistrationInfo> getCourseRegistrationsByStudentAndTerm(String studentId, String termId, ContextInfo contextInfo) 
161         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
162         
163         List<CourseRegistrationInfo> ret = new ArrayList<CourseRegistrationInfo>();
164         for (CourseRegistrationInfo cr : getCourseRegistrationsByStudent(studentId, contextInfo)) {
165             try {
166                 if (getCourseOfferingService().getCourseOffering(cr.getCourseOfferingId(), contextInfo).getTermId().equals(termId)) {
167                     ret.add(cr);
168                 }
169             } catch (DoesNotExistException dne) {
170                 throw new OperationFailedException("bad stuff in reg", dne);
171             }
172         }
173 
174         return Collections.unmodifiableList(ret);
175     }
176 
177     @Override
178     public List<String> searchForCourseRegistrationIds(QueryByCriteria criteria, ContextInfo contextInfo)
179         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
180         
181         List<String> ret = new ArrayList<String>();
182         for (CourseRegistrationInfo cr : searchForCourseRegistrations(criteria, contextInfo)) {
183             ret.add(cr.getId());
184         }
185 
186         return Collections.unmodifiableList(ret);
187     }
188 
189     @Override
190     public ActivityRegistrationInfo getActivityRegistration(String activityRegistrationId, ContextInfo contextInfo)
191         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
192 
193         ActivityRegistrationInfo arInfo = this.arMap.get(activityRegistrationId);
194         if (arInfo == null) {
195             throw new DoesNotExistException(activityRegistrationId + " does not exist");
196         }
197 
198         return arInfo;
199     }
200     
201     public List<ActivityRegistrationInfo> getActivityRegistrations(ContextInfo contextInfo)
202         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
203 
204         return Collections.unmodifiableList(new ArrayList<ActivityRegistrationInfo>(this.arMap.values()));
205     }
206 
207     @Override
208     public List<ActivityRegistrationInfo> getActivityRegistrationsByIds(List<String> activityRegistrationIds, ContextInfo contextInfo)
209         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
210 
211         List<ActivityRegistrationInfo> ret = new ArrayList<ActivityRegistrationInfo>();
212         for (String id : activityRegistrationIds) {
213             ret.add(getActivityRegistration(id, contextInfo));
214         }
215         
216         return Collections.unmodifiableList(ret);
217     }
218 
219     @Override
220     public List<String> getActivityRegistrationIdsByType(String activityRegistrationTypeKey, ContextInfo contextInfo)
221         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
222 
223         List<String> ret = new ArrayList<String>();
224         for (ActivityRegistrationInfo ar : getActivityRegistrations(contextInfo)) {
225             if (ar.getType().equals(activityRegistrationTypeKey)) {
226                 ret.add(ar.getId());
227             }
228         }
229 
230         return Collections.unmodifiableList(ret);
231     }
232 
233     @Override
234     public List<ActivityRegistrationInfo> getActivityRegistrationsForCourseRegistration(String courseRegistrationId, ContextInfo contextInfo)
235         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
236 
237         List<ActivityRegistrationInfo> ret = new ArrayList<ActivityRegistrationInfo>();
238         for (ActivityRegistrationInfo ar : this.regMap.get(courseRegistrationId)) {
239             ret.add(ar);
240         }
241 
242         return Collections.unmodifiableList(ret);
243     }
244 
245     @Override
246     public List<ActivityRegistrationInfo> getActivityRegistrationsByStudent(String studentId, ContextInfo contextInfo)
247         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
248 
249         List<ActivityRegistrationInfo> ret = new ArrayList<ActivityRegistrationInfo>();
250         for (ActivityRegistrationInfo ar : getActivityRegistrations(contextInfo)) {
251             if (ar.getStudentId().equals(studentId)) {
252                 ret.add(ar);
253             }
254         }
255 
256         return Collections.unmodifiableList(ret);
257     }
258 
259     @Override
260     public List<ActivityRegistrationInfo> getActivityRegistrationsByActivityOffering(String activityOfferingId, ContextInfo contextInfo)
261         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
262 
263         List<ActivityRegistrationInfo> ret = new ArrayList<ActivityRegistrationInfo>();
264         for (ActivityRegistrationInfo ar : getActivityRegistrations(contextInfo)) {
265             if (ar.getActivityOfferingId().equals(activityOfferingId)) {
266                 ret.add(ar);
267             }
268         }
269 
270         return Collections.unmodifiableList(ret);
271     }
272 
273     @Override
274     public List<ActivityRegistrationInfo> getActivityRegistrationsByStudentAndActivityOffering(String studentId, String activityOfferingId, ContextInfo contextInfo)
275         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
276 
277         List<ActivityRegistrationInfo> ret = new ArrayList<ActivityRegistrationInfo>();
278         for (ActivityRegistrationInfo ar : getActivityRegistrationsByStudent(studentId, contextInfo)) {
279             if (ar.getActivityOfferingId().equals(activityOfferingId)) {
280                 ret.add(ar);
281             }
282         }
283 
284         return Collections.unmodifiableList(ret);
285     }
286 
287     @Override
288     public List<ActivityRegistrationInfo> getActivityRegistrationsByStudentAndTerm(String studentId, String termId, ContextInfo contextInfo)
289         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
290 
291         List<ActivityRegistrationInfo> ret = new ArrayList<ActivityRegistrationInfo>();
292         for (ActivityRegistrationInfo ar : getActivityRegistrationsByStudent(studentId, contextInfo)) {
293             try {
294                 if (getCourseOfferingService().getActivityOffering(ar.getActivityOfferingId(), contextInfo).getTermId().equals(termId)) {
295                     ret.add(ar);
296                 }
297             } catch (DoesNotExistException dne) {
298                 throw new OperationFailedException("bad stuff in reg", dne);
299             }
300         }
301 
302         return Collections.unmodifiableList(ret);
303     }
304 
305     @Override
306     public List<String> searchForActivityRegistrationIds(QueryByCriteria criteria, ContextInfo contextInfo)
307         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
308 
309         List<String> ret = new ArrayList<String>();
310         for (ActivityRegistrationInfo cr : searchForActivityRegistrations(criteria, contextInfo)) {
311             ret.add(cr.getId());
312         }
313 
314         return Collections.unmodifiableList(ret);
315     }
316 
317     @Override
318     public RegistrationRequestInfo getRegistrationRequest(String registrationRequestId, ContextInfo contextInfo)
319         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
320 
321         RegistrationRequestInfo rrInfo = this.rrMap.get(registrationRequestId);
322         if (rrInfo == null) {
323             throw new DoesNotExistException(registrationRequestId + " does not exist");
324         }
325 
326         return rrInfo;
327     }
328 
329     public List<RegistrationRequestInfo> getRegistrationRequests(ContextInfo contextInfo)
330         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
331 
332         return Collections.unmodifiableList(new ArrayList<RegistrationRequestInfo>(this.rrMap.values()));
333     }
334 
335     @Override
336     public List<RegistrationRequestInfo> getRegistrationRequestsByIds(List<String> registrationRequestIds, ContextInfo contextInfo)
337         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
338 
339         List<RegistrationRequestInfo> ret = new ArrayList<RegistrationRequestInfo>();
340         for (String id : registrationRequestIds) {
341             ret.add(getRegistrationRequest(id, contextInfo));
342         }
343         
344         return Collections.unmodifiableList(ret);
345     }
346 
347     @Override
348     public List<String> getRegistrationRequestIdsByType(String registrationRequestTypeKey, ContextInfo contextInfo)
349         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
350 
351         List<String> ret = new ArrayList<String>();
352         for (RegistrationRequestInfo rr : getRegistrationRequests(contextInfo)) {
353             if (rr.getType().equals(registrationRequestTypeKey)) {
354                 ret.add(rr.getId());
355             }
356         }
357 
358         return Collections.unmodifiableList(ret);
359     }
360 
361     @Override
362     public List<RegistrationRequestInfo> getRegistrationRequestsByRequestor(String personId, ContextInfo contextInfo)
363         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
364 
365         List<RegistrationRequestInfo> ret = new ArrayList<RegistrationRequestInfo>();
366         for (RegistrationRequestInfo rr : getRegistrationRequests(contextInfo)) {
367             if (rr.getRequestorId().equals(personId)) {
368                 ret.add(rr);
369             }
370         }
371 
372         return Collections.unmodifiableList(ret);
373     }
374 
375     @Override
376     public List<RegistrationRequestInfo> getUnsubmittedRegistrationRequestsByRequestorAndTerm(String requestorId, String termId, ContextInfo contextInfo)
377         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
378 
379         List<RegistrationRequestInfo> ret = new ArrayList<RegistrationRequestInfo>();
380         for (RegistrationRequestInfo rr : getRegistrationRequestsByRequestor(requestorId, contextInfo)) {
381             if (rr.getStateKey().equals(LprServiceConstants.LPRTRANS_NEW_STATE_KEY) && rr.getTermId().equals(termId)) {
382                 ret.add(rr);
383             }
384         }
385 
386         return Collections.unmodifiableList(ret);
387     }
388 
389     @Override
390     public List<String> searchForRegistrationRequestIds(QueryByCriteria criteria, ContextInfo contextInfo)
391         throws InvalidParameterException,MissingParameterException, OperationFailedException, PermissionDeniedException {
392 
393         List<String> ret = new ArrayList<String>();
394         for (RegistrationRequestInfo rr : searchForRegistrationRequests(criteria, contextInfo)) {
395             ret.add(rr.getId());
396         }
397 
398         return Collections.unmodifiableList(ret);
399     }
400 
401     @Override
402     public List<ValidationResultInfo> validateRegistrationRequest(String validationTypeKey, String registrationRequestTypeKey, RegistrationRequestInfo registrationRequestInfo, ContextInfo contextInfo)
403         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
404 
405         List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>();
406 
407         if (!registrationRequestTypeKey.equals(LprServiceConstants.LPRTRANS_REGISTER_TYPE_KEY)) {
408             results.add(makeValidationError("RegistrationRequest.type", "unknown type"));
409         }
410 
411         if (registrationRequestInfo.getRequestorId() == null) {
412             results.add(makeValidationError("RegistrationRequest.requestorId", "cannot be null"));
413         }
414 
415         if (registrationRequestInfo.getTermId() == null) {
416             results.add(makeValidationError("RegistrationRequest.termId", "cannot be null"));
417         }
418 
419         if (registrationRequestInfo.getRegistrationRequestItems() == null) {
420             results.add(makeValidationError("RegistrationRequest.registrationRequestItems", "cannot be null"));
421         } else if (registrationRequestInfo.getRegistrationRequestItems().size() == 0) {
422             results.add(makeValidationError("RegistrationRequest.registrationRequestItems", "cannot be empty"));
423         }
424 
425         for (RegistrationRequestItem item : registrationRequestInfo.getRegistrationRequestItems()) {
426             if (item.getStudentId() == null) {
427                 results.add(makeValidationError("RegistrationRequestItem.studentId", "cannot be null"));
428             }
429 
430             if (item.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_ADD_TYPE_KEY)) {
431                 if (item.getNewRegistrationGroupId() == null) {
432                     results.add(makeValidationError("RegistrationRequestItem.newRegistrationGroupId", "cannot be null"));
433                 }
434 
435                 if (item.getExistingRegistrationGroupId() != null) {
436                     results.add(makeValidationError("RegistrationRequestItem.existingRegistrationGroupId", "must be null"));
437                 }
438 
439                 if (item.getCredits() == null) {
440                     results.add(makeValidationError("RegistrationRequestItem.credits", "cannot be null"));
441                 }
442 
443                 if (item.getGradingOptionId() == null) {
444                     results.add(makeValidationError("RegistrationRequestItem.gradingOptionId", "cannot be null"));
445                 }
446             } else if (item.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_DROP_TYPE_KEY)) {
447                 if (item.getNewRegistrationGroupId() != null) {
448                     results.add(makeValidationError("RegistrationRequestItem.newRegistrationGroupId", "must be null"));
449                 }
450 
451                 if (item.getExistingRegistrationGroupId() == null) {
452                     results.add(makeValidationError("RegistrationRequestItem.existingRegistrationGroupId", "cannot be null"));
453                 }
454 
455                 if (item.getCredits() != null) {
456                     results.add(makeValidationError("RegistrationRequestItem.credits", "must be null"));
457                 }
458 
459                 if (item.getGradingOptionId() != null) {
460                     results.add(makeValidationError("RegistrationRequestItem.gradingOptionId", "must be null"));
461                 }
462             } else if (item.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_SWAP_TYPE_KEY)) {
463                 if (item.getNewRegistrationGroupId() == null) {
464                     results.add(makeValidationError("RegistrationRequestItem.newRegistrationGroupId", "cannot be null"));
465                 }
466 
467                 if (item.getExistingRegistrationGroupId() == null) {
468                     results.add(makeValidationError("RegistrationRequestItem.existingRegistrationGroupId", "cannot be null"));
469                 }
470 
471                 if (item.getCredits() == null) {
472                     results.add(makeValidationError("RegistrationRequestItem.credits", "cannot be null"));
473                 }
474 
475                 if (item.getGradingOptionId() == null) {
476                     results.add(makeValidationError("RegistrationRequestItem.gradingOptionId", "cannot be null"));
477                 }
478             } else if (item.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_UPDATE_TYPE_KEY)) {
479                 if (item.getNewRegistrationGroupId() != null) {
480                     results.add(makeValidationError("RegistrationRequestItem.newRegistrationGroupId", "must be null"));
481                 }
482 
483                 if (item.getExistingRegistrationGroupId() == null) {
484                     results.add(makeValidationError("RegistrationRequestItem.existingRegistrationGroupId", "cannot be null"));
485                 }
486             } else {
487                 results.add(makeValidationError("RegistrationRequestItem.typeKey", "unknown item type"));
488             }
489         }
490 
491         return Collections.unmodifiableList(results);
492     }
493 
494     @Override
495     public RegistrationRequestInfo createRegistrationRequest(String registrationRequestTypeKey, RegistrationRequestInfo registrationRequestInfo, ContextInfo contextInfo)
496         throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, ReadOnlyException {
497 
498         List<ValidationResultInfo> results = validateRegistrationRequest("any", registrationRequestTypeKey, registrationRequestInfo, contextInfo);
499         if (results.size() > 0) {
500             throw new DataValidationErrorException("data validation error, use validateRegistrationRequest()");
501         }
502 
503         RegistrationRequestInfo rr = new RegistrationRequestInfo(registrationRequestInfo);
504         rr.setId(newId());
505         rr.setTypeKey(registrationRequestTypeKey);
506         rr.setStateKey(LprServiceConstants.LPRTRANS_NEW_STATE_KEY);
507         rr.setMeta(newMeta(contextInfo));
508         
509         this.rrMap.put(rr.getId(), rr);
510         return rr;
511     }
512 
513     @Override
514     public RegistrationRequestInfo createRegistrationRequestFromExisting(String registrationRequestId, ContextInfo contextInfo)
515         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
516 
517         RegistrationRequestInfo rr = new RegistrationRequestInfo(getRegistrationRequest(registrationRequestId, contextInfo));
518         rr.setMeta(null);
519         rr.setId(newId());
520         rr.setStateKey(LprServiceConstants.LPRTRANS_NEW_STATE_KEY);
521 
522         List<ValidationResultInfo> results = validateRegistrationRequest("any", rr.getTypeKey(), rr, contextInfo);
523         if (results.size() > 0) {
524             throw new OperationFailedException("data validation error in copy");
525         }
526 
527         this.rrMap.put(rr.getId(), rr);
528 
529         return rr;
530     }
531 
532     @Override
533     public RegistrationRequestInfo updateRegistrationRequest(String registrationRequestId, RegistrationRequestInfo registrationRequestInfo, ContextInfo contextInfo)
534         throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, ReadOnlyException, VersionMismatchException {
535 
536         if (!registrationRequestId.equals(registrationRequestInfo.getId())) {
537             throw new InvalidParameterException("The id parameter does not match the id on the info object");
538         }
539 
540         RegistrationRequestInfo copy = new RegistrationRequestInfo(registrationRequestInfo);
541         RegistrationRequestInfo old = getRegistrationRequest(registrationRequestId, contextInfo);
542         if (!old.getMeta().getVersionInd().equals(copy.getMeta().getVersionInd())) {
543             throw new VersionMismatchException(old.getMeta().getVersionInd());
544         }
545 
546         copy.setMeta(updateMeta(copy.getMeta(), contextInfo));
547         this.rrMap.put(registrationRequestId, copy);
548 
549         return new RegistrationRequestInfo(copy);
550     }
551 
552     @Override
553     public StatusInfo deleteRegistrationRequest(String registrationRequestId, ContextInfo contextInfo)
554         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
555 
556         RegistrationRequest rr = getRegistrationRequest(registrationRequestId, contextInfo);
557         if (rr == null) {
558             throw new DoesNotExistException(registrationRequestId + " not found");
559         }
560 
561         if (rr.getStateKey().equals(LprServiceConstants.LPRTRANS_SUCCEEDED_STATE_KEY) ||
562             rr.getStateKey().equals(LprServiceConstants.LPRTRANS_FAILED_STATE_KEY)) {
563             throw new OperationFailedException("cannot delete a submitted registration request, sorry");
564         }
565 
566         this.rrMap.remove(registrationRequestId);
567         return new StatusInfo();
568     }
569 
570     @Override
571     public List<ValidationResultInfo> verifyRegistrationRequestForSubmission(String registrationRequestId, ContextInfo contextInfo)
572         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
573 
574         // it already data validated and we have nothing else to say
575         return Collections.EMPTY_LIST;
576     }
577 
578     @Override
579     public RegistrationResponseInfo submitRegistrationRequest(String registrationRequestId, ContextInfo contextInfo)
580         throws AlreadyExistsException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
581 
582         /* look up the registration request */
583         RegistrationRequestInfo rr = getRegistrationRequest(registrationRequestId, contextInfo);
584         if (!rr.getStateKey().equals(LprServiceConstants.LPRTRANS_NEW_STATE_KEY)) {
585             throw new AlreadyExistsException(registrationRequestId + " already submitted");
586         }
587 
588         /* verify the registration request */
589         List<ValidationResultInfo> results = verifyRegistrationRequestForSubmission(registrationRequestId, contextInfo);
590         if (results.size() > 0) {
591             rr.setStateKey(LprServiceConstants.LPRTRANS_FAILED_STATE_KEY);
592             throw new OperationFailedException("registration failed, try verifying next time");
593         }
594 
595         /* check to see if the student is already regsistered in the course offering */
596         //...... TODO
597 
598         /* create the response */
599         RegistrationResponseInfo response = new RegistrationResponseInfo();
600         response.setRegistrationRequestId(rr.getId());
601         
602         /* cddr through the list of request items */
603         for (RegistrationRequestItemInfo item : rr.getRegistrationRequestItems()) {
604 
605             /* get the registration groups */
606             RegistrationGroup newrg = getCourseOfferingService().getRegistrationGroup(item.getNewRegistrationGroupId(), contextInfo);
607             //RegistrationGroup existingrg = getCourseOfferingService().getRegistrationGroup(item.getExistingRegistrationGroupId(), contextInfo);
608 
609             /* an ADD */
610             if (item.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_ADD_TYPE_KEY)) {
611                 
612                 /* create the course registration */
613                 CourseRegistrationInfo cr = new CourseRegistrationInfo();
614                 cr.setId(newId());
615                 cr.setTypeKey(LprServiceConstants.REGISTRANT_TYPE_KEY);
616                 cr.setStateKey(LprServiceConstants.REGISTERED_STATE_KEY);
617                 cr.setMeta(newMeta(contextInfo));
618                 cr.setStudentId(item.getStudentId());
619                 cr.setCourseOfferingId(newrg.getCourseOfferingId());
620                 cr.setCredits(item.getCredits());
621                 cr.setGradingOptionId(item.getGradingOptionId());
622                 this.crMap.put(cr.getId(), cr);
623 
624                 /* update the local db */
625                 List<RegistrationRequestItemInfo> items = this.xactionMap.get(cr.getId());
626                 if (items == null) {
627                     items = new ArrayList<RegistrationRequestItemInfo>();
628                     this.xactionMap.put(cr.getId(), items);
629                 }
630                 
631                 items.add(item);
632 
633                 items = this.studentMap.get(item.getStudentId());
634                 if (items == null) {
635                     items = new ArrayList<RegistrationRequestItemInfo>();
636                     this.xactionMap.put(item.getStudentId(), items);
637                 }
638                 
639                 items.add(item);
640 
641                 /* create the activity registrations */
642                 for (String activityId : newrg.getActivityOfferingIds()) {
643                     ActivityRegistrationInfo ar = new ActivityRegistrationInfo();
644                     ar.setId(newId());
645                     ar.setTypeKey(LprServiceConstants.REGISTRANT_TYPE_KEY);
646                     ar.setStateKey(LprServiceConstants.REGISTERED_STATE_KEY);
647                     ar.setMeta(newMeta(contextInfo));
648                     ar.setStudentId(item.getStudentId());
649                     ar.setActivityOfferingId(activityId);
650                     this.arMap.put(ar.getId(), ar);
651 
652                     /* update the local db */
653                     List<ActivityRegistrationInfo> ars = this.regMap.get(cr.getId());
654                     if (ars == null) {
655                         ars = new ArrayList<ActivityRegistrationInfo>();
656                         this.regMap.put(cr.getId(), ars);
657                     }
658                     
659                     ars.add(ar);
660                 }
661                 
662                 /* set the state for the item */
663                 item.setStateKey(LprServiceConstants.LPRTRANS_SUCCEEDED_STATE_KEY);
664 
665                 /* add the response item */
666                 RegistrationResponseItemInfo responseItem = new RegistrationResponseItemInfo();
667                 responseItem.setRegistrationRequestItemId(item.getId());
668                 OperationStatusInfo status = new OperationStatusInfo();
669                 status.setStatus("ok");
670                 responseItem.setOperationStatus(status);
671                 responseItem.setCourseRegistrationId(cr.getId());
672                 response.getRegistrationResponseItems().add(responseItem);
673 
674             } else if (item.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_DROP_TYPE_KEY)) {
675                 LOGGER.debug("Empty If Statement: No action defined for " + LprServiceConstants.LPRTRANS_ITEM_DROP_TYPE_KEY);
676                 throw new UnsupportedOperationException("Empty If Statement: No action defined for " + LprServiceConstants.LPRTRANS_ITEM_DROP_TYPE_KEY);
677             } else if (item.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_SWAP_TYPE_KEY)) {
678                 LOGGER.debug("Empty If Statement: No action defined for " + LprServiceConstants.LPRTRANS_ITEM_SWAP_TYPE_KEY);
679                 throw new UnsupportedOperationException("Empty If Statement: No action defined for " + LprServiceConstants.LPRTRANS_ITEM_SWAP_TYPE_KEY);
680             } else if (item.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_UPDATE_TYPE_KEY)) {
681                 LOGGER.debug("Empty If Statement: No action defined for " + LprServiceConstants.LPRTRANS_ITEM_UPDATE_TYPE_KEY);
682                 throw new UnsupportedOperationException("Empty If Statement: No action defined for " + LprServiceConstants.LPRTRANS_ITEM_UPDATE_TYPE_KEY);
683             } 
684         }
685 
686         rr.setStateKey(LprServiceConstants.LPRTRANS_SUCCEEDED_STATE_KEY);
687 
688         OperationStatusInfo status = new OperationStatusInfo();
689         status.setStatus("ok");
690 
691         return response;
692     }
693 
694     @Override
695     public List<RegistrationRequestItemInfo> getRegistrationRequestItemsForCourseRegistration(String courseRegistrationId, ContextInfo contextInfo)
696         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
697 
698         List <RegistrationRequestItemInfo> items = this.xactionMap.get(courseRegistrationId);
699         if (items == null) {
700             return Collections.EMPTY_LIST;
701         } else {
702             return Collections.unmodifiableList(items);
703         }
704     }
705 
706     @Override
707     public List<RegistrationRequestItemInfo> getRegistrationRequestItemsByCourseOfferingAndStudent(String courseOfferingId, String studentId, ContextInfo contextInfo)
708         throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
709 
710         List <RegistrationRequestItemInfo> ret = new ArrayList<RegistrationRequestItemInfo>();
711 
712         for (RegistrationRequestItemInfo rri : this.studentMap.get(studentId)) {
713             try {
714                 for (RegistrationGroup rg : getCourseOfferingService().getRegistrationGroupsForCourseOffering(courseOfferingId, contextInfo)) {
715                     if (rri.getNewRegistrationGroupId().equals(rg.getId()) ||
716                         rri.getExistingRegistrationGroupId().equals(rg.getId())) {
717                         ret.add(rri);
718                     }
719                 }
720             } catch (DoesNotExistException dne) {} // CO should throw this here
721         }
722 
723         return Collections.unmodifiableList(ret);
724     }
725 
726     @Override
727     public List<ValidationResultInfo> checkStudentEligibility(String studentId, ContextInfo contextInfo)
728         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
729 
730         List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>();
731         if (studentId.equals("norm")) {
732             results.add(makeValidationError("basic elegibility", "norm can't register"));
733         }
734 
735         return Collections.unmodifiableList(results);
736     }
737 
738    @Override
739     public List<ValidationResultInfo> checkStudentEligibilityForTerm(String studentId, String termId, ContextInfo contextInfo)
740         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
741 
742         List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>();
743         if (studentId.equals("norm")) {
744             results.add(makeValidationError("elegibility for term", "norm can't register this term"));
745         }
746 
747         return Collections.unmodifiableList(results);
748     }
749 
750     @Override
751     public List<ValidationResultInfo> checkStudentEligibiltyForCourseOffering(String studentId, String courseOfferingId, ContextInfo contextInfo)
752         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
753 
754         List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>();
755         if (studentId.equals("norm") && courseOfferingId.equals("recursion101")) {
756             results.add(makeValidationError("elegibility forcourse offering", "norm can't register in this class"));
757         }
758 
759         return Collections.unmodifiableList(results);
760     }
761 
762     @Override
763     public List<ValidationResultInfo> checkStudentEligibiltyForRegistrationGroup(String studentId, String registrationGroupId, ContextInfo contextInfo)
764         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
765 
766         return Collections.EMPTY_LIST;
767     }
768 
769     @Override
770     public CreditLoadInfo calculateCreditLoadForStudentRegistrationRequest(String registrationRequestId, String studentId, ContextInfo contextInfo)
771         throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
772 
773         RegistrationRequest rr = getRegistrationRequest(registrationRequestId, contextInfo);
774         CreditLoadInfo load = new CreditLoadInfo();
775         load.setStudentId(studentId);
776         load.setCreditLimit(CREDIT_LIMIT);
777 
778         for (RegistrationRequestItem item : rr.getRegistrationRequestItems()) {
779             if (item.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_ADD_TYPE_KEY)) {
780                 load.setAdditionalCredits((new BigDecimal(item.getCredits())).add(new BigDecimal(load.getAdditionalCredits())).toString());
781             } else if (item.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_DROP_TYPE_KEY)) {
782                 LOGGER.debug("Empty If Statement: No action defined for " + LprServiceConstants.LPRTRANS_ITEM_DROP_TYPE_KEY);
783                 // TODO: figure out credits
784             }
785         }
786 
787         return load;
788     }
789 
790     @Override
791 	public StatusInfo changeRegistrationRequestState(
792 			String registrationRequestId, String nextStateKey,
793 			ContextInfo contextInfo) throws DoesNotExistException,
794 			InvalidParameterException, MissingParameterException,
795 			OperationFailedException, PermissionDeniedException {
796 		// TODO KSENROLL-8712
797 		throw new UnsupportedOperationException("not implemented");
798 	}
799     
800 
801     private static String newId() {
802         return Long.toString(++id);
803     }
804 
805     private static MetaInfo newMeta(ContextInfo contextInfo) {
806         MetaInfo meta = new MetaInfo();
807         meta.setCreateId(contextInfo.getPrincipalId());
808         meta.setCreateTime(new Date());
809         meta.setUpdateId(contextInfo.getPrincipalId());
810         meta.setUpdateTime(meta.getCreateTime());
811         meta.setVersionInd("0");
812         return meta;
813     }
814 
815     private static StatusInfo newStatus() {
816         StatusInfo status = new StatusInfo();
817         status.setSuccess(Boolean.TRUE);
818         return status;
819     }
820 
821     private static MetaInfo updateMeta(MetaInfo old, ContextInfo contextInfo) {
822         MetaInfo meta = new MetaInfo(old);
823         meta.setUpdateId(contextInfo.getPrincipalId());
824         meta.setUpdateTime(new Date());
825         meta.setVersionInd((Integer.parseInt(meta.getVersionInd()) + 1) + "");
826         return meta;
827     }
828 
829     private static ValidationResultInfo makeValidationError(String element, String message) {
830         ValidationResultInfo vr = new ValidationResultInfo();
831         vr.setLevel(ValidationResult.ErrorLevel.ERROR);
832         vr.setElement(element);
833         vr.setMessage(message);
834         return vr;
835     }
836 }