Search in sources :

Example 1 with BrowserConverterContext

use of org.sablo.specification.property.BrowserConverterContext in project servoy-client by Servoy.

the class DataAdapterList method executeInlineScript.

/**
 * @param args args to replace in script - used for HTML-triggered executeInlineScript; so calls generated via HTMLTagsConverter.convert(String, IServoyDataConverterContext, boolean) inside some piece of HTML
 * @param appendingArgs args to append in script execution - used for component/service client side code triggered executeInlineScript.
 */
@Override
public Object executeInlineScript(String script, JSONObject args, JSONArray appendingArgs) {
    String decryptedScript = HTMLTagsConverter.decryptInlineScript(script, args);
    if (appendingArgs != null && decryptedScript.endsWith("()")) {
        // this is an executeInlineScript called from component/service client-side code
        ArrayList<Object> javaArguments = new ArrayList<Object>();
        Object argObj = null;
        BrowserConverterContext dataConverterContext = new BrowserConverterContext((WebFormUI) formController.getFormUI(), PushToServerEnum.allow);
        for (int i = 0; i < appendingArgs.length(); i++) {
            try {
                argObj = ServoyJSONObject.jsonNullToNull(appendingArgs.get(i));
                if (argObj instanceof JSONObject) {
                    // $NON-NLS-1$
                    String typeHint = ((JSONObject) argObj).optString("svyType", null);
                    if (typeHint != null) {
                        IPropertyType<?> propertyType = TypesRegistry.getType(typeHint);
                        if (propertyType instanceof IPropertyConverterForBrowser<?>) {
                            javaArguments.add(((IPropertyConverterForBrowser<?>) propertyType).fromJSON(argObj, null, null, /*
											 * TODO this shouldn't be null! Make this better - maybe parse the type or just instantiate a property description
											 * if we don't want full support for what can be defined in spec file as a type
											 */
                            dataConverterContext, null));
                            continue;
                        }
                    }
                }
            } catch (JSONException e) {
                Debug.error(e);
            }
            javaArguments.add(argObj);
        }
        String functionName = decryptedScript.substring(0, decryptedScript.length() - 2);
        int startIdx = functionName.lastIndexOf('.');
        String noPrefixFunctionName = functionName.substring(startIdx > -1 ? startIdx + 1 : 0, functionName.length());
        Scriptable scope = null;
        Function f = null;
        if (functionName.startsWith("forms.")) {
            String formName = functionName.substring("forms.".length(), startIdx);
            FormScope formScope = formController.getFormScope();
            // this is a function that is on a form that is not the active window form.
            if (!formController.getName().equals(formName)) {
                formScope = getApplication().getFormManager().getForm(formName).getFormScope();
            }
            f = formScope.getFunctionByName(noPrefixFunctionName);
            if (f != null && f != Scriptable.NOT_FOUND) {
                scope = formScope;
            }
        } else if (functionName.startsWith("entity.")) {
            scope = (Scriptable) formController.getFoundSet();
            f = (Function) scope.getPrototype().get(noPrefixFunctionName, scope);
        } else {
            ScriptMethod scriptMethod = formController.getApplication().getFlattenedSolution().getScriptMethod(functionName);
            if (scriptMethod != null) {
                scope = formController.getApplication().getScriptEngine().getScopesScope().getGlobalScope(scriptMethod.getScopeName());
            }
            if (scope != null) {
                f = ((GlobalScope) scope).getFunctionByName(noPrefixFunctionName);
            }
        }
        try {
            return formController.getApplication().getScriptEngine().executeFunction(f, scope, scope, javaArguments.toArray(), false, false);
        } catch (Exception ex) {
            Debug.error(ex);
            return null;
        }
    }
    return decryptedScript != null ? formController.eval(decryptedScript) : null;
}
Also used : GlobalScope(com.servoy.j2db.scripting.GlobalScope) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) Scriptable(org.mozilla.javascript.Scriptable) JSONException(org.json.JSONException) ApplicationException(com.servoy.j2db.ApplicationException) IllegalChangeFromClientException(org.sablo.IllegalChangeFromClientException) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) FormScope(com.servoy.j2db.scripting.FormScope) Function(org.mozilla.javascript.Function) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IPropertyConverterForBrowser(org.sablo.specification.property.IPropertyConverterForBrowser) JSONObject(org.json.JSONObject) BaseWebObject(org.sablo.BaseWebObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) BrowserConverterContext(org.sablo.specification.property.BrowserConverterContext) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod)

