Skip to content

Commit

Permalink
Refactor: RepositoryAccess facade
Browse files Browse the repository at this point in the history
  • Loading branch information
petrpodsed committed Jan 1, 2025
1 parent 9e33f7c commit f8109ce
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,16 @@
import cz.incad.kramerius.fedora.ProcessingIndexAccess;
import cz.incad.kramerius.fedora.RepositoryAccess;
import cz.incad.kramerius.fedora.DatastreamAccess;
import cz.incad.kramerius.fedora.impl.tmp.ContentFormat;
import cz.incad.kramerius.fedora.impl.tmp.ResultMapper;
import cz.incad.kramerius.fedora.impl.tmp.ProcessingIndexQueryParameters;
import cz.incad.kramerius.fedora.impl.tmp.UnsupportedContentFormatException;
import cz.incad.kramerius.fedora.impl.tmp.*;
import cz.incad.kramerius.fedora.om.repository.AkubraRepository;
import cz.incad.kramerius.fedora.om.repository.RepositoryDatastream;
import cz.incad.kramerius.fedora.om.repository.RepositoryException;
import cz.incad.kramerius.fedora.om.repository.RepositoryObject;
import cz.incad.kramerius.fedora.om.repository.impl.AkubraDOManager;
import cz.incad.kramerius.fedora.om.repository.impl.AkubraRepositoryImpl;
import cz.incad.kramerius.fedora.utils.AkubraUtils;
import cz.incad.kramerius.fedora.om.resourceindex.ProcessingIndexFeeder;
import cz.incad.kramerius.statistics.accesslogs.AggregatedAccessLogs;
import cz.incad.kramerius.utils.Dom4jUtils;
import cz.incad.kramerius.utils.StringUtils;
import cz.incad.kramerius.utils.java.Pair;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrDocument;
import org.dom4j.Attribute;
import org.dom4j.QName;
import org.ehcache.CacheManager;
Expand All @@ -40,7 +32,6 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.logging.Level;
Expand Down Expand Up @@ -68,6 +59,7 @@ public RepositoryAccessImpl(ProcessingIndexFeeder feeder, @Nullable AggregatedAc
}

