Skip to content

Commit f8109ce

Browse files
committed
Refactor: RepositoryAccess facade
1 parent 9e33f7c commit f8109ce

File tree

4 files changed

+139
-75
lines changed

4 files changed

+139
-75
lines changed

shared/common/src/main/java/cz/incad/kramerius/fedora/impl/RepositoryAccessImpl.java

Lines changed: 44 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,16 @@
77
import cz.incad.kramerius.fedora.ProcessingIndexAccess;
88
import cz.incad.kramerius.fedora.RepositoryAccess;
99
import cz.incad.kramerius.fedora.DatastreamAccess;
10-
import cz.incad.kramerius.fedora.impl.tmp.ContentFormat;
11-
import cz.incad.kramerius.fedora.impl.tmp.ResultMapper;
12-
import cz.incad.kramerius.fedora.impl.tmp.ProcessingIndexQueryParameters;
13-
import cz.incad.kramerius.fedora.impl.tmp.UnsupportedContentFormatException;
10+
import cz.incad.kramerius.fedora.impl.tmp.*;
1411
import cz.incad.kramerius.fedora.om.repository.AkubraRepository;
1512
import cz.incad.kramerius.fedora.om.repository.RepositoryDatastream;
1613
import cz.incad.kramerius.fedora.om.repository.RepositoryException;
1714
import cz.incad.kramerius.fedora.om.repository.RepositoryObject;
1815
import cz.incad.kramerius.fedora.om.repository.impl.AkubraDOManager;
1916
import cz.incad.kramerius.fedora.om.repository.impl.AkubraRepositoryImpl;
20-
import cz.incad.kramerius.fedora.utils.AkubraUtils;
2117
import cz.incad.kramerius.fedora.om.resourceindex.ProcessingIndexFeeder;
2218
import cz.incad.kramerius.statistics.accesslogs.AggregatedAccessLogs;
2319
import cz.incad.kramerius.utils.Dom4jUtils;
24-
import cz.incad.kramerius.utils.StringUtils;
25-
import cz.incad.kramerius.utils.java.Pair;
26-
import org.apache.solr.client.solrj.SolrServerException;
27-
import org.apache.solr.common.SolrDocument;
2820
import org.dom4j.Attribute;
2921
import org.dom4j.QName;
3022
import org.ehcache.CacheManager;
@@ -40,7 +32,6 @@
4032
import java.nio.charset.Charset;
4133
import java.nio.charset.StandardCharsets;
4234
import java.time.LocalDateTime;
43-
import java.time.format.DateTimeParseException;
4435
import java.util.*;
4536
import java.util.concurrent.locks.Lock;
4637
import java.util.logging.Level;
@@ -68,6 +59,7 @@ public RepositoryAccessImpl(ProcessingIndexFeeder feeder, @Nullable AggregatedAc
6859
}
6960

