Search in sources :

Example 1 with ScriptCalculation

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

the class JSDataSourceNode method newCalculation.

/**
 * Creates a new calculation for the given code and the type, if it builds on a column (name is a column name) then type will be ignored.
 *
 * @param code The code of the calculation, this must be a full function declaration.
 * @param type The type of the calculation, one of the JSVariable types.
 *
 * @sample
 * var calc = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation() { return 123; }", JSVariable.INTEGER);
 * var calc2 = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation2() { return '20'; }");
 * var calc3 = solutionModel.getDataSourceNode("db:/example_data/employees").newCalculation("function myCalculation3() { return 'Hello World!'; }",	JSVariable.TEXT);
 *
 * var c = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculation("myCalculation");
 * application.output("Name: " + c.getName() + ", Stored: " + c.isStored());
 *
 * var allCalcs = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculations();
 * for (var i = 0; i < allCalcs.length; i++) {
 * 	application.output(allCalcs[i]);
 * }
 */
@JSFunction
public JSCalculation newCalculation(String code, int type) {
    try {
        FlattenedSolution fs = application.getFlattenedSolution();
        TableNode tablenode = fs.getSolutionCopyTableNode(dataSource);
        String name = JSMethod.parseName(code);
        ScriptCalculation scriptCalculation = tablenode.createNewScriptCalculation(new ScriptNameValidator(fs), name, null, fs.getTable(dataSource));
        scriptCalculation.setDeclaration(code);
        scriptCalculation.setTypeAndCheck(type, application);
        TableScope tableScope = (TableScope) application.getScriptEngine().getTableScope(scriptCalculation.getTable());
        if (tableScope != null) {
            tableScope.put(scriptCalculation, scriptCalculation);
            ((FoundSetManager) application.getFoundSetManager()).flushSQLSheet(dataSource);
        }
        return new JSCalculation(this, scriptCalculation, application, true);
    } catch (RepositoryException e) {
        throw new RuntimeException(e);
    }
}
Also used : ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) TableScope(com.servoy.j2db.scripting.TableScope) TableNode(com.servoy.j2db.persistence.TableNode) FlattenedSolution(com.servoy.j2db.FlattenedSolution) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 2 with ScriptCalculation

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

the class JSCalculation method checkModification.

private void checkModification() {
    if (!isCopy) {
        try {
            TableNode tableNode = application.getFlattenedSolution().getSolutionCopyTableNode(parent.getDataSource());
            ScriptCalculation sc = tableNode.getScriptCalculation(scriptCalculation.getName());
            if (sc == null) {
                sc = (ScriptCalculation) scriptCalculation.clonePersist(tableNode);
                scriptCalculation = sc;
            }
            isCopy = true;
        } catch (RepositoryException e) {
            Debug.error(e);
            throw new RuntimeException("Can't alter ScriptCalculation " + scriptCalculation.getName() + ", clone failed", e);
        }
    }
}
Also used : ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) TableNode(com.servoy.j2db.persistence.TableNode) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Example 3 with ScriptCalculation

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

the class ScriptEngine method compileFunction.

