Search in sources :

Example 6 with FoundSet

use of com.servoy.j2db.dataprocessing.FoundSet in project servoy-client by Servoy.

the class SwingForm method showSortDialog.

public void showSortDialog(IApplication app, String options) {
    ISmartClientApplication application = (ISmartClientApplication) app;
    try {
        Table t = (Table) formController.getTable();
        if (t != null) {
            List<SortColumn> sortColumns = null;
            if ((options == null || options.length() == 0) && formController.getFormModel() instanceof FoundSet) {
                sortColumns = ((FoundSet) formController.getFormModel()).getLastSortColumns();
            } else {
                sortColumns = ((FoundSetManager) application.getFoundSetManager()).getSortColumns(t, options);
            }
            Window window = SwingUtilities.getWindowAncestor(this);
            if (window == null)
                window = application.getMainApplicationFrame();
            // $NON-NLS-1$
            SortDialog nfd = (SortDialog) application.getWindow("SortDialog");
            if (nfd == null || nfd.getOwner() != window) {
                if (window instanceof Frame) {
                    nfd = new SortDialog((Frame) window, application);
                } else if (window instanceof Dialog) {
                    nfd = new SortDialog((Dialog) window, application);
                }
                // $NON-NLS-1$
                application.registerWindow("SortDialog", nfd);
            }
            List<SortColumn> list = nfd.showDialog(t, sortColumns);
            if (list != null)
                formController.sort(list, false);
        }
    } catch (Exception ex) {
        // $NON-NLS-1$
        application.reportError(Messages.getString("servoy.formPanel.error.sortRecordsDialog"), ex);
    }
}
Also used : Window(java.awt.Window) ISmartClientApplication(com.servoy.j2db.ISmartClientApplication) JFrame(javax.swing.JFrame) Frame(java.awt.Frame) Table(com.servoy.j2db.persistence.Table) JTable(javax.swing.JTable) Dialog(java.awt.Dialog) FormDialog(com.servoy.j2db.gui.FormDialog) JDialog(javax.swing.JDialog) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) SortColumn(com.servoy.j2db.dataprocessing.SortColumn)

Example 7 with FoundSet

use of com.servoy.j2db.dataprocessing.FoundSet in project servoy-client by Servoy.

the class JSDataSource method loadRecords.

/**
 * get a new foundset containing records based on a dataset of pks.
 *
 * @sample var fs = datasources.db.example_data.customers.loadRecords(pkDataSet)
 *
 * @param dataSet
 * @return a new JSFoundset
 * @throws ServoyException
 */
@JSFunction
public IFoundSet loadRecords(IDataSet dataSet) throws ServoyException {
    IFoundSet foundset = application.getFoundSetManager().getFoundSet(datasource);
    ((FoundSet) foundset).js_loadRecords(dataSet);
    return checkDataSourceEquality(foundset) ? foundset : null;
}
Also used : IFoundSet(com.servoy.j2db.dataprocessing.IFoundSet) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) IFoundSet(com.servoy.j2db.dataprocessing.IFoundSet) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 8 with FoundSet

use of com.servoy.j2db.dataprocessing.FoundSet in project servoy-client by Servoy.

the class JSUtils method js_stringReplaceTags.

/**
 * Returns the text with %%tags%% replaced, based on provided record or foundset or form.
 *
 * @sample
 * //Next line places a string in variable x, whereby the tag(%%TAG%%) is filled with the value of the database column 'company_name' of the selected record.
 * var x = utils.stringReplaceTags("The companyName of the selected record is %%company_name%% ", foundset)
 * //var otherExample = utils.stringReplaceTags("The amount of the related order line %%amount%% ", order_to_orderdetails);
 * //var recordExample = utils.stringReplaceTags("The amount of the related order line %%amount%% ", order_to_orderdetails.getRecord(i);
 * //Next line places a string in variable y, whereby the tag(%%TAG%%) is filled with the value of the form variable 'x' of the form named 'main'.
 * //var y = utils.stringReplaceTags("The value of form variable is %%x%% ", forms.main);
 * //The next sample shows the use of a javascript object
 * //var obj = new Object();//create a javascript object
 * //obj['x'] = 'test';//assign an named value
 * //var y = utils.stringReplaceTags("The value of object variable is %%x%% ", obj);//use the named value in a tag
 * @param text the text tags to work with
 * @param scriptable the javascript object or foundset,record,form to be used to fill in the tags
 * @return the text with replaced tags
 */
