From 52ce3651239cd0f5b0eeed161803b1f1fdb15d82 Mon Sep 17 00:00:00 2001 From: Nikolaos Laskaris Date: Fri, 27 Oct 2017 13:52:12 +0300 Subject: [PATCH 1/2] MOstly annotations and minor changes on rest api --- dmp-backend/src/main/java/entities/DMP.java | 17 +++ .../src/main/java/entities/Project.java | 17 --- .../{UserProject.java => UserDMP.java} | 16 +-- .../src/main/java/entities/UserInfo.java | 17 ++- .../main/java/helpers/SerializerProvider.java | 29 +++- .../src/main/java/rest/entities/DMPs.java | 124 +++++++++++------- .../java/rest/entities/DataRepositories.java | 22 +--- .../java/rest/entities/DatasetProfiles.java | 53 ++------ .../src/main/java/rest/entities/Datasets.java | 21 +-- .../main/java/rest/entities/DmpProfiles.java | 26 +--- .../java/rest/entities/Organisations.java | 24 +--- .../src/main/java/rest/entities/Projects.java | 72 +++++----- .../main/java/rest/entities/Registries.java | 23 +--- .../main/java/rest/entities/Researchers.java | 23 +--- .../src/main/java/rest/entities/Services.java | 32 +---- .../src/main/java/rest/entities/Users.java | 5 - .../java/security/TokenSessionManager.java | 4 +- dmp-backend/src/test/java/TestRest.java | 24 +++- 18 files changed, 248 insertions(+), 301 deletions(-) rename dmp-backend/src/main/java/entities/{UserProject.java => UserDMP.java} (85%) diff --git a/dmp-backend/src/main/java/entities/DMP.java b/dmp-backend/src/main/java/entities/DMP.java index 3f3bfab4f..45a95928c 100644 --- a/dmp-backend/src/main/java/entities/DMP.java +++ b/dmp-backend/src/main/java/entities/DMP.java @@ -92,6 +92,23 @@ public class DMP implements Serializable { private Set researchers; + @OneToMany(fetch = FetchType.LAZY) + @JoinTable(name="\"UserDMP\"", + joinColumns={@JoinColumn(name="dmp", referencedColumnName="\"ID\"")}, + inverseJoinColumns={@JoinColumn(name="usr", referencedColumnName="id")} + ) + private Set users; + + + public Set getUsers() { + return users; + } + + public void setUsers(Set users) { + this.users = users; + } + + public UUID getId() { return id; } diff --git a/dmp-backend/src/main/java/entities/Project.java b/dmp-backend/src/main/java/entities/Project.java index 03d6fbe3a..391182bfc 100644 --- a/dmp-backend/src/main/java/entities/Project.java +++ b/dmp-backend/src/main/java/entities/Project.java @@ -67,23 +67,6 @@ public class Project implements Serializable { @Type(type="typedefinition.XMLType") @Column(name = "\"Definition\"", columnDefinition = "xml", nullable = true) private String definition; - - - @OneToMany(fetch = FetchType.LAZY) - @JoinTable(name="\"UserProject\"", - joinColumns={@JoinColumn(name="project", referencedColumnName="\"ID\"")}, - inverseJoinColumns={@JoinColumn(name="usr", referencedColumnName="id")} - ) - private Set users; - - - public Set getUsers() { - return users; - } - - public void setUsers(Set users) { - this.users = users; - } public UUID getId() { diff --git a/dmp-backend/src/main/java/entities/UserProject.java b/dmp-backend/src/main/java/entities/UserDMP.java similarity index 85% rename from dmp-backend/src/main/java/entities/UserProject.java rename to dmp-backend/src/main/java/entities/UserDMP.java index 7c01c692b..070bd3ee9 100644 --- a/dmp-backend/src/main/java/entities/UserProject.java +++ b/dmp-backend/src/main/java/entities/UserDMP.java @@ -19,9 +19,9 @@ import com.fasterxml.jackson.annotation.ObjectIdGenerators; import com.fasterxml.jackson.annotation.JsonInclude.Include; @Entity -@Table(name="\"UserProject\"") +@Table(name="\"UserDMP\"") @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id") -public class UserProject implements Serializable{ +public class UserDMP implements Serializable{ private static final long serialVersionUID = -4467370784003784660L; @@ -36,8 +36,8 @@ public class UserProject implements Serializable{ private UUID usr; @Type(type="org.hibernate.type.PostgresUUIDType") //DEPWARN dependency to Hibernate and PostgreSQL - @Column(name = "project", nullable = false) - private UUID project; + @Column(name = "dmp", nullable = false) + private UUID dmp; @Column(name = "role") private Integer role; @@ -59,12 +59,12 @@ public class UserProject implements Serializable{ this.usr = usr; } - public UUID getProject() { - return project; + public UUID getDmp() { + return dmp; } - public void setProject(UUID project) { - this.project = project; + public void setDmp(UUID dmp) { + this.dmp = dmp; } public Integer getRole() { diff --git a/dmp-backend/src/main/java/entities/UserInfo.java b/dmp-backend/src/main/java/entities/UserInfo.java index 99bc289a7..76482691e 100644 --- a/dmp-backend/src/main/java/entities/UserInfo.java +++ b/dmp-backend/src/main/java/entities/UserInfo.java @@ -77,22 +77,21 @@ public class UserInfo implements Serializable{ private String additionalinfo; @OneToMany(fetch = FetchType.LAZY) - @JoinTable(name="\"UserProject\"", + @JoinTable(name="\"UserDMP\"", joinColumns={@JoinColumn(name="usr", referencedColumnName="id")}, - inverseJoinColumns={@JoinColumn(name="project", referencedColumnName="\"ID\"")} + inverseJoinColumns={@JoinColumn(name="dmp", referencedColumnName="\"ID\"")} ) - private Set projects; + private Set dmps; - - public Set getProjects() { - return projects; + + public Set getDmps() { + return dmps; } - public void setProjects(Set projects) { - this.projects = projects; + public void setDmps(Set dmps) { + this.dmps = dmps; } - public UUID getId() { return id; } diff --git a/dmp-backend/src/main/java/helpers/SerializerProvider.java b/dmp-backend/src/main/java/helpers/SerializerProvider.java index ce27c02b6..f1bce00a5 100644 --- a/dmp-backend/src/main/java/helpers/SerializerProvider.java +++ b/dmp-backend/src/main/java/helpers/SerializerProvider.java @@ -1,5 +1,12 @@ package helpers; +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; + +import org.apache.commons.lang3.StringUtils; + +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module; import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module.Feature; @@ -12,7 +19,11 @@ public class SerializerProvider { static { - + initialize(); + } + + + public static void initialize() { Hibernate5Module module = new Hibernate5Module(); module.enable(Feature.SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS); objectMapper = new ObjectMapper() @@ -20,12 +31,26 @@ public class SerializerProvider { // .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .registerModule(new Hibernate5Module()) ; - } public static ObjectMapper getJsonSerializer() { + if(objectMapper==null) + initialize(); return objectMapper; } + public static String toJson(Object obj) { + try { + return getJsonSerializer().writeValueAsString(obj); + }catch(JsonProcessingException ex) { + return ""; + } + } + + public static String toJson(Collection coll) { + List list = coll.parallelStream().map(obj -> toJson(obj)).collect(Collectors.toList()); + return "["+StringUtils.join(list.toArray(), ",")+"]"; + } + } diff --git a/dmp-backend/src/main/java/rest/entities/DMPs.java b/dmp-backend/src/main/java/rest/entities/DMPs.java index 84da97f2b..ee8e8122b 100644 --- a/dmp-backend/src/main/java/rest/entities/DMPs.java +++ b/dmp-backend/src/main/java/rest/entities/DMPs.java @@ -1,5 +1,7 @@ package rest.entities; + +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -72,8 +74,6 @@ public class DMPs { @Autowired private UserInfoDao userInfoDao; - private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer(); - // FETCH BY DMP(S) @@ -81,9 +81,10 @@ public class DMPs { public @ResponseBody ResponseEntity listDMPs(){ try { List allIDs = dMPDao.listAllIDs(); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs)); } catch(Exception ex) { + ex.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); } } @@ -92,7 +93,7 @@ public class DMPs { public @ResponseBody ResponseEntity getDMP(@PathVariable("id") String id){ try { DMP dmp = dMPDao.read(UUID.fromString(id)); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(dmp)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(dmp)); } catch(Exception ex) { ex.printStackTrace(); @@ -104,9 +105,10 @@ public class DMPs { public @ResponseBody ResponseEntity listDmpLabelID(){ try { List allIDLabels = dMPDao.listAllIDsLabels(); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDLabels)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDLabels)); } catch(Exception ex) { + ex.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); } } @@ -120,18 +122,7 @@ public class DMPs { try { List allDMPs = dMPDao.getAll(); - - //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom - List dmpStrL = allDMPs.parallelStream().map((dmpObj) -> { - try { - return objectMapper.writeValueAsString(dmpObj); - } catch (JsonProcessingException e) { - return ""; - } - }).collect(Collectors.toList()); - - return new ResponseEntity("["+String.join(",", dmpStrL)+"]", HttpStatus.OK); - + return new ResponseEntity(SerializerProvider.toJson(allDMPs), HttpStatus.OK); } catch(Exception ex) { ex.printStackTrace(); @@ -143,10 +134,11 @@ public class DMPs { @Transactional @RequestMapping(method = RequestMethod.POST, value = { "/dmp/create" }, consumes = "application/json", produces="application/json") public @ResponseBody ResponseEntity createDMP(@RequestBody DMP dmp) { - DMP createdDmp = dMPDao.update(dmp); try { - return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdDmp)); - } catch (JsonProcessingException e) { + DMP createdDmp = dMPDao.update(dmp); + return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(createdDmp)); + } catch (Exception e) { + e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create DMP!\""); } @@ -155,10 +147,11 @@ public class DMPs { @Transactional @RequestMapping(method = RequestMethod.POST, value = { "/dmp/update" }, consumes = "application/json", produces="application/json") public @ResponseBody ResponseEntity updateDMP(@RequestBody DMP dmp) { - DMP updatedDMP = dMPDao.update(dmp); try { - return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(updatedDMP)); - } catch (JsonProcessingException e) { + DMP updatedDMP = dMPDao.update(dmp); + return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(updatedDMP)); + } catch (Exception e) { + e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not update DMP!\""); } @@ -256,10 +249,10 @@ public class DMPs { @RequestMapping(method = RequestMethod.POST, value = { "/dmp/getdatasets" }, consumes = "application/json", produces="application/json") public @ResponseBody ResponseEntity getDatasetsOfDMP(@RequestBody DMP dmp) { - Set datasets = dMPDao.read(dmp.getId()).getDataset(); try { - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(datasets)); - } catch (JsonProcessingException e) { + Set datasets = dMPDao.read(dmp.getId()).getDataset(); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(datasets)); + } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not get datasets of DMP!\""); } @@ -305,13 +298,7 @@ public class DMPs { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("There's no such a user on the system. You shouldn't be here"); try { - Set userDMPs = new HashSet(); - - userInfo.getProjects().forEach(project -> { - userDMPs.addAll(project.getDmps()); - }); - - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(userDMPs)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(userInfo.getDmps())); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -319,33 +306,33 @@ public class DMPs { } - @RequestMapping(method = RequestMethod.GET, value = { "/dmp/createforproject" }, produces="text/plain") - public @ResponseBody ResponseEntity createDmpOfProject(@RequestParam("projectid") String projectid, @RequestBody DMP dmp){ + @RequestMapping(method = RequestMethod.POST, value = { "/dmp/createofuser" }, produces="text/plain") + public @ResponseBody ResponseEntity createDmpOfUser(@RequestBody DMP dmp){ - UUID projIdUuid; + + String userID = null; try { - projIdUuid = UUID.fromString(projectid); - } - catch(Exception ex) { - return ResponseEntity.status(HttpStatus.NOT_MODIFIED).body("Did not specify an id or id was not valid... Could not do anything"); + userID = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString(); + } catch(NullPointerException ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("You have not logged in. You shouldn't be here"); } + UserInfo userInfo = userInfoDao.read(UUID.fromString(userID)); + + if(userInfo==null) //this should normally never happer + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("There's no such a user on the system. You shouldn't be here"); try { + dmp.setId(null); - dmp = dMPDao.create(dmp); - Project project = projectDao.read(projIdUuid); + Set users = new HashSet(); + users.add(userInfo); + dmp.setUsers(users); - Set dmps = project.getDmps(); - if(dmps == null) - dmps = new HashSet(); - dmps.add(dmp); - project.setDmps(dmps); + DMP newdmp = dMPDao.create(dmp); - project = projectDao.update(project); - - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(dmp)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(newdmp)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -354,6 +341,43 @@ public class DMPs { } + @RequestMapping(method = RequestMethod.POST, value = { "/dmp/adduser" }, produces="text/plain") + public @ResponseBody ResponseEntity addUserToDmp(@RequestBody DMP dmp){ + + + String userID = null; + try { + userID = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString(); + } catch(NullPointerException ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("You have not logged in. You shouldn't be here"); + } + + final UserInfo userInfo = userInfoDao.read(UUID.fromString(userID)); + + if(userInfo==null) //this should normally never happer + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("There's no such a user on the system. You shouldn't be here"); + + if(dmp==null || dmp.getId()==null) + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("DMP is null or dmp has null id"); + + try { + + DMP existingDMP = dMPDao.read(dmp.getId()); + + Set users = existingDMP.getUsers().parallelStream().filter(user -> user.getId().toString() != userInfo.getId().toString()).collect(Collectors.toSet()); + + users.add(userInfo); + dmp.setUsers(users); + + DMP updateddmp = dMPDao.update(dmp); + + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(updateddmp)); + } + catch(Exception ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); + } + + } diff --git a/dmp-backend/src/main/java/rest/entities/DataRepositories.java b/dmp-backend/src/main/java/rest/entities/DataRepositories.java index 74697f48d..bb016998e 100644 --- a/dmp-backend/src/main/java/rest/entities/DataRepositories.java +++ b/dmp-backend/src/main/java/rest/entities/DataRepositories.java @@ -69,16 +69,13 @@ public class DataRepositories { @Autowired private ResearcherDao researcherDao; @Autowired private ServiceDao serviceDao; - - private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer(); - // MANAGE DATAREPOSITORy(IES) @RequestMapping(method = RequestMethod.GET, value = { "/datarepos" }) public @ResponseBody ResponseEntity listDataRepositories(){ try { List allIDs = dataRepositoryDao.listAllIDs(); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -90,7 +87,7 @@ public class DataRepositories { public @ResponseBody ResponseEntity getDataRepository(@PathVariable("id") String id) { try { DataRepository dataRepository = dataRepositoryDao.read(UUID.fromString(id)); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(dataRepository)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(dataRepository)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage()); @@ -105,16 +102,7 @@ public class DataRepositories { try { 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); + return new ResponseEntity(SerializerProvider.toJson(allDataRepositories), HttpStatus.OK); } catch(Exception ex) { @@ -130,8 +118,8 @@ public class DataRepositories { 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.CREATED).body(SerializerProvider.toJson(createdDataRepository)); + } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create data repository!\"}"); } } diff --git a/dmp-backend/src/main/java/rest/entities/DatasetProfiles.java b/dmp-backend/src/main/java/rest/entities/DatasetProfiles.java index 023a66f43..ab645fd6d 100644 --- a/dmp-backend/src/main/java/rest/entities/DatasetProfiles.java +++ b/dmp-backend/src/main/java/rest/entities/DatasetProfiles.java @@ -72,8 +72,6 @@ public class DatasetProfiles { @Autowired private ResearcherDao researcherDao; @Autowired private ServiceDao serviceDao; - private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer(); - //FETCH BY DATASET PROFILE @@ -81,7 +79,7 @@ public class DatasetProfiles { public @ResponseBody ResponseEntity listDMPs(){ try { List allIDs = datasetProfileDao.listAllIDs(); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -93,7 +91,7 @@ public class DatasetProfiles { public @ResponseBody ResponseEntity getDatasetProfile(@PathVariable("id") String id) { try { DatasetProfile profile = datasetProfileDao.read(UUID.fromString(id)); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(profile)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(profile)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage()); @@ -105,18 +103,7 @@ public class DatasetProfiles { try { List allDatasetProfiles = datasetProfileDao.getAll(); - - //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom - List datasetProfilesStrL = allDatasetProfiles.stream().map((datasetProfileObj) -> { - try { - return objectMapper.writeValueAsString(datasetProfileObj); - } catch (JsonProcessingException e) { - e.printStackTrace(); - return ""; - } - }).collect(Collectors.toList()); - - return new ResponseEntity("["+String.join(",", datasetProfilesStrL)+"]", HttpStatus.OK); + return new ResponseEntity(SerializerProvider.toJson(allDatasetProfiles), HttpStatus.OK); } catch(Exception ex) { ex.printStackTrace(); @@ -149,17 +136,11 @@ public class DatasetProfiles { datasetProfile.setViewstyle(datasetprofileviewstyle); } - - try { - if(datasetProfile.getId()==null) - datasetProfileDao.create(datasetProfile); - else - datasetProfileDao.update(datasetProfile); - return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(datasetProfile)); - } catch (JsonProcessingException e) { - e.printStackTrace(); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create dataset profile!\"}"); - } + if(datasetProfile.getId()==null) + datasetProfileDao.create(datasetProfile); + else + datasetProfileDao.update(datasetProfile); + return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(datasetProfile)); } @@ -185,17 +166,11 @@ public class DatasetProfiles { datasetProfileViewstyleDao.update(datasetprofileviewstyle); } - - try { - if(datasetProfile.getId()==null) - datasetProfile = datasetProfileDao.create(datasetProfile); - else - datasetProfile = datasetProfileDao.update(datasetProfile); - return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(datasetProfile)); - } catch (JsonProcessingException e) { - e.printStackTrace(); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create dataset profile!\"}"); - } + if(datasetProfile.getId()==null) + datasetProfile = datasetProfileDao.create(datasetProfile); + else + datasetProfile = datasetProfileDao.update(datasetProfile); + return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(datasetProfile)); } @@ -213,7 +188,7 @@ public class DatasetProfiles { if(datasetprofileviewstyle != null) datasetProfileViewstyleDao.delete(datasetprofileviewstyle); datasetProfileDao.delete(datasetProfile); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(datasetProfile)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(datasetProfile)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not delete dataset profile!\"}"); diff --git a/dmp-backend/src/main/java/rest/entities/Datasets.java b/dmp-backend/src/main/java/rest/entities/Datasets.java index f4b8fd240..a9b12f99d 100644 --- a/dmp-backend/src/main/java/rest/entities/Datasets.java +++ b/dmp-backend/src/main/java/rest/entities/Datasets.java @@ -72,7 +72,6 @@ public class Datasets { @Autowired private ResearcherDao researcherDao; @Autowired private ServiceDao serviceDao; - private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer(); // FETCH BY DATASET(S) @@ -81,7 +80,7 @@ public class Datasets { public @ResponseBody ResponseEntity listDatasets(){ try { List allIDs = datasetDao.listAllIDs(); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -93,7 +92,7 @@ public class Datasets { public @ResponseBody ResponseEntity getDataset(@PathVariable("id") String id) { try { Dataset ds = datasetDao.read(UUID.fromString(id)); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(ds)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(ds)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage()); @@ -109,19 +108,7 @@ public class Datasets { try { List allDatasets = datasetDao.getAll(); - - - //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom - List datasetsStrL = allDatasets.stream().map((datasetObj) -> { - try { - return objectMapper.writeValueAsString(datasetObj); - } catch (JsonProcessingException e) { - e.printStackTrace(); - return ""; - } - }).collect(Collectors.toList()); - - return new ResponseEntity("["+String.join(",", datasetsStrL)+"]", HttpStatus.OK); + return new ResponseEntity(SerializerProvider.toJson(allDatasets), HttpStatus.OK); } catch(Exception ex) { ex.printStackTrace(); @@ -137,7 +124,7 @@ public class Datasets { dataset.setId(null); try { dataset = datasetDao.create(dataset); - return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(dataset)); + return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(dataset)); } catch(Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not create or update Dataset! Reason: " + e.getMessage()); diff --git a/dmp-backend/src/main/java/rest/entities/DmpProfiles.java b/dmp-backend/src/main/java/rest/entities/DmpProfiles.java index d768ffe01..d9df89a27 100644 --- a/dmp-backend/src/main/java/rest/entities/DmpProfiles.java +++ b/dmp-backend/src/main/java/rest/entities/DmpProfiles.java @@ -70,7 +70,6 @@ public class DmpProfiles { @Autowired private ResearcherDao researcherDao; @Autowired private ServiceDao serviceDao; - private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer(); // MANAGE DMPPROFILE(S) @@ -78,7 +77,7 @@ public class DmpProfiles { public @ResponseBody ResponseEntity listDmpProfiles(){ try { List allIDs = dMPProfileDao.listAllIDs(); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -92,7 +91,7 @@ public class DmpProfiles { public @ResponseBody ResponseEntity getDmpProfile(@PathVariable("id") String id) { try { DMPProfile dmpProfile = dMPProfileDao.read(UUID.fromString(id)); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(dmpProfile)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(dmpProfile)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage()); @@ -105,7 +104,7 @@ public class DmpProfiles { public @ResponseBody ResponseEntity listLabelIds(){ try { List allIDs = dMPProfileDao.listAllIDsLabels(); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -117,18 +116,7 @@ public class DmpProfiles { public @ResponseBody ResponseEntity getAllDmpProfiles(){ try { List allDmpProfiles = dMPProfileDao.getAll(); - - //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom - List dmpprofileStrL = allDmpProfiles.parallelStream().map((dmpProfileObj) -> { - try { - return objectMapper.writeValueAsString(dmpProfileObj); - } catch (JsonProcessingException e) { - return ""; - } - }).collect(Collectors.toList()); - - return new ResponseEntity("["+String.join(",", dmpprofileStrL)+"]", HttpStatus.OK); - + return new ResponseEntity(SerializerProvider.toJson(allDmpProfiles), HttpStatus.OK); } catch(Exception ex) { return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); @@ -139,10 +127,10 @@ public class DmpProfiles { @Transactional @RequestMapping(method = RequestMethod.POST, value = { "/dmpprofile/create" }, consumes = "application/json", produces="application/json") public @ResponseBody ResponseEntity setDmpProfile(@RequestBody DMPProfile dmpprofile) { - DMPProfile createdDMPProfile = dMPProfileDao.update(dmpprofile); try { - return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdDMPProfile)); - } catch (JsonProcessingException e) { + DMPProfile createdDMPProfile = dMPProfileDao.update(dmpprofile); + return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(createdDMPProfile)); + } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create 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 b4b86a869..70b58b069 100644 --- a/dmp-backend/src/main/java/rest/entities/Organisations.java +++ b/dmp-backend/src/main/java/rest/entities/Organisations.java @@ -70,7 +70,6 @@ public class Organisations { @Autowired private ServiceDao serviceDao; - private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer(); // MANAGE ORGANISATIONS(S) @@ -78,7 +77,7 @@ public class Organisations { public @ResponseBody ResponseEntity listOrganisations(){ try { List allIDs = organisationDao.listAllIDs(); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -90,7 +89,7 @@ public class Organisations { public @ResponseBody ResponseEntity getOrganisations(@PathVariable("id") String id) { try { Organisation organisation = organisationDao.read(UUID.fromString(id)); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(organisation)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(organisation)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage()); @@ -102,18 +101,7 @@ public class Organisations { public @ResponseBody ResponseEntity getAllOrganisations(){ try { List allOrganisations = organisationDao.getAll(); - - //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom - List organisationStrL = allOrganisations.parallelStream().map((organisationObj) -> { - try { - return objectMapper.writeValueAsString(organisationObj); - } catch (JsonProcessingException e) { - return ""; - } - }).collect(Collectors.toList()); - - return new ResponseEntity("["+String.join(",", organisationStrL)+"]", HttpStatus.OK); - + return new ResponseEntity(SerializerProvider.toJson(allOrganisations), HttpStatus.OK); } catch(Exception ex) { return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); @@ -124,10 +112,10 @@ public class Organisations { @Transactional @RequestMapping(method = RequestMethod.POST, value = { "/organisation/create" }, consumes = "application/json", produces="application/json") public @ResponseBody ResponseEntity setOrganisation(@RequestBody Organisation organisation) { - Organisation createdOrganisation = organisationDao.update(organisation); try { - return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdOrganisation)); - } catch (JsonProcessingException e) { + Organisation createdOrganisation = organisationDao.update(organisation); + return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(createdOrganisation)); + } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create organisation!\"}"); } } diff --git a/dmp-backend/src/main/java/rest/entities/Projects.java b/dmp-backend/src/main/java/rest/entities/Projects.java index b527b0f08..006d27234 100644 --- a/dmp-backend/src/main/java/rest/entities/Projects.java +++ b/dmp-backend/src/main/java/rest/entities/Projects.java @@ -1,7 +1,9 @@ package rest.entities; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; @@ -80,15 +82,13 @@ public class Projects { @Autowired private UserInfoDao userInfoDao; - private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer(); - // MANAGE PROJECT(S) @RequestMapping(method = RequestMethod.GET, value = { "/projects" }, produces="application/json") public @ResponseBody ResponseEntity listProjects(){ try { List allIDs = projectDao.listAllIDs(); - return ResponseEntity.status(HttpStatus.OK).body(allIDs); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -102,7 +102,7 @@ public class Projects { System.out.println(project.getId().toString()); - return ResponseEntity.status(HttpStatus.OK).body(project); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(project)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage()); @@ -126,18 +126,7 @@ public class Projects { public @ResponseBody ResponseEntity getAllProjects(){ try { List allProjects = projectDao.getAll(); - - //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom - List projectStrL = allProjects.parallelStream().map((projectObj) -> { - try { - return objectMapper.writeValueAsString(projectObj); - } catch (JsonProcessingException e) { - return ""; - } - }).collect(Collectors.toList()); - - return new ResponseEntity("["+String.join(",", projectStrL)+"]", HttpStatus.OK); - + return new ResponseEntity(SerializerProvider.toJson(allProjects), HttpStatus.OK); } catch(Exception ex) { return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); @@ -175,8 +164,8 @@ public class Projects { public @ResponseBody ResponseEntity getProjectDmps(@RequestBody Project project) { try { Set dmps = projectDao.read(project.getId()).getDmps(); - return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(dmps)); - } catch (JsonProcessingException e) { + return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(dmps)); + } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create Project!\"}"); } } @@ -200,11 +189,19 @@ public class Projects { UserInfo userInfo = userInfoDao.read(UUID.fromString(userID)); - if(userInfo==null) //this should normally never happer + if(userInfo==null) //this should normally never happen return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("There's no such a user on the system. You shouldn't be here"); + + Map userProjects = new HashMap(); + + userInfo.getDmps().forEach( dmp -> { + Project proj = dmp.getProject(); + userProjects.put(proj.getId(), proj); + }); + try { - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(userInfo.getProjects())); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(userProjects.values())); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -236,16 +233,19 @@ public class Projects { Project newproj = projectDao.create(project); - Set userProjects = userInfo.getProjects(); - if(userProjects==null) - userProjects = new HashSet(); - userProjects.add(newproj); + DMP newdmp = new DMP(); + newdmp.setId(null); + newdmp.setLabel("Auto-Generated"); + newdmp.setVersion(1); + newdmp.setProject(newproj); - userInfo.setProjects(userProjects); + Set users = new HashSet(); + users.add(userInfo); + newdmp.setUsers(users); - userInfo = userInfoDao.update(userInfo); + newdmp = dMPDao.create(newdmp); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(userInfo)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(newdmp)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -253,15 +253,15 @@ public class Projects { } - @Transactional - @RequestMapping(method = RequestMethod.POST, value = { "/project/updateofuser" }, produces="text/plain") - public @ResponseBody ResponseEntity updateProjectOfUser(@RequestBody Project project){ - - if(project.getId()==null) - return ResponseEntity.status(HttpStatus.NOT_MODIFIED).body("Cannot update, id was null"); - return setProject(project); - - } +// @Transactional +// @RequestMapping(method = RequestMethod.POST, value = { "/project/updateofuser" }, produces="text/plain") +// public @ResponseBody ResponseEntity updateProjectOfUser(@RequestBody Project project){ +// +// if(project.getId()==null) +// return ResponseEntity.status(HttpStatus.NOT_MODIFIED).body("Cannot update, id was null"); +// return setProject(project); +// +// } diff --git a/dmp-backend/src/main/java/rest/entities/Registries.java b/dmp-backend/src/main/java/rest/entities/Registries.java index b707d68b7..bca3099eb 100644 --- a/dmp-backend/src/main/java/rest/entities/Registries.java +++ b/dmp-backend/src/main/java/rest/entities/Registries.java @@ -70,15 +70,13 @@ public class Registries { @Autowired private ServiceDao serviceDao; - private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer(); - // MANAGE REGISTRY(IES) @RequestMapping(method = RequestMethod.GET, value = { "/registries" }) public @ResponseBody ResponseEntity listRegistries(){ try { List allIDs = registryDao.listAllIDs(); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -90,7 +88,7 @@ public class Registries { public @ResponseBody ResponseEntity getRegistries(@PathVariable("id") String id) { try { Registry registry = registryDao.read(UUID.fromString(id)); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(registry)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(registry)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage()); @@ -103,7 +101,7 @@ public class Registries { public @ResponseBody ResponseEntity listLabelIds(){ try { List allIDs = registryDao.listAllIDsLabels(); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -116,16 +114,7 @@ public class Registries { try { List allRegistries = registryDao.getAll(); - //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom - List registryStrL = allRegistries.parallelStream().map((registryObj) -> { - try { - return objectMapper.writeValueAsString(registryObj); - } catch (JsonProcessingException e) { - return ""; - } - }).collect(Collectors.toList()); - - return new ResponseEntity("["+String.join(",", registryStrL)+"]", HttpStatus.OK); + return new ResponseEntity(SerializerProvider.toJson(allRegistries), HttpStatus.OK); } catch(Exception ex) { @@ -139,8 +128,8 @@ public class Registries { public @ResponseBody ResponseEntity setRegistry(@RequestBody Registry registry) { Registry createdRegistry = registryDao.update(registry); try { - return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdRegistry)); - } catch (JsonProcessingException e) { + return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(createdRegistry)); + } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create registry!\"}"); } } diff --git a/dmp-backend/src/main/java/rest/entities/Researchers.java b/dmp-backend/src/main/java/rest/entities/Researchers.java index faecb5781..57caabfee 100644 --- a/dmp-backend/src/main/java/rest/entities/Researchers.java +++ b/dmp-backend/src/main/java/rest/entities/Researchers.java @@ -70,15 +70,13 @@ public class Researchers { @Autowired private ServiceDao serviceDao; - private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer(); - // MANAGE RESEARCHER(S) @RequestMapping(method = RequestMethod.GET, value = { "/researchers" }) public @ResponseBody ResponseEntity listResearchers(){ try { List allIDs = researcherDao.listAllIDs(); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -90,7 +88,7 @@ public class Researchers { public @ResponseBody ResponseEntity getResearchers(@PathVariable("id") String id) { try { Researcher researcher = researcherDao.read(UUID.fromString(id)); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(researcher)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(researcher)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage()); @@ -101,7 +99,7 @@ public class Researchers { public @ResponseBody ResponseEntity getResearcherByEmail(@RequestParam("email") String email){ try { Researcher researcher = researcherDao.getResearcherByEmail(email); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(researcher)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(researcher)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage()); @@ -115,16 +113,7 @@ public class Researchers { try { 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); + return new ResponseEntity(SerializerProvider.toJson(allResearchers), HttpStatus.OK); } catch(Exception ex) { @@ -138,8 +127,8 @@ public class Researchers { 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.CREATED).body(SerializerProvider.toJson(createdResearcher)); + } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create researcher!\"}"); } } diff --git a/dmp-backend/src/main/java/rest/entities/Services.java b/dmp-backend/src/main/java/rest/entities/Services.java index d73a29b4c..71a898d17 100644 --- a/dmp-backend/src/main/java/rest/entities/Services.java +++ b/dmp-backend/src/main/java/rest/entities/Services.java @@ -71,9 +71,6 @@ public class Services { @Autowired private ServiceDao serviceDao; - private ObjectMapper objectMapper = - SerializerProvider.getJsonSerializer(); -// new ObjectMapper(); // MANAGE SERVICE(S) @@ -81,7 +78,7 @@ public class Services { public @ResponseBody ResponseEntity listServices(){ try { List allIDs = serviceDao.listAllIDs(); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -93,7 +90,7 @@ public class Services { public @ResponseBody ResponseEntity getServices(@PathVariable("id") String id) { try { Service service = serviceDao.read(UUID.fromString(id)); - return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(service)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(service)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage()); @@ -107,16 +104,7 @@ public class Services { try { List allServices = serviceDao.getAll(); - //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom - List serviceStrL = allServices.parallelStream().map((serviceObj) -> { - try { - return objectMapper.writeValueAsString(serviceObj); - } catch (JsonProcessingException e) { - return ""; - } - }).collect(Collectors.toList()); - - return new ResponseEntity("["+String.join(",", serviceStrL)+"]", HttpStatus.OK); + return new ResponseEntity(SerializerProvider.toJson(allServices), HttpStatus.OK); } catch(Exception ex) { @@ -127,20 +115,12 @@ public class Services { @Transactional @RequestMapping(method = RequestMethod.POST, value = { "/service/create" }, consumes = "application/json", produces="application/json") - public @ResponseBody ResponseEntity setService(@RequestBody String serviceSTR) { - - System.out.println(serviceSTR); - Service service = null; - try { - service = objectMapper.readValue(serviceSTR, Service.class); - } catch (IOException e1) { - e1.printStackTrace(); - } + public @ResponseBody ResponseEntity setService(@RequestBody Service service) { Service createdService = serviceDao.update(service); try { - return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdService)); - } catch (JsonProcessingException e) { + return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(createdService)); + } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create service entity!\"}"); } } diff --git a/dmp-backend/src/main/java/rest/entities/Users.java b/dmp-backend/src/main/java/rest/entities/Users.java index 25ddcf8d3..0a611c286 100644 --- a/dmp-backend/src/main/java/rest/entities/Users.java +++ b/dmp-backend/src/main/java/rest/entities/Users.java @@ -70,11 +70,6 @@ public class Users { @Autowired private ServiceDao serviceDao; - private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer(); - - - - diff --git a/dmp-backend/src/main/java/security/TokenSessionManager.java b/dmp-backend/src/main/java/security/TokenSessionManager.java index 8b4e36047..c1a9a1ec2 100644 --- a/dmp-backend/src/main/java/security/TokenSessionManager.java +++ b/dmp-backend/src/main/java/security/TokenSessionManager.java @@ -74,6 +74,8 @@ public class TokenSessionManager { return sb.toString(); } - +// public static void main(String [] args) throws NoSuchAlgorithmException { +// System.out.println(TokenSessionManager.getInstance().hashPassword("apa$$2gu3$$")); +// } } diff --git a/dmp-backend/src/test/java/TestRest.java b/dmp-backend/src/test/java/TestRest.java index 6bfad4b28..823c3ca2d 100644 --- a/dmp-backend/src/test/java/TestRest.java +++ b/dmp-backend/src/test/java/TestRest.java @@ -64,7 +64,7 @@ public class TestRest { Gson gson = new Gson(); - private static String userID = "0f51f686-a2ce-47c1-9433-00736787aa88"; + private static String userID = "332ffc36-bd51-4d4e-bf9a-ffb01fdee05a"; private ApplicationContext context; @@ -159,12 +159,30 @@ public class TestRest { // @Test public void testProject() { - System.out.println(projectsService.listProjects().getBody()); - System.out.println(dmpsService.listDMPs().getBody()); +// System.out.println(projectsService.listProjects().getBody()); +// System.out.println(dmpsService.listDMPs().getBody()); + + System.out.println(projectsService.getProjectsOfUser().getBody()); + + assertEquals("aaa", "aaa"); + + } + + @Test + public void testDmpUser() { + + System.out.println(dmpsService.getDmpsOfUser().getBody()); + + +// System.out.println(projectsService.listProjects().getBody()); +// System.out.println(dmpsService.listDMPs().getBody()); + + assertEquals("aaa", "aaa"); } + } From a256ecff257c1a16a7d3ff0cc8830dc779a6c3a4 Mon Sep 17 00:00:00 2001 From: Nikolaos Laskaris Date: Fri, 27 Oct 2017 13:56:24 +0300 Subject: [PATCH 2/2] minor change --- dmp-backend/src/main/java/rest/entities/Projects.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dmp-backend/src/main/java/rest/entities/Projects.java b/dmp-backend/src/main/java/rest/entities/Projects.java index 006d27234..8caac83cc 100644 --- a/dmp-backend/src/main/java/rest/entities/Projects.java +++ b/dmp-backend/src/main/java/rest/entities/Projects.java @@ -245,7 +245,7 @@ public class Projects { newdmp = dMPDao.create(newdmp); - return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(newdmp)); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(newproj)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());