@SuppressWarnings("nls")
public Function compileFunction(IScriptProvider sp, Scriptable scope) throws Exception {
    Context cx = Context.enter();
    int iOp = cx.getOptimizationLevel();
    String sourceName = sp.getDataProviderID();
    if (sp.getScopeName() != null) {
        Solution sol = (Solution) sp.getAncestor(IRepository.SOLUTIONS);
        sourceName = sol.getName() + "/scopes/" + sp.getScopeName() + '/' + sourceName;
    } else if (sp.getParent() instanceof Form) {
        Solution sol = (Solution) sp.getAncestor(IRepository.SOLUTIONS);
        sourceName = sol.getName() + "/forms/" + ((Form) sp.getParent()).getName() + '/' + sourceName;
    } else if (sp.getParent() instanceof TableNode) {
        Solution sol = (Solution) sp.getAncestor(IRepository.SOLUTIONS);
        sourceName = sol.getName() + '/' + ((TableNode) sp.getParent()).getDataSource() + '/' + sourceName;
        if (sp instanceof ScriptCalculation) {
            sourceName = sourceName.replace("db:", "calculations");
        } else {
            sourceName = sourceName.replace("db:", "entities");
        }
    }
    try {
        if (// flag should only be used in rich client
        Utils.getAsBoolean(System.getProperty(SERVOY_DISABLE_SCRIPT_COMPILE_PROPERTY, "false"))) {
            cx.setOptimizationLevel(-1);
        } else {
            cx.setOptimizationLevel(9);
        }
        cx.setGeneratingSource(Boolean.getBoolean("servoy.generateJavascriptSource"));
        return compileScriptProvider(sp, scope, cx, sourceName);
    } catch (Exception e) {
        Debug.error("Compilation failed for method: " + sourceName);
        throw e;
    } finally {
        cx.setOptimizationLevel(iOp);
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) Form(com.servoy.j2db.persistence.Form) TableNode(com.servoy.j2db.persistence.TableNode) Solution(com.servoy.j2db.persistence.Solution) ApplicationException(com.servoy.j2db.ApplicationException) ExitScriptException(com.servoy.j2db.ExitScriptException) DataException(com.servoy.j2db.dataprocessing.DataException) ServoyException(com.servoy.j2db.util.ServoyException) JavaScriptException(org.mozilla.javascript.JavaScriptException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Example 4 with ScriptCalculation

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

the class FoundSet method alldataproviders.

/**
 * Get all dataproviders of the foundset.
 *
 * @sample
 * var dataprovidersNames = %%prefix%%alldataproviders;
 * application.output("This foundset has " + dataprovidersNames.length + " data providers.")
 * for (var i=0; i<dataprovidersNames.length; i++)
 * 	application.output(dataprovidersNames[i]);
 *
 * @special
 */
@JSReadonlyProperty
public String[] alldataproviders() {
    List<String> al = new ArrayList<String>();
    Table table = (Table) getTable();
    if (table != null) {
        try {
            Iterator<Column> columnsIt = table.getColumnsSortedByName();
            while (columnsIt.hasNext()) {
                IColumn c = columnsIt.next();
                al.add(c.getDataProviderID());
            }
            Iterator<AggregateVariable> aggIt = fsm.getApplication().getFlattenedSolution().getAggregateVariables(table, true);
            while (aggIt.hasNext()) {
                AggregateVariable av = aggIt.next();
                al.add(av.getDataProviderID());
            }
            Iterator<ScriptCalculation> scriptIt = fsm.getApplication().getFlattenedSolution().getScriptCalculations(table, true);
            while (scriptIt.hasNext()) {
                ScriptCalculation sc = scriptIt.next();
                if (al.contains(sc.getDataProviderID()))
                    al.remove(sc.getDataProviderID());
                al.add(sc.getDataProviderID());
            }
        } catch (Exception ex) {
            Debug.error(ex);
        }
    }
    return al.toArray(new String[al.size()]);
}
Also used : ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) BaseQueryTable(com.servoy.base.query.BaseQueryTable) Table(com.servoy.j2db.persistence.Table) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) QueryColumn(com.servoy.j2db.query.QueryColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) IColumn(com.servoy.j2db.persistence.IColumn) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable) ServoyException(com.servoy.j2db.util.ServoyException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) JSReadonlyProperty(com.servoy.j2db.scripting.annotations.JSReadonlyProperty)

Example 5 with ScriptCalculation

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

the class DataProviderEditor method fillDataProviderList.

