Search in sources :

Example 21 with PersistentWrapper

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

the class IEngineImpl method setElistFix.

private void setElistFix(Transaction transaction) {
    long branchId = getActiveBranchId();
    String delete = "DELETE FROM " + prefix + "attribute_element_lists WHERE attribute_id=? AND element1_id=? AND element2_id = ? AND value_branch_id=?";
    String remove = "UPDATE " + prefix + "attribute_element_lists SET connection_type=?, removed_branch_id=? WHERE attribute_id=? AND element1_id=? AND element2_id = ? AND value_branch_id=?";
    String insert = "INSERT INTO " + prefix + "attribute_element_lists (connection_type, attribute_id, element1_id, element2_id, value_branch_id) VALUES (?, ?, ?, ?, ?)";
    for (Persistent p : transaction.getDelete()) {
        Class<? extends Persistent> clazz = p.getClass();
        PersistentWrapper wrapper = wrappers.get(clazz);
        long bId = (Long) wrapper.getField(p, "valueBranchId");
        long aId = (Long) wrapper.getField(p, "attributeId");
        long e1Id = (Long) wrapper.getField(p, "element1Id");
        long e2Id = (Long) wrapper.getField(p, "element2Id");
        String connectionType = (String) wrapper.getField(p, "connectionType");
        if (bId == branchId) {
            template.update(delete, new Object[] { aId, e1Id, e2Id, bId }, false);
        } else {
            template.update(remove, new Object[] { connectionType, branchId, aId, e1Id, e2Id, bId }, false);
        }
    }
    for (Persistent p : transaction.getSave()) {
        Class<? extends Persistent> clazz = p.getClass();
        PersistentWrapper wrapper = wrappers.get(clazz);
        long aId = (Long) wrapper.getField(p, "attributeId");
        long e1Id = (Long) wrapper.getField(p, "element1Id");
        long e2Id = (Long) wrapper.getField(p, "element2Id");
        String connectionType = (String) wrapper.getField(p, "connectionType");
        template.update(insert, new Object[] { connectionType, aId, e1Id, e2Id, branchId }, false);
    }
}
Also used : Persistent(com.ramussoft.common.persistent.Persistent) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 22 with PersistentWrapper

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

the class BranchesPersistentFactory method getObject.

public Object getObject(Object primaryKey) {
    Class clazz = primaryKey.getClass();
    long id = getActiveBranchId();
    PersistentRow row = rowHash.get(clazz);
    PersistentWrapper wrapper = rowWrappers.get(clazz);
    List<PersistentField> fields = row.getKeyFields();
    Object[] objects = new Object[fields.size()];
    StringBuffer select = new StringBuffer("SELECT * 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 {
            select.append(" AND ");
        }
        select.append(field.getDatabaseName());
        select.append("=?");
        objects[i] = wrapper.getField(primaryKey, field.getName());
    }
    select.append("AND");
    select.append(getSQLBranchCondition());
    objects = addBranchWhereObjects(id, objects);
    return template.queryForObjects(select.toString(), new Mapper(clazz), objects, true);
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 23 with PersistentWrapper

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

the class BranchesPersistentFactory method updateInTransaction.

@Override
public void updateInTransaction(Object object, 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;
    int keyCount = 0;
    for (int i = 0; i < objects.length; i++) {
        PersistentField field = fields.get(i);
        if (field.isPrimary()) {
            keyCount++;
            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() + " AND created_branch_id=?";
    PreparedStatement ps = template.getPreparedStatement(sql, cached);
    synchronized (ps) {
        template.setParams(ps, objects);
        long id = getActiveBranchId();
        ps.setLong(objects.length + 1, id);
        if (ps.executeUpdate() == 0) {
            if (!cached)
                ps.close();
            sql = "UPDATE " + row.getTableName() + " SET removed_branch_id=?" + key.toString() + " AND " + getSQLBranchCondition();
            ps = template.getPreparedStatement(sql, cached);
            ps.setLong(1, id);
            for (int i = objects.length - keyCount; i < objects.length; i++) {
                template.setParam(ps, i + 2 - objects.length + keyCount, objects[i]);
            }
            Object[] objects2 = addBranchWhereObjects(id, new Object[] {});
            for (int i = 0; i < objects2.length; i++) template.setParam(ps, keyCount + 2 + i, objects2[i]);
            ps.executeUpdate();
            if (!cached)
                ps.close();
            StringBuffer insert = new StringBuffer("INSERT INTO " + row.getTableName() + " (");
            StringBuffer values = new StringBuffer(") VALUES (");
            boolean first = true;
            for (int i = 0; i < objects.length; i++) {
                PersistentField field = fields.get(i);
                if (first)
                    first = false;
                else {
                    insert.append(", ");
                    values.append(", ");
                }
                insert.append(field.getDatabaseName());
                values.append('?');
                objects[i] = wrapper.getField(object, field.getName());
            }
            insert.append(", created_branch_id");
            values.append(", ?");
            sql = insert.toString() + values.toString() + ")";
            ps = template.getPreparedStatement(sql, cached);
            template.setParams(ps, objects);
            ps.setLong(objects.length + 1, id);
            ps.executeUpdate();
            if (!cached)
                ps.close();
        } else 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 24 with PersistentWrapper

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

the class BranchesPersistentFactory method deleteInTransaction.

@Override
public void deleteInTransaction(Object object) throws SQLException {
    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("UPDATE " + row.getTableName() + " SET removed_branch_id=? 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());
    }
    delete.append(" AND ");
    delete.append(getSQLBranchCondition());
    String sql = delete.toString();
    PreparedStatement ps = template.getPreparedStatement(sql, true);
    long id = getActiveBranchId();
    ps.setLong(1, id);
    for (int i = 0; i < objects.length; i++) template.setParam(ps, i + 2, objects[i]);
    Object[] objects2 = addBranchWhereObjects(id, new Object[] {});
    for (int i = 0; i < objects2.length; i++) {
        template.setParam(ps, objects.length + i + 2, objects2[i]);
    }
    ps.executeUpdate();
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) PreparedStatement(java.sql.PreparedStatement) 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