Search in sources :

Example 11 with FormElement

use of com.servoy.j2db.server.ngclient.FormElement 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 12 with FormElement

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

the class PersistFieldInstanceTest method testCustomComponentWithFormProperty.

@Test
public void testCustomComponentWithFormProperty() throws RepositoryException, JSONException {
    // TODO this should become a test on form uuid in the inner html/bean xml instead of the form name..
    Form form = solution.getForm("test");
    Assert.assertNotNull(form);
    DataAdapterList dataAdapterList = new DataAdapterList(new TestFormController(form, client));
    Form tabForm = solution.createNewForm(validator, null, "tabform", null, false, new Dimension(600, 400));
    // as client's "inDesigner" == true we will generate an error bean because legacy Bean usage for custom web components with custom object/array types is depreacated and not fully working (in designer at least)
    // so we will check that it generates an error bean (that means no props are set)
    // TODO maybe this can be uncommented after https://support.servoy.com/browse/SVY-9459 is done
    // Bean bean = form.createNewBean("mycustombean", "my-component");
    // bean.setInnerHTML("{atype:{name:'name',form:'tabform'}}");
    List<FormElement> formElements = FormElementHelper.INSTANCE.getFormElements(form.getAllObjects(), new ServoyDataConverterContext(client));
    // Assert.assertEquals(1, formElements.size());
    // WebFormComponent wc = ComponentFactory.createComponent(client, dataAdapterList, formElements.get(0), null);
    @SuppressWarnings("unchecked") WebComponent // form.removeChild(bean);
    webComponent = form.createNewWebComponent("mycustombean", "my-component");
    webComponent.setProperty("atype", new ServoyJSONObject("{name:'name',form:'tabform'}", false));
    formElements = FormElementHelper.INSTANCE.getFormElements(form.getAllObjects(), new ServoyDataConverterContext(client));
    Assert.assertEquals(1, formElements.size());
    WebFormComponent wc = ComponentFactory.createComponent(client, dataAdapterList, formElements.get(0), null, form);
    Map<String, Object> type = (Map<String, Object>) wc.getProperty("atype");
    Assert.assertEquals("name", type.get("name"));
    Assert.assertEquals("tabform", type.get("form"));
    Assert.assertEquals(0, wc.getAndClearChanges().content.size());
    TypedData<Map<String, Object>> props = wc.getProperties();
    String json = JSONUtils.writeDataWithConversions(props.content, props.contentType, null);
    JSONAssert.assertEquals("{\"atype\":{\"rt\":\"my-component.mytype\",\"vEr\":2,\"v\":{\"form\":\"tabform\",\"name\":\"name\"}},\"svyMarkupId\":\"sf331d64ddc0c17747371b7740e3e3447\",\"svy_types\":{\"atype\":\"JSON_obj\"}}", json, true);
}
Also used : Form(com.servoy.j2db.persistence.Form) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) IDataAdapterList(com.servoy.j2db.server.ngclient.IDataAdapterList) DataAdapterList(com.servoy.j2db.server.ngclient.DataAdapterList) Dimension(java.awt.Dimension) FormElement(com.servoy.j2db.server.ngclient.FormElement) WebComponent(com.servoy.j2db.persistence.WebComponent) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) ServoyDataConverterContext(com.servoy.j2db.server.ngclient.ServoyDataConverterContext) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 13 with FormElement

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

the class PersistFieldInstanceTest method testCustomComponentWithI18NProperty.

