X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2FCmsContentSession.java;h=8d475fd204853737eae48b94629c04fcdf858ea2;hb=54098df1bc3ba263dd1e3290aafa880d54d96805;hp=b82507d6f75025528cb4f29c69dc2b9b6637fd91;hpb=7e464c3cedfa41ece64811fb55ddc9ce740a1050;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/acr/CmsContentSession.java b/org.argeo.cms/src/org/argeo/cms/acr/CmsContentSession.java index b82507d6f..8d475fd20 100644 --- a/org.argeo.cms/src/org/argeo/cms/acr/CmsContentSession.java +++ b/org.argeo.cms/src/org/argeo/cms/acr/CmsContentSession.java @@ -1,28 +1,32 @@ package org.argeo.cms.acr; -import java.util.Iterator; +import java.util.HashSet; import java.util.Locale; import java.util.Map; +import java.util.NavigableMap; import java.util.Set; -import java.util.TreeSet; +import java.util.Spliterator; +import java.util.TreeMap; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import java.util.function.Consumer; -import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; import javax.security.auth.Subject; import org.argeo.api.acr.Content; import org.argeo.api.acr.ContentSession; -import org.argeo.api.acr.CrName; -import org.argeo.api.acr.NamespaceUtils; -import org.argeo.api.acr.RuntimeNamespaceContext; +import org.argeo.api.acr.DName; +import org.argeo.api.acr.search.BasicSearch; +import org.argeo.api.acr.search.BasicSearch.Scope; import org.argeo.api.acr.spi.ContentProvider; import org.argeo.api.acr.spi.ProvidedContent; import org.argeo.api.acr.spi.ProvidedRepository; import org.argeo.api.acr.spi.ProvidedSession; -import org.argeo.cms.acr.xml.DomContentProvider; +import org.argeo.api.uuid.UuidFactory; +import org.argeo.cms.CurrentUser; /** Implements {@link ProvidedSession}. */ class CmsContentSession implements ProvidedSession { @@ -32,20 +36,23 @@ class CmsContentSession implements ProvidedSession { private Subject subject; private Locale locale; + private UuidFactory uuidFactory; + private CompletableFuture closed = new CompletableFuture<>(); private CompletableFuture edition; - private Set modifiedProviders = new TreeSet<>(); + private Set modifiedProviders = new HashSet<>(); private Content sessionRunDir; - public CmsContentSession(AbstractContentRepository contentRepository, UUID uuid, Subject subject, Locale locale) { + public CmsContentSession(AbstractContentRepository contentRepository, UUID uuid, Subject subject, Locale locale, + UuidFactory uuidFactory) { this.contentRepository = contentRepository; this.subject = subject; this.locale = locale; this.uuid = uuid; - + this.uuidFactory = uuidFactory; } public void close() { @@ -62,6 +69,8 @@ class CmsContentSession implements ProvidedSession { @Override public Content get(String path) { + if (!path.startsWith(ContentUtils.ROOT_SLASH)) + throw new IllegalArgumentException(path + " is not an absolute path"); ContentProvider contentProvider = contentRepository.getMountManager().findContentProvider(path); String mountPath = contentProvider.getMountPath(); String relativePath = extractRelativePath(mountPath, path); @@ -71,6 +80,8 @@ class CmsContentSession implements ProvidedSession { @Override public boolean exists(String path) { + if (!path.startsWith(ContentUtils.ROOT_SLASH)) + throw new IllegalArgumentException(path + " is not an absolute path"); ContentProvider contentProvider = contentRepository.getMountManager().findContentProvider(path); String mountPath = contentProvider.getMountPath(); String relativePath = extractRelativePath(mountPath, path); @@ -99,6 +110,10 @@ class CmsContentSession implements ProvidedSession { return contentRepository; } + public UuidFactory getUuidFactory() { + return uuidFactory; + } + /* * MOUNT MANAGEMENT */ @@ -111,36 +126,25 @@ class CmsContentSession implements ProvidedSession { } /* - * NAMESPACE CONTEXT + * EDITION */ - -// @Override -// public String getNamespaceURI(String prefix) { -// return RuntimeNamespaceContext.getNamespaceContext().getNamespaceURI(prefix); -//// return NamespaceUtils.getNamespaceURI((p) -> contentRepository.getTypesManager().getPrefixes().get(p), prefix); -// } -// -// @Override -// public Iterator getPrefixes(String namespaceURI) { -// return RuntimeNamespaceContext.getNamespaceContext().getPrefixes(namespaceURI); -//// return NamespaceUtils.getPrefixes((ns) -> contentRepository.getTypesManager().getPrefixes().entrySet().stream() -//// .filter(e -> e.getValue().equals(ns)).map(Map.Entry::getKey).collect(Collectors.toUnmodifiableSet()), -//// namespaceURI); -// } - @Override public CompletionStage edit(Consumer work) { edition = CompletableFuture.supplyAsync(() -> { work.accept(this); return this; }).thenApply((s) -> { - // TODO optimise - for (ContentProvider provider : modifiedProviders) { - if (provider instanceof DomContentProvider) { - ((DomContentProvider) provider).persist(s); + synchronized (CmsContentSession.this) { + // TODO optimise + for (ContentProvider provider : modifiedProviders) { + provider.persist(s); +// if (provider instanceof DomContentProvider) { +// ((DomContentProvider) provider).persist(s); +// } } + modifiedProviders.clear(); + return s; } - return s; }); return edition.minimalCompletionStage(); } @@ -151,7 +155,7 @@ class CmsContentSession implements ProvidedSession { } @Override - public void notifyModification(ProvidedContent content) { + public synchronized void notifyModification(ProvidedContent content) { ContentProvider contentProvider = content.getProvider(); modifiedProviders.add(contentProvider); } @@ -161,7 +165,7 @@ class CmsContentSession implements ProvidedSession { return uuid; } - @Override +// @Override public Content getSessionRunDir() { if (sessionRunDir == null) { String runDirPath = CmsContentRepository.RUN_BASE + '/' + uuid.toString(); @@ -170,30 +174,130 @@ class CmsContentSession implements ProvidedSession { else { Content runDir = get(CmsContentRepository.RUN_BASE); // TODO deal with no run dir available? - sessionRunDir = runDir.add(uuid.toString(), CrName.collection.qName()); + sessionRunDir = runDir.add(uuid.toString(), DName.collection.qName()); } } return sessionRunDir; } -// @Override -// public String findNamespace(String prefix) { -// return prefixes.get(prefix); -// } -// -// @Override -// public Set findPrefixes(String namespaceURI) { -// Set res = prefixes.entrySet().stream().filter(e -> e.getValue().equals(namespaceURI)) -// .map(Map.Entry::getKey).collect(Collectors.toUnmodifiableSet()); -// -// return res; -// } -// -// @Override -// public String findPrefix(String namespaceURI) { -// if (CrName.CR_NAMESPACE_URI.equals(namespaceURI) && prefixes.containsKey(CrName.CR_DEFAULT_PREFIX)) -// return CrName.CR_DEFAULT_PREFIX; -// return ProvidedSession.super.findPrefix(namespaceURI); -// } + @Override + public boolean equals(Object o) { + if (o instanceof CmsContentSession session) + return uuid.equals(session.uuid); + return false; + } + @Override + public String toString() { + return "Content Session " + uuid + " (" + CurrentUser.getUsername(subject) + ")"; + } + + /* + * SEARCH + */ + @Override + public Stream search(Consumer search) { + BasicSearch s = new BasicSearch(); + search.accept(s); + NavigableMap searchPartitions = new TreeMap<>(); + for (Scope scope : s.getFrom()) { + String scopePath = scope.getUri().getPath(); + NavigableMap contentProviders = contentRepository.getMountManager() + .findContentProviders(scopePath); + for (Map.Entry contentProvider : contentProviders.entrySet()) { + // TODO deal with depth + String relPath; + if (scopePath.startsWith(contentProvider.getKey())) { + relPath = scopePath.substring(contentProvider.getKey().length()); + } else { + relPath = null; + } + SearchPartition searchPartition = new SearchPartition(s, relPath, contentProvider.getValue()); + searchPartitions.put(contentProvider.getKey(), searchPartition); + } + } + return StreamSupport.stream(new SearchPartitionsSpliterator(searchPartitions), true); + } + + class SearchPartition { + BasicSearch search; + String relPath; + ContentProvider contentProvider; + + public SearchPartition(BasicSearch search, String relPath, ContentProvider contentProvider) { + super(); + this.search = search; + this.relPath = relPath; + this.contentProvider = contentProvider; + } + + public BasicSearch getSearch() { + return search; + } + + public String getRelPath() { + return relPath; + } + + public ContentProvider getContentProvider() { + return contentProvider; + } + + } + + class SearchPartitionsSpliterator implements Spliterator { + NavigableMap searchPartitions; + + Spliterator currentSpliterator; + + public SearchPartitionsSpliterator(NavigableMap searchPartitions) { + super(); + this.searchPartitions = searchPartitions; + SearchPartition searchPartition = searchPartitions.pollFirstEntry().getValue(); + currentSpliterator = searchPartition.getContentProvider().search(CmsContentSession.this, + searchPartition.getSearch(), searchPartition.getRelPath()); + } + + @Override + public boolean tryAdvance(Consumer action) { + boolean remaining = currentSpliterator.tryAdvance(action); + if (remaining) + return true; + if (searchPartitions.isEmpty()) + return false; + SearchPartition searchPartition = searchPartitions.pollFirstEntry().getValue(); + currentSpliterator = searchPartition.getContentProvider().search(CmsContentSession.this, + searchPartition.getSearch(), searchPartition.getRelPath()); + return true; + } + + @Override + public Spliterator trySplit() { + if (searchPartitions.isEmpty()) { + return null; + } else if (searchPartitions.size() == 1) { + NavigableMap newSearchPartitions = new TreeMap<>(searchPartitions); + searchPartitions.clear(); + return new SearchPartitionsSpliterator(newSearchPartitions); + } else { + NavigableMap newSearchPartitions = new TreeMap<>(); + for (int i = 0; i < searchPartitions.size() / 2; i++) { + Map.Entry searchPartition = searchPartitions.pollLastEntry(); + newSearchPartitions.put(searchPartition.getKey(), searchPartition.getValue()); + } + return new SearchPartitionsSpliterator(newSearchPartitions); + } + } + + @Override + public long estimateSize() { + return Long.MAX_VALUE; + } + + @Override + public int characteristics() { + return NONNULL; + } + + } } \ No newline at end of file