1 | package org.greenstone.gsdl3.gs3build.doctypes;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Iterator;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import java.sql.SQLException;
|
---|
10 | import java.sql.ResultSet;
|
---|
11 |
|
---|
12 | import java.net.URL;
|
---|
13 |
|
---|
14 | import org.greenstone.gsdl3.gs3build.metadata.NamespaceFactory;
|
---|
15 | import org.greenstone.gsdl3.gs3build.metadata.StructureIdentifierFactory;
|
---|
16 | import org.greenstone.gsdl3.gs3build.metadata.GSDL3Namespace;
|
---|
17 | import org.greenstone.gsdl3.gs3build.metadata.METSDescriptiveSet;
|
---|
18 | import org.greenstone.gsdl3.gs3build.metadata.METSFile;
|
---|
19 | import org.greenstone.gsdl3.gs3build.metadata.METSFileSet;
|
---|
20 | import org.greenstone.gsdl3.gs3build.metadata.METSHeader;
|
---|
21 | import org.greenstone.gsdl3.gs3build.metadata.METSStructure;
|
---|
22 | import org.greenstone.gsdl3.gs3build.metadata.METSStructureSet;
|
---|
23 | import org.greenstone.gsdl3.gs3build.metadata.METSDivision;
|
---|
24 | import org.greenstone.gsdl3.gs3build.metadata.METSNamespace;
|
---|
25 | import org.greenstone.gsdl3.gs3build.metadata.MetadataLabel;
|
---|
26 |
|
---|
27 | import org.greenstone.gsdl3.gs3build.util.MultiMap;
|
---|
28 | import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Provide a base-line functionality for the <code>DocumentInterface</code>
|
---|
32 | * class.
|
---|
33 | */
|
---|
34 |
|
---|
35 | public abstract class AbstractDocument implements DocumentInterface
|
---|
36 | {
|
---|
37 | METSFileSet fileSet;
|
---|
38 | METSDescriptiveSet metadata;
|
---|
39 | METSStructureSet structureSet;
|
---|
40 | METSHeader header;
|
---|
41 | DocumentID id;
|
---|
42 | boolean isModified;
|
---|
43 | StructureIdentifierFactory structureIdFactory;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * <p>Create a very vanilla document with a given document identifier.</p>
|
---|
47 | * <p>Most commonly used in dealing with loading files using DocumentFactory
|
---|
48 | * or similar.</p>
|
---|
49 | *
|
---|
50 | * @param <code>DocumentID</code> the document identifier
|
---|
51 | */
|
---|
52 | public AbstractDocument(DocumentID id)
|
---|
53 | { this.fileSet = new METSFileSet();
|
---|
54 | this.metadata = new METSDescriptiveSet();
|
---|
55 | this.header = new METSHeader();
|
---|
56 | this.structureSet = new METSStructureSet();
|
---|
57 | this.id = id;
|
---|
58 | this.structureIdFactory = new StructureIdentifierFactory();
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Create a basic document from a given <code>URL</code. This is usually the form
|
---|
63 | * called through the recognisers.
|
---|
64 | *
|
---|
65 | * @param <code>URL</code> the URL of the first file in the document package
|
---|
66 | */
|
---|
67 | public AbstractDocument(URL url)
|
---|
68 | { this.fileSet = new METSFileSet();
|
---|
69 | METSFile metsFile = this.fileSet.addFile(url);
|
---|
70 | this.metadata = new METSDescriptiveSet();
|
---|
71 | this.header = new METSHeader();
|
---|
72 | this.structureSet = new METSStructureSet();
|
---|
73 | this.id = null;
|
---|
74 |
|
---|
75 | METSStructure structure = new METSStructure("All", "All", "Whole Document");
|
---|
76 | METSDivision documentBody = new METSDivision("All", "All", "All", "Whole Document", "Document");
|
---|
77 | structure.addDivision(documentBody);
|
---|
78 | this.structureSet.addStructure(structure);
|
---|
79 | documentBody.addFileReference(metsFile.getID());
|
---|
80 | documentBody.addMetadataReference("DM1");
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Set the identified for the document. Every document should have
|
---|
85 | * a document number set on its accession, either through metadata
|
---|
86 | * placed upon it internally or externally, or by assignment through
|
---|
87 | * a <code>DocumentIDFactory</code>. Each identifier should be
|
---|
88 | * unique.
|
---|
89 | *
|
---|
90 | * @param <code>DocumentID</code> the document identifier - in XML
|
---|
91 | * terms, the gsdl3:id element.
|
---|
92 | */
|
---|
93 | public void setID(DocumentID id)
|
---|
94 | { this.id = id;
|
---|
95 | this.isModified = true;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Get the document identifier - this should be unique to the document,
|
---|
100 | * but care must be taken in the configuration of the collection to
|
---|
101 | * ensure that this is the case.
|
---|
102 | *
|
---|
103 | * @return <code>DocumentID</code> the identifer
|
---|
104 | */
|
---|
105 | public DocumentID getID()
|
---|
106 | { return this.id;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Indicate whether this document is indexed.
|
---|
111 | *
|
---|
112 | * @see: DocumentInterface.isIndexed
|
---|
113 | */
|
---|
114 | public boolean isIndexed()
|
---|
115 | {
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Obtain the METS header of this document
|
---|
121 | *
|
---|
122 | * @return <code>METSHeader</code> the header
|
---|
123 | */
|
---|
124 | public METSHeader getHeader()
|
---|
125 | { return this.header;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Set the METS header for this document.
|
---|
130 | *
|
---|
131 | * @param <code>METSHeader</code> the header
|
---|
132 | */
|
---|
133 | public void setHeader(METSHeader header)
|
---|
134 | { this.header = header;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * A simple implementation of the isDocumentType function that does <b>not</b> consider
|
---|
139 | * inheritance - it <code>must</code> be extended as required.
|
---|
140 | */
|
---|
141 | public boolean isDocumentType(String type)
|
---|
142 | { return type.equals(this.getDocumentType());
|
---|
143 | }
|
---|
144 |
|
---|
145 | public abstract String getDocumentType();
|
---|
146 |
|
---|
147 | public abstract String getDocumentText();
|
---|
148 |
|
---|
149 | public abstract String getSectionText(String sectionId);
|
---|
150 |
|
---|
151 | public String getMETSType()
|
---|
152 | { return "document";
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * @see DocumentInterface:addDocumentMetadata
|
---|
157 | */
|
---|
158 | public void addDocumentMetadata(MetadataLabel label, String value)
|
---|
159 | { // no need to set isModified, as the following call will do it anyway!
|
---|
160 | this.addDocumentMetadata(label.getNamespace(), label.getLabel(), value);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * @see DocumentInterface:addDocumentMetadata
|
---|
165 | */
|
---|
166 | public void addDocumentMetadata(String namespace, String label, String value)
|
---|
167 | { this.metadata.addMetadata("default", namespace, label, value);
|
---|
168 | this.isModified = true;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Post metadata to a file in this document - the appropriate changes
|
---|
173 | * should be made...
|
---|
174 | */
|
---|
175 | public void postFileMetadata(URL fileLocation, String namespace, String label, String value)
|
---|
176 | {
|
---|
177 | // First get the list of file groups, etc. that this file is associated with...
|
---|
178 | List fileGroups = this.fileSet.findGroups(fileLocation);
|
---|
179 |
|
---|
180 | // Next, get the METS divisions associated with each file group...
|
---|
181 | List divisions = this.structureSet.findDivisionsForFiles(fileGroups);
|
---|
182 |
|
---|
183 | // Finally, post the metadata to the metadata group associated with each structure
|
---|
184 | Iterator divisionIter = divisions.iterator();
|
---|
185 | while (divisionIter.hasNext())
|
---|
186 | { METSDivision division = (METSDivision) divisionIter.next();
|
---|
187 |
|
---|
188 | // get the open namespace for this division
|
---|
189 | METSNamespace namespaceMetadata = division.findNamespace(namespace, true, this.metadata);
|
---|
190 |
|
---|
191 | // then post the metadata to it...
|
---|
192 | namespaceMetadata.addMetadata(label, value);
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * @see DocumentInterface:setDocumentMetadata
|
---|
198 | */
|
---|
199 | public void setDocumentMetadata(String namespace, String label, String value)
|
---|
200 | { this.metadata.setMetadata("default", namespace, label, value);
|
---|
201 | this.isModified = true;
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Get the metadata structure of the document
|
---|
206 | *
|
---|
207 | * @return <code>METSDescriptive</code> the metadata holder for the document.
|
---|
208 | */
|
---|
209 | public METSDescriptiveSet getDocumentMetadata()
|
---|
210 | { return this.metadata;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Set the metadata structure for this document
|
---|
215 | *
|
---|
216 | * @param <code>METSDescriptive</code> the new metadata holder for the document.
|
---|
217 | */
|
---|
218 | public void setDocumentMetadata(METSDescriptiveSet metadata)
|
---|
219 | { this.metadata = metadata;
|
---|
220 | this.isModified = true;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Get the metadata structure of the document
|
---|
225 | *
|
---|
226 | * @return <code>METSStructureSet</code> the metadata holder for the document.
|
---|
227 | */
|
---|
228 | public METSStructureSet getDocumentStructure()
|
---|
229 | { return this.structureSet;
|
---|
230 | }
|
---|
231 |
|
---|
232 | public void setDocumentStructure(METSStructureSet structureSet)
|
---|
233 | { this.structureSet = structureSet;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Get the values associated with a particular metadata value.
|
---|
238 | *
|
---|
239 | * @param <code>String</code> the namespace to find the values in.
|
---|
240 | * @param <code>String</code> the label to match to find the values.
|
---|
241 | *
|
---|
242 | * @return <code>List</code> the values.
|
---|
243 | */
|
---|
244 | public List getDocumentMetadataItem(String namespace, String label)
|
---|
245 | { return this.metadata.getMetadata("default", namespace, label);
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Get the values associated with a particular metadata value.
|
---|
250 | *
|
---|
251 | * @param <code>String</code> the namespace and label separated by a
|
---|
252 | * colon.
|
---|
253 | *
|
---|
254 | * @return <code>List</code> the values.
|
---|
255 | */
|
---|
256 | public List getDocumentMetadataItem(String namespaceLabel)
|
---|
257 | { String namespace, label;
|
---|
258 |
|
---|
259 | int colonAt = namespaceLabel.indexOf(':');
|
---|
260 | if (colonAt < 0)
|
---|
261 | { namespace = GSDL3Namespace.GSDL3_NAMESPACE_ID;
|
---|
262 | label = namespaceLabel;
|
---|
263 | }
|
---|
264 | else
|
---|
265 | { namespace = namespaceLabel.substring(0, colonAt);
|
---|
266 | label = namespaceLabel.substring(colonAt+1);
|
---|
267 | }
|
---|
268 | return this.metadata.getMetadata("default", namespace, label);
|
---|
269 | }
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * @see DocumentInterface:getDocumentFiles
|
---|
273 | */
|
---|
274 | public METSFileSet getDocumentFiles()
|
---|
275 | { return this.fileSet;
|
---|
276 | }
|
---|
277 |
|
---|
278 | public void setDocumentFiles(METSFileSet fileSet)
|
---|
279 | { this.fileSet = fileSet;
|
---|
280 | }
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * This is just a dummy function that does nothing at this level...
|
---|
284 | */
|
---|
285 | public org.w3c.dom.Document getDOMDocument()
|
---|
286 | { return null;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * @see DocumentInterface:isMETSCompatible
|
---|
291 | */
|
---|
292 | public boolean isMETSCompatible()
|
---|
293 | { return true;
|
---|
294 | }
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * Use a default document writer - this may be overridden for subclasses...
|
---|
298 | *
|
---|
299 | * @see DocumentInterface:writeMETSObject
|
---|
300 | */
|
---|
301 | public DocumentWriter getMETSWriter()
|
---|
302 | { return new DocumentWriter();
|
---|
303 | }
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Use a default SQL document writer - this may be overridden for subclasses...
|
---|
307 | *
|
---|
308 | */
|
---|
309 | public DocumentSQLWriter getSQLWriter()
|
---|
310 | { return new DocumentSQLWriter();
|
---|
311 | }
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Obtain a document from the SQL database
|
---|
315 | */
|
---|
316 | public static AbstractDocument readSQL(GS3SQLConnection connection, ResultSet sqlResult)
|
---|
317 | { try {
|
---|
318 | DocumentID id = new DocumentID(sqlResult.getString("DocID"));
|
---|
319 | String type = sqlResult.getString("docType");
|
---|
320 |
|
---|
321 | // Use a factory method to create the correct subtype...
|
---|
322 | AbstractDocument document = DocumentFactory.createDocument(type, id);
|
---|
323 |
|
---|
324 | // Get the individual components of the document
|
---|
325 | METSFileSet fileSet = METSFileSet.readSQL(document, connection);
|
---|
326 | document.setDocumentFiles(fileSet);
|
---|
327 | METSDescriptiveSet descriptiveSet = METSDescriptiveSet.readSQL(document, connection);
|
---|
328 | document.setDocumentMetadata(descriptiveSet);
|
---|
329 | METSStructureSet structureSet = METSStructureSet.readSQL(document, connection);
|
---|
330 | document.setDocumentStructure(structureSet);
|
---|
331 |
|
---|
332 | // indicate that the document is not currently modified
|
---|
333 | document.setModified(false);
|
---|
334 | return document;
|
---|
335 | }
|
---|
336 | catch (SQLException sqlEx) {
|
---|
337 | }
|
---|
338 | return null;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /**
|
---|
342 | *
|
---|
343 | */
|
---|
344 | public boolean isModified()
|
---|
345 | { return this.isModified;
|
---|
346 | }
|
---|
347 |
|
---|
348 | public void setModified(boolean isModified)
|
---|
349 | { this.isModified = isModified;
|
---|
350 | }
|
---|
351 | }
|
---|