Search in sources :

Example 1 with FoundsetLinkedTypeSabloValue

use of com.servoy.j2db.server.ngclient.property.FoundsetLinkedTypeSabloValue in project servoy-client by Servoy.

the class DataAdapterList method pushChanges.

public void pushChanges(WebFormComponent webComponent, String beanProperty, Object value, String foundsetLinkedRowID) {
    // TODO should this all (svy-apply/push) move to DataProviderType client/server side implementation instead of these specialized calls, instanceof checks and string parsing (see getProperty or getPropertyDescription)?
    String dataProviderID = getDataProviderID(webComponent, beanProperty);
    if (dataProviderID == null) {
        Debug.log("apply called on a property that is not bound to a dataprovider: " + beanProperty + ", value: " + value + " of component: " + webComponent);
        return;
    }
    Object newValue = value;
    // Check security
    webComponent.checkPropertyProtection(beanProperty);
    IRecordInternal editingRecord = record;
    if (newValue instanceof FoundsetLinkedTypeSabloValue) {
        if (foundsetLinkedRowID != null) {
            // find the row of the foundset that changed; 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
            editingRecord = getFoundsetLinkedRecord((FoundsetLinkedTypeSabloValue<?, ?>) newValue, foundsetLinkedRowID);
            if (editingRecord == null) {
                Debug.error("Error pushing data from client to server for foundset linked DP (cannot find record): dp=" + newValue + ", 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
        newValue = ((FoundsetLinkedTypeSabloValue) newValue).getWrappedValue();
    }
    if (newValue instanceof DataproviderTypeSabloValue)
        newValue = ((DataproviderTypeSabloValue) newValue).getValue();
    if (editingRecord == null || editingRecord.startEditing() || editingRecord.getParentFoundSet().getColumnIndex(dataProviderID) == -1) {
        Object v;
        // will update property with "image_data", property_filename with "pic.jpg" and property_mimetype with "image/jpeg"
        if (newValue instanceof HashMap) {
            // defining value
            v = ((HashMap<?, ?>) newValue).get("");
            Iterator<Entry<?, ?>> newValueIte = ((HashMap) newValue).entrySet().iterator();
            while (newValueIte.hasNext()) {
                Entry<?, ?> e = newValueIte.next();
                if (!"".equals(e.getKey())) {
                    try {
                        com.servoy.j2db.dataprocessing.DataAdapterList.setValueObject(editingRecord, formController.getFormScope(), dataProviderID + e.getKey(), e.getValue());
                    } catch (IllegalArgumentException ex) {
                        Debug.trace(ex);
                        getApplication().handleException(null, new ApplicationException(ServoyException.INVALID_INPUT, ex));
                    }
                }
            }
        } else {
            v = newValue;
        }
        Object oldValue = null;
        Exception setValueException = null;
        try {
            oldValue = com.servoy.j2db.dataprocessing.DataAdapterList.setValueObject(editingRecord, formController.getFormScope(), dataProviderID, v);
            if (value instanceof DataproviderTypeSabloValue)
                ((DataproviderTypeSabloValue) value).checkValueForChanges(editingRecord);
        } catch (IllegalArgumentException e) {
            Debug.trace(e);
            getApplication().handleException(null, new ApplicationException(ServoyException.INVALID_INPUT, e));
            setValueException = e;
            webComponent.setInvalidState(true);
        }
        DataproviderConfig dataproviderConfig = getDataproviderConfig(webComponent, beanProperty);
        String onDataChange = dataproviderConfig.getOnDataChange();
        if (onDataChange != null) {
            JSONObject event = EventExecutor.createEvent(onDataChange, editingRecord.getParentFoundSet().getSelectedIndex());
            event.put("data", createDataproviderInfo(editingRecord, formController.getFormScope(), dataProviderID));
            Object returnValue = null;
            Exception exception = null;
            String onDataChangeCallback = null;
            if (!Utils.equalObjects(oldValue, v) && setValueException == null && webComponent.hasEvent(onDataChange)) {
                // $NON-NLS-1$ //$NON-NLS-2$
                getApplication().getWebsocketSession().getClientService("$sabloLoadingIndicator").executeAsyncNowServiceCall("showLoading", null);
                try {
                    returnValue = webComponent.executeEvent(onDataChange, new Object[] { oldValue, v, event });
                } catch (Exception e) {
                    Debug.error("Error during onDataChange webComponent=" + webComponent, e);
                    exception = e;
                } finally {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    getApplication().getWebsocketSession().getClientService("$sabloLoadingIndicator").executeAsyncNowServiceCall("hideLoading", null);
                }
                onDataChangeCallback = dataproviderConfig.getOnDataChangeCallback();
            } else if (setValueException != null) {
                returnValue = setValueException.getMessage();
                exception = setValueException;
                onDataChangeCallback = dataproviderConfig.getOnDataChangeCallback();
            } else if (webComponent.isInvalidState() && exception == null) {
                onDataChangeCallback = dataproviderConfig.getOnDataChangeCallback();
                webComponent.setInvalidState(false);
            }
            if (onDataChangeCallback != null) {
                WebObjectFunctionDefinition call = createWebObjectFunction(onDataChangeCallback);
                webComponent.invokeApi(call, new Object[] { event, returnValue, exception == null ? null : exception.getMessage() });
            }
        }
    }
}
Also used : IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) WebObjectFunctionDefinition(org.sablo.specification.WebObjectFunctionDefinition) FoundsetLinkedTypeSabloValue(com.servoy.j2db.server.ngclient.property.FoundsetLinkedTypeSabloValue) 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) Entry(java.util.Map.Entry) ApplicationException(com.servoy.j2db.ApplicationException) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) DataproviderTypeSabloValue(com.servoy.j2db.server.ngclient.property.types.DataproviderTypeSabloValue) JSONObject(org.json.JSONObject) BaseWebObject(org.sablo.BaseWebObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) DataproviderConfig(com.servoy.j2db.server.ngclient.property.DataproviderConfig)

Example 2 with FoundsetLinkedTypeSabloValue

use of com.servoy.j2db.server.ngclient.property.FoundsetLinkedTypeSabloValue 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

IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)2 FoundsetLinkedTypeSabloValue (com.servoy.j2db.server.ngclient.property.FoundsetLinkedTypeSabloValue)2 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)2 JSONObject (org.json.JSONObject)2 BaseWebObject (org.sablo.BaseWebObject)2 IllegalChangeFromClientException (org.sablo.IllegalChangeFromClientException)2 ApplicationException (com.servoy.j2db.ApplicationException)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 DataproviderConfig (com.servoy.j2db.server.ngclient.property.DataproviderConfig)1 DataproviderTypeSabloValue (com.servoy.j2db.server.ngclient.property.types.DataproviderTypeSabloValue)1 ServoyException (com.servoy.j2db.util.ServoyException)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 WeakHashMap (java.util.WeakHashMap)1 JSONException (org.json.JSONException)1 WebObjectFunctionDefinition (org.sablo.specification.WebObjectFunctionDefinition)1