Search in sources :

Example 21 with ServoyJSONObject

use of com.servoy.j2db.util.ServoyJSONObject 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 22 with ServoyJSONObject

use of com.servoy.j2db.util.ServoyJSONObject in project servoy-client by Servoy.

the class MaterialAttributesExtractor method main.

public static void main(String[] args) throws FileNotFoundException, IOException {
    FileInputStream is = new FileInputStream("test/com/servoy/j2db/server/ngclient/component/spec/material_layout.css");
    String css = IOUtils.toString(is, "UTF-8");
    is.close();
    Map<String, Set<String>> attributes = new HashMap<>();
    Pattern p = Pattern.compile("\\[(.+?)(\\=(.+?))?\\]");
    Matcher matcher = p.matcher(css);
    while (matcher.find()) {
        Set<String> list = getValues(attributes, matcher.group(1));
        if (matcher.group(3) != null) {
            list.addAll(Arrays.asList(matcher.group(3).replaceAll("\"", "").split(" ")));
        }
    }
    ServoyJSONObject obj = new ServoyJSONObject(false, true);
    for (String key : attributes.keySet()) {
        Set<String> values = attributes.get(key);
        String type = "string";
        if (!values.isEmpty()) {
            // get the first non-null value
            String v = values.iterator().next();
            if ("0".equals(v) || Utils.getAsInteger(v) != 0)
                type = "object";
        }
        ServoyJSONObject attribute = new ServoyJSONObject(false, false);
        attribute.put("type", type);
        if (type.equals("string") && values.size() > 0) {
            ServoyJSONArray arr = new ServoyJSONArray(values);
            attribute.put("values", arr);
        }
        obj.put(key, attribute);
    }
    ServoyJSONObject o = new ServoyJSONObject(false, true);
    o.put("attributes", obj);
    FileUtils.writeStringToFile(new File("test/com/servoy/j2db/server/ngclient/component/spec/attributes.json"), o.toString());
    System.out.println("Generated file attributes.json");
}
Also used : Pattern(java.util.regex.Pattern) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) Set(java.util.Set) TreeSet(java.util.TreeSet) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ServoyJSONArray(com.servoy.j2db.util.ServoyJSONArray) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 23 with ServoyJSONObject

use of com.servoy.j2db.util.ServoyJSONObject in project servoy-client by Servoy.

the class PersistBasedFormElementImpl method getFormElementPropertyValues.

