Example usage for org.apache.commons.lang3 StringUtils left

List of usage examples for org.apache.commons.lang3 StringUtils left

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils left.

Prototype

public static String left(final String str, final int len) 

Source Link

Document

Gets the leftmost len characters of a String.

If len characters are not available, or the String is null , the String will be returned without an exception.

Usage

From source file:org.apache.nifi.admin.dao.impl.StandardActionDAO.java

@Override
public Action createAction(Action action) throws DataAccessException {
    if (action.getUserIdentity() == null) {
        throw new IllegalArgumentException("User cannot be null.");
    }/*from w  w  w  . java2  s .  c o  m*/

    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().toString());
        statement.setString(5, action.getOperation().toString());
        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);
    }
}

From source file:org.apache.nifi.admin.dao.impl.StandardActionDAO.java

private void createExtensionDetails(int actionId, ExtensionDetails extensionDetails)
        throws DataAccessException {
    PreparedStatement statement = null;
    try {//  w w w  . j a v  a 2s .c o m
        // 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);
    }
}

From source file:org.apache.nifi.admin.dao.impl.StandardActionDAO.java

private void createRemoteProcessGroupDetails(int actionId, RemoteProcessGroupDetails remoteProcessGroupDetails)
        throws DataAccessException {
    PreparedStatement statement = null;
    try {//from   w ww .j  a  va 2  s .c om
        // obtain a statement to insert to the processor action table
        statement = connection.prepareStatement(INSERT_REMOTE_PROCESS_GROUP_DETAILS);
        statement.setInt(1, actionId);
        statement.setString(2, StringUtils.left(remoteProcessGroupDetails.getUri(), 2500));

        // insert the action
        int updateCount = statement.executeUpdate();

        // ensure the operation completed successfully
        if (updateCount != 1) {
            throw new DataAccessException("Unable to insert remote prcoess group details.");
        }
    } catch (SQLException sqle) {
        throw new DataAccessException(sqle);
    } finally {
        RepositoryUtils.closeQuietly(statement);
    }
}

From source file:org.apache.nifi.admin.dao.impl.StandardActionDAO.java

private void createConnectDetails(int actionId, ConnectDetails connectionDetails) throws DataAccessException {
    PreparedStatement statement = null;
    try {/*from   w ww . j  a  va 2s .co  m*/
        // obtain a statement to insert to the processor action table
        statement = connection.prepareStatement(INSERT_CONNECT_DETAILS);
        statement.setInt(1, actionId);
        statement.setString(2, connectionDetails.getSourceId());
        statement.setString(3, StringUtils.left(connectionDetails.getSourceName(), 1000));
        statement.setString(4, StringUtils.left(connectionDetails.getSourceType().toString(), 1000));
        statement.setString(5, StringUtils.left(connectionDetails.getRelationship(), 1000));
        statement.setString(6, connectionDetails.getDestinationId());
        statement.setString(7, StringUtils.left(connectionDetails.getDestinationName(), 1000));
        statement.setString(8, StringUtils.left(connectionDetails.getDestinationType().toString(), 1000));

        // 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);
    }
}

From source file:org.apache.nifi.admin.dao.impl.StandardActionDAO.java

private void createMoveDetails(int actionId, MoveDetails moveDetails) throws DataAccessException {
    PreparedStatement statement = null;
    try {// w  w w  .  j  ava 2 s .  c o m
        // 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);
    }
}

From source file:org.apache.nifi.admin.dao.impl.StandardActionDAO.java

private void createConfigureDetails(int actionId, ConfigureDetails configurationDetails)
        throws DataAccessException {
    PreparedStatement statement = null;
    try {/*w  ww  . j a va  2 s  .c o  m*/
        // obtain a statement to insert to the processor action table
        statement = connection.prepareStatement(INSERT_CONFIGURE_DETAILS);
        statement.setInt(1, actionId);
        statement.setString(2, StringUtils.left(configurationDetails.getName(), 1000));
        statement.setString(3, StringUtils.left(configurationDetails.getValue(), 5000));
        statement.setString(4, StringUtils.left(configurationDetails.getPreviousValue(), 5000));

        // insert the action
        int updateCount = statement.executeUpdate();

        // ensure the operation completed successfully
        if (updateCount != 1) {
            throw new DataAccessException("Unable to insert configure details.");
        }
    } catch (SQLException sqle) {
        throw new DataAccessException(sqle);
    } finally {
        RepositoryUtils.closeQuietly(statement);
    }
}

From source file:org.apache.nifi.admin.dao.impl.StandardUserDAO.java

