Search in sources :

Example 6 with Util

use of com.ramussoft.eval.Util in project ramus by Vitaliy-Yakovchuk.

the class EvalQualifierSetupEditor method load.

@SuppressWarnings("unchecked")
@Override
public void load(Engine engine, Qualifier qualifier) {
    this.engine = engine;
    this.qualifier = qualifier;
    Element element = StandardAttributesPlugin.getElement(engine, qualifier.getId());
    List<FunctionPersistent> list = EvalPlugin.getFunctionAttribute(engine, element);
    if (list == null)
        list = new ArrayList<FunctionPersistent>(0);
    Util utils = Util.getUtils(engine);
    Hashtable<Attribute, ArrayList<Attribute>> attrs = new Hashtable<Attribute, ArrayList<Attribute>>();
    int lentgh = 0;
    for (Attribute attr : qualifier.getAttributes()) {
        if (attr.getAttributeType().toString().equals("Core.Table")) {
            ArrayList<Attribute> aList = new ArrayList<Attribute>();
            attrs.put(attr, aList);
            Qualifier q = StandardAttributesPlugin.getTableQualifierForAttribute(engine, attr);
            aList.addAll(q.getAttributes());
            lentgh += q.getAttributes().size();
        } else {
            attrs.put(attr, new ArrayList<Attribute>(0));
            lentgh++;
        }
    }
    functions = new FunctionPersistent[lentgh];
    names = new String[lentgh];
    attributes = new Attribute[lentgh];
    tableAttributes = new Attribute[lentgh];
    int i = 0;
    for (Attribute attribute : attrs.keySet()) {
        if (attribute.getAttributeType().toString().equals("Core.Table")) {
            for (Attribute tableAttribute : attrs.get(attribute)) {
                attributes[i] = attribute;
                tableAttributes[i] = tableAttribute;
                names[i] = Util.toCanonicalValue(attribute.getName() + "." + tableAttribute.getName());
                functions[i] = getFunction(list, attribute, tableAttribute, utils);
                i++;
            }
        } else {
            attributes[i] = attribute;
            names[i] = Util.toCanonicalValue(attribute.getName());
            functions[i] = getFunction(list, attribute, null, utils);
            i++;
        }
    }
    if (this.qualifier != null) {
        this.fireTableDataChanged();
    }
}
Also used : Attribute(com.ramussoft.common.Attribute) Hashtable(java.util.Hashtable) Element(com.ramussoft.common.Element) ArrayList(java.util.ArrayList) Util(com.ramussoft.eval.Util) Qualifier(com.ramussoft.common.Qualifier) FunctionPersistent(com.ramussoft.eval.FunctionPersistent)

Example 7 with Util

use of com.ramussoft.eval.Util in project ramus by Vitaliy-Yakovchuk.

the class SetFormulaDialog method onOk.

@Override
protected void onOk() {
    if (editor.getText().equals("")) {
        info.setFormula(null);
        ((Journaled) engine).startUserTransaction();
        engine.setCalculateInfo(info);
        ((Journaled) engine).commitUserTransaction();
        super.onOk();
        return;
    }
    Util utils = Util.getUtils(engine);
    try {
        try {
            info.setFormula(utils.compile(editor.getText(), engine.getElement(info.getElementId()), engine.getAttribute(info.getAttributeId())));
        } catch (UnknownValuesException e) {
            StringBuffer sb = new StringBuffer();
            sb.append("<html><body>");
            for (String value : e.getValues()) {
                sb.append(MessageFormat.format(GlobalResourcesManager.getString("Eval.UnknowValue"), value));
                sb.append("<br>");
            }
            sb.append("</body></html>");
            JOptionPane.showMessageDialog(null, sb.toString());
        }
        Eval eval = new Eval(info.getFormula());
        List<String> rec = new ArrayList<String>();
        for (String function : eval.getFunctions()) if (!utils.isFunctionExists(function)) {
            JOptionPane.showMessageDialog(null, MessageFormat.format(GlobalResourcesManager.getString("Eval.UnknowFunction"), function));
            return;
        }
        rec.add(utils.toValue(info.getElementId(), info.getAttributeId()));
        if (recCheck(rec, eval, utils)) {
            JOptionPane.showMessageDialog(null, GlobalResourcesManager.getString("Recursive.Link"));
        } else {
            ((Journaled) engine).startUserTransaction();
            engine.setCalculateInfo(info);
            ((Journaled) engine).commitUserTransaction();
            super.onOk();
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, MessageFormat.format(GlobalResourcesManager.getString("Eval.Error"), editor.getText()));
        return;
    }
}
Also used : Journaled(com.ramussoft.common.journal.Journaled) UnknownValuesException(com.ramussoft.eval.UnknownValuesException) ArrayList(java.util.ArrayList) Util(com.ramussoft.eval.Util) Eval(com.ramussoft.eval.Eval) UnknownValuesException(com.ramussoft.eval.UnknownValuesException)

