Search in sources :

Example 1 with DataConversion

use of org.sablo.websocket.utils.DataConversion in project servoy-client by Servoy.

the class AngularFormGenerator method generateJS.

@SuppressWarnings("nls")
@Override
public String generateJS() throws IOException {
    IWebFormController cachedFormController = client != null ? client.getFormManager().getCachedFormController(realFormName) : null;
    FormTemplateObjectWrapper formTemplate = new FormTemplateObjectWrapper(getAContext(), true, false);
    FormWrapper formWrapper = formTemplate.getFormWrapper(form);
    // for this form it is really just some json.
    StringWriter stringWriter = new StringWriter();
    final JSONWriter writer = new JSONWriter(stringWriter);
    writer.object();
    writer.key(realFormName);
    writer.object();
    writer.key("responsive");
    writer.value(form.isResponsiveLayout());
    writer.key("size");
    writer.object();
    writer.key("width");
    writer.value(form.getWidth());
    writer.key("height");
    writer.value(form.getSize().getHeight());
    writer.endObject();
    String styleClasses = form.getStyleClass();
    if (styleClasses != null) {
        writer.key("styleclass");
        String[] classes = styleClasses.split(" ");
        writer.array();
        for (String cls : classes) {
            writer.value(cls);
        }
        writer.endArray();
    }
    writer.key("children");
    // write the default form value object.
    writer.array();
    writer.object();
    writer.key("name");
    writer.value("");
    writer.key("model");
    writer.object();
    Map<String, Object> containerProperties = null;
    if (cachedFormController != null && cachedFormController.getFormUI() instanceof Container) {
        Container con = (Container) cachedFormController.getFormUI();
        DataConversion dataConversion = new DataConversion();
        TypedData<Map<String, Object>> typedProperties = con.getProperties();
        con.writeProperties(FullValueToJSONConverter.INSTANCE, null, writer, typedProperties, dataConversion);
        JSONUtils.writeClientConversions(writer, dataConversion);
        containerProperties = typedProperties.content;
    }
    final Map<String, Object> finalContainerProperties = containerProperties;
    if (formWrapper != null) {
        Map<String, Object> properties = formWrapper.getProperties();
        properties.forEach((key, value) -> {
            if (finalContainerProperties == null || !finalContainerProperties.containsKey(key)) {
                writer.key(key);
                if (value instanceof Integer || value instanceof Long) {
                    writer.value(((Number) value).longValue());
                } else if (value instanceof Float || value instanceof Double) {
                    writer.value(((Number) value).doubleValue());
                } else if (value instanceof Boolean) {
                    writer.value(((Boolean) value).booleanValue());
                } else if (value instanceof Dimension) {
                    writer.object();
                    writer.key("width");
                    writer.value(((Dimension) value).getWidth());
                    writer.key("height");
                    writer.value(((Dimension) value).getHeight());
                    writer.endObject();
                } else {
                    writer.value(value);
                }
            }
        });
    }
    writer.endObject();
    writer.endObject();
    if (form.isResponsiveLayout()) {
        if (zoomedInContainer != null) {
            zoomedInContainer.acceptVisitor(new ChildrenJSONGenerator(writer, getAContext(), form, null, null, form, true, isDesigner), PositionComparator.XY_PERSIST_COMPARATOR);
        } else {
            form.acceptVisitor(new ChildrenJSONGenerator(writer, getAContext(), form, null, null, form, true, isDesigner), PositionComparator.XY_PERSIST_COMPARATOR);
        }
    } else {
        Iterator<Part> it = form.getParts();
        while (it.hasNext()) {
            Part part = it.next();
            if (!Part.rendersOnlyInPrint(part.getPartType())) {
                writer.object();
                writer.key("part");
                writer.value(true);
                writer.key("classes");
                writer.array();
                writer.value("svy-" + PartWrapper.getName(part));
                if (part.getStyleClass() != null) {
                    writer.value(part.getStyleClass());
                }
                writer.endArray();
                writer.key("layout");
                writer.object();
                writer.key("position");
                writer.value("absolute");
                writer.key("left");
                writer.value("0px");
                writer.key("right");
                writer.value("0px");
                int top = form.getPartStartYPos(part.getID());
                if (part.getPartType() <= Part.BODY) {
                    writer.key("top");
                    writer.value(top + "px");
                }
                if (part.getPartType() >= Part.BODY) {
                    writer.key("bottom");
                    writer.value(form.getSize().height - part.getHeight() + "px");
                }
                if (part.getPartType() != Part.BODY) {
                    writer.key("height");
                    writer.value((part.getHeight() - top) + "px");
                }
                if (part.getBackground() != null && !form.getTransparent()) {
                    writer.key("background-color");
                    writer.value(PersistHelper.createColorString(part.getBackground()));
                }
                if (part.getPartType() == Part.BODY) {
                    writer.key("overflow-x");
                    writer.value(AbstractFormLayoutProvider.getCSSScrolling(form.getScrollbars(), true));
                    writer.key("overflow-y");
                    writer.value(AbstractFormLayoutProvider.getCSSScrolling(form.getScrollbars(), false));
                } else {
                    writer.key("overflow");
                    // $NON-NLS-1$
                    writer.value("hidden");
                }
                writer.endObject();
                writer.key("children");
                // write the default form value object.
                writer.array();
                form.acceptVisitor(new ChildrenJSONGenerator(writer, getAContext(), form, null, part, form, true, isDesigner), ChildrenJSONGenerator.FORM_INDEX_WITH_HIERARCHY_COMPARATOR);
                writer.endArray();
                writer.endObject();
            }
        }
    }
    writer.endArray();
    writer.endObject();
    writer.endObject();
    return stringWriter.toString();
}
Also used : JSONWriter(org.json.JSONWriter) FormWrapper(com.servoy.j2db.server.ngclient.template.FormWrapper) Dimension(java.awt.Dimension) FormTemplateObjectWrapper(com.servoy.j2db.server.ngclient.template.FormTemplateObjectWrapper) Point(java.awt.Point) DataConversion(org.sablo.websocket.utils.DataConversion) LayoutContainer(com.servoy.j2db.persistence.LayoutContainer) Container(org.sablo.Container) StringWriter(java.io.StringWriter) Part(com.servoy.j2db.persistence.Part) Map(java.util.Map)

