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

140 lines
5.5 KiB
Java

package eu.eudat.controllers;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.transaction.Transactional;
import eu.eudat.entities.Organisation;
import eu.eudat.models.external.OrganisationsExternalSourcesModel;
import eu.eudat.models.external.ProjectsExternalSourcesModel;
import eu.eudat.models.helpers.responses.ResponseItem;
import eu.eudat.services.ApiContext;
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.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;
@RestController
@CrossOrigin
public class Organisations extends BaseController{
@Autowired
public Organisations(ApiContext apiContext) {
super(apiContext);
}
@RequestMapping(method = RequestMethod.GET, value = { "/external/organisations" }, produces="application/json")
public @ResponseBody ResponseItem<OrganisationsExternalSourcesModel> listExternalOrganisations(@RequestParam(value="query", required=false) String query ){
try {
List<Map<String,String>> remoteRepos = this.getApiContext().getRemoteFetcher().getOrganisations(query);
OrganisationsExternalSourcesModel projectsExternalSourcesModel = new OrganisationsExternalSourcesModel().fromExternalItem(remoteRepos);
return new ResponseItem<OrganisationsExternalSourcesModel>().payload(projectsExternalSourcesModel).status(HttpStatus.OK);
}
catch(NoURLFound ex) {
return new ResponseItem<OrganisationsExternalSourcesModel>().status(HttpStatus.BAD_REQUEST).message("External Url Not Found");
}
catch(HugeResultSet ex) {
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());
}
}
// MANAGE ORGANISATIONS(S)
@RequestMapping(method = RequestMethod.GET, value = { "/organisations" })
public @ResponseBody ResponseEntity<List<UUID>> listOrganisations(){
try {
List<UUID> allIDs = this.getApiContext().getDatabaseRepository().getOrganisationDao().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 = { "/organisations/{id}" })
public @ResponseBody ResponseEntity<Organisation> getOrganisations(@PathVariable("id") String id) {
try {
Organisation organisation = this.getApiContext().getDatabaseRepository().getOrganisationDao().read(UUID.fromString(id));
return ResponseEntity.status(HttpStatus.OK).body(organisation);
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/organisation/getAll" }, produces="application/json")
public @ResponseBody ResponseEntity<Object> getAllOrganisations(){
try {
List<Organisation> allOrganisations = this.getApiContext().getDatabaseRepository().getOrganisationDao().getAll();
return ResponseEntity.status(HttpStatus.OK).body(allOrganisations);
}
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")
public @ResponseBody ResponseEntity<Organisation> setOrganisation(@RequestBody Organisation organisation) {
try {
Organisation createdOrganisation = this.getApiContext().getDatabaseRepository().getOrganisationDao().update(organisation);
return ResponseEntity.status(HttpStatus.CREATED).body(createdOrganisation);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
@RequestMapping(method = RequestMethod.POST, value = { "/organisation/delete" }, consumes = "application/json", produces="text/plain")
public @ResponseBody ResponseEntity<Organisation> delete(@RequestBody Organisation organisation) {
Organisation org = new Organisation();
org.setId(organisation.getId());
try {
this.getApiContext().getDatabaseRepository().getOrganisationDao().delete(org);
return ResponseEntity.status(HttpStatus.CREATED).body(null);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
}