Search in sources :

Example 1 with PropertyPath

use of com.servoy.j2db.server.ngclient.property.types.PropertyPath in project servoy-client by Servoy.

the class ServoyClientService method initDefaults.

protected void initDefaults(WebObjectSpecification spec) {
    PropertyPath propertyPath = new PropertyPath();
    IServoyDataConverterContext dataConverterContext = getDataConverterContext();
    // was used inside a service
    for (String propName : spec.getAllPropertiesNames()) {
        PropertyDescription pd = spec.getProperty(propName);
        try {
            Object formElementEquivalentValue = null;
            // IMPORTANT NOTE: if you change anything in following if-else please update FormElement.initTemplateProperties as well
            if (pd.hasDefault()) {
                propertyPath.add(pd.getName());
                formElementEquivalentValue = NGConversions.INSTANCE.convertDesignToFormElementValue(pd.getDefaultValue(), pd, dataConverterContext.getSolution(), null, propertyPath);
                propertyPath.backOneLevel();
            } else if (pd.getType() instanceof IDesignDefaultToFormElement<?, ?, ?>) {
                propertyPath.add(pd.getName());
                formElementEquivalentValue = ((IDesignDefaultToFormElement<?, ?, ?>) pd.getType()).toDefaultFormElementValue(pd, dataConverterContext.getSolution(), null, propertyPath);
                propertyPath.backOneLevel();
            } else if (pd.getType().defaultValue(pd) != null || pd.getType() instanceof IFormElementDefaultValueToSabloComponent) {
                // remember that we can use type specified default value when this gets transformed to JSON
                formElementEquivalentValue = NGConversions.IDesignToFormElement.TYPE_DEFAULT_VALUE_MARKER;
            }
            // simulate form element -> web component conversion
            if (formElementEquivalentValue != null) {
                // here form element, web component and dal params are null as this is a service - it will only work for property types that do not use those in this conversion
                setProperty(propName, NGConversions.INSTANCE.convertFormElementToSabloComponentValue(formElementEquivalentValue, pd, null, null, null));
            // we do not use .setDefaultProperty(...) as that is meant actually as 'template' value for components which is not the case for services
            }
        } catch (Exception e) {
            Debug.log("Default value could not be determined for property '" + propName + "' of service '" + name + "'. Type: '" + pd.getType().getName() + "'. See stack-trace. It is possible that the service .spec is using a property type that is only meant to work with components, not services. A null default will be assumed instead.", e);
        }
    }
}
Also used : PropertyDescription(org.sablo.specification.PropertyDescription) IDesignDefaultToFormElement(com.servoy.j2db.server.ngclient.property.types.NGConversions.IDesignDefaultToFormElement) PropertyPath(com.servoy.j2db.server.ngclient.property.types.PropertyPath) IFormElementDefaultValueToSabloComponent(com.servoy.j2db.server.ngclient.property.types.NGConversions.IFormElementDefaultValueToSabloComponent)

Example 2 with PropertyPath

use of com.servoy.j2db.server.ngclient.property.types.PropertyPath in project servoy-client by Servoy.

the class FormElementHelper method generateFormComponentCacheObject.

private static FormComponentCache generateFormComponentCacheObject(INGFormElement parentElement, PropertyDescription pd, Form frm, FlattenedSolution fs, final List<FormElement> list) {
    IFormElementCache cache = new IFormElementCache() {

        @Override
        public FormElement getFormElement(IFormElement component, FlattenedSolution flattendSol, PropertyPath path, boolean design) {
            for (FormElement formElement : list) {
                if (component.getID() == formElement.getPersistIfAvailable().getID()) {
                    return formElement;
                }
            }
            return FormElementHelper.INSTANCE.getFormElement(component, flattendSol, path, design);
        }
    };
    String template = FormLayoutGenerator.generateFormComponent(frm, fs, cache);
    return new FormComponentCache(list, template, frm.getUUID().toString());
}
Also used : IFormElement(com.servoy.j2db.persistence.IFormElement) PropertyPath(com.servoy.j2db.server.ngclient.property.types.PropertyPath) FlattenedSolution(com.servoy.j2db.FlattenedSolution) IFormElement(com.servoy.j2db.persistence.IFormElement)

Example 3 with PropertyPath

use of com.servoy.j2db.server.ngclient.property.types.PropertyPath in project servoy-client by Servoy.

the class DesignFormLayoutStructureGenerator method generateFormElement.

public static void generateFormElement(PrintWriter writer, IFormElement formElement, Form form, FlattenedSolution fs) {
    String tagName = FormTemplateGenerator.getComponentTypeName(formElement);
    String name = formElement.getName();
    writer.print("<");
    writer.print(tagName);
    writer.print(" name='");
    writer.print(name);
    writer.print("'");
    FormElement fe = new FormElement(formElement, fs, new PropertyPath(), false);
    JSONObject json = new JSONObject(fe.getPropertiesString());
    for (String key : json.keySet()) {
        // for now skip these 2, the size is weird that it has that "Dimension" in it
        if (key.equals("size") || key.equals("visible"))
            continue;
        writer.write(' ');
        writer.write(key);
        writer.write("='");
        writer.write(json.opt(key).toString());
        writer.write("'");
    }
    writer.print(" svy-id='");
    writer.print(formElement.getID());
    writer.print("'");
    writer.print("/>\n");
}
Also used : JSONObject(org.json.JSONObject) PropertyPath(com.servoy.j2db.server.ngclient.property.types.PropertyPath) FormElement(com.servoy.j2db.server.ngclient.FormElement) IFormElement(com.servoy.j2db.persistence.IFormElement)

Example 4 with PropertyPath

use of com.servoy.j2db.server.ngclient.property.types.PropertyPath in project servoy-client by Servoy.

the class FormTemplateObjectWrapper method wrap.

@Override
public TemplateModel wrap(Object obj) throws TemplateModelException {
    TemplateModel model = wrapperCache.get(obj);
    if (model != null)
        return model;
    Object wrapped;
    if (obj instanceof Form) {
        wrapped = getFormWrapper((Form) obj);
    } else if (obj instanceof Object[]) {
        this.flattenedForm = context.getSolution().getFlattenedForm((Form) ((Object[]) obj)[0]);
        wrapped = new FormWrapper(flattenedForm, (String) ((Object[]) obj)[1], useControllerProvider, context, design, runtimeProperties != null ? runtimeProperties.getJSONObject("") : null);
    } else if (obj == DefaultNavigator.INSTANCE) {
        wrapped = new FormElement(DefaultNavigator.INSTANCE, context.getSolution(), new PropertyPath(), design);
        JSONObject object = runtimeProperties != null ? runtimeProperties.optJSONObject(((FormElement) wrapped).getName()) : null;
        if (object != null) {
            wrapped = new FormElementContext((FormElement) wrapped, context, object);
        }
    } else if (obj instanceof Part) {
        wrapped = new PartWrapper((Part) obj, flattenedForm, context, design);
    } else if (obj instanceof IFormElement) {
        FormElement fe = null;
        if (formUI != null) {
            List<FormElement> cachedFormElements = formUI.getFormElements();
            for (FormElement cachedFE : cachedFormElements) {
                if (Utils.equalObjects(cachedFE.getPersistIfAvailable(), obj)) {
                    fe = cachedFE;
                    break;
                }
            }
            if (fe == null) {
                Form parentForm = (Form) ((IFormElement) obj).getAncestor(IRepository.FORMS);
                if (parentForm != null && parentForm.isFormComponent()) {
                    for (WebComponent webComponent : formUI.getAllComponents()) {
                        if (webComponent instanceof WebFormComponent) {
                            FormElement cachedFE = ((WebFormComponent) webComponent).getFormElement();
                            if (Utils.equalObjects(cachedFE.getPersistIfAvailable(), obj)) {
                                fe = cachedFE;
                                break;
                            }
                        }
                    }
                }
            }
        }
        FormElement formElement = fe != null ? fe : FormElementHelper.INSTANCE.getFormElement((IFormElement) obj, context.getSolution(), null, false);
        JSONObject object = runtimeProperties != null ? runtimeProperties.optJSONObject(formElement.getName()) : null;
        wrapped = new FormElementContext(formElement, context, object);
    } else {
        wrapped = obj;
    }
    TemplateModel wrap = super.wrap(wrapped);
    wrapperCache.put(obj, wrap);
    return wrap;
}
Also used : Form(com.servoy.j2db.persistence.Form) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) TemplateModel(freemarker.template.TemplateModel) FormElement(com.servoy.j2db.server.ngclient.FormElement) IFormElement(com.servoy.j2db.persistence.IFormElement) IFormElement(com.servoy.j2db.persistence.IFormElement) WebComponent(org.sablo.WebComponent) JSONObject(org.json.JSONObject) Part(com.servoy.j2db.persistence.Part) PropertyPath(com.servoy.j2db.server.ngclient.property.types.PropertyPath) JSONObject(org.json.JSONObject) FormElementContext(com.servoy.j2db.server.ngclient.FormElementContext)