Example 2 with DataConversion

use of org.sablo.websocket.utils.DataConversion in project servoy-client by Servoy.

the class DataProviderDateTest method testWithNightDateOriginal2AndNotUseLocalDateWithddmmyyyFormat.

@Test
public void testWithNightDateOriginal2AndNotUseLocalDateWithddmmyyyFormat() {
    TestDataproviderTypeSabloValue value = createSabloValue(false, THREE_AT_NIGHT_PLUS13, "dd-MM-yyy");
    JSONWriter writer = new JSONStringer();
    writer.object();
    value.toJSON(writer, "mydate", new DataConversion(), null);
    writer.endObject();
    JSONObject json = new JSONObject(writer.toString());
    Assert.assertEquals("2021-11-15T03:00+13:00", json.getString("mydate"));
    value.browserUpdateReceived("2021-11-15T03:00+01:00", null);
    Assert.assertEquals(new Date(NGDatePropertyType.NG_INSTANCE.fromJSON(THREE_AT_NIGHT_PLUS13, false).getTime() + (24 * 60 * 60 * 1000)), value.getValue());
    writer = new JSONStringer();
    writer.object();
    value.toJSON(writer, "mydate", new DataConversion(), null);
    writer.endObject();
    json = new JSONObject(writer.toString());
    Assert.assertEquals("2021-11-16T03:00+13:00", json.getString("mydate"));
}
Also used : JSONWriter(org.json.JSONWriter) DataConversion(org.sablo.websocket.utils.DataConversion) JSONObject(org.json.JSONObject) JSONStringer(org.json.JSONStringer) Date(java.util.Date) Test(org.junit.Test)

Example 3 with DataConversion

use of org.sablo.websocket.utils.DataConversion in project servoy-client by Servoy.

the class DataProviderDateTest method testWithNightDateOriginal2AndNotUseLocalDateWithddMMyyyHHmmFormat.

@Test
public void testWithNightDateOriginal2AndNotUseLocalDateWithddMMyyyHHmmFormat() {
    TestDataproviderTypeSabloValue value = createSabloValue(false, THREE_AT_NIGHT_PLUS13, "dd-MM-yyy HH:mm");
    JSONWriter writer = new JSONStringer();
    writer.object();
    value.toJSON(writer, "mydate", new DataConversion(), null);
    writer.endObject();
    JSONObject json = new JSONObject(writer.toString());
    Assert.assertEquals("2021-11-15T03:00+13:00", json.getString("mydate"));
    // 12 hour more
    value.browserUpdateReceived("2021-11-15T03:00+01:00", null);
    Assert.assertEquals(new Date(NGDatePropertyType.NG_INSTANCE.fromJSON(THREE_AT_NIGHT_PLUS13, false).getTime() + (12 * 60 * 60 * 1000)), value.getValue());
    writer = new JSONStringer();
    writer.object();
    value.toJSON(writer, "mydate", new DataConversion(), null);
    writer.endObject();
    json = new JSONObject(writer.toString());
    Assert.assertEquals("2021-11-15T15:00+13:00", json.getString("mydate"));
}
Also used : JSONWriter(org.json.JSONWriter) DataConversion(org.sablo.websocket.utils.DataConversion) JSONObject(org.json.JSONObject) JSONStringer(org.json.JSONStringer) Date(java.util.Date) Test(org.junit.Test)

Example 4 with DataConversion

use of org.sablo.websocket.utils.DataConversion in project servoy-client by Servoy.

the class DataProviderDateTest method testWithOriginalAndUseLocalDateAndFormatting.

