1 /*
2 * This file is part of *** M y C o R e ***
3 * See http://www.mycore.de/ for details.
4 *
5 * MyCoRe is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * MyCoRe is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with MyCoRe. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package org.mycore.datamodel.ifs2;
20
21 import java.io.IOException;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.nio.file.StandardCopyOption;
25 import java.nio.file.attribute.FileTime;
26 import java.util.Date;
27
28 import org.jdom2.JDOMException;
29 import org.mycore.common.MCRUsageException;
30 import org.mycore.common.content.MCRContent;
31 import org.mycore.common.content.MCRPathContent;
32 import org.xml.sax.SAXException;
33
34 /**
35 * Represents an XML metadata document that is stored in MCRMetadataStore.
36 *
37 * @author Frank Lützenkirchen
38 */
39 public class MCRStoredMetadata {
40
41 /** The ID of the metadata document */
42 protected int id;
43
44 /** The file object in local filesystem storing the data */
45 protected Path path;
46
47 /** The store this document is stored in */
48 protected MCRMetadataStore store;
49
50 private String docType;
51
52 protected boolean deleted;
53
54 /**
55 * Creates a new stored metadata object
56 *
57 * @param store
58 * the store this document is stored in
59 * @param path
60 * the file object storing the data
61 * @param id
62 * the ID of the metadata document
63 * @param docType
64 * if not null overwrites any detected doctype
65 */
66 MCRStoredMetadata(MCRMetadataStore store, Path path, int id, String docType) {
67 this.store = store;
68 this.id = id;
69 this.path = path;
70 this.docType = docType;
71 this.deleted = false;
72 }
73
74 /**
75 * Creates a new local file to save XML to
76 *
77 * @param xml
78 * the XML to save to a new file
79 * @throws JDOMException if content is not XML and corresponding
80 * {@link MCRMetadataStore} forces MCRContent to be XML
81 */
82 void create(MCRContent xml) throws IOException, JDOMException {
83 if (store.shouldForceXML()) {
84 try {
85 xml = xml.ensureXML();
86 } catch (SAXException e) {
87 throw new IOException(e);
88 }
89 }
90 if (!Files.exists(path.getParent())) {
91 Files.createDirectories(path.getParent());
92 }
93 xml.sendTo(path);
94 }
95
96 /**
97 * Updates the stored XML document
98 *
99 * @param xml
100 * the XML document to be stored
101 * @throws JDOMException if content is not XML and corresponding
102 * {@link MCRMetadataStore} forces MCRContent to be XML
103 */
104 public void update(MCRContent xml) throws IOException, JDOMException {
105 if (isDeleted()) {
106 String msg = "You can not update a deleted data object";
107 throw new MCRUsageException(msg);
108 }
109 if (store.shouldForceXML()) {
110 try {
111 xml = xml.ensureXML();
112 } catch (SAXException e) {
113 throw new IOException(e);
114 }
115 }
116 xml.sendTo(path, StandardCopyOption.REPLACE_EXISTING);
117 }
118
119 /**
120 * Returns the stored XML document
121 *
122 * @return the stored XML document
123 */
124 public MCRContent getMetadata() throws IOException {
125 MCRPathContent pathContent = new MCRPathContent(path);
126 pathContent.setDocType(docType);
127 return pathContent;
128 }
129
130 /**
131 * Returns the ID of this metadata document
132 *
133 * @return the ID of this metadata document
134 */
135 public int getID() {
136 return id;
137 }
138
139 /**
140 * Returns the store this metadata document is stored in
141 *
142 * @return the store this metadata document is stored in
143 */
144 public MCRMetadataStore getStore() {
145 return store;
146 }
147
148 /**
149 * Returns the date this metadata document was last modified
150 *
151 * @return the date this metadata document was last modified
152 */
153 public Date getLastModified() throws IOException {
154 return Date.from(Files.getLastModifiedTime(path).toInstant());
155 }
156
157 /**
158 * Sets the date this metadata document was last modified
159 *
160 * @param date
161 * the date this metadata document was last modified
162 */
163 public void setLastModified(Date date) throws IOException {
164 if (!isDeleted()) {
165 Files.setLastModifiedTime(path, FileTime.from(date.toInstant()));
166 }
167 }
168
169 /**
170 * Deletes the metadata document. This object is invalid afterwards, do not
171 * use it any more.
172 *
173 */
174 public void delete() throws IOException {
175 if (!deleted) {
176 store.delete(path);
177 deleted = true;
178 }
179 }
180
181 /**
182 * Returns true if this object is deleted
183 */
184 public boolean isDeleted() throws IOException {
185 return deleted;
186 }
187 }