Changeset 6018
- Timestamp:
- 2003-11-27T12:33:18+13:00 (20 years ago)
- Location:
- trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/doctypes
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/doctypes/AbstractDocument.java
r5944 r6018 89 89 public void setID(DocumentID id) 90 90 { this.id = id; 91 this.isModified = true; 91 92 } 92 93 -
trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/doctypes/DocumentFactory.java
r6010 r6018 1 1 package org.greenstone.gsdl3.gs3build.doctypes; 2 3 import java.sql.SQLException; 4 import java.sql.ResultSet; 5 6 import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection; 2 7 3 8 public class DocumentFactory … … 16 21 return null; 17 22 } 23 24 public static DocumentInterface readSQLDocument(GS3SQLConnection connection, DocumentID id) 25 { 26 String query = "SELECT * FROM document WHERE DocID="+id.toString()+";"; 27 connection.execute(query); 28 29 try { 30 ResultSet results = connection.getResultSet(); 31 if (results != null && results.first()) { 32 return AbstractDocument.readSQL(connection, results); 33 } 34 } 35 catch (SQLException sqlEx) { 36 System.err.println(sqlEx); 37 } 38 return null; 39 } 18 40 } -
trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/doctypes/DocumentList.java
r6010 r6018 50 50 this.count = 0; 51 51 this.connection = connection; 52 } 53 54 55 public List getDocumentIdsWithFile(URL fileLocation) 56 { List reply = new ArrayList(); 57 58 GS3SQLSelect select = new GS3SQLSelect("files"); 59 select.addField("*"); 60 GS3SQLWhere where = new GS3SQLWhere(new GS3SQLWhereItem("FileLocation", "=", fileLocation.toString())); 61 select.setWhere(where); 62 63 this.connection.execute(select.toString()); 64 65 ResultSet results = this.connection.getResultSet(); 66 if (results != null) { 67 select = new GS3SQLSelect("filegroups"); 68 select.addField("DocID"); 69 select.setDistinct(true); 70 71 where = new GS3SQLWhere(); 72 where.setCondition(GS3SQLWhere.OR_CONDITION); 73 74 GS3SQLWhereItem whereItem = null; 75 76 try { 77 results.first(); 78 do { 79 int fileGroupRef = results.getInt("FileGroupRef"); 80 whereItem = new GS3SQLWhereItem("FileGroupRef", "=", Integer.toString(fileGroupRef), GS3SQLField.INTEGER_TYPE); 81 where.add(whereItem); 82 } 83 while (results.next()); 84 select.setWhere(where); 85 results.close(); 86 87 this.connection.execute(select.toString()); 88 89 results = this.connection.getResultSet(); 90 results.first(); 91 do { 92 String docId = results.getString("DocID"); 93 reply.add(docId); 94 } while (results.next()); 95 } 96 catch (SQLException sqlEx) 97 { System.err.println(sqlEx); 98 } 99 } 100 return reply; 52 101 } 53 102 … … 161 210 162 211 /** 212 * Cache a document into the cache, without writing it to the database. 213 * Used directly by other parts of DocumentList when they know that the 214 * document is in the database already, or they are going to write it 215 * themselves... 216 * 217 * @param <code>DocumentInterface</code> the document to cache 218 */ 219 private void cacheDocument(DocumentInterface document) 220 { // increase cache size, etc. as necessary 221 if (this.used == this.size) { 222 if (this.size >= maxSize) { 223 for (int i = 0; i < this.size - 1; i ++) { 224 this.list[i] = this.list[i+1]; 225 } 226 this.used --; 227 } 228 else { 229 this.ensureSize((this.size * 2) > maxSize ? maxSize : (this.size * 2)); 230 } 231 } 232 233 // insert the document itself 234 this.list[this.used] = document; 235 } 236 237 /** 163 238 * Write the document into the document list (cache) and the database. 164 239 * … … 166 241 */ 167 242 public void addDocument(DocumentInterface document) 168 { // increase cache size, etc. as necessary 169 if (this.used == this.size) { 170 if (this.size >= maxSize) { 171 for (int i = 0; i < this.size - 1; i ++) { 172 this.list[i] = this.list[i+1]; 173 } 174 this.used --; 175 } 176 else { 177 this.ensureSize((this.size * 2) > maxSize ? maxSize : (this.size * 2)); 178 } 179 } 180 181 // insert the document itself 182 this.list[this.used] = document; 243 { // first cache it... 244 this.cacheDocument(document); 183 245 184 246 // set the document identifier, if not already set … … 188 250 } 189 251 190 // add to the database as well 191 document.getSQLWriter().writeDocument(document, this.connection); 252 // add to the database as well, if it is modified... 253 if (document.isModified()) { 254 document.getSQLWriter().writeDocument(document, this.connection); 255 } 192 256 193 257 // Remember that we've used one more item from the cache. … … 232 296 } 233 297 298 public DocumentInterface getDocument(DocumentID documentId) 299 { 300 DocumentInterface document = DocumentFactory.readSQLDocument(connection, documentId); 301 if (document != null) { 302 this.cacheDocument(document); 303 } 304 return document; 305 } 306 234 307 /** 235 308 public DocumentID getDocumentID(int index) … … 358 431 { throw new UnsupportedOperationException("DocumentList does not support iterator removal of documents"); 359 432 } 360 361 public List getDocumentIdsWithFile(URL fileLocation)362 { List reply = new ArrayList();363 364 GS3SQLSelect select = new GS3SQLSelect("files");365 select.addField("*");366 GS3SQLWhere where = new GS3SQLWhere(new GS3SQLWhereItem("FileLocation", "=", fileLocation.toString()));367 select.setWhere(where);368 369 this.connection.execute(select.toString());370 371 ResultSet results = this.connection.getResultSet();372 if (results != null) {373 select = new GS3SQLSelect("filegroups");374 select.addField("DocID");375 select.setDistinct(true);376 377 where = new GS3SQLWhere();378 where.setCondition(GS3SQLWhere.OR_CONDITION);379 380 GS3SQLWhereItem whereItem = null;381 382 try {383 results.first();384 do {385 int fileGroupRef = results.getInt("FileGroupRef");386 whereItem = new GS3SQLWhereItem("FileGroupRef", "=", Integer.toString(fileGroupRef), GS3SQLField.INTEGER_TYPE);387 where.add(whereItem);388 }389 while (results.next());390 select.setWhere(where);391 results.close();392 393 this.connection.execute(select.toString());394 395 results = this.connection.getResultSet();396 results.first();397 do {398 String docId = results.getString("DocID");399 reply.add(docId);400 } while (results.next());401 }402 catch (SQLException sqlEx)403 { System.err.println(sqlEx);404 }405 }406 return reply;407 }408 433 }
Note:
See TracChangeset
for help on using the changeset viewer.