7061
//-------- Object ------------------------------------------
62+
// TODO result
7163
@Override
7264
public boolean isObjectAvailable(String pid) throws IOException {
7365
try {
@@ -76,26 +68,16 @@ public boolean isObjectAvailable(String pid) throws IOException {
7668
throw new IOException(e);
7769
}
7870
}
79-
public <T> T getFoxml(String pid, KnownDatastreams dsId, Class<T> returnType) {
80-
// Determine supported formats for the content
81-
ContentFormat supportedFormat = determineSupportedFormat(id);
82-
// Validate the requested format
83-
if ((contentType == String.class && !supportedFormat.supportsString()) ||
84-
(contentType == InputStream.class && !supportedFormat.supportsStream()) ||
85-
(contentType == Document.class && !supportedFormat.supportsXml())) {
86-
throw new UnsupportedContentFormatException("Format not supported for content ID: " + id);
87-
}
71+
public RepositoryObjectWrapper getFoxml(String pid) {
72+
SupportedFormats supportedFormat = determineSupportedFormat(pid);
8873
// Retrieve content as bytes
89-
byte[] rawContent = fetchContentFromStorage(id);
90-
// Convert content to the requested format
91-
if (contentType == String.class) {
92-
return contentType.cast(new String(rawContent, StandardCharsets.UTF_8));
93-
} else if (contentType == InputStream.class) {
94-
return contentType.cast(new ByteArrayInputStream(rawContent));
95-
} else if (contentType == Document.class) {
96-
return contentType.cast(parseXml(rawContent));
74+
RepositoryObject rawContent = null;
75+
try {
76+
rawContent = fetchContentFromStorage(pid);
77+
return new RepositoryObjectWrapper(rawContent, supportedFormat);
78+
} catch (RepositoryException e) {
79+
throw new RuntimeException(e);
9780
}
98-
throw new IllegalArgumentException("Unsupported content type: " + contentType);
9981
}
10082
public <T> T getProperty(String pid, String propertyName, Class<T> returnType) {
10183
org.dom4j.Document objectFoxml = getFoxml(pid);
@@ -131,52 +113,34 @@ public boolean isDatastreamAvailable(String pid, KnownDatastreams dsId) {
131113
boolean exists = this.repositoryApi.datastreamExists(pid, dsId);
132114
return exists;
133115
}
134-
public <T> T getDatastreamFoxml(String pid, KnownDatastreams dsId, Class<T> returnType) {
135-
// Determine supported formats for the content
136-
ContentFormat supportedFormat = determineSupportedFormat(id);
137-
// Validate the requested format
138-
if ((contentType == String.class && !supportedFormat.supportsString()) ||
139-
(contentType == InputStream.class && !supportedFormat.supportsStream()) ||
140-
(contentType == Document.class && !supportedFormat.supportsXml())) {
141-
throw new UnsupportedContentFormatException("Format not supported for content ID: " + id);
142-
}
143-
// Retrieve content as bytes
144-
byte[] rawContent = fetchContentFromStorage(id);
145-
// Convert content to the requested format
146-
if (contentType == String.class) {
147-
return contentType.cast(new String(rawContent, StandardCharsets.UTF_8));
148-
} else if (contentType == InputStream.class) {
149-
return contentType.cast(new ByteArrayInputStream(rawContent));
150-
} else if (contentType == Document.class) {
151-
return contentType.cast(parseXml(rawContent));
152-
}
153-
throw new IllegalArgumentException("Unsupported content type: " + contentType);
116+
// TODO archive format.. kdo to pouziva a proc
117+
// TODO versions - nepouziva se, ale je treba zkontrolovat, ze se bere urcite posledni verze
118+
public <T> T getDatastreamFoxmlElement(String pid, KnownDatastreams dsId) {
119+
return null;
154120
}
155-
public <T> T getDatastreamProperty(String pid, KnownDatastreams dsId, String propertyName, Class<T> returnType) {
121+
122+
public <T> T getDatastreamProperty(String pid, KnownDatastreams dsId, String propertyName, Class<T> returnType) {
156123
org.dom4j.Document objectFoxml = getFoxml(pid);
157124
return objectFoxml == null ? null : extractProperty(objectFoxml, propertyName);
158125
}
159-
public <T> T getDatastreamContent(String pid, KnownDatastreams dsId, Class<T> returnType) {
160-
// Determine supported formats for the content
161-
ContentFormat supportedFormat = determineSupportedFormat(id);
162-
// Validate the requested format
163-
if ((contentType == String.class && !supportedFormat.supportsString()) ||
164-
(contentType == InputStream.class && !supportedFormat.supportsStream()) ||
165-
(contentType == Document.class && !supportedFormat.supportsXml())) {
166-
throw new UnsupportedContentFormatException("Format not supported for content ID: " + id);
167-
}
126+
// TODO nazev, Triplet, Tuple, ????????
127+
public <T> T getRDFSimpleProperty(String pid, String propertyName, Class<T> returnType) {
128+
org.dom4j.Document objectFoxml = getFoxml(pid);
129+
return objectFoxml == null ? null : extractProperty(objectFoxml, propertyName);
130+
}
131+
132+
public DatastreamContentWrapper getDatastreamContent(String pid, KnownDatastreams dsId) {
133+
SupportedFormats supportedFormat = determineSupportedFormat(dsId);
168134
// Retrieve content as bytes
169-
byte[] rawContent = fetchContentFromStorage(id);
170-
// Convert content to the requested format
171-
if (contentType == String.class) {
172-
return contentType.cast(new String(rawContent, StandardCharsets.UTF_8));
173-
} else if (contentType == InputStream.class) {
174-
return contentType.cast(new ByteArrayInputStream(rawContent));
175-
} else if (contentType == Document.class) {
176-
return contentType.cast(parseXml(rawContent));
135+
RepositoryDatastream rawContent = null;
136+
try {
137+
rawContent = fetchContentFromStorage(pid, dsId);
138+
return new DatastreamContentWrapper(rawContent, supportedFormat);
139+
} catch (RepositoryException e) {
140+
throw new RuntimeException(e);
177141
}
178-
throw new IllegalArgumentException("Unsupported content type: " + contentType);
179142
}
143+
180144
public List<String> getDatastreamNames(String pid) {
181145
Lock readLock = AkubraDOManager.getReadLock(pid);
182146
try {
@@ -314,17 +278,24 @@ private void reportAccess(String pid, String streamName) {
314278
LOGGER.log(Level.WARNING, "Can't write statistic records for " + pid + ", stream name: " + streamName, e);
315279
}
316280
}
317-
private ContentFormat determineSupportedFormat(String id) {
281+
private SupportedFormats determineSupportedFormat(KnownDatastreams id) {
318282
// Example logic to determine supported formats
319-
if (id.startsWith("streamOnly")) {
320-
return new ContentFormat(false, true, false);
283+
if (id == KnownDatastreams.BIBLIO_DC) {
284+
return new SupportedFormats(false, true, false);
321285
} else {
322-
return new ContentFormat(true, true, true);
286+
return new SupportedFormats(true, true, true);
323287
}
324288
}
325-
private byte[] fetchContentFromStorage(String id) {
289+
private RepositoryDatastream fetchContentFromStorage(String pid, KnownDatastreams dsId) throws RepositoryException {
290+
RepositoryObject object = akubraRepositoryImpl.getObject(pid);
291+
if (object.streamExists(dsId)) {
292+
RepositoryDatastream stream = object.getStream(dsId);
293+
return stream;
294+
} else {
295+
return null;
296+
}
326297
// Mock: Fetch content as bytes from your storage
327-
return ("<xml>Content for ID: " + id + "</xml>").getBytes(StandardCharsets.UTF_8);
298+
//return ("<xml>Content for ID: " + id + "</xml>").getBytes(StandardCharsets.UTF_8);
328299
}
329300
private Document parseXml(byte[] content) throws IOException {
330301
try {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cz.incad.kramerius.fedora.impl.tmp;
2+
3+
import cz.incad.kramerius.fedora.om.repository.RepositoryDatastream;
4+
import org.w3c.dom.Document;
5+
6+
import javax.xml.parsers.DocumentBuilderFactory;
7+
import java.io.ByteArrayInputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.nio.charset.StandardCharsets;
11+
12+
public class DatastreamContentWrapper {
13+
private final RepositoryDatastream content;
14+
private final SupportedFormats supportedFormat;
15+
16+
public DatastreamContentWrapper(RepositoryDatastream content, SupportedFormats supportedFormat) {
17+
this.content = content;
18+
this.supportedFormat = supportedFormat;
19+
}
20+
21+
public String asString() throws UnsupportedContentFormatException {
22+
if (!supportedFormat.supportsString()) {
23+
throw new UnsupportedContentFormatException("String format not supported.");
24+
}
25+
return new String(content, StandardCharsets.UTF_8);
26+
}
27+
28+
public InputStream asStream() throws UnsupportedContentFormatException {
29+
if (!supportedFormat.supportsStream()) {
30+
throw new UnsupportedContentFormatException("InputStream format not supported.");
31+
}
32+
return new ByteArrayInputStream(content);
33+
}
34+
35+
public Document asXml() throws UnsupportedContentFormatException {
36+
if (!supportedFormat.supportsXml()) {
37+
throw new UnsupportedContentFormatException("XML format not supported.");
38+
}
39+
try {
40+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
41+
return factory.newDocumentBuilder().parse(new ByteArrayInputStream(content));
42+
} catch (Exception e) {
43+
throw new IOException("Failed to parse XML", e);
44+
}
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cz.incad.kramerius.fedora.impl.tmp;
2+
3+
import com.qbizm.kramerius.imp.jaxb.DigitalObject;
4+
import cz.incad.kramerius.fedora.om.repository.RepositoryObject;
5+
import org.w3c.dom.Document;
6+
7+
import javax.xml.parsers.DocumentBuilderFactory;
8+
import java.io.ByteArrayInputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.nio.charset.StandardCharsets;
12+
13+
public class RepositoryObjectWrapper {
14+
private final RepositoryObject content;
15+
private final SupportedFormats supportedFormat;
16+
17+
public RepositoryObjectWrapper(RepositoryObject content, SupportedFormats supportedFormat) {
18+
this.content = content;
19+
this.supportedFormat = supportedFormat;
20+
}
21+
22+
public String asString() throws UnsupportedContentFormatException {
23+
if (!supportedFormat.supportsString()) {
24+
throw new UnsupportedContentFormatException("String format not supported.");
25+
}
26+
return new String(content, StandardCharsets.UTF_8);
27+
}
28+
29+
public InputStream asStream() throws UnsupportedContentFormatException {
30+
if (!supportedFormat.supportsStream()) {
31+
throw new UnsupportedContentFormatException("InputStream format not supported.");
32+
}
33+
return new ByteArrayInputStream(content);
34+
}
35+
36+
public Document asXml() throws UnsupportedContentFormatException {
37+
if (!supportedFormat.supportsXml()) {
38+
throw new UnsupportedContentFormatException("XML format not supported.");
39+
}
40+
try {
41+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
42+
return factory.newDocumentBuilder().parse(new ByteArrayInputStream(content));
43+
} catch (Exception e) {
44+
throw new IOException("Failed to parse XML", e);
45+
}
46+
}
47+
}

shared/common/src/main/java/cz/incad/kramerius/fedora/impl/tmp/ContentFormat.java renamed to shared/common/src/main/java/cz/incad/kramerius/fedora/impl/tmp/SupportedFormats.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package cz.incad.kramerius.fedora.impl.tmp;
22

3-
public class ContentFormat {
3+
public class SupportedFormats {
44
private final boolean supportsString;
55
private final boolean supportsStream;
66
private final boolean supportsXml;
77

8-
public ContentFormat(boolean supportsString, boolean supportsStream, boolean supportsXml) {
8+
public SupportedFormats(boolean supportsString, boolean supportsStream, boolean supportsXml) {
99
this.supportsString = supportsString;
1010
this.supportsStream = supportsStream;
1111
this.supportsXml = supportsXml;

0 commit comments

Comments
 (0)