Search in sources :

Example 1 with QualifierAdapter

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

the class EvalPlugin method init.

@Override
public void init(final Engine engine, AccessRules rules) {
    super.init(engine, rules);
    Qualifier qualifier = StandardAttributesPlugin.getQualifiersQualifier(engine);
    engine.setPluginProperty(getName(), "Plugin", this);
    function = getFunction(qualifier);
    if (function == null) {
        function = engine.createSystemAttribute(new AttributeType(EVAL, "Function", false));
        function.setName(FUNCTION_ATTRIBUTE);
        engine.updateAttribute(function);
        qualifier.getSystemAttributes().add(function);
        engine.updateQualifier(qualifier);
    }
    functionDependences = engine.getSystemQualifier(QUALIFIER_EVAL_FUNCTION_DEPENDENCES);
    if (functionDependences == null) {
        createEvalObjects(engine);
    } else {
        functionDependence = engine.getSystemAttribute(ATTRIBUTE_EVAL_FUNCTION_DEPENDENCE);
        functionDependenceQualifier = engine.getSystemAttribute(ATTRIBUTE_EVAL_DEPENDENCE_QUALIFIER);
        functionDependenceAttribute = engine.getSystemAttribute(ATTRIBUTE_EVAL_DEPENDENCE_ATTRIBUTE);
        functionDependenceSourceAttribute = engine.getSystemAttribute(ATTRIBUTE_EVAL_FUNCTION_DEPENDENCE_SOURCE_ATTRIBUTE);
    }
    if (!StandardAttributesPlugin.isDisableAutoupdate(engine)) {
        final Util util = Util.getUtils(engine);
        util.getScriptHolders().addFunctionsChangeListener(new FunctionsChangeListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void functionsChanged(FunctionsChangeEvent event) {
                IEngine iEngine = engine.getDeligate();
                if ((iEngine != null) && (iEngine instanceof IEngineImpl)) {
                    JDBCTemplate template = ((IEngineImpl) iEngine).getTemplate();
                    String prefix = ((IEngineImpl) iEngine).getPrefix();
                    for (String function : event.getFunctionNames()) {
                        List<FunctionPersistent> list = template.query("SELECT function, qualifier_attribute_id, qualifier_table_attribute_id, autochange, attribute_id, element_id FROM " + prefix + "attribute_functions WHERE function LIKE ? AND autochange=1", new RowMapper() {

                            @Override
                            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                                FunctionPersistent persistent = new FunctionPersistent(rs.getString(1), rs.getLong(2), rs.getLong(3), rs.getInt(4));
                                persistent.setAttributeId(rs.getLong(5));
                                persistent.setElementId(rs.getLong(6));
                                return persistent;
                            }
                        }, new Object[] { "%" + function + "%" }, true);
                        for (FunctionPersistent fp : list) {
                            recalculateQualifierAttribute(engine, StandardAttributesPlugin.getQualifier(engine, engine.getElement(fp.getElementId())), util, fp);
                        }
                    }
                }
                for (String function : event.getFunctionNames()) {
                    for (CalculateInfo info : engine.findCalculateInfos("%" + function + "%", true)) {
                        recalculate(engine, info);
                    }
                }
            }
        });
        engine.addElementAttributeListener(null, new ElementAttributeListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void attributeChanged(AttributeEvent event) {
                if (event.isJournaled())
                    return;
                if (DISABLE_RECALC)
                    return;
                if (event.getElement() == null) {
                    IEngine d = engine.getDeligate();
                    List<Element> allElements = null;
                    if ((d != null) && (d instanceof IEngineImpl)) {
                        JDBCTemplate template = ((IEngineImpl) d).getTemplate();
                        String prefix = ((IEngineImpl) d).getPrefix();
                        allElements = template.query("SELECT * FROM " + prefix + "elements WHERE qualifier_id in\n" + "(SELECT qualifier_id FROM " + prefix + "qualifiers_attributes WHERE attribute_id=?)", new RowMapper() {

                            @Override
                            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                                return new Element(rs.getLong("element_id"), rs.getLong("qualifier_id"), rs.getString("element_name"));
                            }
                        }, new Object[] { event.getAttribute().getId() }, true);
                    } else {
                        Attribute attribute = event.getAttribute();
                        allElements = new ArrayList<Element>();
                        for (Qualifier qualifier : engine.getQualifiers()) {
                            if (qualifier.getAttributes().indexOf(attribute) >= 0) {
                                allElements.addAll(engine.getElements(qualifier.getId()));
                            }
                        }
                    }
                    Attribute attribute = event.getAttribute();
                    for (Element element : allElements) {
                        recalculateElement(engine, new AttributeEvent(engine, element, attribute, null, null));
                    }
                } else
                    recalculateElement(engine, event);
            }

            private void recalculateElement(final Engine engine, AttributeEvent event) {
                MetaValue metaValue = new MetaValue(event.getElement().getId(), event.getAttribute().getId());
                List<MetaValue> metaValueList = hashtable.get(Thread.currentThread());
                if (metaValueList == null) {
                    metaValueList = new ArrayList<MetaValue>();
                    hashtable.put(Thread.currentThread(), metaValueList);
                } else {
                    if (metaValueList.indexOf(metaValue) >= 0)
                        return;
                }
                metaValueList.add(metaValue);
                try {
                    for (CalculateInfo info : engine.getDependences(event.getElement().getId(), event.getAttribute().getId(), true)) {
                        recalculate(engine, info);
                    }
                    List<Element> elements = engine.findElements(functionDependences.getId(), functionDependence, event.getElement().getId());
                    for (Element e : elements) {
                        Qualifier qualifier = engine.getQualifier((Long) engine.getAttribute(e, functionDependenceQualifier));
                        Attribute attribute = engine.getAttribute((Long) engine.getAttribute(e, functionDependenceAttribute));
                        if ((qualifier != null) && (attribute != null)) {
                            Element el = StandardAttributesPlugin.getElement(event.getEngine(), qualifier.getId());
                            if (el != null)
                                for (Element child : engine.getElements(qualifier.getId())) {
                                    recalculateInQualifier(engine, Util.elementIdToValue(event.getElement().getId(), event.getAttribute().getId()), null, el, child);
                                }
                        }
                    }
                    Element element = StandardAttributesPlugin.getElement(event.getEngine(), event.getElement().getQualifierId());
                    if (element == null) {
                        Qualifier qualifier = engine.getQualifier(event.getElement().getQualifierId());
                        if (StandardAttributesPlugin.isTableQualifier(qualifier)) {
                            Element parent = StandardAttributesPlugin.getElementForTableElement(engine, event.getElement());
                            if (parent == null)
                                return;
                            Qualifier main = engine.getQualifier(parent.getQualifierId());
                            Attribute tableAttribute = null;
                            for (Attribute attr : main.getAttributes()) {
                                if (StandardAttributesPlugin.getTableQualifeirName(attr).equals(qualifier.getName())) {
                                    tableAttribute = attr;
                                    break;
                                }
                            }
                            if (tableAttribute == null)
                                return;
                            element = StandardAttributesPlugin.getElement(event.getEngine(), parent.getQualifierId());
                            if (element != null) {
                                if (event.getAttribute().equals(StandardAttributesPlugin.getTableElementIdAttribute(engine)))
                                    recalculateInQualifier(engine, null, event.getElement(), element, parent);
                                else
                                    recalculateInQualifier(engine, Util.tableAttributeToValue(tableAttribute.getId(), event.getAttribute().getId()), event.getElement(), element, parent);
                            }
                        }
                        return;
                    }
                    recalculateInQualifier(engine, Util.attributeIdToValue(event.getAttribute().getId()), null, element, event.getElement());
                } finally {
                    metaValueList.remove(metaValueList.size() - 1);
                }
            }
        });
        engine.addElementListener(null, new ElementAdapter() {

            @SuppressWarnings("unchecked")
            @Override
            public void elementCreated(ElementEvent event) {
                if (event.isJournaled())
                    return;
                Element element = StandardAttributesPlugin.getElement(event.getEngine(), event.getNewElement().getQualifierId());
                if (element == null) {
                    return;
                }
                List<FunctionPersistent> list = getFunctionAttribute(engine, element);
                if (list == null)
                    return;
                Util utils = Util.getUtils(engine);
                for (FunctionPersistent fp : list) if (fp.getAutochange() != 0) {
                    Eval eval = new Eval(fp.getFunction());
                    try {
                        utils.fillAttributes(eval, engine.getQualifier(event.getNewElement().getQualifierId()), event.getNewElement(), null, null, null, true);
                        utils.fillResult(fp.getQualifierAttributeId(), eval, event.getNewElement());
                    } catch (Exception e) {
                        utils.fillResult(fp.getQualifierAttributeId(), e, event.getNewElement());
                        if (e instanceof RuntimeException)
                            throw (RuntimeException) e;
                        throw new RuntimeException(e);
                    } finally {
                    }
                }
            }

            @SuppressWarnings("unchecked")
            @Override
            public void beforeElementDeleted(final ElementEvent event) {
                if ((event.isJournaled()) || (event.getNewElement() != null))
                    return;
                Element oldElement = event.getOldElement();
                Qualifier qualifier = engine.getQualifier(oldElement.getQualifierId());
                for (Attribute attribute : qualifier.getAttributes()) {
                    removeAttributeFromCalculateInfo(engine, oldElement, attribute);
                    CalculateInfo info = engine.getCalculateInfo(oldElement.getId(), attribute.getId());
                    if (info != null) {
                        info.setFormula(null);
                        engine.setCalculateInfo(info);
                    }
                }
                List<Element> elements = engine.findElements(functionDependences.getId(), functionDependence, oldElement.getId());
                Vector<Long> qualifiers = new Vector<Long>(elements.size());
                final String start = Util.ELEMENT_PREFIX + oldElement.getId();
                for (Element element : elements) {
                    Long q = (Long) engine.getAttribute(element, functionDependenceQualifier);
                    if (qualifiers.indexOf(q) < 0) {
                        qualifiers.add(q);
                        Element qElement = StandardAttributesPlugin.getElement(engine, q.longValue());
                        List<FunctionPersistent> fpl = (List<FunctionPersistent>) engine.getAttribute(qElement, function);
                        for (FunctionPersistent fp : fpl) {
                            try {
                                Eval eval = new Eval(fp.getFunction());
                                eval.replaceValueNames(new Replacementable() {

                                    @Override
                                    public String getNewName(String oldName) {
                                        if (oldName.startsWith(start))
                                            return "NULL";
                                        return oldName;
                                    }
                                });
                                fp.setFunction(eval.toString());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        engine.setAttribute(qElement, function, fpl);
                    }
                // engine.deleteElement(element.getId());
                }
            }
        });
        engine.addQualifierListener(new QualifierAdapter() {

            @SuppressWarnings("unchecked")
            @Override
            public void beforeQualifierUpdated(QualifierEvent event) {
                List<Attribute> rem = new ArrayList<Attribute>();
                for (Attribute a : event.getOldQualifier().getAttributes()) {
                    if (event.getNewQualifier().getAttributes().indexOf(a) < 0)
                        rem.add(a);
                }
                if (rem.size() > 0) {
                    List<Element> list = engine.getElements(event.getOldQualifier().getId());
                    for (Attribute attribute : rem) {
                        for (Element element : list) {
                            removeAttributeFromCalculateInfo(engine, element, attribute);
                            CalculateInfo info = engine.getCalculateInfo(element.getId(), attribute.getId());
                            if (info != null) {
                                info.setFormula(null);
                                engine.setCalculateInfo(info);
                            }
                        }
                        List<Element> elements = engine.findElements(functionDependences.getId(), functionDependenceSourceAttribute, attribute.getId());
                        final String end = Util.ATTRIBUTE_PREFIX + attribute.getId();
                        for (Element element : elements) {
                            Long id = (Long) engine.getAttribute(element, functionDependence);
                            if (engine.getQualifierIdForElement(id) == event.getNewQualifier().getId()) {
                                Long qId = (Long) engine.getAttribute(element, functionDependenceQualifier);
                                Element element2 = StandardAttributesPlugin.getElement(engine, qId);
                                List<FunctionPersistent> fpl = (List<FunctionPersistent>) engine.getAttribute(element2, function);
                                for (FunctionPersistent fp : fpl) {
                                    Eval eval = new Eval(fp.getFunction());
                                    eval.replaceValueNames(new Replacementable() {

                                        @Override
                                        public String getNewName(String oldName) {
                                            if (oldName.endsWith(end)) {
                                                return "NULL";
                                            }
                                            return oldName;
                                        }
                                    });
                                    fp.setFunction(eval.toString());
                                }
                                engine.setAttribute(element2, function, fpl);
                            }
                        }
                    }
                }
            }

            @Override
            public void qualifierDeleted(QualifierEvent event) {
                Qualifier qualifier = event.getOldQualifier();
                for (Element element : engine.findElements(functionDependences.getId(), functionDependenceQualifier, qualifier.getId())) {
                    engine.deleteElement(element.getId());
                }
            }
        });
        Qualifier qq = StandardAttributesPlugin.getQualifiersQualifier(engine);
        engine.addElementAttributeListener(qq, new ElementAttributeListener() {

            @Override
            public void attributeChanged(AttributeEvent event) {
                if (event.isJournaled())
                    return;
                if (function.equals(event.getAttribute())) {
                    recalculateFunctionOfQualifier(engine, event);
                }
            }
        });
        engine.addFormulaListener(new FormulaListener() {

            @Override
            public void formulaChanged(FormulaEvent event) {
                if (event.isJournaled())
                    return;
                CalculateInfo info = event.getNewFormula();
                recalculate(engine, info);
            }
        });
    }
}
Also used : ElementAdapter(com.ramussoft.common.event.ElementAdapter) SQLException(java.sql.SQLException) Attribute(com.ramussoft.common.Attribute) ElementAttributeListener(com.ramussoft.common.event.ElementAttributeListener) Element(com.ramussoft.common.Element) IEngine(com.ramussoft.common.IEngine) ArrayList(java.util.ArrayList) Util(com.ramussoft.eval.Util) QualifierEvent(com.ramussoft.common.event.QualifierEvent) AttributeEvent(com.ramussoft.common.event.AttributeEvent) JDBCTemplate(com.ramussoft.jdbc.JDBCTemplate) AttributeType(com.ramussoft.common.AttributeType) ResultSet(java.sql.ResultSet) Qualifier(com.ramussoft.common.Qualifier) ArrayList(java.util.ArrayList) List(java.util.List) Replacementable(com.ramussoft.eval.Replacementable) FunctionPersistent(com.ramussoft.eval.FunctionPersistent) FormulaEvent(com.ramussoft.common.event.FormulaEvent) Eval(com.ramussoft.eval.Eval) Vector(java.util.Vector) Engine(com.ramussoft.common.Engine) IEngine(com.ramussoft.common.IEngine) RowMapper(com.ramussoft.jdbc.RowMapper) QualifierAdapter(com.ramussoft.common.event.QualifierAdapter) MetaValue(com.ramussoft.eval.MetaValue) ElementEvent(com.ramussoft.common.event.ElementEvent) SQLException(java.sql.SQLException) IEngineImpl(com.ramussoft.core.impl.IEngineImpl) FunctionsChangeListener(com.ramussoft.eval.event.FunctionsChangeListener) FunctionsChangeEvent(com.ramussoft.eval.event.FunctionsChangeEvent) CalculateInfo(com.ramussoft.common.CalculateInfo) FormulaListener(com.ramussoft.common.event.FormulaListener) EObject(com.ramussoft.eval.EObject)

