Search in sources :

Example 16 with IDataSet

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

the class WebForm method getFormContext.

public JSDataSet getFormContext() {
    WebForm current = this;
    ITabPanel currentTabPanel = null;
    String currentBeanName = null;
    WebSplitPane currentSplitPane = null;
    IDataSet set = new BufferedDataSet(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$//$NON-NLS-5$//$NON-NLS-6$
    new String[] { "containername", "formname", "tabpanel/splitpane/accordion/beanname", "tabname", "tabindex", "tabindex1based" }, new ArrayList<Object[]>());
    set.addRow(new Object[] { null, current.formController.getName(), null, null, null, null });
    MarkupContainer parent = getParent();
    while (parent != null) {
        if (parent instanceof WebSplitPane) {
            currentSplitPane = (WebSplitPane) parent;
        } else if (parent instanceof ITabPanel) {
            currentTabPanel = (ITabPanel) parent;
        } else if (parent instanceof IServoyAwareBean && parent instanceof IComponent) {
            currentBeanName = ((IComponent) parent).getName();
        } else if (parent instanceof WebForm) {
            if (currentTabPanel != null) {
                int index = -1;
                String tabName = null;
                if (currentTabPanel instanceof WebTabPanel) {
                    index = ((WebTabPanel) currentTabPanel).getTabIndex(current);
                } else if (currentTabPanel instanceof WebAccordionPanel) {
                    index = ((WebAccordionPanel) currentTabPanel).getTabIndex(current);
                }
                if (index != -1) {
                    // js method so +1
                    tabName = currentTabPanel.getTabNameAt(index);
                }
                current = (WebForm) parent;
                set.addRow(0, new Object[] { null, current.formController.getName(), currentTabPanel.getName(), tabName, new Integer(index), new Integer(index + 1) });
            } else if (currentBeanName != null) {
                current = (WebForm) parent;
                set.addRow(0, new Object[] { null, current.formController.getName(), currentBeanName, null, null, null });
            } else if (currentSplitPane != null) {
                int idx = currentSplitPane.getLeftForm() != null && current.equals(((WebTabFormLookup) currentSplitPane.getLeftForm()).getWebForm()) ? 0 : 1;
                current = (WebForm) parent;
                set.addRow(0, new Object[] { null, current.formController.getName(), currentSplitPane.getName(), currentSplitPane.getTabNameAt(idx), new Integer(idx + 1), new Integer(idx + 1) });
            }
            current = (WebForm) parent;
            currentTabPanel = null;
            currentBeanName = null;
            currentSplitPane = null;
        } else if (parent instanceof MainPage) {
            String containerName = ((MainPage) parent).getContainerName();
            if (containerName != null) {
                for (int i = 0; i < set.getRowCount(); i++) {
                    set.getRow(i)[0] = containerName;
                }
            }
            return new JSDataSet(formController.getApplication(), set);
        }
        parent = parent.getParent();
    }
    return new JSDataSet(formController.getApplication(), set);
}
Also used : MarkupContainer(org.apache.wicket.MarkupContainer) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) IComponent(com.servoy.j2db.ui.IComponent) JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) ITabPanel(com.servoy.j2db.ui.ITabPanel) WebTabFormLookup(com.servoy.j2db.server.headlessclient.dataui.WebTabFormLookup) WebSplitPane(com.servoy.j2db.server.headlessclient.dataui.WebSplitPane) Point(java.awt.Point) IServoyAwareBean(com.servoy.j2db.dataui.IServoyAwareBean) BufferedDataSet(com.servoy.j2db.dataprocessing.BufferedDataSet) WebAccordionPanel(com.servoy.j2db.server.headlessclient.dataui.WebAccordionPanel) WebTabPanel(com.servoy.j2db.server.headlessclient.dataui.WebTabPanel) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) IDataSet(com.servoy.j2db.dataprocessing.IDataSet)

Example 17 with IDataSet

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

the class RhinoConversion method defaultToRhino.

/**
 * Default conversion used to convert to Rhino property types that do not explicitly implement component <-> Rhino conversions. <BR/><BR/>
 * Types that don't implement the sablo <-> rhino conversions are by default available and their value is accessible directly.
 */