Example 5 with PropertyPath

use of com.servoy.j2db.server.ngclient.property.types.PropertyPath in project servoy-client by Servoy.

the class ChildrenJSONGenerator method visit.

@SuppressWarnings("nls")
@Override
public Object visit(IPersist o) {
    if (o == skip)
        return IPersistVisitor.CONTINUE_TRAVERSAL;
    if (!isSecurityVisible(o))
        return IPersistVisitor.CONTINUE_TRAVERSAL;
    if (o instanceof IFormElement) {
        FormElement fe = null;
        if (part != null) {
            int startPos = form.getPartStartYPos(part.getID());
            int endPos = part.getHeight();
            Point location = CSSPositionUtils.getLocation((IFormElement) o);
            if (location != null && (startPos > location.y || endPos <= location.y)) {
                return IPersistVisitor.CONTINUE_TRAVERSAL;
            }
        }
        if (cache != null) {
            // this is for form component elements finding
            fe = cache.getFormElement((IFormElement) o, this.context.getSolution(), null, designer);
        }
        if (fe == null && formUI != null) {
            List<FormElement> cachedFormElements = formUI.getFormElements();
            for (FormElement cachedFE : cachedFormElements) {
                if (Utils.equalObjects(cachedFE.getPersistIfAvailable(), o)) {
                    fe = cachedFE;
                    break;
                }
            }
        }
        fe = fe != null ? fe : FormElementHelper.INSTANCE.getFormElement((IFormElement) o, this.context.getSolution(), null, designer);
        writer.object();
        writeFormElement(writer, o, form, fe, formUI, context, designer);
        if (o instanceof WebComponent) {
            WebObjectSpecification spec = fe.getWebComponentSpec();
            if (spec != null) {
                Collection<PropertyDescription> properties = spec.getProperties(FormComponentPropertyType.INSTANCE);
                if (properties.size() > 0) {
                    boolean isResponsive = false;
                    List<String> children = new ArrayList<>();
                    for (PropertyDescription pd : properties) {
                        Object propertyValue = fe.getPropertyValue(pd.getName());
                        Form frm = FormComponentPropertyType.INSTANCE.getForm(propertyValue, context.getSolution());
                        if (frm == null)
                            continue;
                        isResponsive = frm.isResponsiveLayout();
                        // listformcomponents that are responsive must be also send over here (the components are also send over in the FormComponentSabloValue)
                        // this will result in duplicate component data, but we need the structure (and the component names in the right place)
                        // if (!isResponsive && pd.getConfig() instanceof ComponentTypeConfig && ((ComponentTypeConfig)pd.getConfig()).forFoundset != null)
                        // continue;
                        children.add("children_" + pd.getName());
                        writer.key("children_" + pd.getName());
                        writer.array();
                        FormComponentCache fccc = FormElementHelper.INSTANCE.getFormComponentCache(fe, pd, (JSONObject) propertyValue, frm, context.getSolution());
                        if (isResponsive) {
                            // layout containers are not in the cache we need to generate manually the model
                            frm.acceptVisitor(new ChildrenJSONGenerator(writer, context, frm, new IFormElementCache() {

                                @Override
                                public FormElement getFormElement(IFormElement component, FlattenedSolution flattendSol, PropertyPath path, boolean design) {
                                    for (FormElement formElement : fccc.getFormComponentElements()) {
                                        if (component.getID() == formElement.getPersistIfAvailable().getID()) {
                                            return formElement;
                                        }
                                    }
                                    return FormElementHelper.INSTANCE.getFormElement(component, flattendSol, path, design);
                                }
                            }, null, this.form, false, designer), PositionComparator.XY_PERSIST_COMPARATOR);
                        } else {
                            for (FormElement element : fccc.getFormComponentElements()) {
                                IFormElement persistOfElement = (IFormElement) element.getPersistIfAvailable();
                                persistOfElement.acceptVisitor(new ChildrenJSONGenerator(writer, context, null, null, null, this.form, false, designer), FORM_INDEX_WITH_HIERARCHY_COMPARATOR);
                            }
                        }
                        writer.endArray();
                    }
                    writer.key("formComponent");
                    writer.array();
                    children.stream().forEach((child) -> writer.value(child));
                    writer.endArray();
                }
            }
        }
        writer.endObject();
    } else if (o instanceof LayoutContainer) {
        writer.object();
        LayoutContainer layoutContainer = (LayoutContainer) o;
        writeLayoutContainer(writer, layoutContainer, formUI, designer);
        writer.key("children");
        writer.array();
        o.acceptVisitor(new ChildrenJSONGenerator(writer, context, o, cache, null, this.form, false, designer), PositionComparator.XY_PERSIST_COMPARATOR);
        writer.endArray();
        writer.endObject();
        return IPersistVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
    }
    return IPersistVisitor.CONTINUE_TRAVERSAL;
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) FormComponentCache(com.servoy.j2db.server.ngclient.FormElementHelper.FormComponentCache) Form(com.servoy.j2db.persistence.Form) FlattenedForm(com.servoy.j2db.persistence.FlattenedForm) ArrayList(java.util.ArrayList) FlattenedSolution(com.servoy.j2db.FlattenedSolution) Point(java.awt.Point) IFormElement(com.servoy.j2db.persistence.IFormElement) Point(java.awt.Point) PropertyDescription(org.sablo.specification.PropertyDescription) IFormElement(com.servoy.j2db.persistence.IFormElement) WebComponent(com.servoy.j2db.persistence.WebComponent) LayoutContainer(com.servoy.j2db.persistence.LayoutContainer) PropertyPath(com.servoy.j2db.server.ngclient.property.types.PropertyPath) JSONObject(org.json.JSONObject)