@Override
public NiFiUser createUser(NiFiUser user) throws DataAccessException {
    if (user.getIdentity() == null) {
        throw new IllegalArgumentException("User identity must be specified.");
    }/*ww  w . j  a  va2  s  . com*/

    // ensure the user identity is not too lengthy
    if (user.getIdentity().length() > 4096) {
        throw new IllegalArgumentException("User identity must be less than 4096 characters.");
    }

    PreparedStatement statement = null;
    ResultSet rs = null;
    try {
        final String id = UUID.nameUUIDFromBytes(user.getIdentity().getBytes(StandardCharsets.UTF_8))
                .toString();

        // create a statement
        statement = connection.prepareStatement(INSERT_USER, Statement.RETURN_GENERATED_KEYS);
        statement.setString(1, id);
        statement.setString(2, StringUtils.left(user.getIdentity(), 4096));
        statement.setString(3, StringUtils.left(user.getUserName(), 4096));
        statement.setString(4, StringUtils.left(user.getUserGroup(), 100));
        if (user.getLastVerified() != null) {
            statement.setTimestamp(5, new java.sql.Timestamp(user.getLastVerified().getTime()));
        } else {
            statement.setTimestamp(5, null);
        }
        statement.setString(6, StringUtils.left(user.getJustification(), 500));
        statement.setString(7, user.getStatus().toString());

        // insert the user
        int updateCount = statement.executeUpdate();
        if (updateCount == 1) {
            user.setId(id);
        } else {
            throw new DataAccessException("Unable to insert user.");
        }

        return user;
    } catch (SQLException sqle) {
        throw new DataAccessException(sqle);
    } catch (DataAccessException dae) {
        throw dae;
    } finally {
        RepositoryUtils.closeQuietly(rs);
        RepositoryUtils.closeQuietly(statement);
    }
}

From source file:org.apache.nifi.admin.dao.impl.StandardUserDAO.java

@Override
public void updateUser(NiFiUser user) throws DataAccessException {
    PreparedStatement statement = null;
    try {//from  ww w.jav a2 s .  c  om
        // create a statement
        statement = connection.prepareStatement(UPDATE_USER);
        statement.setString(1, StringUtils.left(user.getIdentity(), 4096));
        statement.setString(2, StringUtils.left(user.getUserName(), 4096));
        statement.setString(3, StringUtils.left(user.getUserGroup(), 100));
        statement.setString(6, StringUtils.left(user.getJustification(), 500));
        statement.setString(7, user.getStatus().toString());
        statement.setString(8, user.getId());

        // set the last accessed time accordingly
        if (user.getLastAccessed() == null) {
            statement.setNull(4, Types.TIMESTAMP);
        } else {
            statement.setTimestamp(4, new java.sql.Timestamp(user.getLastAccessed().getTime()));
        }

        // set the last verified time accordingly
        if (user.getLastVerified() == null) {
            statement.setNull(5, Types.TIMESTAMP);
        } else {
            statement.setTimestamp(5, new java.sql.Timestamp(user.getLastVerified().getTime()));
        }

        // perform the update
        int updateCount = statement.executeUpdate();
        if (updateCount != 1) {
            throw new DataAccessException("Unable to update user.");
        }
    } catch (SQLException sqle) {
        throw new DataAccessException(sqle);
    } catch (DataAccessException dae) {
        throw dae;
    } finally {
        RepositoryUtils.closeQuietly(statement);
    }
}

From source file:org.broadleafcommerce.common.util.TableCreator.java

public TableCreator addRow(Object[] data) {
    if (data.length != cols.length) {
        throw new IllegalArgumentException("Wrong number of data elements. Needed [" + cols.length + "] "
                + "but received [" + data.length + "]");
    }/*from  www .  ja va  2s .  c  o  m*/

    sb.append('|');

    for (int i = 0; i < data.length; i++) {
        String trimmed = StringUtils.left(String.valueOf(data[i]), cols[i].width);
        sb.append(' ').append(StringUtils.rightPad(trimmed, cols[i].width)).append(" |");
    }

    sb.append("\r\n");
    return this;
}

From source file:org.broadleafcommerce.common.util.TableCreator.java

public TableCreator addRow(String rowHeader, Object rowData) {
    String trimmed = StringUtils.left(rowHeader, globalRowHeaderWidth);
    sb.append("| ").append(StringUtils.rightPad(trimmed, globalRowHeaderWidth))
            .append(StringUtils.rightPad(String.valueOf(rowData), rowWidth - globalRowHeaderWidth - 3))
            .append("|\r\n");
    return this;
}