Example 2 with BrowserConverterContext

use of org.sablo.specification.property.BrowserConverterContext in project servoy-client by Servoy.

the class FoundsetTest method foundsetRelated.

@Test
public // change selected index in main foundset and related foundset should change
void foundsetRelated() throws // change selected index in main foundset and related foundset should change
JSONException {
    IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("test");
    Assert.assertNotNull(form);
    BrowserConverterContext allowBrowserConverterContext2 = new BrowserConverterContext(form.getFormUI().getWebComponent("mydynamiccustombean"), PushToServerEnum.allow);
    FoundsetTypeSabloValue customBeanFoundSet = (FoundsetTypeSabloValue) form.getFormUI().getWebComponent("mycustombean").getRawPropertyValue("myfoundset");
    FoundsetTypeSabloValue dynamicBeanRelatedFoundset = (FoundsetTypeSabloValue) form.getFormUI().getWebComponent("mydynamiccustombean").getRawPropertyValue("myfoundset");
    dynamicBeanRelatedFoundset.getViewPort().setBounds(1, 1);
    // selection is now 0, so set to 1 and then back again
    customBeanFoundSet.getFoundset().setSelectedIndex(1);
    customBeanFoundSet.getFoundset().setSelectedIndex(0);
    Assert.assertEquals(12, dynamicBeanRelatedFoundset.getViewPort().getSize());
    Assert.assertEquals(0, dynamicBeanRelatedFoundset.getViewPort().getStartIndex());
    dynamicBeanRelatedFoundset.getViewPort().setBounds(1, 1);
    StringWriter stringWriter = new StringWriter();
    JSONWriter jsonWriter = new JSONWriter(stringWriter);
    dynamicBeanRelatedFoundset.addViewPort(jsonWriter);
    Assert.assertEquals("{\"startIndex\":1,\"size\":1,\"rows\":[{\"_svyRowId\":\"1.2;_1\",\"dp1\":\"relatedvalue121\",\"dp2\":\"relatedvalue122\"}]}", stringWriter.toString());
    stringWriter.getBuffer().setLength(0);
    jsonWriter = new JSONWriter(stringWriter);
    dynamicBeanRelatedFoundset.changesToJSON(jsonWriter, new DataConversion(), allowBrowserConverterContext2);
    Assert.assertEquals("{\"upd_serverSize\":12,\"upd_foundsetId\":3,\"upd_selectedRowIndexes\":[0],\"upd_viewPort\":{\"startIndex\":1,\"size\":1,\"rows\":[{\"_svyRowId\":\"1.2;_1\",\"dp1\":\"relatedvalue121\",\"dp2\":\"relatedvalue122\"}]}}", stringWriter.toString());
    customBeanFoundSet.getFoundset().setSelectedIndex(1);
    dynamicBeanRelatedFoundset.getViewPort().setBounds(0, 1);
    stringWriter.getBuffer().setLength(0);
    jsonWriter = new JSONWriter(stringWriter);
    dynamicBeanRelatedFoundset.changesToJSON(jsonWriter, new DataConversion(), allowBrowserConverterContext2);
    Assert.assertEquals("{\"upd_serverSize\":4,\"upd_foundsetId\":4,\"upd_selectedRowIndexes\":[0],\"upd_viewPort\":{\"startIndex\":0,\"size\":1,\"rows\":[{\"_svyRowId\":\"1.4;_0\",\"dp1\":\"relatedvalue241\",\"dp2\":\"relatedvalue242\"}]}}", stringWriter.toString());
}
Also used : JSONWriter(org.json.JSONWriter) DataConversion(org.sablo.websocket.utils.DataConversion) StringWriter(java.io.StringWriter) BrowserConverterContext(org.sablo.specification.property.BrowserConverterContext) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) Test(org.junit.Test)

Example 3 with BrowserConverterContext

use of org.sablo.specification.property.BrowserConverterContext in project servoy-client by Servoy.

the class FoundsetTest method largeFoundsetUsageWithPreferredSize.

