Search in sources :

Example 1 with DataAccessException

use of org.apache.nifi.admin.dao.DataAccessException in project nifi by apache.

the class StandardActionDAO method createPurgeDetails.

private void createPurgeDetails(int actionId, PurgeDetails purgeDetails) throws DataAccessException {
    PreparedStatement statement = null;
    try {
        // obtain a statement to insert to the processor action table
        statement = connection.prepareStatement(INSERT_PURGE_DETAILS);
        statement.setInt(1, actionId);
        statement.setTimestamp(2, new java.sql.Timestamp(purgeDetails.getEndDate().getTime()));
        // insert the action
        int updateCount = statement.executeUpdate();
        // ensure the operation completed successfully
        if (updateCount != 1) {
            throw new DataAccessException("Unable to insert connection details.");
        }
    } catch (SQLException sqle) {
        throw new DataAccessException(sqle);
    } finally {
        RepositoryUtils.closeQuietly(statement);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.apache.nifi.admin.dao.DataAccessException)

Example 2 with DataAccessException

use of org.apache.nifi.admin.dao.DataAccessException in project nifi by apache.

the class StandardActionDAO method findActions.

@Override
public History findActions(HistoryQuery historyQuery) throws DataAccessException {
    // get the sort column
    String sortColumn = "ACTION_TIMESTAMP";
    if (StringUtils.isNotBlank(historyQuery.getSortColumn())) {
        String rawColumnName = historyQuery.getSortColumn();
        if (!columnMap.containsKey(rawColumnName)) {
            throw new IllegalArgumentException(String.format("Unrecognized column name '%s'.", rawColumnName));
        }
        sortColumn = columnMap.get(rawColumnName);
    }
    // get the sort order
    String sortOrder = "desc";
    if (StringUtils.isNotBlank(historyQuery.getSortOrder())) {
        sortOrder = historyQuery.getSortOrder();
    }
    History actionResult = new History();
    Collection<Action> actions = new ArrayList<>();
    PreparedStatement statement = null;
    ResultSet rs = null;
    try {
        List<String> where = new ArrayList<>();
        // append the start time
        if (historyQuery.getStartDate() != null) {
            where.add("ACTION_TIMESTAMP >= ?");
        }
        // append the end time
        if (historyQuery.getEndDate() != null) {
            where.add("ACTION_TIMESTAMP <= ?");
        }
        // append the user id as necessary
        if (historyQuery.getUserIdentity() != null) {
            where.add("UPPER(IDENTITY) LIKE ?");
        }
        // append the source id as necessary
        if (historyQuery.getSourceId() != null) {
            where.add("SOURCE_ID = ?");
        }
        String sql = SELECT_ACTION_COUNT;
        if (!where.isEmpty()) {
            sql += " WHERE " + StringUtils.join(where, " AND ");
        }
        // get the total number of actions
        statement = connection.prepareStatement(sql);
        int paramIndex = 1;
        // set the start date as necessary
        if (historyQuery.getStartDate() != null) {
            statement.setTimestamp(paramIndex++, new java.sql.Timestamp(historyQuery.getStartDate().getTime()));
        }
        // set the end date as necessary
        if (historyQuery.getEndDate() != null) {
            statement.setTimestamp(paramIndex++, new java.sql.Timestamp(historyQuery.getEndDate().getTime()));
        }
        // set the user id as necessary
        if (historyQuery.getUserIdentity() != null) {
            statement.setString(paramIndex++, "%" + historyQuery.getUserIdentity().toUpperCase() + "%");
        }
        // set the source id as necessary
        if (historyQuery.getSourceId() != null) {
            statement.setString(paramIndex, historyQuery.getSourceId());
        }
        // execute the statement
        rs = statement.executeQuery();
        // ensure there are results
        if (rs.next()) {
            actionResult.setTotal(rs.getInt("ACTION_COUNT"));
        } else {
            throw new DataAccessException("Unable to determine total action count.");
        }
        sql = SELECT_ACTIONS;
        if (!where.isEmpty()) {
            sql += " WHERE " + StringUtils.join(where, " AND ");
        }
        // append the sort criteria
        sql += (" ORDER BY " + sortColumn + " " + sortOrder);
        // append the offset and limit
        sql += " LIMIT ? OFFSET ?";
        // close the previous statement
        statement.close();
        // create the statement
        statement = connection.prepareStatement(sql);
        paramIndex = 1;
        // set the start date as necessary
        if (historyQuery.getStartDate() != null) {
            statement.setTimestamp(paramIndex++, new java.sql.Timestamp(historyQuery.getStartDate().getTime()));
        }
        // set the end date as necessary
        if (historyQuery.getEndDate() != null) {
            statement.setTimestamp(paramIndex++, new java.sql.Timestamp(historyQuery.getEndDate().getTime()));
        }
        // set the user id as necessary
        if (historyQuery.getUserIdentity() != null) {
            statement.setString(paramIndex++, "%" + historyQuery.getUserIdentity().toUpperCase() + "%");
        }
        // set the source id as necessary
        if (historyQuery.getSourceId() != null) {
            statement.setString(paramIndex++, historyQuery.getSourceId());
        }
        // set the limit
        statement.setInt(paramIndex++, historyQuery.getCount());
        // set the offset according to the currented page calculated above
        statement.setInt(paramIndex, historyQuery.getOffset());
        // execute the query
        rs = statement.executeQuery();
        // create each corresponding action
        while (rs.next()) {
            final Integer actionId = rs.getInt("ID");
            final Operation operation = Operation.valueOf(rs.getString("OPERATION"));
            final Component component = Component.valueOf(rs.getString("SOURCE_TYPE"));
            FlowChangeAction action = new FlowChangeAction();
            action.setId(actionId);
            action.setUserIdentity(rs.getString("IDENTITY"));
            action.setOperation(Operation.valueOf(rs.getString("OPERATION")));
            action.setTimestamp(new Date(rs.getTimestamp("ACTION_TIMESTAMP").getTime()));
            action.setSourceId(rs.getString("SOURCE_ID"));
            action.setSourceName(rs.getString("SOURCE_NAME"));
            action.setSourceType(Component.valueOf(rs.getString("SOURCE_TYPE")));
            // get the component details if appropriate
            ComponentDetails componentDetails = null;
            if (Component.Processor.equals(component) || Component.ControllerService.equals(component) || Component.ReportingTask.equals(component)) {
                componentDetails = getExtensionDetails(actionId);
            } else if (Component.RemoteProcessGroup.equals(component)) {
                componentDetails = getRemoteProcessGroupDetails(actionId);
            }
            if (componentDetails != null) {
                action.setComponentDetails(componentDetails);
            }
            // get the action details if appropriate
            ActionDetails actionDetails = null;
            if (Operation.Move.equals(operation)) {
                actionDetails = getMoveDetails(actionId);
            } else if (Operation.Configure.equals(operation)) {
                actionDetails = getConfigureDetails(actionId);
            } else if (Operation.Connect.equals(operation) || Operation.Disconnect.equals(operation)) {
                actionDetails = getConnectDetails(actionId);
            } else if (Operation.Purge.equals(operation)) {
                actionDetails = getPurgeDetails(actionId);
            }
            // set the action details
            if (actionDetails != null) {
                action.setActionDetails(actionDetails);
            }
            // add the action
            actions.add(action);
        }
        // populate the action result
        actionResult.setActions(actions);
    } catch (SQLException sqle) {
        throw new DataAccessException(sqle);
    } finally {
        RepositoryUtils.closeQuietly(rs);
        RepositoryUtils.closeQuietly(statement);
    }
    return actionResult;
}
Also used : FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Action(org.apache.nifi.action.Action) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Operation(org.apache.nifi.action.Operation) History(org.apache.nifi.history.History) Date(java.util.Date) ResultSet(java.sql.ResultSet) ActionDetails(org.apache.nifi.action.details.ActionDetails) Component(org.apache.nifi.action.Component) ComponentDetails(org.apache.nifi.action.component.details.ComponentDetails) DataAccessException(org.apache.nifi.admin.dao.DataAccessException) FlowChangeAction(org.apache.nifi.action.FlowChangeAction)

Example 3 with DataAccessException

use of org.apache.nifi.admin.dao.DataAccessException in project nifi by apache.

the class StandardActionDAO method getPreviousValuesForProperty.

private List<PreviousValue> getPreviousValuesForProperty(final String componentId, final String property) {
    List<PreviousValue> previousValues = new ArrayList<>();
    PreparedStatement statement = null;
    ResultSet rs = null;
    try {
        // create the statement
        statement = connection.prepareStatement(SELECT_PREVIOUS_VALUES);
        statement.setString(1, componentId);
        statement.setString(2, property);
        // execute the query
        rs = statement.executeQuery();
        // ensure results
        while (rs.next()) {
            // get the previous value
            final PreviousValue previousValue = new PreviousValue();
            previousValue.setPreviousValue(rs.getString("VALUE"));
            previousValue.setTimestamp(new Date(rs.getTimestamp("ACTION_TIMESTAMP").getTime()));
            previousValue.setUserIdentity(rs.getString("IDENTITY"));
            previousValues.add(previousValue);
        }
    } catch (SQLException sqle) {
        throw new DataAccessException(sqle);
    } finally {
        RepositoryUtils.closeQuietly(rs);
        RepositoryUtils.closeQuietly(statement);
    }
    return previousValues;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PreviousValue(org.apache.nifi.history.PreviousValue) Date(java.util.Date) DataAccessException(org.apache.nifi.admin.dao.DataAccessException)

Example 4 with DataAccessException

use of org.apache.nifi.admin.dao.DataAccessException in project nifi by apache.

the class StandardActionDAO method createExtensionDetails.

private void createExtensionDetails(int actionId, ExtensionDetails extensionDetails) throws DataAccessException {
    PreparedStatement statement = null;
    try {
        // obtain a statement to insert to the extension action table
        statement = connection.prepareStatement(INSERT_EXTENSION_DETAILS);
        statement.setInt(1, actionId);
        statement.setString(2, StringUtils.left(extensionDetails.getType(), 1000));
        // insert the action
        int updateCount = statement.executeUpdate();
        // ensure the operation completed successfully
        if (updateCount != 1) {
            throw new DataAccessException("Unable to insert extension details.");
        }
    } catch (SQLException sqle) {
        throw new DataAccessException(sqle);
    } finally {
        RepositoryUtils.closeQuietly(statement);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.apache.nifi.admin.dao.DataAccessException)

Example 5 with DataAccessException

use of org.apache.nifi.admin.dao.DataAccessException in project nifi by apache.

the class StandardActionDAO method createMoveDetails.

private void createMoveDetails(int actionId, MoveDetails moveDetails) throws DataAccessException {
    PreparedStatement statement = null;
    try {
        // obtain a statement to insert to the processor action table
        statement = connection.prepareStatement(INSERT_MOVE_DETAILS);
        statement.setInt(1, actionId);
        statement.setString(2, moveDetails.getGroupId());
        statement.setString(3, StringUtils.left(moveDetails.getGroup(), 1000));
        statement.setString(4, moveDetails.getPreviousGroupId());
        statement.setString(5, StringUtils.left(moveDetails.getPreviousGroup(), 1000));
        // insert the action
        int updateCount = statement.executeUpdate();
        // ensure the operation completed successfully
        if (updateCount != 1) {
            throw new DataAccessException("Unable to insert move details.");
        }
    } catch (SQLException sqle) {
        throw new DataAccessException(sqle);
    } finally {
        RepositoryUtils.closeQuietly(statement);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.apache.nifi.admin.dao.DataAccessException)

Aggregations

DataAccessException (org.apache.nifi.admin.dao.DataAccessException)30 PreparedStatement (java.sql.PreparedStatement)22 SQLException (java.sql.SQLException)22 ResultSet (java.sql.ResultSet)14 AdministrationException (org.apache.nifi.admin.service.AdministrationException)8 Transaction (org.apache.nifi.admin.service.transaction.Transaction)8 TransactionException (org.apache.nifi.admin.service.transaction.TransactionException)8 Key (org.apache.nifi.key.Key)5 Date (java.util.Date)4 ArrayList (java.util.ArrayList)3 Component (org.apache.nifi.action.Component)3 FlowChangeAction (org.apache.nifi.action.FlowChangeAction)3 ComponentDetails (org.apache.nifi.action.component.details.ComponentDetails)3 ActionDetails (org.apache.nifi.action.details.ActionDetails)3 List (java.util.List)2 Action (org.apache.nifi.action.Action)2 Operation (org.apache.nifi.action.Operation)2 FlowChangeExtensionDetails (org.apache.nifi.action.component.details.FlowChangeExtensionDetails)2 FlowChangeRemoteProcessGroupDetails (org.apache.nifi.action.component.details.FlowChangeRemoteProcessGroupDetails)2 FlowChangeConfigureDetails (org.apache.nifi.action.details.FlowChangeConfigureDetails)2