Search in sources :

Example 1 with PersistentField

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

the class AbstractJournaledEngine method setPersistentAttribute.

private void setPersistentAttribute(Element element, Attribute attribute, Object newObject, AttributeConverter converter) {
    long elementId = (element == null) ? -1 : element.getId();
    long attributeId = attribute.getId();
    List<Persistent>[] object = converter.toPersistens(newObject, elementId, attributeId, this);
    for (int i = 0; i < object.length; i++) {
        object[i] = new ArrayList<Persistent>(object[i]);
    }
    for (List<Persistent> list : object) {
        for (Persistent p : list) {
            PersistentRow row = getPersistentMetadata(p.getClass());
            PersistentWrapper wrapper = getWrapper(p.getClass());
            for (PersistentField field : row.getFields()) {
                if (field.isAutoset()) {
                    if (field.getType() == PersistentField.ATTRIBUTE) {
                        wrapper.setField(p, field.getName(), attributeId);
                    } else if (field.getType() == PersistentField.ELEMENT) {
                        wrapper.setField(p, field.getName(), elementId);
                    } else if (field.getType() == PersistentField.ID) {
                    }
                /*
                         * else throw new NullPointerException(
						 * "No way to autoset field: " + field.getName() +
						 * " for " + row.getClassName());
						 */
                }
            }
        }
    }
    long activeBranch = getActiveBranch();
    List<Persistent>[] old = getBinaryBranchAttribute(elementId, attributeId, activeBranch);
    Transaction transaction = new Transaction();
    for (PersistentPare pare : findChanges(old, object)) {
        if (pare.newPersistent == null) {
            transaction.getDelete().add(pare.oldPersistent);
        } else if (pare.oldPersistent == null) {
            pare.newPersistent.setValueBranchId(activeBranch);
            transaction.getSave().add(pare.newPersistent);
        } else {
            transaction.getUpdate().add(pare.newPersistent);
            transaction.getOldUpdate().add(pare.oldPersistent);
        }
    }
    setAttribute(elementId, attributeId, transaction);
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) Transaction(com.ramussoft.common.persistent.Transaction) PersistentRow(com.ramussoft.common.persistent.PersistentRow) ArrayList(java.util.ArrayList) List(java.util.List) Persistent(com.ramussoft.common.persistent.Persistent) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 2 with PersistentField

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

the class AbstractAttributePlugin method fillAttributeQuery.

@Override
public void fillAttributeQuery(PersistentRow row, long attributeId, long elementId, ArrayList<Object> params, ArrayList<String> paramFields, IEngine engine) {
    for (PersistentField field : row.getFields()) {
        if (field.isAutoset()) {
            if ((field.getType() == PersistentField.ELEMENT) && (elementId >= 0)) {
                params.add(elementId);
                paramFields.add(field.getDatabaseName());
            } else if ((attributeId >= 0) && (field.getType() == PersistentField.ATTRIBUTE)) {
                params.add(attributeId);
                paramFields.add(field.getDatabaseName());
            }
        }
    }
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField)

Example 3 with PersistentField

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

the class IEngineImpl method getAttributeWhatWillBeDeleted.

private Transaction getAttributeWhatWillBeDeleted(long elementId, Attribute attribute, boolean attributeDeteting) {
    final Transaction transaction = new Transaction();
    AttributePlugin plugin = factory.getAttributePlugin(attribute.getAttributeType());
    final Class<? extends Persistent>[] classes;
    if (elementId >= 0)
        classes = plugin.getAttributePersistents();
    else
        classes = plugin.getAttributePropertyPersistents();
    for (int i = 0; i < classes.length; i++) {
        final Class<? extends Persistent> clazz = classes[i];
        final PersistentRow row = metadata.get(clazz);
        final PersistentWrapper wrapper = wrappers.get(clazz);
        ArrayList<Object> params = new ArrayList<Object>(2);
        ArrayList<String> paramFields = new ArrayList<String>(2);
        boolean delete = false;
        for (PersistentField field : row.getFields()) {
            if (field.isAutoset()) {
                if (field.getType() == PersistentField.ELEMENT) {
                    params.add(elementId);
                    paramFields.add(field.getDatabaseName());
                    if (!attributeDeteting)
                        delete = true;
                } else if (field.getType() == PersistentField.ATTRIBUTE) {
                    params.add(attribute.getId());
                    paramFields.add(field.getDatabaseName());
                    if (attributeDeteting)
                        delete = true;
                } else if (field.getType() == PersistentField.VALUE_BRANCH_ID) {
                    params.add(getActiveBranchId());
                    paramFields.add(field.getDatabaseName());
                    if (attributeDeteting)
                        delete = true;
                }
            }
        }
        if (delete) {
            StringBuffer sb = new StringBuffer("SELECT * FROM " + row.getTableName() + " WHERE ");
            boolean first = true;
            for (int x = 0; x < params.size(); x++) {
                if (first) {
                    first = false;
                } else {
                    sb.append(" AND ");
                }
                sb.append(paramFields.get(x));
                sb.append("=?");
            }
            template.query(sb.toString(), new RowMapper() {

                @Override
                public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                    try {
                        Persistent persistent = clazz.newInstance();
                        for (PersistentField field : row.getFields()) {
                            wrapper.setDatabaseField(persistent, field, rs);
                        }
                        transaction.getDelete().add(persistent);
                        return null;
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    throw new RuntimeException();
                }
            }, params.toArray(new Object[params.size()]), true);
        }
    }
    transaction.setRemoveBranchInfo(true);
    return transaction;
}
Also used : AttributePlugin(com.ramussoft.common.attribute.AttributePlugin) PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Persistent(com.ramussoft.common.persistent.Persistent) Transaction(com.ramussoft.common.persistent.Transaction) ResultSet(java.sql.ResultSet) FindObject(com.ramussoft.common.attribute.FindObject) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper) RowMapper(com.ramussoft.jdbc.RowMapper)

