Search in sources :

Example 16 with TableNode

use of com.servoy.j2db.persistence.TableNode in project servoy-client by Servoy.

the class JSDataSourceNode method removeCalculation.

/**
 * Removes the calculation specified by name.
 *
 * @sample
 * var calc1 = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation1() { return 123; }", JSVariable.INTEGER);
 * var calc2 = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation2() { return '20'; }");
 *
 * var c = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculation("myCalculation1");
 * application.output("Name: " + c.getName() + ", Stored: " + c.isStored());
 *
 * solutionModel.getDataSourceNode("db:/example_data/customers").removeCalculation("myCalculation1");
 * c = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculation("myCalculation1");
 * if (c != null) {
 * 	application.output("myCalculation could not be removed.");
 * }
 *
 * var allCalcs = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculations();
 * for (var i = 0; i < allCalcs.length; i++) {
 * 	application.output(allCalcs[i]);
 * }
 *
 * @param name the name of the calculation to be removed
 *
 * @return true if the removal was successful, false otherwise
 */
@JSFunction
public boolean removeCalculation(String name) {
    try {
        FlattenedSolution fs = application.getFlattenedSolution();
        TableNode tablenode = fs.getSolutionCopyTableNode(dataSource);
        ScriptCalculation sc = tablenode.getScriptCalculation(name);
        if (sc != null) {
            tablenode.removeChild(sc);
            return true;
        }
        // it is a design time calculation, therefore we "hide" it
        sc = fs.getScriptCalculation(name, dataSource);
        if (sc != null) {
            fs.addToRemovedPersists(sc);
            return true;
        }
        // not found
        return false;
    } catch (RepositoryException e) {
        throw new RuntimeException(e);
    }
}
Also used : ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) TableNode(com.servoy.j2db.persistence.TableNode) FlattenedSolution(com.servoy.j2db.FlattenedSolution) RepositoryException(com.servoy.j2db.persistence.RepositoryException) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 17 with TableNode

use of com.servoy.j2db.persistence.TableNode in project servoy-client by Servoy.

the class PersistIndex method initDatasourceCache.

protected void initDatasourceCache(String datasource) {
    if ((datasourceToPersist.size() == 0) || (datasource != null && datasourceToPersist.get(datasource) == null)) {
        visit((persist) -> {
            if (persist instanceof TableNode) {
                String tableDs = ((TableNode) persist).getDataSource();
                if (tableDs != null) {
                    ConcurrentMap<Class<? extends IPersist>, ConcurrentMap<String, IPersist>> dsMap = datasourceToPersist.get(tableDs);
                    if (dsMap == null) {
                        dsMap = new ConcurrentHashMap<>(4);
                        dsMap.put(ScriptCalculation.class, new ConcurrentHashMap<String, IPersist>(4));
                        dsMap.put(TableNode.class, new ConcurrentHashMap<String, IPersist>(4));
                        dsMap.put(AggregateVariable.class, new ConcurrentHashMap<String, IPersist>(4));
                        dsMap.put(ScriptMethod.class, new ConcurrentHashMap<String, IPersist>(4));
                        datasourceToPersist.put(tableDs, dsMap);
                    }
                    ConcurrentMap<String, IPersist> tableNodeCache = dsMap.get(TableNode.class);
                    Solution solution = (Solution) ((TableNode) persist).getAncestor(IRepository.SOLUTIONS);
                    tableNodeCache.put(solution.getName(), persist);
                    return IPersistVisitor.CONTINUE_TRAVERSAL;
                }
            } else {
                ISupportChilds parent = persist.getParent();
                if (persist instanceof ScriptCalculation) {
                    if (parent instanceof TableNode) {
                        ConcurrentMap<Class<? extends IPersist>, ConcurrentMap<String, IPersist>> dsMap = datasourceToPersist.get(((TableNode) parent).getDataSource());
                        addInDatasourceCache(dsMap.get(ScriptCalculation.class), persist, datasource);
                    } else {
                        Debug.error("Something wrong with ScriptCalculation " + ((ScriptCalculation) persist).getName() + " should  have table as parent but the parent is: " + parent);
                    }
                } else if (persist instanceof AggregateVariable) {
                    if (parent instanceof TableNode) {
                        ConcurrentMap<Class<? extends IPersist>, ConcurrentMap<String, IPersist>> dsMap = datasourceToPersist.get(((TableNode) parent).getDataSource());
                        addInDatasourceCache(dsMap.get(AggregateVariable.class), persist, datasource);
                    } else {
                        Debug.error("Something wrong with AggregateVariable " + ((ScriptCalculation) persist).getName() + " should  have table as parent but the parent is: " + parent);
                    }
                } else if (persist instanceof ScriptMethod && parent instanceof TableNode) {
                    ConcurrentMap<Class<? extends IPersist>, ConcurrentMap<String, IPersist>> dsMap = datasourceToPersist.get(((TableNode) parent).getDataSource());
                    addInDatasourceCache(dsMap.get(ScriptMethod.class), persist, datasource);
                }
            }
            return persist instanceof Solution ? IPersistVisitor.CONTINUE_TRAVERSAL : IPersistVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
        });
        if (datasource != null && datasourceToPersist.get(datasource) == null) {
            ConcurrentMap<Class<? extends IPersist>, ConcurrentMap<String, IPersist>> dsMap = new ConcurrentHashMap<>(4);
            dsMap.put(ScriptCalculation.class, new ConcurrentHashMap<String, IPersist>(4));
            dsMap.put(TableNode.class, new ConcurrentHashMap<String, IPersist>(4));
            dsMap.put(AggregateVariable.class, new ConcurrentHashMap<String, IPersist>(4));
            dsMap.put(ScriptMethod.class, new ConcurrentHashMap<String, IPersist>(4));
            datasourceToPersist.put(datasource, dsMap);
        }
    }
}
Also used : ISupportChilds(com.servoy.j2db.persistence.ISupportChilds) ConcurrentMap(java.util.concurrent.ConcurrentMap) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable) ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) IPersist(com.servoy.j2db.persistence.IPersist) TableNode(com.servoy.j2db.persistence.TableNode) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Solution(com.servoy.j2db.persistence.Solution)