Example 2 with QualifierAdapter

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

the class IDEF0ViewPlugin method addOpenDiagramListener.

private void addOpenDiagramListener() {
    framework.addActionListener(OPEN_DIAGRAM, new ActionListener() {

        @Override
        public void onAction(final ActionEvent event) {
            if (framework.openView(event))
                return;
            final DiagramData data = new DiagramData();
            data.openDiagram = (OpenDiagram) event.getValue();
            data.listener = new QualifierAdapter() {

                @Override
                public void qualifierDeleted(QualifierEvent event) {
                    if (event.getOldQualifier().equals(data.openDiagram.getQualifier())) {
                        data.view.close();
                    }
                }

                @Override
                public void qualifierUpdated(QualifierEvent event) {
                    if (data.openDiagram.getQualifier().equals(event.getNewQualifier())) {
                        IDEF0TabView view = (IDEF0TabView) data.view;
                        if (view.isBaseFunctionSelected()) {
                            ViewTitleEvent event2 = new ViewTitleEvent(data.view, event.getNewQualifier().getName());
                            view.titleChanged(event2);
                        }
                    }
                }
            };
            data.elementListener = new ElementAdapter() {

                @Override
                public void elementDeleted(ElementEvent event) {
                    if (((IDEF0TabView) data.view).isSelectedElementId(event.getOldElement()))
                        data.view.close();
                }
            };
            engine.addQualifierListener(data.listener);
            engine.addElementListener(data.openDiagram.getQualifier(), data.elementListener);
            data.view = new IDEF0TabView(framework, NDataPluginFactory.getDataPlugin(data.openDiagram.getQualifier(), engine, rules), data.openDiagram.getFunctionId(), frame, rules) {

                @Override
                public void close() {
                    super.close();
                    engine.removeQualifierListener(data.listener);
                    engine.removeElementListener(data.openDiagram.getQualifier(), data.elementListener);
                    TabbedEvent tEvent = new TabbedEvent(IDEF0TabbedView.IDEF0_TAB_VIEW, this);
                    tabRemoved(tEvent);
                }
            };
            TabbedEvent tEvent = new TabbedEvent(IDEF0TabbedView.IDEF0_TAB_VIEW, (TabView) data.view);
            tabCreated(tEvent);
        }
    });
}
Also used : QualifierAdapter(com.ramussoft.common.event.QualifierAdapter) ElementAdapter(com.ramussoft.common.event.ElementAdapter) ActionEvent(com.ramussoft.gui.common.event.ActionEvent) QualifierEvent(com.ramussoft.common.event.QualifierEvent) ElementEvent(com.ramussoft.common.event.ElementEvent) TabbedEvent(com.ramussoft.gui.common.event.TabbedEvent) ViewTitleEvent(com.ramussoft.gui.common.event.ViewTitleEvent) ActionListener(com.ramussoft.gui.common.event.ActionListener)

