Search in sources :

Example 1 with IPersistVisitor

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

the class FlattenedSolution method getMaxID.

protected int getMaxID() {
    final int[] maxId = new int[1];
    IPersistVisitor visitor = new IPersistVisitor() {

        public Object visit(IPersist o) {
            if (maxId[0] < o.getID()) {
                maxId[0] = o.getID();
            }
            return IPersistVisitor.CONTINUE_TRAVERSAL;
        }
    };
    mainSolution.acceptVisitor(visitor);
    if (modules != null) {
        for (Solution module : modules) {
            module.acceptVisitor(visitor);
        }
    }
    int max = maxId[0];
    if (loginFlattenedSolution != null) {
        int loginMax = loginFlattenedSolution.getMaxID();
        if (loginMax > max) {
            max = loginMax;
        }
    }
    return max;
}
Also used : IPersist(com.servoy.j2db.persistence.IPersist) IPersistVisitor(com.servoy.j2db.persistence.IPersistVisitor) Solution(com.servoy.j2db.persistence.Solution)

Example 2 with IPersistVisitor

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

the class FlattenedSolution method clonePersist.

@SuppressWarnings({ "unchecked", "nls" })
public <T extends AbstractBase> T clonePersist(T persist, String newName, AbstractBase newParent) {
    T clone = (T) persist.clonePersist(persist.getParent() == newParent ? null : newParent);
    final Map<Object, Object> updatedElementIds = AbstractPersistFactory.resetUUIDSRecursively(clone, getPersistFactory(), false);
    if (persist.getParent() == newParent) {
        newParent.addChild(clone);
    }
    // make sure that this persist is not seen as a copy a real persist/form
    clone.setRuntimeProperty(CLONE_PROPERTY, null);
    if (clone instanceof ISupportUpdateableName) {
        try {
            ((ISupportUpdateableName) clone).updateName(new ScriptNameValidator(this), newName);
        } catch (Exception e) {
            if (newParent != null) {
                newParent.removeChild(clone);
            }
            throw new RuntimeException("name '" + newName + "' invalid for the clone of " + ((ISupportName) persist).getName() + ", error: " + e.getMessage());
        }
    }
    if (clone instanceof ISupportChilds) {
        clone.acceptVisitor(new IPersistVisitor() {

            public Object visit(IPersist o) {
                if (o instanceof AbstractBase) {
                    Map<String, Object> propertiesMap = ((AbstractBase) o).getPropertiesMap();
                    for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
                        Object elementId = updatedElementIds.get(entry.getValue());
                        if (elementId instanceof Integer) {
                            Element element = StaticContentSpecLoader.getContentSpec().getPropertyForObjectTypeByName(o.getTypeID(), entry.getKey());
                            if (element.getTypeID() == IRepository.ELEMENTS)
                                ((AbstractBase) o).setProperty(entry.getKey(), elementId);
                        } else if (entry.getValue() instanceof JSONObject) {
                            updateElementReferences((JSONObject) entry.getValue(), updatedElementIds);
                        }
                    }
                }
                return null;
            }
        });
    }
    if (securityAccess != null) {
        ConcurrentMap<Object, Integer> securityValues = securityAccess.getLeft();
        for (Object elementUUID : new HashSet(securityValues.keySet())) {
            if (updatedElementIds.containsKey(elementUUID.toString())) {
                UUID uuid = Utils.getAsUUID(updatedElementIds.get(elementUUID.toString()), false);
                if (uuid != null) {
                    securityValues.put(uuid, securityValues.get(elementUUID));
                }
            }
        }
    }
    flush(persist);
    getIndex().reload();
    return clone;
}
Also used : ISupportUpdateableName(com.servoy.j2db.persistence.ISupportUpdateableName) ISupportChilds(com.servoy.j2db.persistence.ISupportChilds) IScriptElement(com.servoy.j2db.persistence.IScriptElement) Element(com.servoy.j2db.persistence.ContentSpec.Element) AbstractBase(com.servoy.j2db.persistence.AbstractBase) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IGlobalValueEntry(com.servoy.j2db.dataprocessing.IGlobalValueEntry) JSONObject(org.json.JSONObject) IPersist(com.servoy.j2db.persistence.IPersist) IPersistVisitor(com.servoy.j2db.persistence.IPersistVisitor) JSONObject(org.json.JSONObject) IRootObject(com.servoy.j2db.persistence.IRootObject) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator) UUID(com.servoy.j2db.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet)

Example 3 with IPersistVisitor

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

the class FlattenedSolution method updatePersistInSolutionCopy.

