Search in sources :

Example 1 with ComponentDetails

use of org.apache.nifi.action.component.details.ComponentDetails 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 2 with ComponentDetails

use of org.apache.nifi.action.component.details.ComponentDetails in project nifi by apache.

the class StandardActionDAO method createAction.

@Override
public Action createAction(Action action) throws DataAccessException {
    if (action.getUserIdentity() == null) {
        throw new IllegalArgumentException("User cannot be null.");
    }
    if (action.getTimestamp() == null) {
        throw new IllegalArgumentException("Action timestamp cannot be null.");
    }
    PreparedStatement statement = null;
    ResultSet rs = null;
    try {
        // obtain a statement to insert to the action table
        statement = connection.prepareStatement(INSERT_ACTION, Statement.RETURN_GENERATED_KEYS);
        statement.setString(1, StringUtils.left(action.getUserIdentity(), 4096));
        statement.setString(2, action.getSourceId());
        statement.setString(3, StringUtils.left(action.getSourceName(), 1000));
        statement.setString(4, action.getSourceType().name());
        statement.setString(5, action.getOperation().name());
        statement.setTimestamp(6, new java.sql.Timestamp(action.getTimestamp().getTime()));
        // insert the action
        int updateCount = statement.executeUpdate();
        final FlowChangeAction createdAction = new FlowChangeAction();
        createdAction.setUserIdentity(action.getUserIdentity());
        createdAction.setSourceId(action.getSourceId());
        createdAction.setSourceName(action.getSourceName());
        createdAction.setSourceType(action.getSourceType());
        createdAction.setOperation(action.getOperation());
        createdAction.setTimestamp(action.getTimestamp());
        createdAction.setActionDetails(action.getActionDetails());
        createdAction.setComponentDetails(action.getComponentDetails());
        // get the action id
        rs = statement.getGeneratedKeys();
        if (updateCount == 1 && rs.next()) {
            createdAction.setId(rs.getInt(1));
        } else {
            throw new DataAccessException("Unable to insert action.");
        }
        // close the previous statement
        statement.close();
        // determine the type of component
        ComponentDetails componentDetails = createdAction.getComponentDetails();
        if (componentDetails instanceof FlowChangeExtensionDetails) {
            createExtensionDetails(createdAction.getId(), (ExtensionDetails) componentDetails);
        } else if (componentDetails instanceof FlowChangeRemoteProcessGroupDetails) {
            createRemoteProcessGroupDetails(createdAction.getId(), (RemoteProcessGroupDetails) componentDetails);
        }
        // determine the type of action
        ActionDetails details = createdAction.getActionDetails();
        if (details instanceof FlowChangeConnectDetails) {
            createConnectDetails(createdAction.getId(), (ConnectDetails) details);
        } else if (details instanceof FlowChangeMoveDetails) {
            createMoveDetails(createdAction.getId(), (MoveDetails) details);
        } else if (details instanceof FlowChangeConfigureDetails) {
            createConfigureDetails(createdAction.getId(), (ConfigureDetails) details);
        } else if (details instanceof FlowChangePurgeDetails) {
            createPurgeDetails(createdAction.getId(), (PurgeDetails) details);
        }
        return createdAction;
    } catch (SQLException sqle) {
        throw new DataAccessException(sqle);
    } finally {
        RepositoryUtils.closeQuietly(rs);
        RepositoryUtils.closeQuietly(statement);
    }
}
Also used : FlowChangeMoveDetails(org.apache.nifi.action.details.FlowChangeMoveDetails) MoveDetails(org.apache.nifi.action.details.MoveDetails) FlowChangePurgeDetails(org.apache.nifi.action.details.FlowChangePurgeDetails) SQLException(java.sql.SQLException) FlowChangeMoveDetails(org.apache.nifi.action.details.FlowChangeMoveDetails) PreparedStatement(java.sql.PreparedStatement) FlowChangeRemoteProcessGroupDetails(org.apache.nifi.action.component.details.FlowChangeRemoteProcessGroupDetails) FlowChangeRemoteProcessGroupDetails(org.apache.nifi.action.component.details.FlowChangeRemoteProcessGroupDetails) RemoteProcessGroupDetails(org.apache.nifi.action.component.details.RemoteProcessGroupDetails) FlowChangeConnectDetails(org.apache.nifi.action.details.FlowChangeConnectDetails) FlowChangeConfigureDetails(org.apache.nifi.action.details.FlowChangeConfigureDetails) FlowChangeExtensionDetails(org.apache.nifi.action.component.details.FlowChangeExtensionDetails) ResultSet(java.sql.ResultSet) FlowChangePurgeDetails(org.apache.nifi.action.details.FlowChangePurgeDetails) PurgeDetails(org.apache.nifi.action.details.PurgeDetails) ActionDetails(org.apache.nifi.action.details.ActionDetails) ComponentDetails(org.apache.nifi.action.component.details.ComponentDetails) DataAccessException(org.apache.nifi.admin.dao.DataAccessException) FlowChangeAction(org.apache.nifi.action.FlowChangeAction)

