2017-10-12 14:04:38 +02:00
package rest.entities ;
2017-09-14 12:37:36 +02:00
2017-10-23 18:06:24 +02:00
import java.util.HashSet ;
2017-09-14 12:37:36 +02:00
import java.util.List ;
2017-10-23 18:06:24 +02:00
import java.util.Set ;
2017-09-14 12:37:36 +02:00
import java.util.UUID ;
2017-10-06 19:20:05 +02:00
import java.util.stream.Collectors ;
import javax.transaction.Transactional ;
2017-09-14 12:37:36 +02:00
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.http.HttpStatus ;
import org.springframework.http.ResponseEntity ;
2017-10-20 17:11:40 +02:00
import org.springframework.security.core.context.SecurityContextHolder ;
2017-09-14 12:37:36 +02:00
import org.springframework.web.bind.annotation.CrossOrigin ;
import org.springframework.web.bind.annotation.PathVariable ;
import org.springframework.web.bind.annotation.RequestBody ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RequestMethod ;
2017-10-04 11:48:21 +02:00
import org.springframework.web.bind.annotation.RequestParam ;
2017-09-14 12:37:36 +02:00
import org.springframework.web.bind.annotation.ResponseBody ;
import org.springframework.web.bind.annotation.RestController ;
2017-10-20 17:11:40 +02:00
import com.fasterxml.jackson.annotation.JsonInclude.Include ;
2017-10-04 11:48:21 +02:00
import com.fasterxml.jackson.core.JsonProcessingException ;
2017-10-20 17:11:40 +02:00
import com.fasterxml.jackson.databind.DeserializationFeature ;
2017-09-14 12:37:36 +02:00
import com.fasterxml.jackson.databind.ObjectMapper ;
2017-10-06 19:20:05 +02:00
import com.mchange.v2.sql.filter.SynchronizedFilterDataSource ;
2017-09-14 12:37:36 +02:00
import dao.entities.DMPDao ;
import dao.entities.DMPProfileDao ;
import dao.entities.DataRepositoryDao ;
import dao.entities.DatasetDao ;
import dao.entities.DatasetProfileDao ;
import dao.entities.DatasetProfileRulesetDao ;
import dao.entities.DatasetProfileViewstyleDao ;
import dao.entities.OrganisationDao ;
import dao.entities.ProjectDao ;
import dao.entities.RegistryDao ;
import dao.entities.ResearcherDao ;
import dao.entities.ServiceDao ;
2017-10-20 17:11:40 +02:00
import dao.entities.UserInfoDao ;
2017-09-14 12:37:36 +02:00
import entities.DMP ;
2017-10-06 19:20:05 +02:00
import entities.DMPProfile ;
2017-09-14 12:37:36 +02:00
import entities.Dataset ;
2017-10-04 11:48:21 +02:00
import entities.DatasetProfile ;
import entities.DatasetProfileRuleset ;
2017-09-14 12:37:36 +02:00
import entities.Project ;
2017-10-20 17:11:40 +02:00
import entities.UserInfo ;
2017-10-04 11:48:21 +02:00
import entities.responses.IDLabelPair ;
2017-10-20 17:11:40 +02:00
import helpers.SerializerProvider ;
2017-09-14 12:37:36 +02:00
import helpers.Transformers ;
2017-10-04 11:48:21 +02:00
import responses.RestResponse ;
2017-09-14 12:37:36 +02:00
@RestController
@CrossOrigin
2017-10-04 11:48:21 +02:00
public class DMPs {
2017-09-14 12:37:36 +02:00
@Autowired private DataRepositoryDao dataRepositoryDao ;
@Autowired private DatasetDao datasetDao ;
@Autowired private DatasetProfileDao datasetProfileDao ;
@Autowired private DatasetProfileRulesetDao datasetProfileRulesetDao ;
@Autowired private DatasetProfileViewstyleDao datasetProfileViewstyleDao ;
@Autowired private DMPDao dMPDao ;
@Autowired private DMPProfileDao dMPProfileDao ;
@Autowired private OrganisationDao organisationDao ;
@Autowired private ProjectDao projectDao ;
@Autowired private RegistryDao registryDao ;
@Autowired private ResearcherDao researcherDao ;
@Autowired private ServiceDao serviceDao ;
2017-10-20 17:11:40 +02:00
@Autowired private UserInfoDao userInfoDao ;
2017-09-14 12:37:36 +02:00
2017-10-20 17:11:40 +02:00
private ObjectMapper objectMapper = SerializerProvider . getJsonSerializer ( ) ;
2017-09-27 18:15:39 +02:00
2017-09-14 12:37:36 +02:00
// FETCH BY DMP(S)
2017-10-06 19:20:05 +02:00
@RequestMapping ( method = RequestMethod . GET , value = { " /dmps " } , produces = " text/plain " )
2017-09-14 12:37:36 +02:00
public @ResponseBody ResponseEntity < Object > listDMPs ( ) {
try {
List < UUID > allIDs = dMPDao . listAllIDs ( ) ;
2017-10-20 17:11:40 +02:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( objectMapper . writeValueAsString ( allIDs ) ) ;
2017-09-14 12:37:36 +02:00
}
catch ( Exception ex ) {
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " Serialization issue: " + ex . getMessage ( ) ) ;
}
}
2017-10-04 11:48:21 +02:00
2017-10-06 19:20:05 +02:00
@RequestMapping ( method = RequestMethod . GET , value = { " /dmps/{id} " } , produces = " application/json " )
public @ResponseBody ResponseEntity < Object > getDMP ( @PathVariable ( " id " ) String id ) {
2017-10-04 11:48:21 +02:00
try {
2017-10-06 19:20:05 +02:00
DMP dmp = dMPDao . read ( UUID . fromString ( id ) ) ;
2017-10-20 17:11:40 +02:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( objectMapper . writeValueAsString ( dmp ) ) ;
2017-10-04 11:48:21 +02:00
}
catch ( Exception ex ) {
2017-10-20 17:11:40 +02:00
ex . printStackTrace ( ) ;
2017-10-06 19:20:05 +02:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( " Erroneous input: " + ex . getMessage ( ) ) ;
2017-10-04 11:48:21 +02:00
}
}
2017-09-14 12:37:36 +02:00
2017-10-06 19:20:05 +02:00
@RequestMapping ( method = RequestMethod . GET , value = { " /dmp/listDMPLabelID " } , produces = " text/plain " )
public @ResponseBody ResponseEntity < Object > listDmpLabelID ( ) {
2017-09-14 12:37:36 +02:00
try {
2017-10-06 19:20:05 +02:00
List < IDLabelPair > allIDLabels = dMPDao . listAllIDsLabels ( ) ;
2017-10-20 17:11:40 +02:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( objectMapper . writeValueAsString ( allIDLabels ) ) ;
2017-09-14 12:37:36 +02:00
}
catch ( Exception ex ) {
2017-10-06 19:20:05 +02:00
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " Serialization issue: " + ex . getMessage ( ) ) ;
2017-09-14 12:37:36 +02:00
}
}
/ * *
* This should be called on extreme cases . It ' s computationally intensive
* /
2017-10-06 19:20:05 +02:00
@RequestMapping ( method = RequestMethod . GET , value = { " /dmp/getAll " } , produces = " application/json " )
2017-09-14 12:37:36 +02:00
public @ResponseBody ResponseEntity < Object > getAllDMPs ( ) {
2017-10-20 17:11:40 +02:00
2017-09-14 12:37:36 +02:00
try {
List < DMP > allDMPs = dMPDao . getAll ( ) ;
2017-10-06 19:20:05 +02:00
//sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom
List < String > dmpStrL = allDMPs . parallelStream ( ) . map ( ( dmpObj ) - > {
try {
return objectMapper . writeValueAsString ( dmpObj ) ;
} catch ( JsonProcessingException e ) {
return " " ;
}
} ) . collect ( Collectors . toList ( ) ) ;
return new ResponseEntity < Object > ( " [ " + String . join ( " , " , dmpStrL ) + " ] " , HttpStatus . OK ) ;
2017-09-14 12:37:36 +02:00
}
catch ( Exception ex ) {
2017-10-24 18:33:51 +02:00
ex . printStackTrace ( ) ;
2017-10-06 19:20:05 +02:00
return new ResponseEntity < > ( null , HttpStatus . INTERNAL_SERVER_ERROR ) ;
2017-09-14 12:37:36 +02:00
}
}
2017-10-06 19:20:05 +02:00
@Transactional
@RequestMapping ( method = RequestMethod . POST , value = { " /dmp/create " } , consumes = " application/json " , produces = " application/json " )
2017-10-23 18:06:24 +02:00
public @ResponseBody ResponseEntity < Object > createDMP ( @RequestBody DMP dmp ) {
2017-10-06 19:20:05 +02:00
DMP createdDmp = dMPDao . update ( dmp ) ;
try {
return ResponseEntity . status ( HttpStatus . CREATED ) . body ( objectMapper . writeValueAsString ( createdDmp ) ) ;
} catch ( JsonProcessingException e ) {
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " { \" msg \" : \" Could not create DMP! \" " ) ;
}
}
2017-10-23 18:06:24 +02:00
@Transactional
@RequestMapping ( method = RequestMethod . POST , value = { " /dmp/update " } , consumes = " application/json " , produces = " application/json " )
public @ResponseBody ResponseEntity < Object > updateDMP ( @RequestBody DMP dmp ) {
DMP updatedDMP = dMPDao . update ( dmp ) ;
try {
return ResponseEntity . status ( HttpStatus . CREATED ) . body ( objectMapper . writeValueAsString ( updatedDMP ) ) ;
} catch ( JsonProcessingException e ) {
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " { \" msg \" : \" Could not update DMP! \" " ) ;
}
}
2017-10-20 17:11:40 +02:00
/ *
2017-10-06 19:20:05 +02:00
@RequestMapping ( method = RequestMethod . POST , value = { " /dmp/set/full " } , consumes = " application/json " , produces = " application/json " )
public @ResponseBody ResponseEntity < Object > setFullDMP ( @RequestBody DMP dmp ) {
2017-09-14 12:37:36 +02:00
//This function is a little bit tricky to implement (due to the irregular ORM mappings of the hibernate).
// Please make changes only if you are sure about what you're altering.
Dataset dataset = SerializationUtils . clone ( dmp . getDataset ( ) ) ;
dmp . setDataset ( null ) ;
int failsDMP = 0 ;
String reasonDmp = " " ;
DMP storedDMP = null ;
//try first to create DMP
try {
storedDMP = dMPDao . create ( dmp ) ;
}
catch ( Exception e ) {
e . printStackTrace ( ) ;
failsDMP + + ;
reasonDmp + = e . getMessage ( ) ;
//try updating DMP
try {
storedDMP = dMPDao . update ( dmp ) ;
}
catch ( Exception ex ) {
reasonDmp + = ( System . lineSeparator ( ) + e . getMessage ( ) ) ;
failsDMP + + ;
ex . printStackTrace ( ) ;
}
}
if ( failsDMP = = 2 )
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " Could not create or update DMP! Reason: " + reasonDmp ) ;
dataset . setDmp ( storedDMP ) ; //very important!
int failsDataset = 0 ;
String reasonDataset = " " ;
Dataset storedDataset = null ;
if ( dataset ! = null ) {
//try first to create DMP
try {
storedDataset = datasetDao . create ( dataset ) ;
}
catch ( Exception e ) {
e . printStackTrace ( ) ;
failsDataset + + ;
reasonDataset + = e . getMessage ( ) ;
//try updating DMP
try {
storedDataset = datasetDao . update ( dataset ) ;
}
catch ( Exception ex ) {
reasonDataset + = ( System . lineSeparator ( ) + e . getMessage ( ) ) ;
failsDataset + + ;
ex . printStackTrace ( ) ;
}
}
}
2017-10-06 19:20:05 +02:00
String respBody ;
try {
respBody = objectMapper . writeValueAsString ( storedDMP . getId ( ) ) ;
}
catch ( JsonProcessingException ex ) {
respBody = " { \" id \" : \" " + storedDMP . getId ( ) + " \" } " ;
}
2017-09-14 12:37:36 +02:00
if ( failsDataset ! = 2 ) {
if ( failsDMP = = 0 )
2017-10-06 19:20:05 +02:00
return ResponseEntity . status ( HttpStatus . CREATED ) . body ( respBody ) ;
2017-09-14 12:37:36 +02:00
else if ( failsDMP = = 1 )
return ResponseEntity . status ( HttpStatus . CREATED ) . body ( " Updated DMP with id: " + storedDMP . getId ( ) ) ;
else
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " Could not create DMP! Reason: " + reasonDmp ) ;
}
else {
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " Could not create DMP! Failed to create or update its Dataset. Reason: " + reasonDataset ) ;
}
}
2017-10-20 17:11:40 +02:00
* /
2017-09-14 12:37:36 +02:00
2017-10-24 18:33:51 +02:00
@RequestMapping ( method = RequestMethod . POST , value = { " /dmp/getdatasets " } , consumes = " application/json " , produces = " application/json " )
public @ResponseBody ResponseEntity < Object > getDatasetsOfDMP ( @RequestBody DMP dmp ) {
Set < Dataset > datasets = dMPDao . read ( dmp . getId ( ) ) . getDataset ( ) ;
try {
return ResponseEntity . status ( HttpStatus . OK ) . body ( objectMapper . writeValueAsString ( datasets ) ) ;
} catch ( JsonProcessingException e ) {
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " { \" msg \" : \" Could not get datasets of DMP! \" " ) ;
}
}
2017-10-06 19:20:05 +02:00
@RequestMapping ( method = RequestMethod . POST , value = { " /dmp/delete " } , consumes = " application/json " , produces = " text/plain " )
public @ResponseBody ResponseEntity < Object > delete ( @RequestBody DMP dmp ) {
DMP d = new DMP ( ) ;
d . setId ( dmp . getId ( ) ) ;
try {
dMPDao . delete ( d ) ;
return ResponseEntity . status ( HttpStatus . CREATED ) . body ( " DELETED! " ) ;
} catch ( Exception e ) {
2017-10-20 12:03:55 +02:00
e . printStackTrace ( ) ;
2017-10-06 19:20:05 +02:00
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " { \" msg \" : \" Could not Delete DMP! \" " ) ;
}
}
2017-10-23 18:06:24 +02:00
////////////////////////////////
//// USER - RELATED ACTIONS ////
////////////////////////////////
@RequestMapping ( method = RequestMethod . GET , value = { " /dmp/getofuser " } , produces = " text/plain " )
public @ResponseBody ResponseEntity < Object > getDmpsOfUser ( ) {
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 " ) ;
}
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 {
Set < DMP > userDMPs = new HashSet < DMP > ( ) ;
userInfo . getProjects ( ) . forEach ( project - > {
userDMPs . addAll ( project . getDmps ( ) ) ;
} ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( objectMapper . writeValueAsString ( userDMPs ) ) ;
}
catch ( Exception ex ) {
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " Serialization issue: " + ex . getMessage ( ) ) ;
}
2017-09-14 12:37:36 +02:00
}
2017-10-23 18:06:24 +02:00
@RequestMapping ( method = RequestMethod . GET , value = { " /dmp/createforproject " } , produces = " text/plain " )
public @ResponseBody ResponseEntity < Object > createDmpOfProject ( @RequestParam ( " projectid " ) String projectid , @RequestBody DMP dmp ) {
UUID projIdUuid ;
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 " ) ;
}
try {
dmp . setId ( null ) ;
dmp = dMPDao . create ( dmp ) ;
Project project = projectDao . read ( projIdUuid ) ;
Set < DMP > dmps = project . getDmps ( ) ;
if ( dmps = = null )
dmps = new HashSet < DMP > ( ) ;
dmps . add ( dmp ) ;
project . setDmps ( dmps ) ;
project = projectDao . update ( project ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( objectMapper . writeValueAsString ( dmp ) ) ;
}
catch ( Exception ex ) {
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " Serialization issue: " + ex . getMessage ( ) ) ;
}
2017-09-21 13:08:40 +02:00
}
2017-10-23 18:06:24 +02:00
2017-09-14 12:37:36 +02:00
}