diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/MainController.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/MainController.java index 9ecc1cc8..749082ad 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/MainController.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/MainController.java @@ -3,6 +3,7 @@ package eu.dnetlib.is; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; @@ -44,7 +45,7 @@ public class MainController { if (restype.isPresent() && restype.get().isSimple()) { map.addAttribute("type", restype.get()); } else { - map.addAttribute("type", new ResourceType("not_present", "???", 0)); + map.addAttribute("type", new ResourceType("not_present", "???", MediaType.TEXT_PLAIN_VALUE, 0)); } return "simpleResources"; } diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java index 36b756e1..9246b806 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java @@ -7,14 +7,15 @@ import javax.transaction.Transactional; import org.apache.commons.lang3.StringUtils; import org.dom4j.Document; +import org.dom4j.DocumentException; import org.dom4j.Node; import org.dom4j.io.SAXReader; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import eu.dnetlib.is.resource.model.SimpleResource; import eu.dnetlib.is.resource.repository.SimpleResourceRepository; +import eu.dnetlib.is.util.InformationServiceException; import eu.dnetlib.is.vocabulary.model.Synonym; import eu.dnetlib.is.vocabulary.model.Vocabulary; import eu.dnetlib.is.vocabulary.model.VocabularyTerm; @@ -34,47 +35,65 @@ public class OldProfilesImporter { private VocabularyTermRepository vocabularyTermRepository; @Transactional - public SimpleResource importSimpleResource(final String xml) throws Exception { + public SimpleResource importSimpleResource(final String xml) throws InformationServiceException { final SAXReader reader = new SAXReader(); - final Document doc = reader.read(new StringReader(xml)); - final String id = StringUtils.substringBefore(doc.valueOf("//RESOURCE_IDENTIFIER/@value"), "_"); - final Date now = new Date(); + try { + final Document doc = reader.read(new StringReader(xml)); - final SimpleResource res = new SimpleResource(); - res.setId(id); - res.setCreationDate(now); - res.setModificationDate(now); - res.setContentType(MediaType.APPLICATION_XML_VALUE); + final String id = StringUtils.substringBefore(doc.valueOf("//RESOURCE_IDENTIFIER/@value"), "_"); + final Date now = new Date(); - String resContent; - switch (doc.valueOf("//RESOURCE_TYPE/@value")) { - case "CleanerDSResourceType": - res.setType("cleaning_rule"); - res.setName(doc.valueOf("//CLEANER_NAME")); - res.setDescription(doc.valueOf("//CLEANER_DESCRIPTION")); - resContent = doc.selectSingleNode("//CLEANER_RULES").asXML(); - break; - case "TransformationRuleDSResourceType": - res.setType("transformation_rule"); - res.setName(doc.valueOf("//SCRIPT/TITLE")); - res.setDescription(""); - resContent = doc.selectSingleNode("//SCRIPT/CODE").asXML(); - break; - case "HadoopJobConfigurationDSResourceType": - res.setType("hadoop_job_configuration"); - res.setName(doc.valueOf("//HADOOP_JOB/@name")); - res.setDescription(doc.valueOf("//HADOOP_JOB/DESCRIPTION")); - resContent = doc.selectSingleNode("//HADOOP_JOB").asXML(); - break; - default: - throw new Exception("Invalid resource type: " + doc.valueOf("//RESOURCE_TYPE/@value")); + final SimpleResource res = new SimpleResource(); + res.setId(id); + res.setCreationDate(now); + res.setModificationDate(now); + + String resContent; + switch (doc.valueOf("//RESOURCE_TYPE/@value")) { + case "CleanerDSResourceType": + res.setType("cleaning_rule"); + res.setName(doc.valueOf("//CLEANER_NAME")); + res.setDescription(doc.valueOf("//CLEANER_DESCRIPTION")); + resContent = doc.selectSingleNode("//CLEANER_RULES").asXML(); + break; + case "TransformationRuleDSResourceType": + res.setName(doc.valueOf("//SCRIPT/TITLE")); + res.setDescription(""); + if (doc.selectNodes("//*[local-name() = 'stylesheet']").size() > 0) { + res.setType("transformation_rule_xslt"); + resContent = doc.selectSingleNode("//*[local-name() = 'stylesheet']").asXML(); + } else { + final String code = doc.valueOf("//SCRIPT/CODE").trim(); + try { + final Document xsltDoc = reader.read(new StringReader(code)); + res.setType("transformation_rule_xslt"); + resContent = xsltDoc.asXML(); + } catch (final DocumentException e) { + e.printStackTrace(); + + res.setType("transformation_rule_legacy"); + resContent = code; + } + } + break; + case "HadoopJobConfigurationDSResourceType": + res.setType("hadoop_job_configuration"); + res.setName(doc.valueOf("//HADOOP_JOB/@name")); + res.setDescription(doc.valueOf("//HADOOP_JOB/DESCRIPTION")); + resContent = doc.selectSingleNode("//HADOOP_JOB").asXML(); + break; + default: + throw new InformationServiceException("Invalid resource type: " + doc.valueOf("//RESOURCE_TYPE/@value")); + } + + simpleResourceRepository.save(res); + simpleResourceRepository.setContentById(id, resContent); + + return res; + } catch (final Exception e) { + throw new InformationServiceException("Error parsing file", e); } - - simpleResourceRepository.save(res); - simpleResourceRepository.setContentById(id, resContent); - - return res; } @Transactional diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java index 0a67108d..35bfc8c6 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java @@ -73,15 +73,14 @@ public class ResourcesRestController { @GetMapping("/{id}/content") public void getContent(@PathVariable final String id, final HttpServletResponse res) throws InformationServiceException { - final SimpleResource sr = simpleResourceRepository.findById(id).orElseThrow(() -> new InformationServiceException("Id not found")); + final String ctype = simpleResourceRepository.findContentTypeById(id).orElseThrow(() -> new InformationServiceException("Id not found")); + final String content = simpleResourceRepository.findContentById(id).orElseThrow(() -> new InformationServiceException("Id not found")); res.setCharacterEncoding(StandardCharsets.UTF_8.name()); - res.setContentType(sr.getContentType()); - - simpleResourceRepository.getContentById(id); + res.setContentType(ctype); try { - IOUtils.write(simpleResourceRepository.getContentById(id), res.getOutputStream(), StandardCharsets.UTF_8.name()); + IOUtils.write(content, res.getOutputStream(), StandardCharsets.UTF_8.name()); } catch (final IOException e) { throw new InformationServiceException("Error retrieving content", e); } diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/DatabaseUtils.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/DatabaseUtils.java index 5e0e41cb..d49493e0 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/DatabaseUtils.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/DatabaseUtils.java @@ -30,7 +30,6 @@ public class DatabaseUtils { res.setName(name); res.setType(type); res.setDescription(description); - res.setContentType(ctype); res.setCreationDate(now); res.setModificationDate(now); diff --git a/apps/dnet-is-application/src/main/resources/templates/simpleResources.html b/apps/dnet-is-application/src/main/resources/templates/simpleResources.html index 5fd61769..a799e3cb 100644 --- a/apps/dnet-is-application/src/main/resources/templates/simpleResources.html +++ b/apps/dnet-is-application/src/main/resources/templates/simpleResources.html @@ -25,7 +25,7 @@
- {{r.contentType}} +
{{r.name}}

{{r.description}}

@@ -114,7 +114,7 @@