Example 18 with TableNode

use of com.servoy.j2db.persistence.TableNode in project servoy-client by Servoy.

the class DebugUtils method getScopesAndFormsToReload.

public static Set<IFormController>[] getScopesAndFormsToReload(final ClientState clientState, Collection<IPersist> changes) {
    Set<IFormController> scopesToReload = new HashSet<IFormController>();
    final Set<IFormController> formsToReload = new HashSet<IFormController>();
    final SpecProviderState specProviderState = WebComponentSpecProvider.getSpecProviderState();
    final Set<Form> formsUpdated = new HashSet<Form>();
    for (IPersist persist : changes) {
        clientState.getFlattenedSolution().updatePersistInSolutionCopy(persist);
        if (persist instanceof ScriptMethod) {
            if (persist.getParent() instanceof Form) {
                Form form = (Form) persist.getParent();
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers(form);
                for (IFormController formController : cachedFormControllers) {
                    scopesToReload.add(formController);
                }
            } else if (persist.getParent() instanceof Solution) {
                LazyCompilationScope scope = clientState.getScriptEngine().getScopesScope().getGlobalScope(((ScriptMethod) persist).getScopeName());
                scope.remove((IScriptProvider) persist);
                scope.put((IScriptProvider) persist, (IScriptProvider) persist);
            } else if (persist.getParent() instanceof TableNode) {
                clientState.getFoundSetManager().reloadFoundsetMethod(((TableNode) persist.getParent()).getDataSource(), (IScriptProvider) persist);
            }
            if (clientState instanceof DebugJ2DBClient) {
                // ((DebugJ2DBClient)clientState).clearUserWindows();  no need for this as window API was refactored and it allows users to clean up dialogs
                ((DebugSwingFormMananger) ((DebugJ2DBClient) clientState).getFormManager()).fillScriptMenu();
            }
        } else if (persist instanceof ScriptVariable) {
            ScriptVariable sv = (ScriptVariable) persist;
            if (persist.getParent() instanceof Solution) {
                clientState.getScriptEngine().getScopesScope().getGlobalScope(sv.getScopeName()).put(sv);
            }
            if (persist.getParent() instanceof Form) {
                Form form = (Form) persist.getParent();
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers(form);
                for (IFormController formController : cachedFormControllers) {
                    FormScope scope = formController.getFormScope();
                    scope.put(sv);
                }
            }
        } else if (persist.getAncestor(IRepository.FORMS) != null) {
            final Form form = (Form) persist.getAncestor(IRepository.FORMS);
            if (form != null && form.isFormComponent().booleanValue()) {
                // if the changed form is a reference form we need to check if that is referenced by a loaded form..
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
                for (IFormController fc : cachedFormControllers) {
                    fc.getForm().acceptVisitor(new IPersistVisitor() {

                        @Override
                        public Object visit(IPersist o) {
                            if (o instanceof WebComponent) {
                                WebComponent wc = (WebComponent) o;
                                WebObjectSpecification spec = FormTemplateGenerator.getWebObjectSpecification(wc);
                                Collection<PropertyDescription> properties = spec != null ? spec.getProperties(FormComponentPropertyType.INSTANCE) : null;
                                if (properties != null && properties.size() > 0) {
                                    Form persistForm = (Form) wc.getAncestor(IRepository.FORMS);
                                    for (PropertyDescription pd : properties) {
                                        Form frm = FormComponentPropertyType.INSTANCE.getForm(wc.getProperty(pd.getName()), clientState.getFlattenedSolution());
                                        if (frm != null && (form.equals(frm) || FlattenedForm.hasFormInHierarchy(frm, form) || isReferenceFormUsedInForm(clientState, form, frm)) && !formsUpdated.contains(persistForm)) {
                                            formsUpdated.add(persistForm);
                                            List<IFormController> cfc = clientState.getFormManager().getCachedFormControllers(persistForm);
                                            for (IFormController formController : cfc) {
                                                formsToReload.add(formController);
                                            }
                                        }
                                    }
                                }
                            }
                            return IPersistVisitor.CONTINUE_TRAVERSAL;
                        }
                    });
                }
            } else if (!formsUpdated.contains(form)) {
                formsUpdated.add(form);
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers(form);
                for (IFormController formController : cachedFormControllers) {
                    formsToReload.add(formController);
                }
            }
            if (persist instanceof Form && clientState.getFormManager() instanceof DebugUtils.DebugUpdateFormSupport) {
                ((DebugUtils.DebugUpdateFormSupport) clientState.getFormManager()).updateForm((Form) persist);
            }
        } else if (persist instanceof ScriptCalculation) {
            ScriptCalculation sc = (ScriptCalculation) persist;
            if (((RemoteDebugScriptEngine) clientState.getScriptEngine()).recompileScriptCalculation(sc)) {
                List<String> al = new ArrayList<String>();
                al.add(sc.getDataProviderID());
                try {
                    String dataSource = clientState.getFoundSetManager().getDataSource(sc.getTable());
                    ((FoundSetManager) clientState.getFoundSetManager()).getRowManager(dataSource).clearCalcs(null, al);
                    ((FoundSetManager) clientState.getFoundSetManager()).flushSQLSheet(dataSource);
                } catch (Exception e) {
                    Debug.error(e);
                }
            }
        // if (clientState instanceof DebugJ2DBClient)
        // {
        // ((DebugJ2DBClient)clientState).clearUserWindows(); no need for this as window API was refactored and it allows users to clean up dialogs
        // }
        } else if (persist instanceof Relation) {
            ((FoundSetManager) clientState.getFoundSetManager()).flushSQLSheet((Relation) persist);
            List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
            try {
                String primary = ((Relation) persist).getPrimaryDataSource();
                for (IFormController formController : cachedFormControllers) {
                    if (primary.equals(formController.getDataSource())) {
                        final IFormController finalController = formController;
                        final Relation finalRelation = (Relation) persist;
                        formController.getForm().acceptVisitor(new IPersistVisitor() {

                            @Override
                            public Object visit(IPersist o) {
                                if (o instanceof Tab && Utils.equalObjects(finalRelation.getName(), ((Tab) o).getRelationName())) {
                                    formsToReload.add(finalController);
                                    return o;
                                }
                                if (o instanceof Field && ((Field) o).getValuelistID() > 0) {
                                    ValueList vl = clientState.getFlattenedSolution().getValueList(((Field) o).getValuelistID());
                                    if (vl != null && Utils.equalObjects(finalRelation.getName(), vl.getRelationName())) {
                                        formsToReload.add(finalController);
                                        return o;
                                    }
                                }
                                if (o instanceof WebComponent) {
                                    WebComponent webComponent = (WebComponent) o;
                                    WebObjectSpecification spec = specProviderState == null ? null : specProviderState.getWebComponentSpecification(webComponent.getTypeName());
                                    if (spec != null) {
                                        Collection<PropertyDescription> properties = spec.getProperties(RelationPropertyType.INSTANCE);
                                        for (PropertyDescription pd : properties) {
                                            if (Utils.equalObjects(webComponent.getFlattenedJson().opt(pd.getName()), finalRelation.getName())) {
                                                formsToReload.add(finalController);
                                                return o;
                                            }
                                        }
                                    }
                                }
                                return CONTINUE_TRAVERSAL;
                            }
                        });
                    }
                }
            } catch (Exception e) {
                Debug.error(e);
            }
        } else if (persist instanceof ValueList) {
            ComponentFactory.flushValueList(clientState, (ValueList) persist);
            List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
            for (IFormController formController : cachedFormControllers) {
                final IFormController finalController = formController;
                final ValueList finalValuelist = (ValueList) persist;
                formController.getForm().acceptVisitor(new IPersistVisitor() {

                    @Override
                    public Object visit(IPersist o) {
                        if (o instanceof Field && ((Field) o).getValuelistID() > 0 && ((Field) o).getValuelistID() == finalValuelist.getID()) {
                            formsToReload.add(finalController);
                            return o;
                        }
                        if (o instanceof WebComponent) {
                            WebComponent webComponent = (WebComponent) o;
                            WebObjectSpecification spec = specProviderState == null ? null : specProviderState.getWebComponentSpecification(webComponent.getTypeName());
                            if (spec != null) {
                                Collection<PropertyDescription> properties = spec.getProperties(ValueListPropertyType.INSTANCE);
                                for (PropertyDescription pd : properties) {
                                    if (Utils.equalObjects(webComponent.getFlattenedJson().opt(pd.getName()), finalValuelist.getUUID().toString())) {
                                        formsToReload.add(finalController);
                                        return o;
                                    }
                                }
                            }
                        }
                        return CONTINUE_TRAVERSAL;
                    }
                });
            }
        } else if (persist instanceof Style) {
            ComponentFactory.flushStyle(null, ((Style) persist));
            List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
            String styleName = ((Style) persist).getName();
            for (IFormController formController : cachedFormControllers) {
                if (styleName.equals(formController.getForm().getStyleName())) {
                    formsToReload.add(formController);
                }
            }
        }
    }
    return new Set[] { scopesToReload, formsToReload };
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) Set(java.util.Set) HashSet(java.util.HashSet) Form(com.servoy.j2db.persistence.Form) FlattenedForm(com.servoy.j2db.persistence.FlattenedForm) ValueList(com.servoy.j2db.persistence.ValueList) LazyCompilationScope(com.servoy.j2db.scripting.LazyCompilationScope) FormScope(com.servoy.j2db.scripting.FormScope) WebComponent(com.servoy.j2db.persistence.WebComponent) Field(com.servoy.j2db.persistence.Field) Relation(com.servoy.j2db.persistence.Relation) Style(com.servoy.j2db.persistence.Style) ValueList(com.servoy.j2db.persistence.ValueList) List(java.util.List) ArrayList(java.util.ArrayList) Solution(com.servoy.j2db.persistence.Solution) HashSet(java.util.HashSet) FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) DebugSwingFormMananger(com.servoy.j2db.debug.DebugJ2DBClient.DebugSwingFormMananger) RhinoException(org.mozilla.javascript.RhinoException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServoyException(com.servoy.j2db.util.ServoyException) PropertyDescription(org.sablo.specification.PropertyDescription) ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) SpecProviderState(org.sablo.specification.SpecProviderState) Tab(com.servoy.j2db.persistence.Tab) IPersist(com.servoy.j2db.persistence.IPersist) IScriptProvider(com.servoy.j2db.persistence.IScriptProvider) TableNode(com.servoy.j2db.persistence.TableNode) ScriptVariable(com.servoy.j2db.persistence.ScriptVariable) IPersistVisitor(com.servoy.j2db.persistence.IPersistVisitor) Collection(java.util.Collection) IFormController(com.servoy.j2db.IFormController) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod)

