Search in sources :

Example 1 with ISupportChilds

use of com.servoy.j2db.persistence.ISupportChilds 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 2 with ISupportChilds

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

the class FlattenedSolution method getSolutionCopy.

public Solution getSolutionCopy(boolean create) {
    if (mainSolution == null) {
        if (loginFlattenedSolution != null) {
            return loginFlattenedSolution.getSolutionCopy(create);
        }
        return null;
    }
    if (copySolution != null || !create)
        return copySolution;
    try {
        SimplePersistFactory factory = getPersistFactory();
        copySolution = SimplePersistFactory.createDummyCopy(mainSolution);
        copySolution.setChangeHandler(new ChangeHandler(factory) {

            // overwrite this new Object to skip the call to rootObject.registerNewObject(object); which shouldn't be needed for solution model solutions.
            @Override
            public IPersist createNewObject(ISupportChilds parent, int object_type_id, int element_id, UUID uuid) throws RepositoryException {
                return factory.createObject(parent, object_type_id, element_id, uuid);
            }
        });
        copySolution.getChangeHandler().addIPersistListener(this);
        getIndex().setSolutionModelSolution(copySolution);
    } catch (Exception e) {
        Debug.error(e);
    }
    return copySolution;
}
Also used : ISupportChilds(com.servoy.j2db.persistence.ISupportChilds) ChangeHandler(com.servoy.j2db.persistence.ChangeHandler) IPersist(com.servoy.j2db.persistence.IPersist) RepositoryException(com.servoy.j2db.persistence.RepositoryException) UUID(com.servoy.j2db.util.UUID) SimplePersistFactory(com.servoy.j2db.persistence.SimplePersistFactory) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Example 3 with ISupportChilds

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

the class FormElementHelper method getControlledTabSeqReplacementFor.

/**
 * Generates a Servoy controlled tab-sequence-index. We try to avoid sending default (0 or null) tabSeq even
 * for forms that do use default tab sequence in order to avoid problems with nesting default and non-default tabSeq forms.
 *
 * @param designValue the value the persist holds for the tabSeq.
 * @param form the form containing the persist
 * @param persistIfAvailable the persist. For now, 'component' type properties might work with non-persist-linked FormElements so it could be null.
 * When those become persist based as well this will never be null.
 * @return the requested controlled tabSeq (should make tabSeq be identical to the one shown in developer).
 */