//-------- Object ------------------------------------------
// TODO result
@Override
public boolean isObjectAvailable(String pid) throws IOException {
try {
Expand All @@ -76,26 +68,16 @@ public boolean isObjectAvailable(String pid) throws IOException {
throw new IOException(e);
}
}
public <T> T getFoxml(String pid, KnownDatastreams dsId, Class<T> returnType) {
// Determine supported formats for the content
ContentFormat supportedFormat = determineSupportedFormat(id);
// Validate the requested format
if ((contentType == String.class && !supportedFormat.supportsString()) ||
(contentType == InputStream.class && !supportedFormat.supportsStream()) ||
(contentType == Document.class && !supportedFormat.supportsXml())) {
throw new UnsupportedContentFormatException("Format not supported for content ID: " + id);
}
public RepositoryObjectWrapper getFoxml(String pid) {
SupportedFormats supportedFormat = determineSupportedFormat(pid);
// Retrieve content as bytes
byte[] rawContent = fetchContentFromStorage(id);
// Convert content to the requested format
if (contentType == String.class) {
return contentType.cast(new String(rawContent, StandardCharsets.UTF_8));
} else if (contentType == InputStream.class) {
return contentType.cast(new ByteArrayInputStream(rawContent));
} else if (contentType == Document.class) {
return contentType.cast(parseXml(rawContent));
RepositoryObject rawContent = null;
try {
rawContent = fetchContentFromStorage(pid);
return new RepositoryObjectWrapper(rawContent, supportedFormat);
} catch (RepositoryException e) {
throw new RuntimeException(e);
}
throw new IllegalArgumentException("Unsupported content type: " + contentType);
}
public <T> T getProperty(String pid, String propertyName, Class<T> returnType) {
org.dom4j.Document objectFoxml = getFoxml(pid);
Expand Down Expand Up @@ -131,52 +113,34 @@ public boolean isDatastreamAvailable(String pid, KnownDatastreams dsId) {
boolean exists = this.repositoryApi.datastreamExists(pid, dsId);
return exists;
}
public <T> T getDatastreamFoxml(String pid, KnownDatastreams dsId, Class<T> returnType) {
// Determine supported formats for the content
ContentFormat supportedFormat = determineSupportedFormat(id);
// Validate the requested format
if ((contentType == String.class && !supportedFormat.supportsString()) ||
(contentType == InputStream.class && !supportedFormat.supportsStream()) ||
(contentType == Document.class && !supportedFormat.supportsXml())) {
throw new UnsupportedContentFormatException("Format not supported for content ID: " + id);
}
// Retrieve content as bytes
byte[] rawContent = fetchContentFromStorage(id);
// Convert content to the requested format
if (contentType == String.class) {
return contentType.cast(new String(rawContent, StandardCharsets.UTF_8));
} else if (contentType == InputStream.class) {
return contentType.cast(new ByteArrayInputStream(rawContent));
} else if (contentType == Document.class) {
return contentType.cast(parseXml(rawContent));
}
throw new IllegalArgumentException("Unsupported content type: " + contentType);
// TODO archive format.. kdo to pouziva a proc
// TODO versions - nepouziva se, ale je treba zkontrolovat, ze se bere urcite posledni verze
public <T> T getDatastreamFoxmlElement(String pid, KnownDatastreams dsId) {
return null;
}
public <T> T getDatastreamProperty(String pid, KnownDatastreams dsId, String propertyName, Class<T> returnType) {

public <T> T getDatastreamProperty(String pid, KnownDatastreams dsId, String propertyName, Class<T> returnType) {
org.dom4j.Document objectFoxml = getFoxml(pid);
return objectFoxml == null ? null : extractProperty(objectFoxml, propertyName);
}
public <T> T getDatastreamContent(String pid, KnownDatastreams dsId, Class<T> returnType) {
// Determine supported formats for the content
ContentFormat supportedFormat = determineSupportedFormat(id);
// Validate the requested format
if ((contentType == String.class && !supportedFormat.supportsString()) ||
(contentType == InputStream.class && !supportedFormat.supportsStream()) ||
(contentType == Document.class && !supportedFormat.supportsXml())) {
throw new UnsupportedContentFormatException("Format not supported for content ID: " + id);
}
// TODO nazev, Triplet, Tuple, ????????
public <T> T getRDFSimpleProperty(String pid, String propertyName, Class<T> returnType) {
org.dom4j.Document objectFoxml = getFoxml(pid);
return objectFoxml == null ? null : extractProperty(objectFoxml, propertyName);
}

public DatastreamContentWrapper getDatastreamContent(String pid, KnownDatastreams dsId) {
SupportedFormats supportedFormat = determineSupportedFormat(dsId);
// Retrieve content as bytes
byte[] rawContent = fetchContentFromStorage(id);
// Convert content to the requested format
if (contentType == String.class) {
return contentType.cast(new String(rawContent, StandardCharsets.UTF_8));
} else if (contentType == InputStream.class) {
return contentType.cast(new ByteArrayInputStream(rawContent));
} else if (contentType == Document.class) {
return contentType.cast(parseXml(rawContent));
RepositoryDatastream rawContent = null;
try {
rawContent = fetchContentFromStorage(pid, dsId);
return new DatastreamContentWrapper(rawContent, supportedFormat);
} catch (RepositoryException e) {
throw new RuntimeException(e);
}
throw new IllegalArgumentException("Unsupported content type: " + contentType);
}

public List<String> getDatastreamNames(String pid) {
Lock readLock = AkubraDOManager.getReadLock(pid);
try {
Expand Down Expand Up @@ -314,17 +278,24 @@ private void reportAccess(String pid, String streamName) {
LOGGER.log(Level.WARNING, "Can't write statistic records for " + pid + ", stream name: " + streamName, e);
}
}
private ContentFormat determineSupportedFormat(String id) {
private SupportedFormats determineSupportedFormat(KnownDatastreams id) {
// Example logic to determine supported formats
if (id.startsWith("streamOnly")) {
return new ContentFormat(false, true, false);
if (id == KnownDatastreams.BIBLIO_DC) {
return new SupportedFormats(false, true, false);
} else {
return new ContentFormat(true, true, true);
return new SupportedFormats(true, true, true);
}
}
private byte[] fetchContentFromStorage(String id) {
private RepositoryDatastream fetchContentFromStorage(String pid, KnownDatastreams dsId) throws RepositoryException {
RepositoryObject object = akubraRepositoryImpl.getObject(pid);
if (object.streamExists(dsId)) {
RepositoryDatastream stream = object.getStream(dsId);
return stream;
} else {
return null;
}
// Mock: Fetch content as bytes from your storage
return ("<xml>Content for ID: " + id + "</xml>").getBytes(StandardCharsets.UTF_8);
//return ("<xml>Content for ID: " + id + "</xml>").getBytes(StandardCharsets.UTF_8);
}
private Document parseXml(byte[] content) throws IOException {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cz.incad.kramerius.fedora.impl.tmp;

import cz.incad.kramerius.fedora.om.repository.RepositoryDatastream;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class DatastreamContentWrapper {
private final RepositoryDatastream content;
private final SupportedFormats supportedFormat;

public DatastreamContentWrapper(RepositoryDatastream content, SupportedFormats supportedFormat) {
this.content = content;
this.supportedFormat = supportedFormat;
}

public String asString() throws UnsupportedContentFormatException {
if (!supportedFormat.supportsString()) {
throw new UnsupportedContentFormatException("String format not supported.");
}
return new String(content, StandardCharsets.UTF_8);
}

public InputStream asStream() throws UnsupportedContentFormatException {
if (!supportedFormat.supportsStream()) {
throw new UnsupportedContentFormatException("InputStream format not supported.");
}
return new ByteArrayInputStream(content);
}

public Document asXml() throws UnsupportedContentFormatException {
if (!supportedFormat.supportsXml()) {
throw new UnsupportedContentFormatException("XML format not supported.");
}
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
return factory.newDocumentBuilder().parse(new ByteArrayInputStream(content));
} catch (Exception e) {
throw new IOException("Failed to parse XML", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cz.incad.kramerius.fedora.impl.tmp;

import com.qbizm.kramerius.imp.jaxb.DigitalObject;
import cz.incad.kramerius.fedora.om.repository.RepositoryObject;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class RepositoryObjectWrapper {
private final RepositoryObject content;
private final SupportedFormats supportedFormat;

public RepositoryObjectWrapper(RepositoryObject content, SupportedFormats supportedFormat) {
this.content = content;
this.supportedFormat = supportedFormat;
}

public String asString() throws UnsupportedContentFormatException {
if (!supportedFormat.supportsString()) {
throw new UnsupportedContentFormatException("String format not supported.");
}
return new String(content, StandardCharsets.UTF_8);
}

public InputStream asStream() throws UnsupportedContentFormatException {
if (!supportedFormat.supportsStream()) {
throw new UnsupportedContentFormatException("InputStream format not supported.");
}
return new ByteArrayInputStream(content);
}

public Document asXml() throws UnsupportedContentFormatException {
if (!supportedFormat.supportsXml()) {
throw new UnsupportedContentFormatException("XML format not supported.");
}
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
return factory.newDocumentBuilder().parse(new ByteArrayInputStream(content));
} catch (Exception e) {
throw new IOException("Failed to parse XML", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cz.incad.kramerius.fedora.impl.tmp;

public class ContentFormat {
public class SupportedFormats {
private final boolean supportsString;
private final boolean supportsStream;
private final boolean supportsXml;

public ContentFormat(boolean supportsString, boolean supportsStream, boolean supportsXml) {
public SupportedFormats(boolean supportsString, boolean supportsStream, boolean supportsXml) {
this.supportsString = supportsString;
this.supportsStream = supportsStream;
this.supportsXml = supportsXml;
Expand Down

0 comments on commit f8109ce

Please sign in to comment.