Search in sources :

Example 1 with ServoyJSONObject

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

the class AbstractBase method copyPropertiesMap.

public void copyPropertiesMap(Map<String, Object> newProperties, boolean overwriteMap) {
    if (overwriteMap && propertiesMap.size() > 0) {
        // remove properties that are not in newProperties
        for (String key : propertiesMap.keySet().toArray(new String[propertiesMap.size()])) {
            if (newProperties == null || !newProperties.containsKey(key)) {
                clearProperty(key);
            }
        }
    }
    // apply the new properties
    if (newProperties != null) {
        try {
            startBufferUseForProperties();
            Iterator<Entry<String, Object>> iterator = newProperties.entrySet().iterator();
            while (iterator.hasNext()) {
                Entry<String, Object> next = iterator.next();
                Object v = next.getValue();
                if (v instanceof ServoyJSONObject) {
                    v = ((ServoyJSONObject) v).clone();
                }
                setProperty(next.getKey(), v);
            }
        } finally {
            applyPropertiesBuffer();
        }
    }
}
Also used : ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) Entry(java.util.Map.Entry) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject)

Example 2 with ServoyJSONObject

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

the class MetaDataUtils method deserializeTableMetaDataContents.

/**
 * Deserialize table contents string to of buffered dataset to a string, includes column names and type info
 *
 * @param data
 * @return
 * @throws JSONException
 */
public static BufferedDataSet deserializeTableMetaDataContents(String data) throws JSONException {
    if (data == null) {
        return null;
    }
    ServoyJSONObject json = new ServoyJSONObject(data, true);
    JSONArray jsonColumns = (JSONArray) json.get("columns");
    String[] columnNames = new String[jsonColumns.length()];
    ColumnType[] columnTypes = new ColumnType[jsonColumns.length()];
    for (int c = 0; c < jsonColumns.length(); c++) {
        JSONObject jsonColumn = (JSONObject) jsonColumns.get(c);
        columnNames[c] = jsonColumn.getString("name");
        JSONArray typeArray = new JSONArray(jsonColumn.getString("type"));
        columnTypes[c] = ColumnType.getInstance(typeArray.getInt(0), typeArray.getInt(1), typeArray.getInt(2));
    }
    List<Object[]> rows = new ArrayList<Object[]>();
    JSONArray jsonArray = (JSONArray) json.get("rows");
    for (int r = 0; r < jsonArray.length(); r++) {
        JSONArray rowobj = (JSONArray) jsonArray.get(r);
        Object[] row = new Object[columnNames.length];
        for (int i = 0; i < columnNames.length; i++) {
            Object val = rowobj.get(i);
            if (val == JSONObject.NULL) {
                row[i] = null;
            } else if (Column.mapToDefaultType(columnTypes[i].getSqlType()) == IColumnTypes.MEDIA && val instanceof String) {
                row[i] = Utils.decodeBASE64((String) val);
            } else if (Column.mapToDefaultType(columnTypes[i].getSqlType()) == IColumnTypes.DATETIME && val instanceof String) {
                Date parsed = ServoyJSONObject.parseDate((String) val);
                // convert when possible, otherwise leave to driver (fails on mysql)
                row[i] = parsed == null ? val : parsed;
            } else {
                row[i] = val;
            }
        }
        rows.add(row);
    }
    return BufferedDataSetInternal.createBufferedDataSet(columnNames, columnTypes, rows, false);
}
Also used : BaseColumnType(com.servoy.base.query.BaseColumnType) ColumnType(com.servoy.j2db.query.ColumnType) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) Date(java.util.Date) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject)

Example 3 with ServoyJSONObject

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

the class MetaDataUtils method serializeTableMetaDataContents.

/**
 * Serialize contents of buffered dataset to a string, includes column names and type info
 * @param dataSet
 * @return
 * @throws JSONException
 */
