Search in sources :

Example 1 with Id

use of org.jaxsb.runtime.Id in project jaxdb by jaxdb.

the class EntitiesJaxSB method toEntity.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static data.Table toEntity(final $Database database, final $Row row) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException, NoSuchMethodException {
    // FIXME: This is brittle... Need to modularize it and make it clearer:
    final String tableName = row.id().substring(0, row.id().lastIndexOf('-'));
    final Class<?> binding = Class.forName(Entities.class.getPackage().getName() + "." + Identifiers.toInstanceCase(database.id()) + "$" + Identifiers.toClassCase(tableName));
    final data.Table table = (data.Table) binding.getDeclaredConstructor().newInstance();
    for (final Method method : Classes.getDeclaredMethodsDeep(row.getClass())) {
        if (!method.getName().startsWith("get") || !Attribute.class.isAssignableFrom(method.getReturnType()))
            continue;
        final Id id = method.getReturnType().getAnnotation(Id.class);
        if (id == null)
            continue;
        final $AnySimpleType type = ($AnySimpleType) method.invoke(row);
        if (type == null)
            continue;
        final String idValue = id.value();
        final int d1 = idValue.indexOf('-');
        final int d2 = idValue.indexOf('-', d1 + 1);
        final Field field = binding.getField(Identifiers.toCamelCase(d2 > -1 ? idValue.substring(d1 + 1, d2) : idValue.substring(d1 + 1)));
        final data.Column column = (data.Column<?>) field.get(table);
        final Class<? extends $AnySimpleType> returnType = (Class<? extends $AnySimpleType>) method.getReturnType();
        final Object value = type.text();
        if (value == null)
            column.set(null);
        else if ($Binary.class.isAssignableFrom(returnType))
            column.set(((HexBinary) value).getBytes());
        else if ($Blob.class.isAssignableFrom(returnType))
            column.set(new ByteArrayInputStream(((HexBinary) value).getBytes()));
        else if (value instanceof String)
            // FIXME: Setting vendor to null here... need to review this pattern
            column.set(column.parseString(null, (String) value));
        else
            column.set(value);
    }
    return table;
}
Also used : Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) XMLSchema.yAA.$AnySimpleType(org.w3.www._2001.XMLSchema.yAA.$AnySimpleType) ByteArrayInputStream(java.io.ByteArrayInputStream) Id(org.jaxsb.runtime.Id) org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Binary(org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Binary)

Example 2 with Id

use of org.jaxsb.runtime.Id in project jaxdb by jaxdb.

the class SqlJaxSBLoader method loadRow.

@SuppressWarnings("unchecked")
private static void loadRow(final ArrayList<Row> rows, final $Row row, final Dialect dialect, final Compiler compiler, final TableToColumnToIncrement tableToColumnToIncrement) {
    try {
        final int i = row.id().lastIndexOf('-');
        final String tableName = row.id().substring(0, i);
        final int weight = Integer.parseInt(row.id().substring(i + 1));
        final StringBuilder columns = new StringBuilder();
        final StringBuilder values = new StringBuilder();
        boolean hasValues = false;
        final Method[] methods = Classes.getDeclaredMethodsDeep(row.getClass());
        for (final Method method : methods) {
            if (!method.getName().startsWith("get") || !Attribute.class.isAssignableFrom(method.getReturnType()))
                continue;
            final Id id = method.getReturnType().getAnnotation(Id.class);
            if (id == null)
                continue;
            final String idValue = id.value();
            final int d1 = idValue.indexOf('-');
            final int d2 = idValue.indexOf('-', d1 + 1);
            final String columnName;
            final String generateOnInsert;
            final boolean isAutoIncremented;
            if (d2 != -1) {
                columnName = idValue.substring(d1 + 1, d2);
                generateOnInsert = idValue.substring(d2 + 1);
                isAutoIncremented = "AUTO_INCREMENT".equals(generateOnInsert);
            } else {
                columnName = idValue.substring(d1 + 1);
                generateOnInsert = null;
                isAutoIncremented = false;
            }
            final $AnySimpleType<?> attribute = ($AnySimpleType<?>) method.invoke(row);
            String value = getValue(compiler, attribute);
            if (value == null) {
                if (generateOnInsert == null || isAutoIncremented)
                    continue;
                value = generateValue(dialect, compiler, (Class<? extends $AnySimpleType<?>>) method.getReturnType(), generateOnInsert);
            } else if (isAutoIncremented) {
                final Map<String, Integer> columnToIncrement = tableToColumnToIncrement.get(tableName);
                final Integer increment = columnToIncrement.get(columnName);
                final Integer intValue = Integer.valueOf(value);
                if (increment == null || increment < intValue)
                    columnToIncrement.put(columnName, intValue);
            }
            if (hasValues) {
                columns.append(", ");
                values.append(", ");
            }
            columns.append(dialect.quoteIdentifier(columnName));
            values.append(value);
            hasValues = true;
        }
        if (columns.length() == 0)
            throw new IllegalStateException();
        rows.add(new Row(weight, compiler.insert(tableName, columns, values)));
    } catch (final IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (final InvocationTargetException e) {
        if (e.getCause() instanceof RuntimeException)
            throw (RuntimeException) e.getCause();
        throw new RuntimeException(e.getCause());
    }
}
Also used : Method(java.lang.reflect.Method) org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Tinyint(org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Tinyint) org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Smallint(org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Smallint) org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Bigint(org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Bigint) InvocationTargetException(java.lang.reflect.InvocationTargetException) XMLSchema.yAA.$AnySimpleType(org.w3.www._2001.XMLSchema.yAA.$AnySimpleType) Id(org.jaxsb.runtime.Id) org.jaxdb.www.sqlx_0_5.xLygluGCXAA.$Row(org.jaxdb.www.sqlx_0_5.xLygluGCXAA.$Row) Map(java.util.Map)

Aggregations

Method (java.lang.reflect.Method)2 Id (org.jaxsb.runtime.Id)2 XMLSchema.yAA.$AnySimpleType (org.w3.www._2001.XMLSchema.yAA.$AnySimpleType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Map (java.util.Map)1 org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Bigint (org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Bigint)1 org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Binary (org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Binary)1 org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Smallint (org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Smallint)1 org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Tinyint (org.jaxdb.www.datatypes_0_5.xL3gluGCXAA.$Tinyint)1 org.jaxdb.www.sqlx_0_5.xLygluGCXAA.$Row (org.jaxdb.www.sqlx_0_5.xLygluGCXAA.$Row)1