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

123 lines
6.8 KiB
Java
Raw Normal View History

2017-12-20 15:52:09 +01:00
package eu.eudat.controllers;
2018-01-22 08:41:31 +01:00
import eu.eudat.entities.Dataset;
2017-12-20 15:52:09 +01:00
import eu.eudat.managers.DatasetManager;
import eu.eudat.managers.DatasetWizardManager;
import eu.eudat.models.datasetwizard.DataManagentPlanListingModel;
2017-12-21 10:26:11 +01:00
import eu.eudat.models.datasetwizard.DatasetProfileWizardAutocompleteRequest;
2017-12-20 15:52:09 +01:00
import eu.eudat.models.datasetwizard.DatasetWizardAutocompleteRequest;
2017-12-22 14:42:47 +01:00
import eu.eudat.models.datasetwizard.DatasetWizardModel;
2017-12-21 10:26:11 +01:00
import eu.eudat.models.dmp.AssociatedProfile;
2017-12-20 15:52:09 +01:00
import eu.eudat.models.helpers.responses.ResponseItem;
import eu.eudat.models.security.Principal;
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;
2018-03-05 17:18:45 +01:00
import org.apache.poi.util.IOUtils;
2017-12-20 15:52:09 +01:00
import org.springframework.beans.factory.annotation.Autowired;
2018-03-05 17:18:45 +01:00
import org.springframework.core.env.Environment;
2018-03-01 10:14:10 +01:00
import org.springframework.http.HttpHeaders;
2017-12-20 15:52:09 +01:00
import org.springframework.http.HttpStatus;
2018-03-01 10:14:10 +01:00
import org.springframework.http.MediaType;
2018-01-22 08:41:31 +01:00
import org.springframework.http.ResponseEntity;
2017-12-20 15:52:09 +01:00
import org.springframework.web.bind.annotation.*;
2018-03-05 17:18:45 +01:00
import javax.activation.MimetypesFileTypeMap;
2017-12-22 14:42:47 +01:00
import javax.transaction.Transactional;
2018-03-01 10:14:10 +01:00
import java.io.File;
import java.io.FileInputStream;
2018-03-05 17:18:45 +01:00
import java.io.IOException;
2018-03-01 10:14:10 +01:00
import java.io.InputStream;
2017-12-20 15:52:09 +01:00
import java.util.List;
2018-02-01 10:08:06 +01:00
2017-12-20 15:52:09 +01:00
@RestController
@CrossOrigin
2018-02-09 16:54:41 +01:00
@RequestMapping(value = {"api/datasetwizard"})
2018-01-19 10:31:05 +01:00
public class DatasetWizardController extends BaseController {
2017-12-22 14:42:47 +01:00
2018-03-05 17:18:45 +01:00
private Environment environment;
2018-01-04 10:32:39 +01:00
@Autowired
2018-03-05 17:18:45 +01:00
public DatasetWizardController(ApiContext apiContext, Environment environment) {
2018-01-04 10:32:39 +01:00
super(apiContext);
2018-03-05 17:18:45 +01:00
this.environment = environment;
2018-01-04 10:32:39 +01:00
}
2017-12-20 15:52:09 +01:00
2018-01-19 10:31:05 +01:00
@RequestMapping(method = RequestMethod.POST, value = {"/userDmps"}, produces = "application/json")
2018-02-08 16:54:31 +01:00
public @ResponseBody
ResponseEntity<ResponseItem<List<DataManagentPlanListingModel>>> getUserDmps(@RequestBody DatasetWizardAutocompleteRequest datasetWizardAutocompleteRequest, Principal principal) {
2017-12-20 15:52:09 +01:00
try {
2018-03-05 17:18:45 +01:00
List<DataManagentPlanListingModel> dataManagementPlans = DatasetWizardManager.getUserDmps(this.getApiContext().getOperationsContext().getDatabaseRepository().getDmpDao(), datasetWizardAutocompleteRequest, principal);
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<DataManagentPlanListingModel>>().status(ApiMessageCode.NO_MESSAGE).payload(dataManagementPlans));
2017-12-20 15:52:09 +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<DataManagentPlanListingModel>>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
2017-12-20 15:52:09 +01:00
}
}
2017-12-21 10:26:11 +01:00
2018-01-19 10:31:05 +01:00
@RequestMapping(method = RequestMethod.POST, value = {"/getAvailableProfiles"}, produces = "application/json")
2018-02-08 16:54:31 +01:00
public @ResponseBody
ResponseEntity<ResponseItem<List<AssociatedProfile>>> getAvailableProfiles(@RequestBody DatasetProfileWizardAutocompleteRequest datasetProfileWizardAutocompleteRequest, Principal principal) {
2017-12-21 10:26:11 +01:00
try {
2018-03-05 17:18:45 +01:00
List<AssociatedProfile> dataManagementPlans = DatasetWizardManager.getAvailableProfiles(this.getApiContext().getOperationsContext().getDatabaseRepository().getDmpDao(), datasetProfileWizardAutocompleteRequest);
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<AssociatedProfile>>().status(ApiMessageCode.NO_MESSAGE).payload(dataManagementPlans));
2017-12-21 10:26:11 +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<AssociatedProfile>>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
2017-12-21 10:26:11 +01:00
}
}
2017-12-22 14:42:47 +01:00
2018-01-19 10:31:05 +01:00
@RequestMapping(method = RequestMethod.GET, value = {"/getSingle/{id}"}, produces = "application/json")
2018-02-08 16:54:31 +01:00
public @ResponseBody
ResponseEntity<ResponseItem<DatasetWizardModel>> getSingle(@PathVariable String id, Principal principal) {
2017-12-22 14:42:47 +01:00
try {
2018-03-05 17:18:45 +01:00
DatasetWizardModel dataset = new DatasetManager().getSingle(this.getApiContext().getOperationsContext().getDatabaseRepository().getDatasetDao(), id);
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DatasetWizardModel>().status(ApiMessageCode.NO_MESSAGE).payload(dataset));
2017-12-22 14:42:47 +01:00
} catch (Exception ex) {
ex.printStackTrace();
2018-01-23 16:21:38 +01:00
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<DatasetWizardModel>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
2017-12-22 14:42:47 +01:00
}
}
@Transactional
2018-01-19 10:31:05 +01:00
@RequestMapping(method = RequestMethod.POST, value = {"/createOrUpdate"}, consumes = "application/json", produces = "application/json")
2018-02-08 16:54:31 +01:00
public @ResponseBody
ResponseEntity<ResponseItem<Dataset>> createOrUpdate(@RequestBody DatasetWizardModel profile, Principal principal) {
2017-12-22 14:42:47 +01:00
try {
2018-02-08 16:54:31 +01:00
eu.eudat.entities.Dataset dataset = DatasetManager.createOrUpdate(this.getApiContext(), profile, principal);
2018-01-25 09:14:43 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<eu.eudat.entities.Dataset>().status(ApiMessageCode.SUCCESS_MESSAGE).message("Created").payload(null));
2017-12-22 14:42:47 +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.entities.Dataset>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
2017-12-22 14:42:47 +01:00
}
}
2018-03-01 10:14:10 +01:00
2018-03-05 17:18:45 +01:00
@RequestMapping(method = RequestMethod.GET, value = {"/getWordDocument/{id}"})
2018-03-01 10:14:10 +01:00
public @ResponseBody
2018-03-05 17:18:45 +01:00
ResponseEntity<byte[]> getWordDocument(@PathVariable String id) throws IllegalAccessException, IOException, InstantiationException {
2018-03-01 10:14:10 +01:00
try {
2018-03-05 17:18:45 +01:00
File file = new DatasetManager().getWordDocument(this.getApiContext().getOperationsContext().getDatabaseRepository().getDatasetDao(), id, this.getApiContext().getUtilitiesService().getVisibilityRuleService());
File pdffile = new DatasetManager().convertToPDF(file, environment, file.getName());
InputStream resource = new FileInputStream(pdffile);
System.out.println("Mime Type of " + file.getName() + " is " +
new MimetypesFileTypeMap().getContentType(file));
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentLength(pdffile.length());
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
responseHeaders.set("Content-Disposition", "attachment;filename=" + pdffile.getName());
responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition");
responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type");
2018-03-01 10:14:10 +01:00
2018-03-05 17:18:45 +01:00
byte[] content = IOUtils.toByteArray(resource);
return new ResponseEntity<>(content,
responseHeaders,
HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.badRequest().body(null);
2018-03-01 10:14:10 +01:00
}
}
2017-12-20 15:52:09 +01:00
}