1 package org
.argeo
.cms
.acr
;
3 import java
.util
.Locale
;
5 import java
.util
.TreeSet
;
7 import java
.util
.concurrent
.CompletableFuture
;
8 import java
.util
.concurrent
.CompletionStage
;
9 import java
.util
.function
.Consumer
;
11 import javax
.security
.auth
.Subject
;
13 import org
.argeo
.api
.acr
.Content
;
14 import org
.argeo
.api
.acr
.ContentSession
;
15 import org
.argeo
.api
.acr
.CrName
;
16 import org
.argeo
.api
.acr
.spi
.ContentProvider
;
17 import org
.argeo
.api
.acr
.spi
.ProvidedContent
;
18 import org
.argeo
.api
.acr
.spi
.ProvidedRepository
;
19 import org
.argeo
.api
.acr
.spi
.ProvidedSession
;
20 import org
.argeo
.api
.uuid
.UuidFactory
;
21 import org
.argeo
.cms
.acr
.xml
.DomContentProvider
;
23 /** Implements {@link ProvidedSession}. */
24 class CmsContentSession
implements ProvidedSession
{
25 final private AbstractContentRepository contentRepository
;
27 private final UUID uuid
;
28 private Subject subject
;
29 private Locale locale
;
31 private UuidFactory uuidFactory
;
33 private CompletableFuture
<ProvidedSession
> closed
= new CompletableFuture
<>();
35 private CompletableFuture
<ContentSession
> edition
;
37 private Set
<ContentProvider
> modifiedProviders
= new TreeSet
<>();
39 private Content sessionRunDir
;
41 public CmsContentSession(AbstractContentRepository contentRepository
, UUID uuid
, Subject subject
, Locale locale
,
42 UuidFactory uuidFactory
) {
43 this.contentRepository
= contentRepository
;
44 this.subject
= subject
;
47 this.uuidFactory
= uuidFactory
;
51 closed
.complete(this);
53 if (sessionRunDir
!= null)
54 sessionRunDir
.remove();
58 public CompletionStage
<ProvidedSession
> onClose() {
59 return closed
.minimalCompletionStage();
63 public Content
get(String path
) {
64 ContentProvider contentProvider
= contentRepository
.getMountManager().findContentProvider(path
);
65 String mountPath
= contentProvider
.getMountPath();
66 String relativePath
= extractRelativePath(mountPath
, path
);
67 ProvidedContent content
= contentProvider
.get(CmsContentSession
.this, relativePath
);
72 public boolean exists(String path
) {
73 ContentProvider contentProvider
= contentRepository
.getMountManager().findContentProvider(path
);
74 String mountPath
= contentProvider
.getMountPath();
75 String relativePath
= extractRelativePath(mountPath
, path
);
76 return contentProvider
.exists(this, relativePath
);
79 private String
extractRelativePath(String mountPath
, String path
) {
80 String relativePath
= path
.substring(mountPath
.length());
81 if (relativePath
.length() > 0 && relativePath
.charAt(0) == '/')
82 relativePath
= relativePath
.substring(1);
87 public Subject
getSubject() {
92 public Locale
getLocale() {
97 public ProvidedRepository
getRepository() {
98 return contentRepository
;
101 public UuidFactory
getUuidFactory() {
109 public Content
getMountPoint(String path
) {
110 String
[] parent
= ContentUtils
.getParentPath(path
);
111 ProvidedContent mountParent
= (ProvidedContent
) get(parent
[0]);
112 // Content mountPoint = mountParent.getProvider().get(CmsContentSession.this, null, path);
113 return mountParent
.getMountPoint(parent
[1]);
121 // public String getNamespaceURI(String prefix) {
122 // return RuntimeNamespaceContext.getNamespaceContext().getNamespaceURI(prefix);
123 //// return NamespaceUtils.getNamespaceURI((p) -> contentRepository.getTypesManager().getPrefixes().get(p), prefix);
127 // public Iterator<String> getPrefixes(String namespaceURI) {
128 // return RuntimeNamespaceContext.getNamespaceContext().getPrefixes(namespaceURI);
129 //// return NamespaceUtils.getPrefixes((ns) -> contentRepository.getTypesManager().getPrefixes().entrySet().stream()
130 //// .filter(e -> e.getValue().equals(ns)).map(Map.Entry::getKey).collect(Collectors.toUnmodifiableSet()),
135 public CompletionStage
<ContentSession
> edit(Consumer
<ContentSession
> work
) {
136 edition
= CompletableFuture
.supplyAsync(() -> {
139 }).thenApply((s
) -> {
141 for (ContentProvider provider
: modifiedProviders
) {
142 if (provider
instanceof DomContentProvider
) {
143 ((DomContentProvider
) provider
).persist(s
);
148 return edition
.minimalCompletionStage();
152 public boolean isEditing() {
153 return edition
!= null && !edition
.isDone();
157 public void notifyModification(ProvidedContent content
) {
158 ContentProvider contentProvider
= content
.getProvider();
159 modifiedProviders
.add(contentProvider
);
163 public UUID
getUuid() {
168 public Content
getSessionRunDir() {
169 if (sessionRunDir
== null) {
170 String runDirPath
= CmsContentRepository
.RUN_BASE
+ '/' + uuid
.toString();
171 if (exists(runDirPath
))
172 sessionRunDir
= get(runDirPath
);
174 Content runDir
= get(CmsContentRepository
.RUN_BASE
);
175 // TODO deal with no run dir available?
176 sessionRunDir
= runDir
.add(uuid
.toString(), CrName
.collection
.qName());
179 return sessionRunDir
;
183 // public String findNamespace(String prefix) {
184 // return prefixes.get(prefix);
188 // public Set<String> findPrefixes(String namespaceURI) {
189 // Set<String> res = prefixes.entrySet().stream().filter(e -> e.getValue().equals(namespaceURI))
190 // .map(Map.Entry::getKey).collect(Collectors.toUnmodifiableSet());
196 // public String findPrefix(String namespaceURI) {
197 // if (CrName.CR_NAMESPACE_URI.equals(namespaceURI) && prefixes.containsKey(CrName.CR_DEFAULT_PREFIX))
198 // return CrName.CR_DEFAULT_PREFIX;
199 // return ProvidedSession.super.findPrefix(namespaceURI);