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.io.PrintWriter;
|
---|
8 |
|
---|
9 | import org.greenstone.gsdl3.gs3build.util.XMLTools;
|
---|
10 | import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
|
---|
11 | import org.greenstone.gsdl3.gs3build.database.*;
|
---|
12 |
|
---|
13 | import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
|
---|
14 |
|
---|
15 |
|
---|
16 | public class METSFileGroup
|
---|
17 | { List children;
|
---|
18 | List childGroups;
|
---|
19 | String id;
|
---|
20 |
|
---|
21 | public METSFileGroup(String id)
|
---|
22 | { this.children = new ArrayList();
|
---|
23 | this.childGroups = new ArrayList();
|
---|
24 | this.id = id;
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Add a filegroup to this group of files.
|
---|
29 | *
|
---|
30 | * @param <code>METSFileGroup</code> the filegroup.
|
---|
31 | */
|
---|
32 | public void addGroup(METSFileGroup group)
|
---|
33 | { this.childGroups.add(group);
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Add a file to this group of files.
|
---|
38 | *
|
---|
39 | * @param <code>METSFile</code> the file in a METS File object.
|
---|
40 | */
|
---|
41 | public void addFile(METSFile file)
|
---|
42 | { this.children.add(file);
|
---|
43 | file.setGroup(this);
|
---|
44 | if (!file.getID().isDefined())
|
---|
45 | { file.setID(new METSFileID(this.id + Integer.toString(this.children.size())));
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Get the nth child of the group
|
---|
51 | *
|
---|
52 | * @param <code>int</code> the index into the group
|
---|
53 | * @return <code>METSFile</code> the corresponding child. This may be <code>null</code>
|
---|
54 | * particularly if the index given falls outside of the valid range of child
|
---|
55 | * indexes.
|
---|
56 | */
|
---|
57 | public METSFile getFile(int index)
|
---|
58 | { if (index < 0 || index >= this.children.size())
|
---|
59 | { return null;
|
---|
60 | }
|
---|
61 |
|
---|
62 | return (METSFile) this.children.get(index);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Get all the files in the group as a <code>List</code>
|
---|
67 | */
|
---|
68 | public List getFiles()
|
---|
69 | { return this.children;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Get all the subgroups as a <code>List</code>
|
---|
74 | */
|
---|
75 | public List getSubgroups()
|
---|
76 | { return this.childGroups;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Get the number of files in the group
|
---|
81 | *
|
---|
82 | * @return <code>int</code> the count
|
---|
83 | */
|
---|
84 | public int noOfFiles()
|
---|
85 | { return this.children.size();
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Get the number of subgroups.
|
---|
90 | */
|
---|
91 | public int noOfSubgroups()
|
---|
92 | { return this.childGroups.size();
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * @return <code>String</code> the name of this group
|
---|
97 | */
|
---|
98 | public String getName()
|
---|
99 | { return this.id;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Write the object in an XML METS format.
|
---|
104 | *
|
---|
105 | * @param <code>PrintWriter</code> the printwriter being used to write the object
|
---|
106 | */
|
---|
107 | public void write(PrintWriter writer)
|
---|
108 | { String tag = XMLTools.getOpenTag("mets","fileGrp");
|
---|
109 | tag = XMLTools.addAttribute(tag, "ID", this.id);
|
---|
110 | writer.println(tag);
|
---|
111 |
|
---|
112 | Iterator childIter = children.iterator();
|
---|
113 | while (childIter.hasNext())
|
---|
114 | { METSFile file = (METSFile) childIter.next();
|
---|
115 | file.write(writer);
|
---|
116 | }
|
---|
117 | writer.println(XMLTools.getCloseTag("mets","fileGrp"));
|
---|
118 | }
|
---|
119 |
|
---|
120 | public void writeSQL(DocumentInterface document, GS3SQLConnection connection)
|
---|
121 | { // check if this node is in the
|
---|
122 | GS3SQLSelect select = new GS3SQLSelect("filegroups");
|
---|
123 | select.addField("*");
|
---|
124 | GS3SQLWhereItem whereItem = new GS3SQLWhereItem("FileGroupID", "=", this.id);
|
---|
125 | GS3SQLWhereItem whereDoc = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
|
---|
126 | GS3SQLWhere where = new GS3SQLWhere(whereItem);
|
---|
127 | where.add(whereDoc);
|
---|
128 | select.setWhere(where);
|
---|
129 |
|
---|
130 | System.out.println(select.toString());
|
---|
131 | connection.execute(select.toString());
|
---|
132 |
|
---|
133 | GS3SQLInsert insert = new GS3SQLInsert("filegroups");
|
---|
134 | insert.addValue("DocID", document.getID().toString());
|
---|
135 | insert.addValue("FileGroupID", this.id);
|
---|
136 | insert.addValue("ParentGroup", "");
|
---|
137 | System.out.println("filegroup done");
|
---|
138 | connection.execute(insert.toString());
|
---|
139 |
|
---|
140 | System.out.println("filegroup done");
|
---|
141 |
|
---|
142 | Iterator childIter = children.iterator();
|
---|
143 | while (childIter.hasNext())
|
---|
144 | { METSFile file = (METSFile) childIter.next();
|
---|
145 | file.writeSQL(connection);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|