@Test
public void largeFoundsetUsageWithPreferredSize() throws Exception {
    IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("test");
    Assert.assertNotNull(form);
    WebFormComponent wc = form.getFormUI().getWebComponent("mycustomseparatefoundsetbean");
    FoundsetTypeSabloValue rawPropertyValue = (FoundsetTypeSabloValue) wc.getRawPropertyValue("myfoundset");
    BrowserConverterContext allowBrowserConverterContext = new BrowserConverterContext(wc, PushToServerEnum.allow);
    FoundsetTypeViewport viewPort = rawPropertyValue.getViewPort();
    viewPort.setPreferredViewportSize(7);
    rawPropertyValue.getFoundset().find();
    rawPropertyValue.getFoundset().search();
    StringWriter stringWriter = new StringWriter();
    JSONWriter jsonWriter = new JSONWriter(stringWriter);
    rawPropertyValue.toJSON(jsonWriter, new DataConversion(), allowBrowserConverterContext);
    JSONAssert.assertEquals(new JSONObject("{\"selectedRowIndexes\":[0],\"viewPort\":{\"startIndex\":0,\"size\":7,\"rows\":[{\"firstname\":\"value00\",\"_svyRowId\":\"1.0;_0\",\"lastname\":\"value01\"},{\"firstname\":\"value10\",\"_svyRowId\":\"1.1;_1\",\"lastname\":\"value11\"},{\"firstname\":\"value20\",\"_svyRowId\":\"1.2;_2\",\"lastname\":\"value21\"},{\"firstname\":\"value30\",\"_svyRowId\":\"1.3;_3\",\"lastname\":\"value31\"},{\"firstname\":\"value40\",\"_svyRowId\":\"1.4;_4\",\"lastname\":\"value41\"},{\"firstname\":\"value50\",\"_svyRowId\":\"1.5;_5\",\"lastname\":\"value51\"},{\"firstname\":\"value60\",\"_svyRowId\":\"1.6;_6\",\"lastname\":\"value61\"}]},\"sortColumns\":\"\",\"serverSize\":200,\"foundsetId\":1,\"multiSelect\":false,\"hasMoreRows\":true}"), new JSONObject(stringWriter.toString()), JSONCompareMode.STRICT);
}
Also used : JSONWriter(org.json.JSONWriter) DataConversion(org.sablo.websocket.utils.DataConversion) StringWriter(java.io.StringWriter) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) BrowserConverterContext(org.sablo.specification.property.BrowserConverterContext) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) Test(org.junit.Test)

Example 4 with BrowserConverterContext

use of org.sablo.specification.property.BrowserConverterContext in project servoy-client by Servoy.

the class CustomArrayAndObjectPropertyRhinoTest method testCustomTypeJavaBasedRhinoChanges.

