use of com.servoy.j2db.server.ngclient.IWebFormController 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);
}
use of com.servoy.j2db.server.ngclient.IWebFormController in project servoy-client by Servoy.
the class PropertySetTest method setColorProperty.
@Test
public void setColorProperty() throws Exception {
IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("test");
Assert.assertNotNull(form);
Collection<WebComponent> components = form.getFormUI().getComponents();
Assert.assertEquals(1, components.size());
WebComponent comp = components.iterator().next();
Assert.assertEquals(Color.BLACK, comp.getProperty("background"));
JSONObject json = new JSONObject();
json.put("formname", "test");
json.put("beanname", comp.getName());
JSONObject changes = new JSONObject();
changes.put("background", "#0000FF");
json.put("changes", changes);
FormServiceHandler.INSTANCE.executeMethod("dataPush", json);
// should be changed.
Assert.assertEquals(Color.BLUE, comp.getProperty("background"));
changes.put("background", "#FF0000");
JSONObject oldvalues = new JSONObject();
oldvalues.put("background", "#0000FF");
json.put("oldvalues", oldvalues);
FormServiceHandler.INSTANCE.executeMethod("dataPush", json);
// should be changed, old value was really the old value.
Assert.assertEquals(Color.RED, comp.getProperty("background"));
changes.put("background", "#00FF00");
// should not be changed, still RED
FormServiceHandler.INSTANCE.executeMethod("dataPush", json);
Assert.assertEquals(Color.RED, comp.getProperty("background"));
}
use of com.servoy.j2db.server.ngclient.IWebFormController in project servoy-client by Servoy.
the class FoundsetTypeSabloValue method browserUpdatesReceived.
public void browserUpdatesReceived(Object jsonValue, PropertyDescription pd, IBrowserConverterContext dataConverterContext) {
PushToServerEnum pushToServer = BrowserConverterContext.getPushToServerValue(dataConverterContext);
if (getFoundset() == null)
return;
try {
if (jsonValue instanceof JSONArray) {
JSONArray arr = (JSONArray) jsonValue;
for (int i = 0; i < arr.length(); i++) {
JSONObject update = (JSONObject) arr.get(i);
// {newViewPort: {startIndex : startIndex, size : size}}
if (update.has("newViewPort")) {
JSONObject newViewport = update.getJSONObject("newViewPort");
int requestID = update.getInt(ID_KEY);
viewPort.clearSendingInitialPreferredViewport();
viewPort.setBounds(newViewport.getInt(START_INDEX), newViewport.getInt(SIZE));
changeMonitor.requestIdHandled(requestID, true);
}
if (update.has(PREFERRED_VIEWPORT_SIZE)) {
viewPort.setPreferredViewportSize(update.getInt(PREFERRED_VIEWPORT_SIZE));
if (update.has(FoundsetPropertyTypeConfig.SEND_SELECTION_VIEWPORT_INITIALLY))
viewPort.setSendSelectionViewportInitially(update.getBoolean(FoundsetPropertyTypeConfig.SEND_SELECTION_VIEWPORT_INITIALLY));
if (update.has(INITIAL_SELECTION_VIEWPORT_CENTERED))
viewPort.setInitialSelectionViewportCentered(update.getBoolean(INITIAL_SELECTION_VIEWPORT_CENTERED));
} else // {loadExtraRecords: negativeOrPositiveCount}
if (update.has("loadExtraRecords")) {
int requestID = update.getInt(ID_KEY);
viewPort.clearSendingInitialPreferredViewport();
viewPort.loadExtraRecords(update.getInt("loadExtraRecords"));
changeMonitor.requestIdHandled(requestID, true);
} else // {loadLessRecords: negativeOrPositiveCount}
if (update.has("loadLessRecords")) {
int requestID = update.getInt(ID_KEY);
viewPort.clearSendingInitialPreferredViewport();
viewPort.loadLessRecords(update.getInt("loadLessRecords"));
changeMonitor.requestIdHandled(requestID, true);
} else if (update.has("sort")) {
int requestID = update.getInt(ID_KEY);
JSONArray columns = update.getJSONArray("sort");
StringBuilder sort = new StringBuilder();
Map<String, String> dp = dataproviders.size() > 0 ? dataproviders : recordDataLinkedPropertyIDToColumnDP;
String dataProviderID = null;
boolean sortAscending = true;
for (int j = 0; j < columns.length(); j++) {
JSONObject sortColumn = columns.getJSONObject(j);
String name = sortColumn.getString("name");
if (dp.containsKey(name)) {
sort.append(dp.get(name));
sort.append(" " + sortColumn.getString("direction"));
if (dataProviderID == null) {
dataProviderID = dp.get(name);
sortAscending = "asc".equalsIgnoreCase(sortColumn.getString("direction"));
}
if (j < columns.length() - 1)
sort.append(",");
}
}
IWebFormUI formUI = getFormUI();
IWebFormController fc = (formUI != null ? formUI.getController() : null);
if (fc != null && fc.getForm().getOnSortCmdMethodID() > 0 && dataProviderID != null) {
// our api only supports one dataproviderid sort at a time
JSEvent event = new JSEvent();
event.setFormName(fc.getName());
fc.executeFunction(String.valueOf(fc.getForm().getOnSortCmdMethodID()), Utils.arrayMerge((new Object[] { dataProviderID, Boolean.valueOf(sortAscending), event }), // $NON-NLS-1$
Utils.parseJSExpressions(fc.getForm().getFlattenedMethodArguments("onSortCmdMethodID"))), true, null, false, // $NON-NLS-1$
"onSortCmdMethodID");
} else {
try {
String currentSort = foundset.getSort();
String newSort = sort.toString();
foundset.setSort(newSort);
if (// really a new sort
!Utils.equalObjects(currentSort, newSort) || // not sorted, send back to client
!Utils.equalObjects(foundset.getSort(), newSort)) {
changeMonitor.foundsetSortChanged();
}
} catch (ServoyException e) {
Debug.error("Cannot sort foundset by " + sort.toString(), e);
}
}
changeMonitor.requestIdHandled(requestID, true);
} else // {newClientSelection: newSelectedIndexesArray}
if (update.has("newClientSelection")) {
JSONArray jsonSelectedIndexes = update.getJSONArray("newClientSelection");
int[] newSelectedIndexes = new int[jsonSelectedIndexes.length()];
for (int j = newSelectedIndexes.length - 1; j >= 0; j--) {
newSelectedIndexes[j] = jsonSelectedIndexes.getInt(j);
}
// this !Arrays.equals check in conjunction with pause()/resume() is needed to avoid an effect on the client that server always sends back changed selection in which case
// if the user quickly changes selection multiple times and the connection is slow, selection will jump all over
// the place until it stabilizes correctly
getListSelectionListener().pause();
try {
if (newSelectedIndexes.length == 1) {
foundset.setSelectedIndex(newSelectedIndexes[0]);
} else {
foundset.setSelectedIndexes(newSelectedIndexes);
}
} finally {
getListSelectionListener().resume();
// if server denies the new selection as invalid and doesn't change selection, send it to the client so that it doesn't keep invalid selection
if (!Arrays.equals(foundset.getSelectedIndexes(), newSelectedIndexes)) {
changeMonitor.selectionChanged(false);
}
}
} else // {newClientSelectionRequest: newSelectedIndexesArray}
if (update.has("newClientSelectionRequest")) {
int requestID = update.getInt(ID_KEY);
JSONArray jsonSelectedIndexes = update.getJSONArray("newClientSelectionRequest");
int[] newSelectedIndexes = new int[jsonSelectedIndexes.length()];
for (int j = newSelectedIndexes.length - 1; j >= 0; j--) {
newSelectedIndexes[j] = jsonSelectedIndexes.getInt(j);
}
int[] oldSelection = foundset.getSelectedIndexes();
// this !Arrays.equals check in conjunction with pause()/resume() is needed to avoid an effect on the client that server always sends back changed selection in which case
// if the user quickly changes selection multiple times and the connection is slow, selection will jump all over
// the place until it stabilizes correctly
getListSelectionListener().pause();
try {
if (newSelectedIndexes.length == 1) {
foundset.setSelectedIndex(newSelectedIndexes[0]);
} else {
foundset.setSelectedIndexes(newSelectedIndexes);
}
} finally {
getListSelectionListener().resume();
if (!Arrays.equals(oldSelection, foundset.getSelectedIndexes())) {
// if the selection is changed, send it back to the client so that its model is also updated
changeMonitor.selectionChanged(false);
changeMonitor.requestIdHandled(requestID, true);
} else {
if (!Arrays.equals(oldSelection, newSelectedIndexes)) {
// it was supposed to change but the server did not allow it
changeMonitor.requestIdHandled(requestID, false);
} else
changeMonitor.requestIdHandled(requestID, true);
}
}
} else if (update.has(ViewportDataChangeMonitor.VIEWPORT_CHANGED)) {
if (PushToServerEnum.allow.compareTo(pushToServer) <= 0) {
// {dataChanged: { ROW_ID_COL_KEY: rowIDValue, dataproviderName: value }}
JSONObject dataChangeJSON = (JSONObject) update.get(ViewportDataChangeMonitor.VIEWPORT_CHANGED);
String rowIDValue = dataChangeJSON.getString(ROW_ID_COL_KEY);
String dpKey = dataChangeJSON.getString(DATAPROVIDER_KEY);
String dataProviderName;
if (dataproviders.containsKey(dpKey)) {
dataProviderName = dataproviders.get(dpKey);
} else {
dataProviderName = recordDataLinkedPropertyIDToColumnDP.get(dpKey);
}
Object value = dataChangeJSON.get(VALUE_KEY);
if (foundset != null) {
Pair<String, Integer> splitHashAndIndex = splitPKHashAndIndex(rowIDValue);
int recordIndex = foundset.getRecordIndex(splitHashAndIndex.getLeft(), splitHashAndIndex.getRight().intValue());
if (recordIndex != -1) {
IRecordInternal record = foundset.getRecord(recordIndex);
// convert Dates where it's needed
// this should be enough for when only foundset dataproviders are used
PropertyDescription dataProviderPropDesc = getDataProviderPropertyDescription(dataProviderName);
ValueReference<Boolean> returnValueAdjustedIncommingValueForRow = new ValueReference<Boolean>(Boolean.FALSE);
value = JSONUtils.fromJSONUnwrapped(null, value, dataProviderPropDesc, dataConverterContext, returnValueAdjustedIncommingValueForRow);
try {
if (record.startEditing()) {
try {
record.setValue(dataProviderName, value);
} catch (IllegalArgumentException e) {
// TODO handle the validaton errors.
IWebFormUI formUI = getFormUI();
formUI.getController().getApplication().reportError("Validation for " + dataProviderName + " for value: " + value + " failed.", e);
}
}
// else cannot start editing; finally block will deal with it (send old value back to client as new one can't be pushed)
} finally {
// if server denies the new value as invalid and doesn't change it, send it to the client so that it doesn't keep invalid value; the same if for example a double was rounded to an int
if (!Utils.equalObjects(record.getValue(dataProviderName), value) || returnValueAdjustedIncommingValueForRow.value.booleanValue()) {
changeMonitor.recordsUpdated(recordIndex, recordIndex, foundset.getSize(), viewPort, Arrays.asList(new String[] { dataProviderName }));
}
}
} else {
Debug.error("Cannot set foundset record (" + rowIDValue + ") dataprovider '" + dataProviderName + "' to value '" + value + ". Record not found.");
}
}
} else {
log.error("Property (" + pd + ") that doesn't define a suitable pushToServer value (allow/shallow/deep) tried to modify foundset dataprovider value serverside. Denying and sending back full viewport!");
changeMonitor.viewPortCompletelyChanged();
}
}
}
}
} catch (JSONException e) {
Debug.error("Error when getting browser updates for property (" + this.toString() + ") for " + jsonValue, e);
}
}
use of com.servoy.j2db.server.ngclient.IWebFormController 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())));
}
use of com.servoy.j2db.server.ngclient.IWebFormController in project servoy-client by Servoy.
the class FoundsetTest method foundsetViewportAllRecordDeleted.
@Test
public void foundsetViewportAllRecordDeleted() throws JSONException, ServoyException {
IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("test");
Assert.assertNotNull(form);
WebFormComponent wc = form.getFormUI().getWebComponent("mycustombean");
FoundsetTypeSabloValue rawPropertyValue = (FoundsetTypeSabloValue) wc.getRawPropertyValue("myfoundset");
BrowserConverterContext allowBrowserConverterContext = new BrowserConverterContext(wc, PushToServerEnum.allow);
FoundsetTypeViewport viewPort = rawPropertyValue.getViewPort();
FoundSet foundset = (FoundSet) form.getFormModel();
viewPort.setBounds(5, 5);
foundset.setSelectedIndex(6);
StringWriter stringWriter = new StringWriter();
JSONWriter jsonWriter = new JSONWriter(stringWriter);
// just to clear changed flags
rawPropertyValue.toJSON(jsonWriter, new DataConversion(), allowBrowserConverterContext);
// create an empty separate foundset from the same datasource
FoundSet sepEmpFs = (FoundSet) client.getFoundSetManager().getNewFoundSet("mem:test");
foundset.js_loadRecords(sepEmpFs);
stringWriter = new StringWriter();
jsonWriter = new JSONWriter(stringWriter);
rawPropertyValue.changesToJSON(jsonWriter, new DataConversion(), allowBrowserConverterContext);
JSONAssert.assertEquals("{\"upd_serverSize\":0,\"upd_selectedRowIndexes\":[],\"upd_viewPort\":{\"startIndex\":0,\"size\":0,\"upd_rows\":[{\"startIndex\":0,\"endIndex\":4,\"type\":2}]}}", stringWriter.toString(), true);
}
Aggregations