Search in sources :

Example 1 with IChildWebObject

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;
}
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)

Aggregations

AbstractBase (com.servoy.j2db.persistence.AbstractBase)1 IBasicWebObject (com.servoy.j2db.persistence.IBasicWebObject)1 IChildWebObject (com.servoy.j2db.persistence.IChildWebObject)1 IDesignValueConverter (com.servoy.j2db.persistence.IDesignValueConverter)1 IFormElement (com.servoy.j2db.persistence.IFormElement)1 IPersist (com.servoy.j2db.persistence.IPersist)1 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)1 UUID (com.servoy.j2db.util.UUID)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 PropertyDescription (org.sablo.specification.PropertyDescription)1 WebObjectSpecification (org.sablo.specification.WebObjectSpecification)1