Example 3 with ComponentDetails

use of org.apache.nifi.action.component.details.ComponentDetails in project nifi by apache.

the class StandardActionDAO method getAction.

@Override
public Action getAction(Integer actionId) throws DataAccessException {
    FlowChangeAction action = null;
    PreparedStatement statement = null;
    ResultSet rs = null;
    try {
        // create the statement
        statement = connection.prepareStatement(SELECT_ACTION_BY_ID);
        statement.setInt(1, actionId);
        // execute the query
        rs = statement.executeQuery();
        // ensure results
        if (rs.next()) {
            Operation operation = Operation.valueOf(rs.getString("OPERATION"));
            Component component = Component.valueOf(rs.getString("SOURCE_TYPE"));
            // populate the action
            action = new FlowChangeAction();
            action.setId(rs.getInt("ID"));
            action.setUserIdentity(rs.getString("IDENTITY"));
            action.setOperation(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);
            // 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);
            }
        }
    } catch (SQLException sqle) {
        throw new DataAccessException(sqle);
    } finally {
        RepositoryUtils.closeQuietly(rs);
        RepositoryUtils.closeQuietly(statement);
    }
    return action;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ActionDetails(org.apache.nifi.action.details.ActionDetails) Operation(org.apache.nifi.action.Operation) Component(org.apache.nifi.action.Component) ComponentDetails(org.apache.nifi.action.component.details.ComponentDetails) Date(java.util.Date) DataAccessException(org.apache.nifi.admin.dao.DataAccessException) FlowChangeAction(org.apache.nifi.action.FlowChangeAction)

Aggregations

PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)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 DataAccessException (org.apache.nifi.admin.dao.DataAccessException)3 Date (java.util.Date)2 Component (org.apache.nifi.action.Component)2 Operation (org.apache.nifi.action.Operation)2 ArrayList (java.util.ArrayList)1 Action (org.apache.nifi.action.Action)1 FlowChangeExtensionDetails (org.apache.nifi.action.component.details.FlowChangeExtensionDetails)1 FlowChangeRemoteProcessGroupDetails (org.apache.nifi.action.component.details.FlowChangeRemoteProcessGroupDetails)1 RemoteProcessGroupDetails (org.apache.nifi.action.component.details.RemoteProcessGroupDetails)1 FlowChangeConfigureDetails (org.apache.nifi.action.details.FlowChangeConfigureDetails)1 FlowChangeConnectDetails (org.apache.nifi.action.details.FlowChangeConnectDetails)1 FlowChangeMoveDetails (org.apache.nifi.action.details.FlowChangeMoveDetails)1 FlowChangePurgeDetails (org.apache.nifi.action.details.FlowChangePurgeDetails)1 MoveDetails (org.apache.nifi.action.details.MoveDetails)1