public static Object defaultToRhino(final Object webComponentValue, final PropertyDescription pd, final IWebObjectContext webObjectContext, Scriptable startScriptable) {
    if (webComponentValue == null) {
        // here we don't know if it's supposed to be null or undefined because for example if value comes from browser to sablo value null (undefined in JSON) will be null but JSONObject.NULL will also be converted into null
        return null;
    }
    if (webComponentValue == JSONObject.NULL) {
        // will probably never happen as you don't usually have JSONObject.NULL as a sablo java value; see comment above
        return null;
    }
    if (webComponentValue instanceof IDataSet) {
        return new JSDataSet((IDataSet) webComponentValue);
    }
    if (webComponentValue instanceof List && !(webComponentValue instanceof NativeArray)) {
        List<?> array = (List<?>) webComponentValue;
        Context cx = Context.enter();
        Scriptable newObject = null;
        try {
            final boolean[] initializing = new boolean[] { true };
            newObject = new // see comment on this constant
            NativeArray(// see comment on this constant
            MAX_NATIVE_ARRAY_LENGTH) {

                @Override
                public void put(int index, Scriptable start, Object value) {
                    super.put(index, start, value);
                    if (!initializing[0]) {
                        value = defaultFromRhino(value, null);
                        if (index < ((List) webComponentValue).size()) {
                            if (!Utils.equalObjects(((List) webComponentValue).get(index), value)) {
                                ((List) webComponentValue).set(index, value);
                                if (webObjectContext != null && pd != null)
                                    webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
                            }
                        } else {
                            for (int i = ((List) webComponentValue).size(); i < index; i++) {
                                ((List) webComponentValue).add(i, null);
                            }
                            ((List) webComponentValue).add(index, value);
                            if (webObjectContext != null && pd != null)
                                webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
                        }
                    }
                }

                @Override
                public void delete(int index) {
                    super.delete(index);
                    if (!initializing[0]) {
                        if (index < ((List) webComponentValue).size()) {
                            ((List) webComponentValue).remove(index);
                        }
                        if (webObjectContext != null && pd != null)
                            webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
                    }
                }
            };
            ScriptableObject.putProperty(newObject, "length", array.size());
            ScriptRuntime.setBuiltinProtoAndParent((NativeArray) newObject, startScriptable, TopLevel.Builtins.Array);
            for (int i = 0; i < array.size(); i++) {
                Object value = null;
                try {
                    value = array.get(i);
                } catch (JSONException e) {
                    Debug.error(e);
                }
                if (value != null) {
                    value = defaultToRhino(value, pd, webObjectContext, startScriptable);
                }
                ScriptRuntime.setObjectIndex(newObject, i, value, cx);
            }
            initializing[0] = false;
            return newObject;
        } finally {
            Context.exit();
        }
    }
    if (webComponentValue instanceof Map && !(webComponentValue instanceof NativeObject)) {
        Map<?, ?> map = (Map) webComponentValue;
        Context cx = Context.enter();
        Scriptable newObject = null;
        try {
            final boolean[] initializing = new boolean[] { true };
            // code from Context.newObject(Scriptable)
            newObject = new NativeObject() {

                @Override
                public void put(String name, Scriptable start, Object value) {
                    super.put(name, start, value);
                    if (!initializing[0]) {
                        value = defaultFromRhino(value, null);
                        if (!Utils.equalObjects(((Map) webComponentValue).get(name), value)) {
                            ((Map) webComponentValue).put(name, value);
                            if (webObjectContext != null)
                                webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
                        }
                    }
                }

                @Override
                public void delete(String name) {
                    super.delete(name);
                    if (!initializing[0]) {
                        ((Map) webComponentValue).remove(name);
                        if (webObjectContext != null)
                            webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
                    }
                }
            };
            ScriptRuntime.setBuiltinProtoAndParent((NativeObject) newObject, startScriptable, TopLevel.Builtins.Object);
            for (Object key : ((Map) webComponentValue).keySet()) {
                ((NativeObject) newObject).defineProperty(key.toString(), defaultToRhino(((Map) webComponentValue).get(key), pd, webObjectContext, startScriptable), ScriptableObject.EMPTY);
            }
            initializing[0] = false;
            return newObject;
        } finally {
            Context.exit();
        }
    }
    if (webComponentValue instanceof JSONObject) {
        JSONObject json = (JSONObject) webComponentValue;
        Context cx = Context.enter();
        Scriptable newObject = null;
        try {
            final boolean[] initializing = new boolean[] { true };
            // code from Context.newObject(Scriptable)
            newObject = new NativeObject() {

                private final JSONConverter converter = new JSONConverter();

                @Override
                public void put(String name, Scriptable start, Object value) {
                    super.put(name, start, value);
                    if (!initializing[0]) {
                        try {
                            value = converter.convertToJSONValue(value);
                        } catch (Exception e) {
                            Debug.error(e);
                        }
                        if (!Utils.equalObjects(json.opt(name), value)) {
                            json.put(name, value);
                            if (webObjectContext != null)
                                webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
                        }
                    }
                }

                @Override
                public void delete(String name) {
                    super.delete(name);
                    if (!initializing[0]) {
                        json.remove(name);
                        if (webObjectContext != null)
                            webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
                    }
                }
            };
            ScriptRuntime.setBuiltinProtoAndParent((NativeObject) newObject, startScriptable, TopLevel.Builtins.Object);
            Iterator<String> iterator = json.keys();
            while (iterator.hasNext()) {
                String key = iterator.next();
                Object value = null;
                try {
                    value = ServoyJSONObject.jsonNullToNull(json.get(key));
                } catch (JSONException e) {
                    Debug.error(e);
                }
                if (value != null) {
                    value = defaultToRhino(value, pd, webObjectContext, startScriptable);
                }
                ScriptRuntime.setObjectElem(newObject, key, value, cx);
            }
            initializing[0] = false;
            return newObject;
        } finally {
            Context.exit();
        }
    }
    if (webComponentValue instanceof JSONArray) {
        JSONArray array = (JSONArray) webComponentValue;
        Context cx = Context.enter();
        NativeArray newObject = null;
        try {
            final boolean[] initializing = new boolean[] { true };
            newObject = new // see comment on this constant
            NativeArray(// see comment on this constant
            MAX_NATIVE_ARRAY_LENGTH) {

                private final JSONConverter converter = new JSONConverter();

                @Override
                public void put(int index, Scriptable start, Object value) {
                    super.put(index, start, value);
                    if (!initializing[0]) {
                        try {
                            value = converter.convertToJSONValue(value);
                        } catch (Exception e) {
                            Debug.error(e);
                        }
                        if (!Utils.equalObjects(array.opt(index), value)) {
                            array.put(index, value);
                            if (webObjectContext != null)
                                webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
                        }
                    }
                }

                @Override
                public void delete(int index) {
                    super.delete(index);
                    if (!initializing[0]) {
                        array.remove(index);
                        if (webObjectContext != null)
                            webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
                    }
                }
            };
            ScriptableObject.putProperty(newObject, "length", array.length());
            ScriptRuntime.setBuiltinProtoAndParent(newObject, startScriptable, TopLevel.Builtins.Array);
            for (int i = 0; i < array.length(); i++) {
                Object value = null;
                try {
                    value = array.get(i);
                } catch (JSONException e) {
                    Debug.error(e);
                }
                if (value != null) {
                    value = defaultToRhino(value, pd, webObjectContext, startScriptable);
                }
                ScriptRuntime.setObjectIndex(newObject, i, value, cx);
            }
            initializing[0] = false;
            return newObject;
        } finally {
            Context.exit();
        }
    }
    return webComponentValue;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) IWebObjectContext(org.sablo.IWebObjectContext) Context(org.mozilla.javascript.Context) JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Scriptable(org.mozilla.javascript.Scriptable) JSONException(org.json.JSONException) NativeObject(org.mozilla.javascript.NativeObject) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) ArrayList(java.util.ArrayList) List(java.util.List) NativeObject(org.mozilla.javascript.NativeObject) JSONObject(org.json.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IDataSet(com.servoy.j2db.dataprocessing.IDataSet) HashMap(java.util.HashMap) Map(java.util.Map) JSONConverter(com.servoy.j2db.util.serialize.JSONConverter)

Aggregations

IDataSet (com.servoy.j2db.dataprocessing.IDataSet)17 JSDataSet (com.servoy.j2db.dataprocessing.JSDataSet)9 BufferedDataSet (com.servoy.j2db.dataprocessing.BufferedDataSet)5 QuerySelect (com.servoy.j2db.query.QuerySelect)5 Column (com.servoy.j2db.persistence.Column)4 CompareCondition (com.servoy.j2db.query.CompareCondition)4 QueryColumn (com.servoy.j2db.query.QueryColumn)4 ArrayList (java.util.ArrayList)4 IDataServer (com.servoy.j2db.dataprocessing.IDataServer)3 QueryColumnValue (com.servoy.j2db.query.QueryColumnValue)3 QueryTable (com.servoy.j2db.query.QueryTable)3 UUID (com.servoy.j2db.util.UUID)3 Point (java.awt.Point)3 HashMap (java.util.HashMap)3 JSONObject (org.json.JSONObject)3 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)3 ScriptableObject (org.mozilla.javascript.ScriptableObject)3 ApplicationException (com.servoy.j2db.ApplicationException)2 CustomValueList (com.servoy.j2db.dataprocessing.CustomValueList)2 FoundSet (com.servoy.j2db.dataprocessing.FoundSet)2