Skip to content

Commit

Permalink
Merge pull request #386 from hbz/DeepGreen
Browse files Browse the repository at this point in the history
Merge changes for Deepgreen Application
  • Loading branch information
inkuss authored Sep 21, 2022
2 parents 494d15c + 58e3bb7 commit 9ad668d
Show file tree
Hide file tree
Showing 22 changed files with 1,463 additions and 93 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ data
bin
conf/application.conf
conf/mail.properties
conf/site.conf
scripts/variables.conf
*.HIST
*~
Expand Down
4 changes: 2 additions & 2 deletions app/actions/Create.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public Node patchResource(Node node, ToScienceObject object) {
* anhand ein- und dessselben ToScience-Objektes.
*
* @param nodes nodes to set new properties for
* @param object the RegalObject contains props that will be applied to all
* nodes in the list
* @param object the ToScienceObject contains props that will be applied to
* all nodes in the list
* @return a message
*/
public String patchResources(List<Node> nodes, ToScienceObject object) {
Expand Down
1 change: 1 addition & 0 deletions app/actions/Enrich.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ private static void enrich(String uri, List<Statement> enrichStatements) {
}

private static Statement getLabelStatement(String uri) {
play.Logger.debug("uri=" + uri);
String prefLabel = MyEtikettMaker.getLabelFromEtikettWs(uri);
ValueFactory v = RdfUtils.valueFactory;
Statement newS = v.createStatement(v.createIRI(uri),
Expand Down
100 changes: 100 additions & 0 deletions app/actions/Modify.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
Expand All @@ -47,12 +48,14 @@
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import com.wordnik.swagger.core.util.JsonUtil;

import archive.fedora.CopyUtils;
import archive.fedora.RdfException;
Expand All @@ -61,6 +64,7 @@
import controllers.MyController;
import helper.DataciteClient;
import helper.HttpArchiveException;
import helper.JsonMapper;
import helper.MyEtikettMaker;
import helper.URN;
import helper.oai.OaiDispatcher;
Expand All @@ -74,6 +78,7 @@
*
*/
public class Modify extends RegalAction {
String msg = "";

/**
* @param pid the pid that must be updated
Expand Down Expand Up @@ -186,6 +191,28 @@ public String updateLobidify2AndEnrichMetadata(String pid, String content) {
}
}

/**
* This method maps DeepGreen data to the Lobid2 format and creates a
* datastream "metadata2" of the resource
*
* @param pid The pid of the resource that must be updated
* @param embargoDuration Die Dauer des Embargos in Monaten
* @param format Das RDF-Format, in das die Metadaten konvertiert werden
* sollen (z.B. TURTLE, XDFXML, NTRIPLES)
* @param content The metadata in the format DeepGreen-XML
* @return a short message
*/
public String updateLobidify2AndEnrichDeepGreenData(String pid,
int embargoDuration, RDFFormat format, Document content) {
try {
Node node = new Read().readNode(pid);
return updateLobidify2AndEnrichDeepGreenData(node, embargoDuration,
format, content);
} catch (Exception e) {
throw new UpdateNodeException(e);
}
}

/**
* @param node The node that must be updated
* @param content The metadata as rdf string
Expand Down Expand Up @@ -257,6 +284,47 @@ public String updateLobidify2AndEnrichMetadata(Node node, String content) {
}
}

/**
* The method maps DeepGreen metadata to the Lobid2 format and creates a data
* stream Metadata2 of the resource
*
* @param node The node of the resource that must be updated
* @param embargoDuration Die Dauer des Embargos in Monaten
* @param format RDF-Format, z.B. NTRIPLES
* @param content The metadata as DeepGreen XML
* @return a short message
*/
public String updateLobidify2AndEnrichDeepGreenData(Node node,
int embargoDuration, RDFFormat format, Document content) {

try {
play.Logger.debug("Start updateLobidify2AndEnrichDeepGreenData");
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");
}

Map<String, Object> rdf = new XmlUtils()
.getLd2Lobidify2DeepGreen(node.getLd2(), embargoDuration, content);
play.Logger.debug("Mapped DeepGrren data to lobid2!");
updateMetadata2(node, rdfToString(rdf, format));
play.Logger.debug("Updated Metadata2 datastream!");

String enrichMessage = Enrich.enrichMetadata2(node);
return pid
+ " DeepGreen-metadata successfully updated, lobidified and enriched! "
+ enrichMessage;
} catch (Exception e) {
play.Logger.error(
"Datastream metadata2 with mapped DeepGreen data could not be created!",
e);
throw new RuntimeException(e);
}
}

public String updateLobidify2AndEnrichMetadataIfRecentlyUpdated(String pid,
String content, LocalDate date) {
try {
Expand Down Expand Up @@ -1030,6 +1098,8 @@ public String lobidify2(Node node, String alephid) {
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."
Expand All @@ -1041,6 +1111,8 @@ String updateMetadata2(Node node, String content) {
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);
Expand Down Expand Up @@ -1097,4 +1169,32 @@ private static List<String> getType(Node node) {
}
}

private String rdfToString(Map<String, Object> result, RDFFormat format) {
try {
String rdf = RdfUtils.readRdfToString(
new ByteArrayInputStream(json(result).getBytes("utf-8")),
RDFFormat.JSONLD, format, "");
return rdf;
} catch (Exception e) {
throw new HttpArchiveException(500, e);
}
}

/*
* Mappt Objekt nach JSON-String. "Objekt" ist typischerweise eine Java Map.
*/
private String json(Object obj) {
try {
play.Logger.debug("Start json(obj)");
StringWriter w = new StringWriter();
ObjectMapper mapper = JsonUtil.mapper();
mapper.writeValue(w, obj);
String result = w.toString();
play.Logger.debug("Return result " + result);
return result;
} catch (IOException e) {
throw new HttpArchiveException(500, e);
}
}

}
17 changes: 8 additions & 9 deletions app/actions/Read.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,16 @@ public Node readNode(String pid) {
public Node getLastModifiedChild(Node node, String contentType) {
if (contentType == null || contentType.isEmpty()) {
return getLastModifiedChild(node);
} else {
Node oldestNode = null;
for (Node n : getParts(node)) {
if (contentType.equals(n.getContentType())) {
oldestNode = compareDates(n, oldestNode);
}
}
Node oldestNode = null;
for (Node n : getParts(node)) {
if (contentType.equals(n.getContentType())) {
oldestNode = compareDates(n, oldestNode);
}
if (oldestNode == null)
return node;
return oldestNode;
}
if (oldestNode == null)
return node;
return oldestNode;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion app/actions/RegalAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
public class RegalAction {

@SuppressWarnings("javadoc")
public Node updateIndex(String pid) {
removeNodeFromCache(pid);
Node node = new Read().readNode(pid);
Expand All @@ -47,9 +48,11 @@ void removeNodeFromCache(String pid) {
Cache.remove(pid);
}

@SuppressWarnings("javadoc")
protected String createAggregationUri(String pid) {
return Globals.useHttpUris
? Globals.protocol + Globals.server + "/resource/" + pid : pid;
? Globals.protocol + Globals.server + "/resource/" + pid
: pid;
}

/**
Expand Down Expand Up @@ -81,10 +84,12 @@ public String apply(List<Node> nodes, ProcessNode action) {
return str.toString();
}

@SuppressWarnings("javadoc")
protected String getUriFromJsonName(String name) {
return Globals.profile.getEtikettByName(name).getUri();
}

@SuppressWarnings("javadoc")
protected String getJsonName(String uri) {
return Globals.profile.getEtikett(uri).getName();
}
Expand Down
1 change: 1 addition & 0 deletions app/archive/fedora/FedoraFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public static FedoraFacade getInstance(String host, String aUser,
*/
public void createNode(Node node) {
try {
play.Logger.debug("Start create Node");
play.Logger.debug("node.getPid()=" + node.getPid());
play.Logger.debug("node.getLabel()=" + node.getLabel());
new Ingest(node.getPid()).label(node.getLabel()).execute();
Expand Down
Loading

0 comments on commit 9ad668d

Please sign in to comment.