[5800] | 1 | package org.greenstone.gsdl3.gs3build.metadata;
|
---|
| 2 |
|
---|
| 3 | import java.io.PrintWriter;
|
---|
| 4 |
|
---|
| 5 | import java.util.Iterator;
|
---|
| 6 | import java.util.List;
|
---|
| 7 | import java.util.ArrayList;
|
---|
| 8 | import java.util.Map;
|
---|
| 9 | import java.util.HashMap;
|
---|
| 10 |
|
---|
| 11 | import java.sql.SQLException;
|
---|
| 12 |
|
---|
| 13 | import org.greenstone.gsdl3.gs3build.util.XMLTools;
|
---|
| 14 | import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
|
---|
| 15 | import org.greenstone.gsdl3.gs3build.database.*;
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | public class METSDivision
|
---|
| 19 | {
|
---|
| 20 | String ID;
|
---|
| 21 | String order; // see the METS documentation for the meaning of the different
|
---|
| 22 | String orderLabel; // label types.
|
---|
| 23 | String userLabel;
|
---|
| 24 | String type; // e.g. section, chapter, physical or logical, etc.
|
---|
| 25 | List metadataRefs;
|
---|
| 26 | List adminRefs;
|
---|
| 27 | List fileRefs;
|
---|
| 28 | Map children;
|
---|
| 29 |
|
---|
| 30 | public METSDivision(String ID, String order, String orderLabel, String userLabel, String type)
|
---|
| 31 | { this.ID = ID;
|
---|
| 32 | this.order = order;
|
---|
| 33 | this.orderLabel = orderLabel;
|
---|
| 34 | this.userLabel = userLabel;
|
---|
| 35 | this.type = type;
|
---|
| 36 | this.fileRefs = new ArrayList();
|
---|
| 37 | this.metadataRefs = new ArrayList();
|
---|
| 38 | this.children = new HashMap();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public String getID()
|
---|
| 42 | { return this.ID;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 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);
|
---|
| 52 | }
|
---|
| 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);
|
---|
| 70 | }
|
---|
| 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 | }
|
---|
| 94 | }
|
---|
| 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;
|
---|
| 110 | }
|
---|
| 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 | }
|
---|
| 121 | }
|
---|
| 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;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 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 |
|
---|
| 270 | }
|
---|