- Timestamp:
- 2003-11-24T14:26:53+13:00 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSStructure.java
r5800 r5945 10 10 11 11 import java.sql.SQLException; 12 import java.sql.ResultSet; 12 13 13 14 import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface; … … 19 20 public class METSStructure 20 21 { 21 Map children; 22 String ID; 23 String label; 24 String type; 25 26 public METSStructure(String id, String label, String type) 27 { this.children = new HashMap(); 28 this.ID = id; 29 this.label = label; 30 this.type = type; 31 } 32 33 public String getID() 34 { return this.ID; 35 } 36 37 public void addDivision(METSDivision division) 38 { this.children.put(division.getID(), division); 39 } 40 41 public void addSubDivision(String divisionID, METSDivision division) 42 { String topDivision = METSDivision.getTopDivisionName(divisionID); 43 44 METSDivision child = (METSDivision) this.children.get(topDivision); 45 if (child == null) 46 { return; 47 } 48 49 String subDivisionID = METSDivision.getSubDivisionName(divisionID); 50 if (subDivisionID == null) 51 { child.addSubDivision(division); 52 } 53 else 54 { child.addSubDivision(subDivisionID, division); 55 } 56 } 57 58 public METSDivision getDivision(String divisionID) 59 { String topDivision = METSDivision.getTopDivisionName(divisionID); 60 61 METSDivision child = (METSDivision) this.children.get(topDivision); 62 if (child == null) 63 { return null; 64 } 65 66 String subDivision = METSDivision.getSubDivisionName(divisionID); 67 if (subDivision != null) 68 { child = child.getDivision(subDivision); 69 } 70 71 return child; 72 } 73 74 public void write(PrintWriter writer) 75 { Iterator groups = this.children.values().iterator(); 76 77 String tag = XMLTools.getOpenTag("mets", "structMap"); 78 tag = XMLTools.addAttribute(tag, "ID", this.ID.toString()); 79 tag = XMLTools.addAttribute(tag, "TYPE", this.type.toString()); 80 tag = XMLTools.addAttribute(tag, "LABEL", this.label.toString()); 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); 81 123 82 writer.println(tag); 83 while (groups.hasNext()) 84 { METSDivision group = (METSDivision) groups.next(); 85 86 group.write(writer); 87 } 88 writer.println("</mets:structMap>"); 89 } 90 91 92 public void writeSQL(DocumentInterface document, GS3SQLConnection connection) 93 { Iterator groups = this.children.values().iterator(); 94 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) { 95 172 GS3SQLInsert insert = new GS3SQLInsert("structure"); 96 173 insert.addValue("DocID", document.getID().toString()); 97 174 insert.addValue("StructureID", this.ID.toString()); 98 insert.addValue("Type", this.type .toString());99 insert.addValue("Label", this.label .toString());175 insert.addValue("Type", this.type); 176 insert.addValue("Label", this.label); 100 177 101 System.out.println(insert.toString()); 102 connection.execute(insert.toString()); 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("Type", this.type); 200 update.addValue("Label", this.label); 201 202 connection.execute(update.toString()); 203 } 103 204 104 // get the new structure reference 105 GS3SQLSelect select = new GS3SQLSelect("structure"); 106 select.addField("StructureRef"); 107 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("StructureID", "=", this.ID); 108 GS3SQLWhereItem item = new GS3SQLWhereItem("DocID", "=", document.getID().toString()); 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(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("Type"); 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); 109 238 GS3SQLWhere where = new GS3SQLWhere(whereItem); 110 where.add(item); 239 whereItem = new GS3SQLWhereItem("ParentType", "=", METSDivision.STRUCTURE_PARENT); 240 where.add(whereItem); 111 241 select.setWhere(where); 112 System.out.println(select.toString()); 242 113 243 connection.execute(select.toString()); 114 244 115 try { 116 // read in the structure reference 117 connection.getResultSet().first(); 118 int sqlRef = connection.getResultSet().getInt("StructureRef"); 119 120 while (groups.hasNext()) 121 { METSDivision group = (METSDivision) groups.next(); 122 123 group.writeSQL(sqlRef, true, connection); 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); 124 252 } 125 253 } 126 catch (SQLException sql) { 127 System.out.println(sql); 128 } 129 } 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 } 130 264 }
Note:
See TracChangeset
for help on using the changeset viewer.