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.nio.file.Files;
23 import java.nio.file.Path;
24 import java.util.Optional;
25
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.jdom2.JDOMException;
29 import org.mycore.common.MCRException;
30 import org.mycore.common.config.MCRConfiguration2;
31 import org.mycore.common.content.MCRContent;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class MCRMetadataStore extends MCRStore {
47
48 public static final Logger LOGGER = LogManager.getLogger();
49
50
51
52
53
54
55
56 protected boolean forceXML = true;
57
58 protected String forceDocType;
59
60
61
62
63
64
65
66 @Override
67 protected void init(String type) {
68 super.init(type);
69 prefix = MCRConfiguration2.getString("MCR.IFS2.Store." + type + ".Prefix").orElse(type + "_");
70 suffix = ".xml";
71 forceXML = MCRConfiguration2.getBoolean("MCR.IFS2.Store." + type + ".ForceXML").orElse(true);
72 if (forceXML) {
73 forceDocType = MCRConfiguration2.getString("MCR.IFS2.Store." + type + ".ForceDocType").orElse(null);
74 LOGGER.debug("Set doctype for {} to {}", type, forceDocType);
75 }
76 }
77
78
79
80
81
82
83
84 @Override
85 protected void init(MCRStoreConfig config) {
86 super.init(config);
87 prefix = Optional.ofNullable(config.getPrefix()).orElseGet(() -> config.getID() + "_");
88 suffix = ".xml";
89 forceXML = MCRConfiguration2.getBoolean("MCR.IFS2.Store." + config.getID() + ".ForceXML").orElse(true);
90 if (forceXML) {
91 forceDocType = MCRConfiguration2.getString("MCR.IFS2.Store." + config.getID() + ".ForceDocType")
92 .orElse(null);
93 LOGGER.debug("Set doctype for {} to {}", config.getID(), forceDocType);
94 }
95 }
96
97 protected boolean shouldForceXML() {
98 return forceXML;
99 }
100
101
102
103
104
105
106
107
108 public MCRStoredMetadata create(MCRContent xml) throws IOException, JDOMException {
109 int id = getNextFreeID();
110 return create(xml, id);
111 }
112
113
114
115
116
117
118
119
120
121
122 public MCRStoredMetadata create(MCRContent xml, int id) throws IOException, JDOMException {
123 if (id <= 0) {
124 throw new MCRException("ID of metadata object must be a positive integer");
125 }
126 Path path = getSlot(id);
127 if (Files.exists(path)) {
128 String msg = "Metadata object with ID " + id + " already exists in store";
129 throw new MCRException(msg);
130 }
131 if (!Files.exists(path.getParent())) {
132 Files.createDirectories(path.getParent());
133 }
134 MCRStoredMetadata meta = buildMetadataObject(path, id);
135 meta.create(xml);
136 return meta;
137 }
138
139
140
141
142
143
144
145
146
147 public MCRStoredMetadata retrieve(int id) throws IOException {
148 Path path = getSlot(id);
149 if (!Files.exists(path)) {
150 return null;
151 } else {
152 return buildMetadataObject(path, id);
153 }
154 }
155
156
157
158
159
160
161
162
163
164 protected MCRStoredMetadata buildMetadataObject(Path fo, int id) {
165 return new MCRStoredMetadata(this, fo, id, forceDocType);
166 }
167 }