001package org.cpsolver.coursett;
002
003import org.apache.log4j.Logger;
004import org.cpsolver.coursett.model.Lecture;
005import org.cpsolver.coursett.model.Placement;
006import org.cpsolver.coursett.model.TimetableModel;
007import org.cpsolver.ifs.assignment.Assignment;
008import org.cpsolver.ifs.util.Callback;
009
010
011/**
012 * Abstract timetable loader class.
013 * 
014 * @version CourseTT 1.3 (University Course Timetabling)<br>
015 *          Copyright (C) 2006 - 2014 Tomas Muller<br>
016 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
017 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
018 * <br>
019 *          This library is free software; you can redistribute it and/or modify
020 *          it under the terms of the GNU Lesser General Public License as
021 *          published by the Free Software Foundation; either version 3 of the
022 *          License, or (at your option) any later version. <br>
023 * <br>
024 *          This library is distributed in the hope that it will be useful, but
025 *          WITHOUT ANY WARRANTY; without even the implied warranty of
026 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
027 *          Lesser General Public License for more details. <br>
028 * <br>
029 *          You should have received a copy of the GNU Lesser General Public
030 *          License along with this library; if not see
031 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
032 */
033
034public abstract class TimetableLoader implements Runnable {
035    private TimetableModel iModel = null;
036    private Assignment<Lecture, Placement> iAssignment = null;
037    private Callback iCallback = null;
038
039    /**
040     * Constructor
041     * 
042     * @param model
043     *            an empty instance of timetable model
044     * @param assignment current assignment
045     */
046    public TimetableLoader(TimetableModel model, Assignment<Lecture, Placement> assignment) {
047        iModel = model;
048        iAssignment = assignment;
049    }
050
051    /**
052     * Returns provided model.
053     * 
054     * @return provided model
055     */
056    protected TimetableModel getModel() {
057        return iModel;
058    }
059    
060    /**
061     * Returns provided assignment
062     * @return provided assignment
063     */
064    protected Assignment<Lecture, Placement> getAssignment() {
065        return iAssignment;
066    }
067
068    /**
069     * Load the model.
070     * @throws Exception thrown when the load fails
071     */
072    public abstract void load() throws Exception;
073
074    /**
075     * Sets callback class
076     * 
077     * @param callback
078     *            method {@link Callback#execute()} is executed when load is
079     *            done
080     */
081    public void setCallback(Callback callback) {
082        iCallback = callback;
083    }
084
085    @Override
086    public void run() {
087        try {
088            load();
089        } catch (Exception e) {
090            Logger.getLogger(this.getClass()).error(e.getMessage(), e);
091        } finally {
092            if (iCallback != null)
093                iCallback.execute();
094        }
095    }
096
097}