From 06bc9ce01744773960c15f4cf4b750bb342b5a41 Mon Sep 17 00:00:00 2001 From: Natasa Bulatovic Date: Fri, 22 Jan 2016 10:26:18 +0100 Subject: [PATCH] Fix for #387 Ingest metadata profile is not working --- .../ingest/controller/IngestController.java | 2 +- .../controller/IngestProfileController.java | 53 ++++++++++++++++--- .../mpg/imeji/logic/ingest/jaxb/JaxbUtil.java | 10 ++-- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/mpg/imeji/logic/ingest/controller/IngestController.java b/src/main/java/de/mpg/imeji/logic/ingest/controller/IngestController.java index b4a9f5135..8c185156e 100644 --- a/src/main/java/de/mpg/imeji/logic/ingest/controller/IngestController.java +++ b/src/main/java/de/mpg/imeji/logic/ingest/controller/IngestController.java @@ -39,7 +39,7 @@ public IngestController(User user, CollectionImeji collection) { public void ingest(File itemListXmlFile, File profileXmlFile) throws Exception { if (profileXmlFile != null) { IngestProfileController ipc = new IngestProfileController(user); - ipc.ingest(profileXmlFile, collection.getProfile()); + ipc.ingest(profileXmlFile, collection); } if (itemListXmlFile != null) { ProfileController pc = new ProfileController(); diff --git a/src/main/java/de/mpg/imeji/logic/ingest/controller/IngestProfileController.java b/src/main/java/de/mpg/imeji/logic/ingest/controller/IngestProfileController.java index 8006fe7a6..61ca1f040 100644 --- a/src/main/java/de/mpg/imeji/logic/ingest/controller/IngestProfileController.java +++ b/src/main/java/de/mpg/imeji/logic/ingest/controller/IngestProfileController.java @@ -2,11 +2,14 @@ import java.io.File; import java.net.URI; +import java.util.Date; import java.util.HashMap; +import de.mpg.imeji.logic.controller.CollectionController; import de.mpg.imeji.logic.controller.ProfileController; import de.mpg.imeji.logic.ingest.parser.ProfileParser; import de.mpg.imeji.logic.util.IdentifierUtil; +import de.mpg.imeji.logic.vo.CollectionImeji; import de.mpg.imeji.logic.vo.MetadataProfile; import de.mpg.imeji.logic.vo.Statement; import de.mpg.imeji.logic.vo.User; @@ -36,23 +39,50 @@ public IngestProfileController(User user) { * @param profileXmlFile * @throws Exception */ - public void ingest(File profileXmlFile, URI profile) throws Exception { + public void ingest(File profileXmlFile, CollectionImeji collection) throws Exception { + URI profile = collection.getProfile(); ProfileParser pp = new ProfileParser(); MetadataProfile mdp = pp.parse(profileXmlFile); - if (isCopyOfOther(mdp, profile)) { - changeStatementURI(mdp); - } + MetadataProfile original; ProfileController pc = new ProfileController(); - MetadataProfile original = pc.retrieve(profile, user); + if (profile != null ){ + if (isCopyOfOther(mdp, profile)) { + changeStatementURI(mdp); + } + original = pc.retrieve(profile, user); + } + else + { + original = new MetadataProfile(); + } + original.setStatements(mdp.getStatements()); // ingested profile can never be default profile original.setDefault(false); try { - pc.update(original, user); + if (profile != null ) { + pc.update(original, user); + pc.removeMetadataWithoutStatement(original); + } + else + { + if (mdp.getTitle()!= null && mdp.getTitle() != "" ) { + original.setTitle(mdp.getTitle()); + } + else + { + original.setTitle("Metadata profile for "+collection.getMetadata().getTitle()+" (ingested at: "+new Date(System.currentTimeMillis())+" )"); + } + original.setDescription(mdp.getDescription()); + mdp = pc.create(original, user); + CollectionController cc = new CollectionController(); + collection.setProfile(mdp.getId()); + cc.update(collection, user); + } } catch (Exception e) { throw new RuntimeException(e); } - pc.removeMetadataWithoutStatement(original); + } /** @@ -99,6 +129,13 @@ private MetadataProfile changeParentId(MetadataProfile mdp, HashMap id * @return */ private boolean isCopyOfOther(MetadataProfile mdp, URI uri) { - return uri.compareTo(mdp.getId()) != 0; + if ( mdp == null || uri ==null) { + return false; + } + else + { + return uri.compareTo(mdp.getId()) != 0; + } + } } diff --git a/src/main/java/de/mpg/imeji/logic/ingest/jaxb/JaxbUtil.java b/src/main/java/de/mpg/imeji/logic/ingest/jaxb/JaxbUtil.java index 7a67fc397..7344e49a3 100644 --- a/src/main/java/de/mpg/imeji/logic/ingest/jaxb/JaxbUtil.java +++ b/src/main/java/de/mpg/imeji/logic/ingest/jaxb/JaxbUtil.java @@ -25,7 +25,7 @@ public class JaxbUtil { public static T unmarshal(String xsdFilename, String xmlFilename, Class clss) throws JAXBException, SAXException { - SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.XMLNS_ATTRIBUTE_NS_URI); + SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = (xsdFilename == null || xsdFilename.trim().length() == 0) ? null : schemaFactory.newSchema(getFileURLInClasspath(xsdFilename)); JAXBContext jaxbContext = JAXBContext.newInstance(clss.getPackage().getName()); @@ -41,7 +41,7 @@ public static T unmarshal(JAXBContext jaxbContext, Schema schema, String xml public static T unmarshal(String xsdFilename, File xmlFile, Class clss) throws JAXBException, SAXException { - SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.XMLNS_ATTRIBUTE_NS_URI); + SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = (xsdFilename == null || xsdFilename.trim().length() == 0) ? null : schemaFactory.newSchema(getFileURLInClasspath(xsdFilename)); JAXBContext jaxbContext = JAXBContext.newInstance(clss.getPackage().getName()); @@ -54,10 +54,12 @@ public static T unmarshal(JAXBContext jaxbContext, Schema schema, File xmlFi unmarshaller.setSchema(schema); return clss.cast(unmarshaller.unmarshal(xmlFile)); } + + //be "http://www.w3.org/2000/xmlns/". public static void marshal(String xsdFilename, String xmlFilename, Object jaxbElement) throws JAXBException, SAXException, FileNotFoundException { - SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.XMLNS_ATTRIBUTE_NS_URI); + SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = (xsdFilename == null || xsdFilename.trim().length() == 0) ? null : schemaFactory.newSchema(getFileURLInClasspath(xsdFilename)); JAXBContext jaxbContext = @@ -76,7 +78,7 @@ public static void marshal(JAXBContext jaxbContext, Schema schema, String xmlFil public static void marshal(String xsdFilename, File xmlFile, Object jaxbElement) throws JAXBException, SAXException, FileNotFoundException { - SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.XMLNS_ATTRIBUTE_NS_URI); + SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = (xsdFilename == null || xsdFilename.trim().length() == 0) ? null : schemaFactory.newSchema(getFileURLInClasspath(xsdFilename)); JAXBContext jaxbContext =