source: branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSStructureSet.java@ 9858

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

OK, changed my mind about making SQLConnection kill off the previous statement.
To make it more transparent what is happening, you now have to create a Statement (connection.createStatement()), then use the Statement to execute the query. This means that the thing doing the query owns the Statement, and can kill it off when finished with it, and nothing else can kill it off unexpectedly. The previous way this was all implemented meant that there was a large memory leak, and some functionality actually relied on this. A newer version of the mysql connector/J has fixed the bug where the statement wasn't closed on garbage collection, but it still seems better to close it explicitly.
Hopefully I have got it all back to working as well as it was bfore, and haven't introduced any bugs :-)

  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 KB
Line 
1package org.greenstone.gsdl3.gs3build.metadata;
2
3import java.io.PrintWriter;
4
5import java.util.HashMap;
6import java.util.Map;
7import java.util.List;
8import java.util.ArrayList;
9import java.util.Iterator;
10
11import java.sql.SQLException;
12import java.sql.Statement;
13import java.sql.ResultSet;
14
15import org.w3c.dom.Element;
16import org.w3c.dom.NodeList;
17
18
19
20import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
21
22import org.greenstone.gsdl3.gs3build.database.*;
23
24public class METSStructureSet
25{
26 Map children;
27
28 public static final String GSDL3_SECTION_STRUCTURE = "Section";
29
30 public METSStructureSet()
31 {
32 this.children = new HashMap();
33 }
34
35 public void addStructure(METSStructure structure)
36 {
37 this.children.put(structure.getID(), structure);
38 }
39
40 public METSStructure getStructure(String name)
41 {
42 return (METSStructure) this.children.get(name);
43 }
44
45 /**
46 * Find the divisions that contain a list of given files
47 *
48 * @param <code>List</code> the list of files to find...
49 * @return <code>List</code> the matching divisions.
50 */
51 public List findDivisionsForFiles(List listOfFileIdentifiers)
52 {
53 List reply = new ArrayList();
54
55 // iterate across all the children...
56 Iterator structures = this.children.values().iterator();
57
58 while (structures.hasNext()){
59 METSStructure structure = (METSStructure) structures.next();
60
61 structure.findDivisionsForFiles(listOfFileIdentifiers, reply);
62 }
63 return reply;
64 }
65
66 /**
67 * Add a division to an indicated structure.
68 *
69 * @param <code>String</code> the structure identifier
70 * @param <code>METSDivision</code> the division to be added.
71 */
72 public void addDivision(String structureId, METSDivision division)
73 {
74 METSStructure structure = (METSStructure) this.children.get(structureId);
75
76 if (structure != null){
77 structure.addDivision(division);
78 }
79 }
80
81 /**
82 * Add a division to an indicated division within structure.
83 *
84 * @param <code>String</code> the structure identifier
85 * @param <code>String</code> the division identifer, within which the division
86 * is to be placed.
87 * @param <code>METSDivision</code> the division to be added.
88 */
89 public void addSubDivision(String structureId, String divisionID, METSDivision division)
90 {
91 METSStructure structure = (METSStructure) this.children.get(structureId);
92
93 if (structure != null){
94 structure.addSubDivision(divisionID, division);
95 }
96 }
97
98 /**
99 * Get a division from within a given structure
100 *
101 * @param <code>String</code> the structure identifier
102 * @param <code>String</code> the division identifer
103 *
104 * @return <code>METSDivision</code> the division
105 */
106 public METSDivision getDivision(String structureId, String divisionId)
107 {
108 METSStructure structure = (METSStructure) this.children.get(structureId);
109
110 if (structure != null){
111 return structure.getDivision(divisionId);
112 }
113 return null;
114 }
115 public static METSStructureSet parseXML(NodeList structMapSecs)
116 {
117 METSStructureSet set = new METSStructureSet();
118
119 for (int c = 0; c < structMapSecs.getLength(); c++) {
120 METSStructure structure = METSStructure.parseXML((Element) structMapSecs.item(c));
121 set.addStructure(structure);
122 }
123 return set;
124 }
125
126
127 public void write(PrintWriter writer)
128 {
129 Iterator structures = this.children.values().iterator();
130
131 while (structures.hasNext()){
132 METSStructure group = (METSStructure) structures.next();
133
134 group.write(writer);
135 }
136 }
137
138 public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
139 {
140 Iterator structures = this.children.values().iterator();
141
142 while (structures.hasNext()){
143 METSStructure group = (METSStructure) structures.next();
144
145 if (!group.writeSQL(document, connection)){
146 return false;
147 }
148 }
149 return true;
150 }
151
152 public static METSStructureSet readSQL(DocumentInterface document, GS3SQLConnection connection)
153 {
154 METSStructureSet set = new METSStructureSet();
155
156 GS3SQLSelect select = new GS3SQLSelect("structure");
157 select.addField("*");
158 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
159 select.setWhere(new GS3SQLWhere(whereItem));
160
161 // start going through the matching metadata blocks
162 try {
163 Statement statement = connection.createStatement();
164 ResultSet resultSet = statement.executeQuery(select.toString());
165 if (resultSet.first()) {
166 do {
167 METSStructure structure = METSStructure.readSQL(document, connection, resultSet);
168 if (structure != null) {
169 set.addStructure(structure);
170 }
171 } while (resultSet.next());
172 }
173 statement.close();
174 }
175 catch (SQLException sqlEx) {
176 System.err.println("METSStructureSet.readSQL(): "+sqlEx);
177 System.exit(1);
178 }
179 return set;
180 }
181}
Note: See TracBrowser for help on using the repository browser.