Example 4 with PersistentField

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

the class IEngineImpl method getBinaryBranchAttribute.

@Override
public List<Persistent>[] getBinaryBranchAttribute(long elementId, long attributeId, long branchId) {
    throwExaptionIfNotCan(getAccessor().canReadElement(elementId, attributeId), "Can not get attribute.");
    Attribute attribute = getAttribute(attributeId);
    AttributePlugin plugin = factory.getAttributePlugin(attribute.getAttributeType());
    final Class<? extends Persistent>[] classes;
    if (elementId >= 0)
        classes = plugin.getAttributePersistents();
    else
        classes = plugin.getAttributePropertyPersistents();
    List<Persistent>[] lists = new List[classes.length];
    for (int i = 0; i < lists.length; i++) {
        final Class<? extends Persistent> clazz = classes[i];
        final PersistentRow row = metadata.get(clazz);
        final PersistentWrapper wrapper = wrappers.get(clazz);
        ArrayList<Object> params = new ArrayList<Object>(3);
        ArrayList<String> paramFields = new ArrayList<String>(3);
        plugin.fillAttributeQuery(row, attributeId, elementId, params, paramFields, this);
        if (elementId >= 0l && attribute.getAttributeType().getTypeName().equals("ElementList") && attribute.getAttributeType().getPluginName().equals("Core")) {
            return getEListFixed(row, clazz, wrapper, paramFields, params, getActiveBranchId());
        }
        params.add(branchId);
        paramFields.add("value_branch_id");
        StringBuffer sb = new StringBuffer("SELECT * FROM " + row.getTableName());
        if (params.size() > 0) {
            sb.append(" WHERE ");
        }
        boolean first = true;
        for (int j = 0; j < params.size(); j++) {
            if (first) {
                first = false;
            } else {
                sb.append(" AND ");
            }
            sb.append(paramFields.get(j));
            sb.append("=?");
        }
        List<Persistent> list = template.query(sb.toString(), new RowMapper() {

            @Override
            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                try {
                    Persistent persistent = clazz.newInstance();
                    for (PersistentField field : row.getFields()) {
                        wrapper.setDatabaseField(persistent, field, rs);
                    }
                    return persistent;
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                throw new RuntimeException();
            }
        }, params.toArray(new Object[params.size()]), true);
        lists[i] = list;
    }
    return lists;
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) Attribute(com.ramussoft.common.Attribute) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) RowMapper(com.ramussoft.jdbc.RowMapper) AttributePlugin(com.ramussoft.common.attribute.AttributePlugin) PersistentRow(com.ramussoft.common.persistent.PersistentRow) Persistent(com.ramussoft.common.persistent.Persistent) FindObject(com.ramussoft.common.attribute.FindObject) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 5 with PersistentField

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

the class IEngineImpl method getBinaryElements.

