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 73672851..11410bcf 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,10 +1,12 @@ package eu.dnetlib.is.resources; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; @@ -16,10 +18,12 @@ 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.RequestParam; 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.InformationServiceException; import eu.dnetlib.is.util.OldProfilesImporter; @RestController @@ -34,22 +38,42 @@ public class ResourcesRestController { private static final Log log = LogFactory.getLog(ResourcesRestController.class); - @DeleteMapping("/{type}/{id}") - public List deleteResources(@PathVariable final String type, @PathVariable final String id) { - log.info("Deleting rewsource: " + id); - simpleResourceRepository.deleteById(id); - return listResources(type); - } - - @GetMapping("/{type}") - public List listResources(@PathVariable final String type) { + @GetMapping("/") + public List listResources(@RequestParam 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") + @DeleteMapping("/{id}") + public void deleteResources(@PathVariable final String id) { + log.info("Deleting resource: " + id); + simpleResourceRepository.deleteById(id); + } + + @GetMapping("/{id}/metadata") + public SimpleResource getMetadataResource(@PathVariable final String id) throws InformationServiceException { + return simpleResourceRepository.findById(id).orElseThrow(() -> new InformationServiceException("Id not found")); + } + + @GetMapping("/{id}/content") + public void getContentResource(@PathVariable final String id, final HttpServletResponse res) throws InformationServiceException { + final SimpleResource sr = simpleResourceRepository.findById(id).orElseThrow(() -> new InformationServiceException("Id not found")); + + res.setCharacterEncoding(StandardCharsets.UTF_8.name()); + res.setContentType(sr.getContentType()); + + simpleResourceRepository.getContentById(id); + + try { + IOUtils.write(simpleResourceRepository.getContentById(id), res.getOutputStream(), StandardCharsets.UTF_8.name()); + } catch (final IOException e) { + throw new InformationServiceException("Error retrieving content", e); + } + } + + @PostMapping(value = "/operation/import", consumes = "text/plain") public SimpleResource importFromOldProfile(final HttpServletRequest request) throws Exception { final String xml = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8); diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/InformationServiceException.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/InformationServiceException.java new file mode 100644 index 00000000..b0471e3b --- /dev/null +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/util/InformationServiceException.java @@ -0,0 +1,15 @@ +package eu.dnetlib.is.util; + +public class InformationServiceException extends Exception { + + private static final long serialVersionUID = 3566871386890315850L; + + public InformationServiceException(final String message, final Throwable cause) { + super(message, cause); + } + + public InformationServiceException(final String message) { + super(message); + } + +} 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 index d707075d..38c9b53d 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/util/OldProfilesImporter.java @@ -10,6 +10,7 @@ import org.dom4j.Document; 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; @@ -44,7 +45,7 @@ public class OldProfilesImporter { res.setId(id); res.setCreationDate(now); res.setModificationDate(now); - res.setContentType("xml"); + res.setContentType(MediaType.APPLICATION_XML_VALUE); String resContent; switch (doc.valueOf("//RESOURCE_TYPE/@value")) { 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 d71697fa..1ccf1789 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,11 +3,12 @@ var app = angular.module('resourcesApp', []); app.controller('resourcesController', function($scope, $http) { $scope.resources = []; $scope.tmpRes = {}; + $scope.tmpContent = "loading..."; $scope.mode = ''; $scope.type = typeId(); $scope.reload = function() { - $http.get('./api/resources/' + $scope.type + '?' + $.now()).then(function successCallback(res) { + $http.get('./api/resources/?type=' + $scope.type + '&' + $.now()).then(function successCallback(res) { $scope.resources = res.data; }, function errorCallback(res) { alert('ERROR: ' + res.data.message); @@ -23,12 +24,26 @@ app.controller('resourcesController', function($scope, $http) { }; } - $scope.prepareEditRes = function(res) { + $scope.prepareEditMetadata = function(res) { + $scope.tmpRes = angular.copy(res); $scope.mode = 'edit'; + } + + $scope.prepareEditContent = function(res) { + $scope.tmpRes = angular.copy(res); + $scope.tmpContent = "loading..."; + $http.get('./api/resources/' + res.id + '/content?' + $.now()).then(function successCallback(res) { + $scope.tmpContent = res.data; + }, function errorCallback(res) { + alert('ERROR: ' + res.data.message); + }); + } + + $scope.prepareUploadContent = function(res) { $scope.tmpRes = angular.copy(res); } - $scope.saveResource = function(res) { + $scope.saveMetadata = function(res) { if ($scope.mode == 'new') { var found = false; @@ -49,13 +64,28 @@ app.controller('resourcesController', function($scope, $http) { }, function errorCallback(res) { alert('ERROR: ' + res.data.message); }); - }; + }; + + $scope.saveContent = function(id, content) { + $http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8"; + $http.post('./api/resources/' + id + '/content?' + $.now(), res).then(function successCallback(res) { + alert("Resource saved"); + }, function errorCallback(res) { + alert('ERROR: ' + res.data.message); + }); + }; + + + $scope.uploadContent = function(id, content) { + // TODO + }; + $scope.deleteResource = function(r) { if (confirm("Are you sure ?")) { - $http.delete('./api/resources/' + encodeURIComponent(r.type) + '/' + encodeURIComponent(r.id) + '?' + $.now()).then(function successCallback(res) { - $scope.resources = res.data; + $http.delete('./api/resources/' + encodeURIComponent(r.id) + '?' + $.now()).then(function successCallback(res) { alert("Resource deleted"); + $scope.reload(); }, 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 6d224426..5fd61769 100644 --- a/apps/dnet-is-application/src/main/resources/templates/simpleResources.html +++ b/apps/dnet-is-application/src/main/resources/templates/simpleResources.html @@ -33,7 +33,10 @@ Creation date: {{r.creationDate}}
Modification date: {{r.modificationDate}}

- + + + + raw content @@ -44,7 +47,7 @@ -