Example 19 with TableNode

use of com.servoy.j2db.persistence.TableNode in project servoy-client by Servoy.

the class FoundSet method tableHasOnDeleteMethods.

private boolean tableHasOnDeleteMethods() {
    try {
        FlattenedSolution solutionRoot = fsm.getApplication().getFlattenedSolution();
        Iterator<TableNode> tableNodes = solutionRoot.getTableNodes(getTable());
        List<ScriptMethod> foundsetMethods = solutionRoot.getFoundsetMethods(getTable(), false);
        while (tableNodes.hasNext()) {
            TableNode node = tableNodes.next();
            int methodId = node.getOnDeleteMethodID();
            if (methodId > 0 && solutionRoot.getScriptMethod(methodId) != null || AbstractBase.selectById(foundsetMethods.iterator(), methodId) != null) {
                return true;
            }
            methodId = node.getOnAfterDeleteMethodID();
            if (methodId > 0 && solutionRoot.getScriptMethod(methodId) != null || AbstractBase.selectById(foundsetMethods.iterator(), methodId) != null) {
                return true;
            }
        }
    } catch (RepositoryException e) {
        Debug.error(e);
    }
    return false;
}
Also used : TableNode(com.servoy.j2db.persistence.TableNode) FlattenedSolution(com.servoy.j2db.FlattenedSolution) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod)

