001    /*
002     * 
003     * $Revision: 14946 $ $Date: 2009-03-18 14:48:40 +0100 (Wed, 18 Mar 2009) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.datamodel.ifs;
025    
026    import org.apache.log4j.Logger;
027    import org.mycore.common.events.MCREvent;
028    import org.mycore.common.events.MCREventHandlerBase;
029    import org.mycore.common.events.MCREventManager;
030    import org.mycore.datamodel.metadata.MCRDerivate;
031    
032    /**
033     * This class manages all operations of the repair of index of all content files
034     * of one derivate.
035     * 
036     * @author Jens Kupferschmidt
037     */
038    public class MCRContentIndexEventHandler extends MCREventHandlerBase {
039    
040        private static Logger logger = Logger.getLogger(MCRContentIndexEventHandler.class);
041    
042        /**
043         * Handles derivate repair events. The method lokk over all files and
044         * initialize a repair event for all files.
045         * 
046         * @param evt
047         *            the event that occured
048         * @param der
049         *            the MCRDerivate that caused the event
050         */
051        protected void handleDerivateRepaired(MCREvent evt, MCRDerivate der) {
052            MCRDirectory rootifs = MCRDirectory.getRootDirectory(der.getId().getId());
053            doForChildren(rootifs);
054        }
055    
056        /**
057         * This is a recursive method to start an event handler for each file.
058         * 
059         * @param thisnode
060         *            a IFS nod (file or directory)
061         */
062        private final void doForChildren(MCRFilesystemNode thisnode) {
063            if (thisnode instanceof MCRDirectory) {
064                MCRFilesystemNode[] childnodes = ((MCRDirectory) thisnode).getChildren();
065                for (int i = 0; i < childnodes.length; i++) {
066                    doForChildren(childnodes[i]);
067                }
068            } else {
069                // handle events
070                MCREvent evt = new MCREvent(MCREvent.FILE_TYPE, MCREvent.REPAIR_EVENT);
071                evt.put("file", (MCRFile)thisnode);
072                MCREventManager.instance().handleEvent(evt);
073                String fn = ((MCRFile) thisnode).getAbsolutePath();
074                logger.debug("repair file " + fn);
075    
076            }
077        }
078    
079    }