org.opentestsystem.delivery.testadmin.domain.schedule.Schedule.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.delivery.testadmin.domain.schedule.Schedule.java

Source

/*******************************************************************************
 * 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.domain.schedule;

import java.util.List;
import java.util.TreeSet;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotBlank;
import org.joda.time.DateTime;
import org.opentestsystem.delivery.testadmin.domain.Affinity;
import org.opentestsystem.delivery.testadmin.domain.TestAdminBase;
import org.opentestsystem.delivery.testadmin.domain.constraints.ValidScheduleDate;
import org.opentestsystem.delivery.testreg.domain.FieldLabel;
import org.opentestsystem.delivery.testreg.domain.constraints.Alphanumeric;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.util.CollectionUtils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Predicate;
import com.google.common.collect.Sets;

/**
 * The Schedule class contains metadata about the timespan of the schedule, the institution it's for any schedule-wide
 * priority rules and a list of the ScheduledDays for the schedule. All of the information about what is scheduled to
 * which time slot is contained inside the ScheduledDay.
 */
@Document
@JsonIgnoreProperties(ignoreUnknown = true)
@ValidScheduleDate
public class Schedule implements TestAdminBase, Cloneable {

    private static final long serialVersionUID = 1228073155461859826L;

    private static final String GET_RESOURCE_NAME = "/schedule/";

    @Id
    private String id;

    @NotBlank(message = "{schedule.name.blank}")
    @Alphanumeric(spacesAllowed = true, message = "{schedule.name.alphanumeric}")
    private String scheduleName;

    @NotNull(message = "{schedule.startdate.blank}")
    private DateTime startDate;

    @NotNull(message = "{schedule.enddate.blank}")
    private DateTime endDate;

    @NotBlank(message = "{schedule.stateAbbreviation.blank}")
    private String stateAbbreviation;

    @NotBlank(message = "{schedule.institutionidentifier.blank}")
    @Alphanumeric(message = "{schedule.institutionidentifier.alphanumeric}")
    @Size(max = 40, message = "{schedule.institutionidentifier.size.max}")
    @FieldLabel("InstitutionIdentifier")
    private String institutionIdentifier;

    private String institutionId; // this is the mongo id of the institution entity

    private String tenantId; //this is required to find out right assessments for scheduling

    private List<ScheduledDay> scheduledDays;

    @Valid
    private List<Affinity> affinities;

    // do not persist this
    /**
     * ScheduledTimeSlots are ordered by start time (including date) in order that we schedule earliest slots first. If
     * there are multiple time slots (from more than one facility) that have the same start time, then we just order by
     * facility id.
     **/
    @Transient
    private TreeSet<ScheduledTimeSlot> orderedTimeSlots;

    /**
     * Contains data related to the creation of the schedule
     */
    private ScheduleCreationInfo creationInfo;

    // default to true
    private boolean doNotScheduleWeekends = true;

    public String getId() {
        return this.id;
    }

    public void setId(final String id) {
        this.id = id;
    }

    public String getScheduleName() {
        return this.scheduleName;
    }

    public void setScheduleName(final String scheduleName) {
        this.scheduleName = scheduleName;
    }

    public DateTime getStartDate() {
        return this.startDate;
    }

    public void setStartDate(final DateTime startDate) {
        this.startDate = startDate;
    }

    public DateTime getEndDate() {
        return this.endDate;
    }

    public void setEndDate(final DateTime endDate) {
        this.endDate = endDate;
    }

    public List<ScheduledDay> getScheduledDays() {
        return this.scheduledDays;
    }

    public void setScheduledDays(final List<ScheduledDay> scheduledDays) {
        this.scheduledDays = scheduledDays;
    }

    public String getInstitutionIdentifier() {
        return this.institutionIdentifier;
    }

    public void setInstitutionIdentifier(final String institutionIdentifier) {
        this.institutionIdentifier = institutionIdentifier;
    }

    public String getInstitutionId() {
        return this.institutionId;
    }

    public void setInstitutionId(final String institutionId) {
        this.institutionId = institutionId;
    }

    public String getStateAbbreviation() {
        return this.stateAbbreviation;
    }

    public void setStateAbbreviation(final String stateAbbreviation) {
        this.stateAbbreviation = stateAbbreviation;
    }

    public String getTenantId() {
        return this.tenantId;
    }

    public void setTenantId(final String tenantId) {
        this.tenantId = tenantId;
    }

    @JsonProperty
    public String getUrl() {
        return GET_RESOURCE_NAME + this.id;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {

        final Schedule cloned = new Schedule();

        cloned.setEndDate(this.endDate);
        cloned.setId(this.id);
        cloned.setStateAbbreviation(this.stateAbbreviation);
        cloned.setInstitutionIdentifier(this.institutionIdentifier);
        cloned.setInstitutionId(this.institutionId);
        cloned.setScheduledDays(this.scheduledDays);
        cloned.setScheduleName(this.scheduleName);
        cloned.setAffinities(this.affinities);
        cloned.setStartDate(this.startDate);
        cloned.setDoNotScheduleWeekends(this.doNotScheduleWeekends);
        return cloned;
    }

    /**
     * Grabs all of the time slots for all the facilities in each of the scheduled days and puts then into an ordered
     * TreeSet
     */
    public void generateOrderedTimeSlots() {

        this.orderedTimeSlots = new TreeSet<ScheduledTimeSlot>();

        for (final ScheduledDay sday : this.scheduledDays) {
            for (final ScheduledFacility sfacil : sday.getFacilities()) {
                this.orderedTimeSlots.addAll(sfacil.getTimeSlots());
            }
        }

    }

    /**
     * If there are affinities (schedule-wide priority rules) set, returns true
     * 
     * @return
     */
    public boolean hasPriorityAllocationRules() {

        return !CollectionUtils.isEmpty(this.affinities);
    }

    /**
     * Returns the ordered timeslots or a filtered view if the argument is true. Filtered view returns all timeslots
     * equal to or after today.
     * 
     * @param rescheduleView
     * @return
     */
    public TreeSet<ScheduledTimeSlot> getOrderedTimeSlots(final boolean rescheduleView) {

        if (rescheduleView) {
            return Sets.newTreeSet(Sets.filter(this.orderedTimeSlots, new Predicate<ScheduledTimeSlot>() {

                @Override
                public boolean apply(final ScheduledTimeSlot timeSlot) {
                    return timeSlot.getStartTime().isEqualNow() || timeSlot.getStartTime().isAfterNow();
                }
            }));
        } else {
            return this.orderedTimeSlots;
        }
    }

    public List<Affinity> getAffinities() {
        return this.affinities;
    }

    public void setAffinities(final List<Affinity> affinities) {
        this.affinities = affinities;
    }

    public ScheduleCreationInfo getCreationInfo() {
        return this.creationInfo;
    }

    public void setCreationInfo(final ScheduleCreationInfo creationInfo) {
        this.creationInfo = creationInfo;
    }

    public boolean isDoNotScheduleWeekends() {
        return this.doNotScheduleWeekends;
    }

    public void setDoNotScheduleWeekends(final boolean doNotScheduleWeekends) {
        this.doNotScheduleWeekends = doNotScheduleWeekends;
    }

    @JsonProperty
    public void setDoNotScheduleWeekends(final String scheduleWeekends) {
        if (scheduleWeekends.equalsIgnoreCase("YES")) {
            this.doNotScheduleWeekends = false;
        }
    }
}