Skip to content

Commit

Permalink
Merge pull request #426 from hbz/persistToscienceDatastreamByMaster
Browse files Browse the repository at this point in the history
Persist toscience datastream by master
  • Loading branch information
hadoud authored Nov 21, 2023
2 parents 02c4a7a + 42db895 commit 6c22748
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 10 deletions.
39 changes: 38 additions & 1 deletion app/actions/Modify.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ String updateMetadata1(Node node, String content) {
content = rewriteContent(content, pid);
// Workaround end
File file = CopyUtils.copyStringToFile(content);
node.setMetadataFile(file.getAbsolutePath());
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,
Expand Down Expand Up @@ -1211,4 +1211,41 @@ private String json(Object obj) {
}
}

public String updateMetadataJson(Node node, String content) {
try {
return updateMetadata("toscience", node, content);
} catch (Exception e) {
play.Logger.error(e.getMessage());
throw new RuntimeException(e);
}
}

public String updateMetadata(String metadataType, Node node, String content) {
try {
String pid = node.getPid();
play.Logger.debug(
"Updating metadata of type " + metadataType + " on 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");
}
File file = CopyUtils.copyStringToFile(content);
play.Logger
.debug("content.file.getAbsolutePath():" + file.getAbsolutePath());
node.setMetadataFile(metadataType, file.getAbsolutePath());
node.setMetadata(metadataType, content);
OaiDispatcher.makeOAISet(node);
reindexNodeAndParent(node);
return pid + " metadata of type " + metadataType
+ " successfully updated!";
} catch (RdfException e) {
throw new HttpArchiveException(400, e);
} catch (IOException e) {
throw new UpdateNodeException(e);
}
}

}
8 changes: 6 additions & 2 deletions app/archive/fedora/FedoraFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,12 @@ public void updateNode(Node node) {
}
play.Logger.debug("Updated stream");
}
if (node.getMetadataFile() != null) {
utils.updateMetadataStream(node);

if (node.getMetadataFile("metadata") != null) {
utils.updateMetadataStream(node, "metadata");
}
if (node.getMetadataFile("toscience") != null) {
utils.updateMetadataJsonStream(node);
}
if (node.getMetadata2File() != null) {
utils.updateMetadata2Stream(node);
Expand Down
26 changes: 24 additions & 2 deletions app/archive/fedora/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,9 @@ public void updateUrlHistStream(Node node) {
}
}

void updateMetadataStream(Node node) {
void updateMetadataStream(Node node, String metadataType) {
try {
File file = new File(node.getMetadataFile());
File file = new File(node.getMetadataFile(metadataType));
if (dataStreamExists(node.getPid(), "metadata")) {
new ModifyDatastream(node.getPid(), "metadata").versionable(true)
.dsLabel("n-triple rdf metadata").dsState("A").controlGroup("M")
Expand Down Expand Up @@ -990,4 +990,26 @@ private Transformer readTransformer(String prefixedPid) {
return t;
}

void updateMetadataJsonStream(Node node) {
updateMetadataStream(archive.fedora.Vocabulary.metadataJson,
"application/json", "Metadata in Format JSON", node);
}

public void updateMetadataStream(String metadataType, String mimeType,
String label, Node node) {
try {
File file = new File(node.getMetadataFile(metadataType));
if (dataStreamExists(node.getPid(), metadataType)) {
new ModifyDatastream(node.getPid(), metadataType).versionable(true)
.dsLabel(label).dsState("A").controlGroup("M").mimeType(mimeType)
.content(file).execute();
} else {
new AddDatastream(node.getPid(), metadataType).versionable(true)
.dsState("A").dsLabel(label).controlGroup("M").mimeType(mimeType)
.content(file).execute();
}
} catch (FedoraClientException e) {
throw new HttpArchiveException(e.getStatus(), e);
}
}
}
5 changes: 5 additions & 0 deletions app/archive/fedora/Vocabulary.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,9 @@ public abstract class Vocabulary {
*/
public final static String REL_LOBID_DOI =
"http://purl.org/ontology/bibo/doi";

public final static String metadata2 = "metadata2";
public final static String lrmiData = "lrmiData";
public final static String metadataJson = "toscience";

}
42 changes: 42 additions & 0 deletions app/controllers/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregations;
import org.json.JSONObject;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
Expand All @@ -57,6 +58,8 @@
import archive.fedora.RdfUtils;
import authenticate.BasicAuth;
import helper.HttpArchiveException;
import helper.RdfHelper;
import helper.ToscienceHelper;
import helper.WebgatherUtils;
import helper.WebsiteVersionPublisher;
import helper.oai.OaiDispatcher;
Expand Down Expand Up @@ -439,6 +442,45 @@ public static Promise<Result> updateMetadata(@PathParam("pid") String pid) {

return new ModifyAction().call(pid, node -> {
try {

/**
* 1. toscienceJson
*/

Node readNode = readNodeOrNull(pid);
JSONObject toscienceJson = null;
play.Logger
.debug("readNode.getContentType()= " + readNode.getContentType());
if (!readNode.getContentType().contains("file")
&& !readNode.getContentType().contains("part")) {

play.Logger.debug("toscienceJson will be mapped");

// ******************************
RDFFormat format = RDFFormat.NTRIPLES;
Map<String, Object> rdf = RdfHelper.getRdfAsMap(readNode, format,
request().body().asText());

play.Logger.debug("rdf=" + rdf.toString());
toscienceJson = new JSONObject(new JSONObject(rdf).toString());

play.Logger.debug("toscienceJson=" + toscienceJson.toString());

toscienceJson = ToscienceHelper.getPrefLabelsResolved(toscienceJson);

play.Logger.debug("toscienceJson=" + toscienceJson.toString());

// ******************************

modify.updateMetadataJson(readNode, toscienceJson.toString());
play.Logger
.debug("tosciecne from Node" + readNode.getMetadata("toscience"));
play.Logger.debug("Done toscienceJson Mapping");
}

/**
* 2. METADATA2
*/
String result = modify.updateLobidify2AndEnrichMetadata(pid,
request().body().asText());
return JsonMessage(new Message(result));
Expand Down
1 change: 1 addition & 0 deletions app/de/hbz/lobid/helper/JsonConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ private Map<String, Object> convert(String subject, Object context,
mainSubjectOfTheResource = subject;
collect(g);
Map<String, Object> result = createMap(g);
play.Logger.debug("result=" + result.toString());
result.put("@context", context);
return result;
}
Expand Down
85 changes: 85 additions & 0 deletions app/helper/RdfHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package helper;

import actions.Enrich;
import actions.Modify;
import actions.Read;
import archive.fedora.RdfUtils;
import controllers.MyController;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import models.Globals;
import models.Node;
import org.eclipse.rdf4j.rio.RDFFormat;
import java.nio.charset.StandardCharsets;
import de.hbz.lobid.helper.JsonConverter;
import de.hbz.lobid.helper.EtikettMakerInterface;

/**
*
* @author adoud
*
*/
public class RdfHelper {

/**
* This method converts lobid (N-triples) into a map object
*
* @param node ParentNode
* @param format N-Triples
* @param content Lobid
* @return Map Object
*/
public static Map<String, Object> getRdfAsMap(Node n, RDFFormat format,
String content) {

Map<String, Object> rdf = null;
String rewriteContent = null;
EtikettMakerInterface profile = Globals.profile;
JsonConverter jsonConverter = new JsonConverter(profile);

if (content == null) {
play.Logger.debug("Lobid (RDF) content is null");
return null;
}

try {

if (content.contains(archive.fedora.Vocabulary.REL_MAB_527)) {
String lobidUri = RdfUtils.findRdfObjects(n.getPid(),
archive.fedora.Vocabulary.REL_MAB_527, content, RDFFormat.NTRIPLES)
.get(0);
String alephid =
lobidUri.replaceFirst("http://lobid.org/resource[s]*/", "");
alephid = alephid.replaceAll("#.*", "");
content = Modify.getLobid2DataAsNtripleString(n, alephid);
// updateMetadata2(node, content);
rewriteContent = new Modify().rewriteContent(content, n.getPid());
play.Logger.debug("rewriteContent=" + rewriteContent);

} else {
rewriteContent = new Modify().rewriteContent(content, n.getPid());
play.Logger.debug("rewriteContent=" + rewriteContent);
}

InputStream stream = new ByteArrayInputStream(
rewriteContent.getBytes(StandardCharsets.UTF_8));

rdf = jsonConverter.convert(n.getPid(), stream, format,
profile.getContext().get("@context"));

play.Logger.debug("getRdfAsMap(),rdf=" + rdf.toString());

rdf.remove("@context");

play.Logger.debug("rdf without key @Context=" + rdf.toString());

} catch (Exception e) {
play.Logger.error("Lobid(RDF) Content could not be convert to Map", e);
}
return rdf;
}

}
86 changes: 86 additions & 0 deletions app/helper/ToscienceHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package helper;

import org.json.JSONObject;
import java.util.List;
import java.util.Iterator;
import org.json.JSONArray;
import helper.MyEtikettMaker;
import java.util.stream.Collectors;
import org.json.JSONException;
import play.Play;

/**
*
* @author adoud
*
*/

public class ToscienceHelper {

/**
* This method gets all unresolved PrefLabels and resolves them using the
* MyEtikettMaker
*
* @param allJsonObjects
* @return JSONObject with resolved prefLabels
*/
public static JSONObject getPrefLabelsResolved(JSONObject allJsonObjects) {

Object oldPrefLabel = null;
JSONObject jsObject = null;

Iterator<String> keys = allJsonObjects.keys();

try {
while (keys.hasNext()) {
String key = keys.next();
Object value = allJsonObjects.get(key);
play.Logger.debug(" key=" + key + " ,value=" + value.toString()
+ " ,artOfObject=" + value.getClass().getName());
if (value.toString().contains("prefLabel")) {
play.Logger.debug("value contains prefLabel");
if (value instanceof JSONObject) {
play.Logger.debug("value instanceof JSONObject");
jsObject = allJsonObjects.getJSONObject(key);
play.Logger.debug("jsObject=" + jsObject.toString());
oldPrefLabel = jsObject.get("prefLabel");
if (oldPrefLabel.toString().contains("http")
&& !oldPrefLabel.toString().contains("www.openstreetmap.org")) {
play.Logger.debug("oldPrefLabel=" + oldPrefLabel.toString());
String newPrefLabel =
MyEtikettMaker.getLabelFromEtikettWs(oldPrefLabel.toString());
play.Logger.debug("newPrefLabel=" + newPrefLabel);
jsObject.put("prefLabel", newPrefLabel);
}
} else if (value instanceof JSONArray) {
play.Logger.debug("value instanceof JSONArray");
JSONArray jsArray = allJsonObjects.getJSONArray(key);
play.Logger.debug("jsArray=" + jsArray.toString());
for (int j = 0; j < jsArray.length(); j++) {
jsObject = jsArray.getJSONObject(j);
oldPrefLabel = jsObject.get("prefLabel");
if (oldPrefLabel.toString().contains("http") && !oldPrefLabel
.toString().contains("www.openstreetmap.org")) {
play.Logger.debug("oldPrefLabel=" + oldPrefLabel.toString());
String newPrefLabel = MyEtikettMaker
.getLabelFromEtikettWs(oldPrefLabel.toString());
play.Logger.debug("newPrefLabel=" + newPrefLabel);
jsObject.put("prefLabel", newPrefLabel);
}
}
}

}

}

} catch (JSONException e) {
// Behandlung der JSONException
e.printStackTrace();
}

return allJsonObjects;

}

}
Loading

0 comments on commit 6c22748

Please sign in to comment.