Search in sources :

Example 1 with Transaction

use of com.ramussoft.common.persistent.Transaction 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 Transaction

use of com.ramussoft.common.persistent.Transaction 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 3 with Transaction

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

the class IEngineImpl method setElementQualifier.

@Override
public void setElementQualifier(final long elementId, final long qualifierId) {
    throwExaptionIfNotCan(getAccessor().canDeleteElements(new long[] { elementId }), "Can not update element's qualifier.");
    throwExaptionIfNotCan(getAccessor().canCreateElement(qualifierId), "Can not update element's qualifier.");
    Element element = getElement(elementId);
    Qualifier current = getQualifier(element.getQualifierId());
    Qualifier newQualifier = getQualifier(qualifierId);
    final List<Attribute> attrs = new ArrayList<Attribute>();
    addNotPresentAttributes(current.getAttributes(), newQualifier.getAttributes(), attrs);
    addNotPresentAttributes(current.getSystemAttributes(), newQualifier.getSystemAttributes(), attrs);
    template.execute(new JDBCCallback() {

        @Override
        public Object execute(Connection connection) throws SQLException {
            Transaction[] transactions = getAttributesWhatWillBeDeleted(elementId, attrs);
            for (Transaction transaction : transactions) {
                executeTransaction(transaction, connection);
            }
            PreparedStatement ps = connection.prepareStatement("UPDATE " + prefix + "elements SET QUALIFIER_ID=? WHERE ELEMENT_ID=?");
            ps.setLong(1, qualifierId);
            ps.setLong(2, elementId);
            ps.execute();
            ps.close();
            return null;
        }
    });
}
Also used : Transaction(com.ramussoft.common.persistent.Transaction) Attribute(com.ramussoft.common.Attribute) SQLException(java.sql.SQLException) Element(com.ramussoft.common.Element) ArrayList(java.util.ArrayList) JDBCCallback(com.ramussoft.jdbc.JDBCCallback) Connection(java.sql.Connection) Qualifier(com.ramussoft.common.Qualifier) FindObject(com.ramussoft.common.attribute.FindObject) PreparedStatement(java.sql.PreparedStatement)

Example 4 with Transaction

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

the class IEngineImpl method deleteElement.

@Override
public void deleteElement(final long id) {
    throwExaptionIfNotCan(getAccessor().canDeleteElements(new long[] { id }), "Can not delete element.");
    template.execute(new JDBCCallback() {

        @Override
        public Object execute(Connection connection) throws SQLException {
            Transaction[] transactions = getAttributeWhatWillBeDeleted(id);
            for (Transaction transaction : transactions) {
                executeTransaction(transaction, connection);
            }
            PreparedStatement ps;
            ps = connection.prepareStatement("DELETE FROM " + prefix + "formula_dependences WHERE source_element_id=?");
            ps.setLong(1, id);
            ps.execute();
            ps.close();
            ps = connection.prepareStatement("DELETE FROM " + prefix + "formulas WHERE element_id=?");
            ps.setLong(1, id);
            ps.execute();
            ps.close();
            ps = connection.prepareStatement("UPDATE " + prefix + "elements SET removed_branch_id=? WHERE ELEMENT_ID=?");
            long branch = getActiveBranchId();
            ps.setLong(1, branch);
            ps.setLong(2, id);
            ps.execute();
            ps.close();
            return null;
        }
    });
}
Also used : Transaction(com.ramussoft.common.persistent.Transaction) SQLException(java.sql.SQLException) JDBCCallback(com.ramussoft.jdbc.JDBCCallback) Connection(java.sql.Connection) FindObject(com.ramussoft.common.attribute.FindObject) PreparedStatement(java.sql.PreparedStatement)

Example 5 with Transaction

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

the class IEngineImpl method getAttributesWhatWillBeDeleted.

@Override
public Transaction[] getAttributesWhatWillBeDeleted(long elementId, List<Attribute> attrs) {
    throwExaptionIfNotCan(getAccessor().canDeleteElements(new long[] { elementId }), "Can not get attributes what will be deleted, as you can not delete element.");
    Transaction[] res = new Transaction[attrs.size()];
    for (int i = 0; i < attrs.size(); i++) {
        Attribute attribute = attrs.get(i);
        res[i] = getAttributeWhatWillBeDeleted(elementId, attribute, false);
    }
    return res;
}
Also used : Transaction(com.ramussoft.common.persistent.Transaction) Attribute(com.ramussoft.common.Attribute)

Aggregations

Transaction (com.ramussoft.common.persistent.Transaction)21 Attribute (com.ramussoft.common.Attribute)9 Persistent (com.ramussoft.common.persistent.Persistent)6 ArrayList (java.util.ArrayList)6 Element (com.ramussoft.common.Element)5 FindObject (com.ramussoft.common.attribute.FindObject)5 SQLException (java.sql.SQLException)5 JDBCCallback (com.ramussoft.jdbc.JDBCCallback)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 Qualifier (com.ramussoft.common.Qualifier)3 Hashtable (java.util.Hashtable)3 ElementEvent (com.ramussoft.common.event.ElementEvent)2 PersistentField (com.ramussoft.common.persistent.PersistentField)2 PersistentRow (com.ramussoft.common.persistent.PersistentRow)2 PersistentWrapper (com.ramussoft.common.persistent.PersistentWrapper)2 List (java.util.List)2 Engine (com.ramussoft.common.Engine)1 IEngine (com.ramussoft.common.IEngine)1 AbstractAttributeConverter (com.ramussoft.common.attribute.AbstractAttributeConverter)1