Search in sources :

Example 11 with NativeJavaArray

use of org.mozilla.javascript.NativeJavaArray in project servoy-client by Servoy.

the class NGCustomJSONArrayType method toSabloComponentValue.

@Override
public Object toSabloComponentValue(final Object rhinoValue, Object previousComponentValue, PropertyDescription pd, final IWebObjectContext componentOrService) {
    if (rhinoValue == null || RhinoConversion.isUndefinedOrNotFound(rhinoValue))
        return null;
    final ChangeAwareList<SabloT, SabloWT> previousSpecialArray = (ChangeAwareList<SabloT, SabloWT>) previousComponentValue;
    if (rhinoValue instanceof RhinoMapOrArrayWrapper) {
        return ((RhinoMapOrArrayWrapper) rhinoValue).getWrappedValue();
    } else {
        // if it's some kind of array
        // we always make a new copy to simplify code; so previous Rhino reference in js code should no longer be used after this conversion
        List<SabloT> rhinoArrayCopy = null;
        PropertyDescription elementPD = getCustomJSONTypeDefinition();
        if (rhinoValue instanceof NativeArray) {
            rhinoArrayCopy = new ArrayList<SabloT>();
            NativeArray nativeArray = (NativeArray) rhinoValue;
            for (Object element : nativeArray) {
                rhinoArrayCopy.add((SabloT) NGConversions.INSTANCE.convertRhinoToSabloComponentValue(element, null, elementPD, componentOrService));
            }
        } else if (rhinoValue instanceof NativeJavaArray) {
            rhinoArrayCopy = new ArrayList<SabloT>();
            NativeJavaArray nativeJavaArray = (NativeJavaArray) rhinoValue;
            int length = ((Integer) nativeJavaArray.get("length", nativeJavaArray)).intValue();
            for (int i = 0; i < length; i++) {
                rhinoArrayCopy.add((SabloT) NGConversions.INSTANCE.convertRhinoToSabloComponentValue(nativeJavaArray.get(i, nativeJavaArray), null, elementPD, componentOrService));
            }
        } else
            Debug.warn(// $NON-NLS-1$
            "Cannot convert value assigned from solution scripting into array property type; class: " + rhinoValue.getClass() + ", new value = " + rhinoValue + // $NON-NLS-1$ //$NON-NLS-2$
            "; property = " + pd.getName() + "; component name = " + // $NON-NLS-1$
            componentOrService.getUnderlyingWebObject().getName());
        if (rhinoArrayCopy != null) {
            return wrap(rhinoArrayCopy, previousSpecialArray, pd, new WrappingContext(componentOrService.getUnderlyingWebObject(), pd.getName()));
        }
    }
    // or should we return null or throw exception here? incompatible thing was assigned
    return previousComponentValue;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) WrappingContext(org.sablo.specification.property.WrappingContext) ArrayList(java.util.ArrayList) RhinoMapOrArrayWrapper(com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) ChangeAwareList(org.sablo.specification.property.ChangeAwareList) PropertyDescription(org.sablo.specification.PropertyDescription) JSONObject(org.json.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject)

Example 12 with NativeJavaArray

use of org.mozilla.javascript.NativeJavaArray in project servoy-client by Servoy.

the class PluginScope method get.

/**
 * @see com.servoy.j2db.scripting.DefaultScope#get(java.lang.String, org.mozilla.javascript.Scriptable)
 */
