001package net.sf.cpsolver.studentsct;
002
003import org.apache.log4j.Logger;
004
005import net.sf.cpsolver.ifs.solution.Solution;
006import net.sf.cpsolver.ifs.solver.Solver;
007import net.sf.cpsolver.ifs.util.Callback;
008import net.sf.cpsolver.studentsct.model.Enrollment;
009import net.sf.cpsolver.studentsct.model.Request;
010
011/**
012 * Abstract student sectioning saver class.
013 * 
014 * @version StudentSct 1.2 (Student Sectioning)<br>
015 *          Copyright (C) 2007 - 2010 Tomáš Müller<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 StudentSectioningSaver implements Runnable {
035    private Solver<Request, Enrollment> iSolver = null;
036    private Callback iCallback = null;
037
038    /**
039     * Constructor
040     */
041    public StudentSectioningSaver(Solver<Request, Enrollment> solver) {
042        iSolver = solver;
043    }
044
045    /** Solver */
046    public Solver<Request, Enrollment> getSolver() {
047        return iSolver;
048    }
049
050    /** Solution to be saved */
051    protected Solution<Request, Enrollment> getSolution() {
052        return iSolver.currentSolution();
053    }
054
055    /** Model of the solution */
056    protected StudentSectioningModel getModel() {
057        return (StudentSectioningModel) iSolver.currentSolution().getModel();
058    }
059
060    /** Save the solution */
061    public abstract void save() throws Exception;
062
063    /**
064     * Sets callback class
065     * 
066     * @param callback
067     *            method {@link Callback#execute()} is executed when save is
068     *            done
069     */
070    public void setCallback(Callback callback) {
071        iCallback = callback;
072    }
073
074    @Override
075    public void run() {
076        try {
077            save();
078        } catch (Exception e) {
079            Logger.getLogger(this.getClass()).error(e.getMessage(), e);
080        } finally {
081            if (iCallback != null)
082                iCallback.execute();
083        }
084    }
085}