use of com.servoy.j2db.util.ServoyJSONObject in project servoy-client by Servoy.
the class WebObjectImpl method setProperty.
/**
* Returns false if it can't set is as a json property. Then caller should set it as another standard persist property.
*/
@Override
public boolean setProperty(String propertyName, Object val) {
PropertyDescription propertyDescription = getPropertyDescription();
if (propertyDescription != null) {
PropertyDescription childPd = propertyDescription.getProperty(propertyName);
if (childPd == null && propertyDescription instanceof WebObjectSpecification && ((WebObjectSpecification) propertyDescription).getHandler(propertyName) != null)
childPd = ((WebObjectSpecification) propertyDescription).getHandler(propertyName).getAsPropertyDescription();
if (childPd != null) {
IPropertyType<?> propertyType = childPd.getType();
if ((val == null && isPersistMappedProperty(propertyName)) || (val instanceof WebCustomType && PropertyUtils.isCustomJSONObjectProperty(propertyType)) || (val instanceof IChildWebObject[] && isArrayOfCustomJSONObject(propertyType)) || (val instanceof WebComponent && isComponent(propertyType)) || (val instanceof IChildWebObject[] && isArrayOfComponent(propertyType))) {
if (getJson() == null)
setJson(new ServoyJSONObject());
getPersistMappedProperties().put(propertyName, val);
persistMappedPropetiesByUUID = null;
updateJSONFromPersistMappedPropeties();
return true;
} else {
// it is a json property defined in spec, but it's not mapping to a persist
setOrRemoveJsonSubproperty(propertyName, convertFromJavaType(getChildPropertyDescription(propertyName), val), false);
return true;
}
}
}
// typeName is not yet set (so normally typed properties are not yet accessed) or it's not a typed property
return false;
}
use of com.servoy.j2db.util.ServoyJSONObject in project servoy-client by Servoy.
the class WebObjectImpl method updateJSONFromPersistMappedPropeties.
@Override
public void updateJSONFromPersistMappedPropeties() {
// writes persist mapped properties back to json
if (arePersistMappedPropertiesLoaded && !skipPersistMappedPropertiesUpdate) {
JSONObject old = (JSONObject) webObject.getOwnProperty(StaticContentSpecLoader.PROPERTY_JSON.getPropertyName());
try {
// we have to keep the same instance if possible cause otherwise com.servoy.eclipse.designer.property.UndoablePropertySheetEntry would set child but restore completely from parent when modifying a child value in case of nested properties
JSONObject entireModel = (old != null ? old : new ServoyJSONObject());
Iterator<String> it = entireModel.keys();
// remove custom properties that were removed (be sure to keep any keys that do not map to custom properties or arrays of custom properties - for example ints, string arrays and so on)
List<String> toRemove = new ArrayList<String>();
while (it.hasNext()) {
String key = it.next();
if (isPersistMappedProperty(key) && !getPersistMappedProperties().containsKey(key))
toRemove.add(key);
}
for (String key : toRemove) {
entireModel.remove(key);
}
for (Map.Entry<String, Object> wo : getPersistMappedProperties().entrySet()) {
if (wo.getValue() == null)
entireModel.put(wo.getKey(), JSONObject.NULL);
else if (wo.getValue() instanceof IChildWebObject) {
entireModel.put(wo.getKey(), ((IChildWebObject) wo.getValue()).getFullJsonInFrmFile());
} else {
ServoyJSONArray jsonArray = new ServoyJSONArray();
int i = 0;
for (IChildWebObject wo1 : (IChildWebObject[]) wo.getValue()) {
if (wo1 != null) {
wo1.setIndex(i);
wo1.setJsonKey(wo.getKey());
}
i++;
jsonArray.put(wo1 != null ? wo1.getFullJsonInFrmFile() : JSONObject.NULL);
}
entireModel.put(wo.getKey(), jsonArray);
}
PropertyDescription childPd = getChildPropertyDescription(wo.getKey());
// the one defined in .spec file as default value, don't write it...)
if (childPd.hasDefault() && Utils.areJSONEqual(childPd.getDefaultValue(), entireModel.opt(wo.getKey()), IChildWebObject.UUID_KEY)) {
entireModel.remove(wo.getKey());
}
}
setJsonInternal(entireModel);
((AbstractBase) webObject).flagChanged();
updateParentJSON();
} catch (JSONException ex) {
Debug.error(ex);
}
}
}
use of com.servoy.j2db.util.ServoyJSONObject in project servoy-client by Servoy.
the class WebObjectImpl method setOrRemoveJsonSubproperty.
private boolean setOrRemoveJsonSubproperty(String key, Object value, boolean remove) {
try {
boolean removed = false;
JSONObject oldJson = (JSONObject) webObject.getOwnProperty(StaticContentSpecLoader.PROPERTY_JSON.getPropertyName());
// we can no longer check for differences here as we now reuse JSON objects/arrays
// we have to keep the same instance if possible cause otherwise com.servoy.eclipse.designer.property.UndoablePropertySheetEntry would set child but restore completely from parent when modifying a child value in case of nested properties
JSONObject jsonObject = (oldJson == null ? new ServoyJSONObject() : oldJson);
ServoyJSONObject oldJsonClone = null;
if (oldJson instanceof ServoyJSONObject) {
oldJsonClone = ((ServoyJSONObject) oldJson).clone();
}
if (remove) {
removed = (jsonObject.remove(key) != null);
} else {
// if value is "null" really allow storing null in there; otherwise jsonObject.put(key, null) will be equivalent to removing the value (useful for when in spec there is a non-null default value given and user chooses null)
jsonObject.put(key, ServoyJSONObject.nullToJsonNull(value));
}
if (oldJsonClone == null || !oldJsonClone.equals(jsonObject)) {
setJsonInternal(jsonObject);
((AbstractBase) webObject).flagChanged();
}
if (arePersistMappedPropertiesLoaded && getPropertyDescription() != null) {
// update this web object's child web objects if needed (if this key affects them)
updatePersistMappedPropertyFromJSON(key, value);
}
// else not yet loaded - they will all load later so nothing to do here
updateParentJSON();
return removed;
} catch (JSONException e) {
Debug.error(e);
}
return false;
}
use of com.servoy.j2db.util.ServoyJSONObject in project servoy-client by Servoy.
the class AbstractBase method putCustomProperty.
@SuppressWarnings("unchecked")
public Object putCustomProperty(String[] path, Object value) {
String customProperties = getTypedProperty(StaticContentSpecLoader.PROPERTY_CUSTOMPROPERTIES);
boolean isNotExtendingAnotherPersist = isNotExtendingAnotherPersist();
if (isNotExtendingAnotherPersist && customProperties == null && value == null)
return null;
if (jsonCustomProperties == null) {
if (customProperties != null) {
jsonCustomProperties = new JSONWrapperMap(customProperties);
} else {
jsonCustomProperties = new JSONWrapperMap(new ServoyJSONObject());
}
}
Map<String, Object> map = jsonCustomProperties;
for (int i = 0; i < path.length - 1; i++) {
if (!map.containsKey(path[i])) {
if (isNotExtendingAnotherPersist && value == null) {
// nothing to set there; it's already not there and it is not overriding any inherited value
return null;
}
map.put(path[i], new ServoyJSONObject());
}
map = (Map<String, Object>) map.get(path[i]);
}
String leaf = path[path.length - 1];
Object old = null;
if (isNotExtendingAnotherPersist && value == null) {
old = map.remove(leaf);
if (map.isEmpty() && path.length > 1) {
// remove empty map
putCustomProperty(Utils.arraySub(path, 0, path.length - 1), null);
}
} else {
// in case of inherited elements when put(key, null) is called we really need to write the null (which for JSONObject means writing JSONObject.NULL; if we would just put null it would be equivalent to a remove(key) which would not override key to remove parent value through inheritance)
old = map.put(leaf, value != null ? value : JSONObject.NULL);
}
setTypedProperty(StaticContentSpecLoader.PROPERTY_CUSTOMPROPERTIES, jsonCustomProperties.toString());
return old;
}
use of com.servoy.j2db.util.ServoyJSONObject in project servoy-client by Servoy.
the class AbstractBase method clearCustomProperty.
@SuppressWarnings("unchecked")
public Object clearCustomProperty(String[] path) {
String customProperties = getTypedProperty(StaticContentSpecLoader.PROPERTY_CUSTOMPROPERTIES);
if (customProperties == null)
return null;
if (jsonCustomProperties == null) {
if (customProperties != null) {
jsonCustomProperties = new JSONWrapperMap(customProperties);
} else {
jsonCustomProperties = new JSONWrapperMap(new ServoyJSONObject());
}
}
Map<String, Object> map = jsonCustomProperties;
for (int i = 0; i < path.length - 1; i++) {
if (!map.containsKey(path[i])) {
// nothing to clear there; it's already not there
return null;
}
map = (Map<String, Object>) map.get(path[i]);
}
String leaf = path[path.length - 1];
Object old = null;
old = map.remove(leaf);
if (map.isEmpty() && path.length > 1) {
// remove empty map
clearCustomProperty(Utils.arraySub(path, 0, path.length - 1));
}
// the recursive clearCustomProperty call above might set jsonCustomProperties to null if there were no more custom properties...
if (jsonCustomProperties != null) {
if (jsonCustomProperties.isEmpty())
clearProperty(StaticContentSpecLoader.PROPERTY_CUSTOMPROPERTIES.getPropertyName());
else
setTypedProperty(StaticContentSpecLoader.PROPERTY_CUSTOMPROPERTIES, jsonCustomProperties.toString());
}
return old;
}
Aggregations