public Integer getControlledTabSeqReplacementFor(Integer designValue, PropertyDescription pd, Form flattenedForm, IPersist persistIfAvailable, // TODO more args will be needed here such as the tabSeq property name or description
FlattenedSolution flattenedSolution, // TODO more args will be needed here such as the tabSeq property name or description
boolean design) {
    // TODO this can be removed when we know we'll always have a persist here; currently don't handle this in any way as it's not supported
    if (persistIfAvailable == null || (nestedCall.get() != null && nestedCall.get().booleanValue()))
        return designValue;
    nestedCall.set(Boolean.TRUE);
    final boolean responsiveForm = flattenedForm.isResponsiveLayout();
    try {
        if (flattenedForm.isFormComponent() && persistIfAvailable instanceof AbstractBase) {
            String mainFormName = ((AbstractBase) persistIfAvailable).getRuntimeProperty(FORM_COMPONENT_FORM_NAME);
            if (mainFormName != null) {
                flattenedForm = flattenedSolution.getFlattenedForm(flattenedSolution.getForm(mainFormName));
                // just return the skip value if this is somehow invalid.
                if (flattenedForm == null)
                    return Integer.valueOf(-2);
            }
        }
        boolean formWasModifiedViaSolutionModel = flattenedSolution.hasCopy(flattenedForm);
        Map<TabSeqProperty, Integer> cachedTabSeq = null;
        if (formWasModifiedViaSolutionModel) {
            Pair<Long, Map<TabSeqProperty, Integer>> pair = flattenedForm.getRuntimeProperty(FORM_TAB_SEQUENCE);
            if (pair != null && flattenedForm.getLastModified() == pair.getLeft().longValue()) {
                cachedTabSeq = pair.getRight();
            }
        } else
            cachedTabSeq = formTabSequences.get(flattenedForm.getUUID());
        if (cachedTabSeq == null) {
            cachedTabSeq = new HashMap<TabSeqProperty, Integer>();
            SortedList<TabSeqProperty> selected = new SortedList<TabSeqProperty>(new Comparator<TabSeqProperty>() {

                public int compare(TabSeqProperty o1, TabSeqProperty o2) {
                    int seq1 = o1.getSeqValue();
                    int seq2 = o2.getSeqValue();
                    if (seq1 == ISupportTabSeq.DEFAULT && seq2 == ISupportTabSeq.DEFAULT) {
                        if (responsiveForm) {
                            if (o1.element.getParent() == o2.element.getParent()) {
                                int positionComparator = PositionComparator.comparePoint(false, o1.getLocation(), o2.getLocation());
                                if (positionComparator != 0) {
                                    return positionComparator;
                                }
                            } else {
                                List<ISupportChilds> ancestors = new ArrayList<ISupportChilds>();
                                IPersist persist = o1.element;
                                while (persist.getParent() instanceof AbstractContainer) {
                                    ancestors.add(persist.getParent());
                                    persist = persist.getParent();
                                }
                                persist = o2.element;
                                while (persist.getParent() instanceof AbstractContainer) {
                                    if (ancestors.contains(persist.getParent())) {
                                        // we found the common ancestor
                                        int index = ancestors.indexOf(persist.getParent());
                                        IPersist comparablePersist = index == 0 ? o1.element : ancestors.get(index - 1);
                                        int positionComparator = PositionComparator.comparePoint(false, ((ISupportBounds) comparablePersist).getLocation(), ((ISupportBounds) persist).getLocation());
                                        if (positionComparator != 0) {
                                            return positionComparator;
                                        }
                                    }
                                    persist = persist.getParent();
                                }
                            }
                        } else {
                            int positionComparator = PositionComparator.comparePoint(false, o1.getLocation(), o2.getLocation());
                            if (positionComparator != 0) {
                                return positionComparator;
                            }
                        }
                    }
                    return compareTabSeq(seq1, o1.element, seq2, o2.element, flattenedSolution);
                }
            });
            Map<TabSeqProperty, List<TabSeqProperty>> listFormComponentMap = new HashMap<TabSeqProperty, List<TabSeqProperty>>();
            List<TabSeqProperty> listFormComponentElements = null;
            TabSeqProperty listFormComponentTabSeq = null;
            Iterator<IFormElement> iterator = flattenedForm.getFlattenedObjects(null).iterator();
            while (iterator.hasNext()) {
                IFormElement formElement = iterator.next();
                if (FormTemplateGenerator.isWebcomponentBean(formElement)) {
                    String componentType = FormTemplateGenerator.getComponentTypeName(formElement);
                    WebObjectSpecification specification = WebComponentSpecProvider.getSpecProviderState().getWebComponentSpecification(componentType);
                    if (specification != null) {
                        Collection<PropertyDescription> properties = specification.getProperties(NGTabSeqPropertyType.NG_INSTANCE);
                        Collection<PropertyDescription> formComponentProperties = specification.getProperties(FormComponentPropertyType.INSTANCE);
                        boolean isListFormComponent = isListFormComponent(formComponentProperties);
                        if (properties != null && properties.size() > 0) {
                            IBasicWebComponent webComponent = (IBasicWebComponent) formElement;
                            for (PropertyDescription tabSeqProperty : properties) {
                                int tabseq = Utils.getAsInteger(webComponent.getProperty(tabSeqProperty.getName()));
                                TabSeqProperty seqProperty = new TabSeqProperty(formElement, tabSeqProperty.getName());
                                if (listFormComponentTabSeq == null && isListFormComponent) {
                                    listFormComponentTabSeq = seqProperty;
                                    listFormComponentElements = new ArrayList<TabSeqProperty>();
                                    listFormComponentMap.put(listFormComponentTabSeq, listFormComponentElements);
                                }
                                if (tabseq >= 0) {
                                    selected.add(seqProperty);
                                } else {
                                    cachedTabSeq.put(seqProperty, Integer.valueOf(-2));
                                }
                            }
                        }
                        addFormComponentProperties(formComponentProperties, formElement, flattenedSolution, cachedTabSeq, selected, listFormComponentElements, design, new HashSet<String>());
                    }
                } else if (formElement instanceof ISupportTabSeq) {
                    if (((ISupportTabSeq) formElement).getTabSeq() >= 0) {
                        selected.add(new TabSeqProperty(formElement, StaticContentSpecLoader.PROPERTY_TABSEQ.getPropertyName()));
                    } else {
                        cachedTabSeq.put(new TabSeqProperty(formElement, StaticContentSpecLoader.PROPERTY_TABSEQ.getPropertyName()), Integer.valueOf(-2));
                    }
                }
            }
            int i = 1;
            for (TabSeqProperty tabSeq : selected) {
                cachedTabSeq.put(tabSeq, Integer.valueOf(i++));
            }
            for (TabSeqProperty tabSeq : listFormComponentMap.keySet()) {
                List<TabSeqProperty> elements = listFormComponentMap.get(tabSeq);
                if (elements != null) {
                    Integer value = cachedTabSeq.get(tabSeq);
                    // all elements inside list form component have same tabindex as the list itself
                    for (TabSeqProperty tabSeqElement : elements) {
                        cachedTabSeq.put(tabSeqElement, value);
                    }
                }
            }
            if (!formWasModifiedViaSolutionModel) {
                formTabSequences.putIfAbsent(flattenedForm.getUUID(), cachedTabSeq);
            } else {
                flattenedForm.setRuntimeProperty(FORM_TAB_SEQUENCE, new Pair<>(Long.valueOf(flattenedForm.getLastModified()), cachedTabSeq));
            }
        }
        IPersist realPersist = flattenedForm.findChild(persistIfAvailable.getUUID());
        if (realPersist == null) {
            realPersist = persistIfAvailable;
        }
        Integer controlledTabSeq = cachedTabSeq.get(new TabSeqProperty(realPersist, pd.getName()));
        // if not in tabSeq, use "skip" value
        if (controlledTabSeq == null)
            controlledTabSeq = Integer.valueOf(-2);
        return controlledTabSeq;
    } finally {
        nestedCall.set(Boolean.FALSE);
    }
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) AbstractContainer(com.servoy.j2db.persistence.AbstractContainer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) SortedList(com.servoy.j2db.util.SortedList) ISupportBounds(com.servoy.j2db.persistence.ISupportBounds) ISupportTabSeq(com.servoy.j2db.persistence.ISupportTabSeq) List(java.util.List) ArrayList(java.util.ArrayList) SortedList(com.servoy.j2db.util.SortedList) IBasicWebComponent(com.servoy.j2db.persistence.IBasicWebComponent) ISupportChilds(com.servoy.j2db.persistence.ISupportChilds) AbstractBase(com.servoy.j2db.persistence.AbstractBase) Point(java.awt.Point) PropertyDescription(org.sablo.specification.PropertyDescription) IFormElement(com.servoy.j2db.persistence.IFormElement) IPersist(com.servoy.j2db.persistence.IPersist) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) WeakHashMap(java.util.WeakHashMap)

