Search in sources :

Example 6 with FeatureAccessException

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

the class JdbcFeatureStore method exist.

/**
 * {@inheritDoc}
 */
@Override
public boolean exist(String uid) {
    assertHasLength(uid);
    Connection sqlConn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        sqlConn = getDataSource().getConnection();
        ps = JdbcUtils.buildStatement(sqlConn, getQueryBuilder().existFeature(), uid);
        rs = ps.executeQuery();
        rs.next();
        return 1 == rs.getInt(1);
    } 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)

Example 7 with FeatureAccessException

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

the class JdbcFeatureStore method readAll.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, Feature> readAll() {
    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().getAllFeatures());
        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);
            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)

Example 8 with FeatureAccessException

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

the class JdbcFeatureStore method delete.

/**
 * {@inheritDoc}
 */
@Override
public void delete(String uid) {
    assertFeatureExist(uid);
    Connection sqlConn = null;
    PreparedStatement ps = null;
    Boolean previousAutoCommit = null;
    try {
        // Create connection
        sqlConn = getDataSource().getConnection();
        previousAutoCommit = sqlConn.getAutoCommit();
        sqlConn.setAutoCommit(false);
        Feature fp = read(uid);
        // Delete Properties
        if (fp.getCustomProperties() != null) {
            for (String property : fp.getCustomProperties().keySet()) {
                ps = sqlConn.prepareStatement(getQueryBuilder().deleteFeatureProperty());
                ps.setString(1, property);
                ps.setString(2, fp.getUid());
                ps.executeUpdate();
                closeStatement(ps);
                ps = null;
            }
        }
        // Delete Roles
        if (fp.getPermissions() != null) {
            for (String role : fp.getPermissions()) {
                ps = sqlConn.prepareStatement(getQueryBuilder().deleteFeatureRole());
                ps.setString(1, fp.getUid());
                ps.setString(2, role);
                ps.executeUpdate();
                closeStatement(ps);
                ps = null;
            }
        }
        // Delete Feature
        ps = sqlConn.prepareStatement(getQueryBuilder().deleteFeature());
        ps.setString(1, fp.getUid());
        ps.executeUpdate();
        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 : FeatureAccessException(org.ff4j.exception.FeatureAccessException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcUtils.closeConnection(org.ff4j.utils.JdbcUtils.closeConnection) PreparedStatement(java.sql.PreparedStatement) Feature(org.ff4j.core.Feature)

Example 9 with FeatureAccessException

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

the class JdbcFeatureStore method update.

/**
 * {@inheritDoc}
 */
@Override
public void update(Feature fp) {
    assertFeatureNotNull(fp);
    Connection sqlConn = null;
    PreparedStatement ps = null;
    try {
        sqlConn = dataSource.getConnection();
        Feature fpExist = read(fp.getUid());
        int enable = 0;
        if (fp.isEnable()) {
            enable = 1;
        }
        String fStrategy = null;
        String fExpression = null;
        if (fp.getFlippingStrategy() != null) {
            fStrategy = fp.getFlippingStrategy().getClass().getName();
            fExpression = MappingUtil.fromMap(fp.getFlippingStrategy().getInitParams());
        }
        update(getQueryBuilder().updateFeature(), enable, fp.getDescription(), fStrategy, fExpression, fp.getGroup(), fp.getUid());
        // ROLES
        // To be deleted (not in new value but was at first)
        Set<String> toBeDeleted = new HashSet<String>();
        toBeDeleted.addAll(fpExist.getPermissions());
        toBeDeleted.removeAll(fp.getPermissions());
        for (String roleToBeDelete : toBeDeleted) {
            removeRoleFromFeature(fpExist.getUid(), roleToBeDelete);
        }
        // To be created : in second but not in first
        Set<String> toBeAdded = new HashSet<String>();
        toBeAdded.addAll(fp.getPermissions());
        toBeAdded.removeAll(fpExist.getPermissions());
        for (String addee : toBeAdded) {
            grantRoleOnFeature(fpExist.getUid(), addee);
        }
        // REMOVE EXISTING CUSTOM PROPERTIES
        ps = sqlConn.prepareStatement(getQueryBuilder().deleteAllFeatureCustomProperties());
        ps.setString(1, fpExist.getUid());
        ps.executeUpdate();
        closeStatement(ps);
        ps = null;
        // CREATE CUSTOM PROPERTIES
        for (Property<?> property : fp.getCustomProperties().values()) {
            ps = createCustomProperty(sqlConn, fp.getUid(), property);
            closeStatement(ps);
            ps = null;
        }
    } catch (SQLException sqlEX) {
        throw new FeatureAccessException(CANNOT_CHECK_FEATURE_EXISTENCE_ERROR_RELATED_TO_DATABASE, sqlEX);
    } finally {
        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) PreparedStatement(java.sql.PreparedStatement) Feature(org.ff4j.core.Feature) HashSet(java.util.HashSet)

Example 10 with FeatureAccessException

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

the class JdbcFeatureStore method existGroup.

/**
 * {@inheritDoc}
 */
@Override
public boolean existGroup(String groupName) {
    assertHasLength(groupName);
    Connection sqlConn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        sqlConn = dataSource.getConnection();
        ps = sqlConn.prepareStatement(getQueryBuilder().existGroup());
        ps.setString(1, groupName);
        rs = ps.executeQuery();
        rs.next();
        return rs.getInt(1) > 0;
    } 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)

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