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

140 lines
5.5 KiB
Java
Raw Normal View History

2017-12-15 00:01:26 +01:00
package eu.eudat.controllers;
2017-10-06 19:20:05 +02:00
import java.util.List;
import java.util.Map;
2017-10-06 19:20:05 +02:00
import java.util.UUID;
2017-10-12 14:02:41 +02:00
import javax.transaction.Transactional;
2017-10-06 19:20:05 +02:00
2017-12-15 00:01:26 +01:00
import eu.eudat.entities.Organisation;
2017-12-19 15:09:49 +01:00
import eu.eudat.models.external.OrganisationsExternalSourcesModel;
import eu.eudat.models.external.ProjectsExternalSourcesModel;
import eu.eudat.models.helpers.responses.ResponseItem;
2018-01-04 10:32:39 +01:00
import eu.eudat.services.ApiContext;
2017-10-06 19:20:05 +02:00
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;
2017-12-15 00:01:26 +01:00
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.proxy.config.exceptions.HugeResultSet;
import eu.eudat.proxy.config.exceptions.NoURLFound;
import eu.eudat.proxy.fetching.RemoteFetcher;
2017-10-06 19:20:05 +02:00
@RestController
@CrossOrigin
2018-01-04 10:32:39 +01:00
public class Organisations extends BaseController{
@Autowired
public Organisations(ApiContext apiContext) {
super(apiContext);
}
2017-11-22 11:14:10 +01:00
@RequestMapping(method = RequestMethod.GET, value = { "/external/organisations" }, produces="application/json")
2017-12-19 15:09:49 +01:00
public @ResponseBody ResponseItem<OrganisationsExternalSourcesModel> listExternalOrganisations(@RequestParam(value="query", required=false) String query ){
try {
2018-01-04 10:32:39 +01:00
List<Map<String,String>> remoteRepos = this.getApiContext().getRemoteFetcher().getOrganisations(query);
2017-12-19 15:09:49 +01:00
OrganisationsExternalSourcesModel projectsExternalSourcesModel = new OrganisationsExternalSourcesModel().fromExternalItem(remoteRepos);
return new ResponseItem<OrganisationsExternalSourcesModel>().payload(projectsExternalSourcesModel).status(HttpStatus.OK);
}
catch(NoURLFound ex) {
2017-12-19 15:09:49 +01:00
return new ResponseItem<OrganisationsExternalSourcesModel>().status(HttpStatus.BAD_REQUEST).message("External Url Not Found");
}
catch(HugeResultSet ex) {
2017-12-19 15:09:49 +01:00
return new ResponseItem<OrganisationsExternalSourcesModel>().status(HttpStatus.BAD_REQUEST).message("Huge Result Set");
}catch (Exception ex){
return new ResponseItem<OrganisationsExternalSourcesModel>().status(HttpStatus.BAD_REQUEST).message(ex.getMessage());
}
}
2017-10-12 14:02:41 +02:00
2017-10-06 19:20:05 +02:00
// MANAGE ORGANISATIONS(S)
2017-10-12 14:02:41 +02:00
@RequestMapping(method = RequestMethod.GET, value = { "/organisations" })
2017-12-15 00:01:26 +01:00
public @ResponseBody ResponseEntity<List<UUID>> listOrganisations(){
2017-10-06 19:20:05 +02:00
try {
2018-01-04 10:32:39 +01:00
List<UUID> allIDs = this.getApiContext().getDatabaseRepository().getOrganisationDao().listAllIDs();
2017-12-15 00:01:26 +01:00
return ResponseEntity.status(HttpStatus.OK).body(allIDs);
2017-10-06 19:20:05 +02:00
}
catch(Exception ex) {
2017-12-15 00:01:26 +01:00
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
2017-10-06 19:20:05 +02:00
}
}
2017-10-12 14:02:41 +02:00
@RequestMapping(method = RequestMethod.GET, value = { "/organisations/{id}" })
2017-12-15 00:01:26 +01:00
public @ResponseBody ResponseEntity<Organisation> getOrganisations(@PathVariable("id") String id) {
2017-10-06 19:20:05 +02:00
try {
2018-01-04 10:32:39 +01:00
Organisation organisation = this.getApiContext().getDatabaseRepository().getOrganisationDao().read(UUID.fromString(id));
2017-12-15 00:01:26 +01:00
return ResponseEntity.status(HttpStatus.OK).body(organisation);
2017-10-06 19:20:05 +02:00
}
catch(Exception ex) {
2017-12-15 00:01:26 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
2017-10-06 19:20:05 +02:00
}
}
2017-10-12 14:02:41 +02:00
@RequestMapping(method = RequestMethod.GET, value = { "/organisation/getAll" }, produces="application/json")
public @ResponseBody ResponseEntity<Object> getAllOrganisations(){
2017-10-06 19:20:05 +02:00
try {
2018-01-04 10:32:39 +01:00
List<Organisation> allOrganisations = this.getApiContext().getDatabaseRepository().getOrganisationDao().getAll();
2017-12-15 00:01:26 +01:00
return ResponseEntity.status(HttpStatus.OK).body(allOrganisations);
2017-10-06 19:20:05 +02:00
}
2017-10-12 14:02:41 +02:00
catch(Exception ex) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Transactional
@RequestMapping(method = RequestMethod.POST, value = { "/organisation/create" }, consumes = "application/json", produces="application/json")
2017-12-15 00:01:26 +01:00
public @ResponseBody ResponseEntity<Organisation> setOrganisation(@RequestBody Organisation organisation) {
2017-10-12 14:02:41 +02:00
try {
2018-01-04 10:32:39 +01:00
Organisation createdOrganisation = this.getApiContext().getDatabaseRepository().getOrganisationDao().update(organisation);
2017-12-15 00:01:26 +01:00
return ResponseEntity.status(HttpStatus.CREATED).body(createdOrganisation);
} catch (Exception e) {
2017-12-15 00:01:26 +01:00
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
2017-10-12 14:02:41 +02:00
}
}
@RequestMapping(method = RequestMethod.POST, value = { "/organisation/delete" }, consumes = "application/json", produces="text/plain")
2017-12-15 00:01:26 +01:00
public @ResponseBody ResponseEntity<Organisation> delete(@RequestBody Organisation organisation) {
2017-10-12 14:02:41 +02:00
Organisation org = new Organisation();
org.setId(organisation.getId());
try {
2018-01-04 10:32:39 +01:00
this.getApiContext().getDatabaseRepository().getOrganisationDao().delete(org);
2017-12-15 00:01:26 +01:00
return ResponseEntity.status(HttpStatus.CREATED).body(null);
2017-10-12 14:02:41 +02:00
} catch (Exception e) {
2017-12-15 00:01:26 +01:00
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
2017-10-06 19:20:05 +02:00
}
2017-10-12 14:02:41 +02:00
2017-10-06 19:20:05 +02:00
}
}