@Override
public Object get(String name, Scriptable start) {
    if (// $NON-NLS-1$
    "length".equals(name)) {
        return new Integer(application.getPluginManager().getPlugins(IClientPlugin.class).size());
    }
    if (// $NON-NLS-1$
    "allnames".equals(name)) {
        Context.enter();
        try {
            List<IClientPlugin> lst = application.getPluginManager().getPlugins(IClientPlugin.class);
            Object[] array = new String[lst.size()];
            for (int i = 0; i < lst.size(); i++) {
                IClientPlugin plugin = lst.get(i);
                array[i] = plugin.getName();
            }
            Arrays.sort(array);
            return new NativeJavaArray(this, array);
        } finally {
            Context.exit();
        }
    }
    String realName = name;
    // we bought, just map, has to backwards compatible anyway //$NON-NLS-1$ //$NON-NLS-2$
    if ("it2be_menubar".equals(realName))
        realName = "menubar";
    Object o = super.get(realName, start);
    if (o == Scriptable.NOT_FOUND || o == null) {
        if (// performance optimize //$NON-NLS-1$
        realName.equals("Function")) {
            return Scriptable.NOT_FOUND;
        }
        IScriptable tocall = null;
        IClientPlugin plugin = application.getPluginManager().getPlugin(IClientPlugin.class, realName);
        if (plugin == null) {
            // original name not found, try updated name
            realName = getUpdatedPluginName(name);
            if (!name.equals(realName)) {
                plugin = application.getPluginManager().getPlugin(IClientPlugin.class, realName);
            }
        }
        if (plugin != null) {
            try {
                Method method = plugin.getClass().getMethod("getScriptObject", (Class[]) null);
                if (method != null) {
                    tocall = (IScriptable) method.invoke(plugin, (Object[]) null);
                }
            } catch (Exception e) {
                Debug.error(e);
            }
        }
        if (tocall != null) {
            // first register the script object itself
            ScriptObjectRegistry.registerScriptObjectForClass(tocall.getClass(), tocall);
            ServoyNativeJavaObject s_tocall = null;
            Context.enter();
            try {
                InstanceJavaMembers ijm = new InstanceJavaMembers(this, tocall.getClass());
                s_tocall = new ServoyNativeJavaObject(this, tocall, ijm);
                setLocked(false);
                // save so we do not all this again
                put(realName, this, s_tocall);
                setLocked(true);
                IExecutingEnviroment scriptEngine = application.getScriptEngine();
                if (scriptEngine != null && tocall instanceof IReturnedTypesProvider) {
                    scriptEngine.registerScriptObjectReturnTypes((IReturnedTypesProvider) tocall, s_tocall);
                }
            } finally {
                Context.exit();
            }
            return s_tocall;
        }
        return Scriptable.NOT_FOUND;
    }
    return o;
}
Also used : Method(java.lang.reflect.Method) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) IClientPlugin(com.servoy.j2db.plugins.IClientPlugin)

Example 13 with NativeJavaArray

use of org.mozilla.javascript.NativeJavaArray in project servoy-client by Servoy.

the class CreationalPrototype method get.

@Override
public Object get(java.lang.String name, Scriptable start) {
    if (// $NON-NLS-1$
    "allnames".equals(name)) {
        ArrayList<String> al = new ArrayList<String>();
        IBasicFormManager fm = application.getFormManager();
        if (fm == null)
            throw new IllegalStateException(// should never happen during normal operation; see case 251716
            "Trying to access forms after client was shut down? This JS code was probably running decoupled from client shut down but at the same time.");
        Iterator<String> it = fm.getPossibleFormNames();
        while (it.hasNext()) {
            al.add(it.next());
        }
        Context.enter();
        try {
            return new NativeJavaArray(this, al.toArray());
        } finally {
            Context.exit();
        }
    }
    Object o = super.get(name, start);
    if ((o == null || o == Scriptable.NOT_FOUND)) {
        IBasicFormManager fm = application.getFormManager();
        if (fm == null)
            throw new IllegalStateException(// should never happen during normal operation; see case 251716
            "Trying to access forms after client was shut down? This JS code was probably running decoupled from client shut down but at the same time.");
        if (!fm.isPossibleForm(name)) {
            return o;
        }
        Context.enter();
        try {
            // $NON-NLS-1$ //$NON-NLS-2$
            Debug.trace("CreationalPrototype:get " + name + " scope " + start);
            if (!application.isEventDispatchThread()) {
                // $NON-NLS-1$ //$NON-NLS-2$
                Debug.log("Form " + name + " is not created because it is CreationalPrototype.get(formname) is called outside of the event thread");
                // $NON-NLS-1$ //$NON-NLS-2$
                return "<Form " + name + " not loaded yet>";
            }
            IFormController fp = fm.leaseFormPanel(name);
            if (fp != null) {
                // This registers the object in this scopes, this must called before anything else! (to prevent repeative calls/lookups here)
                fp.initForJSUsage(this);
                // re-get
                o = super.get(name, this);
            }
            if (o == null)
                o = Scriptable.NOT_FOUND;
        } finally {
            Context.exit();
        }
    }
    if (o instanceof FormScope) {
        IFormController fp = ((FormScope) o).getFormController();
        fp.setView(fp.getView());
        fp.executeOnLoadMethod();
        try {
            if (!fp.isShowingData()) {
                if (fp.wantEmptyFoundSet()) {
                    if (fp.getFormModel() != null)
                        fp.getFormModel().clear();
                } else {
                    fp.loadAllRecordsImpl(true);
                }
            }
        } catch (Exception ex) {
            Debug.error(ex);
            // $NON-NLS-1$
            application.handleException(application.getI18NMessage("servoy.formPanel.error.formData"), ex);
        }
    }
    return o;
}
Also used : ArrayList(java.util.ArrayList) IBasicFormManager(com.servoy.j2db.IBasicFormManager) IFormController(com.servoy.j2db.IFormController) NativeJavaArray(org.mozilla.javascript.NativeJavaArray)

