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);
}
}
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);
}
}
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);
}
}
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;
}
}
Aggregations