Search in sources :

Example 1 with FormElementContext

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

the class ComponentTypeSabloValue method fullToJSON.

/**
 * Writes the entire value of this property as JSON. This includes the template values, not just the runtime component properties.
 * This is currently needed and can get called if the property is nested inside other complex properties (json object/array) that sometimes
 * might want/need to send again the entire content.
 */
public JSONWriter fullToJSON(final JSONWriter writer, DataConversion conversionMarkers, ComponentPropertyType componentPropertyType) throws JSONException {
    // so that the client knows it must use the custom client side JS for what JSON it gets
    if (conversionMarkers != null)
        conversionMarkers.convert(ComponentPropertyType.TYPE_NAME);
    // create children of component as specified by this property
    final FormElement fe = formElementValue.element;
    writer.object();
    // get template model values
    final TypedData<Map<String, Object>> formElementProperties = fe.propertiesForTemplateJSON();
    // we'll need to update them with runtime values
    final TypedData<Map<String, Object>> runtimeProperties = childComponent.getProperties();
    // just for clear
    childComponent.getAndClearChanges();
    // add to useful properties only those formElement properties that didn't get overridden at runtime (so form element value is still used)
    boolean templateValuesRemoved = false;
    Iterator<Entry<String, Object>> formElementPropertyIterator = formElementProperties.content.entrySet().iterator();
    while (formElementPropertyIterator.hasNext()) {
        Entry<String, Object> fePropEntry = formElementPropertyIterator.next();
        if (runtimeProperties.content.containsKey(fePropEntry.getKey())) {
            // it has a non-default runtime value; so template value will be ignored/not sent
            if (!templateValuesRemoved) {
                // otherwise it's unmodifiable
                formElementProperties.content = new HashMap<String, Object>(formElementProperties.content);
                templateValuesRemoved = true;
            }
            formElementProperties.content.remove(fePropEntry.getKey());
        }
    }
    removeRecordDependentProperties(runtimeProperties);
    removeRecordDependentProperties(formElementProperties);
    IWebFormUI parent = childComponent.findParent(IWebFormUI.class);
    final FormElementContext formElementContext = new FormElementContext(fe, new ServoyDataConverterContext(parent.getController()), null);
    componentPropertyType.writeTemplateJSONContent(writer, formElementValue, forFoundsetTypedPropertyName, formElementContext, new IModelWriter() {

        @Override
        public void writeComponentModel() throws JSONException {
            writer.object();
            DataConversion dataConversion = new DataConversion();
            JSONUtils.writeData(FormElementToJSON.INSTANCE, writer, formElementProperties.content, formElementProperties.contentType, dataConversion, formElementContext);
            // always use full to JSON converter here; second arg. is null due to that
            childComponent.writeProperties(JSONUtils.FullValueToJSONConverter.INSTANCE, null, writer, runtimeProperties, dataConversion);
            JSONUtils.writeClientConversions(writer, dataConversion);
            writer.endObject();
        }
    }, recordBasedProperties, false);
    if (forFoundsetTypedPropertyName != null)
        recordBasedProperties.clearChanged();
    writeWholeViewportToJSON(writer);
    writer.endObject();
    return writer;
}
Also used : JSONException(org.json.JSONException) FormElement(com.servoy.j2db.server.ngclient.FormElement) DataConversion(org.sablo.websocket.utils.DataConversion) Entry(java.util.Map.Entry) IWebFormUI(com.servoy.j2db.server.ngclient.IWebFormUI) ServoyDataConverterContext(com.servoy.j2db.server.ngclient.ServoyDataConverterContext) IModelWriter(com.servoy.j2db.server.ngclient.property.ComponentPropertyType.IModelWriter) JSONObject(org.json.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap) FormElementContext(com.servoy.j2db.server.ngclient.FormElementContext)

Example 2 with FormElementContext

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

the class FormTemplateObjectWrapper method wrap.