@Test
public void testCustomComponentWithI18NProperty() throws RepositoryException, JSONException {
    Form form = solution.getForm("test");
    Assert.assertNotNull(form);
    DataAdapterList dataAdapterList = new DataAdapterList(new TestFormController(form, client));
    WebComponent bean = form.createNewWebComponent("mycustombean", "my-component");
    bean.setProperty("atype", new ServoyJSONObject("{name:'name',text:'i18n:servoy.button.ok'}", false));
    List<FormElement> formElements = FormElementHelper.INSTANCE.getFormElements(form.getAllObjects(), new ServoyDataConverterContext(client));
    Assert.assertEquals(1, formElements.size());
    WebFormComponent wc = ComponentFactory.createComponent(client, dataAdapterList, formElements.get(0), null, form);
    BrowserConverterContext allowBrowserConverterContext = new BrowserConverterContext(wc, PushToServerEnum.allow);
    Map<String, Object> type = (Map<String, Object>) wc.getProperty("atype");
    Assert.assertEquals("name", type.get("name"));
    // it gets automatically translated to a static string
    Assert.assertEquals("OK", ((BasicTagStringTypeSabloValue) type.get("text")).getDesignValue());
    Assert.assertEquals("OK", ((BasicTagStringTypeSabloValue) type.get("text")).getTagReplacedValue());
    Assert.assertEquals(0, wc.getAndClearChanges().content.size());
    TypedData<Map<String, Object>> props = wc.getProperties();
    String json = JSONUtils.writeDataWithConversions(props.content, props.contentType, allowBrowserConverterContext);
    JSONAssert.assertEquals("{\"atype\":{\"rt\":\"my-component.mytype\",\"vEr\":2,\"v\":{\"text\":\"OK\",\"name\":\"name\"}},\"svyMarkupId\":\"sf331d64ddc0c17747371b7740e3e3447\",\"svy_types\":{\"atype\":\"JSON_obj\"}}", json, true);
}
Also used : Form(com.servoy.j2db.persistence.Form) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) IDataAdapterList(com.servoy.j2db.server.ngclient.IDataAdapterList) DataAdapterList(com.servoy.j2db.server.ngclient.DataAdapterList) FormElement(com.servoy.j2db.server.ngclient.FormElement) WebComponent(com.servoy.j2db.persistence.WebComponent) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) ServoyDataConverterContext(com.servoy.j2db.server.ngclient.ServoyDataConverterContext) BrowserConverterContext(org.sablo.specification.property.BrowserConverterContext) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 14 with FormElement

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

the class DesignFormLayoutStructureGenerator method generateFormElement.

public static void generateFormElement(PrintWriter writer, IFormElement formElement, Form form, FlattenedSolution fs) {
    String tagName = FormTemplateGenerator.getComponentTypeName(formElement);
    String name = formElement.getName();
    writer.print("<");
    writer.print(tagName);
    writer.print(" name='");
    writer.print(name);
    writer.print("'");
    FormElement fe = new FormElement(formElement, fs, new PropertyPath(), false);
    JSONObject json = new JSONObject(fe.getPropertiesString());
    for (String key : json.keySet()) {
        // for now skip these 2, the size is weird that it has that "Dimension" in it
        if (key.equals("size") || key.equals("visible"))
            continue;
        writer.write(' ');
        writer.write(key);
        writer.write("='");
        writer.write(json.opt(key).toString());
        writer.write("'");
    }
    writer.print(" svy-id='");
    writer.print(formElement.getID());
    writer.print("'");
    writer.print("/>\n");
}
Also used : JSONObject(org.json.JSONObject) PropertyPath(com.servoy.j2db.server.ngclient.property.types.PropertyPath) FormElement(com.servoy.j2db.server.ngclient.FormElement) IFormElement(com.servoy.j2db.persistence.IFormElement)

Example 15 with FormElement

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

the class FormLayoutGenerator method generateRecordViewForm.

