- Timestamp:
- 2003-11-24T14:26:35+13:00 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/doctypes/DocumentList.java
r5800 r5944 1 1 package org.greenstone.gsdl3.gs3build.doctypes; 2 2 3 import java.util.Iterator; 3 4 import java.util.List; 4 5 import java.util.ArrayList; … … 9 10 import java.io.IOException; 10 11 12 import java.net.URL; 13 14 import java.sql.SQLException; 15 import java.sql.ResultSet; 16 11 17 import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection; 18 import org.greenstone.gsdl3.gs3build.database.GS3SQLSelect; 19 import org.greenstone.gsdl3.gs3build.database.GS3SQLWhereItem; 20 import org.greenstone.gsdl3.gs3build.database.GS3SQLWhere; 21 import org.greenstone.gsdl3.gs3build.database.GS3SQLField; 12 22 13 23 public class DocumentList 14 24 { 15 DocumentInterface [] list; 16 int size; 17 int used; 18 DocumentIDFactoryInterface idFactory; 25 DocumentInterface [] list; // what is currently cached 26 int size; // the maximum number in the cache 27 int used; // the actual number in the cache 28 int count; // the total number of known documents 29 DocumentIDFactoryInterface idFactory; // A manufacturer of novel document IDs 30 GS3SQLConnection connection; // used to query the SQL database 31 32 private static final int maxSize = 10; 33 34 public DocumentList(GS3SQLConnection connection) 35 { this.idFactory = null; 36 this.list = new DocumentInterface[10]; 37 this.used = 0; 38 this.size = 10; 39 this.count = 0; 40 this.connection = connection; 41 } 42 43 public DocumentList(DocumentIDFactoryInterface idFactory, GS3SQLConnection connection) 44 { this.idFactory = idFactory; 45 this.list = new DocumentInterface[10]; 46 this.used = 0; 47 this.size = 10; 48 this.count = 0; 49 this.connection = connection; 50 } 51 52 /** 53 * Write the document into the document list (cache) and the database. 54 * 55 * @param <code>DocumentInterface</code> the document itself 56 */ 57 public void addDocument(DocumentInterface document) 58 { // increase cache size, etc. as necessary 59 if (this.used == this.size) { 60 if (this.size >= maxSize) { 61 for (int i = 0; i < this.size - 1; i ++) { 62 this.list[i] = this.list[i+1]; 63 } 64 this.used --; 65 } 66 else { 67 this.ensureSize((this.size * 2) > maxSize ? maxSize : (this.size * 2)); 68 } 69 } 70 71 // insert the document itself 72 this.list[this.used] = document; 73 74 // set the document identifier, if not already set 75 if (document.getID() == null) { 76 DocumentID id = this.idFactory.getNewDocumentID(document); 77 document.setID(id); 78 } 79 80 // add to the database as well 81 document.getSQLWriter().writeDocument(document, this.connection); 82 83 // Remember that we've used one more item from the cache. 84 this.used ++; 85 86 // Note additional document 87 this.count ++; 88 } 89 90 /** 91 * Note that an individual document is modified, and act accordingly 92 * 93 * @param <code>DocumentInterface</code> the document 94 */ 95 public void modifiedDocument(DocumentInterface document) 96 { document.getSQLWriter().writeDocument(document, this.connection); 97 } 98 99 /** 100 * Get an iterator across all the documents, not merely those in 101 * the cache. Note that this <code>Iterator</code> does <b>not</b> 102 * support the <code>remove()</code> function, and will raise an 103 * <code>UnsupportedOperationException</code> if you attempt to do 104 * so. 105 * 106 * @return <code>Iterator</code> the iterator across the documents. 107 */ 108 public Iterator iterator() 109 { return new DocumentListIterator(connection); 110 } 111 112 /** 113 * Get the nth member of the <b>cached</b> document list. 114 * 115 * @deprecated 116 */ 117 public DocumentInterface getDocument(int index) 118 { if (index < 0 || index >= this.used) 119 { return null; 120 } 121 return this.list[index]; 122 } 123 124 /** 125 public DocumentID getDocumentID(int index) 126 { if (index < 0 || index >= this.used) 127 { return null; 128 } 129 return this.list[index].getID(); 130 } 131 */ 132 133 protected void ensureSize(int size) 134 { DocumentInterface [] newList = new DocumentInterface[size]; 135 System.arraycopy(this.list, 0, newList, 0, this.size); 136 this.list = newList; 137 this.size = size; 138 } 139 140 public void writeDocuments(File directory) 141 { Iterator documents = this.iterator(); 142 int item = 0; 143 144 while (documents.hasNext()) 145 { DocumentInterface document = (DocumentInterface) documents.next(); 146 try 147 { item ++; 148 File localFile = new File(directory, "Doc"+Integer.toString(item)+".xml"); 149 FileWriter fileWriter = new FileWriter(localFile); 150 PrintWriter writer = new PrintWriter(fileWriter); 151 document.getMETSWriter().writeDocument(document, writer); 152 writer.close(); 153 fileWriter.close(); 154 } 155 catch (IOException io) 156 { 157 } 158 } 159 } 160 161 public void writeSQLDocuments(GS3SQLConnection connection) 162 { for (int i = 0; i < this.used; i ++) 163 { this.list[i].getSQLWriter().writeDocument(this.list[i], connection); 164 } 165 } 166 167 public static DocumentList readSQLDocuments(GS3SQLConnection connection) 168 { DocumentList list = new DocumentList(connection); 169 170 GS3SQLSelect select = new GS3SQLSelect("document"); 171 select.addField("*"); 172 173 ResultSet documents; 174 try { 175 connection.execute(select.toString()); 176 documents = connection.getResultSet(); 177 178 if (documents.first()) 179 { do 180 { DocumentInterface document = AbstractDocument.readSQL(connection, documents); 181 list.addDocument(document); 182 } 183 while (documents.next()); 184 } 185 } 186 catch (java.sql.SQLException ex) 187 { System.out.println(ex); 188 return null; 189 } 190 191 return list; 192 } 193 194 public int getCount() 195 { return this.count; 196 } 197 198 public int size() 199 { return this.used; 200 } 201 } 202 203 class DocumentListIterator implements Iterator 204 { 205 private boolean hasNext; 206 private ResultSet resultSet; 207 private GS3SQLConnection connection; 208 209 public DocumentListIterator(GS3SQLConnection connection) 210 { 211 this.connection = connection; 212 213 GS3SQLSelect select = new GS3SQLSelect("document"); 214 select.addField("*"); 215 216 try { 217 connection.execute(select.toString()); 218 this.resultSet = connection.getResultSet(); 219 this.hasNext = this.resultSet.first(); 220 } catch (SQLException ex) { 221 this.hasNext = false; 222 } 223 } 224 225 public boolean hasNext() 226 { return this.hasNext; 227 } 228 229 public Object next() 230 { 231 // get the 'next' document first 232 DocumentInterface document = AbstractDocument.readSQL(connection, this.resultSet); 233 234 // now actually step forward to the next item, so that we know if we have one! 235 try { 236 this.hasNext = this.resultSet.next(); 237 238 if (!this.hasNext) { 239 this.resultSet.close(); // be a good citizen & close used result sets 240 } 241 } catch (SQLException ex) { 242 this.hasNext = false; 243 } 244 return document; 245 } 246 247 public void remove() throws UnsupportedOperationException 248 { throw new UnsupportedOperationException("DocumentList does not support iterator removal of documents"); 249 } 250 251 public List getDocumentIdsWithFile(URL fileLocation) 252 { List reply = new ArrayList(); 253 254 GS3SQLSelect select = new GS3SQLSelect("files"); 255 select.addField("*"); 256 GS3SQLWhere where = new GS3SQLWhere(new GS3SQLWhereItem("FileLocation", "=", fileLocation.toString())); 257 select.setWhere(where); 258 259 this.connection.execute(select.toString()); 260 261 ResultSet results = this.connection.getResultSet(); 262 if (results != null) { 263 select = new GS3SQLSelect("filegroups"); 264 select.addField("DocID"); 265 select.setDistinct(true); 266 267 where = new GS3SQLWhere(); 268 where.setCondition(GS3SQLWhere.OR_CONDITION); 269 270 GS3SQLWhereItem whereItem = null; 271 272 try { 273 results.first(); 274 do { 275 int fileGroupRef = results.getInt("FileGroupRef"); 276 whereItem = new GS3SQLWhereItem("FileGroupRef", "=", Integer.toString(fileGroupRef), GS3SQLField.INTEGER_TYPE); 277 where.add(whereItem); 278 } 279 while (results.next()); 280 select.setWhere(where); 281 results.close(); 19 282 20 public DocumentList(DocumentIDFactoryInterface idFactory) 21 { this.idFactory = idFactory; 22 this.list = new DocumentInterface[10]; 23 this.used = 0; 24 this.size = 10; 25 } 26 27 public void addDocument(DocumentInterface document) 28 { if (this.used == this.size) { 29 this.ensureSize(this.size * 2); 30 } 31 this.list[this.used] = document; 32 DocumentID id = this.idFactory.getNewDocumentID(document); 33 document.setID(id); 34 this.used ++; 35 } 36 37 public DocumentInterface getDocument(int index) 38 { if (index < 0 || index >= this.used) 39 { return null; 40 } 41 return this.list[index]; 42 } 43 44 public DocumentID getDocumentID(int index) 45 { if (index < 0 || index >= this.used) 46 { return null; 47 } 48 return this.list[index].getID(); 49 } 50 51 public void ensureSize(int size) 52 { DocumentInterface [] newList = new DocumentInterface[size]; 53 System.arraycopy(this.list, 0, newList, 0, this.size); 54 this.list = newList; 55 this.size = size; 56 } 57 58 public void writeDocuments(File directory) 59 { for (int i = 0; i < this.used; i ++) 60 { try 61 { 62 File localFile = new File(directory, "Doc"+Integer.toString(i)+".xml"); 63 FileWriter fileWriter = new FileWriter(localFile); 64 PrintWriter writer = new PrintWriter(fileWriter); 65 this.list[i].getMETSWriter().writeDocument(this.list[i], writer); 66 writer.close(); 67 fileWriter.close(); 68 } 69 catch (IOException io) 70 { 71 } 72 } 73 } 74 75 public void writeSQLDocuments(GS3SQLConnection connection) 76 { for (int i = 0; i < this.used; i ++) 77 { this.list[i].getSQLWriter().writeDocument(this.list[i], connection); 78 } 79 } 80 81 public int size() 82 { return this.used; 83 } 283 this.connection.execute(select.toString()); 284 285 results = this.connection.getResultSet(); 286 results.first(); 287 do { 288 String docId = results.getString("DocID"); 289 reply.add(docId); 290 } while (results.next()); 291 } 292 catch (SQLException sqlEx) 293 { System.err.println(sqlEx); 294 } 295 } 296 return reply; 297 } 84 298 }
Note:
See TracChangeset
for help on using the changeset viewer.