protected void fillDataProviderList() {
    try {
        ITable table = null;
        if (definedTable == null) {
            FormManager fm = (FormManager) application.getFormManager();
            FormController fc = fm.getCurrentMainShowingFormController();
            if (fc != null) {
                Form form = fc.getForm();
                table = application.getFlattenedSolution().getTable(form.getDataSource());
            }
        } else {
            if (!showRelatedOptionsOnly)
                table = definedTable;
        }
        DefaultListModel model = (DefaultListModel) list.getModel();
        model.removeAllElements();
        if (showNoneOption)
            model.addElement("-none-");
        if (!showColumnsOnly)
            model.addElement("*columns");
        Object o = relationsComboBox.getSelectedItem();
        if (o != null) {
            if (o instanceof String) {
            // table = form.getTable();
            } else {
                table = application.getFlattenedSolution().getTable(((Relation) o).getForeignDataSource());
            }
            if (table != null) {
                Iterator<Column> it = table.getColumnsSortedByName();
                while (it.hasNext()) {
                    IColumn c = it.next();
                    ColumnInfo ci = c.getColumnInfo();
                    if (ci != null && ci.isExcluded()) {
                        continue;
                    }
                    if (hideMediaColumns) {
                        // use dataprovider type as defined by column converter
                        ComponentFormat componentFormat = ComponentFormat.getComponentFormat(null, c, application);
                        if (componentFormat.dpType == IColumnTypes.MEDIA) {
                            continue;
                        }
                    }
                    model.addElement(c);
                }
            }
        }
        FlattenedSolution s = application.getFlattenedSolution();
        if (table != null && !showColumnsOnly) {
            Iterator it = s.getScriptCalculations(table, true);
            if (it.hasNext()) {
                model.addElement("*calculations");
            }
            while (it.hasNext()) {
                ScriptCalculation sc = (ScriptCalculation) it.next();
                for (int i = 0; i < model.size(); i++) {
                    Object obj = model.elementAt(i);
                    if (obj instanceof IDataProvider) {
                        IDataProvider dp = (IDataProvider) obj;
                        if (dp.getDataProviderID().equals(sc.getDataProviderID())) {
                            // remove the column from the list if use by
                            model.remove(i);
                            // stored calc
                            break;
                        }
                    }
                }
                model.addElement(sc);
            }
            Iterator it2 = s.getScriptVariables(true);
            if (it2.hasNext()) {
                model.addElement("*globals");
            }
            while (it2.hasNext()) {
                model.addElement(it2.next());
            }
            Iterator it3 = s.getAggregateVariables(table, true);
            if (it3.hasNext()) {
                model.addElement("*aggregates");
            }
            while (it3.hasNext()) {
                model.addElement(it3.next());
            }
        }
        if (table != null && showColumnsOnly && showSortableOnly) {
            Iterator it3 = s.getAggregateVariables(table, true);
            while (it3.hasNext()) {
                model.addElement(it3.next());
            }
        }
        if (showGlobalsOption && showColumnsOnly) {
            Iterator it2 = s.getScriptVariables(true);
            if (it2.hasNext()) {
                model.addElement("*globals");
            }
            while (it2.hasNext()) {
                model.addElement(it2.next());
            }
        }
    } catch (Exception ex) {
        Debug.error(ex);
    }
}
Also used : FormController(com.servoy.j2db.FormController) Form(com.servoy.j2db.persistence.Form) DefaultListModel(javax.swing.DefaultListModel) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) FlattenedSolution(com.servoy.j2db.FlattenedSolution) IDataProvider(com.servoy.j2db.persistence.IDataProvider) ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) Relation(com.servoy.j2db.persistence.Relation) FormManager(com.servoy.j2db.FormManager) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) Iterator(java.util.Iterator) ITable(com.servoy.j2db.persistence.ITable) ComponentFormat(com.servoy.j2db.component.ComponentFormat)

Aggregations

ScriptCalculation (com.servoy.j2db.persistence.ScriptCalculation)12 RepositoryException (com.servoy.j2db.persistence.RepositoryException)7 TableNode (com.servoy.j2db.persistence.TableNode)6 Column (com.servoy.j2db.persistence.Column)5 IColumn (com.servoy.j2db.persistence.IColumn)4 FlattenedSolution (com.servoy.j2db.FlattenedSolution)3 AggregateVariable (com.servoy.j2db.persistence.AggregateVariable)3 Form (com.servoy.j2db.persistence.Form)3 ITable (com.servoy.j2db.persistence.ITable)3 Solution (com.servoy.j2db.persistence.Solution)3 IBaseColumn (com.servoy.base.persistence.IBaseColumn)2 ApplicationException (com.servoy.j2db.ApplicationException)2 FoundSetManager (com.servoy.j2db.dataprocessing.FoundSetManager)2 ColumnInfo (com.servoy.j2db.persistence.ColumnInfo)2 IDataProvider (com.servoy.j2db.persistence.IDataProvider)2 IPersist (com.servoy.j2db.persistence.IPersist)2 Relation (com.servoy.j2db.persistence.Relation)2 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)2 QueryColumn (com.servoy.j2db.query.QueryColumn)2 ServoyException (com.servoy.j2db.util.ServoyException)2