]> git.argeo.org Git - lgpl/argeo-commons.git/blob - fs/JcrBasicfileAttributes.java
Prepare next development cycle
[lgpl/argeo-commons.git] / fs / JcrBasicfileAttributes.java
1 package org.argeo.jcr.fs;
2
3 import static javax.jcr.Property.JCR_CREATED;
4 import static javax.jcr.Property.JCR_LAST_MODIFIED;
5
6 import java.nio.file.attribute.FileTime;
7 import java.time.Instant;
8
9 import javax.jcr.Binary;
10 import javax.jcr.Node;
11 import javax.jcr.Property;
12 import javax.jcr.RepositoryException;
13 import javax.jcr.nodetype.NodeType;
14
15 import org.argeo.jcr.JcrUtils;
16
17 public class JcrBasicfileAttributes implements NodeFileAttributes {
18 private final Node node;
19
20 private final static FileTime EPOCH = FileTime.fromMillis(0);
21
22 public JcrBasicfileAttributes(Node node) {
23 if (node == null)
24 throw new JcrFsException("Node underlying the attributes cannot be null");
25 this.node = node;
26 }
27
28 @Override
29 public FileTime lastModifiedTime() {
30 try {
31 if (node.hasProperty(JCR_LAST_MODIFIED)) {
32 Instant instant = node.getProperty(JCR_LAST_MODIFIED).getDate().toInstant();
33 return FileTime.from(instant);
34 } else if (node.hasProperty(JCR_CREATED)) {
35 Instant instant = node.getProperty(JCR_CREATED).getDate().toInstant();
36 return FileTime.from(instant);
37 }
38 // if (node.isNodeType(NodeType.MIX_LAST_MODIFIED)) {
39 // Instant instant = node.getProperty(Property.JCR_LAST_MODIFIED).getDate().toInstant();
40 // return FileTime.from(instant);
41 // }
42 return EPOCH;
43 } catch (RepositoryException e) {
44 throw new JcrFsException("Cannot get last modified time", e);
45 }
46 }
47
48 @Override
49 public FileTime lastAccessTime() {
50 return lastModifiedTime();
51 }
52
53 @Override
54 public FileTime creationTime() {
55 try {
56 if (node.hasProperty(JCR_CREATED)) {
57 Instant instant = node.getProperty(JCR_CREATED).getDate().toInstant();
58 return FileTime.from(instant);
59 } else if (node.hasProperty(JCR_LAST_MODIFIED)) {
60 Instant instant = node.getProperty(JCR_LAST_MODIFIED).getDate().toInstant();
61 return FileTime.from(instant);
62 }
63 // if (node.isNodeType(NodeType.MIX_CREATED)) {
64 // Instant instant = node.getProperty(JCR_CREATED).getDate().toInstant();
65 // return FileTime.from(instant);
66 // }
67 return EPOCH;
68 } catch (RepositoryException e) {
69 throw new JcrFsException("Cannot get creation time", e);
70 }
71 }
72
73 @Override
74 public boolean isRegularFile() {
75 try {
76 return node.isNodeType(NodeType.NT_FILE);
77 } catch (RepositoryException e) {
78 throw new JcrFsException("Cannot check if regular file", e);
79 }
80 }
81
82 @Override
83 public boolean isDirectory() {
84 try {
85 if (node.isNodeType(NodeType.NT_FOLDER))
86 return true;
87 // all other non file nodes
88 return !(node.isNodeType(NodeType.NT_FILE) || node.isNodeType(NodeType.NT_LINKED_FILE));
89 } catch (RepositoryException e) {
90 throw new JcrFsException("Cannot check if directory", e);
91 }
92 }
93
94 @Override
95 public boolean isSymbolicLink() {
96 try {
97 return node.isNodeType(NodeType.NT_LINKED_FILE);
98 } catch (RepositoryException e) {
99 throw new JcrFsException("Cannot check if linked file", e);
100 }
101 }
102
103 @Override
104 public boolean isOther() {
105 return !(isDirectory() || isRegularFile() || isSymbolicLink());
106 }
107
108 @Override
109 public long size() {
110 if (isRegularFile()) {
111 Binary binary = null;
112 try {
113 binary = node.getNode(Property.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary();
114 return binary.getSize();
115 } catch (RepositoryException e) {
116 throw new JcrFsException("Cannot check size", e);
117 } finally {
118 JcrUtils.closeQuietly(binary);
119 }
120 }
121 return -1;
122 }
123
124 @Override
125 public Object fileKey() {
126 try {
127 return node.getIdentifier();
128 } catch (RepositoryException e) {
129 throw new JcrFsException("Cannot get identifier", e);
130 }
131 }
132
133 @Override
134 public Node getNode() {
135 return node;
136 }
137
138 }