Search in sources :

Example 1 with FeatureAlreadyExistException

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

the class FeatureStoreNeo4J method create.

/**
 * {@inheritDoc}
 */
@Override
public void create(Feature fp) {
    if (fp == null) {
        throw new IllegalArgumentException("Feature cannot be null nor empty");
    }
    if (exist(fp.getUid())) {
        throw new FeatureAlreadyExistException(fp.getUid());
    }
    Transaction tx = graphDb.beginTx();
    // Create core
    graphDb.execute(createQueryNewCoreFeature(fp));
    // Create Flipping Strategy
    if (fp.getFlippingStrategy() != null) {
        graphDb.execute(createQueryFlippingStrategy(fp));
    }
    // Create Group
    if (fp.getGroup() != null && !"".equals(fp.getGroup())) {
        addToGroup(fp.getUid(), fp.getGroup());
    }
    tx.success();
    // Create Properties
    if (fp.getCustomProperties() != null && fp.getCustomProperties().size() > 0) {
        for (String pName : fp.getCustomProperties().keySet()) {
            createProperty(fp.getProperty(pName), fp.getUid());
        }
    }
}
Also used : FeatureAlreadyExistException(org.ff4j.exception.FeatureAlreadyExistException) Transaction(org.neo4j.graphdb.Transaction)

Example 2 with FeatureAlreadyExistException

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

the class FeatureStoreRedis method create.

/**
 * {@inheritDoc}
 */
@Override
public void create(Feature fp) {
    Util.assertNotNull("Feature", fp);
    if (exist(fp.getUid())) {
        throw new FeatureAlreadyExistException(fp.getUid());
    }
    Jedis jedis = null;
    try {
        String id = fp.getUid();
        jedis = getJedis();
        // Store the feature in the mapping bucket.
        jedis.sadd(KEY_FEATURE_MAP, id);
        jedis.set(KEY_FEATURE + id, fp.toJson());
        jedis.persist(KEY_FEATURE + id);
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
Also used : FeatureAlreadyExistException(org.ff4j.exception.FeatureAlreadyExistException) Jedis(redis.clients.jedis.Jedis)

Example 3 with FeatureAlreadyExistException

use of org.ff4j.exception.FeatureAlreadyExistException 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)

Aggregations

FeatureAlreadyExistException (org.ff4j.exception.FeatureAlreadyExistException)3 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 FeatureAccessException (org.ff4j.exception.FeatureAccessException)1 JdbcUtils.closeConnection (org.ff4j.utils.JdbcUtils.closeConnection)1 Transaction (org.neo4j.graphdb.Transaction)1 Jedis (redis.clients.jedis.Jedis)1