source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFileGroup.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: 10.6 KB
Line 
1package org.greenstone.gsdl3.gs3build.metadata;
2
3import java.util.List;
4import java.util.ArrayList;
5import java.util.Iterator;
6
7import java.net.URL;
8
9import java.sql.SQLException;
10import java.sql.ResultSet;
11
12import org.w3c.dom.Element;
13import org.w3c.dom.NodeList;
14
15
16import java.io.PrintWriter;
17
18import org.greenstone.gsdl3.gs3build.util.XMLTools;
19import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
20import org.greenstone.gsdl3.gs3build.database.*;
21
22import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
23
24/**
25 * Child groups are indicated by colons. E.g. Chapter1:Section1 would indicate
26 * the group "Section1" inside the "Chapter1" group...
27 */
28
29public class METSFileGroup
30{ List children;
31 List childGroups;
32 String id;
33
34 public static final String SECTION_PARENT = "Section";
35 public static final String GROUP_PARENT = "Group";
36
37 public METSFileGroup(String id)
38 { this.children = new ArrayList();
39 this.childGroups = new ArrayList();
40 this.id = id;
41 }
42
43 /**
44 * Get the modified datestamp that applies for this file group. An empty group
45 * will return <code>0</code>.
46 *
47 * @return <code>long</code> the date modified as milliseconds from the 1st January 1970
48 */
49 public long getModifiedDatestamp()
50 { long reply = 0; // empty groups reply '0'
51
52 Iterator childIter = children.iterator();
53 while (childIter.hasNext())
54 { METSFile file = (METSFile) childIter.next();
55 long fileStamp = file.getModifiedDatestamp();
56 if (fileStamp > reply) {
57 reply = fileStamp;
58 }
59 }
60
61 childIter = childGroups.iterator();
62 while (childIter.hasNext())
63 { METSFileGroup fileGroup = (METSFileGroup) childIter.next();
64 long fileStamp = fileGroup.getModifiedDatestamp();
65 if (fileStamp > reply) {
66 reply = fileStamp;
67 }
68 }
69
70 return reply;
71 }
72
73 /**
74 * Find all the occurrences of a given file, and place them
75 * in a <code>List</code>.
76 */
77 public void findGroups(URL findFile, List resultList)
78 {
79 // find in this particular group
80 Iterator childIter = children.iterator();
81 while (childIter.hasNext())
82 { METSFile file = (METSFile) childIter.next();
83 if (file.equals(findFile))
84 { resultList.add(this.id);
85 }
86 }
87
88 // iterate over the child groups
89 childIter = childGroups.iterator();
90 while (childIter.hasNext())
91 { METSFileGroup fileGroup = (METSFileGroup) childIter.next();
92
93 fileGroup.findGroups(findFile, resultList);
94 }
95 }
96
97 /**
98 * Get a sub group of this METSFileGroup.
99 *
100 * @param <code>String</code> the name of the subgroup
101 * @return <code>METSFileGroup</code> the child group, which will
102 * be <code>null</code> if the group is not known.
103 */
104 public METSFileGroup getSubgroup(String id)
105 { String childId;
106
107 int dotAt = id.indexOf(':');
108 if (dotAt == -1)
109 { childId = null;
110 }
111 else
112 { childId = id.substring(dotAt+1);
113 id = id.substring(0, dotAt);
114 }
115
116 // iterate over the child groups
117 Iterator childIter = childGroups.iterator();
118 while (childIter.hasNext())
119 { METSFileGroup group = (METSFileGroup) childIter.next();
120
121 if (group.id.equals(id)) {
122 if (childId == null) {
123 return group;
124 }
125 else {
126 return group.getSubgroup(childId);
127 }
128 }
129 }
130 return null;
131 }
132
133 /**
134 * Add a filegroup to this group of files.
135 *
136 * @param <code>METSFileGroup</code> the filegroup.
137 */
138 public void addGroup(METSFileGroup group)
139 { this.childGroups.add(group);
140 }
141
142 /**
143 * Add a file to this group of files.
144 *
145 * @param <code>METSFile</code> the file in a METS File object.
146 */
147 public void addFile(METSFile file)
148 { this.children.add(file);
149 file.setGroup(this);
150 if (!file.getID().isDefined())
151 { file.setID(new METSFileID(this.id + "." + Integer.toString(this.children.size())));
152 }
153 }
154
155 /**
156 * Get the nth child of the group
157 *
158 * @param <code>int</code> the index into the group
159 * @return <code>METSFile</code> the corresponding child. This may be <code>null</code>
160 * particularly if the index given falls outside of the valid range of child
161 * indexes.
162 */
163 public METSFile getFile(int index)
164 { if (index < 0 || index >= this.children.size())
165 { return null;
166 }
167
168 return (METSFile) this.children.get(index);
169 }
170
171 /**
172 * Get all the files in the group as a <code>List</code>
173 *
174 * @return <code>List</code> the file childen of the group.
175 */
176 public List getFiles()
177 { return this.children;
178 }
179
180 /**
181 * Get all the subgroups as a <code>List</code>
182 */
183 public List getSubgroups()
184 { return this.childGroups;
185 }
186
187 /**
188 * Get the number of files in the group
189 *
190 * @return <code>int</code> the count
191 */
192 public int noOfFiles()
193 { return this.children.size();
194 }
195
196 /**
197 * Get the number of subgroups.
198 */
199 public int noOfSubgroups()
200 { return this.childGroups.size();
201 }
202
203 /**
204 * @return <code>String</code> the name of this group
205 */
206 public String getId()
207 { return this.id;
208 }
209
210 public static METSFileGroup parse_groupXML(Element element, METSFileSet set, String parseFilePath, METSFileGroup group)
211 {
212 //deal with mets:fileGrp
213 NodeList children = element.getChildNodes();
214
215 for (int c = 0; c < children.getLength(); c++) {
216 if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE){
217 continue;
218 }
219 Element child = (Element) children.item(c);
220 String childName = child.getNodeName();
221
222 if (childName.equals("mets:fileGrp")) {
223 String groupId = child.getAttribute("ID");
224 METSFileGroup childGroup = new METSFileGroup(groupId);
225
226 if (group != null){
227 group.addGroup(childGroup);
228 } else {
229 set.addGroup(childGroup);
230 }
231 METSFile file = METSFile.parse_file(child, childGroup, parseFilePath);
232 }
233 }
234 return group;
235 }
236
237 /**
238 * Write the object in an XML METS format.
239 *
240 * @param <code>PrintWriter</code> the printwriter being used to write the object
241 */
242 public void write(PrintWriter writer)
243 { String tag = XMLTools.getOpenTag("mets","fileGrp");
244 tag = XMLTools.addAttribute(tag, "ID", this.id);
245 writer.println(tag);
246
247 Iterator childIter = children.iterator();
248 while (childIter.hasNext())
249 { METSFile file = (METSFile) childIter.next();
250 file.write(writer);
251 }
252 writer.println(XMLTools.getCloseTag("mets","fileGrp"));
253 }
254
255 public boolean writeSQL(DocumentInterface document, String parentRef, boolean parentIsSection,
256 GS3SQLConnection connection)
257 { int sqlId = -1;
258
259 // check if this node is in the database already
260 GS3SQLSelect select = new GS3SQLSelect("filegroups");
261 select.addField("*");
262 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("FileGroupID", "=", this.id);
263 GS3SQLWhereItem whereDoc = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
264 GS3SQLWhere where = new GS3SQLWhere(whereItem);
265 where.add(whereDoc);
266 select.setWhere(where);
267
268 connection.execute(select.toString());
269
270 ResultSet selectResult = connection.getResultSet();
271
272 try {
273 if (selectResult == null ||
274 !selectResult.first()) {
275 GS3SQLInsert insert = new GS3SQLInsert("filegroups");
276
277 insert.addValue("DocID", document.getID().toString());
278 insert.addValue("FileGroupID", this.id);
279 insert.addValue("ParentRef", parentRef, GS3SQLField.INTEGER_TYPE);
280 insert.addValue("ParentType", parentIsSection ? SECTION_PARENT : GROUP_PARENT);
281
282 if (!connection.execute(insert.toString())) {
283 return false;
284 }
285 }
286 else {
287 // TODO: update the data for this file group...
288 }
289 }
290 catch (SQLException ex)
291 { System.err.println(ex);
292 return false;
293 }
294
295 // get the filegroup reference now
296 connection.execute(select.toString());
297
298 try {
299 ResultSet results = connection.getResultSet();
300 if (results == null) {
301 return false;
302 }
303 results.first();
304 sqlId = results.getInt("FileGroupRef");
305 }
306 catch (SQLException sqlex) {
307 System.out.println(sqlex);
308 return false;
309 }
310
311 // iterate over the child groups
312 Iterator childIter = childGroups.iterator();
313 while (childIter.hasNext())
314 { METSFileGroup fileGroup = (METSFileGroup) childIter.next();
315
316 if (!fileGroup.writeSQL(document, Integer.toString(sqlId), false, connection))
317 { return false;
318 }
319 }
320
321 // iterate over the child files
322 childIter = children.iterator();
323 while (childIter.hasNext())
324 { METSFile file = (METSFile) childIter.next();
325
326 if (!file.writeSQL(sqlId, connection))
327 { return false;
328 }
329 }
330
331 return true;
332 }
333
334 public static METSFileGroup readSQL(DocumentInterface document, GS3SQLConnection connection,
335 ResultSet resultSet)
336 {
337 try {
338 String ID = resultSet.getString("FileGroupID");
339 String parentType = resultSet.getString("ParentType");
340 String parentRef = resultSet.getString("ParentRef");
341
342 // create the metadata block object
343 METSFileGroup group = new METSFileGroup(ID);
344
345 // get its metadata reference to retrieve divisions
346 int groupRef = resultSet.getInt("FileGroupRef");
347
348 // query the database for matching groups which are children of this one
349 GS3SQLSelect select = new GS3SQLSelect("filegroups");
350 select.addField("*");
351 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(groupRef),
352 GS3SQLField.INTEGER_TYPE);
353 GS3SQLWhere where = new GS3SQLWhere(whereItem);
354 whereItem = new GS3SQLWhereItem("ParentType", "=", METSFileGroup.GROUP_PARENT);
355 where.add(whereItem);
356 select.setWhere(where);
357
358 connection.execute(select.toString());
359
360 // parse through the child groups
361 ResultSet childSet = connection.getResultSet();
362 if (childSet.first()) {
363 do {
364 METSFileGroup fileGroup = METSFileGroup.readSQL(document, connection, childSet);
365 if (fileGroup != null) {
366 group.addGroup(fileGroup);
367 }
368 }
369 while (childSet.next());
370 }
371
372 // now scan for file members
373 select = new GS3SQLSelect("files");
374 select.addField("*");
375 whereItem = new GS3SQLWhereItem("FileGroupRef", "=", Integer.toString(groupRef),
376 GS3SQLField.INTEGER_TYPE);
377 where = new GS3SQLWhere(whereItem);
378 select.setWhere(where);
379 connection.execute(select.toString());
380
381 ResultSet childFileSet = connection.getResultSet();
382 if (childFileSet != null && childFileSet.first()) {
383 do {
384 METSFile file = METSFile.readSQL(connection, childFileSet);
385 if (file != null) {
386 group.addFile(file);
387 }
388 }
389 while (childFileSet.next());
390 }
391
392 return group;
393 }
394 catch (SQLException sqlEx)
395 { System.out.println(sqlEx);
396 System.exit(1);
397 }
398 return null;
399 }
400}
Note: See TracBrowser for help on using the repository browser.