From 0e6c06033daa1f26001007e1222495f4e49d7565 Mon Sep 17 00:00:00 2001 From: Nikolaos Laskaris Date: Tue, 17 Oct 2017 14:45:11 +0300 Subject: [PATCH] MInor changes on hibernate entities --- .../main/java/entities/DataRepository.java | 3 +- .../java/rest/entities/DataRepositories.java | 75 +++++++++++++------ .../main/java/rest/entities/DmpProfiles.java | 6 +- .../java/rest/entities/Organisations.java | 6 +- .../src/main/java/rest/entities/Projects.java | 6 +- .../main/java/rest/entities/Registries.java | 6 +- .../main/java/rest/entities/Researchers.java | 69 ++++++++++++----- .../src/main/java/rest/entities/Services.java | 6 +- .../CustomAuthenticationProvider.java | 5 -- 9 files changed, 120 insertions(+), 62 deletions(-) diff --git a/dmp-backend/src/main/java/entities/DataRepository.java b/dmp-backend/src/main/java/entities/DataRepository.java index 70c58534e..f1ca03ec8 100644 --- a/dmp-backend/src/main/java/entities/DataRepository.java +++ b/dmp-backend/src/main/java/entities/DataRepository.java @@ -23,8 +23,7 @@ import org.hibernate.annotations.Type; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; - -@Entity(name="\"DataRepository\"") +@Entity @Table(name="\"DataRepository\"") @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id") public class DataRepository implements Serializable { diff --git a/dmp-backend/src/main/java/rest/entities/DataRepositories.java b/dmp-backend/src/main/java/rest/entities/DataRepositories.java index 940aa35f5..0edaab611 100644 --- a/dmp-backend/src/main/java/rest/entities/DataRepositories.java +++ b/dmp-backend/src/main/java/rest/entities/DataRepositories.java @@ -2,6 +2,9 @@ package rest.entities; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; + +import javax.transaction.Transactional; import org.apache.commons.lang3.SerializationUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -66,11 +69,11 @@ public class DataRepositories { @Autowired private ServiceDao serviceDao; - + private ObjectMapper objectMapper = new ObjectMapper(); // MANAGE DATAREPOSITORy(IES) - @RequestMapping(method = RequestMethod.GET, value = { "/datarepositories" }) + @RequestMapping(method = RequestMethod.GET, value = { "/datarepos" }) public @ResponseBody ResponseEntity listDataRepositories(){ try { List allIDs = dataRepositoryDao.listAllIDs(); @@ -82,7 +85,7 @@ public class DataRepositories { } - @RequestMapping(method = RequestMethod.GET, value = { "/datarepositories/{id}" }) + @RequestMapping(method = RequestMethod.GET, value = { "/datarepos/{id}" }) public @ResponseBody ResponseEntity getDataRepository(@PathVariable("id") String id) { try { DataRepository dataRepository = dataRepositoryDao.read(UUID.fromString(id)); @@ -93,28 +96,58 @@ public class DataRepositories { } } + - @RequestMapping(method = RequestMethod.POST, value = { "/setDataRepository" }, consumes = "application/json") - public @ResponseBody ResponseEntity setDataRepository(@RequestBody DataRepository dataRepository) { - String reason = ""; - DataRepository storedDataRepository = null; - //try first to create + @RequestMapping(method = RequestMethod.GET, value = { "/datarepo/getAll" }, produces="application/json") + public @ResponseBody ResponseEntity getAllDataRepositories(){ + try { - storedDataRepository = dataRepositoryDao.create(dataRepository); - return ResponseEntity.status(HttpStatus.CREATED).body("Created dataRepository with id: " + storedDataRepository.getId()); + List allDataRepositories = dataRepositoryDao.getAll(); + + //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom + List datareposStrL = allDataRepositories.parallelStream().map((datarepoObj) -> { + try { + return objectMapper.writeValueAsString(datarepoObj); + } catch (JsonProcessingException e) { + return ""; + } + }).collect(Collectors.toList()); + + return new ResponseEntity("["+String.join(",", datareposStrL)+"]", HttpStatus.OK); + } - catch(Exception e) { - reason += e.getMessage(); - //try updating - try { - storedDataRepository = dataRepositoryDao.update(dataRepository); - return ResponseEntity.status(HttpStatus.CREATED).body("Updated dataRepository with id: " + storedDataRepository.getId()); - } - catch(Exception ex) { - reason += (System.lineSeparator()+e.getMessage()); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Could not create or update dataRepository! Reason: " + reason); - } + catch(Exception ex) { + ex.printStackTrace(); + return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); } + + } + + + @Transactional + @RequestMapping(method = RequestMethod.POST, value = { "/datarepo/create" }, consumes = "application/json", produces="application/json") + public @ResponseBody ResponseEntity setOrganisation(@RequestBody DataRepository dataRepository) { + DataRepository createdDataRepository = dataRepositoryDao.update(dataRepository); + try { + return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdDataRepository)); + } catch (JsonProcessingException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create data repository!\"}"); + } + } + + + @RequestMapping(method = RequestMethod.POST, value = { "/datarepo/delete" }, consumes = "application/json", produces="text/plain") + public @ResponseBody ResponseEntity delete(@RequestBody DataRepository dataRepository) { + + DataRepository dr = new DataRepository(); + dr.setId(dataRepository.getId()); + try { + dataRepositoryDao.delete(dr); + return ResponseEntity.status(HttpStatus.CREATED).body("{\"msg\":\"Deleted data repository!\"}"); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not delete data repository!\"}"); + } + } diff --git a/dmp-backend/src/main/java/rest/entities/DmpProfiles.java b/dmp-backend/src/main/java/rest/entities/DmpProfiles.java index dba6e2c45..6a59bcce6 100644 --- a/dmp-backend/src/main/java/rest/entities/DmpProfiles.java +++ b/dmp-backend/src/main/java/rest/entities/DmpProfiles.java @@ -142,7 +142,7 @@ public class DmpProfiles { try { return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdDMPProfile)); } catch (JsonProcessingException e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create DMP Profile!\""); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create DMP Profile!\"}"); } } @@ -154,9 +154,9 @@ public class DmpProfiles { dmpp.setId(dmpprofile.getId()); try { dMPProfileDao.delete(dmpp); - return ResponseEntity.status(HttpStatus.CREATED).body("DELETED!"); + return ResponseEntity.status(HttpStatus.CREATED).body("{\"msg\":\"Deleted DMP Profile!\"}"); } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete DMP Profile!\""); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete DMP Profile!\"}"); } } diff --git a/dmp-backend/src/main/java/rest/entities/Organisations.java b/dmp-backend/src/main/java/rest/entities/Organisations.java index 49b91704e..85258c0ec 100644 --- a/dmp-backend/src/main/java/rest/entities/Organisations.java +++ b/dmp-backend/src/main/java/rest/entities/Organisations.java @@ -127,7 +127,7 @@ public class Organisations { try { return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdOrganisation)); } catch (JsonProcessingException e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create organisation!\""); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create organisation!\"}"); } } @@ -139,9 +139,9 @@ public class Organisations { org.setId(organisation.getId()); try { organisationDao.delete(org); - return ResponseEntity.status(HttpStatus.CREATED).body("DELETED!"); + return ResponseEntity.status(HttpStatus.CREATED).body("{\"msg\":\"Deleted organisation!\"}"); } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not delete organisation!\""); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not delete organisation!\"}"); } } diff --git a/dmp-backend/src/main/java/rest/entities/Projects.java b/dmp-backend/src/main/java/rest/entities/Projects.java index b642a49c8..f9fdf63d1 100644 --- a/dmp-backend/src/main/java/rest/entities/Projects.java +++ b/dmp-backend/src/main/java/rest/entities/Projects.java @@ -139,7 +139,7 @@ public class Projects { try { return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdProject)); } catch (JsonProcessingException e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create Project!\""); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create Project!\"}"); } } @@ -151,9 +151,9 @@ public class Projects { p.setId(project.getId()); try { projectDao.delete(p); - return ResponseEntity.status(HttpStatus.CREATED).body("DELETED!"); + return ResponseEntity.status(HttpStatus.CREATED).body("{\"msg\":\"Deleted Project entity!\"}"); } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete Project!\""); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete Project!\"}"); } } diff --git a/dmp-backend/src/main/java/rest/entities/Registries.java b/dmp-backend/src/main/java/rest/entities/Registries.java index 54b14b22f..08489c17a 100644 --- a/dmp-backend/src/main/java/rest/entities/Registries.java +++ b/dmp-backend/src/main/java/rest/entities/Registries.java @@ -140,7 +140,7 @@ public class Registries { try { return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdRegistry)); } catch (JsonProcessingException e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create registry!\""); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create registry!\"}"); } } @@ -152,9 +152,9 @@ public class Registries { r.setId(registry.getId()); try { registryDao.delete(r); - return ResponseEntity.status(HttpStatus.CREATED).body("DELETED!"); + return ResponseEntity.status(HttpStatus.CREATED).body("{\"msg\":\"Deleted registry!\"}"); } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete registry!\""); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete registry!\"}"); } } diff --git a/dmp-backend/src/main/java/rest/entities/Researchers.java b/dmp-backend/src/main/java/rest/entities/Researchers.java index 2e5d4827c..ef659665c 100644 --- a/dmp-backend/src/main/java/rest/entities/Researchers.java +++ b/dmp-backend/src/main/java/rest/entities/Researchers.java @@ -2,6 +2,9 @@ package rest.entities; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; + +import javax.transaction.Transactional; import org.apache.commons.lang3.SerializationUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -66,6 +69,8 @@ public class Researchers { @Autowired private ServiceDao serviceDao; + private ObjectMapper objectMapper = new ObjectMapper(); + // MANAGE RESEARCHER(S) @RequestMapping(method = RequestMethod.GET, value = { "/researchers" }) @@ -91,31 +96,57 @@ public class Researchers { } } - - @RequestMapping(method = RequestMethod.POST, value = { "/setResearcher" }, consumes = "application/json") - public @ResponseBody ResponseEntity setResearcher(@RequestBody Researcher researcher) { - String reason = ""; - Researcher storedResearcher = null; - //try first to create + + @RequestMapping(method = RequestMethod.GET, value = { "/researcher/getAll" }, produces="application/json") + public @ResponseBody ResponseEntity getAllResearchers(){ try { - storedResearcher = researcherDao.create(researcher); - return ResponseEntity.status(HttpStatus.CREATED).body("Created researcher with id: " + storedResearcher.getId()); + List allResearchers = researcherDao.getAll(); + + //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom + List researcherStrL = allResearchers.parallelStream().map((researcherObj) -> { + try { + return objectMapper.writeValueAsString(researcherObj); + } catch (JsonProcessingException e) { + return ""; + } + }).collect(Collectors.toList()); + + return new ResponseEntity("["+String.join(",", researcherStrL)+"]", HttpStatus.OK); + } - catch(Exception e) { - reason += e.getMessage(); - //try updating - try { - storedResearcher = researcherDao.update(researcher); - return ResponseEntity.status(HttpStatus.CREATED).body("Updated researcher with id: " + storedResearcher.getId()); - } - catch(Exception ex) { - reason += (System.lineSeparator()+e.getMessage()); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Could not create or update researcher! Reason: " + reason); - } + catch(Exception ex) { + return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); } } + @Transactional + @RequestMapping(method = RequestMethod.POST, value = { "/researcher/create" }, consumes = "application/json", produces="application/json") + public @ResponseBody ResponseEntity setResearcher(@RequestBody Researcher researcher) { + Researcher createdResearcher = researcherDao.update(researcher); + try { + return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdResearcher)); + } catch (JsonProcessingException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create researcher!\"}"); + } + } + + + @RequestMapping(method = RequestMethod.POST, value = { "/researcher/delete" }, consumes = "application/json", produces="text/plain") + public @ResponseBody ResponseEntity delete(@RequestBody Researcher researcher) { + + Researcher res = new Researcher(); + res.setId(researcher.getId()); + try { + researcherDao.delete(res); + return ResponseEntity.status(HttpStatus.CREATED).body("{\"msg\":\"Deleted researcher!\"}"); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not delete researcher!\"}"); + } + + } + + } diff --git a/dmp-backend/src/main/java/rest/entities/Services.java b/dmp-backend/src/main/java/rest/entities/Services.java index 645e95ceb..768a48012 100644 --- a/dmp-backend/src/main/java/rest/entities/Services.java +++ b/dmp-backend/src/main/java/rest/entities/Services.java @@ -128,7 +128,7 @@ public class Services { try { return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdService)); } catch (JsonProcessingException e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create service entity!\""); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create service entity!\"}"); } } @@ -140,9 +140,9 @@ public class Services { s.setId(service.getId()); try { serviceDao.delete(s); - return ResponseEntity.status(HttpStatus.CREATED).body("DELETED!"); + return ResponseEntity.status(HttpStatus.CREATED).body("{\"msg\":\"Deleted Service entity!\"}"); } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete Service entity!\""); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete Service entity!\"}"); } } diff --git a/dmp-backend/src/main/java/security/CustomAuthenticationProvider.java b/dmp-backend/src/main/java/security/CustomAuthenticationProvider.java index e905aec77..71b458f81 100644 --- a/dmp-backend/src/main/java/security/CustomAuthenticationProvider.java +++ b/dmp-backend/src/main/java/security/CustomAuthenticationProvider.java @@ -32,13 +32,8 @@ public class CustomAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { - System.out.println("AUTHENTICATION"); - System.out.println(authentication); - if (authentication != null) { - System.out.println((String)authentication.getCredentials()); - String token = (String)authentication.getCredentials(); TokenValidator tokenValidator = null;