org.opentestsystem.delivery.testreg.service.impl.ExternalStudentServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.delivery.testreg.service.impl.ExternalStudentServiceImpl.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2016 Regents of the University of California
 *
 * 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
 *
 * SmarterApp Open Source Assessment Software Project: http://smarterapp.org
 * Developed by Fairway Technologies, Inc. (http://fairwaytech.com)
 * for the Smarter Balanced Assessment Consortium (http://smarterbalanced.org)
 ******************************************************************************/

package org.opentestsystem.delivery.testreg.service.impl;

import org.apache.commons.lang.StringUtils;
import org.opentestsystem.delivery.testadmin.service.TestStatusService;
import org.opentestsystem.delivery.testreg.domain.FormatType;
import org.opentestsystem.delivery.testreg.domain.Student;
import org.opentestsystem.delivery.testreg.domain.StudentUpsert;
import org.opentestsystem.delivery.testreg.domain.amqp.StudentSecurity;
import org.opentestsystem.delivery.testreg.persistence.criteria.FileUploadSecurityValidator;
import org.opentestsystem.delivery.testreg.service.ExternalStudentService;
import org.opentestsystem.delivery.testreg.service.StudentService;
import org.opentestsystem.delivery.testreg.service.TestRegPersister;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.exceptions.UnauthorizedUserException;
import org.springframework.stereotype.Service;
import org.springframework.validation.FieldError;

import java.util.List;
import java.util.Objects;

@Service
public class ExternalStudentServiceImpl implements ExternalStudentService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExternalStudentServiceImpl.class);

    @Autowired
    private TestRegPersister studentRepository;

    @Autowired
    private StudentService studentService;

    @Autowired
    private TestStatusService testStatusService;

    @Autowired // We are using this because the logic to check for valid user access to manage students for bulk upload already exists in this file
    private FileUploadSecurityValidator fileUploadSecurityValidator;

    @Override
    public StudentUpsert upsertStudent(Student student, StudentSecurity studentSecurity, String stateCode) {
        StudentUpsert studentUpsert = new StudentUpsert();
        Student existingStudent = studentService.findByStudentIdAndStateAbbreviation(student.getEntityId(),
                student.getStateAbbreviation());
        if (existingStudent != null) {
            student.setId(existingStudent.getId());
        }
        // If student hasn't changed, don't save the object, just log the skip.
        if (existingStudent != null
                && Objects.deepEquals(student.toStringArray(), existingStudent.toStringArray())) {
            LOGGER.debug("Upsert SKIPPING validation/save of duplicate student " + student.getEntityId());
        } else {
            String studentErrors = validateStudent(student, studentSecurity, stateCode);
            if (!StringUtils.isEmpty(studentErrors)) {
                throw new IllegalArgumentException(studentErrors);
            }
            studentRepository.saveDomainObject(student);
        }
        studentUpsert.setInsert(existingStudent == null);
        studentUpsert.setLocation(student.getUrl());
        studentUpsert.setStudent(student);
        return studentUpsert;
    }

    @Override
    public boolean deleteStudent(String ssid, StudentSecurity studentSecurity, String stateCode) {
        Student student = studentService.findByStudentIdAndStateAbbreviation(ssid, stateCode);
        if (student == null)
            return false;
        String studentErrors = validateStudent(student, studentSecurity, stateCode);
        if (!StringUtils.isEmpty(studentErrors)) {
            throw new IllegalArgumentException(studentErrors);
        }
        testStatusService.deleteTestStatus(ssid, stateCode);
        studentRepository.deleteDomainObject(student.getId(), FormatType.STUDENT);
        return true;
    }

    private String validateStudent(Student student, StudentSecurity studentSecurity, String stateCode) {
        if (student.getStateAbbreviation().compareTo(stateCode) != 0)
            throw new UnauthorizedUserException("Attempted to modify student in state: "
                    + student.getStateAbbreviation() + " endpoint received " + stateCode);
        String error = "";
        List<FieldError> errors = fileUploadSecurityValidator.validateStudent(student, studentSecurity);
        if (errors.size() != 0) {
            for (FieldError fieldError : errors) {
                error += fieldError.getDefaultMessage() + "\n";
            }
        }
        return error;
    }
}