Skip to content

Commit

Permalink
Merge pull request #427 from hbz/persistKtblDatastreamByMaster
Browse files Browse the repository at this point in the history
Persist ktbl datastream by master
  • Loading branch information
inkuss authored Nov 28, 2023
2 parents 6c22748 + 3704914 commit d598598
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 65 deletions.
2 changes: 1 addition & 1 deletion app/actions/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public String deleteMetadata2Field(String pid, String field) {
RDFFormat.NTRIPLES);
Collection<Statement> myGraph =
RdfUtils.deletePredicateFromRepo(rdfRepo, pred);
return new Modify().updateMetadata2(node,
return new Modify().updateMetadata("metadata2", node,
RdfUtils.graphToString(myGraph, RDFFormat.NTRIPLES));
}
}
2 changes: 1 addition & 1 deletion app/actions/Enrich.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static String enrichMetadata2(Node node) {
List<Statement> enrichStatements = new ArrayList<>();
enrichAll(node, metadata, enrichStatements);
metadata = RdfUtils.replaceTriples(enrichStatements, metadata);
new Modify().updateMetadata2(node, metadata);
new Modify().updateMetadata("metadata2", node, metadata);
} catch (Exception e) {
play.Logger.debug("", e);
return "Enrichment of " + node.getPid() + " partially failed !\n"
Expand Down
78 changes: 42 additions & 36 deletions app/actions/Modify.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ public String updateLobidifyAndEnrichMetadata(Node node, String content) {
String alephid =
lobidUri.replaceFirst("http://lobid.org/resource[s]*/", "");
content = getLobid2DataAsNtripleString(node, alephid);
updateMetadata2(node, content);
updateMetadata("metadata2", node, content);

String enrichMessage2 = Enrich.enrichMetadata2(node);
return pid + " metadata successfully updated, lobidified and enriched! "
+ enrichMessage2;
} else {
updateMetadata2(node, content);
updateMetadata("metadata2", node, content);
String enrichMessage2 = Enrich.enrichMetadata2(node);
return pid + " metadata successfully updated, and enriched! "
+ enrichMessage2;
Expand Down Expand Up @@ -272,13 +272,13 @@ public String updateLobidify2AndEnrichMetadata(Node node, String content) {
lobidUri.replaceFirst("http://lobid.org/resource[s]*/", "");
alephid = alephid.replaceAll("#.*", "");
content = getLobid2DataAsNtripleString(node, alephid);
updateMetadata2(node, content);
updateMetadata("metadata2", node, content);

String enrichMessage = Enrich.enrichMetadata2(node);
return pid + " metadata successfully updated, lobidified and enriched! "
+ enrichMessage;
} else {
updateMetadata2(node, content);
updateMetadata("metadata2", node, content);
String enrichMessage = Enrich.enrichMetadata2(node);
return pid + " metadata successfully updated, and enriched! "
+ enrichMessage;
Expand Down Expand Up @@ -312,7 +312,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!");
updateMetadata2(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 +359,7 @@ public String updateLobidify2AndEnrichMetadataIfRecentlyUpdated(Node node,
try {
content = getLobid2DataAsNtripleStringIfResourceHasRecentlyChanged(node,
alephid, date);
updateMetadata2(node, content);
updateMetadata("metadata2", node, content);
msg.append(Enrich.enrichMetadata2(node));
} catch (NotUpdatedException e) {
play.Logger.debug("", e);
Expand Down Expand Up @@ -1104,40 +1104,41 @@ public String lobidify1(Node node, String alephid) {
}

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

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);
}
}
// 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 =
Expand Down Expand Up @@ -1232,6 +1233,11 @@ public String updateMetadata(String metadataType, Node node, String content) {
+ " This action is not supported."
+ " Use HTTP DELETE instead.\n");
}

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

File file = CopyUtils.copyStringToFile(content);
play.Logger
.debug("content.file.getAbsolutePath():" + file.getAbsolutePath());
Expand Down
18 changes: 14 additions & 4 deletions app/archive/fedora/FedoraFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,16 +397,26 @@ public void updateNode(Node node) {
}
play.Logger.debug("Updated stream");
}
//
// if (node.getMetadataFile("metadata") != null) {
// utils.updateMetadataStream(node, "metadata");
// }

if (node.getMetadataFile("metadata") != null) {
utils.updateMetadataStream(node, "metadata");
if (node.getMetadataFile("metadata2") != null) {
utils.updateMetadata2Stream(node);
}

if (node.getMetadataFile("toscience") != null) {
utils.updateMetadataJsonStream(node);
}
if (node.getMetadata2File() != null) {
utils.updateMetadata2Stream(node);

if (node.getMetadataFile("ktbl") != null) {
utils.updateMetadataKtblStream(node);
}

// if (node.getMetadata2File() != null) {
// utils.updateMetadata2Stream(node);
// }
if (node.getSeqFile() != null) {
utils.updateSeqStream(node);
}
Expand Down
43 changes: 27 additions & 16 deletions app/archive/fedora/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -520,22 +520,28 @@ void updateMetadataStream(Node node, String metadataType) {
}

void updateMetadata2Stream(Node node) {
try {
File file = new File(node.getMetadata2File());
if (dataStreamExists(node.getPid(), "metadata2")) {
new ModifyDatastream(node.getPid(), "metadata2").versionable(true)
.dsLabel("n-triple rdf metadata2").dsState("A").controlGroup("M")
.mimeType("text/plain").content(file).execute();
} else {
new AddDatastream(node.getPid(), "metadata2").versionable(true)
.dsState("A").dsLabel("n-triple rdf metadata2").controlGroup("M")
.mimeType("text/plain").content(file).execute();
}
} catch (FedoraClientException e) {
throw new HttpArchiveException(e.getStatus(), e);
}
updateMetadataStream(archive.fedora.Vocabulary.metadata2, "text/plain",
"n-triple rdf metadata", node);
}

//
// void updateMetadata2Stream(Node node) {
// try {
// File file = new File(node.getMetadata2File());
// if (dataStreamExists(node.getPid(), "metadata2")) {
// new ModifyDatastream(node.getPid(), "metadata2").versionable(true)
// .dsLabel("n-triple rdf metadata2").dsState("A").controlGroup("M")
// .mimeType("text/plain").content(file).execute();
// } else {
// new AddDatastream(node.getPid(), "metadata2").versionable(true)
// .dsState("A").dsLabel("n-triple rdf metadata2").controlGroup("M")
// .mimeType("text/plain").content(file).execute();
// }
// } catch (FedoraClientException e) {
// throw new HttpArchiveException(e.getStatus(), e);
// }
// }

void readRelsExt(Node node) throws FedoraClientException {
FedoraResponse response =
new GetDatastreamDissemination(node.getPid(), "RELS-EXT").download(true)
Expand Down Expand Up @@ -991,8 +997,13 @@ private Transformer readTransformer(String prefixedPid) {
}

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

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

public void updateMetadataStream(String metadataType, String mimeType,
Expand Down
3 changes: 2 additions & 1 deletion app/archive/fedora/Vocabulary.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public abstract class Vocabulary {

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

}
66 changes: 63 additions & 3 deletions app/controllers/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@

import actions.BulkAction;
import actions.Enrich;
import actions.Read;
import archive.fedora.RdfUtils;
import authenticate.BasicAuth;
import helper.HttpArchiveException;
import helper.KTBLMapperHelper;
import helper.RdfHelper;
import helper.ToscienceHelper;
import helper.WebgatherUtils;
Expand Down Expand Up @@ -456,7 +458,6 @@ public static Promise<Result> updateMetadata(@PathParam("pid") String pid) {

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

// ******************************
RDFFormat format = RDFFormat.NTRIPLES;
Map<String, Object> rdf = RdfHelper.getRdfAsMap(readNode, format,
request().body().asText());
Expand All @@ -470,8 +471,6 @@ public static Promise<Result> updateMetadata(@PathParam("pid") String pid) {

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

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

modify.updateMetadataJson(readNode, toscienceJson.toString());
play.Logger
.debug("tosciecne from Node" + readNode.getMetadata("toscience"));
Expand Down Expand Up @@ -544,6 +543,67 @@ public static Promise<Result> updateDeepGreen(@PathParam("pid") String pid,
});
}

@ApiOperation(produces = "application/json", nickname = "updateKtbl", value = "updateKtbl", notes = "Updates the ktbl datastream of a resource.", response = Message.class, httpMethod = "PUT")
@ApiImplicitParams({
@ApiImplicitParam(name = "data", value = "data", dataType = "file", required = true, paramType = "body") })
public static Promise<Result> updateKtbl(@PathParam("pid") String pid) {
return new ModifyAction().call(pid, node -> {
try {

play.Logger.debug("Starting KTBL Mapping");

Node readNode = new Read().readNode(pid);

MultipartFormData body = request().body().asMultipartFormData();
FilePart data = body.getFile("data");

if (data == null) {
return (Result) JsonMessage(new Message("Missing File.", 400));
}

/**
* 1.KTBL(Json)***************************************
*/
String contentOfFile =
KTBLMapperHelper.getStringContentFromJsonFile(data);
play.Logger.debug("contentOfFile=" + contentOfFile);

String ktblMetadata =
KTBLMapperHelper.getToPersistKtblMetadata(contentOfFile);
play.Logger.debug("ktblMetadata=" + ktblMetadata);

String result1 = modify.updateMetadata("ktbl", readNode, ktblMetadata);

play.Logger.debug("Done KTBL Mapping");

/**
* 2. TOSCIENCE(Json)***************************************
*/

// String result2 =
// modify.updateMetadata("toscience", readNode, ktblMetadata);

/**
* 3. METADATA2(rdf)***************************************
*/
// JSONObject ktblJson = new JSONObject(ktblMetadata);
// Map<String, Object> rdf =
// KTBLMapperHelper.getMapFromJSONObject(ktblJson);
//
// String contentRewrite = modify.rewriteContent(rdf.toString(), pid);
//
// String result3 =
// modify.updateMetadata("metadata2", readNode, contentRewrite);

Globals.fedora.updateNode(readNode);

return JsonMessage(new Message(result1));
} catch (Exception e) {
throw new HttpArchiveException(500, e);
}
});
}

@ApiOperation(produces = "application/json", nickname = "updateData", value = "updateData", notes = "Updates the data of a resource", response = Message.class, httpMethod = "PUT")
@ApiImplicitParams({
@ApiImplicitParam(name = "data", value = "data", dataType = "file", required = true, paramType = "body") })
Expand Down
Loading

0 comments on commit d598598

Please sign in to comment.