001 /*
002 * $Revision: 15011 $
003 * $Date: 2009-03-25 11:30:44 +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.Date;
027
028 import org.apache.commons.vfs.FileObject;
029 import org.mycore.common.MCRUsageException;
030
031 /**
032 * Represents an XML metadata document that is stored in MCRMetadataStore.
033 *
034 * @author Frank Lützenkirchen
035 */
036 public class MCRStoredMetadata {
037
038 /** The ID of the metadata document */
039 protected int id;
040
041 /** The file object in local filesystem storing the data */
042 protected FileObject fo;
043
044 /** The store this document is stored in */
045 protected MCRMetadataStore store;
046
047 /**
048 * Creates a new stored metadata object
049 *
050 * @param store
051 * the store this document is stored in
052 * @param fo
053 * the file object storing the data
054 * @param id
055 * the ID of the metadata document
056 */
057 MCRStoredMetadata(MCRMetadataStore store, FileObject fo, int id) {
058 this.store = store;
059 this.id = id;
060 this.fo = fo;
061 }
062
063 /**
064 * Creates a new local file to save XML to
065 *
066 * @param xml
067 * the XML to save to a new file
068 */
069 void create(MCRContent xml) throws Exception {
070 if (store.shouldForceXML())
071 xml = xml.ensureXML();
072 fo.createFile();
073 xml.sendTo(fo);
074 }
075
076 /**
077 * Updates the stored XML document
078 *
079 * @param xml
080 * the XML document to be stored
081 */
082 public void update(MCRContent xml) throws Exception {
083 if (isDeleted()) {
084 String msg = "You can not update a deleted data object";
085 throw new MCRUsageException(msg);
086 }
087 if (store.shouldForceXML())
088 xml = xml.ensureXML();
089 xml.sendTo(fo);
090 }
091
092 /**
093 * Returns the stored XML document
094 *
095 * @return the stored XML document
096 */
097 public MCRContent getMetadata() throws Exception {
098 return MCRContent.readFrom(fo);
099 }
100
101 /**
102 * Returns the ID of this metadata document
103 *
104 * @return the ID of this metadata document
105 */
106 public int getID() {
107 return id;
108 }
109
110 /**
111 * Returns the store this metadata document is stored in
112 *
113 * @return the store this metadata document is stored in
114 */
115 public MCRMetadataStore getStore() {
116 return store;
117 }
118
119 /**
120 * Returns the date this metadata document was last modified
121 *
122 * @return the date this metadata document was last modified
123 */
124 public Date getLastModified() throws Exception {
125 long time = fo.getContent().getLastModifiedTime();
126 return new Date(time);
127 }
128
129 /**
130 * Sets the date this metadata document was last modified
131 *
132 * @param date
133 * the date this metadata document was last modified
134 */
135 public void setLastModified(Date date) throws Exception {
136 if (!isDeleted())
137 fo.getContent().setLastModifiedTime(date.getTime());
138 }
139
140 /**
141 * Deletes the metadata document. This object is invalid afterwards, do not
142 * use it any more.
143 *
144 * @throws Exception
145 */
146 public void delete() throws Exception {
147 if (!isDeleted())
148 store.delete(fo);
149 }
150
151 /**
152 * Returns true if this object is deleted
153 */
154 public boolean isDeleted() throws Exception {
155 return (!fo.exists());
156 }
157 }