Search in sources :

Example 76 with Entity

use of com.ibm.watson.assistant.v1.model.Entity in project java-sdk by watson-developer-cloud.

the class Assistant method getSynonym.

/**
 * Get entity value synonym.
 *
 * <p>Get information about a synonym of an entity value.
 *
 * @param getSynonymOptions the {@link GetSynonymOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link Synonym}
 */
public ServiceCall<Synonym> getSynonym(GetSynonymOptions getSynonymOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(getSynonymOptions, "getSynonymOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", getSynonymOptions.workspaceId());
    pathParamsMap.put("entity", getSynonymOptions.entity());
    pathParamsMap.put("value", getSynonymOptions.value());
    pathParamsMap.put("synonym", getSynonymOptions.synonym());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/entities/{entity}/values/{value}/synonyms/{synonym}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "getSynonym");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    if (getSynonymOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(getSynonymOptions.includeAudit()));
    }
    ResponseConverter<Synonym> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Synonym>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) Synonym(com.ibm.watson.assistant.v1.model.Synonym)

Example 77 with Entity

use of com.ibm.watson.assistant.v1.model.Entity in project java-sdk by watson-developer-cloud.

the class Assistant method updateSynonym.

/**
 * Update entity value synonym.
 *
 * <p>Update an existing entity value synonym with new text.
 *
 * <p>If you want to update multiple synonyms with a single API call, consider using the **[Update
 * entity](#update-entity)** or **[Update entity value](#update-entity-value)** method instead.
 *
 * @param updateSynonymOptions the {@link UpdateSynonymOptions} containing the options for the
 *     call
 * @return a {@link ServiceCall} with a result of type {@link Synonym}
 */
public ServiceCall<Synonym> updateSynonym(UpdateSynonymOptions updateSynonymOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(updateSynonymOptions, "updateSynonymOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", updateSynonymOptions.workspaceId());
    pathParamsMap.put("entity", updateSynonymOptions.entity());
    pathParamsMap.put("value", updateSynonymOptions.value());
    pathParamsMap.put("synonym", updateSynonymOptions.synonym());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/entities/{entity}/values/{value}/synonyms/{synonym}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "updateSynonym");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    if (updateSynonymOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(updateSynonymOptions.includeAudit()));
    }
    final JsonObject contentJson = new JsonObject();
    if (updateSynonymOptions.newSynonym() != null) {
        contentJson.addProperty("synonym", updateSynonymOptions.newSynonym());
    }
    builder.bodyJson(contentJson);
    ResponseConverter<Synonym> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Synonym>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) Synonym(com.ibm.watson.assistant.v1.model.Synonym)

Example 78 with Entity

use of com.ibm.watson.assistant.v1.model.Entity in project dna by leifeld.

the class Sql method updateStatement.

/**
 * Update the variable contents of a statement using new values.
 *
 * @param statementId  The ID of the statement to be updated.
 * @param values       An ArrayList of {@link model.Value Value} objects. They
 *   are used to update each variable value in the statement.
 *
 * @category statement
 */