Example 20 with TableNode

use of com.servoy.j2db.persistence.TableNode in project servoy-client by Servoy.

the class FoundSetManager method getViewFoundSet.

@SuppressWarnings("nls")
@Override
public ViewFoundSet getViewFoundSet(String name, QBSelect query, boolean register) {
    if (query.getQuery().getColumns() == null || query.getQuery().getColumns().size() == 0) {
        throw new RuntimeException("Can't create a ViewFoundset with name: " + name + " and query  " + query + " that has no columns");
    }
    String dataSource = DataSourceUtils.createViewDataSource(name);
    ViewFoundSet vfs = new ViewFoundSet(dataSource, query.build(), application.getFoundSetManager(), config.pkChunkSize());
    // if this datasource defintion is created already in the developer then we need to check if the query columns are correctly matching it.
    ServoyJSONObject columnsDef = null;
    Iterator<TableNode> tblIte = application.getFlattenedSolution().getTableNodes(dataSource);
    while (tblIte.hasNext() && columnsDef == null) {
        TableNode tn = tblIte.next();
        columnsDef = tn.getColumns();
        if (columnsDef != null) {
            TableDef def = DatabaseUtils.deserializeTableInfo(columnsDef);
            for (ColumnInfoDef col : def.columnInfoDefSet) {
                IQuerySelectValue selectValue = getSelectvalue(query, col.name);
                if (selectValue == null) {
                    Debug.error("Column " + col.name + " of type " + col.columnType.toString() + " defined in view datasource '" + dataSource + "' was not found in the provided query.");
                    return null;
                }
                BaseColumnType columnType = selectValue.getColumnType();
                // relax the mapping on default Servoy types
                if (columnType != null && Column.mapToDefaultType(columnType.getSqlType()) != Column.mapToDefaultType(col.columnType.getSqlType())) {
                    Debug.error("Column type for column '" + col.name + " of type " + col.columnType.toString() + "' defined in view datasource '" + dataSource + "' does not match the one " + columnType + " provided in the query.");
                    return null;
                }
            }
        }
    }
    registerViewFoundSet(vfs, !register);
    return vfs;
}
Also used : ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) TableNode(com.servoy.j2db.persistence.TableNode) TableDef(com.servoy.j2db.util.xmlxport.TableDef) ColumnInfoDef(com.servoy.j2db.util.xmlxport.ColumnInfoDef) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue) BaseColumnType(com.servoy.base.query.BaseColumnType)