public static String serializeTableMetaDataContents(BufferedDataSet dataSet) throws JSONException {
    if (dataSet == null) {
        return null;
    }
    ServoyJSONObject json = new ServoyJSONObject();
    // columns
    JSONArray jsonColumns = new JSONArray();
    String[] columnNames = dataSet.getColumnNames();
    BaseColumnType[] columnTypes = BufferedDataSetInternal.getColumnTypeInfo(dataSet);
    for (int c = 0; c < columnNames.length; c++) {
        JSONObject jsonColumn = new JSONObject();
        jsonColumn.put("name", columnNames[c]);
        jsonColumn.put("type", XMLUtils.serializeColumnType(columnTypes[c]));
        jsonColumns.put(jsonColumn);
    }
    json.put("columns", jsonColumns);
    // rows
    JSONArray jsonRows = new JSONArray();
    for (int r = 0; r < dataSet.getRowCount(); r++) {
        Object[] row = dataSet.getRow(r);
        JSONArray rowobj = new JSONArray();
        for (int i = 0; i < row.length && i < columnNames.length; i++) {
            Object val;
            if (row[i] == null) {
                val = JSONObject.NULL;
            } else if (row[i] instanceof byte[]) {
                val = Utils.encodeBASE64((byte[]) row[i]);
            } else {
                val = row[i];
            }
            rowobj.put(val);
        }
        jsonRows.put(rowobj);
    }
    json.put("rows", jsonRows);
    // toString
    return json.toString(true);
}
Also used : ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) BaseColumnType(com.servoy.base.query.BaseColumnType)

Example 4 with ServoyJSONObject

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

the class FormElementHelper method generateFormComponentPersists.

private static List<IFormElement> generateFormComponentPersists(INGFormElement parent, PropertyDescription pd, JSONObject json, Form frm, FlattenedSolution fs) {
    List<IFormElement> elements = new ArrayList<>();
    List<IFormElement> formelements = fs.getFlattenedForm(frm).getFlattenedObjects(PositionComparator.XY_PERSIST_COMPARATOR);
    for (IFormElement element : formelements) {
        element = (IFormElement) ((AbstractBase) element).clonePersist(null);
        // we kind of want to have this element a new uuid, but then it is very hard to get it stable.
        UUID newElementUUID = null;
        synchronized (formComponentElementsUUIDS) {
            Map<UUID, UUID> map = formComponentElementsUUIDS.get(parent.getPersistIfAvailable().getUUID());
            if (map == null) {
                map = new HashMap<>();
                formComponentElementsUUIDS.put(parent.getPersistIfAvailable().getUUID(), map);
            }
            newElementUUID = map.get(element.getUUID());
            if (newElementUUID == null) {
                newElementUUID = UUID.randomUUID();
                map.put(element.getUUID(), newElementUUID);
            }
        }
        ((AbstractBase) element).resetUUID(newElementUUID);
        String elementName = element.getName();
        if (elementName == null) {
            elementName = FormElement.SVY_NAME_PREFIX + String.valueOf(element.getID());
        }
        String templateName = getStartElementName(parent, pd) + elementName;
        String formName = parent.getForm().getName();
        if (parent.getForm().isFormComponent() && parent.getPersistIfAvailable() instanceof AbstractBase && ((AbstractBase) parent.getPersistIfAvailable()).getRuntimeProperty(FORM_COMPONENT_FORM_NAME) != null) {
            formName = ((AbstractBase) parent.getPersistIfAvailable()).getRuntimeProperty(FORM_COMPONENT_FORM_NAME);
        }
        ((AbstractBase) element).setRuntimeProperty(FORM_COMPONENT_FORM_NAME, formName);
        ((AbstractBase) element).setRuntimeProperty(FORM_COMPONENT_UUID, parent.getPersistIfAvailable().getUUID().toString());
        JSONObject elementJson = json.optJSONObject(elementName);
        if (elementJson != null) {
            Map<String, Method> methods = RepositoryHelper.getSetters(element);
            WebObjectSpecification legacySpec = FormTemplateGenerator.getWebObjectSpecification(element);
            for (String key : elementJson.keySet()) {
                Object val = elementJson.get(key);
                if (val != null && methods.get(key) != null) {
                    Method method = methods.get(key);
                    Class<?> paramType = method.getParameterTypes()[0];
                    if (!paramType.isAssignableFrom(val.getClass()) && !(paramType.isPrimitive() && val instanceof Number)) {
                        PropertyDescription property = legacySpec.getProperty(key);
                        if (property != null && property.getType() instanceof IDesignValueConverter) {
                            val = ((IDesignValueConverter) property.getType()).fromDesignValue(val, property);
                        } else {
                            // will not fit, very likely a uuid that should be an int.
                            if (val != null) {
                                IPersist found = fs.searchPersist(val.toString());
                                if (found != null)
                                    val = Integer.valueOf(found.getID());
                            }
                        }
                    }
                }
                if (val instanceof JSONObject && ((AbstractBase) element).getProperty(key) instanceof JSONObject) {
                    // if both are json (like a nested form) then merge it in.
                    ServoyJSONObject.mergeAndDeepCloneJSON((JSONObject) val, (JSONObject) ((AbstractBase) element).getProperty(key));
                } else if (val instanceof String && StaticContentSpecLoader.PROPERTY_CUSTOMPROPERTIES.getPropertyName().equals(key) && ((AbstractBase) element).getCustomProperties() != null) {
                    // custom properties needs to be merged in..
                    JSONObject original = new ServoyJSONObject(((AbstractBase) element).getCustomProperties(), true);
                    ServoyJSONObject.mergeAndDeepCloneJSON(new ServoyJSONObject((String) val, true), original);
                    ((AbstractBase) element).setCustomProperties(ServoyJSONObject.toString(original, true, true, true));
                } else if (val instanceof JSONArray && ((AbstractBase) element).getProperty(key) instanceof IChildWebObject[]) {
                    IChildWebObject[] webObjectChildren = (IChildWebObject[]) ((AbstractBase) element).getProperty(key);
                    JSONArray original = new JSONArray();
                    for (IChildWebObject element2 : webObjectChildren) {
                        original.put(element2.getJson());
                    }
                    ServoyJSONObject.mergeAndDeepCloneJSON((JSONArray) val, original);
                    ((AbstractBase) element).setProperty(key, original);
                } else
                    ((AbstractBase) element).setProperty(key, val);
            }
        }
        element.setName(templateName);
        elements.add(element);
    }
    return elements;
}
Also used : IChildWebObject(com.servoy.j2db.persistence.IChildWebObject) WebObjectSpecification(org.sablo.specification.WebObjectSpecification) IDesignValueConverter(com.servoy.j2db.persistence.IDesignValueConverter) ArrayList(java.util.ArrayList) AbstractBase(com.servoy.j2db.persistence.AbstractBase) JSONArray(org.json.JSONArray) Method(java.lang.reflect.Method) PropertyDescription(org.sablo.specification.PropertyDescription) IFormElement(com.servoy.j2db.persistence.IFormElement) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IPersist(com.servoy.j2db.persistence.IPersist) IBasicWebObject(com.servoy.j2db.persistence.IBasicWebObject) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IChildWebObject(com.servoy.j2db.persistence.IChildWebObject) UUID(com.servoy.j2db.util.UUID)

