2018-06-27 12:29:21 +02:00
|
|
|
package eu.eudat.logic.managers;
|
2017-12-21 10:26:11 +01:00
|
|
|
|
2018-05-14 08:44:35 +02:00
|
|
|
import com.jayway.jsonpath.DocumentContext;
|
|
|
|
import com.jayway.jsonpath.JsonPath;
|
2019-02-15 12:12:20 +01:00
|
|
|
import eu.eudat.data.dao.criteria.DatasetProfileCriteria;
|
2018-03-21 11:57:56 +01:00
|
|
|
import eu.eudat.data.dao.entities.DatasetProfileDao;
|
|
|
|
import eu.eudat.data.entities.DatasetProfile;
|
2018-03-21 13:11:02 +01:00
|
|
|
import eu.eudat.data.query.items.item.datasetprofile.DatasetProfileAutocompleteRequest;
|
|
|
|
import eu.eudat.data.query.items.table.datasetprofile.DatasetProfileTableRequestItem;
|
2019-03-26 15:30:33 +01:00
|
|
|
import eu.eudat.exceptions.datasetprofile.DatasetProfileNewVersionException;
|
2018-10-16 09:09:54 +02:00
|
|
|
import eu.eudat.logic.builders.model.models.DataTableDataBuilder;
|
|
|
|
import eu.eudat.logic.services.ApiContext;
|
2019-03-05 16:33:59 +01:00
|
|
|
import eu.eudat.logic.services.operations.DatabaseRepository;
|
2018-10-16 09:09:54 +02:00
|
|
|
import eu.eudat.logic.utilities.builders.XmlBuilder;
|
2019-02-25 17:46:16 +01:00
|
|
|
import eu.eudat.logic.utilities.documents.helpers.FileEnvelope;
|
|
|
|
import eu.eudat.logic.utilities.documents.xml.datasetProfileXml.ExportXmlBuilderDatasetProfile;
|
|
|
|
import eu.eudat.logic.utilities.documents.xml.datasetProfileXml.ImportXmlBuilderDatasetProfile;
|
2018-06-27 12:29:21 +02:00
|
|
|
import eu.eudat.models.data.components.commons.datafield.AutoCompleteData;
|
|
|
|
import eu.eudat.models.data.datasetprofile.DatasetProfileAutocompleteItem;
|
|
|
|
import eu.eudat.models.data.datasetprofile.DatasetProfileListingModel;
|
|
|
|
import eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.Field;
|
|
|
|
import eu.eudat.models.data.helpermodels.Tuple;
|
|
|
|
import eu.eudat.models.data.helpers.common.DataTableData;
|
2017-12-21 10:26:11 +01:00
|
|
|
import eu.eudat.queryable.QueryableList;
|
2019-03-05 16:33:59 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2018-05-14 08:44:35 +02:00
|
|
|
import org.springframework.http.*;
|
2019-02-01 17:56:55 +01:00
|
|
|
import org.springframework.stereotype.Component;
|
2018-05-14 08:44:35 +02:00
|
|
|
import org.springframework.web.client.RestTemplate;
|
2019-02-25 17:46:16 +01:00
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
2018-03-08 11:54:56 +01:00
|
|
|
import org.w3c.dom.Document;
|
|
|
|
import org.w3c.dom.Element;
|
2017-12-21 10:26:11 +01:00
|
|
|
|
2019-02-25 17:46:16 +01:00
|
|
|
import javax.activation.MimetypesFileTypeMap;
|
2018-03-08 11:54:56 +01:00
|
|
|
import javax.xml.xpath.*;
|
2019-02-25 17:46:16 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
2018-10-16 09:09:54 +02:00
|
|
|
import java.util.*;
|
2017-12-21 10:26:11 +01:00
|
|
|
|
2019-02-25 17:46:16 +01:00
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
|
2019-02-01 17:56:55 +01:00
|
|
|
@Component
|
2017-12-21 10:26:11 +01:00
|
|
|
public class DatasetProfileManager {
|
|
|
|
|
2019-03-05 16:33:59 +01:00
|
|
|
private ApiContext apiContext;
|
|
|
|
private DatabaseRepository databaseRepository;
|
|
|
|
@Autowired
|
|
|
|
public DatasetProfileManager(ApiContext apiContext) {
|
|
|
|
this.apiContext = apiContext;
|
|
|
|
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:30:33 +01:00
|
|
|
public eu.eudat.models.data.admin.composite.DatasetProfile getDatasetProfile(String id) {
|
|
|
|
eu.eudat.data.entities.DatasetProfile profile = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().find(UUID.fromString(id));
|
|
|
|
eu.eudat.models.data.admin.composite.DatasetProfile datasetprofile = AdminManager.generateDatasetProfileModel(profile);
|
|
|
|
datasetprofile.setLabel(profile.getLabel());
|
|
|
|
datasetprofile.setStatus(profile.getStatus());
|
|
|
|
|
|
|
|
return datasetprofile;
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:33:59 +01:00
|
|
|
public List<DatasetProfileAutocompleteItem> getWithCriteria(DatasetProfileAutocompleteRequest datasetProfileAutocompleteRequest) throws IllegalAccessException, InstantiationException {
|
|
|
|
QueryableList<DatasetProfile> items = databaseRepository.getDatasetProfileDao().getWithCriteria(datasetProfileAutocompleteRequest.getCriteria());
|
2018-02-21 11:07:31 +01:00
|
|
|
List<DatasetProfileAutocompleteItem> datasetProfiles = items.select(item -> new DatasetProfileAutocompleteItem().fromDataModel(item));
|
2017-12-21 10:26:11 +01:00
|
|
|
return datasetProfiles;
|
|
|
|
}
|
2018-01-03 11:44:54 +01:00
|
|
|
|
2019-03-05 16:33:59 +01:00
|
|
|
public DatasetProfile clone(String id) {
|
2019-02-25 17:46:16 +01:00
|
|
|
DatasetProfile profile = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().find(UUID.fromString(id));
|
2018-10-16 09:09:54 +02:00
|
|
|
apiContext.getOperationsContext().getDatabaseRepository().detachEntity(profile);
|
|
|
|
profile.setId(null);
|
2018-10-16 13:11:15 +02:00
|
|
|
return profile;
|
2018-10-16 09:09:54 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 16:33:59 +01:00
|
|
|
public DataTableData<DatasetProfileListingModel> getPaged(DatasetProfileTableRequestItem datasetProfileTableRequestItem) throws Exception {
|
2018-03-05 17:18:45 +01:00
|
|
|
QueryableList<DatasetProfile> items = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().getWithCriteria(datasetProfileTableRequestItem.getCriteria());
|
2018-02-16 08:45:18 +01:00
|
|
|
QueryableList<DatasetProfile> pagedItems = PaginationManager.applyPaging(items, datasetProfileTableRequestItem);
|
2018-02-21 11:07:31 +01:00
|
|
|
List<DatasetProfileListingModel> datasetProfiles = pagedItems.select(item -> new DatasetProfileListingModel().fromDataModel(item));
|
2018-03-05 17:18:45 +01:00
|
|
|
return apiContext.getOperationsContext().getBuilderFactory().getBuilder(DataTableDataBuilder.class).data(datasetProfiles).totalCount(items.count()).build();
|
2018-01-03 11:44:54 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 16:33:59 +01:00
|
|
|
public List<DatasetProfileListingModel> getAll() throws IllegalAccessException, InstantiationException {
|
2019-02-15 12:12:20 +01:00
|
|
|
DatasetProfileCriteria criteria = new DatasetProfileCriteria();
|
2019-03-05 16:33:59 +01:00
|
|
|
QueryableList<DatasetProfile> items = databaseRepository.getDatasetProfileDao().getWithCriteria(criteria);
|
2018-02-21 11:07:31 +01:00
|
|
|
List<DatasetProfileListingModel> datasetProfiles = items.select(item -> new DatasetProfileListingModel().fromDataModel(item));
|
2018-01-03 11:44:54 +01:00
|
|
|
return datasetProfiles;
|
|
|
|
}
|
2018-03-08 11:54:56 +01:00
|
|
|
|
2019-02-01 17:56:55 +01:00
|
|
|
public eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.Field queryForField(String xml, String fieldId) throws XPathExpressionException {
|
2018-06-27 12:29:21 +02:00
|
|
|
eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.Field field = new Field();
|
2018-03-08 11:54:56 +01:00
|
|
|
Document document = XmlBuilder.fromXml(xml);
|
|
|
|
XPathFactory xpathFactory = XPathFactory.newInstance();
|
|
|
|
XPath xpath = xpathFactory.newXPath();
|
|
|
|
XPathExpression expr =
|
|
|
|
xpath.compile("//field[@id='" + fieldId + "']");
|
|
|
|
Element name = (Element) expr.evaluate(document, XPathConstants.NODE);
|
|
|
|
field.fromXml(name);
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:56:55 +01:00
|
|
|
public List<Tuple<String, String>> getAutocomplete(AutoCompleteData data, String like) {
|
2018-05-14 08:44:35 +02:00
|
|
|
List<Tuple<String, String>> result = new LinkedList<>();
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
headers.setAccept(Collections.singletonList(MediaType.valueOf("application/vnd.api+json; charset=utf-8")));
|
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
|
|
|
|
|
|
|
|
ResponseEntity<Object> response = restTemplate.exchange(data.getUrl() + "?search=" + like, HttpMethod.GET, entity, Object.class);
|
|
|
|
DocumentContext jsonContext = JsonPath.parse(response.getBody());
|
|
|
|
|
|
|
|
List<Map<String, String>> jsonItems = jsonContext.read(data.getOptionsRoot() + "['" + data.getAutoCompleteOptions().getLabel() + "','" + data.getAutoCompleteOptions().getValue() + "']");
|
|
|
|
jsonItems.forEach(item -> result.add(new Tuple<>(item.get(data.getAutoCompleteOptions().getValue()), item.get(data.getAutoCompleteOptions().getLabel()))));
|
|
|
|
return result;
|
|
|
|
}
|
2019-02-25 17:46:16 +01:00
|
|
|
|
|
|
|
public ResponseEntity<byte[]> getDocument(eu.eudat.models.data.user.composite.DatasetProfile datasetProfile, String label) throws IllegalAccessException, IOException, InstantiationException {
|
|
|
|
FileEnvelope envelope = getXmlDocument(datasetProfile, label);
|
|
|
|
InputStream resource = new FileInputStream(envelope.getFile());
|
|
|
|
System.out.println("Mime Type of " + envelope.getFilename() + " is " +
|
|
|
|
new MimetypesFileTypeMap().getContentType(envelope.getFile()));
|
|
|
|
HttpHeaders responseHeaders = new HttpHeaders();
|
|
|
|
responseHeaders.setContentLength(envelope.getFile().length());
|
|
|
|
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
|
|
|
responseHeaders.set("Content-Disposition", "attachment;filename=" + envelope.getFilename() + ".xml");
|
|
|
|
responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition");
|
|
|
|
responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type");
|
|
|
|
|
|
|
|
byte[] content = org.apache.poi.util.IOUtils.toByteArray(resource);
|
|
|
|
resource.close();
|
|
|
|
Files.deleteIfExists(envelope.getFile().toPath());
|
|
|
|
|
|
|
|
return new ResponseEntity<>(content,
|
|
|
|
responseHeaders,
|
|
|
|
HttpStatus.OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
public FileEnvelope getXmlDocument(eu.eudat.models.data.user.composite.DatasetProfile datatasetProfile, String label) throws InstantiationException, IllegalAccessException, IOException {
|
|
|
|
ExportXmlBuilderDatasetProfile xmlBuilder = new ExportXmlBuilderDatasetProfile();
|
|
|
|
File file = xmlBuilder.build(datatasetProfile);
|
|
|
|
FileEnvelope fileEnvelope = new FileEnvelope();
|
|
|
|
fileEnvelope.setFile(file);
|
|
|
|
fileEnvelope.setFilename(label);
|
|
|
|
return fileEnvelope;
|
|
|
|
}
|
|
|
|
|
|
|
|
public eu.eudat.logic.utilities.documents.xml.datasetProfileXml.datasetProfileModel.DatasetProfile createDatasetProfileFromXml(MultipartFile multiPartFile) {
|
|
|
|
ImportXmlBuilderDatasetProfile xmlBuilder = new ImportXmlBuilderDatasetProfile();
|
|
|
|
try {
|
|
|
|
return xmlBuilder.build(convert(multiPartFile));
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static File convert(MultipartFile file) throws IOException {
|
|
|
|
File convFile = new File(file.getOriginalFilename());
|
|
|
|
convFile.createNewFile();
|
|
|
|
FileOutputStream fos = new FileOutputStream(convFile);
|
|
|
|
fos.write(file.getBytes());
|
|
|
|
fos.close();
|
|
|
|
return convFile;
|
|
|
|
}
|
2019-03-26 15:30:33 +01:00
|
|
|
|
|
|
|
public eu.eudat.data.entities.DatasetProfile createNewVersionDatasetProfile(String id, eu.eudat.models.data.admin.composite.DatasetProfile profile) throws Exception {
|
|
|
|
// Getting the DatasetProfile which we will create its new version.
|
|
|
|
eu.eudat.data.entities.DatasetProfile oldDatasetProfile = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().find(UUID.fromString(id));
|
|
|
|
|
|
|
|
// Getting the DatasetProfile with the latest Version.
|
|
|
|
DatasetProfileCriteria criteria = new DatasetProfileCriteria();
|
|
|
|
LinkedList<UUID> list = new LinkedList<>();
|
|
|
|
list.push(oldDatasetProfile.getGroupId());
|
|
|
|
criteria.setGroupIds(list);
|
|
|
|
criteria.setAllVersions(false);
|
|
|
|
QueryableList<DatasetProfile> datasetProfileQueryableList = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().getWithCriteria(criteria);
|
|
|
|
eu.eudat.data.entities.DatasetProfile latestVersionDatasetProfile = datasetProfileQueryableList.getSingle();
|
|
|
|
|
|
|
|
if (latestVersionDatasetProfile.getVersion().equals(oldDatasetProfile.getVersion())){
|
|
|
|
eu.eudat.models.data.admin.composite.DatasetProfile sortedProfile = profile.toShort();
|
|
|
|
eu.eudat.data.entities.DatasetProfile modelDefinition = AdminManager.generateViewStyleDefinition(sortedProfile, apiContext);
|
|
|
|
modelDefinition.setLabel(oldDatasetProfile.getLabel());
|
|
|
|
modelDefinition.setVersion((short) (oldDatasetProfile.getVersion() + 1));
|
|
|
|
modelDefinition.setGroupId(oldDatasetProfile.getGroupId());
|
|
|
|
apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().createOrUpdate(modelDefinition);
|
|
|
|
return modelDefinition;
|
|
|
|
} else {
|
|
|
|
throw new DatasetProfileNewVersionException("Version to update not the latest.");
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 10:26:11 +01:00
|
|
|
}
|