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