001package org.kuali.student.enrollment.class2.courseregistration.service.impl;
002
003import java.util.ArrayList;
004import java.util.HashMap;
005import java.util.List;
006import java.util.Map;
007
008import org.apache.commons.lang.StringUtils;
009import org.kuali.rice.core.api.criteria.QueryByCriteria;
010import org.kuali.rice.krms.api.engine.EngineResults;
011import org.kuali.student.common.util.krms.RulesExecutionConstants;
012import org.kuali.student.r2.core.statement.dto.ReqComponentInfo;
013import org.kuali.student.r2.core.statement.dto.StatementTreeViewInfo;
014import org.kuali.student.r2.core.statement.service.StatementService;
015import org.kuali.student.r2.core.statement.util.PropositionBuilder;
016import org.kuali.student.r2.core.statement.util.RulesEvaluationUtil;
017import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingInfo;
018import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
019import org.kuali.student.enrollment.courseoffering.dto.RegistrationGroupInfo;
020import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
021import org.kuali.student.enrollment.courseregistration.dto.ActivityRegistrationInfo;
022import org.kuali.student.enrollment.courseregistration.dto.CourseRegistrationInfo;
023import org.kuali.student.enrollment.courseregistration.dto.RegistrationRequestInfo;
024import org.kuali.student.enrollment.courseregistration.dto.RegistrationRequestItemInfo;
025import org.kuali.student.enrollment.courseregistration.dto.RegistrationResponseInfo;
026import org.kuali.student.enrollment.courseregistration.service.CourseRegistrationService;
027import org.kuali.student.enrollment.grading.dto.LoadInfo;
028import org.kuali.student.enrollment.lpr.dto.LprTransactionInfo;
029import org.kuali.student.enrollment.lpr.dto.LprTransactionItemInfo;
030import org.kuali.student.enrollment.lpr.dto.LprInfo;
031import org.kuali.student.enrollment.lpr.service.LprService;
032import org.kuali.student.enrollment.roster.dto.LprRosterEntryInfo;
033import org.kuali.student.enrollment.roster.dto.LprRosterInfo;
034import org.kuali.student.enrollment.roster.service.LprRosterService;
035import org.kuali.student.r2.lum.course.service.CourseService;
036import org.kuali.student.r2.common.assembler.AssemblyException;
037import org.kuali.student.r2.common.dto.ContextInfo;
038import org.kuali.student.r2.common.dto.OperationStatusInfo;
039import org.kuali.student.r2.common.dto.StatusInfo;
040import org.kuali.student.r2.common.dto.ValidationResultInfo;
041import org.kuali.student.r2.common.exceptions.AlreadyExistsException;
042import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
043import org.kuali.student.r2.common.exceptions.DoesNotExistException;
044import org.kuali.student.r2.common.exceptions.InvalidParameterException;
045import org.kuali.student.r2.common.exceptions.MissingParameterException;
046import org.kuali.student.r2.common.exceptions.OperationFailedException;
047import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
048import org.kuali.student.r2.common.exceptions.ReadOnlyException;
049import org.kuali.student.r2.common.exceptions.VersionMismatchException;
050import org.kuali.student.r2.common.infc.ValidationResult;
051import org.kuali.student.r2.common.util.constants.LprServiceConstants;
052import org.kuali.student.r2.lum.util.constants.LrcServiceConstants;
053import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
054import org.kuali.student.r2.core.process.service.ProcessService;
055import org.kuali.student.r2.lum.lrc.dto.ResultScaleInfo;
056import org.kuali.student.r2.lum.lrc.infc.ResultValuesGroup;
057import org.kuali.student.r2.lum.lrc.service.LRCService;
058
059
060public class CourseRegistrationServiceImpl 
061    extends AbstractCourseRegistrationService
062    implements CourseRegistrationService {
063
064    private LprService lprService;
065    private CourseOfferingService courseOfferingService;
066//    private LprRosterService lprRosterService;
067    /*    private RegRequestAssembler regRequestAssembler;
068    private RegResponseAssembler regResponseAssembler;
069    private CourseRegistrationAssembler courseRegistrationAssembler;*/
070//    private StatementService statementService;
071//    private CourseService courseService;
072//    private PropositionBuilder propositionBuilder;
073//    private RulesEvaluationUtil rulesEvaluationUtil;
074//    private ProcessService processService;
075//    private LRCService lrcService;
076
077    
078    public LprService getLprService() {
079        return lprService;
080    }
081
082    public void setLprService(LprService lprService) {
083        this.lprService = lprService;
084    }
085
086    public CourseOfferingService getCourseOfferingService() {
087        return courseOfferingService;
088    }
089
090    public void setCourseOfferingService(CourseOfferingService courseOfferingService) {
091        this.courseOfferingService = courseOfferingService;
092    }
093
094        @Override
095        public StatusInfo changeRegistrationRequestState(
096                        String registrationRequestId, String nextStateKey,
097                        ContextInfo contextInfo) throws DoesNotExistException,
098                        InvalidParameterException, MissingParameterException,
099                        OperationFailedException, PermissionDeniedException {
100                // TODO KSENROLL-8712
101                throw new UnsupportedOperationException("not implemented");
102        }
103    
104    
105//
106//    
107//    public StatementService getStatementService() {
108//        return statementService;
109//    }
110//
111//    public void setStatementService(StatementService statementService) {
112//        this.statementService = statementService;
113//    }
114//
115//    public CourseService getCourseService() {
116//        return courseService;
117//    }
118//
119//    public void setCourseService(CourseService courseService) {
120//        this.courseService = courseService;
121//    }
122//
123//    public PropositionBuilder getPropositionBuilder() {
124//        return propositionBuilder;
125//    }
126//
127//    public void setPropositionBuilder(PropositionBuilder propositionBuilder) {
128//        this.propositionBuilder = propositionBuilder;
129//    }
130//
131//    public RulesEvaluationUtil getRulesEvaluationUtil() {
132//        return rulesEvaluationUtil;
133//    }
134//
135//    public void setRulesEvaluationUtil(RulesEvaluationUtil rulesEvaluationUtil) {
136//        this.rulesEvaluationUtil = rulesEvaluationUtil;
137//    }
138
139    
140    /*************
141     Core-slice code
142
143    public ProcessService getProcessService() {
144        return processService;
145    }
146
147    public void setProcessService(ProcessService processService) {
148        this.processService = processService;
149    }
150
151    public LRCService getLrcService() {
152        return lrcService;
153    }
154
155    public void setLrcService(LRCService lrcService) {
156        this.lrcService = lrcService;
157    }
158
159    public RegRequestAssembler getRegRequestAssembler() {
160        return regRequestAssembler;
161    }
162
163    public void setRegRequestAssembler(RegRequestAssembler regRequestAssembler) {
164        this.regRequestAssembler = regRequestAssembler;
165    }
166
167    public RegResponseAssembler getRegResponseAssembler() {
168        return regResponseAssembler;
169    }
170
171    public void setRegResponseAssembler(RegResponseAssembler regResponseAssembler) {
172        this.regResponseAssembler = regResponseAssembler;
173    }
174
175    public CourseRegistrationAssembler getCourseRegistrationAssembler() {
176        return courseRegistrationAssembler;
177    }
178
179    public void setCourseRegistrationAssembler(CourseRegistrationAssembler courseRegistrationAssembler) {
180        this.courseRegistrationAssembler = courseRegistrationAssembler;
181    }
182
183    private List<LprTransactionItemInfo> createModifiedLprTransactionItemsForNew(RegRequestItemInfo regRequestItem, ContextInfo context) throws DoesNotExistException, InvalidParameterException,
184            MissingParameterException, OperationFailedException, PermissionDeniedException, DataValidationErrorException {
185
186        List<LprTransactionItemInfo> newTransactionItems = new ArrayList<LprTransactionItemInfo>();
187
188        String regGroupId = regRequestItem.getNewRegGroupId();
189        RegistrationGroupInfo regGroup = courseOfferingService.getRegistrationGroup(regGroupId, context);
190        LprTransactionItemInfo lprTransactionItem = regRequestAssembler.disassembleItem(regRequestItem, null, context);
191        if (getAvailableSeatsForStudentInRegGroup(regRequestItem.getStudentId(), regGroupId, context) > 0) {
192            List<LprTransactionItemInfo> lprActivityTransactionItems = new ArrayList<LprTransactionItemInfo>();
193            for (String activityOfferingId : regGroup.getActivityOfferingIds()) {
194                LprTransactionItemInfo activtyItemInfo = regRequestAssembler.disassembleItem(regRequestItem, null, context);
195                activtyItemInfo.setId(null);
196                activtyItemInfo.setNewLuiId(activityOfferingId);
197                newTransactionItems.add(activtyItemInfo);
198            }
199
200            String courseOfferingId = regGroup.getCourseOfferingId();
201            LprTransactionItemInfo courseOfferingItemInfo = regRequestAssembler.disassembleItem(regRequestItem, null, context);
202            courseOfferingItemInfo.setNewLuiId(courseOfferingId);
203            courseOfferingItemInfo.setId(null);
204            ArrayList<String> rvgs = new ArrayList<String>();
205            rvgs.add(LrcServiceConstants.RESULT_GROUP_KEY_GRADE_LETTER);
206            courseOfferingItemInfo.setResultValuesGroupKeys(rvgs);
207            newTransactionItems.add(courseOfferingItemInfo);
208
209        } else {
210            // TODO: copy the transaction item and change the type
211            // of the new one to be ADD TO WAITLIST
212            // and then on that item mark the original with a state
213            // of failed
214
215            lprTransactionItem.setTypeKey(LprServiceConstants.LPRTRANS_ITEM_ADD_TO_WAITLIST_TYPE_KEY);
216            lprTransactionItem.setStateKey(LprServiceConstants.LPRTRANS_ITEM_NEW_STATE_KEY);
217        }
218        newTransactionItems.add(lprTransactionItem);
219        return newTransactionItems;
220
221    }
222
223    private List<LprTransactionItemInfo> createModifiedLprTransactionItemsForDrop(RegRequestItemInfo regRequestItem, ContextInfo context) throws DoesNotExistException, InvalidParameterException,
224            MissingParameterException, OperationFailedException, PermissionDeniedException, DataValidationErrorException {
225        List<LprTransactionItemInfo> newTransactionItems = new ArrayList<LprTransactionItemInfo>();
226
227        String regGroupId = regRequestItem.getExistingRegGroupId();
228        RegistrationGroupInfo regGroup = courseOfferingService.getRegistrationGroup(regGroupId, context);
229        List<LprTransactionItemInfo> lprActivityTransactionItems = new ArrayList<LprTransactionItemInfo>();
230        for (String activityOfferingId : regGroup.getActivityOfferingIds()) {
231            LprTransactionItemInfo activtyItemInfo = regRequestAssembler.disassembleItem(regRequestItem, null, context);
232            activtyItemInfo.setId(null);
233            activtyItemInfo.setExistingLuiId(activityOfferingId);
234//            activtyItemInfo.setGroupId(regRequestItem.getId());
235            newTransactionItems.add(activtyItemInfo);
236
237        }
238
239        String courseOfferingId = regGroup.getCourseOfferingId();
240        LprTransactionItemInfo courseOfferingItemInfo = regRequestAssembler.disassembleItem(regRequestItem, null, context);
241        courseOfferingItemInfo.setId(null);
242        courseOfferingItemInfo.setExistingLuiId(courseOfferingId);
243//        courseOfferingItemInfo.setGroupId(regRequestItem.getId());
244        lprActivityTransactionItems.add(courseOfferingItemInfo);
245        newTransactionItems.add(courseOfferingItemInfo);
246        // regRequestAssembler.disassembleItem(regRequestItem, null, context);
247        return newTransactionItems;
248
249    }
250
251    private LprTransactionInfo createModifiedTransactionItems(LprTransactionInfo storedLprTransaction, RegRequestInfo storedRegRequest, ContextInfo context) throws DoesNotExistException,
252            InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, DataValidationErrorException, VersionMismatchException {
253        List<LprTransactionItemInfo> newTransactionItems = new ArrayList<LprTransactionItemInfo>();
254        List<RegRequestItemInfo> regRequestItems = storedRegRequest.getRegRequestItems();
255        for (RegRequestItemInfo regRequestItem : regRequestItems) {
256            if (regRequestItem.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_ADD_TYPE_KEY)
257                    || regRequestItem.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_DROP_TYPE_KEY)
258                    || regRequestItem.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_UPDATE_TYPE_KEY)) {
259                if (regRequestItem.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_ADD_TYPE_KEY)) {
260
261                    newTransactionItems.addAll(createModifiedLprTransactionItemsForNew(regRequestItem, context));
262
263                } else if (regRequestItem.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_DROP_TYPE_KEY)) {
264
265                    newTransactionItems.addAll(createModifiedLprTransactionItemsForDrop(regRequestItem, context));
266                }
267
268            }
269
270        }
271        storedLprTransaction.setLprTransactionItems(newTransactionItems);
272
273        storedLprTransaction = lprService.updateLprTransaction(storedLprTransaction.getId(), storedLprTransaction, context);
274        return lprService.getLprTransaction(storedLprTransaction.getId(), context);
275
276    }
277
278    @Override
279    public List<ValidationResultInfo> checkStudentEligibility(String studentId, ContextInfo context) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
280            PermissionDeniedException {
281
282//        String matchingProcessKey = null;
283//        List<ProcessInfo> studentElgibilityProcesses = processService.getProcessesForProcessCategory(ProcessServiceConstants.PROCESS_CATEGORY_STUDENT_ELIGIBILITY, context);
284//
285//        for (ProcessInfo processInfo : studentElgibilityProcesses) {
286//            if (processInfo.getOwnerOrgId().equals(ProcessServiceConstants.PROCESS_OWNING_ORG_GRAD_SCHOOL)) {
287//                matchingProcessKey = processInfo.getKey();
288//                break;
289//            }
290//        }
291
292        return null;
293    }
294
295    @Override
296    public List<ValidationResultInfo> checkStudentEligibilityForTerm(String studentId, String termKey, ContextInfo context) throws InvalidParameterException, MissingParameterException,
297            OperationFailedException, PermissionDeniedException {
298        return null;
299    }
300
301    @Override
302    public List<ValidationResultInfo> checkStudentEligibiltyForCourseOffering(String studentId, String courseOfferingId, ContextInfo context) throws InvalidParameterException,
303            MissingParameterException, OperationFailedException, PermissionDeniedException {
304
305        CourseOfferingInfo courseOffering = null;
306        try {
307            courseOffering = courseOfferingService.getCourseOffering(courseOfferingId, context);
308        } catch (DoesNotExistException e) {
309            throw new InvalidParameterException("Invalid courseOfferingId, no course offering found with id of: " + courseOfferingId);
310        }
311
312        List<StatementTreeViewInfo> statements;
313
314        try {
315            // TODO fill in nlUsageType and language parameters once the
316            // implementation actually uses them
317            statements = courseService.getCourseStatements(courseOffering.getCourseId(), null, null, null);
318        } catch (Exception e) {
319            throw new OperationFailedException(e.getMessage(), e);
320        }
321
322        List<ValidationResultInfo> resultInfos = new ArrayList<ValidationResultInfo>();
323
324        // find and process statements that the PropositionBuilder can handle
325        for (StatementTreeViewInfo statementTree : statements) {
326            if (PropositionBuilder.TRANSLATABLE_STATEMENT_TYPES.contains(statementTree.getType())) {
327                PropositionBuilder.TranslationResults translationResults = null;
328                try {
329                    translationResults = propositionBuilder.translateStatement(statementTree, null);
330                } catch (org.kuali.student.r2.common.exceptions.InvalidParameterException e) {
331                    throw new OperationFailedException("Exception thrown attempting statement translation for statement: " + statementTree.getId(), e);
332                }
333
334                Map<String, Object> executionFacts = new HashMap<String, Object>();
335                executionFacts.put(RulesExecutionConstants.STUDENT_ID_TERM_NAME, studentId);
336                executionFacts.put(RulesExecutionConstants.COURSE_ID_TO_ENROLL_TERM_NAME, courseOffering.getCourseId());
337                executionFacts.put(RulesExecutionConstants.CONTEXT_INFO_TERM_NAME, context);
338
339                EngineResults engineResults = rulesEvaluationUtil.executeAgenda(translationResults.agenda, executionFacts);
340
341                List<ReqComponentInfo> failedRequirements = rulesEvaluationUtil.getFailedRequirementsFromEngineResults(engineResults, translationResults.reqComponentPropositionMap);
342
343                if (!failedRequirements.isEmpty()) {
344                    for (ReqComponentInfo failedRequirement : failedRequirements) {
345                        ValidationResultInfo resultInfo = new ValidationResultInfo();
346                        resultInfo.setLevel(ValidationResult.ErrorLevel.ERROR);
347                        resultInfo.setElement(failedRequirement.getId());
348                        try {
349                            resultInfo.setMessage(statementService.getNaturalLanguageForReqComponent(failedRequirement.getId(), "KUALI.RULE", "en"));
350                        } catch (Exception e) {
351                            throw new OperationFailedException(e.getMessage(), e);
352                        }
353
354                        resultInfos.add(resultInfo);
355                    }
356                }
357            }
358        }
359
360        if (resultInfos.isEmpty()) {
361            ValidationResultInfo resultInfo = new ValidationResultInfo();
362            resultInfo.setLevel(ValidationResult.ErrorLevel.OK);
363
364            resultInfos.add(resultInfo);
365        }
366
367        return resultInfos;
368    }
369
370    @Override
371    public List<org.kuali.student.r2.common.dto.ValidationResultInfo> checkStudentEligibiltyForRegGroup(String studentId, String regGroupId, ContextInfo context) throws InvalidParameterException,
372            MissingParameterException, OperationFailedException, PermissionDeniedException {
373        // TODO sambit - THIS METHOD NEEDS JAVADOCS
374        return null;
375    }
376
377    @Override
378    public List<RegistrationGroupInfo> getEligibleRegGroupsForStudentInCourseOffering(String studentId, String courseOfferingId, ContextInfo context) throws InvalidParameterException,
379            MissingParameterException, OperationFailedException, PermissionDeniedException {
380        // TODO sambit - THIS METHOD NEEDS JAVADOCS
381        return null;
382    }
383
384    @Override
385    public LoadInfo calculateCreditLoadForTerm(String studentId, String termKey, ContextInfo context) throws InvalidParameterException, MissingParameterException, OperationFailedException,
386            PermissionDeniedException {
387        // TODO sambit - THIS METHOD NEEDS JAVADOCS
388        return null;
389    }
390
391    @Override
392    public LoadInfo calculateCreditLoadForRegRequest(String studentId, RegRequestInfo regRequestInfo, ContextInfo context) throws InvalidParameterException, MissingParameterException,
393            OperationFailedException, PermissionDeniedException {
394        // TODO sambit - THIS METHOD NEEDS JAVADOCS
395        return null;
396    }
397
398    @Override
399    public Integer getAvailableSeatsForCourseOffering(String courseOfferingId, ContextInfo context) throws InvalidParameterException, MissingParameterException, OperationFailedException,
400            PermissionDeniedException {
401        // TODO sambit - THIS METHOD NEEDS JAVADOCS
402        return null;
403    }
404
405    @Override
406    public Integer getAvailableSeatsForRegGroup(String regGroupId, ContextInfo context) throws InvalidParameterException, MissingParameterException, OperationFailedException,
407            PermissionDeniedException {
408        // TODO sambit - THIS METHOD NEEDS JAVADOCS
409        return null;
410    }
411
412    @Override
413    public Integer getAvailableSeatsForStudentInRegGroup(String studentId, String regGroupId, ContextInfo context) throws InvalidParameterException, MissingParameterException,
414            OperationFailedException, PermissionDeniedException {
415        // TODO - rules and waitlist implementation needed
416        return new Integer(3);
417
418    }
419
420    @Override
421    public Integer getAvailableSeatsInSeatPool(String seatpoolId, ContextInfo context) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
422        // TODO sambit - THIS METHOD NEEDS JAVADOCS
423        return null;
424    }
425
426    @Override
427    public RegRequestInfo updateRegRequest(String regRequestId, RegRequestInfo regRequestInfo, ContextInfo context) throws DataValidationErrorException, DoesNotExistException,
428            InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException {
429        RegRequestInfo rrInfo;
430        try {
431            LprTransactionInfo lprTransactionInfo = regRequestAssembler.disassemble(regRequestInfo, context);
432            rrInfo = regRequestAssembler.assemble(lprService.updateLprTransaction(regRequestId, lprTransactionInfo, context), context);
433        } catch (AssemblyException e) {
434            throw new OperationFailedException("AssemblyException : " + e.getMessage());
435        }
436
437        return rrInfo;
438    }
439
440    @Override
441    public StatusInfo deleteRegRequest(String regRequestId, ContextInfo context) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
442            PermissionDeniedException {
443        return lprService.deleteLprTransaction(regRequestId, context);
444    }
445
446  
447
448    @Override
449        public RegRequestInfo createRegRequest(String regRequestTypeKey,
450                        RegRequestInfo regRequestInfo, ContextInfo context)
451                        throws AlreadyExistsException, DoesNotExistException, DataValidationErrorException,
452                        InvalidParameterException, MissingParameterException,
453                        OperationFailedException, PermissionDeniedException,
454                        ReadOnlyException {
455        LprTransactionInfo lprTransaction = null;
456        try {
457            lprTransaction = regRequestAssembler.disassemble(regRequestInfo, context);
458        } catch (AssemblyException e) {
459            throw new OperationFailedException("AssemblyException in disassembling: " + e.getMessage());
460        }
461
462        LprTransactionInfo newLprTransaction = lprService.createLprTransaction(lprTransaction.getTypeKey(), lprTransaction, context);
463        try {
464            newLprTransaction = lprService.getLprTransaction(newLprTransaction.getId(), context);
465        } catch (DoesNotExistException e) {
466            return null;
467        }
468
469        RegRequestInfo createdRegRequestInfo = null;
470        try {
471            createdRegRequestInfo = regRequestAssembler.assemble(newLprTransaction, context);
472        } catch (AssemblyException e) {
473            throw new OperationFailedException("AssemblyException in assembling: " + e.getMessage());
474        }
475
476        return createdRegRequestInfo;
477    }
478
479    @Override
480    public RegRequestInfo createRegRequestFromExisting(String existingRegRequestId, ContextInfo context) throws DoesNotExistException, InvalidParameterException, MissingParameterException,
481            OperationFailedException, DataValidationErrorException, PermissionDeniedException {
482
483        LprTransactionInfo newLprTransaction = lprService.createLprTransactionFromExisting(existingRegRequestId, context);
484        RegRequestInfo createdRegRequestInfo = null;
485        try {
486            createdRegRequestInfo = regRequestAssembler.assemble(newLprTransaction, context);
487        } catch (AssemblyException e) {
488            throw new OperationFailedException("AssemblyException in assembling: " + e.getMessage());
489        }
490        return createdRegRequestInfo;
491    }
492
493    @Override
494    public RegResponseInfo submitRegRequest(String regRequestId, ContextInfo context) throws DoesNotExistException, InvalidParameterException, MissingParameterException,
495            OperationFailedException, PermissionDeniedException, AlreadyExistsException {
496        LprTransactionInfo storedLprTransaction = lprService.getLprTransaction(regRequestId, context);
497
498        RegRequestInfo storedRegRequest = null;
499        try {
500            storedRegRequest = regRequestAssembler.assemble(storedLprTransaction, context);
501        } catch (AssemblyException e) {
502            throw new OperationFailedException("AssemblyException in assembling: " + e.getMessage());
503        }
504
505        try {
506           
507            List<ValidationResultInfo> verificationResultList = verifyRegRequestForSubmission(regRequestId, context);
508            for (ValidationResultInfo verificationResult : verificationResultList) {
509                if (!verificationResult.isOk()) {
510                    throw new DataValidationErrorException("Error while verifying registration request: " + verificationResult.getMessage());
511                }
512            }
513        } catch (DataValidationErrorException dataValidException) {
514            throw new OperationFailedException(dataValidException.getMessage(), dataValidException);
515        }
516
517        // check eligibility requirements
518        for (RegRequestItemInfo item : storedRegRequest.getRegRequestItems()) {
519            if (!StringUtils.equals(LprServiceConstants.LPRTRANS_ITEM_DROP_TYPE_KEY, item.getTypeKey())) {
520                RegistrationGroupInfo regGroup = courseOfferingService.getRegistrationGroup(item.getNewRegGroupId(), context);
521
522                List<ValidationResultInfo> validations = checkStudentEligibiltyForCourseOffering(storedLprTransaction.getRequestingPersonId(), regGroup.getCourseOfferingId(), context);
523                List<String> errorMessages = new ArrayList<String>();
524                for (ValidationResultInfo validation : validations) {
525                    if (validation.isError()) {
526                        errorMessages.add(validation.getMessage());
527                    }
528                }
529
530                if (!errorMessages.isEmpty()) {
531                    RegResponseInfo errorResponse = new RegResponseInfo();
532                    errorResponse.setOperationStatus(new OperationStatusInfo());
533                    errorResponse.getOperationStatus().setErrors(errorMessages);
534                    errorResponse.getOperationStatus().setStatus("FAILURE");
535
536                    return errorResponse;
537                }
538            }
539        }
540
541        LprTransactionInfo multipleItemsTransaction;
542                try {
543                        multipleItemsTransaction = createModifiedTransactionItems(storedLprTransaction, storedRegRequest, context);
544                } catch (DataValidationErrorException dataValidException) {
545                         throw new OperationFailedException(dataValidException.getMessage(), dataValidException);
546                }catch (VersionMismatchException ex) {
547                         throw new OperationFailedException(ex.getMessage(), ex);
548                }
549
550        LprTransactionInfo submittedLprTransaction = lprService.processLprTransaction(multipleItemsTransaction.getId(), context);
551
552        RegResponseInfo returnRegResponse = null;
553        try {
554            returnRegResponse = regResponseAssembler.assemble(submittedLprTransaction, context);
555        } catch (AssemblyException e) {
556            throw new OperationFailedException("AssemblyException in assembling: " + e.getMessage());
557        }
558
559        if (checkSuccessfulRegCriteria(returnRegResponse)) {
560
561            try {
562                createGradeRosterEntryForRegisteredStudent(submittedLprTransaction, context);
563            } catch (DataValidationErrorException dataValidException) {
564                throw new OperationFailedException(dataValidException.getMessage(), dataValidException);
565            }catch (ReadOnlyException dataValidException) {
566                throw new OperationFailedException(dataValidException.getMessage(), dataValidException);
567            }
568
569        }
570
571        return returnRegResponse;
572
573    }
574
575    private boolean checkSuccessfulRegCriteria(RegResponseInfo returnRegResponse) {
576        return true;
577    }
578
579    private void createGradeRosterEntryForRegisteredStudent(LprTransactionInfo submittedLprTransaction, ContextInfo context) throws DataValidationErrorException, AlreadyExistsException,
580            InvalidParameterException, MissingParameterException, DoesNotExistException, OperationFailedException, PermissionDeniedException, ReadOnlyException {
581
582        // TODO: since we're creating at a minimum 3 lprs per course reg we
583        // shouldn't loop through all of them. need a way to get directly to the
584        // grade roster
585        for (LprTransactionItemInfo lprItem : submittedLprTransaction.getLprTransactionItems()) {
586            if (lprItem.getTypeKey().equals(LprServiceConstants.LPRTRANS_ITEM_ADD_TYPE_KEY)) {
587                LprRosterEntryInfo newLprRosterEntry = new LprRosterEntryInfo();
588                newLprRosterEntry.setLprId(lprItem.getLprTransactionItemResult().getResultingLprId());
589                newLprRosterEntry.setTypeKey(LprServiceConstants.LPRROSTER_COURSE_FINAL_GRADE_TYPE_KEY);
590                newLprRosterEntry.setStateKey(LprServiceConstants.LPRROSTER_COURSE_FINAL_GRADEROSTER_READY_STATE_KEY);
591
592                List<LprRosterInfo> lprRosters = lprRosterService.getLprRostersByTypeAndLui(LprServiceConstants.LPRROSTER_COURSE_FINAL_GRADEROSTER_TYPE_KEY, 
593                        lprItem.getNewLuiId(), context);
594                if (lprRosters.size() == 1) {
595                    newLprRosterEntry.setLprRosterId(lprRosters.get(0).getId());
596                    lprRosterService.createLprRosterEntry(
597                            newLprRosterEntry.getLprRosterId(),
598                            newLprRosterEntry.getLprId(), 
599                            newLprRosterEntry.getTypeKey(),
600                            newLprRosterEntry, context);
601                }
602
603            }
604
605        }
606
607    }
608
609    @Override
610    public CourseRegistrationInfo getCourseRegistration(String courseRegistrationId, ContextInfo context) throws DoesNotExistException, InvalidParameterException, MissingParameterException,
611            OperationFailedException, PermissionDeniedException {
612        return new CourseRegistrationInfo();
613
614    }
615
616    @Override
617        public List<CourseRegistrationInfo> getCourseRegistrationsByIds(
618                        List<String> courseRegistrationIds, ContextInfo context)
619                        throws DoesNotExistException, InvalidParameterException,
620                        MissingParameterException, OperationFailedException,
621                        PermissionDeniedException {
622        
623         List<CourseRegistrationInfo> courseRegistrationInfos = new ArrayList<CourseRegistrationInfo>();
624       ResultValuesGroup rvGroup = null;
625       List<LprInfo> lprs = lprService.getLprsByIds(courseRegistrationIds, context);
626       for (LprInfo lpr : lprs) {
627           for (String rvGroupKey : lpr.getResultValuesGroupKeys()) {
628               rvGroup = lrcService.getResultValuesGroup(rvGroupKey, context);
629               if (rvGroup != null) {
630                   ResultScaleInfo resScale = lrcService.getResultScale(rvGroup.getResultScaleKey(), context);
631                   if (resScale != null) {
632                       if (StringUtils.equals(LrcServiceConstants.RESULT_VALUES_GROUP_TYPE_KEY_FIXED, resScale.getTypeKey())) {
633                           courseRegistrationInfos.add(courseRegistrationAssembler.assemble(lpr, rvGroup, context));
634                           break;
635                       }
636                   }
637               }
638
639           }
640       }
641
642       return courseRegistrationInfos;
643        }
644
645        // TODO - post core slice need to ensure that the list has one
646    private List<LprInfo> filterLprByState(List<LprInfo> lprInfoList, String stateKey) {
647        List<LprInfo> filteredLprInfoList = new ArrayList<LprInfo>();
648        for (LprInfo lprInfo : filteredLprInfoList) {
649            if (lprInfo.getStateKey().equals(stateKey)) {
650                filteredLprInfoList.add(lprInfo);
651            }
652        }
653        return filteredLprInfoList;
654    }
655
656   
657
658    @Override
659    public List<CourseRegistrationInfo> getCourseRegistrationsByStudentAndTerm(String studentId, String termKey, ContextInfo context) throws DoesNotExistException, InvalidParameterException,
660                                                                                                                                             MissingParameterException, OperationFailedException, PermissionDeniedException {
661
662        List<CourseRegistrationInfo> courseRegistrationList = new ArrayList<CourseRegistrationInfo>();
663
664        List<LprInfo> courseLprList = lprService.getLprsByPersonForAtpAndLuiType(studentId, termKey, LuiServiceConstants.COURSE_OFFERING_TYPE_KEY, context);
665
666        List<LprInfo> regGroupLprList = lprService.getLprsByPersonForAtpAndLuiType(studentId, termKey, LuiServiceConstants.REGISTRATION_GROUP_TYPE_KEY, context);
667
668        getCourseRegistration(studentId, courseRegistrationList, courseLprList, regGroupLprList, context);
669
670        return courseRegistrationList;
671    }
672
673    private void getCourseRegistration(String studentId, List<CourseRegistrationInfo> courseRegistrationList, List<LprInfo> courseLprList, List<LprInfo> regGroupLprList,
674                                       ContextInfo context) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
675        if (courseLprList != null && !courseLprList.isEmpty()) {
676            for (LprInfo courseOfferinglprInfo : courseLprList) {
677
678                if (courseOfferinglprInfo.getTypeKey().equals(LprServiceConstants.REGISTRANT_TYPE_KEY)) {
679                    CourseOfferingInfo courseOfferingInfo = courseOfferingService.getCourseOffering(courseOfferinglprInfo.getLuiId(), context);
680
681                    List<RegistrationGroupInfo> regGroupList = courseOfferingService.getRegistrationGroupsForCourseOffering(courseOfferinglprInfo.getLuiId(), context);
682
683                    for (RegistrationGroupInfo regGroup : regGroupList) {
684
685                        RegistrationGroupInfo reg = new RegistrationGroupInfo();
686
687                        Map<LprInfo, ActivityOfferingInfo> activtiesLprInfoMap = new HashMap<LprInfo, ActivityOfferingInfo>();
688
689                        for (LprInfo regGroupLprInfo : regGroupLprList) {
690
691                            if (regGroup.getId().equals(regGroupLprInfo.getLuiId()) && regGroupLprInfo.getTypeKey().equals(LprServiceConstants.REGISTRANT_TYPE_KEY)) {
692
693                                reg = courseOfferingService.getRegistrationGroup(regGroup.getId(), context);
694
695                                for (String activityOfferingId : regGroup.getActivityOfferingIds()) {
696
697                                    List<LprInfo> lprsForActivity = lprService.getLprsByPersonAndLui(studentId, activityOfferingId, context);
698
699                                    for (LprInfo activityLpr : lprsForActivity) {
700
701                                        if (activityLpr.getTypeKey().equals(LprServiceConstants.REGISTRANT_TYPE_KEY) && activityLpr.getStateKey().equals(regGroupLprInfo.getStateKey())) {
702
703                                            ActivityOfferingInfo activityOffering = courseOfferingService.getActivityOffering(activityOfferingId, context);
704
705                                            activtiesLprInfoMap.put(activityLpr, activityOffering);
706                                        }
707                                    }
708
709                                }
710
711                                courseRegistrationList.add(courseRegistrationAssembler.assemble(courseOfferinglprInfo, courseOfferingInfo, activtiesLprInfoMap, regGroupLprInfo, reg, context));
712                            }
713
714                        }
715
716                    }
717                }
718
719            }
720        }
721    }
722
723    
724    @Override
725    public List<RegRequestItemInfo> getRegRequestItemsByCourseRegistration(
726            String courseRegistrationId, ContextInfo context)
727            throws DoesNotExistException, InvalidParameterException,
728            MissingParameterException, OperationFailedException,
729            PermissionDeniedException {
730
731        List<RegRequestItemInfo> regrequests = new ArrayList<RegRequestItemInfo>();
732        List<LprTransactionItemInfo> lprTransactionItems = lprService.getLprTransactionItemsByResultingLpr(
733                courseRegistrationId, context);
734        for (LprTransactionItemInfo lprTransactionItem : lprTransactionItems) {
735            regrequests.add(regRequestAssembler.assembleItem(
736                    lprTransactionItem, context));
737        }
738        return regrequests;
739    }
740
741  
742
743
744    @Override
745    public RegRequestInfo getRegRequest(String regRequestId, ContextInfo context) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
746            PermissionDeniedException {
747        LprTransactionInfo lprTransaction = lprService.getLprTransaction(regRequestId, context);
748        RegRequestInfo regRequest = null;
749        try {
750            regRequest = regRequestAssembler.assemble(lprTransaction, context);
751        } catch (AssemblyException e) {
752            throw new OperationFailedException("AssemblyException in assembling: " + e.getMessage());
753        }
754        return regRequest;
755    }
756   
757    @Override
758    public List<CourseRegistrationInfo> getCourseRegistrationsByStudent(String studentId, ContextInfo context) throws DoesNotExistException, InvalidParameterException, MissingParameterException,
759                                                                                                                      OperationFailedException, PermissionDeniedException {
760        List<CourseRegistrationInfo> courseRegistrationList = new ArrayList<CourseRegistrationInfo>();
761
762        List<LprInfo> courseLprList = lprService.getLprsByPersonAndLuiType(studentId, LuiServiceConstants.COURSE_OFFERING_TYPE_KEY, context);
763
764        List<LprInfo> regGroupLprList = lprService.getLprsByPersonAndLuiType(studentId, LuiServiceConstants.REGISTRATION_GROUP_TYPE_KEY, context);
765
766        getCourseRegistration(studentId, courseRegistrationList, courseLprList, regGroupLprList, context);
767
768        return courseRegistrationList;
769    }
770
771        @Override
772        public List<ValidationResultInfo> verifyRegRequestForSubmission(
773                        String regRequestId, ContextInfo context)
774                        throws DataValidationErrorException, InvalidParameterException,
775                        MissingParameterException, OperationFailedException,
776                        PermissionDeniedException {
777                // TODO Auto-generated method stub
778                return null;
779        }
780
781        @Override
782        public List<ValidationResultInfo> validateRegRequest(
783                        String validationTypeKey, String regRequestTypeKey,
784                        RegRequestInfo regRequestInfo, ContextInfo context)
785                        throws DataValidationErrorException, InvalidParameterException,
786                        MissingParameterException, OperationFailedException,
787                        PermissionDeniedException {
788                // TODO Auto-generated method stub
789                return null;
790        }
791
792        @Override
793        public List<RegRequestInfo> getRegRequestsByIds(List<String> regRequestIds,
794                        ContextInfo context) throws DoesNotExistException,
795                        InvalidParameterException, MissingParameterException,
796                        OperationFailedException, PermissionDeniedException {
797                // TODO Auto-generated method stub
798                return null;
799        }
800
801        @Override
802        public List<RegRequestInfo> getUnsubmittedRegRequestsByRequestorAndTerm(
803                        String requestorId, String termId, ContextInfo context)
804                        throws InvalidParameterException, MissingParameterException,
805                        OperationFailedException, PermissionDeniedException {
806                // TODO Auto-generated method stub
807                return null;
808        }
809
810        @Override
811        public List<String> getCourseRegistrationIdsByType(
812                        String courseRegistrationTypeKey, ContextInfo context)
813                        throws InvalidParameterException, MissingParameterException,
814                        OperationFailedException, PermissionDeniedException {
815                // TODO Auto-generated method stub
816                return null;
817        }
818
819        @Override
820        public List<CourseRegistrationInfo> getCourseRegistrationsByStudentAndCourseOffering(
821                        String studentId, String courseOfferingId, ContextInfo context)
822                        throws InvalidParameterException, MissingParameterException,
823                               OperationFailedException, PermissionDeniedException {
824                // TODO Auto-generated method stub
825                return null;
826        }
827
828        
829
830        @Override
831        public List<CourseRegistrationInfo> getCourseRegistrationsByCourseOffering(
832                        String courseOfferingId, ContextInfo context)
833                        throws DoesNotExistException, InvalidParameterException,
834                        MissingParameterException, OperationFailedException,
835                        PermissionDeniedException {
836                // TODO Auto-generated method stub
837                return null;
838        }
839
840        @Override
841        public List<CourseRegistrationInfo> searchForCourseRegistrations(
842                        QueryByCriteria criteria, ContextInfo context)
843                        throws InvalidParameterException, MissingParameterException,
844                        OperationFailedException, PermissionDeniedException {
845                // TODO Auto-generated method stub
846                return null;
847        }
848
849        @Override
850        public List<RegRequestItemInfo> getRegRequestItemsByCourseOfferingAndStudent(
851                        String courseOfferingId, String studentId, ContextInfo context)
852                        throws DoesNotExistException, InvalidParameterException,
853                        MissingParameterException, OperationFailedException,
854                        PermissionDeniedException {
855                // TODO Auto-generated method stub
856                return null;
857        }
858
859        @Override
860        public List<String> searchForCourseRegistrationIds(
861                        QueryByCriteria criteria, ContextInfo context)
862                        throws InvalidParameterException, MissingParameterException,
863                        OperationFailedException, PermissionDeniedException {
864                // TODO Auto-generated method stub
865                return null;
866        }
867
868        @Override
869        public List<ActivityRegistrationInfo> searchForActivityRegistrations(
870                        QueryByCriteria criteria, ContextInfo context)
871                        throws InvalidParameterException, MissingParameterException,
872                        OperationFailedException, PermissionDeniedException {
873                // TODO Auto-generated method stub
874                return null;
875        }
876
877        @Override
878        public List<String> searchForActivityRegistrationIds(
879                        QueryByCriteria criteria, ContextInfo context)
880                        throws InvalidParameterException, MissingParameterException,
881                        OperationFailedException, PermissionDeniedException {
882                // TODO Auto-generated method stub
883                return null;
884        }
885    **********/
886}