Search in sources :

Example 1 with StatementType

use of model.StatementType in project dna by leifeld.

the class Sql method getStatementTypes.

/* =========================================================================
	 * Statement types
	 * ====================================================================== */
/**
 * Get an array list of all statement types in the database. The variable
 * definitions are saved as an array list of {@link model.Value Value}
 * objects containing the variable ID, variable name, and data type.
 *
 * @return An ArrayList of {@link model.StatementType StatementType} objects.
 *
 * @category statementtype
 */
public ArrayList<StatementType> getStatementTypes() {
    ArrayList<StatementType> statementTypes = new ArrayList<StatementType>();
    try (Connection conn = ds.getConnection();
        PreparedStatement s1 = conn.prepareStatement("SELECT * FROM STATEMENTTYPES;");
        PreparedStatement s2 = conn.prepareStatement("SELECT * FROM VARIABLES WHERE StatementTypeId = ?;")) {
        ArrayList<Value> variables;
        int statementTypeId;
        ResultSet r1 = s1.executeQuery();
        ResultSet r2;
        Color color;
        while (r1.next()) {
            variables = new ArrayList<Value>();
            statementTypeId = r1.getInt("ID");
            color = new Color(r1.getInt("Red"), r1.getInt("Green"), r1.getInt("Blue"));
            s2.setInt(1, statementTypeId);
            r2 = s2.executeQuery();
            while (r2.next()) {
                variables.add(new Value(r2.getInt("ID"), r2.getString("Variable"), r2.getString("DataType")));
            }
            statementTypes.add(new StatementType(r1.getInt("ID"), r1.getString("Label"), color, variables));
        }
        LogEvent l = new LogEvent(Logger.MESSAGE, "[SQL] Retrieved " + statementTypes.size() + " statement types from the database.", "Retrieved " + statementTypes.size() + " statement types from the database.");
        Dna.logger.log(l);
    } catch (SQLException e1) {
        LogEvent l = new LogEvent(Logger.ERROR, "[SQL] Failed to retrieve statement types from the database.", "Failed to retrieve statement types from the database. Check database connection and consistency of the STATEMENTTYPES and VARIABLES tables in the database.", e1);
        Dna.logger.log(l);
    }
    return statementTypes;
}
Also used : LogEvent(logger.LogEvent) SQLException(java.sql.SQLException) StatementType(model.StatementType) Color(java.awt.Color) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) Value(model.Value) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 2 with StatementType

use of model.StatementType in project dna by leifeld.

the class AttributeManager method updateVariableBox.

/**
 * Populate the variable combo box with variables after a statement type has
 * been selected in the statement type combo box.
 */
private void updateVariableBox() {
    StatementType st = (StatementType) statementTypeBox.getSelectedItem();
    ArrayList<Value> values = st.getVariables();
    variableBox.removeAllItems();
    for (int i = 0; i < values.size(); i++) {
        if (values.get(i).getDataType().equals("short text")) {
            variableBox.addItem(values.get(i));
        }
    }
}
Also used : StatementType(model.StatementType) Value(model.Value)

Example 3 with StatementType

use of model.StatementType in project dna by leifeld.

the class StatementPanel method filter.

/**
 * Filter statements. Return {@code true} if the statement should be listed
 * in the table and {@code false} otherwise. This depends on the document
 * that is currently being displayed and on the settings of the filter
 * fields, which always keep the {@code variables} list up-to-date with the
 * current filter contents, using a document filter.
 *
 * @param s           The statement that should be assessed on whether it
 *   should be displayed.
 * @param documentId  The ID of the document that is currently being
 *   displayed.
 * @return            Whether the statement should be shown or not.
 */
private boolean filter(Statement s, int documentId) {
    if (Dna.sql.getActiveCoder() == null || Dna.sql.getConnectionProfile() == null) {
        return false;
    }
    if (s.getCoderId() != Dna.sql.getActiveCoder().getId()) {
        if (Dna.sql.getActiveCoder().isPermissionViewOthersStatements() == false) {
            return false;
        } else if (Dna.sql.getActiveCoder().isPermissionViewOthersStatements(s.getCoderId()) == false) {
            return false;
        }
    }
    if (allButton.isSelected()) {
        // show all statements
        return true;
    } else if (docButton.isSelected()) {
        if (s.getDocumentId() == documentId) {
            // show statement if it's in the right document
            return true;
        } else {
            return false;
        }
    } else if (variables == null || variables.size() == 0) {
        if (statementTypeBox.getSelectedItem() == null) {
            // no statement type -> something went wrong; show the statement
            return true;
        } else if (s.getStatementTypeId() == ((StatementType) statementTypeBox.getSelectedItem()).getId()) {
            // statement type matches, variables cannot be found; show the statement
            return true;
        } else {
            // statement type does not match and there are no variables; don't show the statement
            return false;
        }
    } else {
        // check statement type from statement type box for a non-match
        if (s.getStatementTypeId() != ((StatementType) statementTypeBox.getSelectedItem()).getId()) {
            return false;
        } else {
            // check ID field for a non-match
            Pattern pattern = Pattern.compile(idFieldPattern);
            Matcher m = pattern.matcher(String.valueOf((int) s.getId()));
            if (!m.find()) {
                return false;
            }
            // check variables for a non-match
            for (int i = 0; i < variables.size(); i++) {
                pattern = Pattern.compile((String) variables.get(i).getValue());
                if (s.getValues().get(i).getValue().getClass().toString().endsWith("Entity")) {
                    m = pattern.matcher(((Entity) s.getValues().get(i).getValue()).getValue());
                } else if (s.getValues().get(i).getValue().getClass().toString().endsWith("Integer")) {
                    m = pattern.matcher(String.valueOf((int) s.getValues().get(i).getValue()));
                } else {
                    m = pattern.matcher((String) s.getValues().get(i).getValue());
                }
                if (!m.find()) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : Pattern(java.util.regex.Pattern) Entity(model.Entity) Matcher(java.util.regex.Matcher) StatementType(model.StatementType)

Example 4 with StatementType

use of model.StatementType in project dna by leifeld.

the class NetworkExporter method populateExcludeVariableList.

/**
 * Sets a new {@link DefaultListModel} in the excludeVariableList and adds variables conditional on the statement type selected
 */
public void populateExcludeVariableList() {
    excludeValues.clear();
    StatementType selected = (StatementType) statementTypeBox.getSelectedItem();
    String[] excludeVariableItems = getVariablesList(selected, true, true, true, true);
    DefaultListModel<String> excludeVariableModel = new DefaultListModel<String>();
    for (int i = 0; i < excludeVariableItems.length - 6; i++) {
        excludeVariableModel.addElement(excludeVariableItems[i]);
        excludeValues.put(excludeVariableItems[i], new ArrayList<String>());
    }
    excludeVariableModel.addElement("author");
    excludeVariableModel.addElement("source");
    excludeVariableModel.addElement("section");
    excludeVariableModel.addElement("type");
    excludeVariableList.setModel(excludeVariableModel);
}
Also used : StatementType(model.StatementType) DefaultListModel(javax.swing.DefaultListModel)

Aggregations

StatementType (model.StatementType)4 Value (model.Value)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 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 DefaultListModel (javax.swing.DefaultListModel)1 LogEvent (logger.LogEvent)1 Entity (model.Entity)1