public void updateStatement(int statementId, ArrayList<Value> values, int coderId) {
    try (Connection conn = ds.getConnection();
        PreparedStatement s1 = conn.prepareStatement("UPDATE DATABOOLEAN SET Value = ? WHERE StatementId = ? AND VariableId = ?;");
        PreparedStatement s2 = conn.prepareStatement("UPDATE DATAINTEGER SET Value = ? WHERE StatementId = ? AND VariableId = ?;");
        PreparedStatement s3 = conn.prepareStatement("UPDATE DATALONGTEXT SET Value = ? WHERE StatementId = ? AND VariableId = ?;");
        PreparedStatement s4 = conn.prepareStatement("UPDATE DATASHORTTEXT SET Entity = ? WHERE StatementId = ? AND VariableId = ?;");
        PreparedStatement s5 = conn.prepareStatement("INSERT INTO ENTITIES (VariableId, Value, Red, Green, Blue) VALUES (?, ?, ?, ?, ?);");
        PreparedStatement s6 = conn.prepareStatement("SELECT ID FROM ENTITIES WHERE VariableId = ? AND Value = ?;");
        PreparedStatement s7 = conn.prepareStatement("SELECT ID, AttributeVariable FROM ATTRIBUTEVARIABLES WHERE VariableId = ?;");
        PreparedStatement s8 = conn.prepareStatement("INSERT INTO ATTRIBUTEVALUES (EntityId, AttributeVariableId, AttributeValue) VALUES (?, ?, ?);");
        PreparedStatement s9 = conn.prepareStatement("SELECT COUNT(ID) FROM ATTRIBUTEVALUES WHERE EntityId = ? AND AttributeVariableId = ?;");
        PreparedStatement s10 = conn.prepareStatement("UPDATE STATEMENTS SET Coder = ? WHERE ID = ?;");
        SQLCloseable finish = conn::rollback) {
        conn.setAutoCommit(false);
        LogEvent e1 = new LogEvent(Logger.MESSAGE, "[SQL] Started SQL transaction to update Statement " + statementId + ".", "Started a new SQL transaction to update the variables in the statement with ID " + statementId + ". The contents will not be written into the database until the transaction is committed.");
        Dna.logger.log(e1);
        Entity entity;
        int entityId, variableId, attributeVariableId;
        ResultSet r, r2;
        for (int i = 0; i < values.size(); i++) {
            variableId = values.get(i).getVariableId();
            if (values.get(i).getDataType().equals("boolean")) {
                s1.setInt(1, (int) values.get(i).getValue());
                s1.setInt(2, statementId);
                s1.setInt(3, variableId);
                s1.executeUpdate();
                LogEvent e2 = new LogEvent(Logger.MESSAGE, "[SQL]  ├─ Variable " + variableId + " in Statement " + statementId + " was updated in the transaction.", "Variable " + variableId + " (boolean) in Statement " + statementId + " was updated in the SQL transaction with value: " + (int) values.get(i).getValue() + ".");
                Dna.logger.log(e2);
            } else if (values.get(i).getDataType().equals("integer")) {
                s2.setInt(1, (int) values.get(i).getValue());
                s2.setInt(2, statementId);
                s2.setInt(3, variableId);
                s2.executeUpdate();
                LogEvent e2 = new LogEvent(Logger.MESSAGE, "[SQL]  ├─ Variable " + variableId + " in Statement " + statementId + " was updated in the transaction.", "Variable " + variableId + " (integer) in Statement " + statementId + " was updated in the SQL transaction with value: " + (int) values.get(i).getValue() + ".");
                Dna.logger.log(e2);
            } else if (values.get(i).getDataType().equals("long text")) {
                s3.setString(1, (String) values.get(i).getValue());
                s3.setInt(2, statementId);
                s3.setInt(3, variableId);
                s3.executeUpdate();
                LogEvent e2 = new LogEvent(Logger.MESSAGE, "[SQL]  ├─ Variable " + variableId + " in Statement " + statementId + " was updated in the transaction.", "Variable " + variableId + " (long text) in Statement " + statementId + " was updated in the SQL transaction.");
                Dna.logger.log(e2);
            } else if (values.get(i).getDataType().equals("short text")) {
                // try to recognise entity ID from database; should be more reliable (e.g., with empty Strings)
                entity = (Entity) values.get(i).getValue();
                entityId = -1;
                s6.setInt(1, variableId);
                s6.setString(2, entity.getValue());
                r = s6.executeQuery();
                while (r.next()) {
                    entityId = r.getInt("ID");
                }
                if (entityId == -1) {
                    // if the attribute does not exist, insert new attribute with given String value
                    s5.setInt(1, variableId);
                    s5.setString(2, entity.getValue());
                    s5.setInt(3, entity.getColor().getRed());
                    s5.setInt(4, entity.getColor().getGreen());
                    s5.setInt(5, entity.getColor().getBlue());
                    s5.executeUpdate();
                    // new attribute has been created; now we have to get its ID
                    s6.setInt(1, variableId);
                    s6.setString(2, entity.getValue());
                    r = s6.executeQuery();
                    while (r.next()) {
                        entityId = r.getInt(1);
                    }
                    LogEvent e2 = new LogEvent(Logger.MESSAGE, "[SQL]  ├─ Entity with ID " + entityId + " added to the transaction.", "An entity with ID " + entityId + " and value \"" + entity.getValue() + "\" was created for variable ID " + variableId + " and added to the SQL transaction.");
                    Dna.logger.log(e2);
                    // since the attribute did not exist, we also need to add attributes;
                    // first get the IDs of the attribute variables, then add the attribute values
                    // set variable ID to find all attribute variables by ID corresponding to the variable
                    s7.setInt(1, variableId);
                    r = s7.executeQuery();
                    while (r.next()) {
                        try {
                            attributeVariableId = r.getInt("ID");
                            s9.setInt(1, entityId);
                            s9.setInt(2, attributeVariableId);
                            r2 = s9.executeQuery();
                            while (r2.next()) {
                                if (r2.getInt(1) > 0) {
                                // attribute value already exists in the ATTRIBUTEVALUES table; don't do anything
                                } else {
                                    // entity ID
                                    s8.setInt(1, entityId);
                                    // attribute variable ID
                                    s8.setInt(2, attributeVariableId);
                                    // put an empty value into the attribute variable field initially
                                    s8.setString(3, "");
                                    s8.executeUpdate();
                                    LogEvent l = new LogEvent(Logger.MESSAGE, "[SQL]  ├─ Transaction: Added value for attribute \"" + r.getString("AttributeVariable") + "\" for Entity " + entityId + " to the ATTRIBUTEVALUES table.", "Added attribute \"" + r.getString("AttributeVariable") + "\" for Entity " + entityId + " to the ATTRIBUTEVALUES table during the transaction.");
                                    Dna.logger.log(l);
                                }
                            }
                        } catch (Exception e3) {
                            LogEvent l = new LogEvent(Logger.WARNING, "[SQL]  ├─ Failed to add a new value for attribute \"" + r.getString("AttributeVariable") + "\" for Entity " + entityId + " to the ATTRIBUTEVALUES table.", "Failed to add a new value for attribute \"" + r.getString("AttributeVariable") + "\" for Entity " + entityId + " to the ATTRIBUTEVALUES table. The next step will check if the attribute is already there. If so, no problem. If not, there will be another log event with an error message.", e3);
                            Dna.logger.log(l);
                        }
                    }
                }
                // write the attribute ID as the value in the DATASHORTTEXT table
                s4.setInt(1, entityId);
                s4.setInt(2, statementId);
                s4.setInt(3, variableId);
                s4.executeUpdate();
                LogEvent e2 = new LogEvent(Logger.MESSAGE, "[SQL]  ├─ Variable " + variableId + " in Statement " + statementId + " was updated in the transaction.", "Variable " + variableId + " (short text) in Statement " + statementId + " was updated in the SQL transaction with Entity " + entityId + ".");
                Dna.logger.log(e2);
            }
        }
        s10.setInt(1, coderId);
        s10.setInt(2, statementId);
        s10.executeUpdate();
        conn.commit();
        LogEvent e2 = new LogEvent(Logger.MESSAGE, "[SQL]  └─ Completed SQL transaction to update Statement " + statementId + ".", "Completed SQL transaction to update the variables in the statement with ID " + statementId + ". The contents have been written into the database.");
        Dna.logger.log(e2);
    } catch (SQLException e) {
        LogEvent e2 = new LogEvent(Logger.ERROR, "[SQL]  └─ Statement " + statementId + " could not be updated in the database.", "When the statement popup window for Statement " + statementId + " was closed, the contents for the different variables could not be saved into the database. The database still contains the old values before the contents were edited. Please double-check to make sure that the statement contains the right values for all variables. Check whether the database may be locked and close all programs other than DNA that are currently accessing the database before trying again.", e);
        Dna.logger.log(e2);
    }
}
Also used : Entity(model.Entity) LogEvent(logger.LogEvent) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException) SQLException(java.sql.SQLException)