public String js_stringReplaceTags(String text, Object scriptable) {
    if (text != null) {
        ITagResolver tagResolver = null;
        Properties settings = null;
        if (scriptable instanceof FoundSet) {
            IRecordInternal record = ((FoundSet) scriptable).getRecord(((FoundSet) scriptable).getSelectedIndex());
            if (record != null) {
                settings = record.getParentFoundSet().getFoundSetManager().getApplication().getSettings();
                tagResolver = TagResolver.createResolver(record);
            }
        } else if (scriptable instanceof IRecordInternal) {
            IRecordInternal record = (IRecordInternal) scriptable;
            settings = record.getParentFoundSet().getFoundSetManager().getApplication().getSettings();
            tagResolver = TagResolver.createResolver(record);
        } else if (scriptable instanceof BasicFormController) {
            final BasicFormController fc = (BasicFormController) scriptable;
            IFoundSetInternal fs = fc.getFoundSet();
            final ITagResolver defaultTagResolver = (fs != null) ? TagResolver.createResolver(fs.getRecord(fs.getSelectedIndex())) : null;
            settings = fc.getApplication().getSettings();
            tagResolver = new ITagResolver() {

                public String getStringValue(String name) {
                    Object value = fc.getFormScope().get(name);
                    if (value == null || value == Scriptable.NOT_FOUND) {
                        value = defaultTagResolver != null ? defaultTagResolver.getStringValue(name) : null;
                    }
                    // $NON-NLS-1$
                    return value != null ? value.toString() : "";
                }
            };
        } else if (scriptable instanceof Scriptable) {
            Scriptable scriptObject = (Scriptable) scriptable;
            settings = Settings.getInstance();
            tagResolver = TagResolver.createResolver(scriptObject, application);
        }
        if (tagResolver != null && settings != null) {
            return Text.processTags(TagResolver.formatObject(text, application), tagResolver);
        }
        // $NON-NLS-1$
        return "";
    } else {
        // $NON-NLS-1$
        return "";
    }
}
Also used : IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) ITagResolver(com.servoy.base.util.ITagResolver) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) IJSFoundSet(com.servoy.base.scripting.api.IJSFoundSet) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) Properties(java.util.Properties) Scriptable(org.mozilla.javascript.Scriptable) BasicFormController(com.servoy.j2db.BasicFormController)

Example 9 with FoundSet

use of com.servoy.j2db.dataprocessing.FoundSet in project servoy-client by Servoy.

the class ScriptEngine method compileScriptProvider.

/**
 * @param sp
 * @param scope
 * @param cx
 * @return
 */
@SuppressWarnings("nls")
protected Function compileScriptProvider(IScriptProvider sp, Scriptable scope, Context cx, String sourceName) {
    String declaration = sp.getDeclaration();
    // dont return the calc function itself but still the value.
    if (sp instanceof ScriptCalculation) {
        declaration = extractFunction(declaration, "function $1_");
    } else {
        declaration = extractFunction(declaration, "function $1");
    }
    // f below always seems to be NativeFunction instance as both Codegen.createFunctionObject and Interpreter.createFunctionObject return
    // a NativeFunction instance; and that is what cx.compileFunction(...) ends up calling
    Function f = cx.compileFunction(scope, declaration, sourceName, sp.getLineNumberOffset(), null);
    if (!(sp instanceof ScriptCalculation)) {
        if (sp.getScopeName() != null) {
            // $NON-NLS-1$
            f.put("_scopename_", f, sp.getScopeName());
        }
        // $NON-NLS-1$
        f.put("_methodname_", f, sp.getDataProviderID());
        f.put("_AllowToRunInFind_", f, Boolean.valueOf(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
        sp.getDeclaration().indexOf("@AllowToRunInFind") != -1 || declaration.indexOf(".search") != -1 || declaration.indexOf("controller.loadAllRecords") != -1));
    }
    String methodName = sp.getName();
    PerformanceData performanceData = application instanceof IPerformanceDataProvider ? ((IPerformanceDataProvider) application).getPerformanceData() : null;
    if (performanceData != null) {
        String scopeName = scope.getClassName();
        if (scope instanceof LazyCompilationScope) {
            scopeName = ((LazyCompilationScope) scope).getScopeName();
            scopeName = lookForSuperCalls(sp, scopeName);
        } else if (scope.getParentScope() instanceof LazyCompilationScope) {
            scopeName = ((LazyCompilationScope) scope.getParentScope()).getScopeName();
            scopeName = lookForSuperCalls(sp, scopeName);
        } else if (scope instanceof FoundSet) {
            Scriptable parentScope = ((FoundSet) scope).getPrototype();
            if (parentScope instanceof LazyCompilationScope) {
                scopeName = ((LazyCompilationScope) parentScope).getScopeName();
            }
        }
        methodName = scopeName + "." + methodName;
        return new FunctionWrapper(f, methodName, performanceData, application.getClientID());
    }
    return f;
}
Also used : ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) NativeFunction(org.mozilla.javascript.NativeFunction) Function(org.mozilla.javascript.Function) IPerformanceDataProvider(com.servoy.j2db.server.shared.IPerformanceDataProvider) PerformanceData(com.servoy.j2db.server.shared.PerformanceData) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) Scriptable(org.mozilla.javascript.Scriptable)

Example 10 with FoundSet

use of com.servoy.j2db.dataprocessing.FoundSet in project servoy-client by Servoy.

the class ScriptVariableScope method put.

