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