Search in sources :

Example 1 with IllegalChangeFromClientException

use of org.sablo.IllegalChangeFromClientException in project servoy-client by Servoy.

the class ComponentTypeSabloValue method browserUpdatesReceived.

public void browserUpdatesReceived(Object jsonValue) {
    if (childComponent == null)
        return;
    try {
        JSONArray updates = (JSONArray) jsonValue;
        for (int i = 0; i < updates.length(); i++) {
            JSONObject update = (JSONObject) updates.get(i);
            if (update.has("handlerExec")) {
                // { handlerExec: {
                // eventType: ...,
                // args: ...,
                // rowId : ...
                // }});
                update = update.getJSONObject("handlerExec");
                if (update.has("eventType")) {
                    boolean selectionOk = true;
                    if (update.has("rowId")) {
                        String rowId = update.optString("rowId");
                        if (rowId != null) {
                            FoundsetTypeSabloValue foundsetValue = getFoundsetValue();
                            if (foundsetValue != null) {
                                if (!foundsetValue.setEditingRowByPkHash(rowId)) {
                                    Debug.error("Cannot select row when component event was fired; row identifier: " + rowId + ", forFoundset: '" + foundsetValue + "', component: " + (childComponent != null ? childComponent : getName()));
                                    selectionOk = false;
                                }
                            }
                        }
                    }
                    if (selectionOk) {
                        String eventType = update.getString("eventType");
                        // String beanName = update.getString("beanName");
                        JSONArray jsargs = update.getJSONArray("args");
                        Object[] args = new Object[jsargs == null ? 0 : jsargs.length()];
                        for (int j = 0; jsargs != null && j < jsargs.length(); j++) {
                            args[j] = jsargs.get(j);
                        }
                        Object result = null;
                        String error = null;
                        try {
                            result = childComponent.executeEvent(eventType, args);
                        } catch (ParseException pe) {
                            log.warn("Warning: " + pe.getMessage(), pe);
                        } catch (IllegalChangeFromClientException ilcae) {
                            log.warn("Warning: " + ilcae.getMessage());
                        } catch (Exception e) {
                            error = "Error: " + e.getMessage();
                            log.error(error, e);
                        }
                        int cmsid = update.optInt("defid", -1);
                        if (cmsid != -1) {
                            if (error == null) {
                                Object resultObject = result;
                                PropertyDescription objectType = null;
                                if (result instanceof TypedData) {
                                    resultObject = ((TypedData<?>) result).content;
                                    objectType = ((TypedData<?>) result).contentType;
                                }
                                CurrentWindow.get().getSession().getSabloService().resolveDeferedEvent(cmsid, true, resultObject, objectType);
                            } else {
                                CurrentWindow.get().getSession().getSabloService().resolveDeferedEvent(cmsid, false, error, null);
                            }
                        }
                    }
                }
            } else if (update.has("propertyChanges")) {
                // { propertyChanges : {
                // prop1: ...,
                // prop2: ...
                // }}
                JSONObject changes = update.getJSONObject("propertyChanges");
                Iterator<String> keys = changes.keys();
                while (keys.hasNext()) {
                    String key = keys.next();
                    Object object = changes.get(key);
                    childComponent.putBrowserProperty(key, object);
                }
            } else if (update.has(ViewportDataChangeMonitor.VIEWPORT_CHANGED)) {
                // component is linked to a foundset and the value of a property that depends on the record changed client side;
                // in this case update DataAdapterList with the correct record and then set the value on the component
                FoundsetTypeSabloValue foundsetPropertyValue = getFoundsetValue();
                if (foundsetPropertyValue != null && foundsetPropertyValue.getFoundset() != null) {
                    JSONObject change = update.getJSONObject(ViewportDataChangeMonitor.VIEWPORT_CHANGED);
                    String rowIDValue = change.getString(FoundsetTypeSabloValue.ROW_ID_COL_KEY);
                    String propertyName = change.getString(FoundsetTypeSabloValue.DATAPROVIDER_KEY);
                    Object value = change.get(FoundsetTypeSabloValue.VALUE_KEY);
                    updatePropertyValueForRecord(foundsetPropertyValue, rowIDValue, propertyName, value);
                    foundsetPropertyValue.setDataAdapterListToSelectedRecord();
                } else {
                    Debug.error("Component updates received for record linked property, but component is not linked to a foundset: " + update.get(ViewportDataChangeMonitor.VIEWPORT_CHANGED));
                }
            } else if (update.has("svyApply")) {
                // { svyApply: {
                // rowId: rowId, // only when linked to foundset
                // propertyName: property,
                // propertyValue: propertyValue
                // }}
                JSONObject changeAndApply = update.getJSONObject("svyApply");
                String propertyName = changeAndApply.getString(ComponentPropertyType.PROPERTY_NAME_KEY);
                Object value = changeAndApply.get(ComponentPropertyType.VALUE_KEY);
                try {
                    if (forFoundsetTypedPropertyName != null && recordBasedProperties.contains(propertyName)) {
                        // changes component record and sets value
                        String rowIDValue = changeAndApply.getString(FoundsetTypeSabloValue.ROW_ID_COL_KEY);
                        foundsetLinkedPropOfComponentValueChangeHandler.setApplyingDPValueFromClient(true);
                        FoundsetTypeSabloValue foundsetValue = getFoundsetValue();
                        updatePropertyValueForRecord(foundsetValue, rowIDValue, propertyName, value);
                        // apply change to record/dp
                        foundsetValue.getDataAdapterList().pushChanges(childComponent, propertyName);
                        foundsetValue.setDataAdapterListToSelectedRecord();
                    } else {
                        childComponent.putBrowserProperty(propertyName, value);
                        IWebFormUI formUI = getParentComponent().findParent(IWebFormUI.class);
                        // apply change to record/dp
                        formUI.getDataAdapterList().pushChanges(childComponent, propertyName);
                    }
                    if (forFoundsetTypedPropertyName != null && !recordBasedProperties.contains(propertyName)) {
                        // a global or form var that in case of a foundset linked component will apply the value on the child component but, as it knows it is comming from the browser,
                        // the child component will not notify it as a changed value; we need that though as we need to resend that value for all rows back to client, not just currently selected one
                        childComponent.markPropertyAsChangedByRef(propertyName);
                    }
                } finally {
                    if (foundsetLinkedPropOfComponentValueChangeHandler != null)
                        foundsetLinkedPropOfComponentValueChangeHandler.setApplyingDPValueFromClient(false);
                }
            } else if (update.has("svyStartEdit")) {
                // { svyStartEdit: {
                // rowId: rowId, // only if linked to foundset
                // propertyName: property
                // }}
                JSONObject startEditData = update.getJSONObject("svyStartEdit");
                String propertyName = startEditData.getString(ComponentPropertyType.PROPERTY_NAME_KEY);
                IDataAdapterList dal;
                if (forFoundsetTypedPropertyName != null && recordBasedProperties.contains(propertyName)) {
                    String rowIDValue = startEditData.getString(FoundsetTypeSabloValue.ROW_ID_COL_KEY);
                    IFoundSetInternal foundset = getFoundsetValue().getFoundset();
                    dal = getFoundsetValue().getDataAdapterList();
                    Pair<String, Integer> splitHashAndIndex = FoundsetTypeSabloValue.splitPKHashAndIndex(rowIDValue);
                    if (foundset != null) {
                        int recordIndex = foundset.getRecordIndex(splitHashAndIndex.getLeft(), splitHashAndIndex.getRight().intValue());
                        if (recordIndex != -1) {
                            ((FoundsetDataAdapterList) dal).setRecordQuietly(foundset.getRecord(recordIndex));
                        } else {
                            Debug.error("Cannot find record for foundset linked record dependent component property - startEdit (" + rowIDValue + "); property '" + propertyName, new RuntimeException());
                        }
                    } else {
                        Debug.error("Foundset is null while trying to startEdit for foundset linked record dependent component property (" + rowIDValue + "); property '" + propertyName, new RuntimeException());
                    }
                } else {
                    IWebFormUI formUI = getParentComponent().findParent(IWebFormUI.class);
                    dal = formUI.getDataAdapterList();
                }
                // TODO last arg should be here the foundsetLinked row Id in case the property is itself a foundset-linked DP; this should be done as part of case SVY-10500
                dal.startEdit(childComponent, propertyName, null);
            }
        }
    } catch (Exception ex) {
        Debug.error(ex);
    }
}
Also used : TypedData(org.sablo.websocket.TypedData) IllegalChangeFromClientException(org.sablo.IllegalChangeFromClientException) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ParseException(java.text.ParseException) IllegalChangeFromClientException(org.sablo.IllegalChangeFromClientException) PropertyDescription(org.sablo.specification.PropertyDescription) IWebFormUI(com.servoy.j2db.server.ngclient.IWebFormUI) JSONObject(org.json.JSONObject) Iterator(java.util.Iterator) IDataAdapterList(com.servoy.j2db.server.ngclient.IDataAdapterList) JSONObject(org.json.JSONObject) ParseException(java.text.ParseException)

