Search in sources :

Example 16 with WebComponent

use of com.servoy.j2db.persistence.WebComponent in project servoy-client by Servoy.

the class DebugUtils method getScopesAndFormsToReload.

public static Set<IFormController>[] getScopesAndFormsToReload(final ClientState clientState, Collection<IPersist> changes) {
    Set<IFormController> scopesToReload = new HashSet<IFormController>();
    final Set<IFormController> formsToReload = new HashSet<IFormController>();
    final SpecProviderState specProviderState = WebComponentSpecProvider.getSpecProviderState();
    final Set<Form> formsUpdated = new HashSet<Form>();
    for (IPersist persist : changes) {
        clientState.getFlattenedSolution().updatePersistInSolutionCopy(persist);
        if (persist instanceof ScriptMethod) {
            if (persist.getParent() instanceof Form) {
                Form form = (Form) persist.getParent();
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers(form);
                for (IFormController formController : cachedFormControllers) {
                    scopesToReload.add(formController);
                }
            } else if (persist.getParent() instanceof Solution) {
                LazyCompilationScope scope = clientState.getScriptEngine().getScopesScope().getGlobalScope(((ScriptMethod) persist).getScopeName());
                scope.remove((IScriptProvider) persist);
                scope.put((IScriptProvider) persist, (IScriptProvider) persist);
            } else if (persist.getParent() instanceof TableNode) {
                clientState.getFoundSetManager().reloadFoundsetMethod(((TableNode) persist.getParent()).getDataSource(), (IScriptProvider) persist);
            }
            if (clientState instanceof DebugJ2DBClient) {
                // ((DebugJ2DBClient)clientState).clearUserWindows();  no need for this as window API was refactored and it allows users to clean up dialogs
                ((DebugSwingFormMananger) ((DebugJ2DBClient) clientState).getFormManager()).fillScriptMenu();
            }
        } else if (persist instanceof ScriptVariable) {
            ScriptVariable sv = (ScriptVariable) persist;
            if (persist.getParent() instanceof Solution) {
                clientState.getScriptEngine().getScopesScope().getGlobalScope(sv.getScopeName()).put(sv);
            }
            if (persist.getParent() instanceof Form) {
                Form form = (Form) persist.getParent();
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers(form);
                for (IFormController formController : cachedFormControllers) {
                    FormScope scope = formController.getFormScope();
                    scope.put(sv);
                }
            }
        } else if (persist.getAncestor(IRepository.FORMS) != null) {
            final Form form = (Form) persist.getAncestor(IRepository.FORMS);
            if (form != null && form.isFormComponent().booleanValue()) {
                // if the changed form is a reference form we need to check if that is referenced by a loaded form..
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
                for (IFormController fc : cachedFormControllers) {
                    fc.getForm().acceptVisitor(new IPersistVisitor() {

                        @Override
                        public Object visit(IPersist o) {
                            if (o instanceof WebComponent) {
                                WebComponent wc = (WebComponent) o;
                                WebObjectSpecification spec = FormTemplateGenerator.getWebObjectSpecification(wc);
                                Collection<PropertyDescription> properties = spec != null ? spec.getProperties(FormComponentPropertyType.INSTANCE) : null;
                                if (properties != null && properties.size() > 0) {
                                    Form persistForm = (Form) wc.getAncestor(IRepository.FORMS);
                                    for (PropertyDescription pd : properties) {
                                        Form frm = FormComponentPropertyType.INSTANCE.getForm(wc.getProperty(pd.getName()), clientState.getFlattenedSolution());
                                        if (frm != null && (form.equals(frm) || FlattenedForm.hasFormInHierarchy(frm, form) || isReferenceFormUsedInForm(clientState, form, frm)) && !formsUpdated.contains(persistForm)) {
                                            formsUpdated.add(persistForm);
                                            List<IFormController> cfc = clientState.getFormManager().getCachedFormControllers(persistForm);
                                            for (IFormController formController : cfc) {
                                                formsToReload.add(formController);
                                            }
                                        }
                                    }
                                }
                            }
                            return IPersistVisitor.CONTINUE_TRAVERSAL;
                        }
                    });
                }
            } else if (!formsUpdated.contains(form)) {
                formsUpdated.add(form);
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers(form);
                for (IFormController formController : cachedFormControllers) {
                    formsToReload.add(formController);
                }
            }
            if (persist instanceof Form && clientState.getFormManager() instanceof DebugUtils.DebugUpdateFormSupport) {
                ((DebugUtils.DebugUpdateFormSupport) clientState.getFormManager()).updateForm((Form) persist);
            }
        } else if (persist instanceof ScriptCalculation) {
            ScriptCalculation sc = (ScriptCalculation) persist;
            if (((RemoteDebugScriptEngine) clientState.getScriptEngine()).recompileScriptCalculation(sc)) {
                List<String> al = new ArrayList<String>();
                al.add(sc.getDataProviderID());
                try {
                    String dataSource = clientState.getFoundSetManager().getDataSource(sc.getTable());
                    ((FoundSetManager) clientState.getFoundSetManager()).getRowManager(dataSource).clearCalcs(null, al);
                    ((FoundSetManager) clientState.getFoundSetManager()).flushSQLSheet(dataSource);
                } catch (Exception e) {
                    Debug.error(e);
                }
            }
        // if (clientState instanceof DebugJ2DBClient)
        // {
        // ((DebugJ2DBClient)clientState).clearUserWindows(); no need for this as window API was refactored and it allows users to clean up dialogs
        // }
        } else if (persist instanceof Relation) {
            ((FoundSetManager) clientState.getFoundSetManager()).flushSQLSheet((Relation) persist);
            List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
            try {
                String primary = ((Relation) persist).getPrimaryDataSource();
                for (IFormController formController : cachedFormControllers) {
                    if (primary.equals(formController.getDataSource())) {
                        final IFormController finalController = formController;
                        final Relation finalRelation = (Relation) persist;
                        formController.getForm().acceptVisitor(new IPersistVisitor() {

                            @Override
                            public Object visit(IPersist o) {
                                if (o instanceof Tab && Utils.equalObjects(finalRelation.getName(), ((Tab) o).getRelationName())) {
                                    formsToReload.add(finalController);
                                    return o;
                                }
                                if (o instanceof Field && ((Field) o).getValuelistID() > 0) {
                                    ValueList vl = clientState.getFlattenedSolution().getValueList(((Field) o).getValuelistID());
                                    if (vl != null && Utils.equalObjects(finalRelation.getName(), vl.getRelationName())) {
                                        formsToReload.add(finalController);
                                        return o;
                                    }
                                }
                                if (o instanceof WebComponent) {
                                    WebComponent webComponent = (WebComponent) o;
                                    WebObjectSpecification spec = specProviderState == null ? null : specProviderState.getWebComponentSpecification(webComponent.getTypeName());
                                    if (spec != null) {
                                        Collection<PropertyDescription> properties = spec.getProperties(RelationPropertyType.INSTANCE);
                                        for (PropertyDescription pd : properties) {
                                            if (Utils.equalObjects(webComponent.getFlattenedJson().opt(pd.getName()), finalRelation.getName())) {
                                                formsToReload.add(finalController);
                                                return o;
                                            }
                                        }
                                    }
                                }
                                return CONTINUE_TRAVERSAL;
                            }
                        });
                    }
                }
            } catch (Exception e) {
                Debug.error(e);
            }
        } else if (persist instanceof ValueList) {
            ComponentFactory.flushValueList(clientState, (ValueList) persist);
            List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
            for (IFormController formController : cachedFormControllers) {
                final IFormController finalController = formController;
                final ValueList finalValuelist = (ValueList) persist;
                formController.getForm().acceptVisitor(new IPersistVisitor() {

                    @Override
                    public Object visit(IPersist o) {
                        if (o instanceof Field && ((Field) o).getValuelistID() > 0 && ((Field) o).getValuelistID() == finalValuelist.getID()) {
                            formsToReload.add(finalController);
                            return o;
                        }
                        if (o instanceof WebComponent) {
                            WebComponent webComponent = (WebComponent) o;
                            WebObjectSpecification spec = specProviderState == null ? null : specProviderState.getWebComponentSpecification(webComponent.getTypeName());
                            if (spec != null) {
                                Collection<PropertyDescription> properties = spec.getProperties(ValueListPropertyType.INSTANCE);
                                for (PropertyDescription pd : properties) {
                                    if (Utils.equalObjects(webComponent.getFlattenedJson().opt(pd.getName()), finalValuelist.getUUID().toString())) {
                                        formsToReload.add(finalController);
                                        return o;
                                    }
                                }
                            }
                        }
                        return CONTINUE_TRAVERSAL;
                    }
                });
            }
        } else if (persist instanceof Style) {
            ComponentFactory.flushStyle(null, ((Style) persist));
            List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
            String styleName = ((Style) persist).getName();
            for (IFormController formController : cachedFormControllers) {
                if (styleName.equals(formController.getForm().getStyleName())) {
                    formsToReload.add(formController);
                }
            }
        }
    }
    return new Set[] { scopesToReload, formsToReload };
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) Set(java.util.Set) HashSet(java.util.HashSet) Form(com.servoy.j2db.persistence.Form) FlattenedForm(com.servoy.j2db.persistence.FlattenedForm) ValueList(com.servoy.j2db.persistence.ValueList) LazyCompilationScope(com.servoy.j2db.scripting.LazyCompilationScope) FormScope(com.servoy.j2db.scripting.FormScope) WebComponent(com.servoy.j2db.persistence.WebComponent) Field(com.servoy.j2db.persistence.Field) Relation(com.servoy.j2db.persistence.Relation) Style(com.servoy.j2db.persistence.Style) ValueList(com.servoy.j2db.persistence.ValueList) List(java.util.List) ArrayList(java.util.ArrayList) Solution(com.servoy.j2db.persistence.Solution) HashSet(java.util.HashSet) FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) DebugSwingFormMananger(com.servoy.j2db.debug.DebugJ2DBClient.DebugSwingFormMananger) RhinoException(org.mozilla.javascript.RhinoException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServoyException(com.servoy.j2db.util.ServoyException) PropertyDescription(org.sablo.specification.PropertyDescription) ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) SpecProviderState(org.sablo.specification.SpecProviderState) Tab(com.servoy.j2db.persistence.Tab) IPersist(com.servoy.j2db.persistence.IPersist) IScriptProvider(com.servoy.j2db.persistence.IScriptProvider) TableNode(com.servoy.j2db.persistence.TableNode) ScriptVariable(com.servoy.j2db.persistence.ScriptVariable) IPersistVisitor(com.servoy.j2db.persistence.IPersistVisitor) Collection(java.util.Collection) IFormController(com.servoy.j2db.IFormController) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod)

Example 17 with WebComponent

use of com.servoy.j2db.persistence.WebComponent 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

WebComponent (com.servoy.j2db.persistence.WebComponent)17 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)9 PropertyDescription (org.sablo.specification.PropertyDescription)9 WebObjectSpecification (org.sablo.specification.WebObjectSpecification)9 Form (com.servoy.j2db.persistence.Form)8 JSONObject (org.json.JSONObject)7 ArrayList (java.util.ArrayList)4 ServoyClientSupport (com.servoy.base.scripting.annotations.ServoyClientSupport)3 FlattenedForm (com.servoy.j2db.persistence.FlattenedForm)3 Dimension (java.awt.Dimension)3 JSFunction (org.mozilla.javascript.annotations.JSFunction)3 AbstractContainer (com.servoy.j2db.persistence.AbstractContainer)2 Field (com.servoy.j2db.persistence.Field)2 IPersist (com.servoy.j2db.persistence.IPersist)2 IPersistVisitor (com.servoy.j2db.persistence.IPersistVisitor)2 DataAdapterList (com.servoy.j2db.server.ngclient.DataAdapterList)2 FormElement (com.servoy.j2db.server.ngclient.FormElement)2 IDataAdapterList (com.servoy.j2db.server.ngclient.IDataAdapterList)2 ServoyDataConverterContext (com.servoy.j2db.server.ngclient.ServoyDataConverterContext)2 WebFormComponent (com.servoy.j2db.server.ngclient.WebFormComponent)2