Search in sources :

Example 26 with FeatureAccessException

use of org.ff4j.exception.FeatureAccessException in project ff4j by ff4j.

the class PropertyStoreSpringJdbc method isTableExist.

public boolean isTableExist(String tableName) {
    ResultSet rs = null;
    try {
        DatabaseMetaData dbmd = getJdbcTemplate().getDataSource().getConnection().getMetaData();
        rs = dbmd.getTables(null, null, tableName, new String[] { "TABLE" });
        return rs.next();
    } catch (SQLException sqlEX) {
        throw new FeatureAccessException("Cannot check table existence", sqlEX);
    } finally {
        JdbcUtils.closeResultSet(rs);
    }
}
Also used : FeatureAccessException(org.ff4j.exception.FeatureAccessException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 27 with FeatureAccessException

use of org.ff4j.exception.FeatureAccessException in project ff4j by ff4j.

the class JdbcFeatureStore method update.

/**
 * Utility method to perform UPDATE and DELETE operations.
 *
 * @param query
 *            target query
 * @param params
 *            sql query params
 */
public void update(String query, Object... params) {
    Connection sqlConnection = null;
    PreparedStatement ps = null;
    try {
        sqlConnection = dataSource.getConnection();
        ps = buildStatement(sqlConnection, query, params);
        ps.executeUpdate();
        if (!sqlConnection.getAutoCommit()) {
            sqlConnection.commit();
        }
    } catch (SQLException sqlEX) {
        throw new FeatureAccessException(CANNOT_UPDATE_FEATURES_DATABASE_SQL_ERROR, sqlEX);
    } finally {
        closeStatement(ps);
        closeConnection(sqlConnection);
    }
}
Also used : FeatureAccessException(org.ff4j.exception.FeatureAccessException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcUtils.closeConnection(org.ff4j.utils.JdbcUtils.closeConnection) PreparedStatement(java.sql.PreparedStatement)

Example 28 with FeatureAccessException

use of org.ff4j.exception.FeatureAccessException in project ff4j by ff4j.

the class JdbcFeatureStore method create.

/**
 * {@inheritDoc}
 */
@Override
public void create(Feature fp) {
    assertFeatureNotNull(fp);
    Connection sqlConn = null;
    PreparedStatement ps = null;
    Boolean previousAutoCommit = null;
    try {
        // Create connection
        sqlConn = getDataSource().getConnection();
        if (exist(fp.getUid())) {
            throw new FeatureAlreadyExistException(fp.getUid());
        }
        // Begin TX
        previousAutoCommit = sqlConn.getAutoCommit();
        sqlConn.setAutoCommit(false);
        // Create feature
        ps = sqlConn.prepareStatement(getQueryBuilder().createFeature());
        ps.setString(1, fp.getUid());
        ps.setInt(2, fp.isEnable() ? 1 : 0);
        ps.setString(3, fp.getDescription());
        String strategyColumn = null;
        String expressionColumn = null;
        if (fp.getFlippingStrategy() != null) {
            strategyColumn = fp.getFlippingStrategy().getClass().getName();
            expressionColumn = MappingUtil.fromMap(fp.getFlippingStrategy().getInitParams());
        }
        ps.setString(4, strategyColumn);
        ps.setString(5, expressionColumn);
        ps.setString(6, fp.getGroup());
        ps.executeUpdate();
        closeStatement(ps);
        ps = null;
        // Create roles
        for (String role : fp.getPermissions()) {
            ps = sqlConn.prepareStatement(getQueryBuilder().addRoleToFeature());
            ps.setString(1, fp.getUid());
            ps.setString(2, role);
            ps.executeUpdate();
            closeStatement(ps);
            ps = null;
        }
        // Create customproperties
        if (fp.getCustomProperties() != null && !fp.getCustomProperties().isEmpty()) {
            for (Property<?> pp : fp.getCustomProperties().values()) {
                ps = createCustomProperty(sqlConn, fp.getUid(), pp);
                closeStatement(ps);
                ps = null;
            }
        }
        // Commit
        sqlConn.commit();
    } catch (SQLException sqlEX) {
        rollback(sqlConn);
        throw new FeatureAccessException(CANNOT_UPDATE_FEATURES_DATABASE_SQL_ERROR, sqlEX);
    } finally {
        closeStatement(ps);
        closeConnection(sqlConn, previousAutoCommit);
    }
}
Also used : FeatureAlreadyExistException(org.ff4j.exception.FeatureAlreadyExistException) FeatureAccessException(org.ff4j.exception.FeatureAccessException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcUtils.closeConnection(org.ff4j.utils.JdbcUtils.closeConnection) PreparedStatement(java.sql.PreparedStatement)

Example 29 with FeatureAccessException

use of org.ff4j.exception.FeatureAccessException in project ff4j by ff4j.

the class JdbcFeatureStore method read.

/**
 * {@inheritDoc}
 */
@Override
public Feature read(String uid) {
    assertFeatureExist(uid);
    Connection sqlConn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        sqlConn = getDataSource().getConnection();
        ps = sqlConn.prepareStatement(getQueryBuilder().getFeature());
        ps.setString(1, uid);
        rs = ps.executeQuery();
        // Existence is tested before
        rs.next();
        Feature f = JDBC_FEATURE_MAPPER.mapFeature(rs);
        closeResultSet(rs);
        rs = null;
        closeStatement(ps);
        ps = null;
        // Enrich to get roles 2nd request
        ps = sqlConn.prepareStatement(getQueryBuilder().getRoles());
        ps.setString(1, uid);
        rs = ps.executeQuery();
        while (rs.next()) {
            f.getPermissions().add(rs.getString("ROLE_NAME"));
        }
        closeResultSet(rs);
        rs = null;
        closeStatement(ps);
        ps = null;
        // Enrich with properties 3d request to get custom properties by uid
        ps = sqlConn.prepareStatement(getQueryBuilder().getFeatureProperties());
        ps.setString(1, uid);
        rs = ps.executeQuery();
        while (rs.next()) {
            f.addProperty(JDBC_PROPERTY_MAPPER.map(rs));
        }
        return f;
    } catch (SQLException sqlEX) {
        throw new FeatureAccessException(CANNOT_CHECK_FEATURE_EXISTENCE_ERROR_RELATED_TO_DATABASE, sqlEX);
    } finally {
        closeResultSet(rs);
        closeStatement(ps);
        closeConnection(sqlConn);
    }
}
Also used : FeatureAccessException(org.ff4j.exception.FeatureAccessException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcUtils.closeConnection(org.ff4j.utils.JdbcUtils.closeConnection) ResultSet(java.sql.ResultSet) JdbcUtils.closeResultSet(org.ff4j.utils.JdbcUtils.closeResultSet) PreparedStatement(java.sql.PreparedStatement) Feature(org.ff4j.core.Feature)

Example 30 with FeatureAccessException

use of org.ff4j.exception.FeatureAccessException in project ff4j by ff4j.

the class JdbcFeatureStore method readGroup.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, Feature> readGroup(String groupName) {
    assertGroupExist(groupName);
    LinkedHashMap<String, Feature> mapFP = new LinkedHashMap<String, Feature>();
    Connection sqlConn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        // Returns features
        sqlConn = dataSource.getConnection();
        ps = sqlConn.prepareStatement(getQueryBuilder().getFeatureOfGroup());
        ps.setString(1, groupName);
        rs = ps.executeQuery();
        while (rs.next()) {
            Feature f = JDBC_FEATURE_MAPPER.mapFeature(rs);
            mapFP.put(f.getUid(), f);
        }
        closeResultSet(rs);
        rs = null;
        closeStatement(ps);
        ps = null;
        // Returns Roles
        ps = sqlConn.prepareStatement(getQueryBuilder().getAllRoles());
        rs = ps.executeQuery();
        while (rs.next()) {
            String uid = rs.getString(COL_ROLE_FEATID);
            // only feature in the group must be processed
            if (mapFP.containsKey(uid)) {
                mapFP.get(uid).getPermissions().add(rs.getString(COL_ROLE_ROLENAME));
            }
        }
        closeResultSet(rs);
        rs = null;
        closeStatement(ps);
        ps = null;
        // Read custom properties for each feature
        for (Feature f : mapFP.values()) {
            ps = sqlConn.prepareStatement(getQueryBuilder().getFeatureProperties());
            ps.setString(1, f.getUid());
            rs = ps.executeQuery();
            while (rs.next()) {
                f.addProperty(JDBC_PROPERTY_MAPPER.map(rs));
            }
            closeResultSet(rs);
            rs = null;
            closeStatement(ps);
            ps = null;
        }
        return mapFP;
    } catch (SQLException sqlEX) {
        throw new FeatureAccessException(CANNOT_CHECK_FEATURE_EXISTENCE_ERROR_RELATED_TO_DATABASE, sqlEX);
    } finally {
        closeResultSet(rs);
        closeStatement(ps);
        closeConnection(sqlConn);
    }
}
Also used : FeatureAccessException(org.ff4j.exception.FeatureAccessException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcUtils.closeConnection(org.ff4j.utils.JdbcUtils.closeConnection) ResultSet(java.sql.ResultSet) JdbcUtils.closeResultSet(org.ff4j.utils.JdbcUtils.closeResultSet) PreparedStatement(java.sql.PreparedStatement) Feature(org.ff4j.core.Feature) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

FeatureAccessException (org.ff4j.exception.FeatureAccessException)50 SQLException (java.sql.SQLException)18 Connection (java.sql.Connection)17 PreparedStatement (java.sql.PreparedStatement)16 Response (javax.ws.rs.core.Response)16 JdbcUtils.closeConnection (org.ff4j.utils.JdbcUtils.closeConnection)15 Feature (org.ff4j.core.Feature)11 ResultSet (java.sql.ResultSet)10 ClientResponse (com.sun.jersey.api.client.ClientResponse)9 JdbcUtils.closeResultSet (org.ff4j.utils.JdbcUtils.closeResultSet)8 HashMap (java.util.HashMap)7 FeatureNotFoundException (org.ff4j.exception.FeatureNotFoundException)7 GroupNotFoundException (org.ff4j.exception.GroupNotFoundException)5 WebResource (com.sun.jersey.api.client.WebResource)4 HashSet (java.util.HashSet)4 FeatureJsonParser.parseFeature (org.ff4j.utils.json.FeatureJsonParser.parseFeature)4 IOException (java.io.IOException)3 Connection (org.apache.hadoop.hbase.client.Connection)3 Table (org.apache.hadoop.hbase.client.Table)3 HBaseConnection (org.ff4j.hbase.HBaseConnection)3