]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsContentSession.java
13494dd0a8588825b23c386fa926471bbe5543dc
[lgpl/argeo-commons.git] / CmsContentSession.java
1 package org.argeo.cms.acr;
2
3 import java.util.HashSet;
4 import java.util.Locale;
5 import java.util.Set;
6 import java.util.UUID;
7 import java.util.concurrent.CompletableFuture;
8 import java.util.concurrent.CompletionStage;
9 import java.util.function.Consumer;
10
11 import javax.security.auth.Subject;
12
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;
22
23 /** Implements {@link ProvidedSession}. */
24 class CmsContentSession implements ProvidedSession {
25 final private AbstractContentRepository contentRepository;
26
27 private final UUID uuid;
28 private Subject subject;
29 private Locale locale;
30
31 private UuidFactory uuidFactory;
32
33 private CompletableFuture<ProvidedSession> closed = new CompletableFuture<>();
34
35 private CompletableFuture<ContentSession> edition;
36
37 private Set<ContentProvider> modifiedProviders = new HashSet<>();
38
39 private Content sessionRunDir;
40
41 public CmsContentSession(AbstractContentRepository contentRepository, UUID uuid, Subject subject, Locale locale,
42 UuidFactory uuidFactory) {
43 this.contentRepository = contentRepository;
44 this.subject = subject;
45 this.locale = locale;
46 this.uuid = uuid;
47 this.uuidFactory = uuidFactory;
48 }
49
50 public void close() {
51 closed.complete(this);
52
53 if (sessionRunDir != null)
54 sessionRunDir.remove();
55 }
56
57 @Override
58 public CompletionStage<ProvidedSession> onClose() {
59 return closed.minimalCompletionStage();
60 }
61
62 @Override
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);
68 return content;
69 }
70
71 @Override
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);
77 }
78
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);
83 return relativePath;
84 }
85
86 @Override
87 public Subject getSubject() {
88 return subject;
89 }
90
91 @Override
92 public Locale getLocale() {
93 return locale;
94 }
95
96 @Override
97 public ProvidedRepository getRepository() {
98 return contentRepository;
99 }
100
101 public UuidFactory getUuidFactory() {
102 return uuidFactory;
103 }
104
105 /*
106 * MOUNT MANAGEMENT
107 */
108 @Override
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]);
114 }
115
116 /*
117 * NAMESPACE CONTEXT
118 */
119
120 // @Override
121 // public String getNamespaceURI(String prefix) {
122 // return RuntimeNamespaceContext.getNamespaceContext().getNamespaceURI(prefix);
123 //// return NamespaceUtils.getNamespaceURI((p) -> contentRepository.getTypesManager().getPrefixes().get(p), prefix);
124 // }
125 //
126 // @Override
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()),
131 //// namespaceURI);
132 // }
133
134 @Override
135 public CompletionStage<ContentSession> edit(Consumer<ContentSession> work) {
136 edition = CompletableFuture.supplyAsync(() -> {
137 work.accept(this);
138 return this;
139 }).thenApply((s) -> {
140 // TODO optimise
141 for (ContentProvider provider : modifiedProviders) {
142 if (provider instanceof DomContentProvider) {
143 ((DomContentProvider) provider).persist(s);
144 }
145 }
146 return s;
147 });
148 return edition.minimalCompletionStage();
149 }
150
151 @Override
152 public boolean isEditing() {
153 return edition != null && !edition.isDone();
154 }
155
156 @Override
157 public void notifyModification(ProvidedContent content) {
158 ContentProvider contentProvider = content.getProvider();
159 modifiedProviders.add(contentProvider);
160 }
161
162 @Override
163 public UUID getUuid() {
164 return uuid;
165 }
166
167 @Override
168 public Content getSessionRunDir() {
169 if (sessionRunDir == null) {
170 String runDirPath = CmsContentRepository.RUN_BASE + '/' + uuid.toString();
171 if (exists(runDirPath))
172 sessionRunDir = get(runDirPath);
173 else {
174 Content runDir = get(CmsContentRepository.RUN_BASE);
175 // TODO deal with no run dir available?
176 sessionRunDir = runDir.add(uuid.toString(), CrName.collection.qName());
177 }
178 }
179 return sessionRunDir;
180 }
181
182 // @Override
183 // public String findNamespace(String prefix) {
184 // return prefixes.get(prefix);
185 // }
186 //
187 // @Override
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());
191 //
192 // return res;
193 // }
194 //
195 // @Override
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);
200 // }
201
202 }