Example 4 with ISupportChilds

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

the class ServoyFunctionPropertyType method toJSON.

public JSONWriter toJSON(JSONWriter writer, String key, Object object, PropertyDescription pd, DataConversion clientConversion, FlattenedSolution fs, FormElement fe, WebFormComponent formComponent) throws JSONException {
    Map<String, Object> map = new HashMap<>();
    if (object != null && fs != null) {
        // $NON-NLS-1$
        String[] components = object.toString().split("-");
        if (components.length == 5) {
            String scriptString = null;
            // this is a uuid
            ScriptMethod sm = fs.getScriptMethod(object.toString());
            if (sm != null) {
                ISupportChilds parent = sm.getParent();
                if (parent instanceof Solution) {
                    scriptString = "scopes." + sm.getScopeName() + "." + sm.getName();
                } else if (parent instanceof Form) {
                    if (formComponent != null) {
                        // use the real, runtime form
                        scriptString = formComponent.getDataAdapterList().getForm().getForm().getName() + "." + sm.getName();
                    } else {
                        scriptString = ((Form) parent).getName() + "." + sm.getName();
                    }
                } else if (parent instanceof TableNode && fe != null) {
                    scriptString = "entity." + fe.getForm().getName() + "." + sm.getName();
                }
                object = scriptString;
            } else
                Debug.log("can't find a scriptmethod for: " + object);
        }
    }
    try {
        if (object instanceof String) {
            addScriptToMap((String) object, map);
        } else if (object instanceof NativeFunction) {
            nativeFunctionToJSON((NativeFunction) object, map);
        } else if (object instanceof FunctionWrapper && ((FunctionWrapper) object).getWrappedFunction() instanceof NativeFunction) {
            nativeFunctionToJSON((NativeFunction) ((FunctionWrapper) object).getWrappedFunction(), map);
        } else if (object instanceof Map) {
            map = new HashMap<String, Object>((Map<String, Object>) object);
            if (map.get("script") instanceof String)
                addScriptToMap((String) map.get("script"), map);
        }
    } catch (Exception ex) {
        Debug.error(ex);
    }
    return JSONUtils.toBrowserJSONFullValue(writer, key, map.size() == 0 ? null : map, null, clientConversion, null);
}
Also used : FunctionWrapper(com.servoy.j2db.scripting.FunctionWrapper) ISupportChilds(com.servoy.j2db.persistence.ISupportChilds) HashMap(java.util.HashMap) Form(com.servoy.j2db.persistence.Form) JSForm(com.servoy.j2db.scripting.solutionmodel.JSForm) NativeFunction(org.mozilla.javascript.NativeFunction) JSONException(org.json.JSONException) TableNode(com.servoy.j2db.persistence.TableNode) JSONObject(org.json.JSONObject) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) HashMap(java.util.HashMap) Map(java.util.Map) FlattenedSolution(com.servoy.j2db.FlattenedSolution) Solution(com.servoy.j2db.persistence.Solution)

Example 5 with ISupportChilds

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

the class PersistHelper method getFlattenedPersist.

public static ISupportChilds getFlattenedPersist(FlattenedSolution flattenedSolution, Form parent, ISupportChilds persist) {
    ISupportChilds flattenedPersist = persist;
    if (flattenedPersist instanceof Form) {
        flattenedPersist = flattenedSolution.getFlattenedForm(flattenedPersist);
    }
    if (flattenedPersist instanceof LayoutContainer && !(flattenedPersist instanceof FlattenedLayoutContainer)) {
        FlattenedForm ff = flattenedSolution.getFlattenedForm(parent) instanceof FlattenedForm ? (FlattenedForm) flattenedSolution.getFlattenedForm(parent) : flattenedSolution.createFlattenedForm(parent);
        flattenedPersist = new FlattenedLayoutContainer(ff, (LayoutContainer) flattenedPersist);
    }
    return flattenedPersist;
}
Also used : FlattenedForm(com.servoy.j2db.persistence.FlattenedForm) ISupportChilds(com.servoy.j2db.persistence.ISupportChilds) Form(com.servoy.j2db.persistence.Form) FlattenedForm(com.servoy.j2db.persistence.FlattenedForm) FlattenedLayoutContainer(com.servoy.j2db.persistence.FlattenedLayoutContainer) LayoutContainer(com.servoy.j2db.persistence.LayoutContainer) FlattenedLayoutContainer(com.servoy.j2db.persistence.FlattenedLayoutContainer)

Aggregations

ISupportChilds (com.servoy.j2db.persistence.ISupportChilds)11 Form (com.servoy.j2db.persistence.Form)6 IPersist (com.servoy.j2db.persistence.IPersist)6 Solution (com.servoy.j2db.persistence.Solution)4 AbstractBase (com.servoy.j2db.persistence.AbstractBase)3 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 JSONObject (org.json.JSONObject)3 FlattenedSolution (com.servoy.j2db.FlattenedSolution)2 FlattenedForm (com.servoy.j2db.persistence.FlattenedForm)2 IPersistVisitor (com.servoy.j2db.persistence.IPersistVisitor)2 IRootObject (com.servoy.j2db.persistence.IRootObject)2 LayoutContainer (com.servoy.j2db.persistence.LayoutContainer)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 TableNode (com.servoy.j2db.persistence.TableNode)2 UUID (com.servoy.j2db.util.UUID)2 Point (java.awt.Point)2