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

Last change on this file since 8742 was 8742, checked in by kjdon, 19 years ago

changed the import statements for GS3SQLConnection and GS3SQLConnectionFactory to reflect their move to the database package

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