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