Search in sources :

Example 26 with Attribute

use of com.ramussoft.common.Attribute 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 27 with Attribute

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

the class IEngineImpl method getAttributePropertyWhatWillBeDeleted.

@Override
public Transaction getAttributePropertyWhatWillBeDeleted(long attributeId) {
    throwExaptionIfNotCan(getAccessor().canDeleteAttribute(attributeId), "Can not get attributes what will be deleted, as can not delete element.");
    Attribute attribute = getAttribute(attributeId);
    return getAttributeWhatWillBeDeleted(-1, attribute, true);
}
Also used : Attribute(com.ramussoft.common.Attribute)

Example 28 with Attribute

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

Example 29 with Attribute

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

the class EvalPlugin method replaceElements.

@Override
public void replaceElements(Engine engine, Element[] oldElements, Element newElement) {
    Hashtable<Long, Qualifier> qualifiers = new Hashtable<Long, Qualifier>();
    Qualifier main = getQualifier(newElement.getQualifierId(), qualifiers, engine);
    for (Element element : oldElements) {
        Qualifier qualifier = getQualifier(element.getQualifierId(), qualifiers, engine);
        for (Attribute attr : qualifier.getAttributes()) {
            for (CalculateInfo info : engine.getDependences(element.getId(), attr.getId(), false)) {
                Eval eval = new Eval(info.getFormula());
                final String old = Util.ELEMENT_PREFIX + element.getId() + Util.ATTRIBUTE_PREFIX + attr.getId();
                Attribute attribute = null;
                for (Attribute a : main.getAttributes()) if (a.getId() == attr.getId())
                    attribute = a;
                final String newValue;
                if (attribute == null)
                    newValue = "NULL";
                else
                    newValue = Util.ELEMENT_PREFIX + newElement.getId() + Util.ATTRIBUTE_PREFIX + attribute.getId();
                eval.replaceValueNames(new Replacementable() {

                    @Override
                    public String getNewName(String oldName) {
                        if (oldName.equals(old))
                            return newValue;
                        return null;
                    }
                });
                info.setFormula(eval.toString());
                engine.setCalculateInfo(info);
            }
        }
    }
}
Also used : Attribute(com.ramussoft.common.Attribute) Hashtable(java.util.Hashtable) CalculateInfo(com.ramussoft.common.CalculateInfo) Element(com.ramussoft.common.Element) Qualifier(com.ramussoft.common.Qualifier) Replacementable(com.ramussoft.eval.Replacementable) Eval(com.ramussoft.eval.Eval)

Example 30 with Attribute

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

the class EvalPlugin method recalculateFunctionOfQualifier.

@SuppressWarnings("unchecked")
private void recalculateFunctionOfQualifier(final Engine engine, AttributeEvent event) {
    Qualifier q = StandardAttributesPlugin.getQualifier(engine, event.getElement());
    List<Attribute> attrs = new ArrayList<Attribute>();
    attrs.add(functionDependenceAttribute);
    attrs.add(functionDependenceQualifier);
    Hashtable<Element, Object[]> deps = engine.getElements(functionDependences, attrs);
    for (Attribute qa : q.getAttributes()) {
        for (Entry<Element, Object[]> entry : deps.entrySet()) {
            Object[] value = entry.getValue();
            if ((((((Long) value[0]).longValue() == qa.getId()) || ((Long) value[0]).longValue() == -1l)) && (((Long) value[1]).longValue() == q.getId())) {
                engine.deleteElement(entry.getKey().getId());
            }
        }
    }
    if (event.getNewValue() != null) {
        Util utils = Util.getUtils(engine);
        List<FunctionPersistent> list = (List<FunctionPersistent>) event.getNewValue();
        for (FunctionPersistent fp : list) {
            if (fp.getAutochange() != 0) {
                Eval eval = new Eval(fp.getFunction());
                for (MetaValue value : utils.toMetaValues(eval.getValues())) {
                    if (value != null) {
                        Element element = engine.createElement(functionDependences.getId());
                        engine.setAttribute(element, functionDependence, value.getElementId());
                        engine.setAttribute(element, functionDependenceSourceAttribute, value.getAttributeId());
                        engine.setAttribute(element, functionDependenceAttribute, fp.getQualifierAttributeId());
                        engine.setAttribute(element, functionDependenceQualifier, q.getId());
                    }
                }
                recalculateQualifierAttribute(engine, q, utils, fp);
            }
        }
    }
}
Also used : MetaValue(com.ramussoft.eval.MetaValue) Attribute(com.ramussoft.common.Attribute) Element(com.ramussoft.common.Element) ArrayList(java.util.ArrayList) Util(com.ramussoft.eval.Util) Qualifier(com.ramussoft.common.Qualifier) EObject(com.ramussoft.eval.EObject) ArrayList(java.util.ArrayList) List(java.util.List) FunctionPersistent(com.ramussoft.eval.FunctionPersistent) Eval(com.ramussoft.eval.Eval)

Aggregations

Attribute (com.ramussoft.common.Attribute)203 Qualifier (com.ramussoft.common.Qualifier)72 Element (com.ramussoft.common.Element)70 ArrayList (java.util.ArrayList)53 Engine (com.ramussoft.common.Engine)32 List (java.util.List)20 Row (com.ramussoft.database.common.Row)19 Hashtable (java.util.Hashtable)19 SQLException (java.sql.SQLException)16 AttributeType (com.ramussoft.common.AttributeType)15 FindObject (com.ramussoft.common.attribute.FindObject)11 AttributeEvent (com.ramussoft.common.event.AttributeEvent)11 ResultSet (java.sql.ResultSet)11 AttributePlugin (com.ramussoft.gui.common.AttributePlugin)10 AccessRules (com.ramussoft.common.AccessRules)9 Transaction (com.ramussoft.common.persistent.Transaction)9 HierarchicalPersistent (com.ramussoft.core.attribute.simple.HierarchicalPersistent)9 RowMapper (com.ramussoft.jdbc.RowMapper)9 Row (com.ramussoft.pb.Row)9 ImageIcon (javax.swing.ImageIcon)9