Skip to content

Commit

Permalink
Merge pull request #430 from hbz/refactorGetSetMetadata
Browse files Browse the repository at this point in the history
Refactor get set metadata
  • Loading branch information
inkuss authored Dec 5, 2023
2 parents d598598 + d2f3feb commit 0431f21
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 291 deletions.
7 changes: 4 additions & 3 deletions app/actions/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.rdf4j.rio.RDFFormat;

import archive.fedora.RdfUtils;
import static archive.fedora.Vocabulary.*;
import models.Globals;
import models.Node;

Expand Down Expand Up @@ -157,11 +158,11 @@ public String deleteMetadataField(String pid, String field) {
Node node = new Read().readNode(pid);
String pred = getUriFromJsonName(field);
RepositoryConnection rdfRepo = RdfUtils.readRdfInputStreamToRepository(
new ByteArrayInputStream(node.getMetadata1().getBytes()),
new ByteArrayInputStream(node.getMetadata(metadata1).getBytes()),
RDFFormat.NTRIPLES);
Collection<Statement> myGraph =
RdfUtils.deletePredicateFromRepo(rdfRepo, pred);
return new Modify().updateMetadata1(node,
return new Modify().updateMetadata(metadata1, node,
RdfUtils.graphToString(myGraph, RDFFormat.NTRIPLES));
}

Expand All @@ -173,7 +174,7 @@ public String deleteMetadata2Field(String pid, String field) {
RDFFormat.NTRIPLES);
Collection<Statement> myGraph =
RdfUtils.deletePredicateFromRepo(rdfRepo, pred);
return new Modify().updateMetadata("metadata2", node,
return new Modify().updateMetadata(metadata2, node,
RdfUtils.graphToString(myGraph, RDFFormat.NTRIPLES));
}
}
27 changes: 13 additions & 14 deletions app/actions/Enrich.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import archive.fedora.RdfUtils;
import archive.fedora.XmlUtils;
import static archive.fedora.Vocabulary.*;
import helper.MyEtikettMaker;
import models.Globals;
import models.Node;
Expand All @@ -51,18 +52,18 @@ public class Enrich {
private static final String PREF_LABEL =
"http://www.w3.org/2004/02/skos/core#prefLabel";

public static String enrichMetadata2(Node node) {
public static String enrichMetadata1(Node node) {
try {
play.Logger.info("Enrich 2 " + node.getPid());
String metadata = node.getMetadata2();
play.Logger.info("Enrich " + node.getPid());
String metadata = node.getMetadata(metadata1);
if (metadata == null || metadata.isEmpty()) {
play.Logger.info("No metadata2 to enrich " + node.getPid());
return "No metadata2 to enrich " + node.getPid();
play.Logger.info("No metadata1 to enrich " + node.getPid());
return "No metadata1 to enrich " + node.getPid();
}
List<Statement> enrichStatements = new ArrayList<>();
enrichAll(node, metadata, enrichStatements);
metadata = RdfUtils.replaceTriples(enrichStatements, metadata);
new Modify().updateMetadata("metadata2", node, metadata);
new Modify().updateMetadata(metadata2, node, metadata);
} catch (Exception e) {
play.Logger.debug("", e);
return "Enrichment of " + node.getPid() + " partially failed !\n"
Expand All @@ -71,21 +72,19 @@ public static String enrichMetadata2(Node node) {
return "Enrichment of " + node.getPid() + " succeeded!";
}

public static String enrichMetadata1(Node node) {
public static String enrichMetadata2(Node node) {
try {
play.Logger.info("Enrich " + node.getPid());
String metadata = node.getMetadata1();
play.Logger.info("Enrich 2 " + node.getPid());
String metadata = node.getMetadata2();
if (metadata == null || metadata.isEmpty()) {
play.Logger.info("Not metadata to enrich " + node.getPid());
return "Not metadata to enrich " + node.getPid();
play.Logger.info("No metadata2 to enrich " + node.getPid());
return "No metadata2 to enrich " + node.getPid();
}
List<Statement> enrichStatements = new ArrayList<>();
enrichAll(node, metadata, enrichStatements);
metadata = RdfUtils.replaceTriples(enrichStatements, metadata);
new Modify().updateMetadata1(node, metadata);

new Modify().updateMetadata(metadata2, node, metadata);
} catch (Exception e) {
play.Logger.warn(e.getMessage());
play.Logger.debug("", e);
return "Enrichment of " + node.getPid() + " partially failed !\n"
+ e.getMessage();
Expand Down
81 changes: 6 additions & 75 deletions app/actions/Modify.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import archive.fedora.CopyUtils;
import archive.fedora.RdfException;
import archive.fedora.RdfUtils;
import static archive.fedora.Vocabulary.*;
import archive.fedora.XmlUtils;
import controllers.MyController;
import helper.DataciteClient;
Expand Down Expand Up @@ -236,7 +237,7 @@ public String updateLobidifyAndEnrichMetadata(Node node, String content) {
String alephid =
lobidUri.replaceFirst("http://lobid.org/resource[s]*/", "");
content = getLobid2DataAsNtripleString(node, alephid);
updateMetadata("metadata2", node, content);
updateMetadata(metadata2, node, content);

String enrichMessage2 = Enrich.enrichMetadata2(node);
return pid + " metadata successfully updated, lobidified and enriched! "
Expand Down Expand Up @@ -312,7 +313,7 @@ public String updateLobidify2AndEnrichDeepGreenData(Node node,
Map<String, Object> rdf = new XmlUtils().getLd2Lobidify2DeepGreen(
node.getLd2(), embargoDuration, deepgreenId, content);
play.Logger.debug("Mapped DeepGrren data to lobid2!");
updateMetadata("metadata2", node, rdfToString(rdf, format));
updateMetadata(metadata2, node, rdfToString(rdf, format));
play.Logger.debug("Updated Metadata2 datastream!");

String enrichMessage = Enrich.enrichMetadata2(node);
Expand Down Expand Up @@ -359,7 +360,7 @@ public String updateLobidify2AndEnrichMetadataIfRecentlyUpdated(Node node,
try {
content = getLobid2DataAsNtripleStringIfResourceHasRecentlyChanged(node,
alephid, date);
updateMetadata("metadata2", node, content);
updateMetadata(metadata2, node, content);
msg.append(Enrich.enrichMetadata2(node));
} catch (NotUpdatedException e) {
play.Logger.debug("", e);
Expand All @@ -374,40 +375,6 @@ public String updateLobidify2AndEnrichMetadataIfRecentlyUpdated(Node node,

}

String updateMetadata1(Node node, String content) {
try {
String pid = node.getPid();
if (content == null) {
throw new HttpArchiveException(406,
pid + " You've tried to upload an empty string."
+ " This action is not supported."
+ " Use HTTP DELETE instead.\n");
}
// RdfUtils.validate(content);
// Extreme Workaround to fix subject uris
content = rewriteContent(content, pid);
// Workaround end
File file = CopyUtils.copyStringToFile(content);
node.setMetadataFile("metadata", file.getAbsolutePath());
if (content.contains(archive.fedora.Vocabulary.REL_LOBID_DOI)) {
List<String> dois = RdfUtils.findRdfObjects(node.getPid(),
archive.fedora.Vocabulary.REL_LOBID_DOI, content,
RDFFormat.NTRIPLES);
if (!dois.isEmpty()) {
node.setDoi(dois.get(0));
}
}
node.setMetadata1(content);
OaiDispatcher.makeOAISet(node);
reindexNodeAndParent(node);
return pid + " metadata successfully updated!";
} catch (RdfException e) {
throw new HttpArchiveException(400, e);
} catch (IOException e) {
throw new UpdateNodeException(e);
}
}

public String rewriteContent(String content, String pid) {
Collection<Statement> graph = RdfUtils.readRdfToGraph(
new ByteArrayInputStream(content.getBytes()), RDFFormat.NTRIPLES, "");
Expand Down Expand Up @@ -1097,49 +1064,13 @@ public Map<String, Object> setObjectTimestamp(Node node, Date date,
}
}

public String lobidify1(Node node, String alephid) {
updateMetadata1(node, getLobidDataAsNtripleString(node, alephid));
String enrichMessage = Enrich.enrichMetadata1(node);
return enrichMessage;
}

public String lobidify2(Node node, String alephid) {
updateMetadata("metadata2", node,
updateMetadata(metadata2, node,
getLobid2DataAsNtripleString(node, alephid));
String enrichMessage = Enrich.enrichMetadata2(node);
return enrichMessage;
}

// public String updateMetadata2(Node node, String content) {
// try {
// String pid = node.getPid();
// play.Logger.debug("Updating Metadata2 of PID " + pid);
// play.Logger.debug("content: " + content);
// if (content == null) {
// throw new HttpArchiveException(406,
// pid + " You've tried to upload an empty string."
// + " This action is not supported."
// + " Use HTTP DELETE instead.\n");
// }
// // RdfUtils.validate(content);
// // Extreme Workaround to fix subject uris
// content = rewriteContent(content, pid);
// // Workaround end
// File file = CopyUtils.copyStringToFile(content);
// play.Logger
// .debug("content.file.getAbsolutePath():" + file.getAbsolutePath());
// node.setMetadata2File(file.getAbsolutePath());
// node.setMetadata2(content);
// OaiDispatcher.makeOAISet(node);
// reindexNodeAndParent(node);
// return pid + " metadata2 successfully updated!";
// } catch (RdfException e) {
// throw new HttpArchiveException(400, e);
// } catch (IOException e) {
// throw new UpdateNodeException(e);
// }
// }

private static String getAuthorOrdering(Node node) {
try (InputStream in =
new ByteArrayInputStream(node.getMetadata2().getBytes())) {
Expand Down Expand Up @@ -1234,7 +1165,7 @@ public String updateMetadata(String metadataType, Node node, String content) {
+ " Use HTTP DELETE instead.\n");
}

if (metadataType.equals("metadata2")) {
if (metadataType.equals(metadata2)) {
content = rewriteContent(content, pid);
}

Expand Down
63 changes: 4 additions & 59 deletions app/actions/Read.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

import static archive.fedora.FedoraVocabulary.HAS_PART;
import static archive.fedora.FedoraVocabulary.IS_PART_OF;
import static archive.fedora.Vocabulary.REL_CONTENT_TYPE;
import static archive.fedora.Vocabulary.REL_IS_NODE_TYPE;
import static archive.fedora.Vocabulary.TYPE_OBJECT;
import static archive.fedora.Vocabulary.*;
import helper.HttpArchiveException;
import helper.JsonMapper;
import helper.Webgatherer;
Expand Down Expand Up @@ -472,26 +470,14 @@ public DublinCoreData readDC(String pid) {
return null;
}

/**
* @param pid the pid of the object
* @param field if field is specified, only the value of a certain field will
* be returned
* @return n-triple metadata
*/
public String readMetadata1(String pid, String field) {
Node node = internalReadNode(pid);
String result = readMetadata1(node, field);
return result == null ? "No " + field : result;
}

/**
* @param node
* @param field the shortname of metadata field
* @return the ntriples or just one field
*/
public String readMetadata1(Node node, String field) {
try {
String metadata = node.getMetadata1();
String metadata = node.getMetadata(metadata1);
if (metadata == null)
return null;
if (field == null || field.isEmpty()) {
Expand Down Expand Up @@ -593,35 +579,6 @@ public String readUrlHist(Node node) {
}
}

/**
* @param pid the pid of the object
* @param field if field is specified, only a certain field of the node's
* metadata will be returned
* @return n-triple metadata
*/
public String readMetadata1FromCache(String pid, String field) {
try {
Node node = readNode(pid);
String metadata = node.getMetadata1();
if (metadata == null || metadata.isEmpty())
throw new HttpArchiveException(404,
"No Metadata on " + pid + " available!");
if (field == null || field.isEmpty()) {
return metadata;
} else {
String pred = getUriFromJsonName(field);
List<String> value =
RdfUtils.findRdfObjects(pid, pred, metadata, RDFFormat.NTRIPLES);

return value.isEmpty() ? "No " + field : value.get(0);
}
} catch (UrlConnectionException e) {
throw new HttpArchiveException(404, e);
} catch (Exception e) {
throw new HttpArchiveException(500, e);
}
}

/**
* @param node the pid of the object
* @return ordered json array of parts
Expand Down Expand Up @@ -665,17 +622,6 @@ public Urn getUrnStatus(String urn, String pid) {
return result;
}

/**
* @param node the node to fetch a certain property from
* @param predicate the property in its long form
* @return all objects that are referenced using the predicate
*/
public List<String> getNodeLdProperty1(Node node, String predicate) {
List<String> linkedObjects = RdfUtils.findRdfObjects(node.getPid(),
predicate, node.getMetadata1(), RDFFormat.NTRIPLES);
return linkedObjects;
}

/**
* @param node
* @return 200 if object can be found at oaiprovider, 404 if not, 500 on error
Expand Down Expand Up @@ -786,9 +732,8 @@ private Map<String, Object> getGatherStatus(Node node) {
.equals(Gatherconf.CrawlerSelection.wpull)) {
entries.put("crawlControllerState",
WpullCrawl.getCrawlControllerState(node));
entries.put("crawlExitStatus",
WpullCrawl.getCrawlExitStatus(node) < 0 ? ""
: WpullCrawl.getCrawlExitStatus(node));
entries.put("crawlExitStatus", WpullCrawl.getCrawlExitStatus(node) < 0
? "" : WpullCrawl.getCrawlExitStatus(node));
}
/*
* Launch Count als Summe der Launches über alle Crawler ermitteln -
Expand Down
Loading

0 comments on commit 0431f21

Please sign in to comment.