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.DMPProfile; import entities.DataRepository; import entities.Dataset; import entities.DatasetProfile; import entities.DatasetProfileRuleset; import entities.Organisation; import entities.Project; import entities.Registry; import entities.Researcher; import entities.Service; import entities.responses.IDLabelPair; import helpers.Transformers; import responses.RestResponse; @RestController @CrossOrigin public class Projects { @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; // MANAGE PROJECT(S) @RequestMapping(method = RequestMethod.GET, value = { "/projects" }) public @ResponseBody ResponseEntity listProjects(){ try { List allIDs = projectDao.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 = { "/projects/{id}" }) public @ResponseBody ResponseEntity getProject(@PathVariable("id") String id) { try { Project project = projectDao.read(UUID.fromString(id)); return ResponseEntity.status(HttpStatus.OK).body(new ObjectMapper().writeValueAsString(project)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage()); } } @RequestMapping(method = RequestMethod.GET, value = { "/project/listAllLabelIDs" }) public @ResponseBody ResponseEntity listLabelIds(){ try { List allIDs = projectDao.listAllIDsLabels(); 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.POST, value = { "/project/set" }, consumes = "application/json") public @ResponseBody ResponseEntity setProject(@RequestBody Project project) { String reason = ""; Project storedProject = null; //try first to create try { storedProject = projectDao.create(project); return ResponseEntity.status(HttpStatus.CREATED).body("Created project with id: " + storedProject.getId()); } catch(Exception e) { reason += e.getMessage(); //try updating try { storedProject = projectDao.update(project); return ResponseEntity.status(HttpStatus.CREATED).body("Updated project with id: " + storedProject.getId()); } catch(Exception ex) { reason += (System.lineSeparator()+e.getMessage()); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Could not create or update project! Reason: " + reason); } } } }