001    /*
002     * 
003     * $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
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 java.util.Hashtable;
027    import java.util.List;
028    
029    import org.jdom.Element;
030    import org.mycore.common.MCRConfiguration;
031    import org.mycore.common.MCRException;
032    import org.mycore.common.xml.MCRURIResolver;
033    
034    /**
035     * Decides which MCRContentStore implementation should be used to store the
036     * content of a given file, based on the content type of the file.
037     * 
038     * @author Frank Lützenkirchen
039     * @author Thomas Scheffler (yagee)
040     * @version $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
041     */
042    public class MCRSimpleContentStoreSelector implements MCRContentStoreSelector {
043        /** the default content store to use if no other rule matches */
044        protected String defaultID;
045    
046        /**
047         * store lookup table where keys are file content type IDs, values are
048         * content store IDs
049         */
050        protected Hashtable table;
051    
052        /** list of all storeIDs * */
053        protected String[] storeIDs;
054    
055        public MCRSimpleContentStoreSelector() {
056            MCRConfiguration config = MCRConfiguration.instance();
057            String file = config.getString("MCR.IFS.ContentStoreSelector.ConfigFile");
058            Element xml = MCRURIResolver.instance().resolve("resource:" + file);
059    
060            table = new Hashtable();
061    
062            List stores = xml.getChildren("store");
063            storeIDs = new String[stores.size() + 1];
064    
065            for (int i = 0; i < stores.size(); i++) {
066                Element store = (Element) (stores.get(i));
067                String storeID = store.getAttributeValue("ID");
068                storeIDs[i] = storeID;
069    
070                List types = store.getChildren();
071    
072                for (int j = 0; j < types.size(); j++) {
073                    Element type = (Element) (types.get(j));
074                    String typeID = type.getTextTrim();
075    
076                    table.put(typeID, storeID);
077                }
078            }
079    
080            defaultID = xml.getAttributeValue("default");
081    
082            // NOTE: if defaultID is listed as a <store> it's inserted twice here
083            storeIDs[storeIDs.length - 1] = defaultID;
084        }
085    
086        public String selectStore(MCRFile file) throws MCRException {
087            return getStore(file.getContentTypeID());
088        }
089    
090        public String selectStore(MCRFileContentType type) {
091            return getStore(type.getID());
092        }
093    
094        private String getStore(String typeID) {
095            if (table.containsKey(typeID)) {
096                return (String) (table.get(typeID));
097            }
098            return defaultID;
099        }
100    
101        public String[] getAvailableStoreIDs() {
102            return storeIDs;
103        }
104    }