- 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/METSDivision.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.util.XMLTools; … … 18 19 public class METSDivision 19 20 { 20 String ID; 21 String ID; // the unique identifier for this division (unique within the 22 // document, not necessarily unique within the collection). 21 23 String order; // see the METS documentation for the meaning of the different 22 24 String orderLabel; // label types. … … 27 29 List fileRefs; 28 30 Map children; 31 32 public static final String STRUCTURE_PARENT = "Structure"; 33 public static final String DIVISION_PARENT = "Division"; 29 34 30 35 public METSDivision(String ID, String order, String orderLabel, String userLabel, String type) … … 43 48 } 44 49 45 /** 46 * Add a new file reference to the structure 47 * 48 * @param <code>METSFileRef</code> the file to be referenced. 49 */ 50 public void addFileReference(METSFileID fileRef) 51 { this.fileRefs.add(fileRef); 50 /** 51 * Find all the divisions that match a given list of file group 52 * identifiers... 53 * 54 * @param <code>List</code> the list of identifiers to find 55 * @param <code>List</code> the list into which to place any 56 * matching divisions. 57 */ 58 public void findDivisionsForFiles(List listOfFileIdentifiers, List resultList) 59 { Iterator iterator = this.fileRefs.iterator(); 60 61 // get the references on this division, and mark if necessary 62 while (iterator.hasNext()) 63 { String reference = iterator.next().toString(); 64 65 Iterator fileIterator = listOfFileIdentifiers.iterator(); 66 while (fileIterator.hasNext()) 67 { String fileRef = fileIterator.next().toString(); 68 69 if (fileRef.equals(reference)) 70 { resultList.add(this); 52 71 } 53 54 /** 55 * Add a new metadata reference to the structure 56 */ 57 public void addMetadataReference(String metadataRef) 58 { // TODO: correct the type 59 this.metadataRefs.add(metadataRef); 60 } 61 62 /** 63 * Add a child division, or sub-division to the document. For example, 64 * sections within a chapter. 65 * 66 * @param <code>METSDivision</code> the child division. 67 */ 68 public void addSubDivision(METSDivision child) 69 { this.children.put(child.getID(), child); 72 } 73 } 74 75 // get the child groups, and check each in turn... 76 Iterator groups = this.children.values().iterator(); 77 while (groups.hasNext()) 78 { METSDivision group = (METSDivision) groups.next(); 79 80 group.findDivisionsForFiles(listOfFileIdentifiers, resultList); 81 } 82 } 83 84 /** 85 * Get the namespace from the given set of descriptive blocks... 86 */ 87 public METSNamespace findNamespace(String namespaceName, boolean isOpenNamespace, 88 METSDescriptiveSet descriptiveSet) 89 { METSNamespace namespace = null; 90 91 // Special case - no existing metadata references 92 if (this.metadataRefs.size() == 0) { 93 // Create new Descriptive and add it to the descriptive set 94 METSDescriptive descriptive = new METSDescriptive("DM"+this.ID, this.orderLabel); 95 descriptiveSet.addDescriptive(descriptive); 96 97 // Create the namespace and add it to the Descriptive 98 namespace = NamespaceFactory.initNamespace(namespaceName); 99 if (namespace != null) 100 { descriptive.addNamespace(namespace); 101 } 102 103 return namespace; 104 } 105 106 Iterator descriptiveIterator = this.metadataRefs.iterator(); 107 108 while (descriptiveIterator.hasNext()) 109 { String descriptiveRef = descriptiveIterator.next().toString(); 110 111 METSDescriptive descriptive = descriptiveSet.getDescriptiveById(descriptiveRef); 112 113 if (isOpenNamespace) { 114 namespace = descriptive.getOpenNamespace(namespaceName); 115 } 116 else { 117 namespace = descriptive.getNamespace(namespaceName); 118 } 119 120 if (namespace != null) 121 break; 122 } 123 124 // Special case - no such namespace in any descriptive list... 125 if (namespace == null) 126 { // take the first descriptive area... 127 METSDescriptive descriptive = descriptiveSet.getDescriptiveById(this.metadataRefs.get(0).toString()); 128 129 // create the namespace, and add it to the METSDescriptive 130 namespace = NamespaceFactory.initNamespace(namespaceName); 131 if (namespace != null) 132 { descriptive.addNamespace(namespace); 133 } 134 } 135 136 return namespace; 137 } 138 139 /** 140 * Add a new file reference to the structure 141 * 142 * @param <code>METSFileRef</code> the file to be referenced. 143 */ 144 public void addFileReference(METSFileID fileRef) 145 { this.fileRefs.add(fileRef); 146 } 147 148 /** 149 * Add a new metadata reference to the structure 150 */ 151 public void addMetadataReference(String metadataRef) 152 { // TODO: correct the type 153 this.metadataRefs.add(metadataRef); 154 } 155 156 /** 157 * Add a child division, or sub-division to the document. For example, 158 * sections within a chapter. 159 * 160 * @param <code>METSDivision</code> the child division. 161 */ 162 public void addSubDivision(METSDivision child) 163 { this.children.put(child.getID(), child); 164 } 165 166 /** 167 * Add a child division to a descendent of this division. 168 * 169 * @param <code>String</code> the path to the division into which the 170 * subdivision will be added. 171 * @param <code>METSDivision</code> the division to be added 172 */ 173 public void addSubDivision(String divisionID, METSDivision division) 174 { String topDivision = METSDivision.getTopDivisionName(divisionID); 175 176 METSDivision child = (METSDivision) this.children.get(topDivision); 177 if (child == null) 178 { return; 179 } 180 181 String subDivisionID = METSDivision.getSubDivisionName(divisionID); 182 if (subDivisionID == null) 183 { child.addSubDivision(division); 184 } 185 else 186 { child.addSubDivision(subDivisionID, division); 187 } 188 } 189 190 public METSDivision getDivision(String divisionID) 191 { String topDivision = METSDivision.getTopDivisionName(divisionID); 192 193 METSDivision child = (METSDivision) this.children.get(topDivision); 194 if (child == null) 195 { return null; 196 } 197 198 String subDivision = METSDivision.getSubDivisionName(divisionID); 199 if (subDivision != null) 200 { child = child.getDivision(subDivision); 201 } 202 203 return child; 204 } 205 206 public static String getSubDivisionName(String divisionName) 207 { int at = divisionName.indexOf('.'); 208 209 if (at >= 0) 210 { return divisionName.substring(at+1); 211 } 212 else 213 { return null; 214 } 215 } 216 217 public static String getTopDivisionName(String divisionName) 218 { int at = divisionName.indexOf('.'); 219 220 if (at >= 0) 221 { return divisionName.substring(0, at); 222 } 223 else 224 { return divisionName; 225 } 226 } 227 228 /** 229 * Write the METS file in an XML to a text-output sink 230 * 231 * @param <code>PrintWriter</code> the destination of the output 232 */ 233 public void write(PrintWriter writer) 234 { String tag = XMLTools.getOpenTag("mets", "div"); 235 tag = XMLTools.addAttribute(tag, "ID", this.ID.toString()); 236 tag = XMLTools.addAttribute(tag, "TYPE", this.type); 237 tag = XMLTools.addAttribute(tag, "ORDER", this.order); 238 if (this.orderLabel != null) 239 { tag = XMLTools.addAttribute(tag, "ORDERLABEL", this.orderLabel); 240 } 241 if (this.userLabel != null) 242 { tag = XMLTools.addAttribute(tag, "LABEL", this.userLabel); 243 } 244 writer.println(tag); 245 246 // write the list of file pointers for this structural element 247 if (this.fileRefs.size() > 0) 248 { tag = XMLTools.getOpenTag("mets", "fptr"); 249 250 String fileList = this.writeList(this.fileRefs); 251 252 tag = XMLTools.addAttribute(tag, "FILEID", fileList); 253 254 tag = XMLTools.makeSingleton(tag); 255 writer.println(tag); 256 } 257 258 // ..and then the metadata - this is the same code at present, 259 // but is replicated here in case of future amendment. 260 if (this.metadataRefs.size() > 0) 261 { tag = XMLTools.getOpenTag("mets", "mptr"); 262 263 String metaList = this.writeList(this.metadataRefs); 264 tag = XMLTools.addAttribute(tag, "METAID", metaList); 265 tag = XMLTools.makeSingleton(tag); 266 writer.println(tag); 267 } 268 269 // close the div element 270 writer.println(XMLTools.getCloseTag("mets", "div")); 271 } 272 273 private String writeList(List list) 274 { StringBuffer listString = new StringBuffer(); 275 276 Iterator iterator = list.iterator(); 277 while (iterator.hasNext()) 278 { String item = iterator.next().toString(); 279 if (listString.length() > 0) 280 { listString.append(" "); 281 } 282 listString.append(item); 283 } 284 return listString.toString(); 285 } 286 287 public boolean writeSQL(int parentRef, boolean parentIsStructure, GS3SQLConnection connection) 288 { int sqlRef = -1; 289 290 // set the "where" to find the reference for this division 291 GS3SQLSelect select = new GS3SQLSelect("divisions"); 292 select.addField("divisionRef"); 293 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(parentRef), GS3SQLField.INTEGER_TYPE); 294 GS3SQLWhereItem parentItem = new GS3SQLWhereItem("ParentType", "=", parentIsStructure ? "Structure" : "Division"); 295 GS3SQLWhereItem item = new GS3SQLWhereItem("SectionID", "=", this.ID.toString()); 296 GS3SQLWhere where = new GS3SQLWhere(whereItem); 297 where.add(parentItem); 298 where.add(item); 299 select.setWhere(where); 300 301 connection.execute(select.toString()); 302 303 // Do the actual writing 304 GS3SQLAction action; 305 306 ResultSet resultSet = connection.getResultSet(); 307 try { 308 if (resultSet == null || 309 !resultSet.first()) 310 { resultSet = null; 311 312 GS3SQLInsert insert = new GS3SQLInsert("divisions"); 313 insert.addValue("SectionID", this.ID.toString()); 314 insert.addValue("ParentRef", Integer.toString(parentRef)); 315 insert.addValue("ParentType", parentIsStructure == true ? STRUCTURE_PARENT : DIVISION_PARENT); 316 317 action = insert; 318 } 319 else { 320 sqlRef = resultSet.getInt("divisionRef"); 321 322 GS3SQLUpdate update = new GS3SQLUpdate("divisions"); 323 GS3SQLWhere updateWhere = 324 new GS3SQLWhere(new GS3SQLWhereItem("divisionRef", "=", Integer.toString(sqlRef), 325 GS3SQLField.INTEGER_TYPE)); 326 update.setWhere(updateWhere); 327 action = update; 328 } 329 } 330 catch (SQLException sql) { 331 System.err.println(sql); 332 return false; 333 } 334 335 action.addValue("Type", this.type.toString()); 336 action.addValue("LabelOrder", this.order); 337 action.addValue("ShortLabel", this.orderLabel); 338 action.addValue("UserLabel", this.userLabel); 339 340 if (!connection.execute(action.toString())) 341 { return false; 342 } 343 344 // if doing a fresh item, get the new structure reference... 345 if (resultSet == null) 346 { // get the new structure reference 347 connection.execute(select.toString()); 348 349 // get the sql reference for the division 350 try { 351 // read in the structure reference 352 resultSet = connection.getResultSet(); 353 if (resultSet == null) { 354 return false; 70 355 } 71 72 /** 73 * Add a child division to a descendent of this division. 74 * 75 * @param <code>String</code> the path to the division into which the 76 * subdivision will be added. 77 * @param <code>METSDivision</code> the division to be added 78 */ 79 public void addSubDivision(String divisionID, METSDivision division) 80 { String topDivision = METSDivision.getTopDivisionName(divisionID); 81 82 METSDivision child = (METSDivision) this.children.get(topDivision); 83 if (child == null) 84 { return; 85 } 86 87 String subDivisionID = METSDivision.getSubDivisionName(divisionID); 88 if (subDivisionID == null) 89 { child.addSubDivision(division); 90 } 91 else 92 { child.addSubDivision(subDivisionID, division); 93 } 356 resultSet.first(); 357 sqlRef = resultSet.getInt("divisionRef"); 358 resultSet.close(); 359 resultSet = null; 360 } 361 catch (SQLException sqlex) { 362 System.err.println("Unable to retrieve reference for Division " + sqlex); 363 return false; 364 } 365 } 366 // close the open resultSet, as we don't need it any longer... 367 else { 368 try { 369 resultSet.close(); 370 } 371 catch (SQLException sql) { 372 System.err.println(sql); 373 } 374 } 375 376 // delete the old file/metadata references 377 GS3SQLWhere referenceWhere = 378 new GS3SQLWhere(new GS3SQLWhereItem("divisionRef", "=", Integer.toString(sqlRef), 379 GS3SQLField.INTEGER_TYPE)); 380 381 GS3SQLDelete delete = new GS3SQLDelete("divisionfilerefs"); 382 delete.setWhere(referenceWhere); 383 connection.execute(delete.toString()); 384 385 delete = new GS3SQLDelete("divisionmetarefs"); 386 delete.setWhere(referenceWhere); 387 connection.execute(delete.toString()); 388 389 // write the new file references 390 if (this.fileRefs.size() > 0) 391 { Iterator iterator = this.fileRefs.iterator(); 392 393 while (iterator.hasNext()) 394 { GS3SQLInsert fileinsert = new GS3SQLInsert("divisionfilerefs"); 395 fileinsert.addValue("divisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE); 396 fileinsert.addValue("Type", "Group"); 397 fileinsert.addValue("fileRef", iterator.next().toString()); 398 connection.execute(fileinsert.toString()); 399 } 400 } 401 402 // write the metadata references 403 if (this.metadataRefs.size() > 0) 404 { Iterator iterator = this.metadataRefs.iterator(); 405 406 while (iterator.hasNext()) 407 { GS3SQLInsert metainsert = new GS3SQLInsert("divisionmetarefs"); 408 metainsert.addValue("divisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE); 409 metainsert.addValue("Type", "Group"); 410 metainsert.addValue("metadataRef", iterator.next().toString()); 411 connection.execute(metainsert.toString()); 412 } 413 } 414 415 // write out any children in turn 416 Iterator groups = this.children.values().iterator(); 417 418 while (groups.hasNext()) 419 { METSDivision group = (METSDivision) groups.next(); 420 421 if (!group.writeSQL(sqlRef, false, connection)) { 422 return false; 423 } 424 } 425 return true; 426 } 427 428 public static METSDivision readSQL(GS3SQLConnection connection, ResultSet resultSet) 429 { 430 METSLocation metsLocation = null; 431 432 try { 433 // read the basic information 434 String ID = resultSet.getString("SectionID"); 435 String type = resultSet.getString("Type"); 436 String order = resultSet.getString("LabelOrder"); 437 String orderLabel = resultSet.getString("ShortLabel"); 438 String userLabel = resultSet.getString("UserLabel"); 439 440 // construct the division object 441 METSDivision division = new METSDivision(ID, order, orderLabel, userLabel, type); 442 443 // obtain a list of child divisions 444 int divisionRef = resultSet.getInt("DivisionRef"); 445 446 GS3SQLSelect select = new GS3SQLSelect("divisions"); 447 select.addField("*"); 448 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(divisionRef), 449 GS3SQLField.INTEGER_TYPE); 450 GS3SQLWhere where = new GS3SQLWhere(whereItem); 451 whereItem = new GS3SQLWhereItem("ParentType", "=", DIVISION_PARENT); 452 where.add(whereItem); 453 select.setWhere(where); 454 455 connection.execute(select.toString()); 456 457 // circulate through to obtain further children 458 ResultSet childSet = connection.getResultSet(); 459 if (childSet.first()) 460 { 461 do { 462 METSDivision childDivision = METSDivision.readSQL(connection, childSet); 463 division.addSubDivision(childDivision); 94 464 } 95 96 public METSDivision getDivision(String divisionID) 97 { String topDivision = METSDivision.getTopDivisionName(divisionID); 98 99 METSDivision child = (METSDivision) this.children.get(topDivision); 100 if (child == null) 101 { return null; 102 } 103 104 String subDivision = METSDivision.getSubDivisionName(divisionID); 105 if (subDivision != null) 106 { child = child.getDivision(subDivision); 107 } 108 109 return child; 465 while (childSet.next()); 466 } 467 468 select = new GS3SQLSelect("divisionfilerefs"); 469 select.addField("*"); 470 where = new GS3SQLWhere(new GS3SQLWhereItem("divisionRef", "=", Integer.toString(divisionRef), 471 GS3SQLField.INTEGER_TYPE)); 472 select.setWhere(where); 473 474 connection.execute(select.toString()); 475 476 ResultSet fileSet = connection.getResultSet(); 477 if (fileSet != null && fileSet.first()) 478 { do 479 { String reference = fileSet.getString("fileRef"); 480 division.fileRefs.add(reference); 110 481 } 111 112 public static String getSubDivisionName(String divisionName) 113 { int at = divisionName.indexOf('.'); 114 115 if (at >= 0) 116 { return divisionName.substring(at+1); 117 } 118 else 119 { return null; 120 } 482 while (fileSet.next()); 483 } 484 485 select = new GS3SQLSelect("divisionmetarefs"); 486 select.addField("*"); 487 where = new GS3SQLWhere(new GS3SQLWhereItem("divisionRef", "=", Integer.toString(divisionRef), 488 GS3SQLField.INTEGER_TYPE)); 489 select.setWhere(where); 490 491 connection.execute(select.toString()); 492 493 ResultSet metaSet = connection.getResultSet(); 494 if (metaSet != null && metaSet.first()) 495 { do 496 { String reference = metaSet.getString("metadataRef"); 497 division.metadataRefs.add(reference); 121 498 } 122 123 public static String getTopDivisionName(String divisionName) 124 { int at = divisionName.indexOf('.'); 125 126 if (at >= 0) 127 { return divisionName.substring(0, at); 128 } 129 else 130 { return divisionName; 131 } 132 } 133 134 /** 135 * Write the METS file in an XML to a text-output sink 136 * 137 * @param <code>PrintWriter</code> the destination of the output 138 */ 139 public void write(PrintWriter writer) 140 { String tag = XMLTools.getOpenTag("mets", "div"); 141 tag = XMLTools.addAttribute(tag, "ID", this.ID.toString()); 142 tag = XMLTools.addAttribute(tag, "TYPE", this.type); 143 tag = XMLTools.addAttribute(tag, "ORDER", this.order); 144 if (this.orderLabel != null) 145 { tag = XMLTools.addAttribute(tag, "ORDERLABEL", this.orderLabel); 146 } 147 if (this.userLabel != null) 148 { tag = XMLTools.addAttribute(tag, "LABEL", this.userLabel); 149 } 150 writer.println(tag); 151 152 // write the list of file pointers for this structural element 153 if (this.fileRefs.size() > 0) 154 { tag = XMLTools.getOpenTag("mets", "fptr"); 155 156 String fileList = this.writeList(this.fileRefs); 157 158 tag = XMLTools.addAttribute(tag, "FILEID", fileList); 159 160 tag = XMLTools.makeSingleton(tag); 161 writer.println(tag); 162 } 163 164 // ..and then the metadata - this is the same code at present, 165 // but is replicated here in case of future amendment. 166 if (this.metadataRefs.size() > 0) 167 { tag = XMLTools.getOpenTag("mets", "mptr"); 168 169 String metaList = this.writeList(this.metadataRefs); 170 tag = XMLTools.addAttribute(tag, "METAID", metaList); 171 tag = XMLTools.makeSingleton(tag); 172 writer.println(tag); 173 } 174 175 // close the div element 176 writer.println(XMLTools.getCloseTag("mets", "div")); 177 } 178 179 private String writeList(List list) 180 { StringBuffer listString = new StringBuffer(); 181 182 Iterator iterator = list.iterator(); 183 while (iterator.hasNext()) 184 { String item = iterator.next().toString(); 185 if (listString.length() > 0) 186 { listString.append(" "); 187 } 188 listString.append(item); 189 } 190 return listString.toString(); 191 } 192 193 public void writeSQL(int parentRef, boolean parentIsStructure, GS3SQLConnection connection) 194 { Iterator groups = this.children.values().iterator(); 195 196 GS3SQLInsert insert = new GS3SQLInsert("divisions"); 197 insert.addValue("SectionID", this.ID.toString()); 198 insert.addValue("Type", this.type.toString()); 199 insert.addValue("LabelOrder", this.order); 200 insert.addValue("ShortLabel", this.orderLabel); 201 insert.addValue("UserLabel", this.userLabel); 202 insert.addValue("ParentRef", Integer.toString(parentRef)); 203 insert.addValue("ParentType", parentIsStructure == true ? "Structure" : "Division"); 204 205 System.out.println(insert.toString()); 206 connection.execute(insert.toString()); 207 208 // TODO: write metadata references 209 // TODO: write file references 210 211 // get the new structure reference 212 GS3SQLSelect select = new GS3SQLSelect("divisions"); 213 select.addField("divisionRef"); 214 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(parentRef), GS3SQLField.INTEGER_TYPE); 215 GS3SQLWhereItem parentItem = new GS3SQLWhereItem("ParentType", "=", parentIsStructure ? "Structure" : "Division"); 216 GS3SQLWhereItem item = new GS3SQLWhereItem("SectionID", "=", this.ID.toString()); 217 GS3SQLWhere where = new GS3SQLWhere(whereItem); 218 where.add(parentItem); 219 where.add(item); 220 select.setWhere(where); 221 connection.execute(select.toString()); 222 223 // get the sql reference for the division 224 int sqlRef; 225 226 try { 227 // read in the structure reference 228 connection.getResultSet().first(); 229 sqlRef = connection.getResultSet().getInt("divisionRef"); 230 } 231 catch (SQLException sqlex) { 232 System.out.println("Unable to retrieve reference for Division " + sqlex); 233 return; 499 while (metaSet.next()); 234 500 } 235 501 236 // write the file references 237 if (this.fileRefs.size() > 0) 238 { Iterator iterator = this.fileRefs.iterator(); 239 240 while (iterator.hasNext()) 241 { GS3SQLInsert fileinsert = new GS3SQLInsert("divisionfilerefs"); 242 fileinsert.addValue("divisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE); 243 fileinsert.addValue("Type", "Group"); 244 fileinsert.addValue("fileRef", iterator.next().toString()); 245 connection.execute(fileinsert.toString()); 246 } 247 } 248 249 // write the metadata references 250 if (this.metadataRefs.size() > 0) 251 { Iterator iterator = this.metadataRefs.iterator(); 252 253 while (iterator.hasNext()) 254 { GS3SQLInsert metainsert = new GS3SQLInsert("divisionmetarefs"); 255 metainsert.addValue("divisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE); 256 metainsert.addValue("Type", "Group"); 257 metainsert.addValue("metadataRef", iterator.next().toString()); 258 connection.execute(metainsert.toString()); 259 } 260 } 261 262 // write out any children in turn 263 while (groups.hasNext()) 264 { METSDivision group = (METSDivision) groups.next(); 265 266 group.writeSQL(sqlRef, false, connection); 267 } 268 } 269 502 return division; 503 } 504 catch (SQLException sqlEx) 505 { System.out.println(sqlEx); 506 } 507 return null; 508 } 270 509 }
Note:
See TracChangeset
for help on using the changeset viewer.