package rest.entities; import java.io.IOException; import java.io.PrintStream; import java.util.List; import java.util.UUID; import javax.transaction.Transactional; 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.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; 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.DMPProfile; import entities.Dataset; import entities.DatasetProfile; import entities.DatasetProfileRuleset; import entities.DatasetProfileViewstyle; 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; private ObjectMapper objectMapper = new ObjectMapper(); // FETCH BY DATASET(S) @RequestMapping(method = RequestMethod.GET, value = { "/dataset" }) public @ResponseBody ResponseEntity listDatasets(){ try { List 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 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 getAllDatasets(){ try { List 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 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 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()); } } @RequestMapping(method = RequestMethod.GET, value = { "/assignDMPToDataset" }) public @ResponseBody ResponseEntity assignDMPToDataset(@RequestParam("datasetID") String datasetID, @RequestParam("dmpID") String dmpID) { Dataset dataset = null; try { dataset = datasetDao.read(UUID.fromString(datasetID)); if(dataset==null || dataset.getId()==null) throw new Exception("Could not find a Dataset by this id"); DMP dmp = new DMP(); dmp.setId(UUID.fromString(dmpID)); dataset.setDmp(dmp); datasetDao.update(dataset); return ResponseEntity.status(HttpStatus.OK).build(); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } } /* @RequestMapping(method = RequestMethod.GET, value = { "/createEmptyDataset" }) public @ResponseBody ResponseEntity 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 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()); } } //@Transactional @RequestMapping(method = RequestMethod.POST, value = { "/datasetprofile/set" }, consumes = "application/json", produces="application/json") public @ResponseBody ResponseEntity setDatasetProfile(@RequestBody DatasetProfile datasetProfile) { System.out.println("inside setDatasetProfile"); try { System.out.println(objectMapper.writeValueAsString(datasetProfile)); } catch (JsonProcessingException e) { e.printStackTrace(); } if(datasetProfile.getRuleset()!=null && datasetProfile.getRuleset().getId()!=null){ DatasetProfileRuleset dpr = datasetProfile.getRuleset(); datasetProfileRulesetDao.update(dpr); DatasetProfileRuleset n = new DatasetProfileRuleset(); n.setId(dpr.getId()); datasetProfile.setRuleset(n); } if(datasetProfile.getViewstyle()!=null && datasetProfile.getViewstyle().getId()!=null){ DatasetProfileViewstyle dpv = datasetProfile.getViewstyle(); datasetProfileViewstyleDao.update(dpv); DatasetProfileViewstyle n = new DatasetProfileViewstyle(); n.setId(dpv.getId()); datasetProfile.setViewstyle(n); } datasetProfile.setDataset(null); DatasetProfile createdDatasetProfile = datasetProfileDao.update(datasetProfile); try { return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdDatasetProfile)); } catch (JsonProcessingException e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not set dataset profile!\"}"); } } @RequestMapping(method = RequestMethod.POST, value = { "/createEmptyDatasetProfile" }) public @ResponseBody ResponseEntity createEmptyDatasetProfile(@RequestParam("datasetID") String datasetID) { DatasetProfileRuleset dpr = new DatasetProfileRuleset(); dpr.setLabel(""); dpr.setDefinition(""); dpr.setId(datasetProfileRulesetDao.create(dpr).getId()); DatasetProfileViewstyle dpv = new DatasetProfileViewstyle(); dpv.setLabel(""); dpv.setDefinition(""); dpv.setId(datasetProfileViewstyleDao.create(dpv).getId()); DatasetProfile datasetProfile = new DatasetProfile(); datasetProfile.setLabel(""); datasetProfile.setDefinition(""); datasetProfile.setRuleset(dpr); datasetProfile.setViewstyle(dpv); Dataset ds = new Dataset(); ds.setId(UUID.fromString(datasetID)); datasetProfile.setDataset(ds); try { datasetProfile = datasetProfileDao.create(datasetProfile); return ResponseEntity.status(HttpStatus.CREATED).body(new ObjectMapper().writeValueAsString(datasetProfile)); } catch(Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("FAILED: reason: "+e.getMessage()); } } }