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