Clarify ACR API
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / acr / fs / FsContent.java
index f0c7338579c5f67f319c5122c06a2cebc072948e..de21d32a60da859f41f44396e1c0738dd41ffff2 100644 (file)
@@ -36,18 +36,18 @@ import org.argeo.api.acr.ContentName;
 import org.argeo.api.acr.ContentResourceException;
 import org.argeo.api.acr.CrAttributeType;
 import org.argeo.api.acr.CrName;
+import org.argeo.api.acr.DName;
 import org.argeo.api.acr.NamespaceUtils;
 import org.argeo.api.acr.spi.ContentProvider;
 import org.argeo.api.acr.spi.ProvidedContent;
 import org.argeo.api.acr.spi.ProvidedSession;
-import org.argeo.api.cms.CmsLog;
 import org.argeo.cms.acr.AbstractContent;
-import org.argeo.cms.acr.ContentUtils;
-import org.argeo.util.FsUtils;
+import org.argeo.cms.acr.CmsContent;
+import org.argeo.cms.util.FsUtils;
 
 /** Content persisted as a filesystem {@link Path}. */
 public class FsContent extends AbstractContent implements ProvidedContent {
-       private CmsLog log = CmsLog.getLog(FsContent.class);
+//     private CmsLog log = CmsLog.getLog(FsContent.class);
 
        final static String USER_ = "user:";
 
@@ -55,14 +55,15 @@ public class FsContent extends AbstractContent implements ProvidedContent {
        private static final Map<QName, String> POSIX_KEYS;
        static {
                BASIC_KEYS = new HashMap<>();
-               BASIC_KEYS.put(CrName.creationTime.qName(), "basic:creationTime");
-               BASIC_KEYS.put(CrName.lastModifiedTime.qName(), "basic:lastModifiedTime");
-               BASIC_KEYS.put(CrName.size.qName(), "basic:size");
+               BASIC_KEYS.put(DName.creationdate.qName(), "basic:creationTime");
+               BASIC_KEYS.put(DName.getlastmodified.qName(), "basic:lastModifiedTime");
+               BASIC_KEYS.put(DName.getcontentlength.qName(), "basic:size");
+
                BASIC_KEYS.put(CrName.fileKey.qName(), "basic:fileKey");
 
                POSIX_KEYS = new HashMap<>(BASIC_KEYS);
-               POSIX_KEYS.put(CrName.owner.qName(), "owner:owner");
-               POSIX_KEYS.put(CrName.group.qName(), "posix:group");
+               POSIX_KEYS.put(DName.owner.qName(), "owner:owner");
+               POSIX_KEYS.put(DName.group.qName(), "posix:group");
                POSIX_KEYS.put(CrName.permissions.qName(), "posix:permissions");
        }
 
@@ -79,7 +80,7 @@ public class FsContent extends AbstractContent implements ProvidedContent {
                // TODO check file names with ':' ?
                if (isMountBase) {
                        String mountPath = provider.getMountPath();
-                       if (mountPath != null && !mountPath.equals(ContentUtils.ROOT_SLASH)) {
+                       if (mountPath != null && !mountPath.equals(Content.ROOT_PATH)) {
                                Content mountPoint = session.getMountPoint(mountPath);
                                this.name = mountPoint.getName();
                        } else {
@@ -88,7 +89,7 @@ public class FsContent extends AbstractContent implements ProvidedContent {
                } else {
 
                        // TODO should we support prefixed name for known types?
-                       QName providerName = NamespaceUtils.parsePrefixedName(provider, path.getFileName().toString());
+                       QName providerName = provider.fromFsPrefixedName(path.getFileName().toString());
 //                     QName providerName = new QName(path.getFileName().toString());
                        // TODO remove extension if mounted?
                        this.name = new ContentName(providerName, session);
@@ -124,7 +125,7 @@ public class FsContent extends AbstractContent implements ProvidedContent {
                        } else {
                                UserDefinedFileAttributeView udfav = Files.getFileAttributeView(path,
                                                UserDefinedFileAttributeView.class);
-                               String prefixedName = NamespaceUtils.toPrefixedName(provider, key);
+                               String prefixedName = provider.toFsPrefixedName(key);
                                if (!udfav.list().contains(prefixedName))
                                        return Optional.empty();
                                ByteBuffer buf = ByteBuffer.allocate(udfav.size(prefixedName));
@@ -158,11 +159,15 @@ public class FsContent extends AbstractContent implements ProvidedContent {
                        String[] arr = str.split("\n");
 
                        if (arr.length == 1) {
-                               if (clss.isAssignableFrom(String.class)) {
-                                       res = (A) arr[0];
-                               } else {
-                                       res = (A) CrAttributeType.parse(arr[0]);
-                               }
+//                             if (clss.isAssignableFrom(String.class)) {
+//                                     res = (A) arr[0];
+//                             } else {
+//                                     res = (A) CrAttributeType.parse(arr[0]);
+//                             }
+//                             if (isDefaultAttrTypeRequested(clss))
+//                                     return Optional.of((A) CrAttributeType.parse(str));
+                               return CrAttributeType.cast(clss, str);
+
                        } else {
                                List<Object> lst = new ArrayList<>();
                                for (String s : arr) {
@@ -172,14 +177,15 @@ public class FsContent extends AbstractContent implements ProvidedContent {
                        }
                }
                if (res == null) {
-                       if (isDefaultAttrTypeRequested(clss))
-                               return Optional.of((A) CrAttributeType.parse(value.toString()));
-                       if (clss.isAssignableFrom(value.getClass()))
-                               return Optional.of((A) value);
-                       if (clss.isAssignableFrom(String.class))
-                               return Optional.of((A) value.toString());
-                       log.warn("Cannot interpret " + key + " in " + this);
-                       return Optional.empty();
+//                     if (isDefaultAttrTypeRequested(clss))
+//                             return Optional.of((A) CrAttributeType.parse(value.toString()));
+                       return CrAttributeType.cast(clss, value);
+//                     if (clss.isAssignableFrom(value.getClass()))
+//                             return Optional.of((A) value);
+//                     if (clss.isAssignableFrom(String.class))
+//                             return Optional.of((A) value.toString());
+//                     log.warn("Cannot interpret " + key + " in " + this);
+//                     return Optional.empty();
 //                     try {
 //                             res = (A) value;
 //                     } catch (ClassCastException e) {
@@ -196,7 +202,7 @@ public class FsContent extends AbstractContent implements ProvidedContent {
                if (udfav != null) {
                        try {
                                for (String name : udfav.list()) {
-                                       QName providerName = NamespaceUtils.parsePrefixedName(provider, name);
+                                       QName providerName = provider.fromFsPrefixedName(name);
                                        if (providerName.getNamespaceURI().equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI))
                                                continue; // skip prefix mapping
                                        QName sessionName = new ContentName(providerName, getSession());
@@ -213,7 +219,7 @@ public class FsContent extends AbstractContent implements ProvidedContent {
        protected void removeAttr(QName key) {
                UserDefinedFileAttributeView udfav = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
                try {
-                       udfav.delete(NamespaceUtils.toPrefixedName(provider, key));
+                       udfav.delete(provider.toFsPrefixedName(key));
                } catch (IOException e) {
                        throw new ContentResourceException("Cannot delete attribute " + key + " for " + path, e);
                }
@@ -237,7 +243,7 @@ public class FsContent extends AbstractContent implements ProvidedContent {
                UserDefinedFileAttributeView udfav = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
                ByteBuffer bb = ByteBuffer.wrap(toWrite.getBytes(StandardCharsets.UTF_8));
                try {
-                       udfav.write(NamespaceUtils.toPrefixedName(provider, key), bb);
+                       udfav.write(provider.toFsPrefixedName(key), bb);
                } catch (IOException e) {
                        throw new ContentResourceException("Cannot delete attribute " + key + " for " + path, e);
                }
@@ -248,7 +254,7 @@ public class FsContent extends AbstractContent implements ProvidedContent {
                if (POSIX_KEYS.containsKey(key))
                        return POSIX_KEYS.get(key);
                else
-                       return USER_ + NamespaceUtils.toPrefixedName(provider, key);
+                       return USER_ + provider.toFsPrefixedName(key);
        }
 
        /*
@@ -283,8 +289,8 @@ public class FsContent extends AbstractContent implements ProvidedContent {
        public Content add(QName name, QName... classes) {
                FsContent fsContent;
                try {
-                       Path newPath = path.resolve(NamespaceUtils.toPrefixedName(provider, name));
-                       if (ContentName.contains(classes, CrName.collection.qName()))
+                       Path newPath = path.resolve(provider.toFsPrefixedName(name));
+                       if (ContentName.contains(classes, DName.collection.qName()))
                                Files.createDirectory(newPath);
                        else
                                Files.createFile(newPath);
@@ -313,7 +319,11 @@ public class FsContent extends AbstractContent implements ProvidedContent {
 
        @Override
        public void remove() {
-               FsUtils.delete(path);
+               try {
+                       FsUtils.delete(path);
+               } catch (IOException e) {
+                       throw new RuntimeException("Cannot delete " + path, e);
+               }
        }
 
        @Override
@@ -322,7 +332,7 @@ public class FsContent extends AbstractContent implements ProvidedContent {
                        String mountPath = provider.getMountPath();
                        if (mountPath == null || mountPath.equals("/"))
                                return null;
-                       String[] parent = ContentUtils.getParentPath(mountPath);
+                       String[] parent = CmsContent.getParentPath(mountPath);
                        return getSession().get(parent[0]);
                }
                return new FsContent(this, path.getParent());
@@ -359,14 +369,15 @@ public class FsContent extends AbstractContent implements ProvidedContent {
 
        @Override
        public List<QName> getContentClasses() {
-               List<QName> res = new ArrayList<>();
-               List<String> value = getMultiple(CrName.cc.qName(), String.class);
-               for (String s : value) {
-                       QName name = NamespaceUtils.parsePrefixedName(provider, s);
-                       res.add(name);
-               }
+//             List<QName> res = new ArrayList<>();
+//             List<String> value = getMultiple(DName.resourcetype.qName(), String.class);
+//             for (String s : value) {
+//                     QName name = NamespaceUtils.parsePrefixedName(provider, s);
+//                     res.add(name);
+//             }
+               List<QName> res = getMultiple(DName.resourcetype.qName(), QName.class);
                if (Files.isDirectory(path))
-                       res.add(CrName.collection.qName());
+                       res.add(DName.collection.qName());
                return res;
        }
 
@@ -374,14 +385,14 @@ public class FsContent extends AbstractContent implements ProvidedContent {
        public void addContentClasses(QName... contentClass) {
                List<String> toWrite = new ArrayList<>();
                for (QName cc : getContentClasses()) {
-                       if (cc.equals(CrName.collection.qName()))
+                       if (cc.equals(DName.collection.qName()))
                                continue; // skip
                        toWrite.add(NamespaceUtils.toPrefixedName(provider, cc));
                }
                for (QName cc : contentClass) {
                        toWrite.add(NamespaceUtils.toPrefixedName(provider, cc));
                }
-               put(CrName.cc.qName(), toWrite);
+               put(DName.resourcetype.qName(), toWrite);
        }
 
        /*
@@ -398,7 +409,7 @@ public class FsContent extends AbstractContent implements ProvidedContent {
         */
        @SuppressWarnings("unchecked")
        public <A> CompletableFuture<A> write(Class<A> clss) {
-               if (isContentClass(CrName.collection.qName())) {
+               if (isContentClass(DName.collection.qName())) {
                        throw new IllegalStateException("Cannot directly write to a collection");
                }
                if (InputStream.class.isAssignableFrom(clss)) {