1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.datamodel.ifs2;
20
21 import java.io.IOException;
22 import java.io.UncheckedIOException;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.attribute.BasicFileAttributes;
26 import java.util.stream.Stream;
27
28 import org.jdom2.Element;
29
30
31
32
33
34
35
36 public class MCRDirectory extends MCRStoredNode {
37
38
39
40
41
42
43
44
45
46 protected MCRDirectory(MCRDirectory parent, Path fo, Element data) {
47 super(parent, fo, data);
48 }
49
50
51
52
53
54
55
56
57
58 protected MCRDirectory(MCRDirectory parent, String name) throws IOException {
59 super(parent, name, "dir");
60 Files.createDirectory(path);
61 getRoot().saveAdditionalData();
62 }
63
64
65
66
67
68
69
70 public MCRDirectory createDir(String name) throws IOException {
71 return new MCRDirectory(this, name);
72 }
73
74
75
76
77
78
79
80 public MCRFile createFile(String name) throws IOException {
81 return new MCRFile(this, name);
82 }
83
84 @SuppressWarnings("unchecked")
85 private Element getChildData(String name) {
86 for (Element child : readData(Element::getChildren)) {
87 if (name.equals(child.getAttributeValue("name"))) {
88 return child;
89 }
90 }
91
92 Element childData = new Element("node");
93 childData.setAttribute("name", name);
94 writeData(e -> e.addContent(childData));
95 return childData;
96 }
97
98
99
100
101
102
103
104
105 @Override
106 protected MCRStoredNode buildChildNode(Path fo) {
107 if (fo == null) {
108 return null;
109 }
110 if (!this.path.equals(fo.getParent())) {
111 throw new IllegalArgumentException(fo + " is not a direct child of " + this.path + ".");
112 }
113 BasicFileAttributes attrs;
114 try {
115 attrs = Files.readAttributes(fo, BasicFileAttributes.class);
116 } catch (IOException e) {
117
118 return null;
119 }
120
121 Element childData = getChildData(fo.getFileName().toString());
122 if (attrs.isRegularFile()) {
123 return new MCRFile(this, fo, childData);
124 } else {
125 return new MCRDirectory(this, fo, childData);
126 }
127 }
128
129
130
131
132 @Override
133 @SuppressWarnings("unchecked")
134 void repairMetadata() throws IOException {
135 writeData(e -> {
136 e.setName("dir");
137 e.setAttribute("name", getName());
138
139 for (Element childEntry : e.getChildren()) {
140 childEntry.setName("node");
141 }
142 });
143
144 try (Stream<MCRNode> streamMCRNode = getChildren()) {
145 streamMCRNode.filter(MCRStoredNode.class::isInstance)
146 .map(MCRStoredNode.class::cast)
147 .sequential()
148 .forEach(mcrStoredNode -> {
149 try {
150 mcrStoredNode.repairMetadata();
151 } catch (IOException e) {
152 throw new UncheckedIOException(e);
153 }
154 });
155 } catch (UncheckedIOException e) {
156 throw e.getCause();
157 }
158
159 writeData(e -> e.removeChildren("node"));
160 }
161 }