diff --git a/src/main/java/de/nrw/hbz/edm/impl/AggregationElementOperator.java b/src/main/java/de/nrw/hbz/edm/impl/AggregationElementOperator.java index fedd8a3..5aa4db4 100644 --- a/src/main/java/de/nrw/hbz/edm/impl/AggregationElementOperator.java +++ b/src/main/java/de/nrw/hbz/edm/impl/AggregationElementOperator.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.Hashtable; +import java.io.File; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -25,8 +26,6 @@ public class AggregationElementOperator { //TODO: Implement all operations on the data you wish to have ;-) final static Logger logger = LogManager.getLogger(AggregationElementOperator.class); - - private EdmImpl edmImpl = new EdmImpl(); private Rdf rdf = null; @@ -43,8 +42,7 @@ public AggregationElementOperator(Rdf rdf) { * @param filePath */ public AggregationElementOperator(String filePath) { - this.edmImpl.setFilePath(filePath); - rdf = edmImpl.deserializeXml(); + rdf = EdmProvider.deserialize(new File(filePath)); } /** diff --git a/src/main/java/de/nrw/hbz/edm/impl/ConsoleImpl.java b/src/main/java/de/nrw/hbz/edm/impl/ConsoleImpl.java index 347dda3..682532f 100644 --- a/src/main/java/de/nrw/hbz/edm/impl/ConsoleImpl.java +++ b/src/main/java/de/nrw/hbz/edm/impl/ConsoleImpl.java @@ -4,6 +4,7 @@ package de.nrw.hbz.edm.impl; import java.util.ArrayList; +import java.io.File; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -28,7 +29,7 @@ public class ConsoleImpl { /** * @param args */ - String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "src/main/resources/OaiPmh.xml"; + String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "src/test/resources/OaiPmh.xml"; //static String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "src/main/resources/OAIBase.xml"; @@ -52,9 +53,9 @@ public static void main(String[] args) { // logger.debug(resultOaiPmh.getOaiMethod().getRecord().get(0).getMetadata().getRdf().getProvidedCho()); //impl.serializeXml(resultOaiPmh); - conImpl.filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "src/main/resources/ExampleSip/EDM.xml"; - EdmImpl edmImpl = new EdmImpl(conImpl.filePath); - Rdf resultEdm = edmImpl.deserializeXml(); + conImpl.filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "src/test/resources/ExampleSip/EDM.xml"; + // EdmImpl edmImpl = new EdmImpl(conImpl.filePath); + Rdf resultEdm = EdmProvider.deserialize(new File(conImpl.filePath)); logger.debug(resultOaiPmh.getOaiMethod().getRecord().get(0).getMetadata().getRdf().getProvidedCho()); // edmImpl.serializeXml(resultEdm); diff --git a/src/main/java/de/nrw/hbz/edm/impl/DeserializeEdmXml.java b/src/main/java/de/nrw/hbz/edm/impl/DeserializeEdmXml.java deleted file mode 100644 index cee0163..0000000 --- a/src/main/java/de/nrw/hbz/edm/impl/DeserializeEdmXml.java +++ /dev/null @@ -1,90 +0,0 @@ -/** - * - */ -package de.nrw.hbz.edm.impl; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; - -import com.fasterxml.jackson.core.exc.StreamReadException; -import com.fasterxml.jackson.databind.DatabindException; -import com.fasterxml.jackson.dataformat.xml.XmlMapper; - -import de.nrw.hbz.edm.model.Rdf; -import de.nrw.hbz.edm.model.deserialize.DeserializeRdf; - -/** - * - */ -public class DeserializeEdmXml { - - private Rdf rdf = new DeserializeRdf(); - private File importFile = null; - private InputStream xmlIs = null; - - public DeserializeEdmXml(File file) { - this.importFile = file; - xmlIs = loadXml(); - } - - public DeserializeEdmXml(String fileName) { - importFile = new File(fileName); - xmlIs = loadXml(); - } - - private InputStream loadXml() { - BufferedInputStream bis = null; - try { - FileInputStream fis = new FileInputStream(importFile); - bis = new BufferedInputStream(fis); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - - return bis; - } - - public Rdf deserialize() { - DeserializeRdf rdf = null; - XmlMapper xmlMapper = new XmlMapper(); - try { - rdf = xmlMapper.readValue(xmlIs, DeserializeRdf.class); - - } catch (StreamReadException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (DatabindException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - - return rdf; - } - - - - /** - * @return the oaiPmh - */ - public Rdf getEdm() { - return rdf; - } - - - /** - * @param oaiPmh the oaiPmh to set - */ - public void setEdm(Rdf rdf) { - this.rdf = rdf; - } - - -} diff --git a/src/main/java/de/nrw/hbz/edm/impl/EdmImpl.java b/src/main/java/de/nrw/hbz/edm/impl/EdmImpl.java index f9e6621..5b204e0 100644 --- a/src/main/java/de/nrw/hbz/edm/impl/EdmImpl.java +++ b/src/main/java/de/nrw/hbz/edm/impl/EdmImpl.java @@ -3,6 +3,8 @@ */ package de.nrw.hbz.edm.impl; +import java.io.File; + import de.nrw.hbz.edm.model.Rdf; /** @@ -25,8 +27,7 @@ public EdmImpl(String filePath) { * @return Rdf */ public Rdf deserializeXml() { - DeserializeEdmXml dsXml = new DeserializeEdmXml(filePath); - return dsXml.deserialize(); + return EdmProvider.deserialize(new File(filePath)); } diff --git a/src/main/java/de/nrw/hbz/edm/impl/EdmProvider.java b/src/main/java/de/nrw/hbz/edm/impl/EdmProvider.java new file mode 100644 index 0000000..ea4d36b --- /dev/null +++ b/src/main/java/de/nrw/hbz/edm/impl/EdmProvider.java @@ -0,0 +1,192 @@ +/** + * + */ +package de.nrw.hbz.edm.impl; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.exc.StreamReadException; +import com.fasterxml.jackson.databind.DatabindException; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; + +import de.nrw.hbz.edm.model.Rdf; +import de.nrw.hbz.edm.model.deserialize.DeserializeRdf; +import de.nrw.hbz.edm.model.serialize.SerializeRdf; + +/** + * A class to generate appropriate Pojo-representations from serialized EDM (Europeana Data Model) metadata + * Makes use of the jackson-Framework + */ +public class EdmProvider { + + final static Logger logger = LogManager.getLogger(EdmProvider.class); + + private Rdf rdf = new DeserializeRdf(); + private static InputStream xmlIs = null; + + /** + * Constructor takes serialized EDM as File Object + * @param file + */ + public EdmProvider(File file) { + xmlIs = loadXml(file); + } + + /** + * Constructor takes serialized EDM as String + * @param rdfString + */ + public EdmProvider(String rdfString) { + xmlIs = loadXmlString(rdfString); + } + + /** + * Constructor takes serialized EDM as InputStream + * @param rdfIs + */ + public EdmProvider(InputStream rdfIs) { + //importFile = new File(fileName); + xmlIs = rdfIs; + } + + /** + * loads File content into InputStream + * @return InputStream representing EDM metadata + */ + private static InputStream loadXml(File file) { + BufferedInputStream bis = null; + try { + FileInputStream fis = new FileInputStream(file); + bis = new BufferedInputStream(fis); + } catch (FileNotFoundException e) { + logger.error(e.getMessage()); + } + + return bis; + } + + /** + * @param rdf EDM metadata as String representation + * @return InputStream representing EDM metadata + */ + private static InputStream loadXmlString(String rdfString) { + + BufferedInputStream bis = null; + ByteArrayInputStream baif = new ByteArrayInputStream(rdfString.getBytes()); + bis = new BufferedInputStream(baif); + + return bis; + } + + /** + * method takes serialized EDM as InputStream + * @param is InputStream to be used + * @return EDM as Pojos according to jackson-Framework + */ + public static Rdf deserialize(InputStream is) { + DeserializeRdf rdf = null; + xmlIs = is; + XmlMapper xmlMapper = new XmlMapper(); + try { + rdf = xmlMapper.readValue(xmlIs, DeserializeRdf.class); + + } catch (StreamReadException e) { + logger.error(e.getMessage()); + } catch (DatabindException e) { + logger.error(e.getMessage()); + } catch (IOException e) { + logger.error(e.getMessage()); + } + + + return rdf; + } + + /** + * method takes serialized EDM as File object + * @param file File to be used + * @return EDM as Pojos according to jackson-Framework + */ + public static Rdf deserialize(File file) { + DeserializeRdf rdf = null; + xmlIs = loadXml(file); + XmlMapper xmlMapper = new XmlMapper(); + try { + rdf = xmlMapper.readValue(xmlIs, DeserializeRdf.class); + + } catch (StreamReadException e) { + logger.error(e.getMessage()); + } catch (DatabindException e) { + logger.error(e.getMessage()); + } catch (IOException e) { + logger.error(e.getMessage()); + } + + + return rdf; + } + + /** + * method takes serialized EDM as String + * @param edmString the String used to deserialize + * @return EDM as Pojos according to jackson-Framework + */ + public static Rdf deserialize(String edmString) { + DeserializeRdf rdf = null; + xmlIs = loadXmlString(edmString); + XmlMapper xmlMapper = new XmlMapper(); + try { + rdf = xmlMapper.readValue(xmlIs, DeserializeRdf.class); + + } catch (StreamReadException e) { + logger.error(e.getMessage()); + } catch (DatabindException e) { + logger.error(e.getMessage()); + } catch (IOException e) { + logger.error(e.getMessage()); + } + return rdf; + } + + public static String serialize(Rdf edm) { + XmlMapper xmlMapper = new XmlMapper(); + String xml = null; + try { + xmlMapper.writerFor(SerializeRdf.class); + xml = xmlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(edm); + + } catch (JsonProcessingException e) { + logger.error("Failed to serialize EDM Object: " + edm.toString()); + } + return xml; + } + + /** + * @return the oaiPmh + */ + public Rdf getEdm() { + return rdf; + } + + + /** + * @param oaiPmh the oaiPmh to set + */ + public void setEdm(Rdf rdf) { + this.rdf = rdf; + } + + +} diff --git a/src/main/java/de/nrw/hbz/edm/impl/HtmlProvider.java b/src/main/java/de/nrw/hbz/edm/impl/HtmlProvider.java new file mode 100644 index 0000000..69b648e --- /dev/null +++ b/src/main/java/de/nrw/hbz/edm/impl/HtmlProvider.java @@ -0,0 +1,178 @@ +/** + * + */ +package de.nrw.hbz.edm.impl; + +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Set; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import de.nrw.hbz.edm.model.Aggregation; +import de.nrw.hbz.edm.model.Rdf; +import de.nrw.hbz.edm.model.deserialize.DeserializeAggregation; +import de.nrw.hbz.edm.model.deserialize.DeserializeResourceAttribute; + +/** + * + */ +public class HtmlProvider { + + final static Logger logger = LogManager.getLogger(HtmlProvider.class); + + private Rdf rdf = null; + private String filePath = System.getProperty("user.dir") + + System.getProperty("file.separator") + "src/main/resources/presentation-template.html"; + + private String htmlHead = "\n" + + "\nDA.NRW Item Presentation\n" + + "" + + "\n" + + ""; + private String htmlFoot = "\n"; + private StringBuffer htmlText = new StringBuffer(); + + public HtmlProvider() { + appendHead(); + appendMD(); + appendFoot(); + } + + public HtmlProvider(Rdf rdf) { + this.rdf = rdf; + readTemplate(); + appendHead(); + appendMD(); + appendContent(); + appendFoot(); + } + + + + private void readTemplate() { + File template = new File(filePath); + StringBuffer head = new StringBuffer(); + try { + FileReader is = new FileReader(template); + BufferedReader bis = new BufferedReader(is); + String line = null; + while((line = bis.readLine()) != null) { + head.append(line + "\n"); + } + htmlHead = head.toString(); + } catch (Exception e) { + logger.error(e.getMessage()); + } + } + + private void appendHead() { + htmlText.append(htmlHead); + + } + + private void appendMD() { + + htmlText.append("

" + rdf.getProvidedCho().getDcTitle().get(0) + "

\n"); + + LinkedHashMap> metadata = new LinkedHashMap<>(); + metadata.put("Beschreibung: ", rdf.getProvidedCho().getDcDescription()); + metadata.put("Personen: ", rdf.getProvidedCho().getDcCreator()); + metadata.put("Beteiligte: ", rdf.getProvidedCho().getDcContributor()); + metadata.put("Datum: ", rdf.getProvidedCho().getDcDate()); + metadata.put("Sprache: ", rdf.getProvidedCho().getDcLanguage()); + metadata.put("Umfang, Besonderheit: ", rdf.getProvidedCho().getDctermsExtent()); + + Set metaData = metadata.keySet(); + + Iterator mdEnum = metaData.iterator(); + + while(mdEnum.hasNext()) { + String key = mdEnum.next(); + ArrayList values = metadata.get(key); + Iterator valIt = values.iterator(); + while(valIt.hasNext()) { + htmlText.append("
" + key + valIt.next() + "
\n"); + + } + } + htmlText.append("
\n"); + + // htmlText.append("

" + rdf.getProvidedCho().getDcDescription().get(0) + "

\n"); + // htmlText.append("

" + rdf.getProvidedCho().getDcPublisher().get(0) + "

\n"); + } + + private void appendContent() { + htmlText.append("
\n
    \n"); + + for(int i = 0; i < rdf.getAggregation().size(); i++) { + htmlText.append("
  • Bestandteil " + (i+1) + "
  • \n"); + } + htmlText.append("
\n"); + } + + private void appendFoot() { + htmlText.append(htmlFoot); + } + + public File toTempFile() { + File tmpFile = null; + BufferedOutputStream bos = null; + try { + tmpFile = File.createTempFile("html-", ".html"); + FileOutputStream fos = new FileOutputStream(tmpFile); + bos = new BufferedOutputStream(fos); + bos.write(htmlText.toString().getBytes("UTF-8")); + bos.flush(); + bos.close(); + return tmpFile; + } catch (IOException e) { + e.printStackTrace(); + } finally { + tmpFile.deleteOnExit(); + } + return null; + } + + public Rdf appendHtmlAggregation(String dsUrl) { + int i = rdf.getAggregation().size(); + Aggregation aggregation = new DeserializeAggregation(); + aggregation.setEdmAggregatedCHO(new DeserializeResourceAttribute("SplashPage")); + aggregation.setEdmIsShownBy(new DeserializeResourceAttribute(dsUrl)); + aggregation.setEdmObject(new DeserializeResourceAttribute(dsUrl)); + aggregation.setEdmAboutCHOResource("SplashPage"); + aggregation.setEdmDataProvider(rdf.getAggregation().get(0).getEdmDataProvider()); + aggregation.setEdmProvider(rdf.getAggregation().get(0).getEdmProvider()); + aggregation.setEdmRights(new DeserializeResourceAttribute("http://creativecommons.org/publicdomain/zero/1.0")); + rdf.addAggregation(aggregation); + + return rdf; + } + + @Override + public String toString() { + return htmlText.toString(); + } +} diff --git a/src/main/java/de/nrw/hbz/genericSipLoader/impl/DipsLoaderImpl.java b/src/main/java/de/nrw/hbz/genericSipLoader/impl/DipsLoaderImpl.java index b94af8a..42d6052 100644 --- a/src/main/java/de/nrw/hbz/genericSipLoader/impl/DipsLoaderImpl.java +++ b/src/main/java/de/nrw/hbz/genericSipLoader/impl/DipsLoaderImpl.java @@ -20,10 +20,13 @@ import org.apache.logging.log4j.Logger; import de.nrw.hbz.edm.impl.AggregationElementOperator; +import de.nrw.hbz.edm.impl.EdmProvider; +import de.nrw.hbz.edm.impl.HtmlProvider; import de.nrw.hbz.edm.impl.EdmImpl; import de.nrw.hbz.edm.impl.QdcProvider; import de.nrw.hbz.edm.model.ProvidedCHO; import de.nrw.hbz.edm.model.Rdf; +import de.nrw.hbz.edm.model.serialize.SerializeAggregation; import de.nrw.hbz.genericSipLoader.restClient.Fedora38Client; import de.nrw.hbz.genericSipLoader.util.FileScanner; import de.nrw.hbz.genericSipLoader.util.ZipExtractor; @@ -105,10 +108,9 @@ public void addPayLoadStream(String pid, int id, File file) { public void cuFedoraObject(Set fList) { Iterator fIt = fList.iterator(); - int i = 1; while(fIt.hasNext()) { Hashtable idReplacement = new Hashtable<>(); - // find correct sourceId + // find correct sourceId String fileName = fIt.next(); logger.debug(fileName); int index = fileName.lastIndexOf("/"); @@ -157,14 +159,17 @@ public void cuFedoraObject(Set fList) { String dsUrl = createDSUrl(pid, "DS" + id); idReplacement.put(plId, dsUrl); // logger.debug("Replace " + plId + " with " + idReplacement.get(plId)); - i = i-1; } logger.debug(pid); - + addMetadataStream(pid, "EDM_submitted.xml", new File(fileName)); - + + HtmlProvider htmlProv = new HtmlProvider(EdmProvider.deserialize(refactorEdm(idReplacement, fileName))); + addPayLoadStream(pid, id+1, htmlProv.toTempFile()); + String edmResult = EdmProvider.serialize(htmlProv.appendHtmlAggregation(createDSUrl(pid, "DS" + (id+1)))); + Hashtable xmlStreams = new Hashtable<>(); - xmlStreams.put("EDM.xml", refactorEdm(idReplacement, fileName)); + xmlStreams.put("EDM.xml", edmResult); xmlStreams.put("QDC.xml", createQDC(fileName)); Enumeration sEn = xmlStreams.keys(); @@ -182,7 +187,7 @@ public void cuFedoraObject(Set fList) { } catch (IOException e) { e.printStackTrace(); } finally { - tmpFile.delete(); + tmpFile.delete(); try { bos.close(); } catch (IOException e) { @@ -197,9 +202,7 @@ public void cuFedoraObject(Set fList) { private String createQDC(String edmFileName) { - // TODO: implement method for getting QDC - EdmImpl edmImpl = new EdmImpl(edmFileName); - Rdf rdf = edmImpl.deserializeXml(); + Rdf rdf = EdmProvider.deserialize(new File(edmFileName)); ProvidedCHO provCho = rdf.getProvidedCho(); QdcProvider qdcProvider = new QdcProvider(provCho); return qdcProvider.getQdc(); @@ -211,8 +214,7 @@ private String createQDC(String edmFileName) { * @return refactored EDM as String */ private String refactorEdm(Hashtable replacements, String edmFileName) { - EdmImpl edmImpl = new EdmImpl(edmFileName); - Rdf rdf = edmImpl.deserializeXml(); + Rdf rdf = EdmProvider.deserialize(new File(edmFileName)); AggregationElementOperator ago = new AggregationElementOperator(rdf); ago.replaceAllIsShownBy(replacements); return ago.toString(); diff --git a/src/main/resources/fedora-api.properties b/src/main/resources/fedora-api.properties index c2ad024..cb4a5a4 100644 --- a/src/main/resources/fedora-api.properties +++ b/src/main/resources/fedora-api.properties @@ -1,4 +1,4 @@ host=danrw-q-repo.hbz-nrw.de port=8080 protocol=http -namespace=danrw:202402151235- \ No newline at end of file +namespace=danrw:202402191235- \ No newline at end of file diff --git a/src/main/resources/presentation-template.html b/src/main/resources/presentation-template.html new file mode 100644 index 0000000..de7e2d1 --- /dev/null +++ b/src/main/resources/presentation-template.html @@ -0,0 +1,50 @@ + + +DA.NRW Item Presentation + + +
+
+
+ +