Search in sources :

Example 1 with TableScope

use of com.servoy.j2db.scripting.TableScope in project servoy-client by Servoy.

the class JSCalculation method setVariableType.

@JSSetter
public void setVariableType(int type) {
    if (isStored())
        throw new RuntimeException("Can't alter variable type of the stored calculation " + scriptCalculation.getName());
    checkModification();
    scriptCalculation.setTypeAndCheck(type, application);
    TableScope tableScope;
    try {
        tableScope = (TableScope) application.getScriptEngine().getTableScope(scriptCalculation.getTable());
        if (tableScope != null) {
            tableScope.put(scriptCalculation, scriptCalculation);
            ((FoundSetManager) application.getFoundSetManager()).flushSQLSheet(scriptCalculation.getTable().getDataSource());
        }
    } catch (RepositoryException e) {
        Debug.error(e);
    }
}
Also used : FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) TableScope(com.servoy.j2db.scripting.TableScope) RepositoryException(com.servoy.j2db.persistence.RepositoryException) JSSetter(org.mozilla.javascript.annotations.JSSetter)

Example 2 with TableScope

use of com.servoy.j2db.scripting.TableScope 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 3 with TableScope

use of com.servoy.j2db.scripting.TableScope in project servoy-client by Servoy.

the class JSCalculation method setCode.

@JSSetter
public void setCode(String code) {
    checkModification();
    String name = JSMethod.parseName(code);
    if (!name.equals(scriptCalculation.getName())) {
        try {
            scriptCalculation.updateName(new ScriptNameValidator(application.getFlattenedSolution()), name);
        } catch (RepositoryException e) {
            // $NON-NLS-1$ //$NON-NLS-2$
            throw new RuntimeException("Error updating the name from " + scriptCalculation.getName() + " to " + name, e);
        }
    }
    scriptCalculation.setDeclaration(code);
    try {
        TableScope tableScope = (TableScope) application.getScriptEngine().getTableScope(scriptCalculation.getTable());
        if (tableScope != null) {
            tableScope.put(scriptCalculation, scriptCalculation);
            String dataSource = scriptCalculation.getTable().getDataSource();
            FoundSetManager fsm = (FoundSetManager) application.getFoundSetManager();
            fsm.flushSQLSheet(dataSource);
            fsm.getRowManager(dataSource).clearCalcs(null, Arrays.asList(scriptCalculation.getName()));
        }
    } catch (ServoyException e) {
        Debug.error(e);
    }
}
Also used : FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) TableScope(com.servoy.j2db.scripting.TableScope) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator) ServoyException(com.servoy.j2db.util.ServoyException) JSSetter(org.mozilla.javascript.annotations.JSSetter)

Example 4 with TableScope

use of com.servoy.j2db.scripting.TableScope in project servoy-client by Servoy.

the class FoundSet method getCalculationValue.

/**
 * Found set is using scriptengine to recalculate the specified calculation,check first with containsCalculation before calling
 */
public Object getCalculationValue(IRecordInternal state, String dataProviderID, Object[] vargs, UsedDataProviderTracker usedDataProviderTracker) {
    UsedDataProviderTracker current = null;
    try {
        Object obj;
        TableScope tableScope = (TableScope) fsm.getScriptEngine().getTableScope(sheet.getTable());
        tableScope.setArguments(vargs);
        current = tableScope.setUsedDataProviderTracker(usedDataProviderTracker);
        Scriptable previous = tableScope.getPrototype();
        try {
            // make sure its set correctly
            tableScope.setPrototype((Scriptable) state);
            obj = tableScope.getCalculationValue(dataProviderID, tableScope);
            if (// fix for postgress
            obj instanceof Byte) {
                obj = Integer.valueOf(((Byte) obj).intValue());
            } else {
                Column column = sheet.getTable().getColumn(dataProviderID);
                if (column != null) {
                    // TODO: check case with stored calc on column with column converter
                    if (column.getScale() > 0 && column.getDataProviderType() == IColumnTypes.NUMBER && obj != null) {
                        // if rounding results in the old value we do not have to save.
                        try {
                            obj = Utils.roundNumber(obj, column.getLength(), true);
                        } catch (Exception e) {
                            // $NON-NLS-1$
                            Debug.error("Could not round stored calculation '" + dataProviderID + '\'', e);
                        }
                    }
                }
            }
        // TODO: in developer we must check if the return type matches the one specified on a calculation otherwise relations will not work in some cases
        } finally {
            tableScope.setPrototype(previous);
            tableScope.setUsedDataProviderTracker(current);
        }
        return obj;
    } catch (Exception ex) {
        // fsm.getApplication().reportJSError(Messages.getString("servoy.error.executingCalculation",new Object[] {dataProviderID,getTable().getName(),ex.getMessage()}),ex) ;	 //$NON-NLS-1$
        fsm.getApplication().reportJSError(ex.getMessage(), ex);
        // $NON-NLS-1$
        Debug.error("error executing calc: " + dataProviderID, ex);
        return null;
    }
}
Also used : TableScope(com.servoy.j2db.scripting.TableScope) UsedDataProviderTracker(com.servoy.j2db.scripting.UsedDataProviderTracker) QueryColumn(com.servoy.j2db.query.QueryColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) Scriptable(org.mozilla.javascript.Scriptable) ServoyException(com.servoy.j2db.util.ServoyException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Aggregations

RepositoryException (com.servoy.j2db.persistence.RepositoryException)4 TableScope (com.servoy.j2db.scripting.TableScope)4 FoundSetManager (com.servoy.j2db.dataprocessing.FoundSetManager)3 ScriptNameValidator (com.servoy.j2db.persistence.ScriptNameValidator)2 ServoyException (com.servoy.j2db.util.ServoyException)2 JSSetter (org.mozilla.javascript.annotations.JSSetter)2 ApplicationException (com.servoy.j2db.ApplicationException)1 FlattenedSolution (com.servoy.j2db.FlattenedSolution)1 Column (com.servoy.j2db.persistence.Column)1 IColumn (com.servoy.j2db.persistence.IColumn)1 ScriptCalculation (com.servoy.j2db.persistence.ScriptCalculation)1 TableNode (com.servoy.j2db.persistence.TableNode)1 QueryColumn (com.servoy.j2db.query.QueryColumn)1 UsedDataProviderTracker (com.servoy.j2db.scripting.UsedDataProviderTracker)1 RemoteException (java.rmi.RemoteException)1 Scriptable (org.mozilla.javascript.Scriptable)1 JSFunction (org.mozilla.javascript.annotations.JSFunction)1