@Override public String valueToString(Object value) { throw new ActiveObjectsException("Cannot convert BLOB value to string"); } }
@Override public String getMessage() { return "There was a SQL exception thrown by the Active Objects library:\n" + database + "\n" + driver + "\n\n" + super.getMessage(); }
@Override public String valueToString(Object value) { throw new ActiveObjectsException("Cannot convert BLOB value to string"); } }
@Override public HealthStatus doInTransaction() { try { activeObjects.find(HealthCheckEntity.class); return healthStatusFactory.healthy(); } catch (ActiveObjectsException e) { return healthStatusFactory.failed(e.getMessage()); } } });
public static Date checkAgainstMaxDate(Date date) { if (date.compareTo(MAX_DATE) > 0) { throw new ActiveObjectsException("Default date value must be strictly before " + MAX_DATE); } return date; }
public static Date checkAgainstMaxDate(Date date) { if (date.compareTo(MAX_DATE) > 0) { throw new ActiveObjectsException("Default date value must be strictly before " + MAX_DATE); } return date; }
public static Double checkDouble(Double d) { if (d.compareTo(MAX_VALUE) > 0) { throw new ActiveObjectsException("The max value of double allowed with Active Objects is " + MAX_VALUE + ", checked double is " + d); } if (d.compareTo(MIN_VALUE) < 0) { throw new ActiveObjectsException("The min value of double allowed with Active Objects is " + MIN_VALUE + ", checked double is " + d); } return d; } }
public static Double checkDouble(Double d) { if (d.compareTo(MAX_VALUE) > 0) { throw new ActiveObjectsException("The max value of double allowed with Active Objects is " + MAX_VALUE + ", checked double is " + d); } if (d.compareTo(MIN_VALUE) < 0) { throw new ActiveObjectsException("The min value of double allowed with Active Objects is " + MIN_VALUE + ", checked double is " + d); } return d; } }
public Query alias(Class<? extends RawEntity<?>> table, String alias) { if (aliases.containsValue(alias)) { throw new ActiveObjectsException("There is already a table aliased '" + alias + "' for this query!"); } aliases.put(table, alias); return this; }
public Query alias(Class<? extends RawEntity<?>> table, String alias) { if (aliases.containsValue(alias)) { throw new ActiveObjectsException("There is already a table aliased '" + alias + "' for this query!"); } aliases.put(table, alias); return this; }
public DisposableDataSource getDataSource(Class<? extends Driver> driverClass, String url, String username, String password) { Objects.requireNonNull(dataSourceFactoryClass, "dataSourceFactoryClass can't be null"); try { return dataSourceFactoryClass.newInstance().getDataSource(driverClass, url, username, password); } catch (InstantiationException | IllegalAccessException e) { throw new ActiveObjectsException("Could not create an instance of <" + dataSourceFactoryClass + ">, have you called isAvailable before hand?", e); } }
@Override public T invoke(K k) throws SQLException { final T[] ts = find(type, primaryKeyField + " = ?", k); if (ts.length == 1) { return ts[0]; } else if (ts.length == 0) { return null; } else { throw new ActiveObjectsException("Found more that one object of type '" + type.getName() + "' for key '" + k + "'"); } } };
public <T> T doInTransaction(final TransactionCallback<T> callback) { try { return new Transaction<T>(entityManager) { public T run() { return callback.doInTransaction(); } }.execute(); } catch (SQLException e) { throw new ActiveObjectsException(e); } } }, findDatabaseType(entityManager));
@Override public T invoke(K k) throws SQLException { final T[] ts = find(type, primaryKeyField + " = ?", k); if (ts.length == 1) { return ts[0]; } else if (ts.length == 0) { return null; } else { throw new ActiveObjectsException("Found more that one object of type '" + type.getName() + "' for key '" + k + "'"); } } };
static String checkLength(String name, String errorMsg) { if (enforceLength() && name != null && name.length() > MAX_LENGTH) { throw new ActiveObjectsException(errorMsg); } return name; }
static String checkLength(String name, String errorMsg) { if (enforceLength() && name != null && name.length() > MAX_LENGTH) { throw new ActiveObjectsException(errorMsg); } return name; }
@SuppressWarnings("unchecked") @Override public Enum<?> pullFromDatabase(EntityManager manager, ResultSet res, Class<Enum<?>> type, String columnName) throws SQLException { final String dbValue = res.getString(columnName); if (StringUtils.isBlank(dbValue)) { return null; } try { return Enum.valueOf((Class<? extends Enum>) type, dbValue); } catch (IllegalArgumentException e) { throw new ActiveObjectsException("Could not find enum value for '" + type + "' corresponding to database value '" + dbValue + "'"); } }
@SuppressWarnings("unchecked") @Override public Enum<?> pullFromDatabase(EntityManager manager, ResultSet res, Class<Enum<?>> type, String columnName) throws SQLException { final String dbValue = res.getString(columnName); if (StringUtils.isBlank(dbValue)) { return null; } try { return Enum.valueOf((Class<? extends Enum>) type, dbValue); } catch (IllegalArgumentException e) { throw new ActiveObjectsException("Could not find enum value for '" + type + "' corresponding to database value '" + dbValue + "'"); } }
private static DatabaseType findDatabaseType(EntityManager entityManager) { Connection connection = null; try { connection = entityManager.getProvider().getConnection(); String dbName = connection.getMetaData().getDatabaseProductName(); for (Map.Entry<String, DatabaseType> entry : DATABASE_PRODUCT_TO_TYPE_MAP.entrySet()) { // We use "startsWith" here, because the ProductName for DB2 contains OS information. if (dbName.startsWith(entry.getKey())) { return entry.getValue(); } } } catch (SQLException e) { throw new ActiveObjectsException(e); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { throw new ActiveObjectsException(e); } } } return DatabaseType.UNKNOWN; } }
static SupportedDatabase fromUri(String uri) { for (SupportedDatabase supported : values()) { if (supported.accept(uri)) { return supported; } } throw new ActiveObjectsException("Could not resolve database for database connection URI <" + uri + ">, are you sure this database is supported"); }