package rest.entities; import java.util.Date; import java.util.List; import java.util.UUID; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.context.SecurityContextHolder; 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 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 dao.entities.UserInfoDao; import entities.DMP; import entities.Dataset; import entities.DatasetProfile; import entities.UserInfo; import helpers.SafeCleanAttribs; import helpers.SerializerProvider; import models.criteria.DataRepositoryCriteria; import models.criteria.RegistryCriteria; import models.criteria.ServiceCriteria; 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; @Autowired private UserInfoDao userInfoDao; // FETCH BY DATASET(S) @RequestMapping(method = RequestMethod.GET, value = { "/datasets" }) public @ResponseBody ResponseEntity listDatasets(){ try { List allIDs = datasetDao.listAllIDs(); return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); } } @RequestMapping(method = RequestMethod.GET, value = { "/datasets/{id}" }) public @ResponseBody ResponseEntity getDataset(@PathVariable("id") String id) { try { Dataset ds = datasetDao.read(UUID.fromString(id)); ds.setDmp(ds.getDmp()); return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(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 = { "/dataset/getAll" }) public @ResponseBody ResponseEntity getAllDatasets(){ try { List allDatasets = datasetDao.getAll(); return new ResponseEntity(SerializerProvider.toJson(allDatasets), HttpStatus.OK); } catch(Exception ex) { ex.printStackTrace(); return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); } } @RequestMapping(method = RequestMethod.POST, value = { "/dataset/create" }, consumes = "application/json", produces="application/json") public @ResponseBody ResponseEntity createDataset(@RequestBody models.dataset.Dataset modeldataset) { 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"); Dataset dataset = modeldataset.toDataModel(); if(dataset.getDataRepositories()!=null&&!dataset.getDataRepositories().isEmpty()){ for(entities.DataRepository dataRepo : dataset.getDataRepositories()){ DataRepositoryCriteria criteria = new DataRepositoryCriteria(); criteria.setLike(dataRepo.getReference()); List entries = this.dataRepositoryDao.listBy(criteria); if(entries!=null&&!entries.isEmpty())dataRepo.setId(entries.get(0).getId()); else dataRepo = this.dataRepositoryDao.create(dataRepo); } } if(dataset.getServices()!=null&&!dataset.getServices().isEmpty()){ for(entities.Service service : dataset.getServices()){ ServiceCriteria criteria = new ServiceCriteria(); criteria.setLike(service.getReference()); List entries = this.serviceDao.listBy(criteria); if(entries!=null&&!entries.isEmpty())service.setId(entries.get(0).getId()); else service = this.serviceDao.create(service); } } if(dataset.getRegistries()!=null&&!dataset.getRegistries().isEmpty()){ for(entities.Registry registry : dataset.getRegistries()){ RegistryCriteria criteria = new RegistryCriteria(); criteria.setLike(registry.getReference()); List entries = this.registryDao.listBy(criteria); if(entries!=null&&!entries.isEmpty())registry.setId( entries.get(0).getId()); else registry = this.registryDao.create(registry); } } dataset.setId(null); dataset.setCreated(new Date()); dataset.setModified(new Date()); dataset.setStatus(new Short("0")); dataset.setCreator(userInfo); if("".equals(dataset.getReference())) dataset.setReference(null); if("".equals(dataset.getProperties())) dataset.setProperties(null); try { dataset = datasetDao.create(dataset); return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(dataset)); } catch(Exception e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not create or update Dataset! Reason: " + e.getMessage()); } } @RequestMapping(method = RequestMethod.POST, value = { "/dataset/update" }, consumes = "application/json", produces="application/json") public @ResponseBody ResponseEntity updateDataset(@RequestBody String datasetJson) { Dataset dataset; try { dataset = SerializerProvider.fromJson(datasetJson, Dataset.class); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not create or update Dataset! Reason: " + e.getMessage()); } SafeCleanAttribs.clean(dataset); if("".equals(dataset.getReference())) dataset.setReference(null); if("".equals(dataset.getProperties())) dataset.setProperties(null); try { dataset = datasetDao.update(dataset); return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(dataset)); } catch(Exception ex) { ex.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not create or update Dataset! Reason: " + ex.getMessage()); } } @RequestMapping(method = RequestMethod.POST, value = { "/dataset/delete" }, 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.POST, value = { "/dataset/softdelete" }, consumes = "application/json", produces="text/plain") public @ResponseBody ResponseEntity softDelete(@RequestBody Dataset dataset) { Dataset d = datasetDao.read(dataset.getId()); d.setStatus(new Short("-1")); try { int code = updateDataset(SerializerProvider.toJson(d)).getStatusCodeValue(); if(code>199 && code<300) return ResponseEntity.status(HttpStatus.CREATED).body("DELETED!"); else return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not soft delete dataset!\""); } catch (Exception e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not soft delete dataset!\""); } } @RequestMapping(method = RequestMethod.GET, value = { "/dataset/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 = { "/dataset/assignProfileToDataset" }) public @ResponseBody ResponseEntity assignProfileToDataset(@RequestParam("datasetID") String datasetID, @RequestParam("profileID") String profileID) { 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"); DatasetProfile profile = new DatasetProfile(); profile.setId(UUID.fromString(profileID)); dataset.setProfile(profile); datasetDao.update(dataset); return ResponseEntity.status(HttpStatus.OK).build(); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } } }