use of com.servoy.j2db.persistence.IChildWebObject 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;
}
Aggregations