Example 14 with NativeJavaArray

use of org.mozilla.javascript.NativeJavaArray in project servoy-client by Servoy.

the class FormScope method get.

@Override
public Object get(String name, Scriptable start) {
    if (_fp == null) {
        Debug.warn("Error accessing a form " + formName + "  that is already destroyed for getting: " + name);
        throw new ExitScriptException("killing current script, client/solution already terminated");
    }
    _fp.touch();
    if (// $NON-NLS-1$
    "alldataproviders".equals(name)) {
        List<String> al = new ArrayList<String>();
        Table table = (Table) _fp.getTable();
        if (table != null) {
            al = getDataproviderIdList(table);
        }
        return new NativeJavaArray(this, al.toArray(new String[al.size()]));
    }
    if (// $NON-NLS-1$
    "allmethods".equals(name)) {
        List<String> al = new ArrayList<String>();
        Iterator<ScriptMethod> it = _fp.getForm().getScriptMethods(true);
        while (it.hasNext()) {
            ScriptMethod sm = it.next();
            al.add(sm.getName());
        }
        return new NativeJavaArray(this, al.toArray(new String[al.size()]));
    }
    if (// $NON-NLS-1$
    "allrelations".equals(name)) {
        List<String> al = getFormRelationsIdList(_fp.getForm());
        return new NativeJavaArray(this, al.toArray(new String[al.size()]));
    }
    if (// $NON-NLS-1$
    "allvariables".equals(name)) {
        List<String> al = getAllVariablesIdList(_fp.getForm());
        return new NativeJavaArray(this, al.toArray(new String[al.size()]));
    }
    Object object = super.get(name, start);
    if ((object == null || object == Scriptable.NOT_FOUND) && ("foundset".equals(name) || "elements".equals(name))) {
        Debug.error(Thread.currentThread().getName() + ": For form " + _fp + " the foundset/elements were asked for but that was not (or was no longer) set. ", new RuntimeException());
        if (name.equals("foundset"))
            return _fp.getFormModel();
    }
    return object;
}
Also used : Table(com.servoy.j2db.persistence.Table) ArrayList(java.util.ArrayList) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) ExitScriptException(com.servoy.j2db.ExitScriptException)

Aggregations

NativeJavaArray (org.mozilla.javascript.NativeJavaArray)14 NativeArray (org.mozilla.javascript.NativeArray)8 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)8 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Collection (java.util.Collection)5 NodeList (org.w3c.dom.NodeList)4 EngineException (com.twinsoft.convertigo.engine.EngineException)3 Vector (java.util.Vector)3 XmlSchemaCollection (org.apache.ws.commons.schema.XmlSchemaCollection)3 NativeObject (org.mozilla.javascript.NativeObject)3 Scriptable (org.mozilla.javascript.Scriptable)2 Undefined (org.mozilla.javascript.Undefined)2 Node (org.w3c.dom.Node)2 ApplicationException (com.servoy.j2db.ApplicationException)1 ExitScriptException (com.servoy.j2db.ExitScriptException)1 IBasicFormManager (com.servoy.j2db.IBasicFormManager)1 IFormController (com.servoy.j2db.IFormController)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)1