Example 5 with ServoyJSONObject

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

the class ComponentFactory method parseJSonProperties.

public static <T> Map<String, T> parseJSonProperties(String properties) throws IOException {
    if (properties == null || properties.length() <= 0) {
        return null;
    }
    if (properties.startsWith("{")) {
        // new format: json
        try {
            return new JSONWrapperMap<T>(new ServoyJSONObject(properties, false, true, false));
        } catch (JSONException e) {
            Debug.error(e);
            throw new IOException();
        }
    }
    // old format: OpenProperties
    OpenProperties props = new OpenProperties();
    props.load(new StringReader(properties));
    return (Map<String, T>) props;
}
Also used : ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) OpenProperties(com.servoy.j2db.util.OpenProperties) JSONWrapperMap(com.servoy.j2db.util.JSONWrapperMap) StringReader(java.io.StringReader) JSONException(org.json.JSONException) IOException(java.io.IOException) Map(java.util.Map) JSONWrapperMap(com.servoy.j2db.util.JSONWrapperMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) WeakHashMap(java.util.WeakHashMap)

Aggregations

ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)22 JSONObject (org.json.JSONObject)12 JSONException (org.json.JSONException)7 HashMap (java.util.HashMap)6 Map (java.util.Map)5 JSONArray (org.json.JSONArray)5 BaseColumnType (com.servoy.base.query.BaseColumnType)4 WebComponent (com.servoy.j2db.persistence.WebComponent)4 ServoyJSONArray (com.servoy.j2db.util.ServoyJSONArray)4 ArrayList (java.util.ArrayList)4 PropertyDescription (org.sablo.specification.PropertyDescription)4 Form (com.servoy.j2db.persistence.Form)3 JSONWrapperMap (com.servoy.j2db.util.JSONWrapperMap)3 ServoyException (com.servoy.j2db.util.ServoyException)3 IOException (java.io.IOException)3 WebObjectSpecification (org.sablo.specification.WebObjectSpecification)3 BaseQueryTable (com.servoy.base.query.BaseQueryTable)2 ApplicationException (com.servoy.j2db.ApplicationException)2 AbstractBase (com.servoy.j2db.persistence.AbstractBase)2 ITable (com.servoy.j2db.persistence.ITable)2