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.
argos/dmp-backend/src/main/java/eu/eudat/controllers/DatasetProfileController.java

81 lines
4.3 KiB
Java

package eu.eudat.controllers;
import eu.eudat.managers.UserManager;
import eu.eudat.models.components.commons.datafield.AutoCompleteData;
import eu.eudat.models.helpers.common.AutoCompleteLookupItem;
import eu.eudat.models.helpers.responses.ResponseItem;
import eu.eudat.models.properties.PropertiesModel;
import eu.eudat.models.user.composite.PagedDatasetProfile;
import eu.eudat.services.ApiContext;
import eu.eudat.types.ApiMessageCode;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@RestController
@CrossOrigin
@RequestMapping(value = {"/api"})
public class DatasetProfileController extends BaseController {
@Autowired
public DatasetProfileController(ApiContext apiContext) {
super(apiContext);
}
@RequestMapping(method = RequestMethod.GET, value = {"/datasetwizard/get/{id}"}, produces = "application/json")
public ResponseEntity<ResponseItem<PagedDatasetProfile>> getSingle(@PathVariable String id) {
try {
eu.eudat.entities.DatasetProfile profile = this.getApiContext().getOperationsContext().getDatabaseRepository().getDatasetProfileDao().find(UUID.fromString(id));
eu.eudat.models.user.composite.DatasetProfile datasetprofile = UserManager.generateDatasetProfileModel(profile);
PagedDatasetProfile pagedDatasetProfile = new PagedDatasetProfile();
pagedDatasetProfile.buildPagedDatasetProfile(datasetprofile);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<PagedDatasetProfile>().status(ApiMessageCode.NO_MESSAGE).payload(pagedDatasetProfile));
} catch (Exception ex) {
ex.printStackTrace();
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<PagedDatasetProfile>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
}
}
@Transactional
@RequestMapping(method = RequestMethod.POST, value = {"/datasetprofile/save/{id}"}, consumes = "application/json", produces = "application/json")
public ResponseEntity<Object> updateDataset(@PathVariable String id, @RequestBody PropertiesModel properties) {
try {
eu.eudat.entities.Dataset dataset = this.getApiContext().getOperationsContext().getDatabaseRepository().getDatasetDao().find(UUID.fromString(id));
Map<String, Object> values = new HashMap();
properties.toMap(values);
JSONObject jobject = new JSONObject(values);
dataset.setProperties(jobject.toString());
dataset.setStatus((short) properties.getStatus());
this.getApiContext().getOperationsContext().getDatabaseRepository().getDatasetDao().createOrUpdate(dataset); //TODO
return ResponseEntity.status(HttpStatus.OK).body(properties);
} catch (Exception ex) {
ex.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: " + ex.getMessage());
}
}
@RequestMapping(method = RequestMethod.POST, value = {"/search/autocomplete"}, consumes = "application/json", produces = "application/json")
public ResponseEntity<Object> getDataForAutocomplete(@RequestBody AutoCompleteLookupItem lookupItem) {
try {
eu.eudat.entities.Dataset dataset = this.getApiContext().getOperationsContext().getDatabaseRepository().getDatasetDao().find(UUID.fromString(lookupItem.getProfileID()));
eu.eudat.entities.xmlmodels.datasetprofiledefinition.Field modelfield = new eu.eudat.entities.xmlmodels.datasetprofiledefinition.Field();
AutoCompleteData data = new AutoCompleteData().fromData(modelfield.getData());
URL url = new URL(data.getUrl() + lookupItem.getSearchTerm());
HttpURLConnection con = (HttpURLConnection) url.openConnection();
return ResponseEntity.status(HttpStatus.OK).body(null);
} catch (Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: " + ex.getMessage());
}
}
}