001 /*
002 * $Revision: 14954 $
003 * $Date: 2009-03-18 16:38:08 +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.ifs2;
025
026 import org.apache.commons.vfs.FileObject;
027 import org.mycore.common.MCRConfiguration;
028 import org.mycore.common.MCRException;
029
030 /**
031 * Stores XML metadata documents (or optionally any other BLOB data) in a
032 * persistent filesystem structure
033 *
034 * For each object type, a store must be defined as follows:
035 *
036 * MCR.IFS2.Store.DocPortal_document.Class=org.mycore.datamodel.ifs2.MCRMetadataStore
037 * MCR.IFS2.Store.DocPortal_document.BaseDir=/foo/bar
038 * MCR.IFS2.Store.DocPortal_document.SlotLayout=4-2-2 MCR.IFS2.
039 * MCR.IFS2.Store.DocPortal_document.ForceXML=true (which is default)
040 *
041 * @author Frank Lützenkirchen
042 */
043 public class MCRMetadataStore extends MCRStore {
044
045 /**
046 * If true (which is default), store will enforce it gets
047 * XML to store, otherwise any binary content can be stored here.
048 *
049 * Override with MCR.IFS2.Store.<ObjectType>.ForceXML=true|false
050 */
051 protected boolean forceXML = true;
052
053 /**
054 * Returns the store for the given metadata document type
055 *
056 * @param type
057 * the type of metadata to store
058 * @return the store for this metadata type
059 */
060 public static MCRMetadataStore getStore(String type) {
061 return (MCRMetadataStore) (MCRStore.getStore(type));
062 }
063
064 /**
065 * Initializes a new metadata store instance.
066 *
067 * @param type
068 * the document type that is stored in this store
069 */
070 protected void init(String type) {
071 super.init(type);
072 this.prefix = type + "_";
073 this.suffix = ".xml";
074 this.forceXML = MCRConfiguration.instance().getBoolean("MCR.IFS2.Store." + type + ".ForceXML", true);
075 }
076
077 protected boolean shouldForceXML() {
078 return forceXML;
079 }
080
081 /**
082 * Stores a newly created document, using the next free ID.
083 *
084 * @param xml
085 * the XML document to be stored
086 * @return the stored metadata object
087 */
088 public MCRStoredMetadata create(MCRContent xml) throws Exception {
089 int id = getNextFreeID();
090 return create(xml, id);
091 }
092
093 /**
094 * Stores a newly created document under the given ID.
095 *
096 * @param xml
097 * the XML document to be stored
098 * @param id
099 * the ID under which the document should be stored
100 * @return the stored metadata object
101 */
102 public MCRStoredMetadata create(MCRContent xml, int id) throws Exception {
103 FileObject fo = getSlot(id);
104 if (fo.exists()) {
105 String msg = "Metadata object with ID " + id + " already exists in store";
106 throw new MCRException(msg);
107 }
108 fo.createFile();
109 MCRStoredMetadata meta = buildMetadataObject(fo, id);
110 meta.create(xml);
111 return meta;
112 }
113
114 /**
115 * Returns the metadata stored under the given ID, or null
116 *
117 * @param id
118 * the ID of the XML document
119 * @return the metadata stored under that ID, or null when there is no such
120 * metadata object
121 */
122 public MCRStoredMetadata retrieve(int id) throws Exception {
123 FileObject fo = getSlot(id);
124 if (!fo.exists())
125 return null;
126 else
127 return buildMetadataObject(fo, id);
128 }
129
130 /**
131 * Builds a new stored metadata object in this store
132 *
133 * @param fo
134 * the FileObject that stores the data
135 * @param id
136 * the ID of the metadata object
137 */
138 protected MCRStoredMetadata buildMetadataObject(FileObject fo, int id) {
139 return new MCRStoredMetadata(this, fo, id);
140 }
141 }