@Test
public void testCustomTypeJavaBasedRhinoChanges() throws JSONException {
    Context.enter();
    try {
        WebComponent component = new WebComponent("mycomponent", "testComponentName");
        BrowserConverterContext allowingBrowserConverterContext = new BrowserConverterContext(component, PushToServerEnum.allow);
        PropertyDescription arrayTPD = component.getSpecification().getProperty("arrayT");
        // just some initial checks and setting a java value
        assertNull(component.getProperty("arrayT"));
        List<Map<String, Object>> javaV = new ArrayList<>();
        HashMap<String, Object> hm = new HashMap<String, Object>();
        javaV.add(hm);
        hm.put("text", "Just some text");
        component.setProperty("arrayT", javaV);
        // set should turn it into a change-aware list
        Object wrapped = component.getProperty("arrayT");
        assertTrue(wrapped instanceof ChangeAwareList<?, ?>);
        ChangeAwareList<Map<String, Object>, Map<String, Object>> cal = (ChangeAwareList) wrapped;
        assertEquals("Just some text", cal.get(0).get("text"));
        assertTrue(cal.get(0) instanceof ChangeAwareMap<?, ?>);
        ChangeAwareMap cam = ((ChangeAwareMap<?, ?>) cal.get(0));
        Changes chMap = cam.getChanges();
        ChangeAwareList<Map<String, Object>, Map<String, Object>>.Changes chList = cal.getChanges();
        // TODO I guess this kind of reference changes should be treated in the BaseWebObject directly when we have separate methods for changesToJSON and fullToJSON
        // so for now the change aware things do not report as being changed...
        assertTrue(!chList.mustSendAll());
        assertTrue(!chMap.mustSendAll());
        // still the component has to see them as changed!
        TypedData<Map<String, Object>> changes = component.getAndClearChanges();
        assertTrue(changes.content.get("arrayT") != null);
        assertTrue(changes.contentType.getProperty("arrayT").getType() instanceof CustomJSONArrayType);
        Object arrayCh = changes.content.get("arrayT");
        assertTrue(arrayCh != null);
        assertTrue(changes.contentType.getProperty("arrayT").getType() instanceof CustomJSONArrayType);
        Object mapCh = ((ChangeAwareList) arrayCh).get(0);
        assertTrue(mapCh != null);
        assertTrue(((CustomJSONPropertyType<?>) changes.contentType.getProperty("arrayT").getType()).getCustomJSONTypeDefinition().getType() instanceof CustomJSONObjectType);
        JSONUtils.writeDataWithConversions(changes.content, changes.contentType, allowingBrowserConverterContext);
        // ok now that we called component.getChanges() no changes should be present any more
        assertTrue(!chList.mustSendAll());
        assertTrue(!chMap.mustSendAll());
        assertEquals(0, component.getAndClearChanges().content.size());
        assertFalse(chList.getGranularUpdatesKeeper().hasChanges());
        // check changing java => change reflected in Rhino
        ScriptableObject topLevel = new ScriptableObject() {

            @Override
            public String getClassName() {
                return "test_top_level_scope";
            }
        };
        Scriptable rhinoVal = (Scriptable) NGConversions.INSTANCE.convertSabloComponentToRhinoValue(component.getProperty("arrayT"), arrayTPD, component, topLevel);
        assertEquals("Just some text", ((Scriptable) rhinoVal.get(0, rhinoVal)).get("text", rhinoVal));
        cam.put("text", "Just some text 2");
        assertEquals(1, chMap.getKeysChangedByRef().size());
        assertEquals("text", chMap.getKeysChangedByRef().iterator().next());
        ArrayOperation[] opSeq = chList.getGranularUpdatesKeeper().getEquivalentSequenceOfOperations();
        assertEquals(1, opSeq.length);
        assertGranularOpIs(0, 0, ArrayOperation.CHANGE, ChangeAwareList.GRANULAR_UPDATE_OP, opSeq[0]);
        assertTrue(!chList.mustSendAll());
        assertTrue(!chMap.mustSendAll());
        assertEquals("Just some text 2", ((Scriptable) rhinoVal.get(0, rhinoVal)).get("text", rhinoVal));
        cam.put("active", new ArrayList());
        assertTrue(!chList.mustSendAll());
        assertTrue(!chMap.mustSendAll());
        assertEquals(2, chMap.getKeysChangedByRef().size());
        assertTrue(chMap.getKeysChangedByRef().contains("text"));
        assertTrue(chMap.getKeysChangedByRef().contains("active"));
        cam.remove("active");
        assertTrue(!chList.mustSendAll());
        assertTrue(chMap.mustSendAll());
        cal.add(new HashMap<String, Object>());
        ChangeAwareMap cam1 = ((ChangeAwareMap<?, ?>) cal.get(1));
        assertFalse(chList.mustSendAll());
        opSeq = chList.getGranularUpdatesKeeper().getEquivalentSequenceOfOperations();
        assertEquals(2, opSeq.length);
        assertGranularOpIs(0, 0, ArrayOperation.CHANGE, ChangeAwareList.GRANULAR_UPDATE_OP, opSeq[0]);
        assertGranularOpIs(1, 1, ArrayOperation.INSERT, null, opSeq[1]);
        assertTrue(chMap.mustSendAll());
        // ok clear changes
        changes = component.getAndClearChanges();
        JSONUtils.writeDataWithConversions(changes.content, changes.contentType, allowingBrowserConverterContext);
        assertEquals(1, changes.content.size());
        assertEquals(0, component.getAndClearChanges().content.size());
        assertTrue(!chList.mustSendAll());
        assertTrue(!chMap.mustSendAll());
        assertFalse(chList.getGranularUpdatesKeeper().hasChanges());
        assertEquals(0, chMap.getKeysChangedByRef().size());
        assertEquals(0, chMap.getKeysWithUpdates().size());
        // assign some native values
        Scriptable oneO = Context.getCurrentContext().newObject(topLevel);
        Scriptable activeA1 = Context.getCurrentContext().newArray(topLevel, 0);
        Scriptable activeA1Obj = Context.getCurrentContext().newObject(topLevel);
        rhinoVal.put(0, rhinoVal, oneO);
        // same as 'cam' but in it's Rhino representation
        Scriptable oneOScriptable = (Scriptable) rhinoVal.get(0, rhinoVal);
        assertTrue(!chList.mustSendAll());
        opSeq = chList.getGranularUpdatesKeeper().getEquivalentSequenceOfOperations();
        assertEquals(1, opSeq.length);
        assertGranularOpIs(0, 0, ArrayOperation.CHANGE, null, opSeq[0]);
        cam = ((ChangeAwareMap<?, ?>) cal.get(0));
        chMap = cam.getChanges();
        activeA1Obj.put("field", activeA1Obj, 11);
        activeA1.put(0, activeA1, activeA1Obj);
        oneOScriptable.put("active", oneOScriptable, activeA1);
        assertEquals(11, ((Map) ((List) ((Map) cal.get(0)).get("active")).get(0)).get("field"));
        ((Map) ((List) ((Map) cal.get(0)).get("active")).get(0)).put("percent", 0.22);
        assertEquals(1, chMap.getKeysChangedByRef().size());
        assertEquals(0, chMap.getKeysWithUpdates().size());
        assertTrue(chMap.getKeysChangedByRef().contains("active"));
        assertTrue(!chList.mustSendAll());
        opSeq = chList.getGranularUpdatesKeeper().getEquivalentSequenceOfOperations();
        assertEquals(1, opSeq.length);
        assertGranularOpIs(0, 0, ArrayOperation.CHANGE, null, opSeq[0]);
        // now change the native values using initial ref to see if it changed in java; this is no longer supported after case SVY-11027
        // activeA1Obj.put("field", activeA1Obj, 98);
        // assertEquals(98, ((Map)((List)((Map)cal.get(0)).get("active")).get(0)).get("field"));
        // activeA1.put(1, activeA1, activeA2Obj);
        // activeA2Obj.put("field", activeA2Obj, 45);
        // assertEquals(45, ((Map)((List)((Map)cal.get(0)).get("active")).get(1)).get("field"));
        changes = component.getAndClearChanges();
        String msg = JSONUtils.writeChangesWithConversions(changes.content, changes.contentType, allowingBrowserConverterContext);
        JSONAssert.assertEquals("{\"arrayT\":{\"vEr\":3,\"g\":[{\"op\":[0,0,0],\"d\":[{\"rt\":\"mycomponent.mytype007\",\"vEr\":5,\"v\":{\"active\":{\"vEr\":2,\"v\":[{\"rt\":\"mycomponent.activeType\",\"vEr\":2,\"v\":{\"field\":11,\"percent\":0.22}}],\"svy_types\":{\"0\":\"JSON_obj\"}}},\"svy_types\":{\"active\":\"JSON_arr\"}}],\"svy_types\":{\"0\":\"JSON_obj\"}}]},\"svy_types\":{\"arrayT\":\"JSON_arr\"}}", msg, JSONCompareMode.NON_EXTENSIBLE);
        ((Map) ((List) ((Map) cal.get(0)).get("active")).get(0)).put("percent", 0.33);
        assertEquals(0, chMap.getKeysChangedByRef().size());
        assertEquals(1, chMap.getKeysWithUpdates().size());
        changes = component.getAndClearChanges();
        msg = JSONUtils.writeChangesWithConversions(changes.content, changes.contentType, allowingBrowserConverterContext);
        JSONAssert.assertEquals("{\"arrayT\":{\"vEr\":3,\"g\":[{\"op\":[0,0,0],\"d\":[{\"rt\":\"mycomponent.mytype007\",\"vEr\":5,\"u\":[{\"k\":\"active\",\"v\":{\"vEr\":2,\"g\":[{\"op\":[0,0,0],\"d\":[{\"rt\":\"mycomponent.activeType\",\"vEr\":2,\"u\":[{\"k\":\"percent\",\"v\":0.33}]}],\"svy_types\":{\"0\":\"JSON_obj\"}}]}}],\"svy_types\":{\"0\":{\"v\":\"JSON_arr\"}}}],\"svy_types\":{\"0\":\"JSON_obj\"}}]},\"svy_types\":{\"arrayT\":\"JSON_arr\"}}", msg, JSONCompareMode.NON_EXTENSIBLE);
        ((List) ((Map) cal.get(0)).get("active")).add(new HashMap<String, Object>());
        ((Map) ((List) ((Map) cal.get(0)).get("active")).get(1)).put("percent", 0.99);
        component.getAndClearChanges();
        // now simulate another request cycle that makes some change to the property from javascript
        rhinoVal = (Scriptable) NGConversions.INSTANCE.convertSabloComponentToRhinoValue(component.getProperty("arrayT"), arrayTPD, component, topLevel);
        Scriptable v = ((Scriptable) rhinoVal.get(0, rhinoVal));
        v = (Scriptable) v.get("active", v);
        v = (Scriptable) v.get(1, v);
        assertEquals(0.99, v.get("percent", v));
        v.put("percent", v, 0.56);
        assertEquals(0.56, ((Map) ((List) ((Map) cal.get(0)).get("active")).get(1)).get("percent"));
        assertTrue(!chMap.mustSendAll());
        assertTrue(!chList.mustSendAll());
        opSeq = chList.getGranularUpdatesKeeper().getEquivalentSequenceOfOperations();
        assertEquals(1, opSeq.length);
        assertGranularOpIs(0, 0, ArrayOperation.CHANGE, ChangeAwareList.GRANULAR_UPDATE_OP, opSeq[0]);
        assertEquals(1, chMap.getKeysWithUpdates().size());
        assertEquals("active", chMap.getKeysWithUpdates().iterator().next());
    } finally {
        Context.exit();
    }
}
Also used : ArrayOperation(org.sablo.specification.property.ArrayOperation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) WebComponent(org.sablo.WebComponent) CustomJSONObjectType(org.sablo.specification.property.CustomJSONObjectType) CustomJSONArrayType(org.sablo.specification.property.CustomJSONArrayType) BrowserConverterContext(org.sablo.specification.property.BrowserConverterContext) ChangeAwareMap(org.sablo.specification.property.ChangeAwareMap) ArrayList(java.util.ArrayList) List(java.util.List) ChangeAwareList(org.sablo.specification.property.ChangeAwareList) Changes(org.sablo.specification.property.ChangeAwareMap.Changes) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) ChangeAwareList(org.sablo.specification.property.ChangeAwareList) PropertyDescription(org.sablo.specification.PropertyDescription) ScriptableObject(org.mozilla.javascript.ScriptableObject) HashMap(java.util.HashMap) Map(java.util.Map) ChangeAwareMap(org.sablo.specification.property.ChangeAwareMap) Test(org.junit.Test)

