MInor changes on hibernate entities

This commit is contained in:
Nikolaos Laskaris 2017-10-17 14:45:11 +03:00
parent f2869169c2
commit 0e6c06033d
9 changed files with 120 additions and 62 deletions

View File

@ -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 {

View File

@ -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<Object> listDataRepositories(){
try {
List<UUID> 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<Object> 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<Object> 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<Object> getAllDataRepositories(){
try {
storedDataRepository = dataRepositoryDao.create(dataRepository);
return ResponseEntity.status(HttpStatus.CREATED).body("Created dataRepository with id: " + storedDataRepository.getId());
List<DataRepository> allDataRepositories = dataRepositoryDao.getAll();
//sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom
List<String> datareposStrL = allDataRepositories.parallelStream().map((datarepoObj) -> {
try {
return objectMapper.writeValueAsString(datarepoObj);
} catch (JsonProcessingException e) {
return "";
}
}).collect(Collectors.toList());
return new ResponseEntity<Object>("["+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<Object> 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<Object> 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!\"}");
}
}

View File

@ -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!\"}");
}
}

View File

@ -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!\"}");
}
}

View File

@ -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!\"}");
}
}

View File

@ -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!\"}");
}
}

View File

@ -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<Object> 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<Object> getAllResearchers(){
try {
storedResearcher = researcherDao.create(researcher);
return ResponseEntity.status(HttpStatus.CREATED).body("Created researcher with id: " + storedResearcher.getId());
List<Researcher> allResearchers = researcherDao.getAll();
//sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom
List<String> researcherStrL = allResearchers.parallelStream().map((researcherObj) -> {
try {
return objectMapper.writeValueAsString(researcherObj);
} catch (JsonProcessingException e) {
return "";
}
}).collect(Collectors.toList());
return new ResponseEntity<Object>("["+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<Object> 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<Object> 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!\"}");
}
}
}

View File

@ -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!\"}");
}
}

View File

@ -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;