Search in sources :

Example 1 with IPropertyConverterForBrowser

use of org.sablo.specification.property.IPropertyConverterForBrowser 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 IPropertyConverterForBrowser

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

the class DataproviderTypeSabloValue method browserUpdateReceived.

public void browserUpdateReceived(Object newJSONValue, IBrowserConverterContext dataConverterContext) {
    Object oldUIValue = uiValue;
    ValueReference<Boolean> serverSideValueIsNotTheSameAsClient = new ValueReference<>(Boolean.FALSE);
    if (!findMode && typeOfDP != null) {
        if (typeOfDP.getType() instanceof DatePropertyType && fieldFormat != null && fieldFormat.parsedFormat != null && newJSONValue != null && fieldFormat.parsedFormat.getDisplayFormat() != null && (oldUIValue instanceof Date || oldUIValue == null)) {
            boolean hasNoDateConversion = NGDatePropertyType.hasNoDateConversion(typeOfDP);
            Date newValue = NGDatePropertyType.NG_INSTANCE.fromJSON(newJSONValue, hasNoDateConversion);
            if (oldUIValue != null) {
                String format = fieldFormat.parsedFormat.getEditFormat() != null ? fieldFormat.parsedFormat.getEditFormat() : fieldFormat.parsedFormat.getDisplayFormat();
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                if (!hasNoDateConversion)
                    sdf.setTimeZone(dataAdapterList.getApplication().getTimeZone());
                try {
                    String oldFormatted = sdf.format(oldUIValue);
                    String newFormatted = sdf.format(newValue);
                    // need to go back to the default time zone so it doesn't make it sudden 2 jan 1970 because of the
                    // time zone difference between the default here and where it needs to go to.
                    sdf.setTimeZone(TimeZone.getDefault());
                    Date oldValueParsed = sdf.parse(oldFormatted);
                    Date newValueParsed = sdf.parse(newFormatted);
                    uiValue = new Date(((Date) oldUIValue).getTime() + (newValueParsed.getTime() - oldValueParsed.getTime()));
                } catch (ParseException e) {
                    uiValue = newValue;
                }
            } else {
                uiValue = newValue;
            }
        } else if (typeOfDP.getType() instanceof IPropertyConverterForBrowser<?>) {
            try {
                uiValue = ((IPropertyConverterForBrowser<Object>) typeOfDP.getType()).fromJSON(newJSONValue, uiValue, typeOfDP, dataConverterContext, serverSideValueIsNotTheSameAsClient);
            } catch (ClassCastException e) {
                // this can hapen if a find mode uiVaue keeps hanging
                uiValue = ((IPropertyConverterForBrowser<Object>) typeOfDP.getType()).fromJSON(newJSONValue, null, typeOfDP, dataConverterContext, serverSideValueIsNotTheSameAsClient);
            }
        } else {
            uiValue = newJSONValue;
        }
    } else
        uiValue = newJSONValue;
    if (oldUIValue != uiValue && (oldUIValue == null || !oldUIValue.equals(uiValue))) {
        jsonValue = null;
    }
    if (serverSideValueIsNotTheSameAsClient.value.booleanValue()) {
        // if we detect that the new server value (it's representation on client) is no longer what the client has showing, we must update the client's value
        jsonValue = null;
        // value changed from client so why do we need this one might ask (client already has the value)?
        changeMonitor.valueChanged();
    // because for example in a field an INTEGER dataprovider might be shown with format ##0.00 and if the user enters non-int value client side
    // the server will trunc/round to an INTEGER and then the client shows double value while the server DP has the int value (which are not the same)
    }
}
Also used : DatePropertyType(org.sablo.specification.property.types.DatePropertyType) Date(java.util.Date) IPropertyConverterForBrowser(org.sablo.specification.property.IPropertyConverterForBrowser) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) ValueReference(org.sablo.util.ValueReference)

Aggregations

IPropertyConverterForBrowser (org.sablo.specification.property.IPropertyConverterForBrowser)2 ApplicationException (com.servoy.j2db.ApplicationException)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)1 FormScope (com.servoy.j2db.scripting.FormScope)1 GlobalScope (com.servoy.j2db.scripting.GlobalScope)1 ServoyException (com.servoy.j2db.util.ServoyException)1 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 Function (org.mozilla.javascript.Function)1 Scriptable (org.mozilla.javascript.Scriptable)1 BaseWebObject (org.sablo.BaseWebObject)1 IllegalChangeFromClientException (org.sablo.IllegalChangeFromClientException)1 BrowserConverterContext (org.sablo.specification.property.BrowserConverterContext)1 DatePropertyType (org.sablo.specification.property.types.DatePropertyType)1