source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFileSet.java@ 8699

Last change on this file since 8699 was 8461, checked in by kjdon, 20 years ago

added in Chi's changes for METS documents. mostly the addition of new/improved parseXML methods

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