Example 8 with Util

use of com.ramussoft.eval.Util in project ramus by Vitaliy-Yakovchuk.

the class EvalPlugin method calculate.

public static void calculate(Engine engine, Qualifier qualifier, List<FunctionPersistent> functions) {
    Util utils = Util.getUtils(engine);
    Eval[] evals = utils.sort(functions);
    List<Attribute> sources = new ArrayList<Attribute>();
    for (Eval eval : evals) {
        utils.addAttributes(sources, eval, qualifier.getAttributes());
    }
    Hashtable<Element, Object[]> hash = engine.getElements(qualifier, sources);
    for (Entry<Element, Object[]> entry : hash.entrySet()) {
        Element element = entry.getKey();
        Object[] values = entry.getValue();
        int i = 0;
        for (Eval eval : evals) {
            utils.fillAttributes(eval, element, sources, values, true);
            FunctionPersistent fp = functions.get(i);
            if (fp.getQualifierTableAttributeId() == -1l) {
                try {
                    Object object = utils.fillResult(fp.getQualifierAttributeId(), eval, element);
                    for (int j = 0; j < values.length; j++) {
                        if (sources.get(j).getId() == fp.getQualifierAttributeId())
                            values[j] = object;
                    }
                } catch (Exception e) {
                    utils.fillResult(fp.getQualifierAttributeId(), e, element);
                }
            } else {
                Attribute tableAttribute = engine.getAttribute(fp.getQualifierAttributeId());
                Qualifier tableQualifier = StandardAttributesPlugin.getTableQualifierForAttribute(engine, tableAttribute);
                List<Element> tableElements = StandardAttributesPlugin.getTableElements(engine, tableAttribute, element);
                List<Attribute> tableAttributes = utils.getTableAttributes(tableQualifier, fp.getQualifierAttributeId(), eval);
                for (Element tableElement : tableElements) {
                    for (Attribute attr : tableAttributes) {
                        Object value = engine.getAttribute(tableElement, attr);
                        eval.setValue(Util.tableAttributeToValue(fp.getQualifierAttributeId(), attr.getId()), new EObject(value, tableElement, attr, engine));
                    }
                    try {
                        utils.fillResult(fp.getQualifierTableAttributeId(), eval, tableElement);
                    } catch (Exception e) {
                        utils.fillResult(fp.getQualifierTableAttributeId(), e, tableElement);
                    }
                }
            }
            i++;
        }
    }
}
Also used : Attribute(com.ramussoft.common.Attribute) Element(com.ramussoft.common.Element) ArrayList(java.util.ArrayList) Util(com.ramussoft.eval.Util) SQLException(java.sql.SQLException) EObject(com.ramussoft.eval.EObject) EObject(com.ramussoft.eval.EObject) Qualifier(com.ramussoft.common.Qualifier) FunctionPersistent(com.ramussoft.eval.FunctionPersistent) Eval(com.ramussoft.eval.Eval)

Aggregations

Util (com.ramussoft.eval.Util)8 Element (com.ramussoft.common.Element)7 Eval (com.ramussoft.eval.Eval)7 FunctionPersistent (com.ramussoft.eval.FunctionPersistent)6 ArrayList (java.util.ArrayList)6 Attribute (com.ramussoft.common.Attribute)5 Qualifier (com.ramussoft.common.Qualifier)5 SQLException (java.sql.SQLException)4 EObject (com.ramussoft.eval.EObject)3 MetaValue (com.ramussoft.eval.MetaValue)2 Replacementable (com.ramussoft.eval.Replacementable)2 List (java.util.List)2 AttributeType (com.ramussoft.common.AttributeType)1 CalculateInfo (com.ramussoft.common.CalculateInfo)1 Engine (com.ramussoft.common.Engine)1 IEngine (com.ramussoft.common.IEngine)1 AttributeEvent (com.ramussoft.common.event.AttributeEvent)1 ElementAdapter (com.ramussoft.common.event.ElementAdapter)1 ElementAttributeListener (com.ramussoft.common.event.ElementAttributeListener)1 ElementEvent (com.ramussoft.common.event.ElementEvent)1