- 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/METSFileSet.java
r5800 r5945 8 8 import java.util.Map; 9 9 import java.util.HashMap; 10 import java.util.List; 11 import java.util.ArrayList; 10 12 import java.util.Iterator; 11 13 12 14 import java.io.File; 13 15 import java.io.PrintWriter; 16 17 import java.sql.SQLException; 18 import java.sql.ResultSet; 19 14 20 import java.net.URL; 15 21 … … 20 26 21 27 public class METSFileSet 22 { Map fileGroups; 28 { Map fileGroups; 29 String reference; 23 30 24 31 public METSFileSet() 25 32 { this.fileGroups = new HashMap(); 26 33 this.fileGroups.put("default", new METSFileGroup("default")); 27 } 28 34 this.reference = null; 35 } 36 37 /** 38 * Get the group that corresponds to the given name... 39 * 40 * @param <code>String</code> the name of the group. 41 * @return <code>METSFileGroup</code> the group object - this will be 42 * <code>null</code> if the group is not found 43 */ 29 44 public METSFileGroup getGroup(String name) 30 45 { if (!this.fileGroups.containsKey(name)) … … 34 49 } 35 50 51 /** 52 * Get the Nth file from the default group... 53 * 54 * @param <code>int</code> the index into the default group to use... 55 * @return <code>METSFile</code> the file. 56 */ 36 57 public METSFile getFile(int index) 37 58 { METSFileGroup group = this.getGroup("default"); … … 70 91 } 71 92 93 /** 94 * Add a group at the top level of the METS File set. 95 * 96 * @param <code>METSFileGroup</code> the file group to add. 97 */ 72 98 public void addGroup(METSFileGroup group) 73 99 { this.fileGroups.put(group.getName(), group); 100 } 101 102 /** 103 * Get all the groups that contain a given URL/file. 104 * 105 * @param <code>URL</code> the location of the file. 106 * @return <code>List</code> the list of group references that contain the file. 107 */ 108 public List findGroups(URL file) 109 { List resultList = new ArrayList(); 110 111 Iterator groups = this.fileGroups.values().iterator(); 112 113 while (groups.hasNext()) 114 { METSFileGroup group = (METSFileGroup) groups.next(); 115 116 group.findGroups(file, resultList); 117 } 118 return resultList; 74 119 } 75 120 … … 86 131 } 87 132 88 public voidwriteSQL(DocumentInterface document, GS3SQLConnection connection)133 public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection) 89 134 { Iterator groups = this.fileGroups.values().iterator(); 90 135 91 GS3SQLInsert insert = new GS3SQLInsert("filesection"); 92 insert.addValue("DocID", document.getID().toString()); 93 insert.addValue("FileSecID", "test"); // TODO: remove magic string 94 95 connection.execute(insert.toString()); 136 // write the set if the reference does not already exist... 137 if (this.reference == null) 138 { // Insert the file section into the database 139 GS3SQLInsert insert = new GS3SQLInsert("filesection"); 140 insert.addValue("DocID", document.getID().toString()); 141 insert.addValue("FileSecID", "test"); // TODO: remove magic string 142 143 if (!connection.execute(insert.toString())) 144 { return false; 145 } 146 147 // find the file section number 148 GS3SQLSelect select = new GS3SQLSelect("filesection"); 149 select.addField("*"); 150 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("FileSecID", "=", "test"); 151 GS3SQLWhereItem whereDoc = new GS3SQLWhereItem("DocID", "=", document.getID().toString()); 152 GS3SQLWhere where = new GS3SQLWhere(whereItem); 153 where.add(whereDoc); 154 select.setWhere(where); 155 156 try { 157 connection.execute(select.toString()); 158 159 ResultSet set = connection.getResultSet(); 160 set.first(); 161 int sectionRef = set.getInt("FileSectionRef"); 162 163 this.reference = Integer.toString(sectionRef); 164 } 165 catch (SQLException ex) 166 { System.out.println(ex); 167 return false; 168 } 169 } 96 170 171 // write out the children 97 172 while (groups.hasNext()) 98 173 { METSFileGroup group = (METSFileGroup) groups.next(); 99 100 group.writeSQL(document, connection); 101 } 102 } 174 175 if (!group.writeSQL(document, this.reference, true, connection)) 176 { return false; 177 } 178 } 179 180 return true; 181 } 182 183 public static METSFileSet readSQL(DocumentInterface document, GS3SQLConnection connection) 184 { 185 METSFileSet set = new METSFileSet(); 186 187 // Get file sections from the filesection table (currently redundant) 188 GS3SQLSelect select = new GS3SQLSelect("filesection"); 189 select.addField("*"); 190 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString()); 191 select.setWhere(new GS3SQLWhere(whereItem)); 192 connection.execute(select.toString()); 193 194 // Get the identifier for this file set, etc. 195 ResultSet sections = connection.getResultSet(); 196 int fileSetRef; 197 try { 198 sections.first(); 199 fileSetRef = sections.getInt("FileSectionRef"); 200 set.reference = Integer.toString(fileSetRef); 201 } 202 catch (SQLException ex) 203 { System.out.println(ex); 204 return null; 205 } 206 207 // Get child file groups 208 select = new GS3SQLSelect("filegroups"); 209 select.addField("*"); 210 whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString()); 211 GS3SQLWhere where = new GS3SQLWhere(whereItem); 212 whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(fileSetRef), 213 GS3SQLField.INTEGER_TYPE); 214 where.add(whereItem); 215 whereItem = new GS3SQLWhereItem("ParentType", "=", METSFileGroup.SECTION_PARENT); 216 where.add(whereItem); 217 select.setWhere(where); 218 connection.execute(select.toString()); 219 220 // start going through the matching file groups 221 try { 222 ResultSet resultSet = connection.getResultSet(); 223 resultSet.first(); 224 do { 225 METSFileGroup filegroup = METSFileGroup.readSQL(document, connection, resultSet); 226 if (filegroup != null) { 227 set.addGroup(filegroup); 228 } 229 } while (resultSet.next()); 230 } 231 catch (SQLException sqlEx) { 232 System.out.println(sqlEx); 233 System.exit(1); 234 } 235 236 return set; 237 } 238 103 239 }
Note:
See TracChangeset
for help on using the changeset viewer.