Search in sources :

Example 11 with PersistentWrapper

use of com.ramussoft.common.persistent.PersistentWrapper in project ramus by Vitaliy-Yakovchuk.

the class UniversalPersistentFactory method checkClass.

@Override
protected void checkClass(ClassLoader classLoader, Class<?> p, String pluginName, String typeName) {
    PersistentWrapper wrapper = new PersistentWrapper(p);
    Table table = p.getAnnotation(Table.class);
    String className = p.getName();
    PersistentRow row = getRow(className);
    if (row == null) {
        row = new PersistentRow(classLoader);
        rows.add(row);
    } else
        row.setClassLoader(classLoader);
    row.setClassName(className);
    row.setExists(true);
    row.setPluginName(pluginName);
    fillTable(table, row, p);
    if (table != null)
        row.setTableType(table.type().ordinal());
    row.setTypeName(typeName);
    String[] fields = wrapper.getFields();
    for (String fieldName : fields) {
        String databaseField = wrapper.toDatabaseField(fieldName);
        PersistentField field = getField(row, databaseField);
        if (field == null) {
            field = new PersistentField();
            row.getFields().add(field);
        }
        int type = wrapper.getAnnotationType(fieldName);
        Class<? extends Annotation> annotation = PersistentWrapper.ANNOTATIONS[type];
        if (Attribute.class.isAssignableFrom(annotation)) {
            Attribute attr = wrapper.getAnnotation(fieldName, Attribute.class);
            field.setAutoset(attr.autoset());
            field.setFieldId(attr.id());
            field.setPrimary(attr.primary());
        } else if (Element.class.isAssignableFrom(annotation)) {
            Element attr = wrapper.getAnnotation(fieldName, Element.class);
            field.setAutoset(attr.autoset());
            field.setFieldId(attr.id());
            field.setPrimary(attr.primary());
        } else if (com.ramussoft.common.persistent.Long.class.isAssignableFrom(annotation)) {
            com.ramussoft.common.persistent.Long attr = wrapper.getAnnotation(fieldName, com.ramussoft.common.persistent.Long.class);
            field.setFieldId(attr.id());
            field.setPrimary(attr.primary());
            if (attr.setDefaultValue()) {
                field.setDefaultValue(Long.toString(attr.defaultValue()));
            }
        } else if (com.ramussoft.common.persistent.Double.class.isAssignableFrom(annotation)) {
            com.ramussoft.common.persistent.Double attr = wrapper.getAnnotation(fieldName, com.ramussoft.common.persistent.Double.class);
            field.setFieldId(attr.id());
            field.setPrimary(attr.primary());
        } else if (Date.class.isAssignableFrom(annotation)) {
            Date attr = wrapper.getAnnotation(fieldName, Date.class);
            field.setFieldId(attr.id());
            field.setPrimary(attr.primary());
        } else if (Text.class.isAssignableFrom(annotation)) {
            Text attr = wrapper.getAnnotation(fieldName, Text.class);
            field.setFieldId(attr.id());
            field.setPrimary(attr.primary());
        } else if (Id.class.isAssignableFrom(annotation)) {
            Id attr = wrapper.getAnnotation(fieldName, Id.class);
            field.setFieldId(attr.id());
            field.setPrimary(true);
            field.setAutoset(true);
        } else if (Qualifier.class.isAssignableFrom(annotation)) {
            Qualifier attr = wrapper.getAnnotation(fieldName, Qualifier.class);
            field.setFieldId(attr.id());
            field.setPrimary(attr.primary());
        } else if (Binary.class.isAssignableFrom(annotation)) {
            Binary binary = wrapper.getAnnotation(fieldName, Binary.class);
            field.setFieldId(binary.id());
        } else if (com.ramussoft.common.persistent.Integer.class.isAssignableFrom(annotation)) {
            com.ramussoft.common.persistent.Integer integer = wrapper.getAnnotation(fieldName, com.ramussoft.common.persistent.Integer.class);
            field.setFieldId(integer.id());
            field.setPrimary(integer.primary());
        } else {
            throw new RuntimeException("Unknown annotation class: " + annotation.getName());
        }
        field.setDatabaseName(databaseField);
        field.setType(type);
        field.setName(fieldName);
    }
}
Also used : Table(com.ramussoft.common.persistent.Table) PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) Attribute(com.ramussoft.common.attribute.Attribute) Element(com.ramussoft.common.persistent.Element) Text(com.ramussoft.common.persistent.Text) Date(com.ramussoft.common.persistent.Date) Qualifier(com.ramussoft.common.persistent.Qualifier) Id(com.ramussoft.common.persistent.Id) Binary(com.ramussoft.common.persistent.Binary) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 12 with PersistentWrapper

use of com.ramussoft.common.persistent.PersistentWrapper in project ramus by Vitaliy-Yakovchuk.

the class TransactionStorageCommand method storePersistent.