Example 3 with QualifierAdapter

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

the class OtherElementPlugin method setFramework.

@Override
public void setFramework(GUIFramework framework) {
    super.setFramework(framework);
    final QualifierListener listener = new QualifierAdapter() {

        @Override
        public void qualifierDeleted(QualifierEvent event) {
            RowSetValue value = sets.get(event.getOldQualifier());
            if (value != null) {
                value.rowSet.close();
                sets.remove(event.getOldQualifier());
            }
        }
    };
    framework.getEngine().addQualifierListener(listener);
    framework.addCloseMainFrameListener(new CloseMainFrameAdapter() {

        @Override
        public void closed() {
            for (RowSetValue value : sets.values()) {
                try {
                    value.rowSet.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            sets.clear();
            OtherElementPlugin.this.framework.getEngine().removeQualifierListener(listener);
        }
    });
}
Also used : QualifierAdapter(com.ramussoft.common.event.QualifierAdapter) CloseMainFrameAdapter(com.ramussoft.gui.common.event.CloseMainFrameAdapter) QualifierListener(com.ramussoft.common.event.QualifierListener) QualifierEvent(com.ramussoft.common.event.QualifierEvent)

Example 4 with QualifierAdapter

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

the class QualifierPlugin method setFramework.

@Override
public void setFramework(final GUIFramework framework) {
    super.setFramework(framework);
    this.engine = framework.getEngine();
    this.accessor = framework.getAccessRules();
    framework.addActionListener(OPEN_QUALIFIER, new ActionListener() {

        @Override
        public void onAction(final ActionEvent event) {
            if (framework.openView(event))
                return;
            final Data data = new Data();
            data.qualifier = (Qualifier) event.getValue();
            if (data.qualifier != null) {
                data.qualifier = engine.getQualifier(data.qualifier.getId());
            }
            if (data.qualifier == null)
                return;
            data.listener = new QualifierAdapter() {

                @Override
                public void qualifierUpdated(QualifierEvent event) {
                    if (event.getNewQualifier().equals(data.qualifier)) {
                        ViewTitleEvent e = new ViewTitleEvent(data.view, event.getNewQualifier().getName());
                        data.view.titleChanged(e);
                    }
                }

                @Override
                public void qualifierDeleted(QualifierEvent event) {
                    if (event.getOldQualifier().equals(data.qualifier)) {
                        data.view.close();
                    }
                }
            };
            engine.addQualifierListener(data.listener);
            data.view = new TableTabView(framework, engine, accessor, data.qualifier) {

                @Override
                public void close() {
                    super.close();
                    engine.removeQualifierListener(data.listener);
                    TabbedEvent tEvent = new TabbedEvent("TabbedTableView", this);
                    tabRemoved(tEvent);
                }

                @Override
                public String getTitle() {
                    return qualifier.getName();
                }

                @Override
                public ActionEvent getOpenAction() {
                    return new ActionEvent(OPEN_QUALIFIER, data.qualifier);
                }
            };
            TabbedEvent tEvent = new TabbedEvent(TabbedTableView.MAIN_TABBED_VIEW, (TabView) data.view);
            tabCreated(tEvent);
        }
    });
    framework.setSystemAttributeName(StandardAttributesPlugin.getAttributeNameAttribute(engine), GlobalResourcesManager.getString("AttributeName"));
    framework.setSystemAttributeName(StandardAttributesPlugin.getAttributeTypeNameAttribute(engine), GlobalResourcesManager.getString("AttributeTypeName"));
}
Also used : QualifierAdapter(com.ramussoft.common.event.QualifierAdapter) ViewTitleEvent(com.ramussoft.gui.common.event.ViewTitleEvent) ActionListener(com.ramussoft.gui.common.event.ActionListener) TabView(com.ramussoft.gui.common.TabView) TableTabView(com.ramussoft.gui.qualifier.table.TableTabView) ActionEvent(com.ramussoft.gui.common.event.ActionEvent) TableTabView(com.ramussoft.gui.qualifier.table.TableTabView) Qualifier(com.ramussoft.common.Qualifier) QualifierEvent(com.ramussoft.common.event.QualifierEvent) TabbedEvent(com.ramussoft.gui.common.event.TabbedEvent)

Example 5 with QualifierAdapter

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

the class StandardAttributesPlugin method init.

@SuppressWarnings("deprecation")
@Override
public void init(final Engine engine, final AccessRules accessor) {
    super.init(engine, accessor);
    engine.setPluginProperty(getName(), STANDARD_ATTRIBUTES_PLUGIN, this);
    if ((attributes != null) || (qualifiers != null)) {
        throw new RuntimeException("Plugin has already been inted");
    }
    qualifiers = engine.getSystemQualifier(QUALIFIERS_QUALIFIER);
    attributes = engine.getSystemQualifier(ATTRIBUTES_QUALIFIER);
    iconsQualifier = engine.getSystemQualifier(ICONS_QUALIFIER);
    historyQualifier = engine.getSystemQualifier(QUALIFIER_HISTORY);
    if (historyQualifier == null) {
        historyQualifier = engine.createSystemQualifier();
        historyQualifier.setName(QUALIFIER_HISTORY);
        historyElement = engine.createSystemAttribute(new AttributeType("Core", "Long", true));
        historyElement.setName(ATTRIBUTE_HISTORY_ELEMENT);
        engine.updateAttribute(historyElement);
        historyAttribute = engine.createSystemAttribute(new AttributeType("Core", "Long", true));
        historyAttribute.setName(ATTRIBUTE_HISTORY_ATTRIBUTE);
        engine.updateAttribute(historyAttribute);
        historyTime = engine.createSystemAttribute(new AttributeType("Core", "Date", true));
        historyTime.setName(ATTRIBUTE_HISTORY_TIME);
        engine.updateAttribute(historyTime);
        historyQualifier.getSystemAttributes().add(historyElement);
        historyQualifier.getSystemAttributes().add(historyAttribute);
        historyQualifier.getSystemAttributes().add(historyTime);
        engine.updateQualifier(historyQualifier);
    } else {
        historyElement = engine.getSystemAttribute(ATTRIBUTE_HISTORY_ELEMENT);
        historyAttribute = engine.getSystemAttribute(ATTRIBUTE_HISTORY_ATTRIBUTE);
        historyTime = engine.getSystemAttribute(ATTRIBUTE_HISTORY_TIME);
    }
    for (Qualifier qualifier : engine.getSystemQualifiers()) {
        if (qualifier.getName().startsWith(TABLE_QUALIFIER))
            tableQualifierIds.add(qualifier.getId());
    }
    if (((qualifiers == null) && (attributes != null)) || ((qualifiers != null) && (attributes == null))) {
        throw new RuntimeException("Attributes and qualifiers must inited both in the same time.");
    }
    if (qualifiers == null) {
        initQualifiers(engine);
    } else {
        loadAttributes();
    }
    iconsAttribute = engine.getSystemAttribute(ICONS_ATTRIBUTE);
    iconsQualifierLeafs = engine.getSystemAttribute(ICONS_QUALIFIER_LEAFS);
    iconsQualifierOpenFolders = engine.getSystemAttribute(ICONS_QUALIFIER_OPEN_FOLDERS);
    iconsQualifierClosedFolders = engine.getSystemAttribute(ICONS_QUALIFIER_CLOSED_FOLDERS);
    tableElementIdAttribute = engine.getSystemAttribute(TABLE_ELEMENT_ID_ATTRIBUTE);
    if (iconsQualifier == null) {
        createIconsQualifier(engine);
    }
    if (tableElementIdAttribute == null) {
        createTableElementIdAttribute(engine);
    }
    engine.setPluginProperty(getName(), ATTRIBUTES_QUALIFIER, attributes);
    engine.setPluginProperty(getName(), QUALIFIERS_QUALIFIER, qualifiers);
    engine.setPluginProperty(getName(), ATTRIBUTE_NAME, nameAttribute);
    engine.setPluginProperty(getName(), QUALIFIER_ID, aQualifierId);
    engine.setPluginProperty(getName(), ATTRIBUTE_TYPE_NAME, attributeTypeName);
    engine.setPluginProperty(getName(), ATTRIBUTE_ID, aAttributeId);
    engine.setPluginProperty(getName(), QUALIFIER_ATTRIBUTES, aList);
    engine.setPluginProperty(getName(), ICONS_QUALIFIER, iconsQualifier);
    engine.setPluginProperty(getName(), ICONS_ATTRIBUTE, iconsAttribute);
    engine.setPluginProperty(getName(), ICONS_QUALIFIER_LEAFS, iconsQualifierLeafs);
    engine.setPluginProperty(getName(), ICONS_QUALIFIER_OPEN_FOLDERS, iconsQualifierOpenFolders);
    engine.setPluginProperty(getName(), ICONS_QUALIFIER_CLOSED_FOLDERS, iconsQualifierClosedFolders);
    engine.addQualifierListener(new QualifierAdapter() {

        @Override
        public void qualifierCreated(QualifierEvent event) {
            if (!autocreateFromQualifiers)
                return;
            if (disableAutoupdate)
                return;
            if (event.isJournaled())
                return;
            synchronized (threadLocker) {
                if (Thread.currentThread() == currentThread)
                    return;
            }
            try {
                synchronized (threadLocker) {
                    currentThread = Thread.currentThread();
                }
                Engine e = event.getEngine();
                Element element = e.createElement(qualifiers.getId());
                e.setAttribute(element, aQualifierId, event.getNewQualifier().getId());
                e.setAttribute(element, nameAttribute, "");
            } finally {
                synchronized (threadLocker) {
                    currentThread = null;
                }
            }
        }

        @Override
        public void qualifierDeleted(QualifierEvent event) {
            Qualifier qualifier = event.getOldQualifier();
            if ((qualifier.isSystem()) && (qualifier.getName().startsWith(TABLE_QUALIFIER))) {
                tableQualifierIds.remove(new Long(qualifier.getId()));
            }
            if (event.isJournaled())
                return;
            if (disableAutoupdate)
                return;
            synchronized (threadLocker) {
                if (Thread.currentThread() == currentThread)
                    return;
            }
            try {
                synchronized (threadLocker) {
                    currentThread = Thread.currentThread();
                }
                Engine e = event.getEngine();
                List<Element> elements = e.findElements(qualifiers.getId(), aQualifierId, event.getOldQualifier().getId());
                if (elements.size() != 1) {
                    return;
                }
                e.deleteElement(elements.get(0).getId());
            } finally {
                synchronized (threadLocker) {
                    currentThread = null;
                }
            }
        }

        @Override
        public void qualifierUpdated(QualifierEvent event) {
            Qualifier qualifier = event.getNewQualifier();
            if ((qualifier.isSystem()) && (qualifier.getName().startsWith(TABLE_QUALIFIER)))
                tableQualifierIds.add(qualifier.getId());
            if (event.isJournaled())
                return;
            if (disableAutoupdate)
                return;
            List<Attribute> attributesToRemove = getAttributesToRemove(event);
            List<Element> allElements = null;
            for (Attribute attribute : attributesToRemove) {
                if (attribute.getAttributeType().toString().equals("Core.Table")) {
                    if (allElements == null) {
                        allElements = engine.getElements(event.getNewQualifier().getId());
                    }
                    for (Element element : allElements) {
                        for (Element element2 : getTableElements(engine, attribute, element)) {
                            engine.deleteElement(element2.getId());
                        }
                    }
                }
            }
            if (event.getNewQualifier().isSystem())
                return;
            synchronized (threadLocker) {
                if (Thread.currentThread() == currentThread)
                    return;
            }
            try {
                synchronized (threadLocker) {
                    currentThread = Thread.currentThread();
                }
                Engine e = event.getEngine();
                List<Element> elements = e.findElements(qualifiers.getId(), aQualifierId, event.getNewQualifier().getId());
                if (elements.size() != 1) {
                    // .println("Not regidtered qualifier was updated, or data integrity wrong.");
                    return;
                }
                e.setAttribute(elements.get(0), nameAttribute, event.getNewQualifier().getName());
                List<Attribute> list = event.getNewQualifier().getAttributes();
                List<ElementListPersistent> pList = new ArrayList<ElementListPersistent>();
                for (Attribute a : list) {
                    if (isSystem(a.getAttributeType()))
                        continue;
                    List<Element> ems = e.findElements(attributes.getId(), aAttributeId, a.getId());
                    if (ems.size() != 1) {
                        throw new RuntimeException("Fatal error, not registered attribute was removed, or data integrity wrong.");
                    }
                    pList.add(new ElementListPersistent(elements.get(0).getId(), ems.get(0).getId()));
                }
                e.setAttribute(elements.get(0), aList, pList);
            } finally {
                synchronized (threadLocker) {
                    currentThread = null;
                }
            }
        }

        private List<Attribute> getAttributesToRemove(QualifierEvent event) {
            List<Attribute> res = new ArrayList<Attribute>(0);
            diff(event.getOldQualifier().getAttributes(), event.getNewQualifier().getAttributes(), res);
            diff(event.getOldQualifier().getSystemAttributes(), event.getNewQualifier().getSystemAttributes(), res);
            return res;
        }

        private void diff(List<Attribute> attributes, List<Attribute> attributes2, List<Attribute> res) {
            for (Attribute attribute : attributes) if (attributes2.indexOf(attribute) < 0) {
                res.add(attribute);
            }
        }
    });
    engine.addAttributeListener(new AttributeAdapter() {

        @Override
        public void attributeCreated(AttributeEvent event) {
            if (event.isJournaled())
                return;
            if (disableAutoupdate)
                return;
            if (isSystem(event.getAttribute().getAttributeType()))
                return;
            synchronized (threadLocker) {
                if (Thread.currentThread() == currentThread)
                    return;
            }
            try {
                synchronized (threadLocker) {
                    currentThread = Thread.currentThread();
                }
                long lastId = getLastId();
                HierarchicalPersistent hp = new HierarchicalPersistent();
                hp.setIconId(-1l);
                hp.setParentElementId(-1l);
                hp.setPreviousElementId(lastId);
                Engine e = event.getEngine();
                Element element = e.createElement(attributes.getId());
                e.setAttribute(element, aAttributeId, event.getAttribute().getId());
                e.setAttribute(element, getHierarchicalAttribute(engine), hp);
                e.setAttribute(element, nameAttribute, "");
                e.setAttribute(element, attributeTypeName, event.getAttribute().getAttributeType().toString());
            } finally {
                synchronized (threadLocker) {
                    currentThread = null;
                }
            }
        }

        private long getLastId() {
            try {
                if (engine.getDeligate() instanceof IEngineImpl) {
                    IEngineImpl impl = ((IEngineImpl) engine.getDeligate());
                    String prefix = impl.getPrefix();
                    String sql = "SELECT element_id FROM " + prefix + "elements WHERE element_id NOT IN (SELECT previous_element_id FROM " + prefix + "attribute_hierarchicals) AND qualifier_id=?";
                    JDBCTemplate template = impl.getTemplate();
                    Object res = template.queryForObject(sql, new RowMapper() {

                        @Override
                        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                            return rs.getLong(1);
                        }
                    }, attributes.getId(), true);
                    if (res != null)
                        return ((Long) res).longValue();
                    return -1l;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            List<Attribute> attrs = new ArrayList<Attribute>();
            attrs.add(getHierarchicalAttribute(engine));
            Hashtable<Element, Object[]> hash = engine.getElements(attributes, attrs);
            Element element = null;
            for (Element e1 : hash.keySet()) {
                element = e1;
                for (Object[] objects : hash.values()) {
                    HierarchicalPersistent hp = (HierarchicalPersistent) objects[0];
                    if (hp != null) {
                        if (hp.getPreviousElementId() == element.getId()) {
                            element = null;
                            break;
                        }
                    }
                }
                if (element != null)
                    break;
            }
            if (element == null)
                return -1l;
            return element.getId();
        }

        @Override
        public void attributeDeleted(AttributeEvent event) {
            if (event.isJournaled())
                return;
            if (disableAutoupdate)
                return;
            if (((Attribute) event.getOldValue()).getAttributeType().toString().equals("Core.Table")) {
                Qualifier qualifier = getTableQualifierForAttribute(engine, (Attribute) event.getOldValue());
                engine.deleteQualifier(qualifier.getId());
            }
            if (isSystem(((Attribute) event.getOldValue()).getAttributeType()))
                return;
            synchronized (threadLocker) {
                if (Thread.currentThread() == currentThread)
                    return;
            }
            try {
                synchronized (threadLocker) {
                    currentThread = Thread.currentThread();
                }
                Engine e = event.getEngine();
                List<Element> elements = e.findElements(attributes.getId(), aAttributeId, event.getAttribute().getId());
                if (elements.size() != 1) {
                    throw new RuntimeException("Fatal error, not registered qualifier was removed, or data integrity wrong.");
                }
                e.deleteElement(elements.get(0).getId());
            } finally {
                synchronized (threadLocker) {
                    currentThread = null;
                }
            }
        }

        @Override
        public void attributeUpdated(AttributeEvent event) {
            if (event.isJournaled())
                return;
            if (disableAutoupdate)
                return;
            if (isSystem(event.getAttribute().getAttributeType()))
                return;
            synchronized (threadLocker) {
                if (Thread.currentThread() == currentThread)
                    return;
            }
            try {
                synchronized (threadLocker) {
                    currentThread = Thread.currentThread();
                }
                Engine e = event.getEngine();
                List<Element> elements = e.findElements(attributes.getId(), aAttributeId, event.getAttribute().getId());
                if (elements.size() != 1) {
                    return;
                // throw new RuntimeException(
                // "Fatal error, not registered attribute was removed, or data integrity wrong.");
                }
                e.setAttribute(elements.get(0), nameAttribute, event.getAttribute().getName());
            } finally {
                synchronized (threadLocker) {
                    currentThread = null;
                }
            }
        }

        @Override
        public void beforeAttributeDeleted(AttributeEvent event) {
            if (event.isJournaled())
                return;
            if (disableAutoupdate)
                return;
            Attribute attribute = event.getAttribute();
            Engine engine = event.getEngine();
            if (attribute.getAttributeType().toString().equals("Core.Table")) {
                Qualifier qualifier = getTableQualifierForAttribute(engine, attribute);
                if (qualifier != null) {
                    for (Element element : engine.getElements(qualifier.getId())) {
                        engine.deleteElement(element.getId());
                    }
                }
            }
        }
    });
    engine.addElementAttributeListener(qualifiers, new ElementAttributeListener() {

        @SuppressWarnings("unchecked")
        @Override
        public void attributeChanged(AttributeEvent event) {
            if (event.isJournaled())
                return;
            if (disableAutoupdate)
                return;
            if (event.getAttribute().equals(aQualifierId)) {
                Engine e = event.getEngine();
                Qualifier q = e.getQualifier((Long) event.getNewValue());
                List<Element> elements = e.findElements(qualifiers.getId(), aQualifierId, q.getId());
                if (elements.size() != 1) {
                    // .println("Not regidtered qualifier was updated, or data integrity wrong.");
                    return;
                }
                List<Attribute> list = q.getAttributes();
                List<ElementListPersistent> pList = new ArrayList<ElementListPersistent>();
                for (Attribute a : list) {
                    List<Element> ems = e.findElements(attributes.getId(), aAttributeId, a.getId());
                    if (ems.size() != 1) {
                        throw new RuntimeException("Fatal error, not registered attribute was removed, or data integrity wrong.");
                    }
                    pList.add(new ElementListPersistent(elements.get(0).getId(), ems.get(0).getId()));
                }
                Element element = elements.get(0);
                element.setQualifierId(qualifiers.getId());
                e.setAttribute(element, aList, pList);
            }
            synchronized (threadLocker) {
                if (Thread.currentThread() == currentThread)
                    return;
            }
            Engine e = event.getEngine();
            try {
                synchronized (threadLocker) {
                    currentThread = Thread.currentThread();
                }
                if (event.getAttribute().equals(nameAttribute)) {
                    Qualifier qualifier = e.getQualifier((Long) e.getAttribute(event.getElement(), aQualifierId));
                    qualifier.setName((String) event.getNewValue());
                    e.updateQualifier(qualifier);
                } else if (event.getAttribute().equals(aList)) {
                    Long attribute = (Long) e.getAttribute(event.getElement(), aQualifierId);
                    if (attribute == null)
                        return;
                    Qualifier qualifier = e.getQualifier(attribute);
                    List<ElementListPersistent> list = (List<ElementListPersistent>) e.getAttribute(event.getElement(), aList);
                    List<Attribute> attrs = new ArrayList<Attribute>();
                    for (ElementListPersistent p : list) {
                        Element attr = e.getElement(p.getElement2Id());
                        Attribute a = e.getAttribute(((Long) e.getAttribute(attr, aAttributeId)));
                        attrs.add(a);
                    }
                    List<Attribute> al = qualifier.getAttributes();
                    for (int i = al.size() - 1; i >= 0; i--) {
                        if (attrs.indexOf(al.get(i)) < 0) {
                            al.remove(i);
                        }
                    }
                    for (Attribute a : attrs) {
                        if (al.indexOf(a) < 0)
                            al.add(a);
                    }
                    e.updateQualifier(qualifier);
                }
            } finally {
                synchronized (threadLocker) {
                    currentThread = null;
                }
            }
        }
    });
    engine.addElementAttributeListener(attributes, new ElementAttributeListener() {

        @Override
        public void attributeChanged(AttributeEvent event) {
            if (event.isJournaled())
                return;
            if (disableAutoupdate)
                return;
            synchronized (threadLocker) {
                if (Thread.currentThread() == currentThread)
                    return;
            }
            Engine e = event.getEngine();
            try {
                synchronized (threadLocker) {
                    currentThread = Thread.currentThread();
                }
                if (event.getAttribute().equals(nameAttribute)) {
                    Attribute attribute = e.getAttribute((Long) e.getAttribute(event.getElement(), aAttributeId));
                    attribute.setName((String) event.getNewValue());
                    e.updateAttribute(attribute);
                }
            } finally {
                synchronized (threadLocker) {
                    currentThread = null;
                }
            }
        }
    });
    engine.addElementAttributeListener(null, new ElementAttributeListener() {

        @Override
        public void attributeChanged(AttributeEvent event) {
            if (disableAutoupdate)
                return;
            Element element = event.getElement();
            if (element == null)
                return;
            try {
                if (tableQualifierIds.indexOf(new Long(element.getQualifierId())) >= 0) {
                    Object parentElementId;
                    if (event.getAttribute().equals(tableElementIdAttribute))
                        parentElementId = event.getNewValue();
                    else
                        parentElementId = engine.getAttribute(element, tableElementIdAttribute);
                    if (parentElementId != null) {
                        Qualifier table = engine.getQualifier(element.getQualifierId());
                        Attribute attribute = getAttributeForTable(engine, table);
                        Element parent = engine.getElement((Long) parentElementId);
                        AttributeEvent event2 = new AttributeEvent(engine, parent, attribute, null, null, event.isJournaled());
                        for (ElementAttributeListener listener : engine.getElementAttributeListeners(-1)) listener.attributeChanged(event2);
                        for (ElementAttributeListener listener : engine.getElementAttributeListeners(parent.getQualifierId())) listener.attributeChanged(event2);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (event.isJournaled())
                return;
            List<Long> attrs = historyQualifiers.get(element.getQualifierId());
            if (attrs != null)
                if (attrs.indexOf(event.getAttribute().getId()) >= 0) {
                    Element data = engine.createElement(historyQualifier.getId());
                    engine.setAttribute(data, event.getAttribute(), event.getNewValue());
                    engine.setAttribute(data, historyElement, element.getId());
                    engine.setAttribute(data, historyAttribute, event.getAttribute().getId());
                    engine.setAttribute(data, historyTime, new Date());
                }
        }
    });
    engine.addElementListener(qualifiers, new ElementAdapter() {

        @Override
        public void elementCreated(ElementEvent event) {
            if (event.isJournaled())
                return;
            if (disableAutoupdate)
                return;
            synchronized (threadLocker) {
                if (Thread.currentThread() == currentThread)
                    return;
            }
            Engine e = event.getEngine();
            try {
                synchronized (threadLocker) {
                    currentThread = Thread.currentThread();
                }
                Qualifier qualifier = e.createQualifier();
                e.setAttribute(event.getNewElement(), aQualifierId, qualifier.getId());
            } finally {
                synchronized (threadLocker) {
                    currentThread = null;
                }
            }
        }

        @Override
        public void beforeElementDeleted(ElementEvent event) {
            if (event.isJournaled())
                return;
            if (disableAutoupdate)
                return;
            synchronized (threadLocker) {
                if (Thread.currentThread() == currentThread)
                    return;
            }
            Engine e = event.getEngine();
            long qualifierId = event.getOldElement().getQualifierId();
            if ((qualifierId != qualifiers.getId()) && (qualifierId != attributes.getId()))
                return;
            try {
                synchronized (threadLocker) {
                    currentThread = Thread.currentThread();
                }
                if (qualifierId == qualifiers.getId()) {
                    Object attribute = e.getAttribute(event.getOldElement(), aQualifierId);
                    if (attribute != null)
                        e.deleteQualifier((Long) attribute);
                } else {
                    throw new RuntimeException("Element for attribute can not be deleted");
                }
            } finally {
                synchronized (threadLocker) {
                    currentThread = null;
                }
            }
        }
    });
    engine.addElementListener(null, new ElementAdapter() {

        @Override
        public void beforeElementDeleted(ElementEvent event) {
            if (disableAutoupdate)
                return;
            Element element = event.getOldElement();
            if (element == null)
                return;
            try {
                if (tableQualifierIds.indexOf(new Long(element.getQualifierId())) >= 0) {
                    Object parentElementId;
                    parentElementId = engine.getAttribute(element, tableElementIdAttribute);
                    if (parentElementId != null) {
                        Qualifier table = engine.getQualifier(element.getQualifierId());
                        Attribute attribute = getAttributeForTable(engine, table);
                        Element parent = engine.getElement((Long) parentElementId);
                        AttributeEvent event2 = new AttributeEvent(engine, parent, attribute, null, null, event.isJournaled());
                        for (ElementAttributeListener listener : engine.getElementAttributeListeners(-1)) listener.attributeChanged(event2);
                        for (ElementAttributeListener listener : engine.getElementAttributeListeners(parent.getQualifierId())) listener.attributeChanged(event2);
                    }
                } else {
                    Qualifier qualifier = engine.getQualifier(element.getQualifierId());
                    for (Attribute attribute : qualifier.getAttributes()) {
                        if (attribute.getAttributeType().toString().equals("Core.Table")) {
                            for (Element element2 : getTableElements(engine, attribute, element)) {
                                engine.deleteElement(element2.getId());
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            List<Long> attrs = historyQualifiers.get(element.getQualifierId());
            if (attrs != null) {
                List<Element> list = engine.findElements(historyQualifier.getId(), historyElement, element.getId());
                for (Element element2 : list) engine.deleteElement(element2.getId());
            }
        }
    });
    engine.addStreamListener(new StreamAdapter() {

        @Override
        public void streamUpdated(StreamEvent event) {
            if (event.isJournaled())
                return;
            String path = event.getPath();
            if (path.equals(PROPERTIES)) {
                loadHistoryQualifiers((!disableAutoupdate) && (!event.isJournaled()));
            }
        }
    });
    loadHistoryQualifiers(false);
}
Also used : ElementAdapter(com.ramussoft.common.event.ElementAdapter) ElementListPersistent(com.ramussoft.core.attribute.simple.ElementListPersistent) Attribute(com.ramussoft.common.Attribute) ElementAttributeListener(com.ramussoft.common.event.ElementAttributeListener) Element(com.ramussoft.common.Element) ArrayList(java.util.ArrayList) QualifierEvent(com.ramussoft.common.event.QualifierEvent) AttributeEvent(com.ramussoft.common.event.AttributeEvent) JDBCTemplate(com.ramussoft.jdbc.JDBCTemplate) AttributeType(com.ramussoft.common.AttributeType) ResultSet(java.sql.ResultSet) Qualifier(com.ramussoft.common.Qualifier) ArrayList(java.util.ArrayList) List(java.util.List) Engine(com.ramussoft.common.Engine) IEngine(com.ramussoft.common.IEngine) RowMapper(com.ramussoft.jdbc.RowMapper) QualifierAdapter(com.ramussoft.common.event.QualifierAdapter) Hashtable(java.util.Hashtable) StreamEvent(com.ramussoft.common.event.StreamEvent) ElementEvent(com.ramussoft.common.event.ElementEvent) SQLException(java.sql.SQLException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Date(java.util.Date) StreamAdapter(com.ramussoft.common.event.StreamAdapter) IEngineImpl(com.ramussoft.core.impl.IEngineImpl) HierarchicalPersistent(com.ramussoft.core.attribute.simple.HierarchicalPersistent) AttributeAdapter(com.ramussoft.common.event.AttributeAdapter)

Aggregations

QualifierAdapter (com.ramussoft.common.event.QualifierAdapter)7 QualifierEvent (com.ramussoft.common.event.QualifierEvent)7 Qualifier (com.ramussoft.common.Qualifier)5 Attribute (com.ramussoft.common.Attribute)3 AttributeType (com.ramussoft.common.AttributeType)3 ElementAdapter (com.ramussoft.common.event.ElementAdapter)3 ElementEvent (com.ramussoft.common.event.ElementEvent)3 Element (com.ramussoft.common.Element)2 Engine (com.ramussoft.common.Engine)2 IEngine (com.ramussoft.common.IEngine)2 AttributeEvent (com.ramussoft.common.event.AttributeEvent)2 ElementAttributeListener (com.ramussoft.common.event.ElementAttributeListener)2 IEngineImpl (com.ramussoft.core.impl.IEngineImpl)2 ActionEvent (com.ramussoft.gui.common.event.ActionEvent)2 ActionListener (com.ramussoft.gui.common.event.ActionListener)2 TabbedEvent (com.ramussoft.gui.common.event.TabbedEvent)2 ViewTitleEvent (com.ramussoft.gui.common.event.ViewTitleEvent)2 JDBCTemplate (com.ramussoft.jdbc.JDBCTemplate)2 RowMapper (com.ramussoft.jdbc.RowMapper)2 ResultSet (java.sql.ResultSet)2