Search in sources :

Example 1 with ISupportUpdateableName

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

Aggregations

IGlobalValueEntry (com.servoy.j2db.dataprocessing.IGlobalValueEntry)1 AbstractBase (com.servoy.j2db.persistence.AbstractBase)1 Element (com.servoy.j2db.persistence.ContentSpec.Element)1 IPersist (com.servoy.j2db.persistence.IPersist)1 IPersistVisitor (com.servoy.j2db.persistence.IPersistVisitor)1 IRootObject (com.servoy.j2db.persistence.IRootObject)1 IScriptElement (com.servoy.j2db.persistence.IScriptElement)1 ISupportChilds (com.servoy.j2db.persistence.ISupportChilds)1 ISupportUpdateableName (com.servoy.j2db.persistence.ISupportUpdateableName)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 ScriptNameValidator (com.servoy.j2db.persistence.ScriptNameValidator)1 UUID (com.servoy.j2db.util.UUID)1 RemoteException (java.rmi.RemoteException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 JSONObject (org.json.JSONObject)1