001    /*
002     * $Revision: 15002 $ 
003     * $Date: 2009-03-25 09:36:28 +0100 (Wed, 25 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.ifs2;
025    
026    import java.util.List;
027    
028    import org.apache.commons.vfs.FileObject;
029    import org.apache.commons.vfs.FileType;
030    import org.jdom.Element;
031    
032    /**
033     * Represents a directory stored in a file collection, which may contain other
034     * files and directories.
035     * 
036     * @author Frank Lützenkirchen
037     */
038    public class MCRDirectory extends MCRStoredNode {
039    
040        /**
041         * Create MCRDirectory representing an existing, already stored directory.
042         * 
043         * @param parent
044         *            the parent directory of this directory
045         * @param fo
046         *            the local directory in the store storing this directory
047         */
048        protected MCRDirectory(MCRDirectory parent, FileObject fo, Element data) throws Exception {
049            super(parent, fo, data);
050        }
051    
052        /**
053         * Create a new MCRDirectory that does not exist yet
054         * 
055         * @param parent
056         *            the parent directory of this directory
057         * @param name
058         *            the name of the new subdirectory to create
059         */
060        protected MCRDirectory(MCRDirectory parent, String name) throws Exception {
061            super(parent, name, "dir");
062            fo.createFolder();
063            getRoot().saveAdditionalData();
064        }
065    
066        /**
067         * Creates a new subdirectory within this directory
068         * 
069         * @param name
070         *            the name of the new directory
071         */
072        public MCRDirectory createDir(String name) throws Exception {
073            return new MCRDirectory(this, name);
074        }
075    
076        /**
077         * Creates a new file within this directory
078         * 
079         * @param name
080         *            the name of the new file
081         */
082        public MCRFile createFile(String name) throws Exception {
083            return new MCRFile(this, name);
084        }
085    
086        private Element getChildData(String name) {
087            for (Element child : (List<Element>) (data.getChildren()))
088                if (name.equals(child.getAttributeValue("name")))
089                    return child;
090    
091            Element childData = new Element("node");
092            childData.setAttribute("name", name);
093            data.addContent(childData);
094            return childData;
095        }
096    
097        /**
098         * Returns the MCRFile or MCRDirectory that is represented by the given
099         * FileObject, which is a direct child of the directory FileObject this
100         * MCRDirectory is stored in.
101         * 
102         * @return an MCRFile or MCRDirectory child
103         */
104        protected MCRStoredNode buildChildNode(FileObject fo) throws Exception {
105            if (fo == null)
106                return null;
107    
108            Element childData = getChildData(fo.getName().getBaseName());
109            if (fo.getType().equals(FileType.FILE))
110                return new MCRFile(this, fo, childData);
111            else
112                return new MCRDirectory(this, fo, childData);
113        }
114    
115        /**
116         * Repairs additional metadata of this directory and all its children
117         */
118        void repairMetadata() throws Exception {
119            data.setName("dir");
120            data.setAttribute("name", getName());
121    
122            for (Element childEntry : (List<Element>) (data.getChildren()))
123                childEntry.setName("node");
124    
125            for (MCRNode child : getChildren())
126                ((MCRStoredNode) child).repairMetadata();
127    
128            data.removeChildren("node");
129        }
130    }