diff --git a/app/actions/Delete.java b/app/actions/Delete.java index 24668707..a22b1c37 100644 --- a/app/actions/Delete.java +++ b/app/actions/Delete.java @@ -173,7 +173,7 @@ public String deleteMetadata2Field(String pid, String field) { RDFFormat.NTRIPLES); Collection myGraph = RdfUtils.deletePredicateFromRepo(rdfRepo, pred); - return new Modify().updateMetadata2(node, + return new Modify().updateMetadata("metadata2", node, RdfUtils.graphToString(myGraph, RDFFormat.NTRIPLES)); } } diff --git a/app/actions/Enrich.java b/app/actions/Enrich.java index 72f1537f..3d29c3e5 100644 --- a/app/actions/Enrich.java +++ b/app/actions/Enrich.java @@ -62,7 +62,7 @@ public static String enrichMetadata2(Node node) { List 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" diff --git a/app/actions/Modify.java b/app/actions/Modify.java index c076caf2..395d0c97 100644 --- a/app/actions/Modify.java +++ b/app/actions/Modify.java @@ -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; @@ -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; @@ -312,7 +312,7 @@ public String updateLobidify2AndEnrichDeepGreenData(Node node, Map 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); @@ -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); @@ -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 = @@ -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()); diff --git a/app/archive/fedora/FedoraFacade.java b/app/archive/fedora/FedoraFacade.java index 724b451b..c3158b30 100644 --- a/app/archive/fedora/FedoraFacade.java +++ b/app/archive/fedora/FedoraFacade.java @@ -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); } diff --git a/app/archive/fedora/Utils.java b/app/archive/fedora/Utils.java index 156ab82e..1e863f76 100644 --- a/app/archive/fedora/Utils.java +++ b/app/archive/fedora/Utils.java @@ -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) @@ -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, diff --git a/app/archive/fedora/Vocabulary.java b/app/archive/fedora/Vocabulary.java index c60b0651..9f3442f2 100644 --- a/app/archive/fedora/Vocabulary.java +++ b/app/archive/fedora/Vocabulary.java @@ -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"; } diff --git a/app/controllers/Resource.java b/app/controllers/Resource.java index aa12dd40..57c76024 100644 --- a/app/controllers/Resource.java +++ b/app/controllers/Resource.java @@ -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; @@ -456,7 +458,6 @@ public static Promise updateMetadata(@PathParam("pid") String pid) { play.Logger.debug("toscienceJson will be mapped"); - // ****************************** RDFFormat format = RDFFormat.NTRIPLES; Map rdf = RdfHelper.getRdfAsMap(readNode, format, request().body().asText()); @@ -470,8 +471,6 @@ public static Promise 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")); @@ -544,6 +543,67 @@ public static Promise 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 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 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") }) diff --git a/app/helper/KTBLMapperHelper.java b/app/helper/KTBLMapperHelper.java new file mode 100644 index 00000000..d70ca177 --- /dev/null +++ b/app/helper/KTBLMapperHelper.java @@ -0,0 +1,114 @@ +package helper; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; +import java.io.FileNotFoundException; +import org.json.JSONObject; +import play.mvc.Http.MultipartFormData.FilePart; +import org.json.JSONException; +import java.io.IOException; + +/** + * + * @author adoud + * + */ +public class KTBLMapperHelper { + + /** + * This method gets the content of a FilePart(Json File) and returns it as a + * string + * + * @param fp + * @return the content of the file as a string + */ + static public String getStringContentFromJsonFile(FilePart fp) { + StringBuilder ktblMetadata = null; + BufferedReader br = null; + + try { + ktblMetadata = new StringBuilder(); + if (fp != null) { + File file = (File) fp.getFile(); + br = new BufferedReader(new FileReader(file)); + String line; + while ((line = br.readLine()) != null) { + ktblMetadata.append(line); + } + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException e) { + } + } + } + play.Logger.debug("ktblMetadata.toString()=" + ktblMetadata.toString()); + return ktblMetadata.toString(); + } + + /** + * The method gets the required KTBL metadata from the json file and returns + * it as a string + * + * @param allKtblMetadata + * @return required KTBL metadata, which must be persisted + */ + static public String getToPersistKtblMetadata(String contentJsFile) { + + JSONObject wantedKtblMetadata = null; + JSONObject allKtblMetadata = null; + try { + allKtblMetadata = new JSONObject(contentJsFile); + wantedKtblMetadata = new JSONObject(); + + wantedKtblMetadata.put("recordingPeriod", + allKtblMetadata.getJSONArray("recordingPeriod")); + wantedKtblMetadata.put("relatedDatasets", + allKtblMetadata.getJSONArray("relatedDatasets")); + wantedKtblMetadata.put("info", allKtblMetadata.get("info")); + + } catch (JSONException e) { + play.Logger.debug("JSONException:getToPersistKtblMetadata()"); + } + + return wantedKtblMetadata.toString(); + } + + /** + * This method converts a JSONObject into a Map + * + * @param jsonObject + * @return + */ + + public static Map getMapFromJSONObject( + JSONObject jsonObject) { + Map map = null; + try { + map = new LinkedHashMap<>(); + Iterator keys = jsonObject.keys(); + while (keys.hasNext()) { + String key = keys.next(); + Object value = jsonObject.get(key); + map.put(key, value); + } + + } catch (JSONException e) { + play.Logger.debug("JSONException:getMapFromJSONObject()"); + } + + return map; + } + +} diff --git a/app/models/Node.java b/app/models/Node.java index cb6c45d3..361465da 100644 --- a/app/models/Node.java +++ b/app/models/Node.java @@ -325,9 +325,9 @@ public String getMetadata2File() { /** * @param metadataFile The absolutepath to the metadatafile */ - public void setMetadata2File(String metadataFile) { - this.metadata2File = metadataFile; - } + // public void setMetadata2File(String metadataFile) { + // this.metadata2File = metadataFile; + // } /** * The metadata file