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 1b181ed4..ac4ff9e1 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 @@ -58,13 +58,8 @@ public class MainController { @GetMapping("/simpleResources") public void simpleResources(@RequestParam final String type, final ModelMap map) { - System.out.println("TYPE: " + type); - final Optional restype = simpleResourceTypeRepository.findById(type); - System.out.println("OP TYPE: " + restype); - System.out.println("OP TYPE: " + restype.isPresent()); - if (restype.isPresent()) { map.addAttribute("type", restype.get()); } else { 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 45f77d60..1afef4a9 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 @@ -1,29 +1,48 @@ package eu.dnetlib.is.resources; +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.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; import eu.dnetlib.is.resource.model.SimpleResource; import eu.dnetlib.is.resource.repository.SimpleResourceRepository; +import eu.dnetlib.is.util.OldProfilesImporter; @RestController @RequestMapping("/api/resources") public class ResourcesRestController { + @Autowired + private OldProfilesImporter oldProfilesImporter; + @Autowired private SimpleResourceRepository simpleResourceRepository; - @GetMapping("/") - public List listVocs() { - return simpleResourceRepository.findAll() + @GetMapping("/{type}") + public List listResources(@PathVariable final String type) { + return simpleResourceRepository.findByType(type) .stream() .sorted((r1, r2) -> StringUtils.compareIgnoreCase(r1.getName(), r2.getName())) .collect(Collectors.toList()); } + + @PostMapping(value = "/import", consumes = "text/plain") + 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/OldProfilesImporter.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/OldProfilesImporter.java new file mode 100644 index 00000000..d707075d --- /dev/null +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/OldProfilesImporter.java @@ -0,0 +1,114 @@ +package eu.dnetlib.is.util; + +import java.io.StringReader; +import java.util.Date; + +import javax.transaction.Transactional; + +import org.apache.commons.lang3.StringUtils; +import org.dom4j.Document; +import org.dom4j.Node; +import org.dom4j.io.SAXReader; +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; +import eu.dnetlib.is.vocabulary.model.Synonym; +import eu.dnetlib.is.vocabulary.model.Vocabulary; +import eu.dnetlib.is.vocabulary.model.VocabularyTerm; +import eu.dnetlib.is.vocabulary.repository.VocabularyRepository; +import eu.dnetlib.is.vocabulary.repository.VocabularyTermRepository; + +@Component +public class OldProfilesImporter { + + @Autowired + private SimpleResourceRepository simpleResourceRepository; + + @Autowired + private VocabularyRepository vocabularyRepository; + + @Autowired + private VocabularyTermRepository vocabularyTermRepository; + + @Transactional + public SimpleResource importSimpleResource(final String xml) throws Exception { + 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(); + + final SimpleResource res = new SimpleResource(); + res.setId(id); + res.setCreationDate(now); + res.setModificationDate(now); + res.setContentType("xml"); + + 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")); + } + + simpleResourceRepository.save(res); + simpleResourceRepository.setContentById(id, resContent); + + return res; + } + + @Transactional + public Vocabulary importVocabulary(final String xml) throws Exception { + final SAXReader reader = new SAXReader(); + final Document doc = reader.read(new StringReader(xml)); + + final Vocabulary voc = new Vocabulary(); + final String vocId = doc.valueOf("//VOCABULARY_NAME/@code"); + final String vocName = doc.valueOf("//VOCABULARY_NAME"); + final String vocDesc = doc.valueOf("//VOCABULARY_DESCRIPTION"); + + voc.setId(vocId); + voc.setName(vocName); + voc.setDescription(vocDesc); + + vocabularyRepository.save(voc); + + for (final Node n : doc.selectNodes("//TERM")) { + final VocabularyTerm term = new VocabularyTerm(); + term.setVocabulary(vocId); + term.setCode(n.valueOf("@code")); + term.setName(n.valueOf("@english_name")); + term.setEncoding(n.valueOf("@encoding")); + term.setSynonyms(n.selectNodes(".//SYNONYM") + .stream() + .map(ns -> new Synonym(ns.valueOf("@term"), ns.valueOf("@encoding"))) + .sorted() + .distinct() + .toArray(Synonym[]::new)); + + vocabularyTermRepository.save(term); + } + + return voc; + } + +} 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 9f6479a1..6c1e57d0 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,7 +1,6 @@ package eu.dnetlib.is.vocabulary; import java.io.IOException; -import java.io.StringReader; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.stream.Collectors; @@ -12,10 +11,6 @@ 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.Document; -import org.dom4j.DocumentException; -import org.dom4j.Node; -import org.dom4j.io.SAXReader; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -26,7 +21,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.vocabulary.model.Synonym; +import eu.dnetlib.is.util.OldProfilesImporter; import eu.dnetlib.is.vocabulary.model.Vocabulary; import eu.dnetlib.is.vocabulary.model.VocabularyTerm; import eu.dnetlib.is.vocabulary.model.VocabularyTermPK; @@ -43,6 +38,9 @@ public class VocabularyRestController extends AbstractDnetController { @Autowired private VocabularyTermRepository vocabularyTermRepository; + @Autowired + private OldProfilesImporter oldProfilesImporter; + private static final Log log = LogFactory.getLog(VocabularyRestController.class); @GetMapping("/") @@ -72,42 +70,10 @@ public class VocabularyRestController extends AbstractDnetController { return listVocs(); } - @PostMapping(value = "/load", consumes = "text/plain") - public Vocabulary loadFromOldProfile(final HttpServletRequest request) throws DocumentException, IOException { - + @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); - - final SAXReader reader = new SAXReader(); - final Document doc = reader.read(new StringReader(xml)); - - final Vocabulary voc = new Vocabulary(); - final String vocId = doc.valueOf("//VOCABULARY_NAME/@code"); - final String vocName = doc.valueOf("//VOCABULARY_NAME"); - final String vocDesc = doc.valueOf("//VOCABULARY_DESCRIPTION"); - - voc.setId(vocId); - voc.setName(vocName); - voc.setDescription(vocDesc); - - vocabularyRepository.save(voc); - - for (final Node n : doc.selectNodes("//TERM")) { - final VocabularyTerm term = new VocabularyTerm(); - term.setVocabulary(vocId); - term.setCode(n.valueOf("@code")); - term.setName(n.valueOf("@english_name")); - term.setEncoding(n.valueOf("@encoding")); - term.setSynonyms(n.selectNodes(".//SYNONYM") - .stream() - .map(ns -> new Synonym(ns.valueOf("@term"), ns.valueOf("@encoding"))) - .sorted() - .distinct() - .toArray(Synonym[]::new)); - - vocabularyTermRepository.save(term); - } - - return voc; + return oldProfilesImporter.importVocabulary(xml); } @GetMapping("/{vocabulary}/terms") diff --git a/apps/dnet-is-application/src/main/resources/static/js/simpleResources.js b/apps/dnet-is-application/src/main/resources/static/js/simpleResources.js index f335efef..9b14013c 100644 --- a/apps/dnet-is-application/src/main/resources/static/js/simpleResources.js +++ b/apps/dnet-is-application/src/main/resources/static/js/simpleResources.js @@ -3,10 +3,11 @@ var app = angular.module('resourcesApp', []); app.controller('resourcesController', function($scope, $http) { $scope.resources = []; $scope.tmpRes = {}; - $scope.mode = ''; - + $scope.mode = ''; + $scope.type = typeId(); + $scope.reload = function() { - $http.get('./api/resources/?' + $.now()).then(function successCallback(res) { + $http.get('./api/resources/' + $scope.type + '?' + $.now()).then(function successCallback(res) { $scope.resources = res.data; }, function errorCallback(res) { alert('ERROR: ' + res.data.message); 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 b67a4a34..51399b19 100644 --- a/apps/dnet-is-application/src/main/resources/templates/simpleResources.html +++ b/apps/dnet-is-application/src/main/resources/templates/simpleResources.html @@ -3,6 +3,12 @@ + + diff --git a/libs/dnet-is-common/src/main/java/eu/dnetlib/is/resource/model/SimpleResource.java b/libs/dnet-is-common/src/main/java/eu/dnetlib/is/resource/model/SimpleResource.java index 2513a3bd..43a28e99 100644 --- a/libs/dnet-is-common/src/main/java/eu/dnetlib/is/resource/model/SimpleResource.java +++ b/libs/dnet-is-common/src/main/java/eu/dnetlib/is/resource/model/SimpleResource.java @@ -23,6 +23,12 @@ public class SimpleResource implements Serializable { @Column(name = "name") private String name; + @Column(name = "type") + private String type; + + @Column(name = "content_type") + private String contentType; + @Column(name = "description") private String description; @@ -50,6 +56,22 @@ public class SimpleResource implements Serializable { this.name = name; } + public String getType() { + return type; + } + + public void setType(final String type) { + this.type = type; + } + + public String getContentType() { + return contentType; + } + + public void setContentType(final String contentType) { + this.contentType = contentType; + } + public String getDescription() { return description; } diff --git a/libs/dnet-is-common/src/main/java/eu/dnetlib/is/resource/repository/SimpleResourceRepository.java b/libs/dnet-is-common/src/main/java/eu/dnetlib/is/resource/repository/SimpleResourceRepository.java index 5a537584..b0baec94 100644 --- a/libs/dnet-is-common/src/main/java/eu/dnetlib/is/resource/repository/SimpleResourceRepository.java +++ b/libs/dnet-is-common/src/main/java/eu/dnetlib/is/resource/repository/SimpleResourceRepository.java @@ -1,5 +1,7 @@ package eu.dnetlib.is.resource.repository; +import java.util.List; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; @@ -8,10 +10,12 @@ import eu.dnetlib.is.resource.model.SimpleResource; public interface SimpleResourceRepository extends JpaRepository { - @Query(value = "select content from resources where id = :id", nativeQuery = true) + @Query(value = "select content from resources where id = ?1", nativeQuery = true) String getContentById(String id); @Modifying - @Query(value = "update resources set content = :content where id = :id", nativeQuery = true) - String setContentById(String id, String content); + @Query(value = "update resources set content = ?2 where id = ?1", nativeQuery = true) + void setContentById(String id, String content); + + List findByType(String type); } diff --git a/libs/dnet-is-common/src/main/resources/sql/schema.sql b/libs/dnet-is-common/src/main/resources/sql/schema.sql index 1113a927..5e85e834 100644 --- a/libs/dnet-is-common/src/main/resources/sql/schema.sql +++ b/libs/dnet-is-common/src/main/resources/sql/schema.sql @@ -81,14 +81,17 @@ CREATE TABLE wf_history ( -- Other Resources CREATE TABLE resource_types(id text PRIMARY KEY, name text); -INSERT INTO resource_types(id, name) VALUES ('transformation_rule', 'Transformation Rules'), ('cleaning_rule', 'Cleaning Rules'); +INSERT INTO resource_types(id, name) VALUES + ('transformation_rule', 'Transformation Rules'), + ('cleaning_rule', 'Cleaning Rules'), + ('hadoop_job_configuration', 'Hadoop Job Configurations'); CREATE TABLE resources ( id text PRIMARY KEY, name text NOT NULL, description text, - content_type text NOT NULL, - content text NOT NULL, + content_type text NOT NULL DEFAULT 'xml', + content text NOT NULL DEFAULT '', type text NOT NULL REFERENCES resource_types(id), creation_date timestamp NOT NULL DEFAULT now(), modification_date timestamp NOT NULL DEFAULT now()