Aggregations

PropertyPath (com.servoy.j2db.server.ngclient.property.types.PropertyPath)8 IFormElement (com.servoy.j2db.persistence.IFormElement)7 JSONObject (org.json.JSONObject)4 Form (com.servoy.j2db.persistence.Form)3 ArrayList (java.util.ArrayList)3 PropertyDescription (org.sablo.specification.PropertyDescription)3 FlattenedSolution (com.servoy.j2db.FlattenedSolution)2 Part (com.servoy.j2db.persistence.Part)2 FormElement (com.servoy.j2db.server.ngclient.FormElement)2 Point (java.awt.Point)2 AbstractBase (com.servoy.j2db.persistence.AbstractBase)1 FlattenedForm (com.servoy.j2db.persistence.FlattenedForm)1 GraphicalComponent (com.servoy.j2db.persistence.GraphicalComponent)1 IBasicWebObject (com.servoy.j2db.persistence.IBasicWebObject)1 IChildWebObject (com.servoy.j2db.persistence.IChildWebObject)1 IPersist (com.servoy.j2db.persistence.IPersist)1 LayoutContainer (com.servoy.j2db.persistence.LayoutContainer)1 WebComponent (com.servoy.j2db.persistence.WebComponent)1 FormElementContext (com.servoy.j2db.server.ngclient.FormElementContext)1 FormComponentCache (com.servoy.j2db.server.ngclient.FormElementHelper.FormComponentCache)1