package eu.eudat.controllers; import java.util.Date; import java.util.List; import java.util.UUID; import eu.eudat.entities.DMP; import eu.eudat.entities.Dataset; import eu.eudat.entities.DatasetProfile; import eu.eudat.entities.UserInfo; import eu.eudat.managers.DatasetManager; import eu.eudat.managers.ProjectManager; import eu.eudat.models.dataset.DatasetTableRequest; import eu.eudat.models.helpers.DataTableData; import eu.eudat.models.project.Project; import eu.eudat.models.project.ProjectTableRequest; import eu.eudat.models.responses.ResponseItem; 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 eu.eudat.dao.entities.DMPDao; import eu.eudat.dao.entities.DMPProfileDao; import eu.eudat.dao.entities.DataRepositoryDao; import eu.eudat.dao.entities.DatasetDao; import eu.eudat.dao.entities.DatasetProfileDao; import eu.eudat.dao.entities.DatasetProfileRulesetDao; import eu.eudat.dao.entities.DatasetProfileViewstyleDao; import eu.eudat.dao.entities.OrganisationDao; import eu.eudat.dao.entities.ProjectDao; import eu.eudat.dao.entities.RegistryDao; import eu.eudat.dao.entities.ResearcherDao; import eu.eudat.dao.entities.ServiceDao; import eu.eudat.dao.entities.UserInfoDao; import eu.eudat.models.criteria.DataRepositoryCriteria; import eu.eudat.models.criteria.RegistryCriteria; import eu.eudat.models.criteria.ServiceCriteria; import eu.eudat.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; @RequestMapping(method = RequestMethod.POST, value = { "/datasets/getPaged" }, consumes = "application/json", produces="application/json") public @ResponseBody ResponseItem> getPaged(@RequestBody DatasetTableRequest datasetTableRequest) { try { DataTableData dataTable = new DatasetManager().getPaged(datasetDao, datasetTableRequest); return new ResponseItem>().status(HttpStatus.OK).payload(dataTable); } catch (Exception ex) { ex.printStackTrace(); return new ResponseItem>().status(HttpStatus.OK).message(ex.getMessage()); } } @RequestMapping(method = RequestMethod.GET, value = { "/datasets/getSingle/{id}" }, produces="application/json") public @ResponseBody ResponseItem getPaged(@PathVariable String id) { try { eu.eudat.models.dataset.Dataset dataset = new DatasetManager().getSingle(datasetDao, id); return new ResponseItem().status(HttpStatus.OK).payload(dataset); } catch (Exception ex) { ex.printStackTrace(); return new ResponseItem().status(HttpStatus.OK).message(ex.getMessage()); } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 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(allIDs); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); } } @RequestMapping(method = RequestMethod.GET, value = { "/datasets/{id}" }) public @ResponseBody ResponseEntity getDataset(@PathVariable("id") String id) { try { Dataset ds = datasetDao.read(UUID.fromString(id)); eu.eudat.models.dataset.Dataset dataset = new eu.eudat.models.dataset.Dataset(); dataset.fromDataModel(ds); return ResponseEntity.status(HttpStatus.OK).body(dataset); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); } } /** * 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 ResponseEntity.status(HttpStatus.OK).body(allDatasets); } catch(Exception ex) { ex.printStackTrace(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); } } @RequestMapping(method = RequestMethod.POST, value = { "/dataset/create" }, consumes = "application/json", produces="application/json") public @ResponseBody ResponseEntity createDataset(@RequestBody eu.eudat.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(null); } UserInfo userInfo = userInfoDao.read(UUID.fromString(userID)); if(userInfo==null) //this should normally never happer return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); Dataset dataset = modeldataset.toDataModel(); if(dataset.getDataRepositories()!=null&&!dataset.getDataRepositories().isEmpty()){ for(eu.eudat.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(eu.eudat.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(eu.eudat.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(dataset); } catch(Exception e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); } } @RequestMapping(method = RequestMethod.POST, value = { "/dataset/update" }, consumes = "application/json", produces="application/json") public @ResponseBody ResponseEntity updateDataset(@RequestBody eu.eudat.models.dataset.Dataset modeldataset) { Dataset dataset = modeldataset.toDataModel(); if(dataset.getDataRepositories()!=null&&!dataset.getDataRepositories().isEmpty()){ for(eu.eudat.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(eu.eudat.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(eu.eudat.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 olddataset = datasetDao.read(modeldataset.getId()); olddataset.getServices().clear(); olddataset.setServices(dataset.getServices()); olddataset.getDataRepositories().clear(); olddataset.setDataRepositories(dataset.getDataRepositories()); olddataset.getRegistries().clear(); olddataset.setRegistries(dataset.getRegistries()); olddataset.setLabel(modeldataset.getLabel()); olddataset.setDescription(modeldataset.getDescription()); //SafeCleanAttribs.clean(dataset); if("".equals(dataset.getReference())) dataset.setReference(null); if("".equals(dataset.getProperties())) dataset.setProperties(null); try { dataset = datasetDao.update(olddataset); return ResponseEntity.status(HttpStatus.CREATED).body(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) { try { Dataset d = datasetDao.read(dataset.getId()); d.setStatus(new Short("-1")); dataset = datasetDao.update(d); return ResponseEntity.status(HttpStatus.OK).body("{\"msg\":\"Deleted 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(); } } }