Search in sources :

Example 1 with TableDocument

use of model.TableDocument in project dna by leifeld.

the class Sql method getTableDocuments.

/**
 * Get documents for a batch of document IDs. The documents are of class
 * {@link model.TableDocument TableDocument} and contain neither the
 * document text nor any statement statements. They do contain the full
 * coder and the number of statements at the time of execution of the method
 * as a field. If the document ID list is empty, all documents are returned.
 *
 * @param documentIds    An array of document IDs for which the data should
 *   be queried. Can be empty (to select all documents).
 * @return             An {@link java.util.ArrayList ArrayList} of
 *   {@link model.TableDocument TableDocument} objects, containing the
 *   document meta-data.
 *
 * @category document
 */
public ArrayList<TableDocument> getTableDocuments(int[] documentIds) {
    ArrayList<TableDocument> documents = new ArrayList<TableDocument>();
    String sql = "SELECT DOCUMENTS.ID, Title, Author, Source, Section, Type, Notes, Date, " + "CODERS.ID AS CoderId, Name AS CoderName, Red, Green, Blue, " + "COALESCE(Frequency, 0) AS Frequency " + "FROM DOCUMENTS LEFT JOIN " + "(SELECT DocumentId, COUNT(DocumentId) AS Frequency FROM STATEMENTS GROUP BY DocumentId) AS C ON C.DocumentId = DOCUMENTS.ID " + "LEFT JOIN CODERS ON CODERS.ID = DOCUMENTS.Coder";
    if (documentIds.length > 0) {
        sql = sql + " WHERE DOCUMENTS.ID IN(";
        for (int i = 0; i < documentIds.length; i++) {
            sql = sql + documentIds[i];
            if (i < documentIds.length - 1) {
                sql = sql + ", ";
            }
        }
        sql = sql + ")";
    }
    sql = sql + ";";
    try (Connection conn = getDataSource().getConnection();
        PreparedStatement s = conn.prepareStatement(sql)) {
        ResultSet rs = s.executeQuery();
        while (rs.next()) {
            TableDocument d = new TableDocument(rs.getInt("ID"), rs.getString("Title"), rs.getInt("Frequency"), new Coder(rs.getInt("CoderId"), rs.getString("CoderName"), new Color(rs.getInt("Red"), rs.getInt("Green"), rs.getInt("Blue"))), rs.getString("Author"), rs.getString("Source"), rs.getString("Section"), rs.getString("Type"), rs.getString("Notes"), LocalDateTime.ofEpochSecond(rs.getLong("Date"), 0, ZoneOffset.UTC));
            documents.add(d);
        }
    } catch (SQLException e) {
        LogEvent l = new LogEvent(Logger.WARNING, "[SQL] Failed to retrieve document meta-data from the database.", "Attempted to retrieve document data (other than the document text) for " + documentIds.length + " documents, but this failed. Check if all documents are being displayed in the user interface.", e);
        Dna.logger.log(l);
    }
    return documents;
}
Also used : Coder(model.Coder) TableDocument(model.TableDocument) SQLException(java.sql.SQLException) LogEvent(logger.LogEvent) Color(java.awt.Color) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 2 with TableDocument

use of model.TableDocument in project dna by leifeld.

the class DocumentTableModel method addRows.

/**
 * Add a list of {@link TableDocument} objects to the table model and notify
 * the listeners.
 *
 * @param chunks A list of {@link TableDocument} objects.
 */
void addRows(List<TableDocument> chunks) {
    int n = this.rows.size();
    for (TableDocument row : chunks) {
        rows.add(row);
    }
    // subtract one because we don't need the cursor to be at the next position; it should refer to the last position
    fireTableRowsInserted(n, n + chunks.size() - 1);
}
Also used : TableDocument(model.TableDocument)

Aggregations

TableDocument (model.TableDocument)2 Color (java.awt.Color)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 LogEvent (logger.LogEvent)1 Coder (model.Coder)1