public Object put(String name, Object val) {
    Object value = Utils.mapToNullIfUnmanageble(val);
    Integer variableType = nameType.get(name);
    int type = 0;
    boolean xmlType = false;
    if (variableType == null) {
        // global doesn't exist. dynamic new one.. so MEDIA type
        type = IColumnTypes.MEDIA;
        nameType.put(name, new Integer(type));
        // $NON-NLS-1$//$NON-NLS-2$
        Debug.trace("Warning: " + name + " is not defined in the variables, a dynamic (media type) variable is created");
    } else {
        if (variableType.intValue() == Types.OTHER) {
            type = IColumnTypes.MEDIA;
            xmlType = true;
        } else {
            type = Column.mapToDefaultType(variableType.intValue());
        }
    }
    if (type == IColumnTypes.TEXT) {
        Object txt = value;
        while (txt instanceof IDelegate<?>) {
            txt = ((IDelegate<?>) txt).getDelegate();
        }
        if (txt instanceof IDataSet) {
            IDataSet set = (IDataSet) txt;
            StringBuilder sb = new StringBuilder();
            sb.append('\n');
            for (int i = 0; i < set.getRowCount(); i++) {
                sb.append(set.getRow(i)[0]);
                sb.append('\n');
            }
            value = sb.toString();
        } else if (txt instanceof FoundSet) {
            StringBuilder sb = new StringBuilder();
            sb.append('\n');
            FoundSet fs = (FoundSet) txt;
            for (int i = 0; i < fs.getSize(); i++) {
                IRecordInternal record = fs.getRecord(i);
                sb.append(record.getPKHashKey());
                sb.append('\n');
            }
            value = sb.toString();
        }
    }
    if (value != null && variableType != null) {
        Object unwrapped = value;
        while (unwrapped instanceof Wrapper) {
            unwrapped = ((Wrapper) unwrapped).unwrap();
            if (unwrapped == value) {
                break;
            }
        }
        if (type == IColumnTypes.MEDIA) {
            if (!(unwrapped instanceof UUID)) {
                Object previousValue = get(name);
                if (previousValue instanceof UUID || previousValue == null) {
                    Iterator<ScriptVariable> scriptVariablesIte = getScriptLookup().getScriptVariables(false);
                    ScriptVariable sv;
                    while (scriptVariablesIte.hasNext()) {
                        sv = scriptVariablesIte.next();
                        if (name.equals(sv.getName())) {
                            if (UUID.class.getSimpleName().equals(getDeclaredType(sv))) {
                                value = Utils.getAsUUID(unwrapped, false);
                            }
                            break;
                        }
                    }
                }
            }
        } else {
            value = unwrapped;
            try {
                // dont convert with timezone here, its not ui but from scripting
                value = Column.getAsRightType(variableType.intValue(), IBaseColumn.NORMAL_COLUMN, value, null, Integer.MAX_VALUE, null, true, false);
            } catch (Exception e) {
                throw new IllegalArgumentException(// $NON-NLS-1$
                Messages.getString(// $NON-NLS-1$
                "servoy.conversion.error.global", new Object[] { name, Column.getDisplayTypeString(variableType.intValue()), value }));
            }
        }
    }
    if (value instanceof Date) {
        // make copy so then when it is further used in js it won't change this one.
        value = new Date(((Date) value).getTime());
    } else if (xmlType && value instanceof String) {
        // $NON-NLS-1$
        value = evalValue(name, (String) value, "internal_anon", -1);
    }
    Object oldVar = allVars.get(name);
    allVars.put(name, value);
    if (variableType != null && !Utils.equalObjects(oldVar, value)) {
        fireModificationEvent(name, value);
    }
    return oldVar;
}
Also used : Wrapper(org.mozilla.javascript.Wrapper) IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) WrappedException(org.mozilla.javascript.WrappedException) Date(java.util.Date) ScriptVariable(com.servoy.j2db.persistence.ScriptVariable) XMLObject(org.mozilla.javascript.xml.XMLObject) IDataSet(com.servoy.j2db.dataprocessing.IDataSet) UUID(com.servoy.j2db.util.UUID) IDelegate(com.servoy.j2db.util.IDelegate)

Aggregations

FoundSet (com.servoy.j2db.dataprocessing.FoundSet)19 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)7 IFoundSet (com.servoy.j2db.dataprocessing.IFoundSet)6 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)6 ISwingFoundSet (com.servoy.j2db.dataprocessing.ISwingFoundSet)6 ArrayList (java.util.ArrayList)5 Scriptable (org.mozilla.javascript.Scriptable)5 IJSFoundSet (com.servoy.base.scripting.api.IJSFoundSet)4 RelatedFoundSet (com.servoy.j2db.dataprocessing.RelatedFoundSet)4 SortColumn (com.servoy.j2db.dataprocessing.SortColumn)3 ViewFoundSet (com.servoy.j2db.dataprocessing.ViewFoundSet)3 RepositoryException (com.servoy.j2db.persistence.RepositoryException)3 ITwoNativeJavaObject (com.servoy.j2db.scripting.ITwoNativeJavaObject)3 ServoyException (com.servoy.j2db.util.ServoyException)3 Function (org.mozilla.javascript.Function)3 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)3 JSFunction (org.mozilla.javascript.annotations.JSFunction)3 FormController (com.servoy.j2db.FormController)2 FormManager (com.servoy.j2db.FormManager)2 FoundSetManager (com.servoy.j2db.dataprocessing.FoundSetManager)2