source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/doctypes/AbstractDocument.java@ 5944

Last change on this file since 5944 was 5944, checked in by cs025, 20 years ago

Index document type, metadata extensions

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