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