Example 5 with BrowserConverterContext

use of org.sablo.specification.property.BrowserConverterContext in project servoy-client by Servoy.

the class FoundsetTest method largeFoundsetUsage.

@Test
public void largeFoundsetUsage() throws JSONException {
    IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("test");
    Assert.assertNotNull(form);
    WebFormComponent wc = form.getFormUI().getWebComponent("mycustomseparatefoundsetbean");
    FoundsetTypeSabloValue rawPropertyValue = (FoundsetTypeSabloValue) wc.getRawPropertyValue("myfoundset");
    BrowserConverterContext allowBrowserConverterContext = new BrowserConverterContext(wc, PushToServerEnum.allow);
    FoundsetTypeViewport viewPort = rawPropertyValue.getViewPort();
    viewPort.setBounds(0, 1);
    StringWriter stringWriter = new StringWriter();
    JSONWriter jsonWriter = new JSONWriter(stringWriter);
    rawPropertyValue.toJSON(jsonWriter, new DataConversion(), allowBrowserConverterContext);
    Assert.assertTrue(new JSONObject("{\"serverSize\":200,\"foundsetId\":1,\"sortColumns\":\"\",\"selectedRowIndexes\":[0],\"multiSelect\":false,\"hasMoreRows\":true,\"viewPort\":{\"startIndex\":0,\"size\":1,\"rows\":[{\"_svyRowId\":\"1.0;_0\",\"lastname\":\"value01\",\"firstname\":\"value00\"}]}}").similar(new JSONObject(stringWriter.toString())));
    // foundset loads more records due to server side access - client should be aware of new size and hasMoreRows
    rawPropertyValue.getFoundset().getRecord(200);
    StringWriter stringWriter2 = new StringWriter();
    JSONWriter jsonWriter2 = new JSONWriter(stringWriter2);
    rawPropertyValue.changesToJSON(jsonWriter2, new DataConversion(), allowBrowserConverterContext);
    // bounds is 0,1 we should only get size update, but no rows
    Assert.assertEquals(new JSONObject("{\"upd_serverSize\":799}").toString(), new JSONObject(stringWriter2.toString()).toString());
    viewPort.setBounds(0, 15);
    rawPropertyValue.getFoundset().getRecord(200);
    StringWriter stringWriter2_1 = new StringWriter();
    JSONWriter jsonWriter2_1 = new JSONWriter(stringWriter2_1);
    rawPropertyValue.changesToJSON(jsonWriter2_1, new DataConversion(), allowBrowserConverterContext);
    // bounds is 0,15 we should get the rows now
    Assert.assertEquals(new JSONObject("{\"upd_viewPort\":{\"startIndex\":0,\"size\":15,\"rows\":[{\"_svyRowId\":\"1.0;_0\",\"firstname\":\"value00\",\"lastname\":\"value01\"},{\"_svyRowId\":\"1.1;_1\",\"firstname\":\"value10\",\"lastname\":\"value11\"},{\"_svyRowId\":\"1.2;_2\",\"firstname\":\"value20\",\"lastname\":\"value21\"},{\"_svyRowId\":\"1.3;_3\",\"firstname\":\"value30\",\"lastname\":\"value31\"},{\"_svyRowId\":\"1.4;_4\",\"firstname\":\"value40\",\"lastname\":\"value41\"},{\"_svyRowId\":\"1.5;_5\",\"firstname\":\"value50\",\"lastname\":\"value51\"},{\"_svyRowId\":\"1.6;_6\",\"firstname\":\"value60\",\"lastname\":\"value61\"},{\"_svyRowId\":\"1.7;_7\",\"firstname\":\"value70\",\"lastname\":\"value71\"},{\"_svyRowId\":\"1.8;_8\",\"firstname\":\"value80\",\"lastname\":\"value81\"},{\"_svyRowId\":\"1.9;_9\",\"firstname\":\"value90\",\"lastname\":\"value91\"},{\"_svyRowId\":\"2.10;_10\",\"firstname\":\"value100\",\"lastname\":\"value101\"},{\"_svyRowId\":\"2.11;_11\",\"firstname\":\"value110\",\"lastname\":\"value111\"},{\"_svyRowId\":\"2.12;_12\",\"firstname\":\"value120\",\"lastname\":\"value121\"},{\"_svyRowId\":\"2.13;_13\",\"firstname\":\"value130\",\"lastname\":\"value131\"},{\"_svyRowId\":\"2.14;_14\",\"firstname\":\"value140\",\"lastname\":\"value141\"}]}}").toString(), new JSONObject(stringWriter2_1.toString()).toString());
    // foundset loads more records due to client side wanting more records
    viewPort.setBounds(800, 1);
    StringWriter stringWriter3 = new StringWriter();
    JSONWriter jsonWriter3 = new JSONWriter(stringWriter3);
    rawPropertyValue.changesToJSON(jsonWriter3, new DataConversion(), allowBrowserConverterContext);
    Assert.assertTrue(new JSONObject("{\"upd_serverSize\":943,\"upd_hasMoreRows\":false,\"upd_viewPort\":{\"startIndex\":800,\"size\":1,\"rows\":[{\"_svyRowId\":\"3.800;_800\",\"lastname\":\"value8001\",\"firstname\":\"value8000\"}]}}").similar(new JSONObject(stringWriter3.toString())));
}
Also used : JSONWriter(org.json.JSONWriter) DataConversion(org.sablo.websocket.utils.DataConversion) StringWriter(java.io.StringWriter) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) BrowserConverterContext(org.sablo.specification.property.BrowserConverterContext) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) Test(org.junit.Test)

Aggregations

BrowserConverterContext (org.sablo.specification.property.BrowserConverterContext)15 Test (org.junit.Test)11 WebFormComponent (com.servoy.j2db.server.ngclient.WebFormComponent)10 IWebFormController (com.servoy.j2db.server.ngclient.IWebFormController)9 JSONObject (org.json.JSONObject)8 DataConversion (org.sablo.websocket.utils.DataConversion)8 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)7 StringWriter (java.io.StringWriter)7 JSONWriter (org.json.JSONWriter)7 ServoyException (com.servoy.j2db.util.ServoyException)3 ArrayList (java.util.ArrayList)3 PropertyDescription (org.sablo.specification.PropertyDescription)3 ApplicationException (com.servoy.j2db.ApplicationException)2 Form (com.servoy.j2db.persistence.Form)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)2 FormScope (com.servoy.j2db.scripting.FormScope)2 GlobalScope (com.servoy.j2db.scripting.GlobalScope)2 HashMap (java.util.HashMap)2 List (java.util.List)2