package eu.eudat.controllers; import java.util.List; import java.util.Map; import javax.transaction.Transactional; import javax.validation.Valid; import eu.eudat.models.external.ProjectsExternalSourcesModel; import eu.eudat.models.helpers.responses.*; import eu.eudat.models.project.ProjectCriteriaRequest; import eu.eudat.models.project.ProjectListingModel; import eu.eudat.models.security.Principal; import eu.eudat.services.ApiContext; import eu.eudat.types.ApiMessageCode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; 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.entities.Project; import eu.eudat.managers.ProjectManager; import eu.eudat.models.helpers.common.DataTableData; import eu.eudat.models.project.ProjectTableRequest; import eu.eudat.proxy.config.exceptions.HugeResultSet; import eu.eudat.proxy.config.exceptions.NoURLFound; @RestController @CrossOrigin public class Projects extends BaseController { @Autowired public Projects(ApiContext apiContext) { super(apiContext); } @RequestMapping(method = RequestMethod.POST, value = {"/projects/getPaged"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity>> getPaged(@Valid @RequestBody ProjectTableRequest projectTableRequest, Principal principal) { try { DataTableData dataTable = new ProjectManager().getPaged(this.getApiContext().getDatabaseRepository().getProjectDao(), projectTableRequest); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE)); } catch (Exception ex) { ex.printStackTrace(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage())); } } @RequestMapping(method = RequestMethod.GET, value = {"/projects/getSingle/{id}"}, produces = "application/json") public @ResponseBody ResponseEntity> getSingle(@PathVariable String id, Principal principal) { try { eu.eudat.models.project.Project project = new ProjectManager().getSingle(this.getApiContext().getDatabaseRepository().getProjectDao(), id); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().payload(project).status(ApiMessageCode.NO_MESSAGE)); } catch (Exception ex) { ex.printStackTrace(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage())); } } @Transactional @RequestMapping(method = RequestMethod.POST, value = {"/projects/createOrUpdate"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity> addProject(@Valid @RequestBody eu.eudat.models.project.Project project, Principal principal) { try { ProjectManager.createOrUpdate(this.getApiContext().getDatabaseRepository().getProjectDao(), this.getApiContext().getDatabaseRepository().getUserInfoDao(), project, principal); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.SUCCESS_MESSAGE).message("Created")); } catch (Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage())); } } @Transactional @RequestMapping(method = RequestMethod.DELETE, value = {"/projects/inactivate/{id}"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity> inactivate(@PathVariable String id, Principal principal) { try { Project project = new ProjectManager().inactivate(this.getApiContext().getDatabaseRepository().getProjectDao(), id); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.SUCCESS_MESSAGE)); } catch (Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage())); } } @RequestMapping(method = RequestMethod.POST, value = {"/projects/getWithExternal"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity>> getWithExternal(@RequestBody ProjectCriteriaRequest projectCriteria, Principal principal) { try { List dataTable = new ProjectManager().getCriteriaWithExternal(this.getApiContext().getDatabaseRepository().getProjectDao(), projectCriteria, this.getApiContext().getRemoteFetcher()); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE)); } catch (Exception ex) { ex.printStackTrace(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage())); } } @RequestMapping(method = RequestMethod.POST, value = {"/projects/get"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity>> get(@RequestBody ProjectCriteriaRequest projectCriteria, Principal principal) { try { List dataTable = new ProjectManager().getCriteria(this.getApiContext().getDatabaseRepository().getProjectDao(), projectCriteria, this.getApiContext().getRemoteFetcher()); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE)); } catch (Exception ex) { ex.printStackTrace(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage())); } } @RequestMapping(method = RequestMethod.GET, value = {"/external/projects"}, produces = "application/json") public @ResponseBody ResponseEntity> listExternalProjects(@RequestParam(value = "query", required = false) String query, Principal principal) { try { List> remoteRepos = this.getApiContext().getRemoteFetcher().getProjects(query); ProjectsExternalSourcesModel projectsExternalSourcesModel = new ProjectsExternalSourcesModel().fromExternalItem(remoteRepos); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().payload(projectsExternalSourcesModel).status(ApiMessageCode.SUCCESS_MESSAGE)); } catch (NoURLFound ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.ERROR_MESSAGE).message("External Url Not Found")); } catch (HugeResultSet ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.ERROR_MESSAGE).message("Huge Result Set")); } catch (Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.ERROR_MESSAGE).message(ex.getMessage())); } } }