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);
}
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");
}
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();
}
}
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;
}
Aggregations