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