Example 79 with Entity

use of com.ibm.watson.assistant.v1.model.Entity in project dna by leifeld.

the class Sql method updateStatements.

/**
 * Update the variable contents of multiple statements using new values.
 *
 * @param statementIds  The IDs of the statements to be updated.
 * @param values        An ArrayList of ArrayLists of {@link model.Value
 *   Value} objects. They are used to update each variable value in each
 *   statement. The outer ArrayList is for the statements, and the inner
 *   ArrayList is for the variables in the given statement.
 * @param coderIds      An ArrayList of new coder IDs for the statements.
 *
 * @category statement
 */
public void updateStatements(ArrayList<Integer> statementIds, ArrayList<ArrayList<Value>> values, ArrayList<Integer> coderIds) {
    try (Connection conn = ds.getConnection();
        PreparedStatement s1 = conn.prepareStatement("UPDATE DATABOOLEAN SET Value = ? WHERE StatementId = ? AND VariableId = ?;");
        PreparedStatement s2 = conn.prepareStatement("UPDATE DATAINTEGER SET Value = ? WHERE StatementId = ? AND VariableId = ?;");
        PreparedStatement s3 = conn.prepareStatement("UPDATE DATALONGTEXT SET Value = ? WHERE StatementId = ? AND VariableId = ?;");
        PreparedStatement s4 = conn.prepareStatement("UPDATE DATASHORTTEXT SET Entity = ? WHERE StatementId = ? AND VariableId = ?;");
        PreparedStatement s5 = conn.prepareStatement("INSERT INTO ENTITIES (VariableId, Value, Red, Green, Blue) VALUES (?, ?, ?, ?, ?);");
        PreparedStatement s6 = conn.prepareStatement("SELECT ID FROM ENTITIES WHERE VariableId = ? AND Value = ?;");
        PreparedStatement s7 = conn.prepareStatement("SELECT ID, AttributeVariable FROM ATTRIBUTEVARIABLES WHERE VariableId = ?;");
        PreparedStatement s8 = conn.prepareStatement("INSERT INTO ATTRIBUTEVALUES (EntityId, AttributeVariableId, AttributeValue) VALUES (?, ?, ?);");
        PreparedStatement s9 = conn.prepareStatement("SELECT COUNT(ID) FROM ATTRIBUTEVALUES WHERE EntityId = ? AND AttributeVariableId = ?;");
        PreparedStatement s10 = conn.prepareStatement("UPDATE STATEMENTS SET Coder = ? WHERE ID = ?;");
        SQLCloseable finish = conn::rollback) {
        conn.setAutoCommit(false);
        LogEvent e1 = new LogEvent(Logger.MESSAGE, "[SQL] Started SQL transaction to update " + statementIds.size() + " statements.", "Started a new SQL transaction to update the variables in a set of " + statementIds.size() + " statements. The contents will not be written into the database until the transaction is committed.");
        Dna.logger.log(e1);
        Entity entity;
        int entityId, variableId, attributeVariableId;
        ResultSet r, r2;
        for (int i = 0; i < values.size(); i++) {
            for (int j = 0; j < values.get(i).size(); j++) {
                variableId = values.get(i).get(j).getVariableId();
                if (values.get(i).get(j).getDataType().equals("boolean")) {
                    s1.setInt(1, (int) values.get(i).get(j).getValue());
                    s1.setInt(2, statementIds.get(i));
                    s1.setInt(3, variableId);
                    s1.executeUpdate();
                    LogEvent e2 = new LogEvent(Logger.MESSAGE, "[SQL]  ├─ Variable " + variableId + " in Statement " + statementIds.get(i) + " was updated in the transaction.", "Variable " + variableId + " (boolean) in Statement " + statementIds.get(i) + " was updated in the SQL transaction with value: " + (int) values.get(i).get(j).getValue() + ".");
                    Dna.logger.log(e2);
                } else if (values.get(i).get(j).getDataType().equals("integer")) {
                    s2.setInt(1, (int) values.get(i).get(j).getValue());
                    s2.setInt(2, statementIds.get(i));
                    s2.setInt(3, variableId);
                    s2.executeUpdate();
                    LogEvent e2 = new LogEvent(Logger.MESSAGE, "[SQL]  ├─ Variable " + variableId + " in Statement " + statementIds.get(i) + " was updated in the transaction.", "Variable " + variableId + " (integer) in Statement " + statementIds.get(i) + " was updated in the SQL transaction with value: " + (int) values.get(i).get(j).getValue() + ".");
                    Dna.logger.log(e2);
                } else if (values.get(i).get(j).getDataType().equals("long text")) {
                    s3.setString(1, (String) values.get(i).get(j).getValue());
                    s3.setInt(2, statementIds.get(i));
                    s3.setInt(3, variableId);
                    s3.executeUpdate();
                    LogEvent e2 = new LogEvent(Logger.MESSAGE, "[SQL]  ├─ Variable " + variableId + " in Statement " + statementIds.get(i) + " was updated in the transaction.", "Variable " + variableId + " (long text) in Statement " + statementIds.get(i) + " was updated in the SQL transaction.");
                    Dna.logger.log(e2);
                } else if (values.get(i).get(j).getDataType().equals("short text")) {
                    // try to recognise entity ID from database; should be more reliable (e.g., with empty Strings)
                    entity = (Entity) values.get(i).get(j).getValue();
                    entityId = -1;
                    s6.setInt(1, variableId);
                    s6.setString(2, entity.getValue());
                    r = s6.executeQuery();
                    while (r.next()) {
                        entityId = r.getInt("ID");
                    }
                    if (entityId == -1) {
                        // if the attribute does not exist, insert new attribute with given String value
                        s5.setInt(1, variableId);
                        s5.setString(2, entity.getValue());
                        s5.setInt(3, entity.getColor().getRed());
                        s5.setInt(4, entity.getColor().getGreen());
                        s5.setInt(5, entity.getColor().getBlue());
                        s5.executeUpdate();
                        // new attribute has been created; now we have to get its ID
                        s6.setInt(1, variableId);
                        s6.setString(2, entity.getValue());
                        r = s6.executeQuery();
                        while (r.next()) {
                            entityId = r.getInt(1);
                        }
                        LogEvent e2 = new LogEvent(Logger.MESSAGE, "[SQL]  ├─ Entity with ID " + entityId + " added to the transaction.", "An entity with ID " + entityId + " and value \"" + entity.getValue() + "\" was created for variable ID " + variableId + " and added to the SQL transaction.");
                        Dna.logger.log(e2);
                        // since the attribute did not exist, we also need to add attributes;
                        // first get the IDs of the attribute variables, then add the attribute values
                        // set variable ID to find all attribute variables by ID corresponding to the variable
                        s7.setInt(1, variableId);
                        r = s7.executeQuery();
                        while (r.next()) {
                            try {
                                attributeVariableId = r.getInt("ID");
                                s9.setInt(1, entityId);
                                s9.setInt(2, attributeVariableId);
                                r2 = s9.executeQuery();
                                while (r2.next()) {
                                    if (r2.getInt(1) > 0) {
                                    // attribute value already exists in the ATTRIBUTEVALUES table; don't do anything
                                    } else {
                                        // entity ID
                                        s8.setInt(1, entityId);
                                        // attribute variable ID
                                        s8.setInt(2, attributeVariableId);
                                        // put an empty value into the attribute variable field initially
                                        s8.setString(3, "");
                                        s8.executeUpdate();
                                        LogEvent l = new LogEvent(Logger.MESSAGE, "[SQL]  ├─ Transaction: Added value for attribute \"" + r.getString("AttributeVariable") + "\" for Entity " + entityId + " to the ATTRIBUTEVALUES table.", "Added attribute \"" + r.getString("AttributeVariable") + "\" for Entity " + entityId + " to the ATTRIBUTEVALUES table during the transaction.");
                                        Dna.logger.log(l);
                                    }
                                }
                            } catch (Exception e3) {
                                LogEvent l = new LogEvent(Logger.WARNING, "[SQL]  ├─ Failed to add a new value for attribute \"" + r.getString("AttributeVariable") + "\" for Entity " + entityId + " to the ATTRIBUTEVALUES table.", "Failed to add a new value for attribute \"" + r.getString("AttributeVariable") + "\" for Entity " + entityId + " to the ATTRIBUTEVALUES table. The next step will check if the attribute is already there. If so, no problem. If not, there will be another log event with an error message.", e3);
                                Dna.logger.log(l);
                            }
                        }
                    }
                    // write the attribute ID as the value in the DATASHORTTEXT table
                    s4.setInt(1, entityId);
                    s4.setInt(2, statementIds.get(i));
                    s4.setInt(3, variableId);
                    s4.executeUpdate();
                    LogEvent e2 = new LogEvent(Logger.MESSAGE, "[SQL]  ├─ Variable " + variableId + " in Statement " + statementIds.get(i) + " was updated in the transaction.", "Variable " + variableId + " (short text) in Statement " + statementIds.get(i) + " was updated in the SQL transaction with Entity " + entityId + ".");
                    Dna.logger.log(e2);
                }
            }
            s10.setInt(1, coderIds.get(i));
            s10.setInt(2, statementIds.get(i));
            s10.executeUpdate();
        }
        conn.commit();
        LogEvent e2 = new LogEvent(Logger.MESSAGE, "[SQL]  └─ Completed SQL transaction to update " + statementIds.size() + " statements.", "Completed SQL transaction to update the variables in " + statementIds.size() + " statements. The contents have been written into the database.");
        Dna.logger.log(e2);
    } catch (SQLException e) {
        LogEvent e2 = new LogEvent(Logger.ERROR, "[SQL]  └─ Statements could not be updated in the database.", "When the statement recoder tried to update statement details in the database, something went wrong. Maybe another coder concurrently removed the statements you were working on, or maybe there was a connection issue. See exception below.", e);
        Dna.logger.log(e2);
    }
}
Also used : Entity(model.Entity) LogEvent(logger.LogEvent) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException) SQLException(java.sql.SQLException)