static void storePersistent(JournaledEngine engine, BinaryDataOutput output, Persistent persistent) throws IOException {
    Class<? extends Persistent> key = persistent.getClass();
    byte id = (byte) engine.getPersistentClassId(key);
    output.writeByte(id);
    PersistentRow row = engine.getPersistentMetadata(key);
    List<PersistentField> fields = new ArrayList<PersistentField>();
    for (PersistentField field : row.getFields()) if (!field.isAutoset())
        fields.add(field);
    output.write(fields.size());
    for (PersistentField field : fields) {
        output.writeByte(field.getFieldId());
        String fieldName = field.getName();
        PersistentWrapper wrapper = engine.getWrapper(key);
        Object object = wrapper.getField(persistent, fieldName);
        if (object == null)
            output.write(0);
        else {
            output.write(1);
            switch(field.getType()) {
                case ATTRIBUTE:
                case ELEMENT:
                case QUALIFIER:
                case ID:
                case LONG:
                    output.writeLong((Long) object);
                    break;
                case TEXT:
                    output.writeSwimedString((String) object);
                    break;
                case DATE:
                    output.writeLong(((Timestamp) object).getTime());
                    break;
                case DOUBLE:
                    output.writeDouble((Double) object);
                    break;
                case PersistentField.BINARY:
                    output.writeBinary((byte[]) object);
                    break;
                case PersistentField.INTEGER:
                    output.writeInt((Integer) object);
                    break;
                default:
                    throw new RuntimeException("Unknown field type: " + field.getType());
            }
        }
    }
    output.writeLong(persistent.getValueBranchId());
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) ArrayList(java.util.ArrayList) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 13 with PersistentWrapper

use of com.ramussoft.common.persistent.PersistentWrapper in project ramus by Vitaliy-Yakovchuk.

the class TransactionStorageCommand method loadPersistent.

static Persistent loadPersistent(JournaledEngine engine, BinaryDataInput input) throws IOException, InstantiationException, IllegalAccessException {
    Class<? extends Persistent> key = engine.getPersistentClassById(input.read());
    PersistentRow row = engine.getPersistentMetadata(key);
    Persistent persistent = key.newInstance();
    int size = input.read();
    for (int i = 0; i < size; i++) {
        int fieldId = input.read();
        PersistentField field = row.getFieldById(fieldId);
        String fieldName = field.getName();
        PersistentWrapper wrapper = engine.getWrapper(key);
        if (input.read() == 1)
            switch(field.getType()) {
                case ATTRIBUTE:
                case ELEMENT:
                case QUALIFIER:
                case ID:
                case LONG:
                    wrapper.setField(persistent, fieldName, input.readLong());
                    break;
                case TEXT:
                    wrapper.setField(persistent, fieldName, input.readSwimedString());
                    break;
                case DATE:
                    wrapper.setField(persistent, fieldName, new Timestamp(input.readLong()));
                    break;
                case DOUBLE:
                    wrapper.setField(persistent, fieldName, input.readDouble());
                    break;
                case BINARY:
                    wrapper.setField(persistent, fieldName, input.readBinary());
                    break;
                case INTEGER:
                    wrapper.setField(persistent, fieldName, input.readInt());
                    break;
                default:
                    throw new RuntimeException("Unknown field type: " + field.getType());
            }
    }
    persistent.setValueBranchId(input.readLong());
    return persistent;
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) Persistent(com.ramussoft.common.persistent.Persistent) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper) Timestamp(java.sql.Timestamp)

Example 14 with PersistentWrapper

use of com.ramussoft.common.persistent.PersistentWrapper in project ramus by Vitaliy-Yakovchuk.

the class AbstractJournaledEngine method equalsPrimaryKeys.

private boolean equalsPrimaryKeys(Persistent a, Persistent b) {
    PersistentWrapper wrapper = getWrapper(a.getClass());
    PersistentRow row = getPersistentMetadata(a.getClass());
    List<PersistentField> fields = row.getFields();
    for (PersistentField field : fields) if (field.isPrimary()) {
        if (!equals(wrapper.getField(a, field.getName()), wrapper.getField(b, field.getName())))
            return false;
    }
    return true;
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 15 with PersistentWrapper

use of com.ramussoft.common.persistent.PersistentWrapper in project ramus by Vitaliy-Yakovchuk.

the class AbstractJournaledEngine method equalsAllFields.

private boolean equalsAllFields(PersistentPare pare) {
    Persistent a = pare.oldPersistent;
    Persistent b = pare.newPersistent;
    PersistentWrapper wrapper = getWrapper(a.getClass());
    PersistentRow row = getPersistentMetadata(a.getClass());
    for (PersistentField f : row.getFields()) {
        if (!f.isAutoset()) {
            String field = f.getName();
            if (!equals(wrapper.getField(a, field), wrapper.getField(b, field)))
                return false;
        }
    }
    return true;
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) Persistent(com.ramussoft.common.persistent.Persistent) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Aggregations

PersistentWrapper (com.ramussoft.common.persistent.PersistentWrapper)24 PersistentField (com.ramussoft.common.persistent.PersistentField)22 PersistentRow (com.ramussoft.common.persistent.PersistentRow)22 Persistent (com.ramussoft.common.persistent.Persistent)9 ArrayList (java.util.ArrayList)9 PreparedStatement (java.sql.PreparedStatement)6 List (java.util.List)6 FindObject (com.ramussoft.common.attribute.FindObject)5 AttributePlugin (com.ramussoft.common.attribute.AttributePlugin)4 RowMapper (com.ramussoft.jdbc.RowMapper)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 Attribute (com.ramussoft.common.Attribute)3 Attribute (com.ramussoft.common.attribute.Attribute)2 Binary (com.ramussoft.common.persistent.Binary)2 Date (com.ramussoft.common.persistent.Date)2 Element (com.ramussoft.common.persistent.Element)2 Id (com.ramussoft.common.persistent.Id)2 Qualifier (com.ramussoft.common.persistent.Qualifier)2 Table (com.ramussoft.common.persistent.Table)2