From 4ff77964cab935e76fa3bfe58325025e0540c520 Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Wed, 23 Nov 2022 15:32:36 +0100 Subject: [PATCH] importers --- .../is/context/ContextRestController.java | 21 ------ .../ContextImporter.java | 2 +- .../is/importer/ImporterController.java | 73 +++++++++++++++++++ .../OldProfilesImporter.java | 2 +- .../{wfs => importer}/WfHistoryImporter.java | 2 +- .../is/resources/ResourcesRestController.java | 24 +++--- .../eu/dnetlib/is/util/DatabaseUtils.java | 42 +++++++++++ .../vocabulary/VocabularyRestController.java | 13 +--- .../is/wfs/WfHistoryRestController.java | 13 ---- 9 files changed, 133 insertions(+), 59 deletions(-) rename apps/dnet-is-application/src/main/java/eu/dnetlib/is/{context => importer}/ContextImporter.java (99%) create mode 100644 apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ImporterController.java rename apps/dnet-is-application/src/main/java/eu/dnetlib/is/{util => importer}/OldProfilesImporter.java (99%) rename apps/dnet-is-application/src/main/java/eu/dnetlib/is/{wfs => importer}/WfHistoryImporter.java (98%) create mode 100644 apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/DatabaseUtils.java diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/context/ContextRestController.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/context/ContextRestController.java index 72370a0b..50ba0b14 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/context/ContextRestController.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/context/ContextRestController.java @@ -1,22 +1,14 @@ package eu.dnetlib.is.context; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; -import javax.servlet.http.HttpServletRequest; - -import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.dom4j.DocumentException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -51,9 +43,6 @@ public class ContextRestController { @Autowired private ConceptLevel2Repository conceptLevel2Repository; - @Autowired - private ContextImporter contextImporter; - private static final Log log = LogFactory.getLog(ContextRestController.class); @GetMapping("/") @@ -128,14 +117,4 @@ public class ContextRestController { } } - @PostMapping(value = "/load", consumes = "text/plain") - public List loadFromOldProfile(final HttpServletRequest request) throws DocumentException, IOException { - - final String xml = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8); - - contextImporter.loadFromOldProfile(xml); - - return Arrays.asList("Done."); - } - } diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/context/ContextImporter.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ContextImporter.java similarity index 99% rename from apps/dnet-is-application/src/main/java/eu/dnetlib/is/context/ContextImporter.java rename to apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ContextImporter.java index ac6b5957..0b72604e 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/context/ContextImporter.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ContextImporter.java @@ -1,4 +1,4 @@ -package eu.dnetlib.is.context; +package eu.dnetlib.is.importer; import java.io.IOException; import java.io.StringReader; diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ImporterController.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ImporterController.java new file mode 100644 index 00000000..d0f515b6 --- /dev/null +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/ImporterController.java @@ -0,0 +1,73 @@ +package eu.dnetlib.is.importer; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.io.IOUtils; +import org.dom4j.DocumentException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import eu.dnetlib.is.resource.model.SimpleResource; +import eu.dnetlib.is.vocabulary.model.Vocabulary; + +@RestController +@RequestMapping("/api/import") +public class ImporterController { + + @Autowired + private ContextImporter contextImporter; + + @Autowired + private OldProfilesImporter oldProfilesImporter; + + @Autowired + private WfHistoryImporter wfHistoryImporter; + + @PostMapping(value = "/context", consumes = { + MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE + }) + public List importContext(final HttpServletRequest request) throws DocumentException, IOException { + + final String xml = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8); + + contextImporter.loadFromOldProfile(xml); + + return Arrays.asList("Done."); + + } + + @PostMapping(value = "/resource", consumes = { + MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE + }) + public SimpleResource importResource(final HttpServletRequest request) throws Exception { + final String xml = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8); + return oldProfilesImporter.importSimpleResource(xml); + } + + @PostMapping(value = "/vocabulary", consumes = { + MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE + }) + public Vocabulary importVocabulary(final HttpServletRequest request) throws Exception, IOException { + final String xml = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8); + return oldProfilesImporter.importVocabulary(xml); + } + + @GetMapping(value = "/wf_logs") + public List importWfLogs(@RequestParam final String path) throws Exception { + // mongoexport -d dnet_logs -c wf_logs --jsonArray -o /tmp/mongodump.json + + wfHistoryImporter.load(path); + + return Arrays.asList("Done."); + } +} diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/OldProfilesImporter.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java similarity index 99% rename from apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/OldProfilesImporter.java rename to apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java index 38c9b53d..36b756e1 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/OldProfilesImporter.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/OldProfilesImporter.java @@ -1,4 +1,4 @@ -package eu.dnetlib.is.util; +package eu.dnetlib.is.importer; import java.io.StringReader; import java.util.Date; diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/wfs/WfHistoryImporter.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/WfHistoryImporter.java similarity index 98% rename from apps/dnet-is-application/src/main/java/eu/dnetlib/is/wfs/WfHistoryImporter.java rename to apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/WfHistoryImporter.java index d25b9b79..fde3928e 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/wfs/WfHistoryImporter.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/WfHistoryImporter.java @@ -1,4 +1,4 @@ -package eu.dnetlib.is.wfs; +package eu.dnetlib.is.importer; import java.io.File; import java.util.Date; 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 51de8f75..0a67108d 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 @@ -26,18 +26,18 @@ import org.springframework.web.bind.annotation.RestController; import eu.dnetlib.is.resource.model.SimpleResource; import eu.dnetlib.is.resource.repository.SimpleResourceRepository; +import eu.dnetlib.is.util.DatabaseUtils; import eu.dnetlib.is.util.InformationServiceException; -import eu.dnetlib.is.util.OldProfilesImporter; @RestController @RequestMapping("/api/resources") public class ResourcesRestController { @Autowired - private OldProfilesImporter oldProfilesImporter; + private SimpleResourceRepository simpleResourceRepository; @Autowired - private SimpleResourceRepository simpleResourceRepository; + private DatabaseUtils databaseUtils; private static final Log log = LogFactory.getLog(ResourcesRestController.class); @@ -49,6 +49,17 @@ public class ResourcesRestController { .collect(Collectors.toList()); } + @PostMapping("/") + public SimpleResource newResource(@RequestParam final String name, + @RequestParam final String type, + @RequestParam(required = false, defaultValue = "") final String description, + @RequestParam(required = false, defaultValue = MediaType.APPLICATION_XML_VALUE) final String ctype, + @RequestParam final String content) + throws InformationServiceException { + + return databaseUtils.saveNewResource(name, type, description, ctype, content); + } + @DeleteMapping("/{id}") public void deleteResource(@PathVariable final String id) { log.info("Deleting resource: " + id); @@ -110,11 +121,4 @@ public class ResourcesRestController { } } - @PostMapping(value = "/operation/import", consumes = { - MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE - }) - public SimpleResource importFromOldProfile(final HttpServletRequest request) throws Exception { - final String xml = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8); - return oldProfilesImporter.importSimpleResource(xml); - } } 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 new file mode 100644 index 00000000..5e0e41cb --- /dev/null +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/DatabaseUtils.java @@ -0,0 +1,42 @@ +package eu.dnetlib.is.util; + +import java.util.Date; +import java.util.UUID; + +import javax.transaction.Transactional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import eu.dnetlib.is.resource.model.SimpleResource; +import eu.dnetlib.is.resource.repository.SimpleResourceRepository; + +@Component +public class DatabaseUtils { + + @Autowired + private SimpleResourceRepository simpleResourceRepository; + + @Transactional + public SimpleResource saveNewResource(final String name, + final String type, + final String description, + final String ctype, + final String content) { + final Date now = new Date(); + + final SimpleResource res = new SimpleResource(); + res.setId(UUID.randomUUID().toString()); + res.setName(name); + res.setType(type); + res.setDescription(description); + res.setContentType(ctype); + res.setCreationDate(now); + res.setModificationDate(now); + + simpleResourceRepository.save(res); + simpleResourceRepository.setContentById(res.getId(), content); + + return res; + } +} diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/vocabulary/VocabularyRestController.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/vocabulary/VocabularyRestController.java index 6c1e57d0..adf18fb8 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/vocabulary/VocabularyRestController.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/vocabulary/VocabularyRestController.java @@ -1,13 +1,8 @@ package eu.dnetlib.is.vocabulary; -import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.util.List; import java.util.stream.Collectors; -import javax.servlet.http.HttpServletRequest; - -import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -21,7 +16,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import eu.dnetlib.common.controller.AbstractDnetController; -import eu.dnetlib.is.util.OldProfilesImporter; +import eu.dnetlib.is.importer.OldProfilesImporter; import eu.dnetlib.is.vocabulary.model.Vocabulary; import eu.dnetlib.is.vocabulary.model.VocabularyTerm; import eu.dnetlib.is.vocabulary.model.VocabularyTermPK; @@ -70,12 +65,6 @@ public class VocabularyRestController extends AbstractDnetController { return listVocs(); } - @PostMapping(value = "/import", consumes = "text/plain") - public Vocabulary importFromOldProfile(final HttpServletRequest request) throws Exception, IOException { - final String xml = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8); - return oldProfilesImporter.importVocabulary(xml); - } - @GetMapping("/{vocabulary}/terms") public Iterable listTerms(@PathVariable final String vocabulary) { return vocabularyTermRepository.findByVocabularyOrderByCode(vocabulary); diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/wfs/WfHistoryRestController.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/wfs/WfHistoryRestController.java index f3e8480a..33eaa8d3 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/wfs/WfHistoryRestController.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/wfs/WfHistoryRestController.java @@ -1,6 +1,5 @@ package eu.dnetlib.is.wfs; -import java.util.Arrays; import java.util.Date; import java.util.List; @@ -23,9 +22,6 @@ public class WfHistoryRestController { @Autowired private WfProcessExecutionRepository wfProcessExecutionRepository; - @Autowired - private WfHistoryImporter wfHistoryImporter; - public static final int MAX_NUMBER_OF_RECENT_WFS = 100; @GetMapping("/") @@ -46,13 +42,4 @@ public class WfHistoryRestController { return wfProcessExecutionRepository.findById(processId).get(); } - @GetMapping(value = "/load") - public List loadFromOldProfile(@RequestParam final String path) throws Exception { - // mongoexport -d dnet_logs -c wf_logs --jsonArray -o /tmp/mongodump.json - - wfHistoryImporter.load(path); - - return Arrays.asList("Done."); - } - }