2017-10-04 11:48:21 +02:00
package rest ;
import java.util.List ;
import java.util.UUID ;
import org.apache.commons.lang3.SerializationUtils ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.http.HttpStatus ;
import org.springframework.http.MediaType ;
import org.springframework.http.ResponseEntity ;
import org.springframework.util.MultiValueMap ;
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 ;
import org.springframework.web.bind.annotation.RequestParam ;
import org.springframework.web.bind.annotation.ResponseBody ;
import org.springframework.web.bind.annotation.RestController ;
import com.fasterxml.jackson.core.JsonProcessingException ;
import com.fasterxml.jackson.databind.ObjectMapper ;
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 ;
import entities.DMP ;
import entities.Dataset ;
import entities.DatasetProfile ;
import entities.DatasetProfileRuleset ;
import entities.Project ;
import helpers.Transformers ;
import responses.RestResponse ;
@RestController
@CrossOrigin
public class Datasets {
@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 ;
// FETCH BY DATASET(S)
@RequestMapping ( method = RequestMethod . GET , value = { " /dataset " } )
public @ResponseBody ResponseEntity < Object > listDatasets ( ) {
try {
List < UUID > allIDs = datasetDao . listAllIDs ( ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ObjectMapper ( ) . writeValueAsString ( allIDs ) ) ;
}
catch ( Exception ex ) {
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " Serialization issue: " + ex . getMessage ( ) ) ;
}
}
@RequestMapping ( method = RequestMethod . GET , value = { " /dataset/{id} " } )
public @ResponseBody ResponseEntity < Object > getDataset ( @PathVariable ( " id " ) String id ) {
try {
Dataset ds = datasetDao . read ( UUID . fromString ( id ) ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ObjectMapper ( ) . writeValueAsString ( ds ) ) ;
}
catch ( Exception ex ) {
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( " Erroneous input: " + ex . getMessage ( ) ) ;
}
}
/ * *
* This should be called on extreme cases . It ' s computationally intensive
* /
@RequestMapping ( method = RequestMethod . GET , value = { " /getAllDatasets " } )
public @ResponseBody ResponseEntity < Object > getAllDatasets ( ) {
try {
List < Dataset > allDatasets = datasetDao . getAll ( ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ObjectMapper ( ) . writeValueAsString ( allDatasets ) ) ;
}
catch ( Exception ex ) {
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " Serialization issue: " + ex . getMessage ( ) ) ;
}
}
@RequestMapping ( method = RequestMethod . POST , value = { " /setDataset " } , consumes = " application/json " )
public @ResponseBody ResponseEntity < Object > setDataset ( @RequestBody Dataset dataset ) {
String reason = " " ;
Dataset storedDataset = null ;
//try first to create
try {
storedDataset = datasetDao . create ( dataset ) ;
RestResponse rr = new RestResponse ( " Created dataset with id: " + storedDataset . toString ( ) , storedDataset . getId ( ) . toString ( ) ) ;
return ResponseEntity . status ( HttpStatus . CREATED ) . body ( rr . toString ( ) ) ;
}
catch ( Exception e ) {
e . printStackTrace ( ) ;
reason + = e . getMessage ( ) ;
//try updating
try {
storedDataset = datasetDao . update ( dataset ) ;
RestResponse rr = new RestResponse ( " Updated dataset with id: " + storedDataset . toString ( ) , storedDataset . getId ( ) . toString ( ) ) ;
return ResponseEntity . status ( HttpStatus . CREATED ) . body ( rr . toString ( ) ) ;
}
catch ( Exception ex ) {
ex . printStackTrace ( ) ;
reason + = ( System . lineSeparator ( ) + e . getMessage ( ) ) ;
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " Could not create or update Dataset! Reason: " + reason ) ;
}
}
}
@RequestMapping ( method = RequestMethod . POST , value = { " /deleteDataset " } , consumes = " application/json " )
public @ResponseBody ResponseEntity < Object > deleteDataset ( @RequestBody Dataset dataset ) {
//if we want to make sure it won't cascade up to other (child) components, we can just unhook them by setting them = new ones
// e.g: DMP dmp = new DMP() and then dataset.setDMP(dmp)
try {
datasetDao . delete ( dataset ) ;
RestResponse rr = new RestResponse ( " Deleted dataset with id: " + dataset . getId ( ) . toString ( ) , dataset . getId ( ) . toString ( ) ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( rr . toString ( ) ) ;
}
catch ( Exception e ) {
e . printStackTrace ( ) ;
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " Could not delete Dataset! Reason: " + e . getMessage ( ) ) ;
}
}
2017-10-04 19:44:41 +02:00
@RequestMapping ( method = RequestMethod . GET , value = { " /assignDMPToDataset " } )
public @ResponseBody ResponseEntity < Object > assignDMPToDataset ( @RequestParam ( " datasetID " ) String datasetID , @RequestParam ( " dmpID " ) String dmpID ) {
DMP dmp = null ;
Dataset dataset = null ;
try {
dmp = dMPDao . read ( UUID . fromString ( dmpID ) ) ;
dataset = datasetDao . read ( UUID . fromString ( datasetID ) ) ;
dataset . setDmp ( dmp ) ;
datasetDao . update ( dataset ) ;
return ResponseEntity . status ( HttpStatus . OK ) . build ( ) ;
}
catch ( Exception ex ) {
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . build ( ) ;
}
// try {dmp = dMPDao.read(UUID.fromString(dmpID));}
// catch(Exception e) {
// RestResponse rr = new RestResponse("Could not find a DMP with id: "+dmpID+"! Detailed message: " + e.getMessage(), null);
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(rr.toString());
// }
// try {dataset = datasetDao.read(UUID.fromString(datasetID));}
// catch(Exception e) {
// RestResponse rr = new RestResponse("Could not find a Dataset with id: "+datasetID+"! Detailed message: " + e.getMessage(), null);
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(rr.toString());
// }
// try {
// dataset.setDmp(dmp);
// datasetDao.update(dataset);
// RestResponse rr = new RestResponse("Assigned DMP with id: "+dmpID+" on Dataset with id: "+datasetID+"!", null);
// return ResponseEntity.status(HttpStatus.OK).body(rr.toString());
// }
// catch(Exception e) {
// RestResponse rr = new RestResponse("Could not assign DMP with id: "+dmpID+" on Dataset with id: "+datasetID+"! Detailed message: " + e.getMessage(), null);
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(rr.toString());
// }
}
2017-10-04 11:48:21 +02:00
/ *
@RequestMapping ( method = RequestMethod . GET , value = { " /createEmptyDataset " } )
public @ResponseBody ResponseEntity < Object > createEmptyDataset ( ) {
Dataset dataset = new Dataset ( ) ;
dataset . setLabel ( " " ) ;
try {
dataset = datasetDao . create ( dataset ) ;
}
catch ( Exception e ) {
e . printStackTrace ( ) ;
try {
dataset = datasetDao . update ( dataset ) ;
}
catch ( Exception ex ) {
ex . printStackTrace ( ) ;
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " FAILED: reason: " + e . getMessage ( ) ) ;
}
}
return ResponseEntity . status ( HttpStatus . CREATED ) . body ( " Created Dataset with id: " + dataset . getId ( ) ) ;
}
* /
//FETCH BY DATASET PROFILE
@RequestMapping ( method = RequestMethod . GET , value = { " /datasetprofile/{id} " } )
public @ResponseBody ResponseEntity < Object > getDatasetProfile ( @PathVariable ( " id " ) String id ) {
try {
DatasetProfile profile = datasetProfileDao . read ( UUID . fromString ( id ) ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ObjectMapper ( ) . writeValueAsString ( profile ) ) ;
}
catch ( Exception ex ) {
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( " Erroneous input: " + ex . getMessage ( ) ) ;
}
}
@RequestMapping ( method = RequestMethod . POST , value = { " /setDatasetProfile " } , consumes = " application/json " )
public @ResponseBody ResponseEntity < Object > setDatasetProfile ( @RequestBody DatasetProfile datasetProfile ) {
String reason = " " ;
DatasetProfile storedDatasetProfile = null ;
//try first to create
try {
storedDatasetProfile = datasetProfileDao . create ( datasetProfile ) ;
return ResponseEntity . status ( HttpStatus . CREATED ) . body ( " Created DatasetProfile with id: " + storedDatasetProfile . getId ( ) ) ;
}
catch ( Exception e ) {
reason + = e . getMessage ( ) ;
//try updating
try {
storedDatasetProfile = datasetProfileDao . update ( datasetProfile ) ;
return ResponseEntity . status ( HttpStatus . CREATED ) . body ( " Updated DatasetProfile with id: " + storedDatasetProfile . getId ( ) ) ;
}
catch ( Exception ex ) {
reason + = ( System . lineSeparator ( ) + e . getMessage ( ) ) ;
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( " Could not create or update DatasetProfile! Reason: " + reason ) ;
}
}
}
@RequestMapping ( method = RequestMethod . GET , value = { " /createEmptyDatasetProfile " } )
public @ResponseBody ResponseEntity < Object > createEmptyDatasetProfile ( ) {
DatasetProfile datasetProfile = new DatasetProfile ( ) ;
datasetProfile . setLabel ( " " ) ;
datasetProfile . setDefinition ( " " ) ;
try {
datasetProfile = datasetProfileDao . create ( datasetProfile ) ;
}
catch ( Exception e ) {
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( " FAILED: reason: " + e . getMessage ( ) ) ;
}
return ResponseEntity . status ( HttpStatus . CREATED ) . body ( " Created DatasetProfile with id: " + datasetProfile . getId ( ) ) ;
}
}