public void updatePersistInSolutionCopy(final IPersist persist) {
    if (mainSolution == null && loginFlattenedSolution != null) {
        loginFlattenedSolution.updatePersistInSolutionCopy(persist);
        return;
    }
    if (copySolution == null)
        return;
    IPersist copyPersist = (IPersist) copySolution.acceptVisitor(new IPersistVisitor() {

        public Object visit(IPersist o) {
            if (o.getUUID().equals(persist.getUUID())) {
                return o;
            }
            return IPersistVisitor.CONTINUE_TRAVERSAL;
        }
    });
    if (copyPersist != null) {
        ((AbstractBase) copyPersist).copyPropertiesMap(((AbstractBase) persist).getPropertiesMap(), false);
        ISupportChilds parent = copyPersist.getParent();
        flush(persist);
        if (parent instanceof Form) {
            ((Form) parent).setLastModified(System.currentTimeMillis());
        } else if (copyPersist instanceof Form) {
            ((Form) copyPersist).setLastModified(System.currentTimeMillis());
        }
    } else if (persist.getParent() != null && !(persist.getParent() instanceof Solution)) {
        ISupportChilds copyParent = (ISupportChilds) copySolution.acceptVisitor(new IPersistVisitor() {

            public Object visit(IPersist o) {
                if (o.getUUID().equals(persist.getParent().getUUID())) {
                    return o;
                }
                return IPersistVisitor.CONTINUE_TRAVERSAL;
            }
        });
        if (copyParent != null) {
            if (persist instanceof ICloneable) {
                ((ICloneable) persist).clonePersist((AbstractBase) copyParent);
            } else {
                copyParent.addChild(persist);
            }
            flush(persist);
            if (copyParent instanceof Form) {
                ((Form) copyParent).setLastModified(System.currentTimeMillis());
            }
        }
    }
}
Also used : ICloneable(com.servoy.j2db.persistence.ICloneable) ISupportChilds(com.servoy.j2db.persistence.ISupportChilds) IPersist(com.servoy.j2db.persistence.IPersist) Form(com.servoy.j2db.persistence.Form) FlattenedForm(com.servoy.j2db.persistence.FlattenedForm) AbstractBase(com.servoy.j2db.persistence.AbstractBase) IPersistVisitor(com.servoy.j2db.persistence.IPersistVisitor) JSONObject(org.json.JSONObject) IRootObject(com.servoy.j2db.persistence.IRootObject) Solution(com.servoy.j2db.persistence.Solution)

Example 4 with IPersistVisitor

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

the class DebugUtils method isReferenceFormUsedInForm.

private static boolean isReferenceFormUsedInForm(final ClientState clientState, final Form referenceForm, Form form) {
    final boolean[] isReferenceFormUsedInForm = { false };
    form.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) {
                    for (PropertyDescription pd : properties) {
                        Form frm = FormComponentPropertyType.INSTANCE.getForm(wc.getProperty(pd.getName()), clientState.getFlattenedSolution());
                        if (referenceForm.equals(frm)) {
                            isReferenceFormUsedInForm[0] = true;
                            return IPersistVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
                        } else {
                            isReferenceFormUsedInForm[0] = isReferenceFormUsedInForm(clientState, referenceForm, frm);
                            if (isReferenceFormUsedInForm[0]) {
                                return IPersistVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
                            }
                        }
                    }
                }
            }
            return IPersistVisitor.CONTINUE_TRAVERSAL;
        }
    });
    return isReferenceFormUsedInForm[0];
}
Also used : PropertyDescription(org.sablo.specification.PropertyDescription) WebComponent(com.servoy.j2db.persistence.WebComponent) WebObjectSpecification(org.sablo.specification.WebObjectSpecification) IPersist(com.servoy.j2db.persistence.IPersist) Form(com.servoy.j2db.persistence.Form) FlattenedForm(com.servoy.j2db.persistence.FlattenedForm) IPersistVisitor(com.servoy.j2db.persistence.IPersistVisitor) Collection(java.util.Collection)

Example 5 with IPersistVisitor

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

Aggregations

IPersist (com.servoy.j2db.persistence.IPersist)5 IPersistVisitor (com.servoy.j2db.persistence.IPersistVisitor)5 FlattenedForm (com.servoy.j2db.persistence.FlattenedForm)3 Form (com.servoy.j2db.persistence.Form)3 Solution (com.servoy.j2db.persistence.Solution)3 AbstractBase (com.servoy.j2db.persistence.AbstractBase)2 IRootObject (com.servoy.j2db.persistence.IRootObject)2 ISupportChilds (com.servoy.j2db.persistence.ISupportChilds)2 WebComponent (com.servoy.j2db.persistence.WebComponent)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 JSONObject (org.json.JSONObject)2 PropertyDescription (org.sablo.specification.PropertyDescription)2 WebObjectSpecification (org.sablo.specification.WebObjectSpecification)2 IFormController (com.servoy.j2db.IFormController)1 FoundSetManager (com.servoy.j2db.dataprocessing.FoundSetManager)1 IGlobalValueEntry (com.servoy.j2db.dataprocessing.IGlobalValueEntry)1 DebugSwingFormMananger (com.servoy.j2db.debug.DebugJ2DBClient.DebugSwingFormMananger)1 Element (com.servoy.j2db.persistence.ContentSpec.Element)1 Field (com.servoy.j2db.persistence.Field)1