Example 80 with Entity

use of com.ibm.watson.assistant.v1.model.Entity in project dna by leifeld.

the class Sql method getEntities.

/**
 * Retrieve the full set of entities for a set of variable IDs. The result
 * is an array list with nested array lists of entities for each variable
 * ID.
 *
 * @param variableIds     The IDs of the variables for which all entities will
 *   be retrieved, supplied as an array list of integers.
 * @param withAttributes  Include attributes and indicator of whether the
 *   entity has been used in the database in each {@link model.Entity
 *   Entity}? Doing so takes much longer for large databases.
 * @return                An array list of array lists of {@link dna.Entity
 *   Entity} objects.
 *
 * @category entity
 */
public ArrayList<ArrayList<Entity>> getEntities(ArrayList<Integer> variableIds, boolean withAttributes) {
    ArrayList<ArrayList<Entity>> entities = new ArrayList<ArrayList<Entity>>();
    String sqlString = "SELECT ID, Value, Red, Green, Blue FROM ENTITIES WHERE VariableId = ?;";
    if (withAttributes) {
        sqlString = "SELECT ID, Value, Red, Green, Blue, ChildOf, (SELECT COUNT(ID) FROM DATASHORTTEXT WHERE DATASHORTTEXT.VariableId = ENTITIES.VariableId AND Entity = ENTITIES.ID) AS Count FROM ENTITIES WHERE VariableId = ?;";
    }
    try (Connection conn = ds.getConnection();
        PreparedStatement s1 = conn.prepareStatement(sqlString);
        PreparedStatement s2 = conn.prepareStatement("SELECT AttributeVariable, AttributeValue FROM ATTRIBUTEVALUES AS AVAL INNER JOIN ATTRIBUTEVARIABLES AS AVAR ON AVAL.AttributeVariableId = AVAR.ID WHERE EntityId = ?;")) {
        ResultSet r1, r2;
        HashMap<String, String> map;
        ArrayList<Entity> entitiesList;
        for (int i = 0; i < variableIds.size(); i++) {
            entitiesList = new ArrayList<Entity>();
            s1.setInt(1, variableIds.get(i));
            r1 = s1.executeQuery();
            while (r1.next()) {
                if (withAttributes) {
                    map = new HashMap<String, String>();
                    s2.setInt(1, r1.getInt("ID"));
                    r2 = s2.executeQuery();
                    while (r2.next()) {
                        map.put(r2.getString("AttributeVariable"), r2.getString("AttributeValue"));
                    }
                    entitiesList.add(new Entity(r1.getInt("ID"), variableIds.get(i), r1.getString("Value"), new Color(r1.getInt("Red"), r1.getInt("Green"), r1.getInt("Blue")), r1.getInt("ChildOf"), r1.getInt("Count") > 0, map));
                } else {
                    entitiesList.add(new Entity(r1.getInt("ID"), variableIds.get(i), r1.getString("Value"), new Color(r1.getInt("Red"), r1.getInt("Green"), r1.getInt("Blue"))));
                }
            }
            entities.add(entitiesList);
        }
        LogEvent e = new LogEvent(Logger.MESSAGE, "[SQL] Retrieved entities for " + variableIds.size() + " variables.", "Retrieved entities for " + variableIds.size() + " variables.");
        Dna.logger.log(e);
    } catch (SQLException e1) {
        LogEvent e = new LogEvent(Logger.WARNING, "[SQL] Entities could not be retrieved.", "Entities for " + variableIds.size() + " could not be retrieved. Check if the database is still there and/or if the connection has been interrupted, then try again.", e1);
        Dna.logger.log(e);
    }
    return entities;
}
Also used : Entity(model.Entity) LogEvent(logger.LogEvent) SQLException(java.sql.SQLException) Color(java.awt.Color) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)32 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)32 Test (org.testng.annotations.Test)32 HashMap (java.util.HashMap)27 Test (org.junit.Test)24 CreateEntityOptions (com.ibm.watson.assistant.v1.model.CreateEntityOptions)19 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)18 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)13 CreateValueOptions (com.ibm.watson.assistant.v1.model.CreateValueOptions)13 Entity (com.ibm.watson.assistant.v1.model.Entity)12 Value (com.ibm.watson.assistant.v1.model.Value)12 Synonym (com.ibm.watson.assistant.v1.model.Synonym)11 JsonObject (com.google.gson.JsonObject)10 CreateValue (com.ibm.watson.assistant.v1.model.CreateValue)10 Entity (model.Entity)10 CreateSynonymOptions (com.ibm.watson.assistant.v1.model.CreateSynonymOptions)7 DeleteEntityOptions (com.ibm.watson.assistant.v1.model.DeleteEntityOptions)7 DeleteSynonymOptions (com.ibm.watson.assistant.v1.model.DeleteSynonymOptions)7 DeleteValueOptions (com.ibm.watson.assistant.v1.model.DeleteValueOptions)7 Example (com.ibm.watson.assistant.v1.model.Example)7