001    package net.sf.cpsolver.studentsct;
002    
003    import org.apache.log4j.Logger;
004    
005    
006    import net.sf.cpsolver.ifs.solution.Solution;
007    import net.sf.cpsolver.ifs.solver.Solver;
008    import net.sf.cpsolver.ifs.util.Callback;
009    
010    /**
011     * Abstract student sectioning saver class.
012     * 
013     * @version
014     * StudentSct 1.1 (Student Sectioning)<br>
015     * Copyright (C) 2007 Tomáš Müller<br>
016     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
017     * Lazenska 391, 76314 Zlin, Czech Republic<br>
018     * <br>
019     * This library is free software; you can redistribute it and/or
020     * modify it under the terms of the GNU Lesser General Public
021     * License as published by the Free Software Foundation; either
022     * version 2.1 of the License, or (at your option) any later version.
023     * <br><br>
024     * This library is distributed in the hope that it will be useful,
025     * but 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.
028     * <br><br>
029     * You should have received a copy of the GNU Lesser General Public
030     * License along with this library; if not, write to the Free Software
031     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
032     */
033    
034    public abstract class StudentSectioningSaver implements Runnable {
035        private Solver iSolver = null; 
036        private Callback iCallback = null;
037        /** Constructor
038         */
039        public StudentSectioningSaver(Solver solver) {
040            iSolver = solver;
041        }
042        /** Solver */
043        public Solver getSolver() { return iSolver; }
044        /** Solution to be saved */
045        protected Solution getSolution() { return iSolver.currentSolution(); }
046        /** Model of the solution */
047        protected StudentSectioningModel getModel() { return (StudentSectioningModel)iSolver.currentSolution().getModel(); }
048        /** Save the solution*/
049        public abstract void save() throws Exception;
050        /** Sets callback class
051         * @param callback method {@link Callback#execute()} is executed when save is done
052         */
053        public void setCallback(Callback callback) { iCallback = callback; }
054    
055        public void run() { 
056            try {
057                save(); 
058            } catch (Exception e) {
059                Logger.getLogger(this.getClass()).error(e.getMessage(),e);
060            } finally {
061                if (iCallback!=null)
062                    iCallback.execute();
063            }
064        }
065    }