Search in sources :

Example 11 with Element

use of com.ramussoft.common.Element 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 12 with Element

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

the class IEngineImpl method getElements.

@SuppressWarnings("unchecked")
@Override
public List<Element> getElements(long qualifierId) {
    throwExaptionIfNotCan(getAccessor().canReadQualifier(qualifierId), "Can not get qualifier.");
    Qualifier qualifier = getQualifier(qualifierId);
    if (qualifier == null)
        return new ArrayList<Element>(0);
    long attr = qualifier.getAttributeForName();
    String where = " AND attribute_id=" + attr + " AND elmts.element_id=element_id";
    long branchId = getActiveBranchId();
    return template.query("SELECT elmts.ELEMENT_ID, elmts.QUALIFIER_ID, (SELECT value FROM " + prefix + "attribute_texts WHERE element_id=elmts.element_id AND attribute_id=" + attr + " " + " AND (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))) AS ELEMENT_TEXT_NAME FROM " + prefix + "elements elmts, " + prefix + "qualifiers q WHERE elmts.QUALIFIER_ID=? AND q.qualifier_id=elmts.qualifier_id AND elmts.created_branch_id <=? " + "AND (elmts.removed_branch_id >?) ORDER BY ELEMENT_ID", new ElementRowMapper(), new Object[] { branchId, branchId, qualifierId, branchId, branchId }, true);
}
Also used : Element(com.ramussoft.common.Element) Qualifier(com.ramussoft.common.Qualifier)

Example 13 with Element

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

Example 14 with Element

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

the class IEngineImpl method createElement.

@Override
public Element createElement(long qualifierId, long elementId) {
    throwExaptionIfNotCan(getAccessor().canCreateElement(qualifierId), "Can not create element.");
    if (elementId == -1) {
        elementId = nextValue("elements_sequence");
        long id = template.queryForLong("SELECT MAX(ELEMENT_ID) FROM " + prefix + "elements;");
        while (id >= elementId) {
            elementId = template.nextVal(prefix + "elements_sequence");
        }
    }
    Element element = new Element(elementId, qualifierId, "");
    element.setId(elementId);
    long branchId = getActiveBranchId();
    Long l = (Long) template.queryForObjects("SELECT COUNT(*) FROM " + prefix + "elements WHERE element_id=? AND removed_branch_id=?", new RowMapper() {

        @Override
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
            return rs.getLong(1);
        }
    }, new Object[] { elementId, branchId }, false);
    if (l.longValue() == 0)
        template.update("INSERT INTO " + prefix + "elements (ELEMENT_ID, ELEMENT_NAME, QUALIFIER_ID, created_branch_id) VALUES (?, ?, ?, ?)", new Object[] { elementId, element.getName(), qualifierId, branchId }, true);
    else {
        template.update("UPDATE " + prefix + "elements SET removed_branch_id=? WHERE ELEMENT_ID=? AND removed_branch_id=?", new Object[] { Integer.MAX_VALUE, elementId, branchId }, false);
    }
    return element;
}
Also used : Element(com.ramussoft.common.Element) ResultSet(java.sql.ResultSet) FindObject(com.ramussoft.common.attribute.FindObject) RowMapper(com.ramussoft.jdbc.RowMapper)

Example 15 with Element

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

the class IEngineImpl method getElement.

@Override
public Element getElement(long id) {
    throwExaptionIfNotCan(getAccessor().canReadQualifier(id), "Can not get element.");
    Qualifier qualifier = getQualifier(getQualifierIdForElement(id));
    if (qualifier == null)
        return null;
    long attr = qualifier.getAttributeForName();
    String where = " AND attribute_id=" + attr + " AND elmts.element_id=element_id";
    long branchId = getActiveBranchId();
    return (Element) template.queryForObjects("SELECT ELEMENT_ID, elmts.QUALIFIER_ID, (SELECT value FROM " + prefix + "attribute_texts WHERE element_id=elmts.element_id AND attribute_id=" + attr + " " + " AND (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))) AS ELEMENT_TEXT_NAME  FROM " + prefix + "elements elmts, " + prefix + "qualifiers q WHERE ELEMENT_ID=? AND elmts.qualifier_id=q.qualifier_id AND elmts.created_branch_id<= ? " + "AND elmts.removed_branch_id >?", new ElementRowMapper(), new Object[] { branchId, branchId, id, branchId, branchId }, true);
}
Also used : Element(com.ramussoft.common.Element) Qualifier(com.ramussoft.common.Qualifier) FindObject(com.ramussoft.common.attribute.FindObject)

Aggregations

Element (com.ramussoft.common.Element)142 Attribute (com.ramussoft.common.Attribute)70 Qualifier (com.ramussoft.common.Qualifier)59 ArrayList (java.util.ArrayList)48 Engine (com.ramussoft.common.Engine)27 SQLException (java.sql.SQLException)14 List (java.util.List)14 Hashtable (java.util.Hashtable)13 Row (com.ramussoft.database.common.Row)12 ElementEvent (com.ramussoft.common.event.ElementEvent)11 HierarchicalPersistent (com.ramussoft.core.attribute.simple.HierarchicalPersistent)11 Eval (com.ramussoft.eval.Eval)9 AttributeEvent (com.ramussoft.common.event.AttributeEvent)8 ElementListPersistent (com.ramussoft.core.attribute.simple.ElementListPersistent)8 Row (com.ramussoft.pb.Row)8 Journaled (com.ramussoft.common.journal.Journaled)7 FunctionPersistent (com.ramussoft.eval.FunctionPersistent)7 Util (com.ramussoft.eval.Util)7 EObject (com.ramussoft.eval.EObject)6 IOException (java.io.IOException)6