source: trunk/greenstone3-extensions/gs3build/src/org/greenstone/gsdl3/gs3build/metadata/METSFileSet.java@ 12188

Last change on this file since 12188 was 12188, checked in by kjdon, 18 years ago

Initial revision

  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1package org.greenstone.gsdl3.gs3build.metadata;
2
3/**
4 * A class to hold all the alternative file structures
5 * for a particular METS Document
6 */
7
8import java.util.Map;
9import java.util.HashMap;
10import java.util.List;
11import java.util.ArrayList;
12import java.util.Iterator;
13
14import java.io.File;
15import java.io.PrintWriter;
16
17import java.sql.SQLException;
18import java.sql.Statement;
19import java.sql.ResultSet;
20
21import org.w3c.dom.Element;
22import org.w3c.dom.NodeList;
23
24
25import java.net.URL;
26
27import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
28
29import org.greenstone.gsdl3.gs3build.database.*;
30
31public class METSFileSet
32{
33 Map fileGroups;
34 String reference;
35 FileIdentifierFactory identifierFactory;
36 FileGroupIdentifierFactory groupIdentifierFactory;
37
38 class FileGroupIdentifierFactory extends AbstractIdentifierFactory
39 {
40 public static final String FILEGROUP_PRELUDE = "FG";
41
42 public FileGroupIdentifierFactory()
43 {
44 super("FILEGROUP_PRELUDE");
45 }
46 }
47
48 public METSFileSet()
49 {
50 this.fileGroups = new HashMap();
51 this.fileGroups.put("default", new METSFileGroup("default"));
52 this.reference = null;
53 this.identifierFactory = new FileIdentifierFactory();
54 this.groupIdentifierFactory = new FileGroupIdentifierFactory();
55 }
56
57 public long getModifiedDatestamp()
58 {
59 long reply = 0;
60
61 Iterator groupsIter = this.fileGroups.values().iterator();
62 while (groupsIter.hasNext()) {
63 METSFileGroup group = (METSFileGroup) groupsIter.next();
64 long groupStamp = group.getModifiedDatestamp();
65 if (groupStamp > reply){
66 reply = groupStamp;
67 }
68 }
69 return reply;
70 }
71
72
73 /**
74 * Get a new FileGroup to lie within this fileset. The group is not initialised with
75 * a parent, etc. - all that is done is the allocation of a unique group identifier.
76 *
77 * @return <code>METSFileGroup</code> the new group.
78 */
79 public METSFileGroup createGroup()
80 {
81 METSFileGroup group = new METSFileGroup(this.groupIdentifierFactory.getNextIdentifier());
82 return group;
83 }
84
85 public METSFile createFile(METSFilePos filePos, String type)
86 {
87 METSFile file = new METSFile(new METSFileID(this.identifierFactory.getNextIdentifier()), filePos, type);
88 return file;
89 }
90
91 /**
92 * Get the group that corresponds to the given name...
93 *
94 * @param <code>String</code> the name of the group.
95 * @return <code>METSFileGroup</code> the group object - this will be
96 * <code>null</code> if the group is not found
97 */
98 public METSFileGroup getGroup(String name)
99 {
100 if (!this.fileGroups.containsKey(name)){
101 return null;
102 }
103 return (METSFileGroup) this.fileGroups.get(name);
104 }
105
106 /**
107 * Get the Nth file from the default group...
108 *
109 * @param <code>int</code> the index into the default group to use...
110 * @return <code>METSFile</code> the file.
111 */
112 public METSFile getFile(int index)
113 {
114 METSFileGroup group = this.getGroup("default");
115
116 if (group == null) {
117 return null;
118 }
119 return group.getFile(0);
120 }
121
122 /**
123 * Add a file to the default file group
124 */
125 public void addFile(METSFile file)
126 {
127 METSFileGroup defaultGroup = (METSFileGroup) this.fileGroups.get("default");
128 defaultGroup.addFile(file);
129 }
130
131 /**
132 * Add a file to the default file group
133 *
134 * @return <code>METSFile</code> an object that wraps the given url into
135 * a METS file object
136 */
137 public METSFile addFile(URL url)
138 {
139 METSFile file = METSFile.makeMETSFile(url);
140 this.addFile(file);
141 return file;
142 }
143
144 public void addFile(METSFile file, String toGroup)
145 {
146 METSFileGroup destGroup = (METSFileGroup) this.fileGroups.get(toGroup);
147
148 if (destGroup == null){
149 destGroup = new METSFileGroup(toGroup);
150 this.fileGroups.put(destGroup.getId(), destGroup);
151 }
152
153 destGroup.addFile(file);
154 }
155
156 public void addFile(URL url, String toGroup)
157 {
158 METSFile file = METSFile.makeMETSFile(url);
159 this.addFile(file, toGroup);
160 }
161
162 /**
163 * Add a group at the top level of the METS File set.
164 *
165 * @param <code>METSFileGroup</code> the file group to add.
166 */
167 public void addGroup(METSFileGroup group)
168 {
169 this.fileGroups.put(group.getId(), group);
170 }
171
172 /**
173 * Get all the groups that contain a given URL/file.
174 *
175 * @param <code>URL</code> the location of the file.
176 * @return <code>List</code> the list of group references that contain the file.
177 */
178 public List findGroups(URL file)
179 {
180 List resultList = new ArrayList();
181
182 Iterator groups = this.fileGroups.values().iterator();
183
184 while (groups.hasNext()){
185 METSFileGroup group = (METSFileGroup) groups.next();
186
187 group.findGroups(file, resultList);
188 }
189 return resultList;
190 }
191
192 /*
193 *
194 *
195 */
196 public static METSFileSet parseXML(NodeList fileSecs, METSFileSet fileSet, String parseFilePath)
197 {
198 //METSFileSet set = new METSFileSet();>
199 // this is in effect a group without a sense of 'self'...
200 for (int c = 0; c < fileSecs.getLength(); c++) {
201 if (fileSecs.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE){
202 continue;
203 }
204
205 METSFileGroup groups = METSFileGroup.parse_groupXML((Element)fileSecs.item(c), fileSet, parseFilePath, null);
206 }
207 return fileSet;
208 }
209
210
211 public void write(PrintWriter writer)
212 {
213 Iterator groups = this.fileGroups.values().iterator();
214
215 writer.println("<mets:fileSec>");
216 while (groups.hasNext()){
217 METSFileGroup group = (METSFileGroup) groups.next();
218
219 group.write(writer);
220 }
221 writer.println("</mets:fileSec>");
222 }
223
224 public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
225 {
226 Iterator groups = this.fileGroups.values().iterator();
227
228 // write the set if the reference does not already exist...
229 if (this.reference == null){
230 // Insert the file section into the database
231 GS3SQLInsert insert = new GS3SQLInsert("filesection");
232 insert.addValue("DocID", document.getID().toString());
233 insert.addValue("FileSecID", "test"); // TODO: remove magic string
234
235 Statement statement = null;
236 try {
237 statement = connection.createStatement();
238 statement.execute(insert.toString());
239 } catch (SQLException e) {
240 System.err.println("METSFileSet.writeSQL(): "+e);
241 return false;
242 }
243
244 // find the file section number
245 GS3SQLSelect select = new GS3SQLSelect("filesection");
246 select.addField("*");
247 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("FileSecID", "=", "test");
248 GS3SQLWhereItem whereDoc = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
249 GS3SQLWhere where = new GS3SQLWhere(whereItem);
250 where.add(whereDoc);
251 select.setWhere(where);
252
253 try {
254 ResultSet set = statement.executeQuery(select.toString());
255 if (set.first()) {
256 int sectionRef = set.getInt("FileSectionRef");
257
258 this.reference = Integer.toString(sectionRef);
259 }
260 statement.close();
261 }
262 catch (SQLException ex){
263 System.out.println("METSFileSet.writeSQL(): "+ex);
264 return false;
265 }
266 }
267
268 if (this.reference == null) {
269 return false;
270 }
271 // write out the children
272 while (groups.hasNext()){
273 METSFileGroup group = (METSFileGroup) groups.next();
274
275 if (!group.writeSQL(document, this.reference, true, connection)){
276 return false;
277 }
278 }
279
280 return true;
281 }
282
283 public static METSFileSet readSQL(DocumentInterface document, GS3SQLConnection connection)
284 {
285 METSFileSet set = new METSFileSet();
286
287 // Get file sections from the filesection table (currently redundant)
288 GS3SQLSelect select = new GS3SQLSelect("filesection");
289 select.addField("*");
290 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
291 select.setWhere(new GS3SQLWhere(whereItem));
292
293 Statement statement = null;
294
295 // Get the identifier for this file set, etc.
296 int fileSetRef = -1;
297 try {
298 statement = connection.createStatement();
299 ResultSet sections = statement.executeQuery(select.toString());
300 if (sections.first()) {
301 fileSetRef = sections.getInt("FileSectionRef");
302 set.reference = Integer.toString(fileSetRef);
303 } else {
304 statement.close();
305 return null;
306 }
307
308 }
309 catch (SQLException ex){
310 System.err.println("METSFileSet.readSQL() "+ex);
311 return null;
312 }
313 // Get child file groups
314 select = new GS3SQLSelect("filegroups");
315 select.addField("*");
316 whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
317 GS3SQLWhere where = new GS3SQLWhere(whereItem);
318 whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(fileSetRef),
319 GS3SQLField.INTEGER_TYPE);
320 where.add(whereItem);
321 whereItem = new GS3SQLWhereItem("ParentType", "=", METSFileGroup.SECTION_PARENT);
322 where.add(whereItem);
323 select.setWhere(where);
324 // start going through the matching file groups
325 try {
326 ResultSet resultSet = statement.executeQuery(select.toString());
327 if (resultSet.first()) {
328 do {
329 METSFileGroup filegroup = METSFileGroup.readSQL(document, connection, resultSet);
330 if (filegroup != null) {
331 set.addGroup(filegroup);
332 }
333 } while (resultSet.next());
334 }
335 statement.close();
336 }
337 catch (SQLException sqlEx) {
338 System.out.println("METSFileSet.readSQL(): "+sqlEx);
339 System.exit(1);
340 }
341 return set;
342 }
343
344}
Note: See TracBrowser for help on using the repository browser.