argos/dmp-backend/web/src/main/java/eu/eudat/controllers/Projects.java

132 lines
8.6 KiB
Java
Raw Normal View History

2017-12-15 11:05:51 +01:00
package eu.eudat.controllers;
2018-03-21 11:57:56 +01:00
import eu.eudat.data.entities.Project;
2018-02-16 11:34:02 +01:00
import eu.eudat.managers.ProjectManager;
2017-12-19 15:09:49 +01:00
import eu.eudat.models.external.ProjectsExternalSourcesModel;
2018-02-16 11:34:02 +01:00
import eu.eudat.models.helpers.common.DataTableData;
import eu.eudat.models.helpers.responses.ResponseItem;
2018-03-21 13:11:02 +01:00
import eu.eudat.data.query.items.item.project.ProjectCriteriaRequest;
2018-01-22 08:41:31 +01:00
import eu.eudat.models.project.ProjectListingModel;
2018-03-21 13:11:02 +01:00
import eu.eudat.data.query.items.table.project.ProjectTableRequest;
2017-12-20 15:52:09 +01:00
import eu.eudat.models.security.Principal;
2018-02-16 11:34:02 +01:00
import eu.eudat.proxy.config.exceptions.HugeResultSet;
import eu.eudat.proxy.config.exceptions.NoURLFound;
2018-01-04 10:32:39 +01:00
import eu.eudat.services.ApiContext;
2018-01-23 16:21:38 +01:00
import eu.eudat.types.ApiMessageCode;
2017-12-15 11:05:51 +01:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
2018-02-16 11:34:02 +01:00
import org.springframework.web.bind.annotation.*;
2017-12-15 11:05:51 +01:00
2018-02-16 11:34:02 +01:00
import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
2018-01-31 16:39:16 +01:00
2017-12-15 11:05:51 +01:00
@RestController
@CrossOrigin
2018-02-09 16:54:41 +01:00
@RequestMapping(value = {"/api"})
2018-01-17 13:03:51 +01:00
public class Projects extends BaseController {
2018-01-03 11:44:54 +01:00
2018-01-17 13:03:51 +01:00
@Autowired
public Projects(ApiContext apiContext) {
super(apiContext);
}
2017-12-15 11:05:51 +01:00
2018-01-17 13:03:51 +01:00
@RequestMapping(method = RequestMethod.POST, value = {"/projects/getPaged"}, consumes = "application/json", produces = "application/json")
2018-01-31 16:39:16 +01:00
public @ResponseBody
ResponseEntity<ResponseItem<DataTableData<ProjectListingModel>>> getPaged(@Valid @RequestBody ProjectTableRequest projectTableRequest, Principal principal) {
2018-01-17 13:03:51 +01:00
try {
2018-03-19 13:40:04 +01:00
DataTableData<eu.eudat.models.project.ProjectListingModel> dataTable = new ProjectManager().getPaged(this.getApiContext().getOperationsContext().getDatabaseRepository().getProjectDao(), projectTableRequest, principal);
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DataTableData<eu.eudat.models.project.ProjectListingModel>>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE));
2017-12-19 09:38:47 +01:00
2018-01-17 13:03:51 +01:00
} catch (Exception ex) {
ex.printStackTrace();
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<DataTableData<eu.eudat.models.project.ProjectListingModel>>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
2018-01-17 13:03:51 +01:00
}
}
2017-12-19 09:38:47 +01:00
2018-01-17 13:03:51 +01:00
@RequestMapping(method = RequestMethod.GET, value = {"/projects/getSingle/{id}"}, produces = "application/json")
2018-01-31 16:39:16 +01:00
public @ResponseBody
ResponseEntity<ResponseItem<eu.eudat.models.project.Project>> getSingle(@PathVariable String id, Principal principal) {
2018-01-17 13:03:51 +01:00
try {
2018-03-05 17:18:45 +01:00
eu.eudat.models.project.Project project = new ProjectManager().getSingle(this.getApiContext().getOperationsContext().getDatabaseRepository().getProjectDao(), id);
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<eu.eudat.models.project.Project>().payload(project).status(ApiMessageCode.NO_MESSAGE));
2018-01-17 13:03:51 +01:00
} catch (Exception ex) {
ex.printStackTrace();
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<eu.eudat.models.project.Project>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
2018-01-17 13:03:51 +01:00
}
}
2017-12-19 09:38:47 +01:00
2018-01-17 13:03:51 +01:00
@Transactional
@RequestMapping(method = RequestMethod.POST, value = {"/projects/createOrUpdate"}, consumes = "application/json", produces = "application/json")
2018-01-31 16:39:16 +01:00
public @ResponseBody
ResponseEntity<ResponseItem<Project>> addProject(@Valid @RequestBody eu.eudat.models.project.Project project, Principal principal) {
2018-01-17 13:03:51 +01:00
try {
2018-03-19 13:40:04 +01:00
ProjectManager.createOrUpdate(this.getApiContext().getOperationsContext().getFileStorageService(), this.getApiContext().getOperationsContext().getDatabaseRepository().getProjectDao(), this.getApiContext().getOperationsContext().getDatabaseRepository().getContentDao(), this.getApiContext().getOperationsContext().getDatabaseRepository().getUserInfoDao(), project, principal);
2018-03-21 11:57:56 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<eu.eudat.data.entities.Project>().status(ApiMessageCode.SUCCESS_MESSAGE).message("Created"));
2018-01-17 13:03:51 +01:00
} catch (Exception ex) {
2018-03-19 13:40:04 +01:00
ex.printStackTrace();
2018-03-21 11:57:56 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<eu.eudat.data.entities.Project>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
2018-01-17 13:03:51 +01:00
}
}
2017-12-22 14:42:47 +01:00
2017-12-19 09:38:47 +01:00
2018-01-17 13:03:51 +01:00
@Transactional
@RequestMapping(method = RequestMethod.DELETE, value = {"/projects/inactivate/{id}"}, consumes = "application/json", produces = "application/json")
2018-01-31 16:39:16 +01:00
public @ResponseBody
ResponseEntity<ResponseItem<Project>> inactivate(@PathVariable String id, Principal principal) {
2018-01-17 13:03:51 +01:00
try {
2018-03-05 17:18:45 +01:00
Project project = new ProjectManager().inactivate(this.getApiContext().getOperationsContext().getDatabaseRepository().getProjectDao(), id);
2018-03-21 11:57:56 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<eu.eudat.data.entities.Project>().status(ApiMessageCode.SUCCESS_MESSAGE));
2018-01-17 13:03:51 +01:00
} catch (Exception ex) {
2018-03-21 11:57:56 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<eu.eudat.data.entities.Project>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
2018-01-17 13:03:51 +01:00
}
}
2017-12-19 09:38:47 +01:00
2017-12-15 11:05:51 +01:00
2018-01-17 13:03:51 +01:00
@RequestMapping(method = RequestMethod.POST, value = {"/projects/getWithExternal"}, consumes = "application/json", produces = "application/json")
2018-01-31 16:39:16 +01:00
public @ResponseBody
ResponseEntity<ResponseItem<List<eu.eudat.models.project.Project>>> getWithExternal(@RequestBody ProjectCriteriaRequest projectCriteria, Principal principal) {
2018-01-17 13:03:51 +01:00
try {
2018-03-05 17:18:45 +01:00
List<eu.eudat.models.project.Project> dataTable = new ProjectManager().getCriteriaWithExternal(this.getApiContext(), projectCriteria, this.getApiContext().getOperationsContext().getRemoteFetcher());
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<eu.eudat.models.project.Project>>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE));
2018-01-17 13:03:51 +01:00
} catch (Exception ex) {
ex.printStackTrace();
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<List<eu.eudat.models.project.Project>>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
2018-01-17 13:03:51 +01:00
}
}
@RequestMapping(method = RequestMethod.POST, value = {"/projects/get"}, consumes = "application/json", produces = "application/json")
2018-01-31 16:39:16 +01:00
public @ResponseBody
ResponseEntity<ResponseItem<List<eu.eudat.models.project.Project>>> get(@RequestBody ProjectCriteriaRequest projectCriteria, Principal principal) {
2018-01-17 13:03:51 +01:00
try {
2018-03-05 17:18:45 +01:00
List<eu.eudat.models.project.Project> dataTable = new ProjectManager().getCriteria(this.getApiContext().getOperationsContext().getDatabaseRepository().getProjectDao(), projectCriteria, this.getApiContext().getOperationsContext().getRemoteFetcher());
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<eu.eudat.models.project.Project>>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE));
2018-01-17 13:03:51 +01:00
} catch (Exception ex) {
ex.printStackTrace();
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<List<eu.eudat.models.project.Project>>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
2018-01-17 13:03:51 +01:00
}
}
@RequestMapping(method = RequestMethod.GET, value = {"/external/projects"}, produces = "application/json")
2018-01-31 16:39:16 +01:00
public @ResponseBody
ResponseEntity<ResponseItem<ProjectsExternalSourcesModel>> listExternalProjects(@RequestParam(value = "query", required = false) String query, Principal principal) {
2018-01-17 13:03:51 +01:00
try {
2018-03-05 17:18:45 +01:00
List<Map<String, String>> remoteRepos = this.getApiContext().getOperationsContext().getRemoteFetcher().getProjects(query);
2018-01-17 13:03:51 +01:00
ProjectsExternalSourcesModel projectsExternalSourcesModel = new ProjectsExternalSourcesModel().fromExternalItem(remoteRepos);
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<ProjectsExternalSourcesModel>().payload(projectsExternalSourcesModel).status(ApiMessageCode.SUCCESS_MESSAGE));
2018-01-17 13:03:51 +01:00
} catch (NoURLFound ex) {
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<ProjectsExternalSourcesModel>().status(ApiMessageCode.ERROR_MESSAGE).message("External Url Not Found"));
2018-01-17 13:03:51 +01:00
} catch (HugeResultSet ex) {
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<ProjectsExternalSourcesModel>().status(ApiMessageCode.ERROR_MESSAGE).message("Huge Result Set"));
2018-01-17 13:03:51 +01:00
} catch (Exception ex) {
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<ProjectsExternalSourcesModel>().status(ApiMessageCode.ERROR_MESSAGE).message(ex.getMessage()));
2018-01-17 13:03:51 +01:00
}
}
2017-12-15 11:05:51 +01:00
}