source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSStructure.java@ 6103

Last change on this file since 6103 was 6017, checked in by cs025, 21 years ago

Improved field names in database

  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
1package org.greenstone.gsdl3.gs3build.metadata;
2
3import java.io.PrintWriter;
4
5import java.util.List;
6import java.util.ArrayList;
7import java.util.Map;
8import java.util.HashMap;
9import java.util.Iterator;
10
11import java.sql.SQLException;
12import java.sql.ResultSet;
13
14import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
15
16import org.greenstone.gsdl3.gs3build.util.XMLTools;
17import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
18import org.greenstone.gsdl3.gs3build.database.*;
19
20public class METSStructure
21{
22 Map children;
23 String ID;
24 String label;
25 String type;
26
27 public METSStructure(String id, String label, String type)
28 { this.children = new HashMap();
29 this.ID = id;
30 this.label = label;
31 this.type = type;
32 }
33
34 /**
35 * Get the identifer of this structure
36 *
37 * @param <code>String</code> the identifier.
38 */
39 public String getID()
40 { return this.ID;
41 }
42
43 /**
44 * Find all the divisions that match a given list of file group
45 * identifiers...
46 *
47 * @param <code>List</code> the list of identifiers to find
48 * @param <code>List</code> the list into which to place any
49 * matching identifiers.
50 */
51 public void findDivisionsForFiles(List listOfFileIdentifiers, List resultList)
52 { Iterator groups = this.children.values().iterator();
53 while (groups.hasNext())
54 { METSDivision group = (METSDivision) groups.next();
55
56 group.findDivisionsForFiles(listOfFileIdentifiers, resultList);
57 }
58 }
59
60 /**
61 * Add a division directly underneath this structure.
62 *
63 * @param <code>METSDivision</code> the division to add.
64 */
65 public void addDivision(METSDivision division)
66 { this.children.put(division.getID(), division);
67 }
68
69 /**
70 * Add a division further into this structure.
71 *
72 * @param <code>String</code> the identifier for the division
73 * within which the division is to be added.
74 * @param <code>METSDivision</code> the division to add.
75 */
76 public void addSubDivision(String divisionID, METSDivision division)
77 { String topDivision = METSDivision.getTopDivisionName(divisionID);
78
79 METSDivision child = (METSDivision) this.children.get(topDivision);
80 if (child == null)
81 { return;
82 }
83
84 String subDivisionID = METSDivision.getSubDivisionName(divisionID);
85 if (subDivisionID == null)
86 { child.addSubDivision(division);
87 }
88 else
89 { child.addSubDivision(subDivisionID, division);
90 }
91 }
92
93 /**
94 * Get a subdivision of a given identifier...
95 *
96 * @param <code>String</code> the identifier of the division
97 * @return <code>METSDivision</code> the division, which will be
98 * <code>null</code> if it is not found.
99 */
100 public METSDivision getDivision(String divisionID)
101 { String topDivision = METSDivision.getTopDivisionName(divisionID);
102
103 METSDivision child = (METSDivision) this.children.get(topDivision);
104 if (child == null)
105 { return null;
106 }
107
108 String subDivision = METSDivision.getSubDivisionName(divisionID);
109 if (subDivision != null)
110 { child = child.getDivision(subDivision);
111 }
112
113 return child;
114 }
115
116 public void write(PrintWriter writer)
117 { Iterator groups = this.children.values().iterator();
118
119 String tag = XMLTools.getOpenTag("mets", "structMap");
120 tag = XMLTools.addAttribute(tag, "ID", this.ID.toString());
121 tag = XMLTools.addAttribute(tag, "TYPE", this.type);
122 tag = XMLTools.addAttribute(tag, "LABEL", this.label);
123
124 writer.println(tag);
125 while (groups.hasNext())
126 { METSDivision group = (METSDivision) groups.next();
127
128 group.write(writer);
129 }
130 writer.println("</mets:structMap>");
131 }
132
133
134 /**
135 * Write this METSStructure to an SQL database....
136 *
137 * @param <code>DocumentInterface</code> the enclosing document.
138 * @param <code>GS3SQLConnection</code> the SQL database connection.
139 */
140 public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
141 { int sqlRef = -1;
142 ResultSet results;
143
144 // Prepare query to see if this structure has already been written
145 GS3SQLSelect select = new GS3SQLSelect("structure");
146 select.addField("StructureRef");
147 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
148 GS3SQLWhereItem item = new GS3SQLWhereItem("StructureID", "=", this.ID.toString());
149 GS3SQLWhere where = new GS3SQLWhere(whereItem);
150 where.add(item);
151 select.setWhere(where);
152
153 // attempt to execute the query & get the current structure's reference
154 try {
155 connection.execute(select.toString());
156 results = connection.getResultSet();
157 if (results != null &&
158 results.first())
159 { sqlRef = results.getInt("structureRef");
160 }
161 else
162 { results = null;
163 }
164 }
165 catch (SQLException sqlEx) {
166 System.err.print(sqlEx);
167 return false;
168 }
169
170 // insert a new structure if it wasn't there previously
171 if (results == null) {
172 GS3SQLInsert insert = new GS3SQLInsert("structure");
173 insert.addValue("DocID", document.getID().toString());
174 insert.addValue("StructureID", this.ID.toString());
175 insert.addValue("StructureType", this.type);
176 insert.addValue("Label", this.label);
177
178 if (!connection.execute(insert.toString())) {
179 return false;
180 }
181
182 // get the new structure reference by re-running the original select object
183 connection.execute(select.toString());
184
185 try {
186 results = connection.getResultSet();
187 results.first();
188 sqlRef = results.getInt("StructureRef");
189 }
190 catch (SQLException sql) {
191 System.err.println(sql);
192 return false;
193 }
194 }
195 else {
196 GS3SQLUpdate update = new GS3SQLUpdate("structure");
197 update.setWhere(where);
198
199 update.addValue("StructureType", this.type);
200 update.addValue("Label", this.label);
201
202 connection.execute(update.toString());
203 }
204
205 // write out the child groups (Divisions) now...
206 Iterator groups = this.children.values().iterator();
207
208 while (groups.hasNext())
209 { METSDivision group = (METSDivision) groups.next();
210
211 if (!group.writeSQL(document.getID(), sqlRef, true, connection))
212 { return false;
213 }
214 }
215 return true;
216 }
217
218
219 public static METSStructure readSQL(DocumentInterface document, GS3SQLConnection connection,
220 ResultSet resultSet)
221 {
222 try {
223 String ID = resultSet.getString("StructureID");
224 String type = resultSet.getString("StructureType");
225 String label = resultSet.getString("Label");
226
227 // create the metadata block object
228 METSStructure structure = new METSStructure(ID, label, type);
229
230 // get its metadata reference to retrieve divisions
231 int structureRef = resultSet.getInt("StructureRef");
232
233 // query the database for matching division for this structure block
234 GS3SQLSelect select = new GS3SQLSelect("divisions");
235 select.addField("*");
236 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(structureRef),
237 GS3SQLField.INTEGER_TYPE);
238 GS3SQLWhere where = new GS3SQLWhere(whereItem);
239 whereItem = new GS3SQLWhereItem("ParentType", "=", METSDivision.STRUCTURE_PARENT);
240 where.add(whereItem);
241 select.setWhere(where);
242
243 connection.execute(select.toString());
244
245 // parse through the divisions
246 ResultSet divisionSet = connection.getResultSet();
247 divisionSet.first();
248 do {
249 METSDivision division = METSDivision.readSQL(connection, divisionSet);
250 if (division != null) {
251 structure.addDivision(division);
252 }
253 }
254 while (divisionSet.next());
255
256 return structure;
257 }
258 catch (SQLException sqlEx)
259 { System.out.println(sqlEx);
260 System.exit(1);
261 }
262 return null;
263 }
264}
Note: See TracBrowser for help on using the repository browser.