Search in sources :

Example 11 with JSDataSet

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

the class ValueListPropertyType method toSabloComponentValue.

@Override
public ValueListTypeSabloValue toSabloComponentValue(Object rhinoValue, ValueListTypeSabloValue previousComponentValue, PropertyDescription pd, IWebObjectContext webObjectContext) {
    if (rhinoValue == null || RhinoConversion.isUndefinedOrNotFound(rhinoValue))
        return null;
    if (previousComponentValue == null) {
        return rhinoValue instanceof String ? createValuelistSabloValueByNameFromRhino((String) rhinoValue, pd, webObjectContext) : null;
    }
    if (!previousComponentValue.isInitialized()) {
        if (rhinoValue instanceof String) {
            // weird; but we are going to create a new value anyway so it doesn't matter much
            return createValuelistSabloValueByNameFromRhino((String) rhinoValue, pd, webObjectContext);
        } else {
            // we cannot set values from a dataset if the previous value is not ready for it
            Debug.error("Trying to make changes (assignment) to an uninitialized valuelist property (this is not allowed): " + pd + " of " + webObjectContext, new RuntimeException());
            return previousComponentValue;
        }
    }
    ParsedFormat format = null;
    int type = -1;
    IValueList list = previousComponentValue.getValueList();
    if (list.getName().equals(rhinoValue)) {
        // no need to create a new value if we have the same valuelist name
        return previousComponentValue;
    }
    ValueListTypeSabloValue newValue;
    IValueList newVl = null;
    // see if it's a component.setValuelistItems (legacy) equivalent
    if (list != null && list instanceof CustomValueList && (rhinoValue instanceof JSDataSet || rhinoValue instanceof IDataSet)) {
        // here we create a NEW, separate (runtime) custom valuelist instance for this component only (no longer the 'global' custom valuelist with that name that can be affected by application.setValuelistItems(...))
        INGApplication application = previousComponentValue.getDataAdapterList().getApplication();
        ValueList valuelist = application.getFlattenedSolution().getValueList(list.getName());
        if (valuelist != null && valuelist.getValueListType() == IValueListConstants.CUSTOM_VALUES) {
            format = ((CustomValueList) list).getFormat();
            type = ((CustomValueList) list).getValueType();
            newVl = ValueListFactory.fillRealValueList(application, valuelist, IValueListConstants.CUSTOM_VALUES, format, type, rhinoValue);
            if (newVl != null) {
                previousComponentValue.setNewCustomValuelistInstance(newVl, rhinoValue);
                newValue = previousComponentValue;
            } else {
                // should never happen; ValueListFactory.fillRealValueList seems to always return non-null
                Debug.error("Assignment to Valuelist typed property '" + pd.getName() + "' of component '" + webObjectContext + "' failed for an unknown reason; dataset: " + rhinoValue, new RuntimeException());
                // just keep old value
                newValue = previousComponentValue;
            }
        } else {
            Debug.error("Assignment to Valuelist typed property '" + pd.getName() + "' of component '" + webObjectContext + "' failed. Assigning a dataset is ONLY allowed for custom valuelists; dataset: " + rhinoValue, new RuntimeException());
            newValue = previousComponentValue;
        }
    } else if (rhinoValue instanceof String) {
        // the Rhino value is a different valuelist name; create a full new one
        newValue = createValuelistSabloValueByNameFromRhino((String) rhinoValue, pd, webObjectContext);
    } else {
        Debug.error("Assignment to Valuelist typed property '" + pd.getName() + "' of component '" + webObjectContext + "' failed. Assigning this value is not supported: " + rhinoValue, new RuntimeException());
        // whatever was set here is not supported; so keep the previous value
        newValue = previousComponentValue;
    }
    return newValue;
}
Also used : ParsedFormat(com.servoy.j2db.util.FormatParser.ParsedFormat) CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) INGApplication(com.servoy.j2db.server.ngclient.INGApplication) ValueList(com.servoy.j2db.persistence.ValueList) JSValueList(com.servoy.j2db.scripting.solutionmodel.JSValueList) CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) IValueList(com.servoy.j2db.dataprocessing.IValueList) JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) IDataSet(com.servoy.j2db.dataprocessing.IDataSet) IValueList(com.servoy.j2db.dataprocessing.IValueList)

Example 12 with JSDataSet

use of com.servoy.j2db.dataprocessing.JSDataSet 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 13 with JSDataSet

