Changeset 6018


Ignore:
Timestamp:
2003-11-27T12:33:18+13:00 (20 years ago)
Author:
cs025
Message:

Extensions to the document creation calls

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  
    8989  public void setID(DocumentID id)
    9090  { this.id = id;
     91    this.isModified = true;
    9192  }
    9293
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/doctypes/DocumentFactory.java

    r6010 r6018  
    11package org.greenstone.gsdl3.gs3build.doctypes;
     2
     3import java.sql.SQLException;
     4import java.sql.ResultSet;
     5
     6import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
    27
    38public class DocumentFactory
     
    1621    return null;
    1722  }
     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  }
    1840}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/doctypes/DocumentList.java

    r6010 r6018  
    5050    this.count = 0;
    5151    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;
    52101  }
    53102
     
    161210
    162211  /**
     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  /**
    163238   *  Write the document into the document list (cache) and the database.
    164239   *
     
    166241   */
    167242  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);
    183245
    184246    // set the document identifier, if not already set
     
    188250    }
    189251
    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    }
    192256
    193257    // Remember that we've used one more item from the cache.
     
    232296  }
    233297
     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
    234307  /**
    235308  public DocumentID getDocumentID(int index)
     
    358431  { throw new UnsupportedOperationException("DocumentList does not support iterator removal of documents");
    359432  }
    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   }
    408433}
Note: See TracChangeset for help on using the changeset viewer.