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.testreg.service.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.apache.commons.lang.StringUtils; import org.opentestsystem.delivery.testreg.domain.FormatType; import org.opentestsystem.delivery.testreg.domain.HierarchyLevel; import org.opentestsystem.delivery.testreg.domain.Sb11Entity; import org.opentestsystem.delivery.testreg.domain.Sb11NonEntity; import org.opentestsystem.delivery.testreg.domain.Student; import org.opentestsystem.delivery.testreg.domain.StudentGroup; import org.opentestsystem.delivery.testreg.domain.search.StudentGroupSearchRequest; import org.opentestsystem.delivery.testreg.persistence.Sb11NonEntityRepository; import org.opentestsystem.delivery.testreg.persistence.StudentGroupRepository; import org.opentestsystem.delivery.testreg.service.Sb11EntityRepositoryService; import org.opentestsystem.delivery.testreg.service.StudentGroupService; import org.opentestsystem.delivery.testreg.service.StudentService; import org.opentestsystem.delivery.testreg.service.TestRegistrationService; import org.opentestsystem.shared.exception.LocalizedException; import org.opentestsystem.shared.search.domain.AbstractSearchRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; /** * Implementation of StudentGroup Service. */ @Service("studentGroupService") public class StudentGroupServiceImpl<T extends StudentGroup> extends Sb11NonEntityServiceImpl<T> implements StudentGroupService { @Autowired private StudentGroupRepository studentGroupRepository; @Resource private Map<FormatType, Sb11NonEntityRepository<Sb11NonEntity>> nonEntityRepositoryMap; @Resource(name = "sb11NonEntityService") private TestRegistrationService<? extends Sb11NonEntity> sb11NonEntityService; @Resource(name = "sb11EntityService") private Sb11EntityRepositoryService sb11EntityService; @Autowired private StudentService studentService; @Override public T saveDomainObject(final T domainObj) { if (domainObj.getStudentId() != null) { // it must be from the import file domainObj.addStudentIds(domainObj.getStudentId()); setParentEntityMongoId(domainObj); domainObj.addStudentMongoIds( getStudentMongoId(domainObj.getStudentId(), domainObj.getStateAbbreviation())); return super.saveDomainObject(domainObj); } else { // it must be from the UI setParentEntityMongoId(domainObj); for (final String studentId : domainObj.getStudentIds()) { domainObj.addStudentMongoIds(getStudentMongoId(studentId, domainObj.getStateAbbreviation())); } return super.saveDomainObject(domainObj); } } /** * {@inheritDoc} */ @Override public List<T> saveDomainObjects(final List<T> domainObjList) { List<T> savedObjects = new ArrayList<T>(); final List<T> studentGroups = new ArrayList<T>(); if (!CollectionUtils.isEmpty(domainObjList)) { for (final T domainObj : domainObjList) { setParentEntityMongoId(domainObj); final StudentGroup studentGroup = findStudentGroupInACollection(studentGroups, domainObj); if (studentGroup != null) { studentGroup.addStudentIds(domainObj.getStudentId()); studentGroup.addStudentMongoIds( getStudentMongoId(domainObj.getStudentId(), domainObj.getStateAbbreviation())); } else { domainObj.addStudentIds(domainObj.getStudentId()); domainObj.addStudentMongoIds( getStudentMongoId(domainObj.getStudentId(), domainObj.getStateAbbreviation())); studentGroups.add(domainObj); } } savedObjects = super.saveDomainObjects(studentGroups); } return savedObjects; } /** * {@inheritDoc} */ @Override public List<T> updateDomainObjects(final List<T> domainObjList) { List<T> updatedObjects = new ArrayList<T>(); // Getting all StudentGroup's from studentGroup collection final List<T> studentGroups = (List<T>) studentGroupRepository.findAll(); if (!CollectionUtils.isEmpty(domainObjList)) { for (final T domainObj : domainObjList) { setParentEntityMongoId(domainObj); final StudentGroup studentGroup = findStudentGroupInACollection(studentGroups, domainObj); if (studentGroup != null) { studentGroup.addStudentIds(domainObj.getStudentId()); studentGroup.addStudentMongoIds( getStudentMongoId(domainObj.getStudentId(), domainObj.getStateAbbreviation())); } else { domainObj.addStudentIds(domainObj.getStudentId()); domainObj.addStudentMongoIds( getStudentMongoId(domainObj.getStudentId(), domainObj.getStateAbbreviation())); studentGroups.add(domainObj); } } updatedObjects = super.updateDomainObjects(studentGroups); } return updatedObjects; } @SuppressWarnings("unchecked") @Override public void deleteDomainObjects(final List<T> domainObjList) { // StudentGroup is not deleted instead student association is deleted if (!CollectionUtils.isEmpty(domainObjList)) { for (final StudentGroup domainObj : domainObjList) { final T dbStudentGroup = (T) findStudentGroupByAlternateKey(domainObj); if (dbStudentGroup != null) { final String uploadedStudentId = domainObj.getStudentId(); if (dbStudentGroup.getStudentIds().size() == 1 && dbStudentGroup.getStudentIds().contains(uploadedStudentId)) { deleteDomainObject(dbStudentGroup); } else { for (final String studentId : dbStudentGroup.getStudentIds()) { if (uploadedStudentId.equals(studentId)) { dbStudentGroup.removeStudentIds(uploadedStudentId); dbStudentGroup.removeStudentMongoIds(getStudentMongoId(domainObj.getStudentId(), domainObj.getStateAbbreviation())); break; } } updateDomainObject(dbStudentGroup); } } } } } private String getStudentMongoId(final String studentId, final String stateAbbreviation) { final Student student = this.studentService.findByStudentIdAndStateAbbreviation(studentId, stateAbbreviation); if (student != null) { return student.getId(); } else { throw new LocalizedException("student.not.found", new String[] { studentId, stateAbbreviation }); } } private StudentGroup findStudentGroupByAlternateKey(final StudentGroup studentGroupObj) { // find studentGroup final Map<String, String[]> params = new HashMap<String, String[]>(); params.put(StudentGroupSearchRequest.SEARCH_KEY_GROUP_NAME, new String[] { studentGroupObj.getStudentGroupName() }); params.put(StudentGroupSearchRequest.SEARCH_KEY_INST_ID, new String[] { studentGroupObj.getInstitutionIdentifier() }); params.put(StudentGroupSearchRequest.SEARCH_KEY_STATE_ABBR, new String[] { studentGroupObj.getStateAbbreviation() }); final AbstractSearchRequest searchRequest = new StudentGroupSearchRequest(params); return findByAlternateKey(searchRequest, StudentGroup.class); } private void setParentEntityMongoId(final StudentGroup studentGroup) { // get and set district mongo id if (StringUtils.isNotBlank(studentGroup.getDistrictIdentifier())) { final Sb11Entity districtEntity = this.sb11EntityService.findByEntityIdAndStateAbbreviation( studentGroup.getDistrictIdentifier(), studentGroup.getStateAbbreviation(), HierarchyLevel.DISTRICT.getEntityClass()); if (districtEntity != null) { studentGroup.setDistrictEntityMongoId(districtEntity.getId()); } } // get and set institution mongo id if (StringUtils.isNotBlank(studentGroup.getInstitutionIdentifier())) { final Sb11Entity institutionEntity = this.sb11EntityService.findByEntityIdAndStateAbbreviation( studentGroup.getInstitutionIdentifier(), studentGroup.getStateAbbreviation(), HierarchyLevel.INSTITUTION.getEntityClass()); if (institutionEntity != null) { studentGroup.setInstitutionEntityMongoId(institutionEntity.getId()); if (StringUtils.isBlank(studentGroup.getDistrictIdentifier()) && institutionEntity.getParentEntityType() == HierarchyLevel.DISTRICT) { studentGroup.setDistrictIdentifier(institutionEntity.getParentEntityId()); } } } } @Override public List<StudentGroup> findGroupsForStudent(final String studentId, final String stateAbbreviation) { return this.studentGroupRepository.findGroupsForStudent(studentId, stateAbbreviation); } private StudentGroup findStudentGroupInACollection(final List<T> studentGroups, final StudentGroup domainObj) { if (CollectionUtils.isEmpty(studentGroups)) { return null; } final StudentGroup sg = Iterables.find(studentGroups, new Predicate<StudentGroup>() { @Override public boolean apply(final StudentGroup studentGroup) { return com.google.common.base.Objects.equal(studentGroup.getStudentGroupName(), domainObj.getStudentGroupName()) && com.google.common.base.Objects.equal(studentGroup.getInstitutionIdentifier(), domainObj.getInstitutionIdentifier()) && com.google.common.base.Objects.equal(studentGroup.getStateAbbreviation(), domainObj.getStateAbbreviation()); } }, null); return sg; } @Override public List<StudentGroup> findStudentGroups(final String stateAbbreviation, final String institutionIdentifier) { return this.studentGroupRepository.findByStateAbbreviationAndInstitutionIdentifier(stateAbbreviation, institutionIdentifier); } @Override public List<StudentGroup> findStudentGroups(final String institutionMongoId) { return this.studentGroupRepository.findByInstitutionEntityMongoId(institutionMongoId); } @Override public List<StudentGroup> findStudentGroupsByOwnerEmail(final String ownerEmail) { return this.studentGroupRepository.findByOwnerEmail(ownerEmail); } }