You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
6.8 KiB
Java
123 lines
6.8 KiB
Java
package eu.eudat.controllers;
|
|
|
|
import eu.eudat.entities.Dataset;
|
|
import eu.eudat.managers.DatasetManager;
|
|
import eu.eudat.managers.DatasetWizardManager;
|
|
import eu.eudat.models.datasetwizard.DataManagentPlanListingModel;
|
|
import eu.eudat.models.datasetwizard.DatasetProfileWizardAutocompleteRequest;
|
|
import eu.eudat.models.datasetwizard.DatasetWizardAutocompleteRequest;
|
|
import eu.eudat.models.datasetwizard.DatasetWizardModel;
|
|
import eu.eudat.models.dmp.AssociatedProfile;
|
|
import eu.eudat.models.helpers.responses.ResponseItem;
|
|
import eu.eudat.models.security.Principal;
|
|
import eu.eudat.services.ApiContext;
|
|
import eu.eudat.types.ApiMessageCode;
|
|
import org.apache.poi.util.IOUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.core.env.Environment;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.activation.MimetypesFileTypeMap;
|
|
import javax.transaction.Transactional;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.List;
|
|
|
|
|
|
@RestController
|
|
@CrossOrigin
|
|
@RequestMapping(value = {"api/datasetwizard"})
|
|
public class DatasetWizardController extends BaseController {
|
|
|
|
private Environment environment;
|
|
|
|
@Autowired
|
|
public DatasetWizardController(ApiContext apiContext, Environment environment) {
|
|
super(apiContext);
|
|
this.environment = environment;
|
|
}
|
|
|
|
@RequestMapping(method = RequestMethod.POST, value = {"/userDmps"}, produces = "application/json")
|
|
public @ResponseBody
|
|
ResponseEntity<ResponseItem<List<DataManagentPlanListingModel>>> getUserDmps(@RequestBody DatasetWizardAutocompleteRequest datasetWizardAutocompleteRequest, Principal principal) {
|
|
try {
|
|
List<DataManagentPlanListingModel> dataManagementPlans = DatasetWizardManager.getUserDmps(this.getApiContext().getOperationsContext().getDatabaseRepository().getDmpDao(), datasetWizardAutocompleteRequest, principal);
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<DataManagentPlanListingModel>>().status(ApiMessageCode.NO_MESSAGE).payload(dataManagementPlans));
|
|
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<List<DataManagentPlanListingModel>>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
|
|
}
|
|
}
|
|
|
|
@RequestMapping(method = RequestMethod.POST, value = {"/getAvailableProfiles"}, produces = "application/json")
|
|
public @ResponseBody
|
|
ResponseEntity<ResponseItem<List<AssociatedProfile>>> getAvailableProfiles(@RequestBody DatasetProfileWizardAutocompleteRequest datasetProfileWizardAutocompleteRequest, Principal principal) {
|
|
try {
|
|
List<AssociatedProfile> dataManagementPlans = DatasetWizardManager.getAvailableProfiles(this.getApiContext().getOperationsContext().getDatabaseRepository().getDmpDao(), datasetProfileWizardAutocompleteRequest);
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<AssociatedProfile>>().status(ApiMessageCode.NO_MESSAGE).payload(dataManagementPlans));
|
|
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<List<AssociatedProfile>>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
|
|
}
|
|
}
|
|
|
|
@RequestMapping(method = RequestMethod.GET, value = {"/getSingle/{id}"}, produces = "application/json")
|
|
public @ResponseBody
|
|
ResponseEntity<ResponseItem<DatasetWizardModel>> getSingle(@PathVariable String id, Principal principal) {
|
|
try {
|
|
DatasetWizardModel dataset = new DatasetManager().getSingle(this.getApiContext().getOperationsContext().getDatabaseRepository().getDatasetDao(), id);
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DatasetWizardModel>().status(ApiMessageCode.NO_MESSAGE).payload(dataset));
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<DatasetWizardModel>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
|
|
}
|
|
}
|
|
|
|
@Transactional
|
|
@RequestMapping(method = RequestMethod.POST, value = {"/createOrUpdate"}, consumes = "application/json", produces = "application/json")
|
|
public @ResponseBody
|
|
ResponseEntity<ResponseItem<Dataset>> createOrUpdate(@RequestBody DatasetWizardModel profile, Principal principal) {
|
|
try {
|
|
eu.eudat.entities.Dataset dataset = DatasetManager.createOrUpdate(this.getApiContext(), profile, principal);
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<eu.eudat.entities.Dataset>().status(ApiMessageCode.SUCCESS_MESSAGE).message("Created").payload(null));
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<eu.eudat.entities.Dataset>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
|
|
}
|
|
}
|
|
|
|
@RequestMapping(method = RequestMethod.GET, value = {"/getWordDocument/{id}"})
|
|
public @ResponseBody
|
|
ResponseEntity<byte[]> getWordDocument(@PathVariable String id) throws IllegalAccessException, IOException, InstantiationException {
|
|
try {
|
|
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");
|
|
|
|
byte[] content = IOUtils.toByteArray(resource);
|
|
return new ResponseEntity<>(content,
|
|
responseHeaders,
|
|
HttpStatus.OK);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return ResponseEntity.badRequest().body(null);
|
|
}
|
|
}
|
|
}
|