@Override
public TemplateModel wrap(Object obj) throws TemplateModelException {
    TemplateModel model = wrapperCache.get(obj);
    if (model != null)
        return model;
    Object wrapped;
    if (obj instanceof Form) {
        wrapped = getFormWrapper((Form) obj);
    } else if (obj instanceof Object[]) {
        this.flattenedForm = context.getSolution().getFlattenedForm((Form) ((Object[]) obj)[0]);
        wrapped = new FormWrapper(flattenedForm, (String) ((Object[]) obj)[1], useControllerProvider, context, design, runtimeProperties != null ? runtimeProperties.getJSONObject("") : null);
    } else if (obj == DefaultNavigator.INSTANCE) {
        wrapped = new FormElement(DefaultNavigator.INSTANCE, context.getSolution(), new PropertyPath(), design);
        JSONObject object = runtimeProperties != null ? runtimeProperties.optJSONObject(((FormElement) wrapped).getName()) : null;
        if (object != null) {
            wrapped = new FormElementContext((FormElement) wrapped, context, object);
        }
    } else if (obj instanceof Part) {
        wrapped = new PartWrapper((Part) obj, flattenedForm, context, design);
    } else if (obj instanceof IFormElement) {
        FormElement fe = null;
        if (formUI != null) {
            List<FormElement> cachedFormElements = formUI.getFormElements();
            for (FormElement cachedFE : cachedFormElements) {
                if (Utils.equalObjects(cachedFE.getPersistIfAvailable(), obj)) {
                    fe = cachedFE;
                    break;
                }
            }
            if (fe == null) {
                Form parentForm = (Form) ((IFormElement) obj).getAncestor(IRepository.FORMS);
                if (parentForm != null && parentForm.isFormComponent()) {
                    for (WebComponent webComponent : formUI.getAllComponents()) {
                        if (webComponent instanceof WebFormComponent) {
                            FormElement cachedFE = ((WebFormComponent) webComponent).getFormElement();
                            if (Utils.equalObjects(cachedFE.getPersistIfAvailable(), obj)) {
                                fe = cachedFE;
                                break;
                            }
                        }
                    }
                }
            }
        }
        FormElement formElement = fe != null ? fe : FormElementHelper.INSTANCE.getFormElement((IFormElement) obj, context.getSolution(), null, false);
        JSONObject object = runtimeProperties != null ? runtimeProperties.optJSONObject(formElement.getName()) : null;
        wrapped = new FormElementContext(formElement, context, object);
    } else {
        wrapped = obj;
    }
    TemplateModel wrap = super.wrap(wrapped);
    wrapperCache.put(obj, wrap);
    return wrap;
}
Also used : Form(com.servoy.j2db.persistence.Form) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) TemplateModel(freemarker.template.TemplateModel) FormElement(com.servoy.j2db.server.ngclient.FormElement) IFormElement(com.servoy.j2db.persistence.IFormElement) IFormElement(com.servoy.j2db.persistence.IFormElement) WebComponent(org.sablo.WebComponent) JSONObject(org.json.JSONObject) Part(com.servoy.j2db.persistence.Part) PropertyPath(com.servoy.j2db.server.ngclient.property.types.PropertyPath) JSONObject(org.json.JSONObject) FormElementContext(com.servoy.j2db.server.ngclient.FormElementContext)

Example 3 with FormElementContext

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

the class ComponentPropertyType method toTemplateJSONValue.

@Override
public JSONWriter toTemplateJSONValue(final JSONWriter writer, String key, ComponentTypeFormElementValue formElementValue, PropertyDescription pd, DataConversion conversionMarkers, final FormElementContext formElementContext) throws JSONException {
    if (formElementValue == null)
        return writer;
    FlattenedSolution clientFlattenedSolution = (formElementContext != null && formElementContext.getContext() != null) ? formElementContext.getContext().getSolution() : null;
    if (!formElementValue.isSecurityViewable(clientFlattenedSolution)) {
        return writer;
    }
    // so that the client knows it must use the custom client side JS for what JSON it gets
    if (conversionMarkers != null)
        conversionMarkers.convert(ComponentPropertyType.TYPE_NAME);
    // create children of component as specified by this property
    final FormElementContext feContext = new FormElementContext(formElementValue.element, formElementContext.getContext(), null);
    JSONUtils.addKeyIfPresent(writer, key);
    writer.object();
    writeTemplateJSONContent(writer, formElementValue, forFoundsetTypedPropertyName(pd), feContext, new IModelWriter() {

        @Override
        public void writeComponentModel() throws JSONException {
            // TODO here we could remove record based props from fe.propertiesForTemplateJSON(); but normally record based props will not write any value in template anyway
            TypedData<Map<String, Object>> modelProperties = feContext.getFormElement().propertiesForTemplateJSON();
            writer.object();
            JSONUtils.writeDataWithConversions(FormElementToJSON.INSTANCE, writer, modelProperties.content, modelProperties.contentType, feContext);
            writer.endObject();
        }
    }, formElementValue.recordBasedProperties, true);
    writer.endObject();
    return writer;
}
Also used : TypedData(org.sablo.websocket.TypedData) FlattenedSolution(com.servoy.j2db.FlattenedSolution) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) FormElementContext(com.servoy.j2db.server.ngclient.FormElementContext)

Aggregations

FormElementContext (com.servoy.j2db.server.ngclient.FormElementContext)3 JSONObject (org.json.JSONObject)3 FormElement (com.servoy.j2db.server.ngclient.FormElement)2 JSONException (org.json.JSONException)2 FlattenedSolution (com.servoy.j2db.FlattenedSolution)1 Form (com.servoy.j2db.persistence.Form)1 IFormElement (com.servoy.j2db.persistence.IFormElement)1 Part (com.servoy.j2db.persistence.Part)1 IWebFormUI (com.servoy.j2db.server.ngclient.IWebFormUI)1 ServoyDataConverterContext (com.servoy.j2db.server.ngclient.ServoyDataConverterContext)1 WebFormComponent (com.servoy.j2db.server.ngclient.WebFormComponent)1 IModelWriter (com.servoy.j2db.server.ngclient.property.ComponentPropertyType.IModelWriter)1 PropertyPath (com.servoy.j2db.server.ngclient.property.types.PropertyPath)1 TemplateModel (freemarker.template.TemplateModel)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 WebComponent (org.sablo.WebComponent)1 TypedData (org.sablo.websocket.TypedData)1 DataConversion (org.sablo.websocket.utils.DataConversion)1