public Map<String, Object> getFormElementPropertyValues(FlattenedSolution fs, Map<String, PropertyDescription> specProperties, PropertyPath propertyPath) {
    Map<String, String> parsedAttributes = new HashMap<String, String>();
    if (persist instanceof BaseComponent) {
        Map<String, String> attributes = new HashMap<String, String>(((BaseComponent) persist).getMergedAttributes());
        if (attributes != null && attributes.size() > 0) {
            attributes.forEach((key, value) -> {
                if (value != null && key != null)
                    parsedAttributes.put(StringEscapeUtils.escapeEcmaScript(key), value);
            });
        }
    }
    if (persist instanceof IBasicWebComponent) {
        if (FormTemplateGenerator.isWebcomponentBean(persist)) {
            JSONObject jsonProperties = ((IBasicWebComponent) persist).getFlattenedJson();
            if (jsonProperties == null)
                jsonProperties = new ServoyJSONObject();
            // convert from persist design-time value (which might be non-json) to the expected value
            Map<String, Object> jsonMap = processPersistProperties(fs, specProperties, propertyPath);
            // this is handled separately as NG component definition
            jsonMap.remove(IContentSpecConstantsBase.PROPERTY_BEANXML);
            // this is handled separately as NG component definition
            jsonMap.remove(IContentSpecConstants.PROPERTY_JSON);
            try {
                // add beanXML (which is actually a JSON string here) defined properties to the map
                formElement.convertFromJSONToFormElementValues(fs, specProperties, jsonMap, formElement.getWebComponentSpec().getHandlers(), jsonProperties, propertyPath);
            } catch (Exception ex) {
                Debug.error("Error while parsing bean design json", ex);
                jsonMap.put("error", "Error while parsing bean design json(bean not supported in NGClient?): " + persist);
            }
            if (parsedAttributes.size() > 0) {
                jsonMap.put(IContentSpecConstants.PROPERTY_ATTRIBUTES, parsedAttributes);
            }
            return jsonMap;
        } else {
            Map<String, Object> defaultProperties = new HashMap<String, Object>();
            defaultProperties.put(StaticContentSpecLoader.PROPERTY_SIZE.getPropertyName(), ((IBasicWebComponent) persist).getSize());
            defaultProperties.put(StaticContentSpecLoader.PROPERTY_NAME.getPropertyName(), ((IBasicWebComponent) persist).getName());
            defaultProperties.put("error", "Bean not supported in NGClient: " + persist);
            return defaultProperties;
        }
    } else if (persist instanceof AbstractBase) {
        Map<String, Object> map = processPersistProperties(fs, specProperties, propertyPath);
        if (persist instanceof Field && ((Field) persist).getDisplayType() == Field.MULTISELECT_LISTBOX) {
            map.put("multiselectListbox", Boolean.TRUE);
        } else if (persist instanceof TabPanel) {
            convertFromTabPanelToNGProperties((IFormElement) persist, fs, map, specProperties, propertyPath);
        } else if (persist instanceof Portal) {
            convertFromPortalToNGProperties((Portal) persist, fs, map, specProperties, propertyPath);
        }
        if (parsedAttributes.size() > 0) {
            map.put(IContentSpecConstants.PROPERTY_ATTRIBUTES, parsedAttributes);
        }
        return map;
    } else {
        return Collections.emptyMap();
    }
}
Also used : TabPanel(com.servoy.j2db.persistence.TabPanel) BaseComponent(com.servoy.j2db.persistence.BaseComponent) HashMap(java.util.HashMap) AbstractBase(com.servoy.j2db.persistence.AbstractBase) JSONException(org.json.JSONException) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) Field(com.servoy.j2db.persistence.Field) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) Portal(com.servoy.j2db.persistence.Portal) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IBasicWebComponent(com.servoy.j2db.persistence.IBasicWebComponent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with ServoyJSONObject

use of com.servoy.j2db.util.ServoyJSONObject in project servoy-client by Servoy.

the class FoundSetManager method getViewFoundSet.

@SuppressWarnings("nls")
@Override
public ViewFoundSet getViewFoundSet(String name, QBSelect query, boolean register) {
    if (query.getQuery().getColumns() == null || query.getQuery().getColumns().size() == 0) {
        throw new RuntimeException("Can't create a ViewFoundset with name: " + name + " and query  " + query + " that has no columns");
    }
    String dataSource = DataSourceUtils.createViewDataSource(name);
    ViewFoundSet vfs = new ViewFoundSet(dataSource, query.build(), application.getFoundSetManager(), config.pkChunkSize());
    // if this datasource defintion is created already in the developer then we need to check if the query columns are correctly matching it.
    ServoyJSONObject columnsDef = null;
    Iterator<TableNode> tblIte = application.getFlattenedSolution().getTableNodes(dataSource);
    while (tblIte.hasNext() && columnsDef == null) {
        TableNode tn = tblIte.next();
        columnsDef = tn.getColumns();
        if (columnsDef != null) {
            TableDef def = DatabaseUtils.deserializeTableInfo(columnsDef);
            for (ColumnInfoDef col : def.columnInfoDefSet) {
                IQuerySelectValue selectValue = getSelectvalue(query, col.name);
                if (selectValue == null) {
                    Debug.error("Column " + col.name + " of type " + col.columnType.toString() + " defined in view datasource '" + dataSource + "' was not found in the provided query.");
                    return null;
                }
                BaseColumnType columnType = selectValue.getColumnType();
                // relax the mapping on default Servoy types
                if (columnType != null && Column.mapToDefaultType(columnType.getSqlType()) != Column.mapToDefaultType(col.columnType.getSqlType())) {
                    Debug.error("Column type for column '" + col.name + " of type " + col.columnType.toString() + "' defined in view datasource '" + dataSource + "' does not match the one " + columnType + " provided in the query.");
                    return null;
                }
            }
        }
    }
    registerViewFoundSet(vfs, !register);
    return vfs;
}
Also used : ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) TableNode(com.servoy.j2db.persistence.TableNode) TableDef(com.servoy.j2db.util.xmlxport.TableDef) ColumnInfoDef(com.servoy.j2db.util.xmlxport.ColumnInfoDef) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue) BaseColumnType(com.servoy.base.query.BaseColumnType)

Aggregations

ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)24 JSONObject (org.json.JSONObject)12 JSONException (org.json.JSONException)7 BaseColumnType (com.servoy.base.query.BaseColumnType)6 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 JSONArray (org.json.JSONArray)5 TableNode (com.servoy.j2db.persistence.TableNode)4 WebComponent (com.servoy.j2db.persistence.WebComponent)4 ServoyException (com.servoy.j2db.util.ServoyException)4 ServoyJSONArray (com.servoy.j2db.util.ServoyJSONArray)4 ColumnInfoDef (com.servoy.j2db.util.xmlxport.ColumnInfoDef)4 TableDef (com.servoy.j2db.util.xmlxport.TableDef)4 IOException (java.io.IOException)4 PropertyDescription (org.sablo.specification.PropertyDescription)4 BaseQueryTable (com.servoy.base.query.BaseQueryTable)3 ApplicationException (com.servoy.j2db.ApplicationException)3 Form (com.servoy.j2db.persistence.Form)3 ITable (com.servoy.j2db.persistence.ITable)3