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 java.util.Iterator;
027    
028    import org.apache.commons.vfs.FileObject;
029    import org.mycore.common.MCRException;
030    
031    /**
032     * Stores file collections containing files and directories.
033     * 
034     * For each store, properties must be defined, for example
035     * 
036     * MCR.IFS2.Store.ID.Class=org.mycore.datamodel.ifs2.MCRFileStore
037     * MCR.IFS2.Store.ID.BaseDir=/foo/bar MCR.IFS2.Store.ID.SlotLayout=4-2-2
038     * 
039     * @author Frank Lützenkirchen
040     */
041    public class MCRFileStore extends MCRStore {
042    
043        /**
044         * Returns the store with the given ID
045         * 
046         * @param ID
047         *            the ID of the store
048         * @return the store with that ID
049         */
050        public static MCRFileStore getStore(String type) {
051            return (MCRFileStore) (MCRStore.getStore(type));
052        }
053    
054        /**
055         * Creates and stores a new, empty file collection using the next free ID in
056         * the store.
057         * 
058         * @return a newly created file collection
059         */
060        public MCRFileCollection create() throws Exception {
061            int id = getNextFreeID();
062            return create(id);
063        }
064    
065        /**
066         * Creates and stores a new, empty file collection with the given ID
067         * 
068         * @param id
069         *            the ID of the file collection
070         * @return a newly created file collection
071         * @throws Exception
072         *             when a file collection with the given ID already exists
073         */
074        public MCRFileCollection create(int id) throws Exception {
075            FileObject fo = getSlot(id);
076            if (fo.exists()) {
077                String msg = "FileCollection with ID " + id + " already exists";
078                throw new MCRException(msg);
079            }
080            return new MCRFileCollection(this, id);
081        }
082    
083        /**
084         * Returns the file collection stored under the given ID, or null when no
085         * collection is stored for the given ID.
086         * 
087         * @param id
088         *            the file collection's ID
089         * @return the file collection with the given ID, or null
090         */
091        public MCRFileCollection retrieve(int id) throws Exception {
092            FileObject fo = getSlot(id);
093            if (!fo.exists())
094                return null;
095            else
096                return new MCRFileCollection(this, id);
097        }
098    
099        /**
100         * Repairs metadata of all file collections stored here
101         * 
102         * @throws Exception
103         */
104        public void repairAllMetadata() throws Exception {
105            for (Iterator<Integer> e = listIDs(MCRStore.ASCENDING); e.hasNext();)
106                retrieve(e.next()).repairMetadata();
107        }
108    }