Aggregations

TableNode (com.servoy.j2db.persistence.TableNode)20 RepositoryException (com.servoy.j2db.persistence.RepositoryException)9 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)9 FlattenedSolution (com.servoy.j2db.FlattenedSolution)8 ScriptCalculation (com.servoy.j2db.persistence.ScriptCalculation)6 Solution (com.servoy.j2db.persistence.Solution)6 Form (com.servoy.j2db.persistence.Form)5 BaseColumnType (com.servoy.base.query.BaseColumnType)4 IPersist (com.servoy.j2db.persistence.IPersist)4 ServoyException (com.servoy.j2db.util.ServoyException)4 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)4 ColumnInfoDef (com.servoy.j2db.util.xmlxport.ColumnInfoDef)4 TableDef (com.servoy.j2db.util.xmlxport.TableDef)4 ArrayList (java.util.ArrayList)4 ApplicationException (com.servoy.j2db.ApplicationException)3 FoundSetManager (com.servoy.j2db.dataprocessing.FoundSetManager)3 ITable (com.servoy.j2db.persistence.ITable)3 JavaScriptException (org.mozilla.javascript.JavaScriptException)3 JSFunction (org.mozilla.javascript.annotations.JSFunction)3 BaseQueryTable (com.servoy.base.query.BaseQueryTable)2