@Override
public Hashtable<Element, List<Persistent>[][]> getBinaryElements(long qualifierId, final long[] attributeIds) {
    for (long attributeId : attributeIds) throwExaptionIfNotCan(getAccessor().canReadAttribute(qualifierId, attributeId), "Can not get attribute for qualifier.");
    List<Element> elements = getElements(qualifierId);
    Hashtable<Element, List<Persistent>[][]> result = new Hashtable<Element, List<Persistent>[][]>();
    final Hashtable<Long, List<Persistent>[][]> values = new Hashtable<Long, List<Persistent>[][]>();
    int index = 0;
    final int[] persistentCount = new int[attributeIds.length];
    for (int i = 0; i < attributeIds.length; i++) {
        Attribute attribute = getAttribute(attributeIds[i]);
        AttributePlugin plugin = factory.getAttributePlugin(attribute.getAttributeType());
        persistentCount[i] = plugin.getAttributePersistents().length;
    }
    for (long attributeId : attributeIds) {
        final int attrIndex = index;
        Attribute attribute = getAttribute(attributeId);
        AttributePlugin plugin = factory.getAttributePlugin(attribute.getAttributeType());
        final Class<? extends Persistent>[] classes;
        classes = plugin.getAttributePersistents();
        for (int i = 0; i < classes.length; i++) {
            final Class<? extends Persistent> clazz = classes[i];
            final int listIndex = i;
            final PersistentRow row = metadata.get(clazz);
            final PersistentWrapper wrapper = wrappers.get(clazz);
            ArrayList<Object> params = new ArrayList<Object>(2);
            ArrayList<String> paramFields = new ArrayList<String>(2);
            plugin.fillAttributeQuery(row, attributeId, -1l, params, paramFields, this);
            StringBuffer sb = new StringBuffer("SELECT * FROM " + row.getTableName() + " main_table");
            StringBuffer where = new StringBuffer(" AND attribute_id=?");
            for (int j = 0; j < params.size(); j++) if (paramFields.get(j).startsWith("element") && paramFields.get(j).endsWith("id")) {
                where.append(" AND main_table.");
                where.append(paramFields.get(j));
                where.append("=element_id");
            }
            for (PersistentField field : row.getFields()) {
                if (field.isAutoset()) {
                    if (field.getType() == PersistentField.ELEMENT) {
                        where.append(" AND main_table.");
                        where.append(field.getDatabaseName());
                        where.append("=element_id");
                    }
                }
            }
            sb.append(" WHERE (value_branch_id IN (SELECT MAX(branch_id) FROM " + prefix + "attributes_data_metadata WHERE branch_id<=? " + where + ") OR ((SELECT MAX(branch_id) FROM " + prefix + "attributes_data_metadata WHERE branch_id<=? " + where + ") IS NULL AND value_branch_id=0))");
            for (int j = 0; j < params.size(); j++) {
                sb.append(" AND ");
                sb.append(paramFields.get(j));
                sb.append("=?");
            }
            params.add(0, getActiveBranchId());
            params.add(1, attributeId);
            params.add(2, getActiveBranchId());
            params.add(3, attributeId);
            template.query(sb.toString(), new RowMapper() {

                @Override
                public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                    try {
                        Persistent persistent = clazz.newInstance();
                        long id = -1;
                        for (PersistentField field : row.getFields()) {
                            wrapper.setDatabaseField(persistent, field, rs);
                            if ((field.isAutoset()) && (field.getType() == PersistentField.ELEMENT)) {
                                id = rs.getLong(field.getDatabaseName());
                            }
                        }
                        getLists(values, id, attributeIds.length, persistentCount)[attrIndex][listIndex].add(persistent);
                        return null;
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    throw new RuntimeException();
                }
            }, params.toArray(new Object[params.size()]), false);
        }
        index++;
    }
    for (Element element : elements) {
        result.put(element, getLists(values, element.getId(), attributeIds.length, persistentCount));
    }
    return result;
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) Attribute(com.ramussoft.common.Attribute) SQLException(java.sql.SQLException) Element(com.ramussoft.common.Element) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) RowMapper(com.ramussoft.jdbc.RowMapper) AttributePlugin(com.ramussoft.common.attribute.AttributePlugin) PersistentRow(com.ramussoft.common.persistent.PersistentRow) Hashtable(java.util.Hashtable) Persistent(com.ramussoft.common.persistent.Persistent) FindObject(com.ramussoft.common.attribute.FindObject) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Aggregations

PersistentField (com.ramussoft.common.persistent.PersistentField)27 PersistentRow (com.ramussoft.common.persistent.PersistentRow)25 PersistentWrapper (com.ramussoft.common.persistent.PersistentWrapper)22 ArrayList (java.util.ArrayList)11 Persistent (com.ramussoft.common.persistent.Persistent)9 SQLException (java.sql.SQLException)8 PreparedStatement (java.sql.PreparedStatement)7 List (java.util.List)7 FindObject (com.ramussoft.common.attribute.FindObject)6 RowMapper (com.ramussoft.jdbc.RowMapper)5 ResultSet (java.sql.ResultSet)5 AttributePlugin (com.ramussoft.common.attribute.AttributePlugin)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