Search in sources :

Example 1 with PropertyAlreadyExistException

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

the class PropertyStoreHttp method createProperty.

/**
 * {@inheritDoc}
 */
public <T> void createProperty(Property<T> value) {
    Util.assertNotNull(value);
    Util.assertHasLength(value.getName());
    if (existProperty(value.getName())) {
        throw new PropertyAlreadyExistException("Property already exist");
    }
    /* Now can process upsert through PUT HTTP method
        Response cRes = getStore().path(value.getName())//
                .request(MediaType.APPLICATION_JSON)
                .put(Entity.entity(new PropertyApiBean(value), MediaType.APPLICATION_JSON));*/
    Response cRes = ClientHttpUtils.createRequest(getStore().path(value.getName()), authorization, null).put(Entity.entity(new PropertyApiBean(value), MediaType.APPLICATION_JSON));
    // Check response code CREATED or raised error
    if (Status.CREATED.getStatusCode() != cRes.getStatus()) {
        throw new FeatureAccessException("Cannot create properties, an HTTP error " + cRes.getStatus() + OCCURED);
    }
}
Also used : Response(javax.ws.rs.core.Response) PropertyApiBean(org.ff4j.web.api.resources.domain.PropertyApiBean) FeatureAccessException(org.ff4j.exception.FeatureAccessException) PropertyAlreadyExistException(org.ff4j.exception.PropertyAlreadyExistException)

Example 2 with PropertyAlreadyExistException

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

the class JdbcPropertyStore method createProperty.

/**
 * {@inheritDoc}
 */
public <T> void createProperty(Property<T> ap) {
    Util.assertNotNull(ap);
    Connection sqlConn = null;
    PreparedStatement ps = null;
    try {
        sqlConn = getDataSource().getConnection();
        if (existProperty(ap.getName())) {
            throw new PropertyAlreadyExistException(ap.getName());
        }
        ps = sqlConn.prepareStatement(getQueryBuilder().createProperty());
        ps.setString(1, ap.getName());
        ps.setString(2, ap.getType());
        ps.setString(3, ap.asString());
        ps.setString(4, ap.getDescription());
        if (ap.getFixedValues() != null && !ap.getFixedValues().isEmpty()) {
            String fixedValues = ap.getFixedValues().toString();
            ps.setString(5, fixedValues.substring(1, fixedValues.length() - 1));
        } else {
            ps.setString(5, null);
        }
        ps.executeUpdate();
    } catch (SQLException sqlEX) {
        throw new PropertyAccessException("Cannot update properties database, SQL ERROR", sqlEX);
    } finally {
        // Connection is closed alse here within clos statement
        closeStatement(ps);
        closeConnection(sqlConn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcUtils.closeConnection(org.ff4j.utils.JdbcUtils.closeConnection) PropertyAlreadyExistException(org.ff4j.exception.PropertyAlreadyExistException) PropertyAccessException(org.ff4j.exception.PropertyAccessException) PreparedStatement(java.sql.PreparedStatement)

Example 3 with PropertyAlreadyExistException

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

the class PropertyStoreHttp method createProperty.

/**
 * {@inheritDoc}
 */
public <T> void createProperty(Property<T> value) {
    Util.assertNotNull(value);
    Util.assertHasLength(value.getName());
    if (existProperty(value.getName())) {
        throw new PropertyAlreadyExistException("Property already exist");
    }
    ClientResponse cRes = // 
    getStore().path(value.getName()).type(// 
    MediaType.APPLICATION_JSON).put(ClientResponse.class, new PropertyApiBean(value));
    // Check response code CREATED or raised error
    if (Status.CREATED.getStatusCode() != cRes.getStatus()) {
        throw new FeatureAccessException("Cannot create properties, an HTTP error " + cRes.getStatus() + OCCURED);
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) PropertyApiBean(org.ff4j.web.api.resources.domain.PropertyApiBean) FeatureAccessException(org.ff4j.exception.FeatureAccessException) PropertyAlreadyExistException(org.ff4j.exception.PropertyAlreadyExistException)

Example 4 with PropertyAlreadyExistException

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

the class PropertyStoreNeo4j method createProperty.

/**
 * {@inheritDoc}
 */
public <T> void createProperty(Property<T> ap) {
    Util.assertNotNull(ap);
    Util.assertHasLength(ap.getName());
    if (existProperty(ap.getName())) {
        throw new PropertyAlreadyExistException(ap.getName());
    }
    StringBuilder cypherCreate = new StringBuilder();
    cypherCreate.append("CREATE (p:" + FF4jNeo4jLabels.FF4J_PROPERTY + " { name :'");
    cypherCreate.append(ap.getName());
    cypherCreate.append("', type:'");
    cypherCreate.append(ap.getType());
    cypherCreate.append("', value:'");
    cypherCreate.append(ap.getValue());
    cypherCreate.append("', fixedValues:[");
    if (ap.getFixedValues() != null && !ap.getFixedValues().isEmpty()) {
        boolean first = true;
        for (Object fix : ap.getFixedValues()) {
            if (!first) {
                cypherCreate.append(",");
            }
            cypherCreate.append("'" + fix.toString() + "'");
            first = false;
        }
    }
    cypherCreate.append("]");
    if (ap.getDescription() != null && ap.getDescription().length() > 0) {
        cypherCreate.append(", description:'");
        cypherCreate.append(ap.getDescription());
        cypherCreate.append("'");
    }
    cypherCreate.append("});");
    Transaction tx = graphDb.beginTx();
    graphDb.execute(cypherCreate.toString());
    tx.success();
}
Also used : Transaction(org.neo4j.graphdb.Transaction) PropertyAlreadyExistException(org.ff4j.exception.PropertyAlreadyExistException)

Example 5 with PropertyAlreadyExistException

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

the class PropertyStoreRedis method createProperty.

/**
 * {@inheritDoc}
 */
public <T> void createProperty(Property<T> prop) {
    Util.assertNotNull(prop);
    if (existProperty(prop.getName())) {
        throw new PropertyAlreadyExistException(prop.getName());
    }
    Jedis jedis = null;
    try {
        jedis = getJedis();
        String name = prop.getName();
        // Store the feature in the mapping bucket.
        jedis.sadd(KEY_PROPERTY_MAP, name);
        jedis.set(KEY_PROPERTY + name, prop.toJson());
        jedis.persist(KEY_PROPERTY + name);
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) PropertyAlreadyExistException(org.ff4j.exception.PropertyAlreadyExistException)

Aggregations

PropertyAlreadyExistException (org.ff4j.exception.PropertyAlreadyExistException)5 FeatureAccessException (org.ff4j.exception.FeatureAccessException)2 PropertyApiBean (org.ff4j.web.api.resources.domain.PropertyApiBean)2 ClientResponse (com.sun.jersey.api.client.ClientResponse)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 Response (javax.ws.rs.core.Response)1 PropertyAccessException (org.ff4j.exception.PropertyAccessException)1 JdbcUtils.closeConnection (org.ff4j.utils.JdbcUtils.closeConnection)1 Transaction (org.neo4j.graphdb.Transaction)1 Jedis (redis.clients.jedis.Jedis)1