Search in sources :

Example 16 with PersistentWrapper

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

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

Example 18 with PersistentWrapper

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

the class UniversalPersistentFactory method updateInTransaction.

public void updateInTransaction(Object object, final boolean cached, Class clazz) throws SQLException {
    PersistentRow row = rowHash.get(clazz);
    PersistentWrapper wrapper = rowWrappers.get(clazz);
    List<PersistentField> fields = row.getFields();
    Object[] objects = new Object[fields.size()];
    StringBuffer set = new StringBuffer("UPDATE " + row.getTableName() + " SET ");
    StringBuffer key = new StringBuffer(" WHERE ");
    boolean firstP = true;
    boolean firstS = true;
    int k = 0;
    for (int i = 0; i < objects.length; i++) {
        PersistentField field = fields.get(i);
        if (field.isPrimary()) {
            if (firstP)
                firstP = false;
            else
                key.append(" AND ");
        } else {
            if (firstS)
                firstS = false;
            else
                set.append(", ");
        }
        if (field.isPrimary()) {
            key.append(field.getDatabaseName());
            key.append("=?");
        } else {
            set.append(field.getDatabaseName());
            set.append("=?");
        }
        if (!field.isPrimary()) {
            objects[k] = wrapper.getField(object, field.getName());
            k++;
        }
    }
    for (int i = 0; i < objects.length; i++) if (fields.get(i).isPrimary()) {
        objects[k] = wrapper.getField(object, fields.get(i).getName());
        k++;
    }
    String sql = set.toString() + key.toString();
    PreparedStatement ps = template.getPreparedStatement(sql, cached);
    synchronized (ps) {
        template.setParams(ps, objects);
        ps.execute();
        if (!cached)
            ps.close();
    }
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) PreparedStatement(java.sql.PreparedStatement) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 19 with PersistentWrapper

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

the class UniversalPersistentFactory method delete.

public void delete(Object object) {
    Class clazz = object.getClass();
    PersistentRow row = rowHash.get(clazz);
    PersistentWrapper wrapper = rowWrappers.get(clazz);
    List<PersistentField> fields = row.getKeyFields();
    Object[] objects = new Object[fields.size()];
    StringBuffer delete = new StringBuffer("DELETE FROM " + row.getTableName() + " WHERE ");
    boolean first = true;
    for (int i = 0; i < objects.length; i++) {
        PersistentField field = fields.get(i);
        if (first)
            first = false;
        else {
            delete.append(" AND ");
        }
        delete.append(field.getDatabaseName());
        delete.append("=?");
        objects[i] = wrapper.getField(object, field.getName());
    }
    String sql = delete.toString();
    template.update(sql, objects, true);
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 20 with PersistentWrapper

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

the class UniversalPersistentFactory method update.

public Object update(Object object, boolean cached, JDBCCallback callback, Class clazz) {
    if (object instanceof List)
        return updateList((List) object, cached, callback);
    PersistentRow row = rowHash.get(clazz);
    PersistentWrapper wrapper = rowWrappers.get(clazz);
    List<PersistentField> fields = row.getFields();
    Object[] objects = new Object[fields.size()];
    StringBuffer set = new StringBuffer("UPDATE " + row.getTableName() + " SET ");
    StringBuffer key = new StringBuffer(" WHERE ");
    boolean firstP = true;
    boolean firstS = true;
    int k = 0;
    for (int i = 0; i < objects.length; i++) {
        PersistentField field = fields.get(i);
        if (field.isPrimary()) {
            if (firstP)
                firstP = false;
            else
                key.append(" AND ");
        } else {
            if (firstS)
                firstS = false;
            else
                set.append(", ");
        }
        if (field.isPrimary()) {
            key.append(field.getDatabaseName());
            key.append("=?");
        } else {
            set.append(field.getDatabaseName());
            set.append("=?");
        }
        if (!field.isPrimary()) {
            objects[k] = wrapper.getField(object, field.getName());
            k++;
        }
    }
    for (int i = 0; i < objects.length; i++) if (fields.get(i).isPrimary()) {
        objects[k] = wrapper.getField(object, fields.get(i).getName());
        k++;
    }
    String sql = set.toString() + key.toString();
    return template.update(sql, objects, cached, callback);
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) ArrayList(java.util.ArrayList) List(java.util.List) 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