use of com.servoy.j2db.dataprocessing.JSDataSet 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)

Example 14 with JSDataSet

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

the class RhinoConversion method defaultFromRhino.

/**
 * Default conversion used to convert from Rhino property types that do not explicitly implement component <-> Rhino conversions. <BR/><BR/>
 * Values of types that don't implement the sablo <-> rhino conversions are by default accessible directly.
 */
@SuppressWarnings("unchecked")
public static // PropertyDescription / IWebObjectContext ... can be made available here if needed
Object defaultFromRhino(// PropertyDescription / IWebObjectContext ... can be made available here if needed
Object propertyValue, // PropertyDescription / IWebObjectContext ... can be made available here if needed
Object oldValue) {
    // convert simple values to json values
    if (isUndefinedOrNotFound(propertyValue)) {
        return null;
    }
    if (propertyValue instanceof NativeDate) {
        return ((NativeDate) propertyValue).unwrap();
    }
    if (propertyValue instanceof NativeObject) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> oldMap = (oldValue instanceof Map) ? (Map<String, Object>) oldValue : null;
        NativeObject no = (NativeObject) propertyValue;
        Object[] ids = no.getIds();
        for (Object id2 : ids) {
            Object value = null;
            if (id2 instanceof String) {
                value = no.get((String) id2, no);
            } else if (id2 instanceof Symbol) {
                value = no.get((Symbol) id2, no);
            } else if (id2 instanceof Number) {
                value = no.get(((Number) id2).intValue(), no);
            }
            String id = String.valueOf(id2);
            map.put(id, defaultFromRhino(value, oldMap != null ? oldMap.get(id) : null));
        }
        return map;
    }
    if (propertyValue instanceof NativeArray) {
        List<Object> list = new ArrayList<Object>();
        List<Object> oldList = (oldValue instanceof List) ? (List<Object>) oldValue : null;
        final NativeArray no = (NativeArray) propertyValue;
        final long naLength = no.getLength();
        for (int id = 0; id < naLength; id++) {
            list.add(defaultFromRhino(no.get(id, no), oldList != null && oldList.size() > id ? oldList.get(id) : null));
        }
        return list;
    }
    if (propertyValue instanceof FormScope && ((FormScope) propertyValue).getFormController() != null)
        return ((FormScope) propertyValue).getFormController().getName();
    if (propertyValue instanceof IFormController)
        return ((IFormController) propertyValue).getName();
    if (propertyValue instanceof JSDataSet) {
        return ((JSDataSet) propertyValue).getDataSet();
    }
    if (propertyValue instanceof RhinoMapOrArrayWrapper) {
        return ((RhinoMapOrArrayWrapper) propertyValue).getWrappedValue();
    }
    if (propertyValue instanceof CharSequence) {
        return propertyValue.toString();
    }
    return propertyValue;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) HashMap(java.util.HashMap) Symbol(org.mozilla.javascript.Symbol) ArrayList(java.util.ArrayList) JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) FormScope(com.servoy.j2db.scripting.FormScope) NativeObject(org.mozilla.javascript.NativeObject) NativeDate(org.mozilla.javascript.NativeDate) NativeObject(org.mozilla.javascript.NativeObject) JSONObject(org.json.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) ArrayList(java.util.ArrayList) List(java.util.List) IFormController(com.servoy.j2db.IFormController) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JSDataSet (com.servoy.j2db.dataprocessing.JSDataSet)14 IDataSet (com.servoy.j2db.dataprocessing.IDataSet)9 BufferedDataSet (com.servoy.j2db.dataprocessing.BufferedDataSet)7 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 List (java.util.List)4 JSONObject (org.json.JSONObject)4 ScriptableObject (org.mozilla.javascript.ScriptableObject)4 ServoyException (com.servoy.j2db.util.ServoyException)3 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)3 Point (java.awt.Point)3 HashMap (java.util.HashMap)3 NativeArray (org.mozilla.javascript.NativeArray)3 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)3 ApplicationException (com.servoy.j2db.ApplicationException)2 CustomValueList (com.servoy.j2db.dataprocessing.CustomValueList)2 IValueList (com.servoy.j2db.dataprocessing.IValueList)2 IServoyAwareBean (com.servoy.j2db.dataui.IServoyAwareBean)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 ValueList (com.servoy.j2db.persistence.ValueList)2