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