From 9e33f7caea410cb597c6786496acec6b87e16ce6 Mon Sep 17 00:00:00 2001 From: ppodsednik Date: Mon, 30 Dec 2024 14:59:41 +0100 Subject: [PATCH] Refactor: RepositoryAccess facade --- .../incad/kramerius/fedora/ObjectAccess.java | 4 + .../fedora/impl/DatastreamAccessImpl.java | 178 +++++++-- .../fedora/impl/ObjectAccessImpl.java | 112 ++++++ .../fedora/impl/RepositoryAccessImpl.java | 341 ++++++++---------- .../impl/tmp/TmpAbstractRepositoryAccess.java | 149 +------- 5 files changed, 418 insertions(+), 366 deletions(-) create mode 100644 shared/common/src/main/java/cz/incad/kramerius/fedora/ObjectAccess.java create mode 100644 shared/common/src/main/java/cz/incad/kramerius/fedora/impl/ObjectAccessImpl.java diff --git a/shared/common/src/main/java/cz/incad/kramerius/fedora/ObjectAccess.java b/shared/common/src/main/java/cz/incad/kramerius/fedora/ObjectAccess.java new file mode 100644 index 000000000..4e7d53672 --- /dev/null +++ b/shared/common/src/main/java/cz/incad/kramerius/fedora/ObjectAccess.java @@ -0,0 +1,4 @@ +package cz.incad.kramerius.fedora; + +public interface ObjectAccess { +} diff --git a/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/DatastreamAccessImpl.java b/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/DatastreamAccessImpl.java index 27f8cfb9a..085c8be39 100644 --- a/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/DatastreamAccessImpl.java +++ b/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/DatastreamAccessImpl.java @@ -2,8 +2,12 @@ import com.qbizm.kramerius.imp.jaxb.DatastreamVersionType; import com.qbizm.kramerius.imp.jaxb.DigitalObject; +import cz.incad.kramerius.FedoraNamespaces; +import cz.incad.kramerius.ProcessSubtreeException; +import cz.incad.kramerius.TreeNodeProcessor; import cz.incad.kramerius.fedora.RepositoryAccess; import cz.incad.kramerius.fedora.DatastreamAccess; +import cz.incad.kramerius.fedora.impl.tmp.TmpAbstractRepositoryAccess; import cz.incad.kramerius.fedora.om.repository.RepositoryDatastream; import cz.incad.kramerius.fedora.om.repository.RepositoryException; import cz.incad.kramerius.fedora.om.repository.RepositoryObject; @@ -12,13 +16,20 @@ import cz.incad.kramerius.fedora.utils.FedoraUtils; import cz.incad.kramerius.fedora.utils.pid.LexerException; import cz.incad.kramerius.repository.utils.NamespaceRemovingVisitor; +import cz.incad.kramerius.utils.XMLUtils; +import org.w3c.dom.DOMException; import org.w3c.dom.Document; +import org.w3c.dom.Element; import javax.xml.xpath.XPathExpressionException; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; import java.util.Date; +import java.util.List; +import java.util.Stack; import java.util.concurrent.locks.Lock; +import java.util.logging.Level; public class DatastreamAccessImpl implements DatastreamAccess { @Override @@ -300,6 +311,149 @@ public String getTypeOfDatastream(String pid, String dsId) throws RepositoryExce } } + @Override + public void updateRelsExt(String pid, org.dom4j.Document relsExtDoc) throws IOException, RepositoryException { + updateInlineXmlDatastream(pid, RepositoryAccess.KnownDatastreams.RELS_EXT.toString(), relsExtDoc, RepositoryAccess.KnownXmlFormatUris.RELS_EXT); + } + @Override + public void updateMods(String pid, org.dom4j.Document modsDoc) throws IOException, RepositoryException { + updateInlineXmlDatastream(pid, RepositoryAccess.KnownDatastreams.BIBLIO_MODS.toString(), modsDoc, RepositoryAccess.KnownXmlFormatUris.BIBLIO_MODS); + } + @Override + public void updateDublinCore(String pid, org.dom4j.Document dcDoc) throws IOException, RepositoryException { + updateInlineXmlDatastream(pid, RepositoryAccess.KnownDatastreams.BIBLIO_DC.toString(), dcDoc, RepositoryAccess.KnownXmlFormatUris.BIBLIO_DC); + } + + // relsExt + @Override + public String getDonator(String pid) throws IOException { + return getDonator(getRelsExt(pid)); + } + @Override + public String getKrameriusModelName(String pid) throws IOException { + return getKrameriusModelName(getRelsExt(pid)); + } + @Override + public String findFirstViewablePid(String pid) throws IOException { + final List foundPids = new ArrayList(); + try { + processSubtree(makeSureObjectPid(pid), new TreeNodeProcessor() { + boolean breakProcess = false; + int previousLevel = 0; + + @Override + public boolean breakProcessing(String pid, int level) { + return breakProcess; + } + + @Override + public boolean skipBranch(String pid, int level) { + return false; + } + + @Override + public void process(String pid, int level) throws ProcessSubtreeException { + try { + if (previousLevel < level || level == 0) { + if (TmpAbstractRepositoryAccess.this.isImageFULLAvailable(pid)) { + foundPids.add(pid); + breakProcess = true; + } + } else if (previousLevel > level) { + breakProcess = true; + } else if ((previousLevel == level) && (level != 0)) { + breakProcess = true; + } + previousLevel = level; + } catch (Exception e) { + throw new ProcessSubtreeException(e); + } + } + }); + } catch (ProcessSubtreeException e) { + throw new IOException(e); + } catch (LexerException e) { + throw new IOException(e); + } + + return foundPids.isEmpty() ? null : foundPids.get(0); + } + @Override + public void processSubtree(String pid, TreeNodeProcessor processor) throws ProcessSubtreeException, IOException { + try { + pid = makeSureObjectPid(pid); + Document relsExt = null; + try { + // should be from + if (isStreamAvailable(pid, FedoraUtils.RELS_EXT_STREAM)) { + relsExt = getRelsExt(pid); + } else { + LOGGER.warning("could not read root RELS-EXT, skipping object (" + pid + ")"); + } + } catch (Exception ex) { + LOGGER.warning("could not read root RELS-EXT, skipping object (" + pid + "):" + ex); + } + if (!processor.skipBranch(pid, 0)) { + processSubtreeInternal(pid, relsExt, processor, 0, new Stack()); + } + } catch (LexerException e) { + LOGGER.warning("Error in pid: " + pid); + throw new ProcessSubtreeException(e); + } catch (XPathExpressionException e) { + throw new ProcessSubtreeException(e); + } + } + @Override + public List getPages(String pid, boolean deep) throws IOException { + Document relsExt = getRelsExt(pid); + return getPages(pid, relsExt.getDocumentElement()); + } + @Override + public String getFirstItemPid(String pid) throws IOException { + Document relsExt = getRelsExt(pid); + return getFirstItemPid(relsExt); + } + @Override + public String getFirstVolumePid(String pid) throws IOException { + Document relsExt = getRelsExt(pid); + return getFirstVolumePid(relsExt); + } + @Override + public boolean getFirstViewablePath(List pids, List models) throws IOException { + try { + String pid = pids.get(pids.size() - 1); + pid = makeSureObjectPid(pid); + if (isImageFULLAvailable(pid)) { + return true; + } + Document relsExt = getRelsExt(pid); + Element descEl = XMLUtils.findElement(relsExt.getDocumentElement(), "Description", + FedoraNamespaces.RDF_NAMESPACE_URI); + List els = XMLUtils.getElements(descEl); + for (Element el : els) { + if (getTreePredicates().contains(el.getLocalName())) { + if (el.hasAttribute("rdf:resource")) { + pid = el.getAttributes().getNamedItem("rdf:resource").getNodeValue(); + pids.add(pid); + models.add(getKrameriusModelName(pid)); + if (getFirstViewablePath(pids, models)) { + return true; + } else { + pids.remove(pids.size() - 1); + models.remove(pids.size() - 1); + } + } + } + } + return false; + } catch (DOMException e) { + LOGGER.log(Level.SEVERE, e.getMessage(), e); + throw new IOException(e); + } catch (LexerException e) { + LOGGER.log(Level.SEVERE, e.getMessage(), e); + throw new IOException(e); + } + } /* // TODO current stream access methods, just one method rendering 2 content types; also we can add new par dsId and if null the whole foxml will be returned @@ -322,30 +476,6 @@ public org.dom4j.Document getDatastreamXml(String pid, String dsId) throws Repos readLock.unlock(); } } - @Override - public InputStream getFoxml(String pid, boolean archive) throws IOException { - try { - if (archive){ - DigitalObject obj = manager.readObjectCloneFromStorage(pid); - manager.resolveArchivedDatastreams(obj); - return this.manager.marshallObject(obj); - }else { - return this.manager.retrieveObject(pid); - } - } catch (Exception e) { - throw new IOException(e); - } - } - @Override - public org.dom4j.Document getFoxml(String pid) throws RepositoryException, IOException { - Lock readLock = AkubraDOManager.getReadLock(pid); - try { - RepositoryObject object = akubraRepositoryImpl.getObject(pid); - return Utils.inputstreamToDocument(object.getFoxml(), true); - } finally { - readLock.unlock(); - } - } // <--- AkubraObject.getStream.getContent (6x) public InputStream getLatestVersionOfDatastream(String pid, String dsId) throws RepositoryException, IOException { diff --git a/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/ObjectAccessImpl.java b/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/ObjectAccessImpl.java new file mode 100644 index 000000000..fbd9dc8da --- /dev/null +++ b/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/ObjectAccessImpl.java @@ -0,0 +1,112 @@ +package cz.incad.kramerius.fedora.impl; + +import com.qbizm.kramerius.imp.jaxb.DigitalObject; +import cz.incad.kramerius.fedora.ObjectAccess; +import cz.incad.kramerius.fedora.om.repository.RepositoryException; +import cz.incad.kramerius.fedora.utils.AkubraUtils; +import cz.incad.kramerius.utils.Dom4jUtils; +import org.apache.solr.client.solrj.SolrServerException; + +import java.io.IOException; +import java.time.LocalDateTime; +import java.time.format.DateTimeParseException; +import java.util.Date; +import java.util.Map; + +public class ObjectAccessImpl implements ObjectAccess { + + //-------- get object property + @Override + public String getProperty(String pid, String propertyName) throws IOException, RepositoryException { + org.dom4j.Document objectFoxml = getFoxml(pid); + return objectFoxml == null ? null : extractProperty(objectFoxml, propertyName); + } + private String extractProperty(org.dom4j.Document foxmlDoc, String name) { + org.dom4j.Node node = Dom4jUtils.buildXpath(String.format("/foxml:digitalObject/foxml:objectProperties/foxml:property[@NAME='%s']/@VALUE", name)).selectSingleNode(foxmlDoc); + return node == null ? null : Dom4jUtils.toStringOrNull(node); + } + @Override + public String getPropertyLabel(String pid) throws IOException, RepositoryException { + return getProperty(pid, "info:fedora/fedora-system:def/model#label"); + } + @Override + public LocalDateTime getPropertyCreated(String pid) throws IOException, RepositoryException { + String propertyValue = getProperty(pid, "info:fedora/fedora-system:def/model#createdDate"); + if (propertyValue != null) { + try { + return LocalDateTime.parse(propertyValue, RepositoryApi.TIMESTAMP_FORMATTER); + } catch (DateTimeParseException e) { + System.out.println(String.format("cannot parse createdDate %s from object %s", propertyValue, pid)); + } + } + return null; + } + @Override + public LocalDateTime getPropertyLastModified(String pid) throws IOException, RepositoryException { + String propertyValue = getProperty(pid, "info:fedora/fedora-system:def/view#lastModifiedDate"); + if (propertyValue != null) { + try { + return LocalDateTime.parse(propertyValue, RepositoryApi.TIMESTAMP_FORMATTER); + } catch (DateTimeParseException e) { + System.out.println(String.format("cannot parse lastModifiedDate %s from object %s", propertyValue, pid)); + } + } + return null; + } + @Override + public Date getObjectLastmodifiedFlag(String pid) throws IOException { + DigitalObject object = manager.readObjectFromStorage(pid); + if (object != null) { + return AkubraUtils.getLastModified(object); + } + throw new IOException("Object not found: " + pid); + } + @Override + public String getModel(String objectPid) throws RepositoryException, IOException, SolrServerException { + Map description = repositoryApi.getDescription(objectPid); + String model = description.get("model"); + return model == null ? null : model.substring("model:".length()); + } + /* + @Override + public InputStream getFoxml(String pid, boolean archive) throws IOException { + try { + if (archive){ + DigitalObject obj = manager.readObjectCloneFromStorage(pid); + manager.resolveArchivedDatastreams(obj); + return this.manager.marshallObject(obj); + }else { + return this.manager.retrieveObject(pid); + } + } catch (Exception e) { + throw new IOException(e); + } + } + @Override + public org.dom4j.Document getFoxml(String pid) throws RepositoryException, IOException { + Lock readLock = AkubraDOManager.getReadLock(pid); + try { + RepositoryObject object = akubraRepositoryImpl.getObject(pid); + return Utils.inputstreamToDocument(object.getFoxml(), true); + } finally { + readLock.unlock(); + } + } + @Override + public boolean objectExists(String pid) throws RepositoryException { + Lock readLock = AkubraDOManager.getReadLock(pid); + try { + return akubraRepositoryImpl.objectExists(pid); + } finally { + readLock.unlock(); + } + } + @Override + public boolean isPidAvailable(String pid) throws IOException, RepositoryException { + boolean exists = this.repositoryApi.objectExists(pid); + return exists; + } + + */ + +} diff --git a/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/RepositoryAccessImpl.java b/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/RepositoryAccessImpl.java index 06f3c6fd1..4298a89c6 100644 --- a/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/RepositoryAccessImpl.java +++ b/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/RepositoryAccessImpl.java @@ -3,6 +3,7 @@ import com.google.inject.Inject; import com.google.inject.name.Named; import com.qbizm.kramerius.imp.jaxb.DigitalObject; +import cz.incad.kramerius.fedora.ObjectAccess; import cz.incad.kramerius.fedora.ProcessingIndexAccess; import cz.incad.kramerius.fedora.RepositoryAccess; import cz.incad.kramerius.fedora.DatastreamAccess; @@ -52,7 +53,6 @@ public class RepositoryAccessImpl implements RepositoryAccess { private ProcessingIndexFeeder feeder; private AggregatedAccessLogs accessLog; - @Inject public RepositoryAccessImpl(ProcessingIndexFeeder feeder, @Nullable AggregatedAccessLogs accessLog, @Named("akubraCacheManager") CacheManager cacheManager) throws IOException { super( accessLog); @@ -76,95 +76,61 @@ public boolean isObjectAvailable(String pid) throws IOException { throw new IOException(e); } } - @Override - public boolean objectExists(String pid) throws RepositoryException { - Lock readLock = AkubraDOManager.getReadLock(pid); - try { - return akubraRepositoryImpl.objectExists(pid); - } finally { - readLock.unlock(); + public T getFoxml(String pid, KnownDatastreams dsId, Class 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); } - @Override - public boolean isPidAvailable(String pid) throws IOException, RepositoryException { - boolean exists = this.repositoryApi.objectExists(pid); - return exists; - } - //-------- get object property - @Override - public String getProperty(String pid, String propertyName) throws IOException, RepositoryException { + public T getProperty(String pid, String propertyName, Class returnType) { org.dom4j.Document objectFoxml = getFoxml(pid); return objectFoxml == null ? null : extractProperty(objectFoxml, propertyName); } - private String extractProperty(org.dom4j.Document foxmlDoc, String name) { - org.dom4j.Node node = Dom4jUtils.buildXpath(String.format("/foxml:digitalObject/foxml:objectProperties/foxml:property[@NAME='%s']/@VALUE", name)).selectSingleNode(foxmlDoc); - return node == null ? null : Dom4jUtils.toStringOrNull(node); - } - @Override - public String getPropertyLabel(String pid) throws IOException, RepositoryException { - return getProperty(pid, "info:fedora/fedora-system:def/model#label"); - } @Override - public LocalDateTime getPropertyCreated(String pid) throws IOException, RepositoryException { - String propertyValue = getProperty(pid, "info:fedora/fedora-system:def/model#createdDate"); - if (propertyValue != null) { - try { - return LocalDateTime.parse(propertyValue, RepositoryApi.TIMESTAMP_FORMATTER); - } catch (DateTimeParseException e) { - System.out.println(String.format("cannot parse createdDate %s from object %s", propertyValue, pid)); - } - } - return null; - } - @Override - public LocalDateTime getPropertyLastModified(String pid) throws IOException, RepositoryException { - String propertyValue = getProperty(pid, "info:fedora/fedora-system:def/view#lastModifiedDate"); - if (propertyValue != null) { - try { - return LocalDateTime.parse(propertyValue, RepositoryApi.TIMESTAMP_FORMATTER); - } catch (DateTimeParseException e) { - System.out.println(String.format("cannot parse lastModifiedDate %s from object %s", propertyValue, pid)); - } + public void ingestObject(org.dom4j.Document foxmlDoc, String pid) throws RepositoryException, IOException { + DigitalObject digitalObject = foxmlDocToDigitalObject(foxmlDoc); + Lock writeLock = AkubraDOManager.getWriteLock(pid); + try { + akubraRepositoryImpl.ingestObject(digitalObject); + akubraRepositoryImpl.commitTransaction(); + } finally { + writeLock.unlock(); } - return null; } @Override - public Date getObjectLastmodifiedFlag(String pid) throws IOException { - DigitalObject object = manager.readObjectFromStorage(pid); - if (object != null) { - return AkubraUtils.getLastModified(object); + public void deleteObject(String pid, boolean deleteDataOfManagedDatastreams) throws RepositoryException, IOException { + Lock writeLock = AkubraDOManager.getWriteLock(pid); + try { + akubraRepositoryImpl.deleteObject(pid, deleteDataOfManagedDatastreams, true); + akubraRepositoryImpl.commitTransaction(); + } finally { + writeLock.unlock(); } - throw new IOException("Object not found: " + pid); } - @Override - public String getModel(String objectPid) throws RepositoryException, IOException, SolrServerException { - Map description = repositoryApi.getDescription(objectPid); - String model = description.get("model"); - return model == null ? null : model.substring("model:".length()); + public ObjectAccess getObjectAccessHelper(){ + return null; } - // --- -- STREAMS --------------------------------------------------- + // --- -- Object stream --------------------------------------------------- public boolean isDatastreamAvailable(String pid, KnownDatastreams dsId) { boolean exists = this.repositoryApi.datastreamExists(pid, dsId); return exists; } - public List getDatastreamNames(String pid) { - Lock readLock = AkubraDOManager.getReadLock(pid); - try { - RepositoryObject object = akubraRepositoryImpl.getObject(pid); - List streams = object.getStreams(); - return streams.stream().map(it -> { - try { - return it.getName(); - } catch (RepositoryException e) { - LOGGER.log(Level.SEVERE,e.getMessage(),e); - return null; - } - }).collect(Collectors.toList()); - } finally { - readLock.unlock(); - } - } public T getDatastreamFoxml(String pid, KnownDatastreams dsId, Class returnType) { // Determine supported formats for the content ContentFormat supportedFormat = determineSupportedFormat(id); @@ -186,7 +152,7 @@ public T getDatastreamFoxml(String pid, KnownDatastreams dsId, Class retu } throw new IllegalArgumentException("Unsupported content type: " + contentType); } - public T getDatastreamVersionProperty(String pid, KnownDatastreams dsId, String propertyName, Class returnType) { + public T getDatastreamProperty(String pid, KnownDatastreams dsId, String propertyName, Class returnType) { org.dom4j.Document objectFoxml = getFoxml(pid); return objectFoxml == null ? null : extractProperty(objectFoxml, propertyName); } @@ -211,8 +177,82 @@ public T getDatastreamContent(String pid, KnownDatastreams dsId, Class re } throw new IllegalArgumentException("Unsupported content type: " + contentType); } - public DatastreamAccess getDatastreamAccessHelper(){ - return null; + public List getDatastreamNames(String pid) { + Lock readLock = AkubraDOManager.getReadLock(pid); + try { + RepositoryObject object = akubraRepositoryImpl.getObject(pid); + List streams = object.getStreams(); + return streams.stream().map(it -> { + try { + return it.getName(); + } catch (RepositoryException e) { + LOGGER.log(Level.SEVERE,e.getMessage(),e); + return null; + } + }).collect(Collectors.toList()); + } finally { + readLock.unlock(); + } + } + @Override + public void updateInlineXmlDatastream(String pid, String dsId, org.dom4j.Document streamDoc, String formatUri) throws RepositoryException, IOException { + Lock writeLock = AkubraDOManager.getWriteLock(pid); + try { + RepositoryObject object = akubraRepositoryImpl.getObject(pid); + + object.deleteStream(dsId); + object.createStream(dsId, "text/xml", new ByteArrayInputStream(streamDoc.asXML().getBytes(Charset.forName("UTF-8")))); + + } finally { + writeLock.unlock(); + } + } + public void updateBinaryDatastream(String pid, String streamName, String mimeType, byte[] byteArray) throws RepositoryException { + Lock writeLock = AkubraDOManager.getWriteLock(pid); + try { + RepositoryObject object = akubraRepositoryImpl.getObject(pid); + if (object != null) { + if (object.streamExists(streamName)) { + object.deleteStream(streamName); + } + ByteArrayInputStream bos = new ByteArrayInputStream(byteArray); + object.createManagedStream(streamName, mimeType, bos); + } + } finally { + writeLock.unlock(); + } + } + @Override + public void setDatastreamXml(String pid, String dsId, org.dom4j.Document ds) throws RepositoryException, IOException { + Lock writeLock = AkubraDOManager.getWriteLock(pid); + try { + org.dom4j.Document foxml = getFoxml(pid); + org.dom4j.Element originalDsEl = (org.dom4j.Element) Dom4jUtils.buildXpath(String.format("/foxml:digitalObject/foxml:datastream[@ID='%s']", dsId)).selectSingleNode(foxml); + if (originalDsEl != null) { + originalDsEl.detach(); + } + foxml.getRootElement().add(ds.getRootElement().detach()); + updateLastModifiedTimestamp(foxml); + DigitalObject updatedDigitalObject = foxmlDocToDigitalObject(foxml); + akubraRepositoryImpl.deleteObject(pid, false, false); + akubraRepositoryImpl.ingestObject(updatedDigitalObject); + akubraRepositoryImpl.commitTransaction(); + } finally { + writeLock.unlock(); + } + } + public void deleteDatastream(String pid, String streamName) throws RepositoryException { + Lock writeLock = AkubraDOManager.getWriteLock(pid); + try { + RepositoryObject object = akubraRepositoryImpl.getObject(pid); + if (object != null) { + if (object.streamExists(streamName)) { + object.deleteStream(streamName); + } + } + } finally { + writeLock.unlock(); + } } // TODO here we always use AkubraUtils.getStreamContent but we have also AkubraObject.AkubraDatastream for fetching stream content /* TODO @@ -246,6 +286,34 @@ public List> getStreamsOfObject(String pid) throws IOExcepti } } */ + public DatastreamAccess getDatastreamAccessHelper(){ + return null; + } + + //------Processing index---------------------------------------------------- + public T queryProcessingIndex(ProcessingIndexQueryParameters params, ResultMapper mapper) { + return null; + }; + public ProcessingIndexAccess getProcessingIndexAccessHelper(){ + return null; + } + + @Override + public void shutdown() { + manager.shutdown(); + } + @Override + public String getFedoraVersion() throws IOException { + return "Akubra"; + } + + private void reportAccess(String pid, String streamName) { + try { + this.accessLog.reportAccess(pid, streamName); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Can't write statistic records for " + pid + ", stream name: " + streamName, e); + } + } private ContentFormat determineSupportedFormat(String id) { // Example logic to determine supported formats if (id.startsWith("streamOnly")) { @@ -272,29 +340,6 @@ private Map createMap(String label) { map.put("label", label); return map; } - //------------------------------------------------------------->>>>> - - - ///////Processing index..//////////////////////////////////////////////////////////////////////////////////////// - public T queryProcessingIndex(ProcessingIndexQueryParameters params, ResultMapper mapper) { - return null; - }; - public ProcessingIndexAccess getProcessingIndexAccessHelper(){ - return null; - } - - //--- UPDATE ------------------------------------------------------------------------------------------------ - @Override - public void ingestObject(org.dom4j.Document foxmlDoc, String pid) throws RepositoryException, IOException { - DigitalObject digitalObject = foxmlDocToDigitalObject(foxmlDoc); - Lock writeLock = AkubraDOManager.getWriteLock(pid); - try { - akubraRepositoryImpl.ingestObject(digitalObject); - akubraRepositoryImpl.commitTransaction(); - } finally { - writeLock.unlock(); - } - } private DigitalObject foxmlDocToDigitalObject(org.dom4j.Document foxml) throws IOException { try { return (DigitalObject) digitalObjectUnmarshaller.unmarshal(new StringReader(foxml.asXML())); @@ -302,78 +347,6 @@ private DigitalObject foxmlDocToDigitalObject(org.dom4j.Document foxml) throws I throw new IOException(e); } } - @Override - public void updateInlineXmlDatastream(String pid, String dsId, org.dom4j.Document streamDoc, String formatUri) throws RepositoryException, IOException { - Lock writeLock = AkubraDOManager.getWriteLock(pid); - try { - RepositoryObject object = akubraRepositoryImpl.getObject(pid); - - object.deleteStream(dsId); - object.createStream(dsId, "text/xml", new ByteArrayInputStream(streamDoc.asXML().getBytes(Charset.forName("UTF-8")))); - - } finally { - writeLock.unlock(); - } - } - @Override - public void updateRelsExt(String pid, org.dom4j.Document relsExtDoc) throws IOException, RepositoryException { - updateInlineXmlDatastream(pid, KnownDatastreams.RELS_EXT.toString(), relsExtDoc, KnownXmlFormatUris.RELS_EXT); - } - @Override - public void updateMods(String pid, org.dom4j.Document modsDoc) throws IOException, RepositoryException { - updateInlineXmlDatastream(pid, KnownDatastreams.BIBLIO_MODS.toString(), modsDoc, KnownXmlFormatUris.BIBLIO_MODS); - } - @Override - public void updateDublinCore(String pid, org.dom4j.Document dcDoc) throws IOException, RepositoryException { - updateInlineXmlDatastream(pid, KnownDatastreams.BIBLIO_DC.toString(), dcDoc, KnownXmlFormatUris.BIBLIO_DC); - } - public void updateBinaryDatastream(String pid, String streamName, String mimeType, byte[] byteArray) throws RepositoryException { - Lock writeLock = AkubraDOManager.getWriteLock(pid); - try { - RepositoryObject object = akubraRepositoryImpl.getObject(pid); - if (object != null) { - if (object.streamExists(streamName)) { - object.deleteStream(streamName); - } - ByteArrayInputStream bos = new ByteArrayInputStream(byteArray); - object.createManagedStream(streamName, mimeType, bos); - } - } finally { - writeLock.unlock(); - } - } - public void deleteDatastream(String pid, String streamName) throws RepositoryException { - Lock writeLock = AkubraDOManager.getWriteLock(pid); - try { - RepositoryObject object = akubraRepositoryImpl.getObject(pid); - if (object != null) { - if (object.streamExists(streamName)) { - object.deleteStream(streamName); - } - } - } finally { - writeLock.unlock(); - } - } - @Override - public void setDatastreamXml(String pid, String dsId, org.dom4j.Document ds) throws RepositoryException, IOException { - Lock writeLock = AkubraDOManager.getWriteLock(pid); - try { - org.dom4j.Document foxml = getFoxml(pid); - org.dom4j.Element originalDsEl = (org.dom4j.Element) Dom4jUtils.buildXpath(String.format("/foxml:digitalObject/foxml:datastream[@ID='%s']", dsId)).selectSingleNode(foxml); - if (originalDsEl != null) { - originalDsEl.detach(); - } - foxml.getRootElement().add(ds.getRootElement().detach()); - updateLastModifiedTimestamp(foxml); - DigitalObject updatedDigitalObject = foxmlDocToDigitalObject(foxml); - akubraRepositoryImpl.deleteObject(pid, false, false); - akubraRepositoryImpl.ingestObject(updatedDigitalObject); - akubraRepositoryImpl.commitTransaction(); - } finally { - writeLock.unlock(); - } - } private void updateLastModifiedTimestamp(org.dom4j.Document foxml) { Attribute valueAttr = (Attribute) Dom4jUtils.buildXpath("/foxml:digitalObject/foxml:objectProperties/foxml:property[@NAME='info:fedora/fedora-system:def/view#lastModifiedDate']/@VALUE").selectSingleNode(foxml); if (valueAttr != null) { @@ -414,31 +387,5 @@ private int extractLatestDsIdVersion(org.dom4j.Element datastreamEl) { } return maxVersion; } - @Override - public void deleteObject(String pid, boolean deleteDataOfManagedDatastreams) throws RepositoryException, IOException { - Lock writeLock = AkubraDOManager.getWriteLock(pid); - try { - akubraRepositoryImpl.deleteObject(pid, deleteDataOfManagedDatastreams, true); - akubraRepositoryImpl.commitTransaction(); - } finally { - writeLock.unlock(); - } - } - - @Override - public void shutdown() { - manager.shutdown(); - } - @Override - public String getFedoraVersion() throws IOException { - return "Akubra"; - } - private void reportAccess(String pid, String streamName) { - try { - this.accessLog.reportAccess(pid, streamName); - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Can't write statistic records for " + pid + ", stream name: " + streamName, e); - } - } } diff --git a/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/tmp/TmpAbstractRepositoryAccess.java b/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/tmp/TmpAbstractRepositoryAccess.java index 17586d294..ba3b02516 100644 --- a/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/tmp/TmpAbstractRepositoryAccess.java +++ b/shared/common/src/main/java/cz/incad/kramerius/fedora/impl/tmp/TmpAbstractRepositoryAccess.java @@ -57,112 +57,6 @@ public TmpAbstractRepositoryAccess(@Nullable StatisticsAccessLog accessLog) this.xPathFactory = XPathFactory.newInstance(); } - @Override - public String findFirstViewablePid(String pid) throws IOException { - final List foundPids = new ArrayList(); - try { - processSubtree(makeSureObjectPid(pid), new TreeNodeProcessor() { - boolean breakProcess = false; - int previousLevel = 0; - - @Override - public boolean breakProcessing(String pid, int level) { - return breakProcess; - } - - @Override - public boolean skipBranch(String pid, int level) { - return false; - } - - @Override - public void process(String pid, int level) throws ProcessSubtreeException { - try { - if (previousLevel < level || level == 0) { - if (TmpAbstractRepositoryAccess.this.isImageFULLAvailable(pid)) { - foundPids.add(pid); - breakProcess = true; - } - } else if (previousLevel > level) { - breakProcess = true; - } else if ((previousLevel == level) && (level != 0)) { - breakProcess = true; - } - previousLevel = level; - } catch (Exception e) { - throw new ProcessSubtreeException(e); - } - } - }); - } catch (ProcessSubtreeException e) { - throw new IOException(e); - } catch (LexerException e) { - throw new IOException(e); - } - - return foundPids.isEmpty() ? null : foundPids.get(0); - } - - @Override - public void processSubtree(String pid, TreeNodeProcessor processor) throws ProcessSubtreeException, IOException { - try { - pid = makeSureObjectPid(pid); - Document relsExt = null; - try { - // should be from - if (isStreamAvailable(pid, FedoraUtils.RELS_EXT_STREAM)) { - relsExt = getRelsExt(pid); - } else { - LOGGER.warning("could not read root RELS-EXT, skipping object (" + pid + ")"); - } - } catch (Exception ex) { - LOGGER.warning("could not read root RELS-EXT, skipping object (" + pid + "):" + ex); - } - if (!processor.skipBranch(pid, 0)) { - processSubtreeInternal(pid, relsExt, processor, 0, new Stack()); - } - } catch (LexerException e) { - LOGGER.warning("Error in pid: " + pid); - throw new ProcessSubtreeException(e); - } catch (XPathExpressionException e) { - throw new ProcessSubtreeException(e); - } - } - - - // TODO something more generic like getFieldFromStream - @Override - public String getDonator(String pid) throws IOException { - return getDonator(getRelsExt(pid)); - } - - // TODO something more generic like getFieldFromStream - @Override - public String getKrameriusModelName(String pid) throws IOException { - return getKrameriusModelName(getRelsExt(pid)); - } - - // TODO something more generic like getFieldFromStream - @Override - public List getPages(String pid, boolean deep) throws IOException { - Document relsExt = getRelsExt(pid); - return getPages(pid, relsExt.getDocumentElement()); - } - - - // TODO something more generic like getFieldFromStream - @Override - public String getFirstItemPid(String pid) throws IOException { - Document relsExt = getRelsExt(pid); - return getFirstItemPid(relsExt); - } - - // TODO something more generic like getFieldFromStream - @Override - public String getFirstVolumePid(String pid) throws IOException { - Document relsExt = getRelsExt(pid); - return getFirstVolumePid(relsExt); - } /* @Override @@ -175,42 +69,6 @@ public boolean isImageFULLAvailable(String pid) throws IOException { } }*/ - @Override - public boolean getFirstViewablePath(List pids, List models) throws IOException { - try { - String pid = pids.get(pids.size() - 1); - pid = makeSureObjectPid(pid); - if (isImageFULLAvailable(pid)) { - return true; - } - Document relsExt = getRelsExt(pid); - Element descEl = XMLUtils.findElement(relsExt.getDocumentElement(), "Description", - FedoraNamespaces.RDF_NAMESPACE_URI); - List els = XMLUtils.getElements(descEl); - for (Element el : els) { - if (getTreePredicates().contains(el.getLocalName())) { - if (el.hasAttribute("rdf:resource")) { - pid = el.getAttributes().getNamedItem("rdf:resource").getNodeValue(); - pids.add(pid); - models.add(getKrameriusModelName(pid)); - if (getFirstViewablePath(pids, models)) { - return true; - } else { - pids.remove(pids.size() - 1); - models.remove(pids.size() - 1); - } - } - } - } - return false; - } catch (DOMException e) { - LOGGER.log(Level.SEVERE, e.getMessage(), e); - throw new IOException(e); - } catch (LexerException e) { - LOGGER.log(Level.SEVERE, e.getMessage(), e); - throw new IOException(e); - } - } /* @Override @@ -244,6 +102,7 @@ public List getModelsOfRel(String pid) throws IOException { return getModelsOfRel(getRelsExt(pid)); }*/ // RepoApiImpl------------------------------------------------------------------------------------------- +/* @Inject public RepositoryApiImpl(ProcessingIndexFeeder processingIndexFeeder, @Named("akubraCacheManager") CacheManager cacheManager) throws RepositoryException { try { @@ -256,8 +115,8 @@ public RepositoryApiImpl(ProcessingIndexFeeder processingIndexFeeder, @Named("ak throw new RepositoryException("Error initializing JAXB unmarshaller for " + DigitalObject.class.getName()); } } - - +*/ +/* //--------- KraRepositoryAPIIMPl @javax.inject.Inject private RepositoryApiImpl repositoryApi; @@ -270,6 +129,6 @@ public RepositoryApiImpl(ProcessingIndexFeeder processingIndexFeeder, @Named("ak public RepositoryApi getLowLevelApi() { return repositoryApi; } - +*/ }