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

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

Added getSectionText member function to documents

  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 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 this.isModified = true;
92 }
93
94 /**
95 * Get the document identifier - this should be unique to the document,
96 * but care must be taken in the configuration of the collection to
97 * ensure that this is the case.
98 *
99 * @return <code>DocumentID</code> the identifer
100 */
101 public DocumentID getID()
102 { return this.id;
103 }
104
105 /**
106 * Indicate whether this document is indexed.
107 *
108 * @see: DocumentInterface.isIndexed
109 */
110 public boolean isIndexed()
111 {
112 return true;
113 }
114
115 /**
116 * Obtain the METS header of this document
117 *
118 * @return <code>METSHeader</code> the header
119 */
120 public METSHeader getHeader()
121 { return this.header;
122 }
123
124 /**
125 * Set the METS header for this document.
126 *
127 * @param <code>METSHeader</code> the header
128 */
129 public void setHeader(METSHeader header)
130 { this.header = header;
131 }
132
133 /**
134 * A simple implementation of the isDocumentType function that does <b>not</b> consider
135 * inheritance - it <code>must</code> be extended as required.
136 */
137 public boolean isDocumentType(String type)
138 { return type.equals(this.getDocumentType());
139 }
140
141 public abstract String getDocumentType();
142
143 public abstract String getDocumentText();
144
145 public abstract String getSectionText(String sectionId);
146
147 public String getMETSType()
148 { return "document";
149 }
150
151 /**
152 * @see DocumentInterface:addDocumentMetadata
153 */
154 public void addDocumentMetadata(String name, String value)
155 { int colonAt = name.indexOf(":");
156 String namespace;
157
158 if (colonAt > 0) {
159 namespace = name.substring(0, colonAt);
160 name = name.substring(colonAt+1);
161 }
162 else {
163 namespace = GSDL3Namespace.GSDL3_NAMESPACE_ID;
164 }
165
166 // no need to set isModified, as the following call will do it anyway!
167 this.addDocumentMetadata(namespace, name, value);
168 }
169
170 /**
171 * @see DocumentInterface:addDocumentMetadata
172 */
173 public void addDocumentMetadata(String namespace, String label, String value)
174 { this.metadata.addMetadata("default", namespace, label, value);
175 this.isModified = true;
176 }
177
178 /**
179 * Post metadata to a file in this document - the appropriate changes
180 * should be made...
181 */
182 public void postFileMetadata(URL fileLocation, String namespace, String label, String value)
183 {
184 // First get the list of file groups, etc. that this file is associated with...
185 List fileGroups = this.fileSet.findGroups(fileLocation);
186
187 // Next, get the METS divisions associated with each file group...
188 List divisions = this.structureSet.findDivisionsForFiles(fileGroups);
189
190 // Finally, post the metadata to the metadata group associated with each structure
191 Iterator divisionIter = divisions.iterator();
192 while (divisionIter.hasNext())
193 { METSDivision division = (METSDivision) divisionIter.next();
194
195 // get the open namespace for this division
196 METSNamespace namespaceMetadata = division.findNamespace(namespace, true, this.metadata);
197
198 // then post the metadata to it...
199 namespaceMetadata.addMetadata(label, value);
200 }
201 }
202
203 /**
204 * @see DocumentInterface:setDocumentMetadata
205 */
206 public void setDocumentMetadata(String namespace, String label, String value)
207 { this.metadata.setMetadata("default", namespace, label, value);
208 this.isModified = true;
209 }
210
211 /**
212 * Get the metadata structure of the document
213 *
214 * @return <code>METSDescriptive</code> the metadata holder for the document.
215 */
216 public METSDescriptiveSet getDocumentMetadata()
217 { return this.metadata;
218 }
219
220 /**
221 * Set the metadata structure for this document
222 *
223 * @param <code>METSDescriptive</code> the new metadata holder for the document.
224 */
225 public void setDocumentMetadata(METSDescriptiveSet metadata)
226 { this.metadata = metadata;
227 this.isModified = true;
228 }
229
230 /**
231 * Get the metadata structure of the document
232 *
233 * @return <code>METSStructureSet</code> the metadata holder for the document.
234 */
235 public METSStructureSet getDocumentStructure()
236 { return this.structureSet;
237 }
238
239 public void setDocumentStructure(METSStructureSet structureSet)
240 { this.structureSet = structureSet;
241 }
242
243 /**
244 * Get the values associated with a particular metadata value.
245 *
246 * @param <code>String</code> the namespace to find the values in.
247 * @param <code>String</code> the label to match to find the values.
248 *
249 * @return <code>List</code> the values.
250 */
251 public List getDocumentMetadataItem(String namespace, String label)
252 { return this.metadata.getMetadata("default", namespace, label);
253 }
254
255 /**
256 * Get the values associated with a particular metadata value.
257 *
258 * @param <code>String</code> the namespace and label separated by a
259 * colon.
260 *
261 * @return <code>List</code> the values.
262 */
263 public List getDocumentMetadataItem(String namespaceLabel)
264 { String namespace, label;
265
266 int colonAt = namespaceLabel.indexOf(':');
267 if (colonAt < 0)
268 { namespace = GSDL3Namespace.GSDL3_NAMESPACE_ID;
269 label = namespaceLabel;
270 }
271 else
272 { namespace = namespaceLabel.substring(0, colonAt);
273 label = namespaceLabel.substring(colonAt+1);
274 }
275 return this.metadata.getMetadata("default", namespace, label);
276 }
277
278 /**
279 * @see DocumentInterface:getDocumentFiles
280 */
281 public METSFileSet getDocumentFiles()
282 { return this.fileSet;
283 }
284
285 public void setDocumentFiles(METSFileSet fileSet)
286 { this.fileSet = fileSet;
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}
Note: See TracBrowser for help on using the repository browser.