/** {@inheritDoc} */ public <T> void updateProperty(Property<T> prop) { if (prop == null || prop.getName() == null) { throw new IllegalArgumentException("Cannot update property, please provide property name"); } deleteProperty(prop.getName()); createProperty(prop); }
/**s * Constructor from DataSource. * * @param jdbcDS * native jdbc datasource */ public JdbcPropertyStore(DataSource jdbcDS, String xmlConfFile) { this(jdbcDS); importPropertiesFromXmlFile(xmlConfFile); }
/** {@inheritDoc} */ public void deleteProperty(String name) { Util.assertHasLength(name); Connection sqlConn = null; PreparedStatement ps = null; try { sqlConn = getDataSource().getConnection(); if (!existProperty(name)) { throw new PropertyNotFoundException(name); } ps = buildStatement(sqlConn, getQueryBuilder().deleteProperty(), name); ps.executeUpdate(); } catch (SQLException sqlEX) { throw new PropertyAccessException("Cannot delete property database, SQL ERROR", sqlEX); } finally { closeStatement(ps); closeConnection(sqlConn); } }
/** {@inheritDoc} */ @Override public void createSchema() { DataSource ds = getDataSource(); JdbcQueryBuilder qb = getQueryBuilder(); if (!isTableExist(ds, qb.getTableNameProperties())) { executeUpdate(ds, qb.sqlCreateTableProperties()); } }
/** {@inheritDoc} */ public void updateProperty(String name, String newValue) { Util.assertHasLength(name); Connection sqlConn = null; PreparedStatement ps = null; try { sqlConn = getDataSource().getConnection(); // Check existence Property<?> ab = readProperty(name); // Check new value validity ab.fromString(newValue); ps = buildStatement(sqlConn, getQueryBuilder().updateProperty(), newValue, name); ps.executeUpdate(); } catch (SQLException sqlEX) { throw new PropertyAccessException("Cannot update property database, SQL ERROR", sqlEX); } finally { closeStatement(ps); closeConnection(sqlConn); } }
/** {@inheritDoc} */ public Property<?> readProperty(String name) { Util.assertHasLength(name); Connection sqlConn = null; PreparedStatement ps = null; ResultSet rs = null; try { sqlConn = getDataSource().getConnection(); if (!existProperty(name)) { throw new PropertyNotFoundException(name); } // Returns features ps = buildStatement(sqlConn, getQueryBuilder().getProperty(), name); rs = ps.executeQuery(); rs.next(); return JDBC_MAPPER.map(rs); } catch (SQLException sqlEX) { throw new PropertyAccessException("Cannot check property existence, error related to database", sqlEX); } finally { closeResultSet(rs); closeStatement(ps); closeConnection(sqlConn); } }
/** {@inheritDoc} */ public void clear() { PreparedStatement ps = null; Connection sqlConn = null; try { sqlConn = getDataSource().getConnection(); ps = buildStatement(sqlConn, getQueryBuilder().deleteAllProperties()); ps.executeUpdate(); } catch (SQLException sqlEX) { throw new PropertyAccessException("Cannot clear properties table, SQL ERROR", sqlEX); } finally { closeStatement(ps); closeConnection(sqlConn); } }
/** {@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); } }
/** {@inheritDoc} */ public Set<String> listPropertyNames() { Set < String > propertyNames = new HashSet<String>(); PreparedStatement ps = null; Connection sqlConn = null; ResultSet rs = null; try { sqlConn = getDataSource().getConnection(); ps = buildStatement(sqlConn, getQueryBuilder().getAllPropertiesNames()); rs = ps.executeQuery(); while (rs.next()) { propertyNames.add(rs.getString(COL_PROPERTY_ID)); } } catch (SQLException sqlEX) { throw new PropertyAccessException("Cannot read properties within database, SQL ERROR", sqlEX); } finally { closeResultSet(rs); closeStatement(ps); closeConnection(sqlConn); } return propertyNames; }
/** {@inheritDoc} */ public boolean existProperty(String name) { Util.assertHasLength(name); PreparedStatement ps = null; ResultSet rs = null; Connection sqlConn = null; try { sqlConn = getDataSource().getConnection(); ps = buildStatement(sqlConn, getQueryBuilder().existProperty(), name); rs = ps.executeQuery(); rs.next(); return 1 == rs.getInt(1); } catch (SQLException sqlEX) { throw new PropertyAccessException("Cannot check feature existence, error related to database", sqlEX); } finally { closeResultSet(rs); closeStatement(ps); closeConnection(sqlConn); } }
/** {@inheritDoc} */ @Override public Map<String, Property<?>> readAllProperties() { Map<String, Property<?>> properties = new LinkedHashMap<String, Property<?>>(); Connection sqlConn = null; PreparedStatement ps = null; ResultSet rs = null; try { sqlConn = getDataSource().getConnection(); ps = buildStatement(sqlConn, getQueryBuilder().getAllProperties()); rs = ps.executeQuery(); while (rs.next()) { Property<?> ap = JDBC_MAPPER.map(rs); properties.put(ap.getName(),ap); } } catch (SQLException sqlEX) { throw new PropertyAccessException("Cannot read properties within database, SQL ERROR", sqlEX); } finally { closeResultSet(rs); closeStatement(ps); closeConnection(sqlConn); } return properties; }