Java tutorial
/******************************************************************************* * Educational Online Test Delivery System * Copyright (c) 2013 American Institutes for Research * * Distributed under the AIR Open Source License, Version 1.0 * See accompanying file AIR-License-1_0.txt or at * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf ******************************************************************************/ package org.opentestsystem.delivery.testadmin.service.impl; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.opentestsystem.delivery.testadmin.domain.ParticipationDetailReport; import org.opentestsystem.delivery.testadmin.domain.Status; import org.opentestsystem.delivery.testadmin.domain.TestAdminReport; import org.opentestsystem.delivery.testadmin.domain.TestStatus; import org.opentestsystem.delivery.testadmin.persistence.TestStatusRepository; import org.opentestsystem.delivery.testadmin.service.ParticipationDetailReportService; import org.opentestsystem.delivery.testadmin.service.ScheduleService; import org.opentestsystem.delivery.testreg.domain.Assessment; import org.opentestsystem.delivery.testreg.domain.EligibleStudent; import org.opentestsystem.delivery.testreg.domain.FormatType; import org.opentestsystem.delivery.testreg.domain.Sb11Entity; import org.opentestsystem.delivery.testreg.domain.Student; import org.opentestsystem.delivery.testreg.persistence.EligibleStudentRepository; import org.opentestsystem.delivery.testreg.persistence.StudentRepository; import org.opentestsystem.delivery.testreg.service.Sb11EntityRepositoryService; import org.opentestsystem.delivery.testreg.service.TestRegPersister; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.google.common.base.Function; import com.google.common.collect.Maps; @Service public class ParticipationDetailReportServiceImpl implements ParticipationDetailReportService { @Autowired ScheduleService scheduleService; @Autowired StudentRepository studentRepository; @Autowired private TestRegPersister entityService; @Autowired TestStatusRepository studentTestRepository; @Autowired private Sb11EntityRepositoryService sb11EntityService; @Autowired private EligibleStudentRepository eligibleStudentRepository; static final Function<EligibleStudent, String> STUDENT_GROUP_BY_ID = new Function<EligibleStudent, String>() { @Override public String apply(final EligibleStudent eligibleStudent) { return eligibleStudent.getStudent().getEntityId(); } }; static final Function<TestStatus, String> STUDENT_TEST_REPORT_GROUP_BY_ID = new Function<TestStatus, String>() { @Override public String apply(final TestStatus studentTestReport) { return studentTestReport.getStudentId(); } }; @Override public List<TestAdminReport> buildPartipationReport(String instituionMongoId, Map<String, Assessment> assessmentMap, String testStatus, int opportunity) { List<TestAdminReport> detailReportList = new ArrayList<TestAdminReport>(); Set<String> assessmentIds = assessmentMap.keySet(); Sb11Entity institutionEntity = entityService.findById(instituionMongoId, FormatType.INSTITUTION); for (String assessmentId : assessmentIds) { List<EligibleStudent> eligibleStudents = eligibleStudentRepository .findByInstitutionIdAndAssessmentId(institutionEntity.getId(), assessmentId); Map<String, EligibleStudent> eligibleStudentMap = Maps.uniqueIndex(eligibleStudents, STUDENT_GROUP_BY_ID); List<TestStatus> studentReports = studentTestRepository.findStudentReport(eligibleStudentMap.keySet(), institutionEntity.getStateAbbreviation(), assessmentId, opportunity, testStatus); Map<String, TestStatus> studentTestReportMap = Maps.uniqueIndex(studentReports, STUDENT_TEST_REPORT_GROUP_BY_ID); if (testStatus == null || testStatus.isEmpty()) { for (Entry<String, EligibleStudent> studentEntry : eligibleStudentMap.entrySet()) { Student student = studentEntry.getValue().getStudent(); TestStatus testReport = studentTestReportMap.get(studentEntry.getKey()); detailReportList.add(populateDetailReport(student, testReport, testStatus, assessmentMap.get(assessmentId), opportunity)); } } else if (testStatus.equals(Status.SCHEDULED.name())) { List<TestStatus> testReports = studentTestRepository.findStudentReport(eligibleStudentMap.keySet(), institutionEntity.getStateAbbreviation(), assessmentId, opportunity, null); Map<String, TestStatus> allTestStatusReport = Maps.uniqueIndex(testReports, STUDENT_TEST_REPORT_GROUP_BY_ID); // First find out the not Scheduled tests for (Entry<String, EligibleStudent> studentEntry : eligibleStudentMap.entrySet()) { if (allTestStatusReport.get(studentEntry.getKey()) == null) { Student student = studentEntry.getValue().getStudent(); detailReportList.add(populateDetailReport(student, null, testStatus, assessmentMap.get(assessmentId), opportunity)); } } addTestReport(assessmentMap, testStatus, detailReportList, assessmentId, eligibleStudentMap, studentTestReportMap, opportunity); } else { addTestReport(assessmentMap, testStatus, detailReportList, assessmentId, eligibleStudentMap, studentTestReportMap, opportunity); } } return detailReportList; } private void addTestReport(Map<String, Assessment> assessmentMap, String testStatus, List<TestAdminReport> detailReportList, String assessmentId, Map<String, EligibleStudent> eligibleStudentMap, Map<String, TestStatus> studentTestReportMap, int opportunity) { for (Entry<String, TestStatus> studentTestEntry : studentTestReportMap.entrySet()) { EligibleStudent eligibleStudent = eligibleStudentMap.get(studentTestEntry.getKey()); if (eligibleStudent != null) { Student student = eligibleStudent.getStudent(); TestStatus testReport = studentTestEntry.getValue(); detailReportList.add(populateDetailReport(student, testReport, testStatus, assessmentMap.get(assessmentId), opportunity)); } } } private ParticipationDetailReport populateDetailReport(Student student, TestStatus testReport, String testStatus, Assessment assessment, int opportunity) { ParticipationDetailReport detailReport = new ParticipationDetailReport(); detailReport.setFirstName(student.getFirstName()); detailReport.setLastName(student.getLastName()); detailReport.setMiddleName(student.getMiddleName()); detailReport.setSsId(student.getEntityId()); detailReport.setTestName(assessment.getTestName()); detailReport.setTestLabel(assessment.getTestLabel()); if (testReport != null) { detailReport.setTestStatus(testReport.getStatus()); detailReport.setOpportunity(testReport.getOpportunity().toString()); detailReport.setStarted(testReport.getActualStart()); detailReport.setCompleted(testReport.getActualComplete()); } else { // Set not Scheduled status detailReport.setTestStatus(Status.SCHEDULED); detailReport.setOpportunity(String.valueOf(opportunity)); } return detailReport; } }