@Test
public void testWithOriginalAndUseLocalDateAndFormatting() {
    TestDataproviderTypeSabloValue value = createSabloValue(true, THREE_IN_AFTERNOON_PLUS13, "dd-MM-yyy");
    value.browserUpdateReceived(THREE_AT_NIGHT_PLUS1, null);
    Assert.assertEquals(NGDatePropertyType.NG_INSTANCE.fromJSON(THREE_IN_AFTERNOON_PLUS13, false), value.getValue());
    JSONWriter writer = new JSONStringer();
    writer.object();
    value.toJSON(writer, "mydate", new DataConversion(), null);
    writer.endObject();
    JSONObject json = new JSONObject(writer.toString());
    Assert.assertEquals("2021-11-15T15:00", json.getString("mydate"));
    value = createSabloValue(true, THREE_IN_AFTERNOON_PLUS13, "dd-MM-yyy");
    value.browserUpdateReceived(THREE_IN_AFTERNOON_PLUS1, null);
    Assert.assertEquals(NGDatePropertyType.NG_INSTANCE.fromJSON("2021-11-14T15:00:00.000+13:00", false), value.getValue());
    writer = new JSONStringer();
    writer.object();
    value.toJSON(writer, "mydate", new DataConversion(), null);
    writer.endObject();
    json = new JSONObject(writer.toString());
    Assert.assertEquals("2021-11-14T15:00", json.getString("mydate"));
    value = createSabloValue(true, THREE_AT_NIGHT_PLUS13, "dd-MM-yyy");
    value.browserUpdateReceived(THREE_IN_AFTERNOON_PLUS1, null);
    Assert.assertEquals(NGDatePropertyType.NG_INSTANCE.fromJSON("2021-11-14T03:00:00.000+13:00", false), value.getValue());
    writer = new JSONStringer();
    writer.object();
    value.toJSON(writer, "mydate", new DataConversion(), null);
    writer.endObject();
    json = new JSONObject(writer.toString());
    Assert.assertEquals("2021-11-14T03:00", json.getString("mydate"));
}
Also used : JSONWriter(org.json.JSONWriter) DataConversion(org.sablo.websocket.utils.DataConversion) JSONObject(org.json.JSONObject) JSONStringer(org.json.JSONStringer) Test(org.junit.Test)

Example 5 with DataConversion

use of org.sablo.websocket.utils.DataConversion in project servoy-client by Servoy.

the class DataProviderDateTest method testWithAfternoonDateOriginal1AndNotUseLocalDateWithoutFormat.

@Test
public void testWithAfternoonDateOriginal1AndNotUseLocalDateWithoutFormat() {
    TestDataproviderTypeSabloValue value = createSabloValue(false, THREE_IN_AFTERNOON_PLUS13, null);
    JSONWriter writer = new JSONStringer();
    writer.object();
    value.toJSON(writer, "mydate", new DataConversion(), null);
    writer.endObject();
    JSONObject json = new JSONObject(writer.toString());
    Assert.assertEquals("2021-11-15T15:00+13:00", json.getString("mydate"));
    // without a format the date is taken as is, no merges.
    value.browserUpdateReceived("2021-11-16T15:00+01:00", null);
    Assert.assertEquals(new Date(NGDatePropertyType.NG_INSTANCE.fromJSON("2021-11-16T15:00+01:00", false).getTime()), value.getValue());
    writer = new JSONStringer();
    writer.object();
    value.toJSON(writer, "mydate", new DataConversion(), null);
    writer.endObject();
    json = new JSONObject(writer.toString());
    Assert.assertEquals("2021-11-17T03:00+13:00", json.getString("mydate"));
}
Also used : JSONWriter(org.json.JSONWriter) DataConversion(org.sablo.websocket.utils.DataConversion) JSONObject(org.json.JSONObject) JSONStringer(org.json.JSONStringer) Date(java.util.Date) Test(org.junit.Test)

Aggregations

DataConversion (org.sablo.websocket.utils.DataConversion)45 JSONObject (org.json.JSONObject)25 JSONWriter (org.json.JSONWriter)24 Test (org.junit.Test)24 JSONStringer (org.json.JSONStringer)18 Date (java.util.Date)12 StringWriter (java.io.StringWriter)10 IWebFormController (com.servoy.j2db.server.ngclient.IWebFormController)8 BrowserConverterContext (org.sablo.specification.property.BrowserConverterContext)8 WebFormComponent (com.servoy.j2db.server.ngclient.WebFormComponent)7 Map (java.util.Map)6 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)5 HashMap (java.util.HashMap)5 JSONException (org.json.JSONException)4 ComponentTypeSabloValue (com.servoy.j2db.server.ngclient.property.ComponentTypeSabloValue)2 Point (java.awt.Point)2 FormAndTableDataProviderLookup (com.servoy.j2db.FormAndTableDataProviderLookup)1 DBValueList (com.servoy.j2db.dataprocessing.DBValueList)1 FoundSet (com.servoy.j2db.dataprocessing.FoundSet)1 LookupValueList (com.servoy.j2db.dataprocessing.LookupValueList)1