Example 2 with IllegalChangeFromClientException

use of org.sablo.IllegalChangeFromClientException in project servoy-client by Servoy.

the class DataAdapterList method startEdit.

public void startEdit(WebFormComponent webComponent, String property, String foundsetLinkedRowID) {
    try {
        webComponent.checkPropertyProtection(property);
    } catch (IllegalChangeFromClientException ex) {
        // ignore, this is just to check if we can edit it, if not, do not enter edit mode
        return;
    }
    String dataProviderID = getDataProviderID(webComponent, property);
    if (dataProviderID == null) {
        Debug.log("startEdit called on a property that is not bound to a dataprovider: " + property + " of component: " + webComponent);
        return;
    }
    IRecordInternal recordToUse = record;
    if (!ScopesUtils.isVariableScope(dataProviderID)) {
        Object propertyValue = webComponent.getProperty(property);
        if (propertyValue instanceof FoundsetLinkedTypeSabloValue) {
            if (foundsetLinkedRowID != null) {
                // find the row of the foundset that the user started to edit; we can't use client's index (as server-side indexes might have changed meanwhile on server); so we are doing it based on client sent rowID
                recordToUse = getFoundsetLinkedRecord((FoundsetLinkedTypeSabloValue<?, ?>) propertyValue, foundsetLinkedRowID);
                if (recordToUse == null) {
                    Debug.error("Error executing startEdit (from client) for foundset linked DP (cannot find record): dp=" + propertyValue + ", rowID=" + foundsetLinkedRowID);
                    return;
                }
            }
        // hmm, this is strange - usually we should always get rowID, even if foundset linked is actually set by developer to a global or form variable - even though there rowID is not actually needed; just treat this as if it is not record linked
        }
        if (recordToUse != null) {
            int rowIndex = recordToUse.getParentFoundSet().getRecordIndex(recordToUse);
            if (Arrays.binarySearch(recordToUse.getParentFoundSet().getSelectedIndexes(), rowIndex) < 0) {
                recordToUse.getParentFoundSet().setSelectedIndex(rowIndex);
            }
            recordToUse.startEditing();
        }
    }
}
Also used : IllegalChangeFromClientException(org.sablo.IllegalChangeFromClientException) IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) JSONObject(org.json.JSONObject) BaseWebObject(org.sablo.BaseWebObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) FoundsetLinkedTypeSabloValue(com.servoy.j2db.server.ngclient.property.FoundsetLinkedTypeSabloValue)

Aggregations

JSONObject (org.json.JSONObject)2 IllegalChangeFromClientException (org.sablo.IllegalChangeFromClientException)2 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)1 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)1 IDataAdapterList (com.servoy.j2db.server.ngclient.IDataAdapterList)1 IWebFormUI (com.servoy.j2db.server.ngclient.IWebFormUI)1 FoundsetLinkedTypeSabloValue (com.servoy.j2db.server.ngclient.property.FoundsetLinkedTypeSabloValue)1 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)1 ParseException (java.text.ParseException)1 Iterator (java.util.Iterator)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 BaseWebObject (org.sablo.BaseWebObject)1 PropertyDescription (org.sablo.specification.PropertyDescription)1 TypedData (org.sablo.websocket.TypedData)1