public static void generateRecordViewForm(PrintWriter writer, Form form, String realFormName, IServoyDataConverterContext context, boolean design) {
    generateFormStartTag(writer, form, realFormName, false, design);
    Iterator<Part> it = form.getParts();
    if (design) {
        while (it.hasNext()) {
            Part part = it.next();
            if (!Part.rendersOnlyInPrint(part.getPartType())) {
                writer.print("<div ng-style=\"");
                writer.print(PartWrapper.getName(part));
                writer.print("Style\"");
                String partClass = "svy-" + PartWrapper.getName(part);
                if (part.getStyleClass() != null) {
                    partClass += " " + part.getStyleClass();
                }
                writer.print(" class=\"");
                writer.print(partClass);
                writer.print("\">");
                generateEndDiv(writer);
            }
        }
    }
    Map<IPersist, FormElement> cachedElementsMap = new HashMap<IPersist, FormElement>();
    if (context != null && context.getApplication() != null) {
        IFormController controller = ((BasicFormManager) context.getApplication().getFormManager()).getCachedFormController(realFormName);
        if (controller != null && controller.getFormUI() instanceof WebFormUI) {
            List<FormElement> cachedFormElements = ((WebFormUI) controller.getFormUI()).getFormElements();
            for (FormElement fe : cachedFormElements) {
                if (fe.getPersistIfAvailable() != null) {
                    cachedElementsMap.put(fe.getPersistIfAvailable(), fe);
                }
            }
        }
    }
    it = form.getParts();
    while (it.hasNext()) {
        Part part = it.next();
        if (!Part.rendersOnlyInPrint(part.getPartType())) {
            if (!design) {
                writer.print("<div svy-ng-style=\"");
                writer.print(PartWrapper.getName(part));
                writer.print("Style\"");
                String partClass = "svy-" + PartWrapper.getName(part);
                if (part.getStyleClass() != null) {
                    partClass += " " + part.getStyleClass();
                }
                writer.print(" class=\"");
                writer.print(partClass);
                writer.print("\">");
            }
            for (IFormElement bc : PartWrapper.getBaseComponents(part, form, context, design, false)) {
                FormElement fe = null;
                if (cachedElementsMap.containsKey(bc)) {
                    fe = cachedElementsMap.get(bc);
                }
                if (fe == null) {
                    fe = FormElementHelper.INSTANCE.getFormElement(bc, context.getSolution(), null, design);
                }
                generateFormElementWrapper(writer, fe, form, form.isResponsiveLayout());
                generateFormElement(writer, fe, form);
                generateEndDiv(writer);
            }
            if (!design)
                generateEndDiv(writer);
        }
    }
    generateFormEndTag(writer, design);
}
Also used : IFormElement(com.servoy.j2db.persistence.IFormElement) WebFormUI(com.servoy.j2db.server.ngclient.WebFormUI) IPersist(com.servoy.j2db.persistence.IPersist) HashMap(java.util.HashMap) Part(com.servoy.j2db.persistence.Part) BasicFormManager(com.servoy.j2db.BasicFormManager) IFormController(com.servoy.j2db.IFormController) FormElement(com.servoy.j2db.server.ngclient.FormElement) IFormElement(com.servoy.j2db.persistence.IFormElement)

Aggregations

FormElement (com.servoy.j2db.server.ngclient.FormElement)22 IFormElement (com.servoy.j2db.persistence.IFormElement)12 Form (com.servoy.j2db.persistence.Form)11 WebFormComponent (com.servoy.j2db.server.ngclient.WebFormComponent)10 HashMap (java.util.HashMap)10 JSONObject (org.json.JSONObject)9 ServoyDataConverterContext (com.servoy.j2db.server.ngclient.ServoyDataConverterContext)6 IDataAdapterList (com.servoy.j2db.server.ngclient.IDataAdapterList)5 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)5 Dimension (java.awt.Dimension)5 Map (java.util.Map)5 Test (org.junit.Test)5 DataAdapterList (com.servoy.j2db.server.ngclient.DataAdapterList)4 FormComponentCache (com.servoy.j2db.server.ngclient.FormElementHelper.FormComponentCache)4 INGFormElement (com.servoy.j2db.server.ngclient.INGFormElement)4 IWebFormUI (com.servoy.j2db.server.ngclient.IWebFormUI)4 WebFormUI (com.servoy.j2db.server.ngclient.WebFormUI)4 CustomValueList (com.servoy.j2db.dataprocessing.CustomValueList)3 IValueList (com.servoy.j2db.dataprocessing.IValueList)3 IPersist (com.servoy.j2db.persistence.IPersist)3