]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.jcr/src/org/argeo/jcr/fs/JcrBasicfileAttributes.java
Remove unnecessary dependency to JTA.
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / fs / JcrBasicfileAttributes.java
index e59abbc70a8a1fc1f65556c4208b7946eb2c5b96..7c9711bf06ee0739019fe43b53f53218e7f9ca93 100644 (file)
@@ -1,5 +1,8 @@
 package org.argeo.jcr.fs;
 
+import static javax.jcr.Property.JCR_CREATED;
+import static javax.jcr.Property.JCR_LAST_MODIFIED;
+
 import java.nio.file.attribute.FileTime;
 import java.time.Instant;
 
@@ -14,19 +17,28 @@ import org.argeo.jcr.JcrUtils;
 public class JcrBasicfileAttributes implements NodeFileAttributes {
        private final Node node;
 
-       private FileTime EPOCH = FileTime.fromMillis(0);
+       private final static FileTime EPOCH = FileTime.fromMillis(0);
 
        public JcrBasicfileAttributes(Node node) {
+               if (node == null)
+                       throw new JcrFsException("Node underlying the attributes cannot be null");
                this.node = node;
        }
 
        @Override
        public FileTime lastModifiedTime() {
                try {
-                       if (node.isNodeType(NodeType.MIX_LAST_MODIFIED)) {
-                               Instant instant = node.getProperty(Property.JCR_LAST_MODIFIED).getDate().toInstant();
+                       if (node.hasProperty(JCR_LAST_MODIFIED)) {
+                               Instant instant = node.getProperty(JCR_LAST_MODIFIED).getDate().toInstant();
+                               return FileTime.from(instant);
+                       } else if (node.hasProperty(JCR_CREATED)) {
+                               Instant instant = node.getProperty(JCR_CREATED).getDate().toInstant();
                                return FileTime.from(instant);
                        }
+//                     if (node.isNodeType(NodeType.MIX_LAST_MODIFIED)) {
+//                             Instant instant = node.getProperty(Property.JCR_LAST_MODIFIED).getDate().toInstant();
+//                             return FileTime.from(instant);
+//                     }
                        return EPOCH;
                } catch (RepositoryException e) {
                        throw new JcrFsException("Cannot get last modified time", e);
@@ -41,10 +53,17 @@ public class JcrBasicfileAttributes implements NodeFileAttributes {
        @Override
        public FileTime creationTime() {
                try {
-                       if (node.isNodeType(NodeType.MIX_CREATED)) {
-                               Instant instant = node.getProperty(Property.JCR_CREATED).getDate().toInstant();
+                       if (node.hasProperty(JCR_CREATED)) {
+                               Instant instant = node.getProperty(JCR_CREATED).getDate().toInstant();
+                               return FileTime.from(instant);
+                       } else if (node.hasProperty(JCR_LAST_MODIFIED)) {
+                               Instant instant = node.getProperty(JCR_LAST_MODIFIED).getDate().toInstant();
                                return FileTime.from(instant);
                        }
+//                     if (node.isNodeType(NodeType.MIX_CREATED)) {
+//                             Instant instant = node.getProperty(JCR_CREATED).getDate().toInstant();
+//                             return FileTime.from(instant);
+//                     }
                        return EPOCH;
                } catch (RepositoryException e) {
                        throw new JcrFsException("Cannot get creation time", e);
@@ -63,7 +82,10 @@ public class JcrBasicfileAttributes implements NodeFileAttributes {
        @Override
        public boolean isDirectory() {
                try {
-                       return node.isNodeType(NodeType.NT_FOLDER);
+                       if (node.isNodeType(NodeType.NT_FOLDER))
+                               return true;
+                       // all other non file nodes
+                       return !(node.isNodeType(NodeType.NT_FILE) || node.isNodeType(NodeType.NT_LINKED_FILE));
                } catch (RepositoryException e) {
                        throw new JcrFsException("Cannot check if directory", e);
                }
@@ -88,7 +110,7 @@ public class JcrBasicfileAttributes implements NodeFileAttributes {
                if (isRegularFile()) {
                        Binary binary = null;
                        try {
-                               binary = node.getNode(Property.JCR_DATA).getProperty(Property.JCR_CONTENT).getBinary();
+                               binary = node.getNode(Property.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary();
                                return binary.getSize();
                        } catch (RepositoryException e) {
                                throw new JcrFsException("Cannot check size", e);
@@ -101,7 +123,11 @@ public class JcrBasicfileAttributes implements NodeFileAttributes {
 
        @Override
        public Object fileKey() {
-               return null;
+               try {
+                       return node.getIdentifier();
+               } catch (RepositoryException e) {
+                       throw new JcrFsException("Cannot get identifier", e);
+               }
        }
 
        @Override