package org.greenstone.gsdl3.gs3build.metadata; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.List; import java.util.ArrayList; import java.util.Iterator; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface; import org.greenstone.gsdl3.gs3build.database.*; public class METSStructureSet { Map children; public static final String GSDL3_SECTION_STRUCTURE = "Section"; public METSStructureSet() { this.children = new HashMap(); } public void addStructure(METSStructure structure) { this.children.put(structure.getID(), structure); } public METSStructure getStructure(String name) { return (METSStructure) this.children.get(name); } /** * Find the divisions that contain a list of given files * * @param List the list of files to find... * @return List the matching divisions. */ public List findDivisionsForFiles(List listOfFileIdentifiers) { List reply = new ArrayList(); // iterate across all the children... Iterator structures = this.children.values().iterator(); while (structures.hasNext()){ METSStructure structure = (METSStructure) structures.next(); structure.findDivisionsForFiles(listOfFileIdentifiers, reply); } return reply; } /** * Add a division to an indicated structure. * * @param String the structure identifier * @param METSDivision the division to be added. */ public void addDivision(String structureId, METSDivision division) { METSStructure structure = (METSStructure) this.children.get(structureId); if (structure != null){ structure.addDivision(division); } } /** * Add a division to an indicated division within structure. * * @param String the structure identifier * @param String the division identifer, within which the division * is to be placed. * @param METSDivision the division to be added. */ public void addSubDivision(String structureId, String divisionID, METSDivision division) { METSStructure structure = (METSStructure) this.children.get(structureId); if (structure != null){ structure.addSubDivision(divisionID, division); } } /** * Get a division from within a given structure * * @param String the structure identifier * @param String the division identifer * * @return METSDivision the division */ public METSDivision getDivision(String structureId, String divisionId) { METSStructure structure = (METSStructure) this.children.get(structureId); if (structure != null){ return structure.getDivision(divisionId); } return null; } public static METSStructureSet parseXML(NodeList structMapSecs) { METSStructureSet set = new METSStructureSet(); for (int c = 0; c < structMapSecs.getLength(); c++) { METSStructure structure = METSStructure.parseXML((Element) structMapSecs.item(c)); set.addStructure(structure); } return set; } public void write(PrintWriter writer) { Iterator structures = this.children.values().iterator(); while (structures.hasNext()){ METSStructure group = (METSStructure) structures.next(); group.write(writer); } } public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection) { Iterator structures = this.children.values().iterator(); while (structures.hasNext()){ METSStructure group = (METSStructure) structures.next(); if (!group.writeSQL(document, connection)){ return false; } } return true; } public static METSStructureSet readSQL(DocumentInterface document, GS3SQLConnection connection) { METSStructureSet set = new METSStructureSet(); GS3SQLSelect select = new GS3SQLSelect("structure"); select.addField("*"); GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString()); select.setWhere(new GS3SQLWhere(whereItem)); // start going through the matching metadata blocks try { Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(select.toString()); if (resultSet.first()) { do { METSStructure structure = METSStructure.readSQL(document, connection, resultSet); if (structure != null) { set.addStructure(structure); } } while (resultSet.next()); } statement.close(); } catch (SQLException sqlEx) { System.err.println("METSStructureSet.readSQL(): "+sqlEx); System.exit(1); } return set; } }