This commit is contained in:
parent
7e7f6ff746
commit
d5fb85d933
|
@ -3,30 +3,23 @@ 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;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
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.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
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.DatabaseUtils;
|
||||
import eu.dnetlib.is.util.InformationServiceException;
|
||||
|
||||
@RestController
|
||||
|
@ -34,19 +27,11 @@ import eu.dnetlib.is.util.InformationServiceException;
|
|||
public class ResourcesRestController {
|
||||
|
||||
@Autowired
|
||||
private SimpleResourceRepository simpleResourceRepository;
|
||||
|
||||
@Autowired
|
||||
private DatabaseUtils databaseUtils;
|
||||
|
||||
private static final Log log = LogFactory.getLog(ResourcesRestController.class);
|
||||
private SimpleResourceService service;
|
||||
|
||||
@GetMapping("/")
|
||||
public List<SimpleResource> listResources(@RequestParam final String type) {
|
||||
return simpleResourceRepository.findByType(type)
|
||||
.stream()
|
||||
.sorted((r1, r2) -> StringUtils.compareIgnoreCase(r1.getName(), r2.getName()))
|
||||
.collect(Collectors.toList());
|
||||
return service.listResources(type);
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
|
@ -57,24 +42,23 @@ public class ResourcesRestController {
|
|||
@RequestParam final String content)
|
||||
throws InformationServiceException {
|
||||
|
||||
return databaseUtils.saveNewResource(name, type, description, ctype, content);
|
||||
return service.saveNewResource(name, type, description, ctype, content);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void deleteResource(@PathVariable final String id) {
|
||||
log.info("Deleting resource: " + id);
|
||||
simpleResourceRepository.deleteById(id);
|
||||
service.deleteResource(id);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/metadata")
|
||||
public SimpleResource getMetadata(@PathVariable final String id) throws InformationServiceException {
|
||||
return simpleResourceRepository.findById(id).orElseThrow(() -> new InformationServiceException("Id not found"));
|
||||
return service.getMetadata(id);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/content")
|
||||
public void getContent(@PathVariable final String id, final HttpServletResponse res) throws InformationServiceException {
|
||||
final String ctype = simpleResourceRepository.findContentTypeById(id).orElseThrow(() -> new InformationServiceException("Id not found"));
|
||||
final String content = simpleResourceRepository.findContentById(id).orElseThrow(() -> new InformationServiceException("Id not found"));
|
||||
final String ctype = service.getContentType(id);
|
||||
final String content = service.getContent(id);
|
||||
|
||||
res.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||
res.setContentType(ctype);
|
||||
|
@ -87,36 +71,24 @@ public class ResourcesRestController {
|
|||
}
|
||||
|
||||
@PostMapping("/{id}/metadata")
|
||||
public void saveMetadata(@RequestBody final SimpleResource r) throws InformationServiceException {
|
||||
if (simpleResourceRepository.existsById(r.getId())) {
|
||||
simpleResourceRepository.save(r);
|
||||
} else {
|
||||
throw new InformationServiceException("Resource not found");
|
||||
}
|
||||
public void saveMetadata(@PathVariable final String id, @RequestBody final SimpleResource r) throws InformationServiceException {
|
||||
service.saveMetadata(id, r);
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/content")
|
||||
public void saveContent(@PathVariable final String id, @RequestParam final String content) throws InformationServiceException {
|
||||
if (simpleResourceRepository.existsById(id)) {
|
||||
simpleResourceRepository.setContentById(id, content);
|
||||
} else {
|
||||
throw new InformationServiceException("Resource not found");
|
||||
}
|
||||
service.saveContent(id, content);
|
||||
}
|
||||
|
||||
@PutMapping(value = "/{id}/content", consumes = {
|
||||
@PostMapping(value = "/{id}/file", consumes = {
|
||||
MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE
|
||||
})
|
||||
public void uploadContent(@PathVariable final String id, final HttpServletRequest request) throws InformationServiceException {
|
||||
if (simpleResourceRepository.existsById(id)) {
|
||||
try {
|
||||
final String content = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
|
||||
simpleResourceRepository.setContentById(id, content);
|
||||
} catch (final Exception e) {
|
||||
throw new InformationServiceException("Error processing file", e);
|
||||
}
|
||||
} else {
|
||||
throw new InformationServiceException("Resource not found");
|
||||
service.saveContent(id, content);
|
||||
} catch (final IOException e) {
|
||||
throw new InformationServiceException("Error processing input file", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
package eu.dnetlib.is.resources;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
import eu.dnetlib.is.resource.model.SimpleResource;
|
||||
import eu.dnetlib.is.resource.repository.SimpleResourceRepository;
|
||||
import eu.dnetlib.is.util.InformationServiceException;
|
||||
|
||||
@Service
|
||||
public class SimpleResourceService {
|
||||
|
||||
@Autowired
|
||||
private SimpleResourceRepository simpleResourceRepository;
|
||||
|
||||
private static final Log log = LogFactory.getLog(SimpleResourceService.class);
|
||||
|
||||
public SimpleResource getMetadata(final String id) throws InformationServiceException {
|
||||
return simpleResourceRepository.findById(id).orElseThrow(() -> new InformationServiceException("Id not found"));
|
||||
}
|
||||
|
||||
public String getContent(final String id) throws InformationServiceException {
|
||||
return simpleResourceRepository.findContentById(id).orElseThrow(() -> new InformationServiceException("Id not found"));
|
||||
}
|
||||
|
||||
public String getContentType(final String id) throws InformationServiceException {
|
||||
return simpleResourceRepository.findContentTypeById(id).orElseThrow(() -> new InformationServiceException("Id not found"));
|
||||
}
|
||||
|
||||
public List<SimpleResource> listResources(final String type) {
|
||||
return simpleResourceRepository.findByType(type)
|
||||
.stream()
|
||||
.sorted((r1, r2) -> StringUtils.compareIgnoreCase(r1.getName(), r2.getName()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void deleteResource(@PathVariable final String id) {
|
||||
log.info("Deleting resource: " + id);
|
||||
simpleResourceRepository.deleteById(id);
|
||||
}
|
||||
|
||||
@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.setCreationDate(now);
|
||||
res.setModificationDate(now);
|
||||
|
||||
simpleResourceRepository.save(res);
|
||||
simpleResourceRepository.setContentById(res.getId(), content);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void saveMetadata(final String id, final SimpleResource r) throws InformationServiceException {
|
||||
if (simpleResourceRepository.existsById(id)) {
|
||||
r.setId(id);
|
||||
simpleResourceRepository.save(r);
|
||||
} else {
|
||||
throw new InformationServiceException("Resource not found");
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void saveContent(final String id, final String content) throws InformationServiceException {
|
||||
if (simpleResourceRepository.existsById(id)) {
|
||||
simpleResourceRepository.setContentById(id, content);
|
||||
} else {
|
||||
throw new InformationServiceException("Resource not found");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
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.setCreationDate(now);
|
||||
res.setModificationDate(now);
|
||||
|
||||
simpleResourceRepository.save(res);
|
||||
simpleResourceRepository.setContentById(res.getId(), content);
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
|
@ -4,7 +4,6 @@ app.controller('resourcesController', function($scope, $http) {
|
|||
$scope.resources = [];
|
||||
$scope.tmpRes = {};
|
||||
$scope.tmpContent = "loading...";
|
||||
$scope.mode = '';
|
||||
$scope.type = typeId();
|
||||
|
||||
$scope.reload = function() {
|
||||
|
@ -16,7 +15,6 @@ app.controller('resourcesController', function($scope, $http) {
|
|||
};
|
||||
|
||||
$scope.prepareNewRes = function() {
|
||||
$scope.mode = 'new';
|
||||
$scope.tmpRes = {
|
||||
'id' : '',
|
||||
'name' : '',
|
||||
|
@ -26,7 +24,6 @@ app.controller('resourcesController', function($scope, $http) {
|
|||
|
||||
$scope.prepareEditMetadata = function(res) {
|
||||
$scope.tmpRes = angular.copy(res);
|
||||
$scope.mode = 'edit';
|
||||
}
|
||||
|
||||
$scope.prepareEditContent = function(res) {
|
||||
|
@ -43,22 +40,9 @@ app.controller('resourcesController', function($scope, $http) {
|
|||
$scope.tmpRes = angular.copy(res);
|
||||
}
|
||||
|
||||
$scope.saveMetadata = function(md) {
|
||||
if ($scope.mode == 'new') {
|
||||
var found = false;
|
||||
|
||||
angular.forEach($scope.resources, function(r) {
|
||||
if (md.id == r.id) { found = true; };
|
||||
});
|
||||
|
||||
if (found) {
|
||||
alert("Insertion failed: resource already exists !");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$scope.saveMetadata = function(id, md) {
|
||||
$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
|
||||
$http.post('./api/resources/?' + $.now(), md).then(function successCallback(res) {
|
||||
$http.post('./api/resources/' + id + '/metadata?' + $.now(), md).then(function successCallback(res) {
|
||||
alert("Resource saved");
|
||||
$scope.reload();
|
||||
}, function errorCallback(res) {
|
||||
|
|
|
@ -35,7 +35,6 @@
|
|||
</p>
|
||||
<button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#editMetadataModal" ng-click="prepareEditMetadata(r)">edit metadata</button>
|
||||
<button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#editContentModal" ng-click="prepareEditContent(r)">edit content</button>
|
||||
<button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#uploadContentModal" ng-click="prepareUploadContent(r)">upload content file</button>
|
||||
<a href="./api/resources/{{r.id}}/content" class="btn btn-sm btn-primary" target="_blank">raw content</a>
|
||||
<button type="button" class="btn btn-sm btn-danger" ng-click="deleteResource(r)">delete</button>
|
||||
</div>
|
||||
|
@ -55,12 +54,11 @@
|
|||
<h4 class="modal-title" ng-if="mode == 'edit'">Edit resource</h4>
|
||||
<button type="button" class="close" data-dismiss="modal">×</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form>
|
||||
<div class="modal-body">
|
||||
<div class="form-group">
|
||||
<label>ID</label>
|
||||
<input ng-show="mode == 'new'" type="text" class="form-control" ng-model="tmpRes.id" />
|
||||
<input ng-show="mode == 'edit'" type="text" readonly class="form-control-plaintext" ng-model="tmpRes.id" />
|
||||
<input type="text" readonly class="form-control-plaintext" ng-model="tmpRes.id" />
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Type</label>
|
||||
|
@ -74,12 +72,12 @@
|
|||
<label>Description</label>
|
||||
<textarea class="form-control" ng-model="tmpRes.description"></textarea>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-sm btn-secondary" data-dismiss="modal">Close</button>
|
||||
<button type="submit" class="btn btn-sm btn-primary" data-dismiss="modal" ng-click="saveResource(tmpRes)" ng-disabled="!tmpRes.id || !tmpRes.name">Submit</button>
|
||||
<button type="submit" class="btn btn-sm btn-primary" data-dismiss="modal" ng-click="saveMetadata(tmpRes.id, tmpRes)" ng-disabled="!tmpRes.id || !tmpRes.name">Submit</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -91,39 +89,16 @@
|
|||
<h4 class="modal-title">Edit content</h4>
|
||||
<button type="button" class="close" data-dismiss="modal">×</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form>
|
||||
<div class="modal-body">
|
||||
<textarea class="form-control small" ng-model="tmpContent" rows="25"></textarea>
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-sm btn-secondary" data-dismiss="modal">Close</button>
|
||||
<button type="submit" class="btn btn-sm btn-primary" data-dismiss="modal" ng-click="saveContent(tmpRes.id, tmpContent)" ng-disabled="!tmpContent">Submit</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="modal fade" tabindex="-1" id="uploadContentModal">
|
||||
<div class="modal-dialog modal-lg">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h4 class="modal-title">Upload content</h4>
|
||||
<button type="button" class="close" data-dismiss="modal">×</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form>
|
||||
<div class="form-group">
|
||||
<label>Format: <i th:text="${type.contentType}"></i></label>
|
||||
<input type="file" class="form-control-file" />
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-sm btn-secondary" data-dismiss="modal">Close</button>
|
||||
<button type="submit" class="btn btn-sm btn-primary" data-dismiss="modal" ng-click="saveResource(tmpRes)" ng-disabled="!tmpContent">Submit</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
Loading…
Reference in New Issue