Search in sources :

Example 21 with Persistent

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

Example 22 with Persistent

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

the class BooleanPlugin method getAttributeConverter.

@Override
public AttributeConverter getAttributeConverter() {
    return new AbstractAttributeConverter() {

        @SuppressWarnings("unchecked")
        @Override
        public List<Persistent>[] toPersistens(Object object, long elementId, long attributeId, IEngine engine) {
            if (object == null)
                return new List[] { new ArrayList<Persistent>(0) };
            ArrayList<Persistent> l = new ArrayList<Persistent>(1);
            l.add(new BooleanPersistent(((Boolean) object) ? 1 : 0));
            return new List[] { l };
        }

        @SuppressWarnings("unchecked")
        @Override
        public Object toObject(List<Persistent>[] persistents, long elementId, long attributeId, IEngine engine) {
            List l = persistents[0];
            if (l.size() == 0)
                return null;
            BooleanPersistent p = (BooleanPersistent) l.get(0);
            return p.getValue() != 0;
        }
    };
}
Also used : IEngine(com.ramussoft.common.IEngine) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Persistent(com.ramussoft.common.persistent.Persistent) AbstractAttributeConverter(com.ramussoft.common.attribute.AbstractAttributeConverter)

Example 23 with Persistent

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

the class IEngineImpl method getEListFixed.

private List<Persistent>[] getEListFixed(final PersistentRow row, final Class<? extends Persistent> clazz, final PersistentWrapper wrapper, ArrayList<String> paramFields, ArrayList<Object> params, long branchId) {
    String sqlBranchCondition = " AND value_branch_id<=? " + "AND (removed_branch_id > ? OR removed_branch_id is NULL)";
    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("=?");
    }
    sb.append(sqlBranchCondition);
    params.add(branchId);
    params.add(branchId);
    return new 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(), true) };
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) SQLException(java.sql.SQLException) Persistent(com.ramussoft.common.persistent.Persistent) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) FindObject(com.ramussoft.common.attribute.FindObject) RowMapper(com.ramussoft.jdbc.RowMapper)

Example 24 with Persistent

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

the class IEngineImpl method getBinaryAttribute.

@Override
@SuppressWarnings("unchecked")
public List<Persistent>[] getBinaryAttribute(long elementId, long attributeId) {
    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>(2);
        ArrayList<String> paramFields = new ArrayList<String>(2);
        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());
        }
        long branchId = 0l;
        try {
            branchId = getBranch(prefix + "attributes_data_metadata", new Object[] { "element_id", "attribute_id" }, new Object[] { elementId, attributeId }, getActiveBranchId());
        } catch (SQLException e1) {
            e1.printStackTrace();
            throw new RuntimeException(e1);
        }
        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 25 with Persistent

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

the class IEngineImpl method executeTransaction.

private void executeTransaction(final Transaction transaction, Connection connection) throws SQLException {
    for (Persistent key : transaction.getUpdate()) {
        PersistentRow row = metadata.get(key.getClass());
        PersistentWrapper wrapper = wrappers.get(key.getClass());
        StringBuffer sb = new StringBuffer("UPDATE " + row.getTableName() + " SET ");
        boolean first = true;
        List<Object> list = new ArrayList<Object>(5);
        for (PersistentField field : row.getDataFields()) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }
            sb.append(field.getDatabaseName());
            sb.append("=?");
            list.add(wrapper.getField(key, field.getName()));
        }
        sb.append(" WHERE ");
        first = true;
        for (PersistentField field : row.getKeyFields()) {
            if (first) {
                first = false;
            } else {
                sb.append(" AND ");
            }
            sb.append(field.getDatabaseName());
            sb.append("=?");
            list.add(wrapper.getField(key, field.getName()));
        }
        PreparedStatement ps = template.getPreparedStatement(sb.toString(), true);
        template.setParams(ps, list.toArray(new Object[list.size()]));
        ps.execute();
    // ps.close();
    }
    for (Persistent key : transaction.getDelete()) {
        PersistentRow row = metadata.get(key.getClass());
        PersistentWrapper wrapper = wrappers.get(key.getClass());
        StringBuffer sb = new StringBuffer("DELETE FROM " + row.getTableName() + " WHERE ");
        boolean first = true;
        first = true;
        List<Object> list = new ArrayList<Object>(2);
        for (PersistentField field : row.getKeyFields()) {
            if (first) {
                first = false;
            } else {
                sb.append("AND ");
            }
            sb.append(field.getDatabaseName());
            sb.append("=?");
            list.add(wrapper.getField(key, field.getName()));
        }
        PreparedStatement ps = template.getPreparedStatement(sb.toString(), // connection.prepareStatement(sb.toString());
        true);
        template.setParams(ps, list.toArray(new Object[list.size()]));
        ps.execute();
    // ps.close();
    }
    for (Persistent key : transaction.getSave()) {
        PersistentRow row = metadata.get(key.getClass());
        PersistentWrapper wrapper = wrappers.get(key.getClass());
        StringBuffer sb = new StringBuffer("INSERT INTO " + row.getTableName() + "(");
        boolean first = true;
        List<Object> list = new ArrayList<Object>(5);
        for (PersistentField field : row.getFields()) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }
            sb.append(field.getDatabaseName());
            list.add(wrapper.getField(key, field.getName()));
        }
        sb.append(") VALUES(");
        first = true;
        for (int i = 0; i < list.size(); i++) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }
            sb.append("?");
        }
        sb.append(")");
        PreparedStatement ps = template.getPreparedStatement(sb.toString(), // connection.prepareStatement(sb.toString());
        true);
        template.setParams(ps, list.toArray(new Object[list.size()]));
        ps.execute();
    // ps.close();
    }
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) ArrayList(java.util.ArrayList) Persistent(com.ramussoft.common.persistent.Persistent) FindObject(com.ramussoft.common.attribute.FindObject) PreparedStatement(java.sql.PreparedStatement) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Aggregations

Persistent (com.ramussoft.common.persistent.Persistent)27 ArrayList (java.util.ArrayList)15 List (java.util.List)13 PersistentField (com.ramussoft.common.persistent.PersistentField)9 PersistentWrapper (com.ramussoft.common.persistent.PersistentWrapper)9 PersistentRow (com.ramussoft.common.persistent.PersistentRow)8 Attribute (com.ramussoft.common.Attribute)6 IEngine (com.ramussoft.common.IEngine)6 FindObject (com.ramussoft.common.attribute.FindObject)6 Transaction (com.ramussoft.common.persistent.Transaction)6 AbstractAttributeConverter (com.ramussoft.common.attribute.AbstractAttributeConverter)5 RowMapper (com.ramussoft.jdbc.RowMapper)5 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 AttributePlugin (com.ramussoft.common.attribute.AttributePlugin)4 SimpleAttributeConverter (com.ramussoft.core.attribute.simple.SimpleAttributeConverter)3 Element (com.ramussoft.common.Element)2 Qualifier (com.ramussoft.common.Qualifier)2 ProjectOptions (com.dsoft.pb.idef.elements.ProjectOptions)1 Readed (com.dsoft.pb.idef.elements.Readed)1