001    /*
002     * $Revision: 15009 $ 
003     * $Date: 2009-03-25 11:09:54 +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 org.apache.commons.vfs.FileObject;
027    import org.apache.commons.vfs.VFS;
028    import org.apache.log4j.Logger;
029    import org.jdom.Document;
030    import org.jdom.Element;
031    
032    /**
033     * Represents a set of files and directories belonging together, that are stored
034     * in a persistent MCRFileStore. A FileCollection has a unique ID within the
035     * store, it is the root folder of all files and directories in the collection.
036     * 
037     * @author Frank Lützenkirchen
038     */
039    public class MCRFileCollection extends MCRDirectory {
040    
041        /**
042         * The logger
043         */
044        private final static Logger LOGGER = Logger.getLogger(MCRFileCollection.class);
045    
046        /**
047         * The store this file collection is stored in.
048         */
049        private MCRStore store;
050    
051        /**
052         * The ID of this file collection
053         */
054        private int id;
055    
056        /**
057         * Creates a new file collection in the given store, or retrieves an
058         * existing one.
059         * 
060         * @see MCRFileStore
061         * 
062         * @param store
063         *            the store this file collection is stored in
064         * @param id
065         *            the ID of this file collection
066         */
067        protected MCRFileCollection(MCRStore store, int id) throws Exception {
068            super(null, store.getSlot(id), new Element("collection"));
069            this.store = store;
070            this.id = id;
071            if (fo.exists())
072                readAdditionalData();
073            else {
074                fo.createFolder();
075                new Document(data);
076                saveAdditionalData();
077            }
078        }
079    
080        private final static String dataFile = "mcrdata.xml";
081    
082        private void readAdditionalData() throws Exception {
083            FileObject src = VFS.getManager().resolveFile(fo, dataFile);
084            if (!src.exists()) {
085                LOGGER.warn("Metadata file is missing, repairing metadata...");
086                this.data = new Element("collection");
087                new Document(data);
088                repairMetadata();
089            }
090            data = MCRContent.readFrom(src).asXML().getRootElement();
091        }
092    
093        protected void saveAdditionalData() throws Exception {
094            FileObject target = VFS.getManager().resolveFile(fo, dataFile);
095            MCRContent.readFrom(data.getDocument()).sendTo(target);
096        }
097    
098        /**
099         * Throws a exception, because a file collection's name is always the empty
100         * string and therefore can not be renamed.
101         */
102        public void renameTo(String name) {
103            throw new UnsupportedOperationException("File collections can not be renamed");
104        }
105    
106        /**
107         * Returns the store this file collection is stored in.
108         * 
109         * @return the store this file collection is stored in.
110         */
111        public MCRStore getStore() {
112            return store;
113        }
114    
115        /**
116         * Returns the ID of this file collection
117         * 
118         * @return the ID of this file collection
119         */
120        public int getID() {
121            return id;
122        }
123    
124        /**
125         * Returns this object, because the FileCollection instance is the root of
126         * all files and directories contained in the collection.
127         * 
128         * @return this
129         */
130        public MCRFileCollection getRoot() {
131            return this;
132        }
133    
134        public int getNumChildren() throws Exception {
135            return super.getNumChildren() - 1;
136        }
137    
138        public MCRNode getChild(String name) throws Exception {
139            if (dataFile.equals(name))
140                return null;
141            else
142                return super.getChild(name);
143        }
144    
145        public String getName() {
146            return "";
147        }
148    
149        /**
150         * Repairs additional metadata stored for all files and directories in this
151         * collection
152         */
153        public void repairMetadata() throws Exception {
154            super.repairMetadata();
155            data.setName("collection");
156            data.removeAttribute("name");
157            saveAdditionalData();
158        }
159    
160        /**
161         * Returns additional metadata stored for all files and directories in this
162         * collection
163         */
164        Document getMetadata() throws Exception {
165            return data.getDocument();
166        }
167    }