Add extra sanity checks
This commit is contained in:
parent
045dd51014
commit
65644c3a35
|
@ -287,6 +287,9 @@ public class Dmp implements ElasticEntity<Dmp> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dmp fromElasticEntity(Map<String, Object> fields) {
|
public Dmp fromElasticEntity(Map<String, Object> fields) {
|
||||||
|
if (fields == null || fields.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
this.id = UUID.fromString((String) fields.get(MapKey.ID.getName()));
|
this.id = UUID.fromString((String) fields.get(MapKey.ID.getName()));
|
||||||
if (fields.size() > 1) {
|
if (fields.size() > 1) {
|
||||||
this.label = (String) fields.get(MapKey.LABEL.getName());
|
this.label = (String) fields.get(MapKey.LABEL.getName());
|
||||||
|
|
|
@ -55,6 +55,7 @@ public class DatasetRepository extends ElasticRepository<Dataset, DatasetCriteri
|
||||||
if (this.getClient() != null) {
|
if (this.getClient() != null) {
|
||||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||||
Dmp dmp = this.dmpRepository.findDocument(entity.getDmp().toString());
|
Dmp dmp = this.dmpRepository.findDocument(entity.getDmp().toString());
|
||||||
|
if (dmp != null) {
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
if (dmp.getDatasets() != null && !dmp.getDatasets().isEmpty()) {
|
if (dmp.getDatasets() != null && !dmp.getDatasets().isEmpty()) {
|
||||||
for (int i = 0; i < dmp.getDatasets().size(); i++) {
|
for (int i = 0; i < dmp.getDatasets().size(); i++) {
|
||||||
|
@ -73,6 +74,7 @@ public class DatasetRepository extends ElasticRepository<Dataset, DatasetCriteri
|
||||||
}
|
}
|
||||||
IndexRequest request = new IndexRequest(this.environment.getProperty("elasticsearch.index")).id(dmp.getId().toString()).source(dmp.toElasticEntity(builder));//new IndexRequest("datasets", "doc", entity.getId()).source(entity.toElasticEntity(builder));
|
IndexRequest request = new IndexRequest(this.environment.getProperty("elasticsearch.index")).id(dmp.getId().toString()).source(dmp.toElasticEntity(builder));//new IndexRequest("datasets", "doc", entity.getId()).source(entity.toElasticEntity(builder));
|
||||||
this.getClient().index(request, RequestOptions.DEFAULT);
|
this.getClient().index(request, RequestOptions.DEFAULT);
|
||||||
|
}
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -1,159 +0,0 @@
|
||||||
package eu.eudat.logic.managers;
|
|
||||||
|
|
||||||
import eu.eudat.data.dao.entities.DatasetDao;
|
|
||||||
import eu.eudat.logic.proxy.config.configloaders.ConfigLoader;
|
|
||||||
import eu.eudat.logic.services.ApiContext;
|
|
||||||
import eu.eudat.logic.services.forms.VisibilityRuleService;
|
|
||||||
import eu.eudat.logic.utilities.documents.helpers.FileEnvelope;
|
|
||||||
import eu.eudat.logic.utilities.documents.word.WordBuilder;
|
|
||||||
import eu.eudat.logic.utilities.documents.xml.ExportXmlBuilder;
|
|
||||||
import eu.eudat.models.HintedModelFactory;
|
|
||||||
import eu.eudat.models.data.datasetwizard.DatasetWizardModel;
|
|
||||||
import eu.eudat.models.data.user.composite.PagedDatasetProfile;
|
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
import org.springframework.core.io.FileSystemResource;
|
|
||||||
import org.springframework.http.*;
|
|
||||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipInputStream;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by ikalyvas on 10/16/2018.
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class DocumentManager {
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(DocumentManager.class);
|
|
||||||
|
|
||||||
private ApiContext context;
|
|
||||||
private DatasetManager datasetManager;
|
|
||||||
private ConfigLoader configLoader;
|
|
||||||
private Environment environment;
|
|
||||||
|
|
||||||
public DocumentManager(ApiContext context, DatasetManager datasetManager, ConfigLoader configLoader, Environment environment) {
|
|
||||||
this.context = context;
|
|
||||||
this.datasetManager = datasetManager;
|
|
||||||
this.configLoader = configLoader;
|
|
||||||
this.environment = environment;
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getWordDocument(ConfigLoader configLoader, DatasetDao datatasetRepository, String id, VisibilityRuleService visibilityRuleService) throws InstantiationException, IllegalAccessException, IOException {
|
|
||||||
WordBuilder wordBuilder = new WordBuilder();
|
|
||||||
DatasetWizardModel dataset = new DatasetWizardModel();
|
|
||||||
XWPFDocument document = configLoader.getDocument();
|
|
||||||
eu.eudat.data.entities.Dataset datasetEntity = datatasetRepository.find(UUID.fromString(id), HintedModelFactory.getHint(DatasetWizardModel.class));
|
|
||||||
Map<String, Object> properties = new HashMap<>();
|
|
||||||
if (datasetEntity.getProperties() != null) {
|
|
||||||
JSONObject jobject = new JSONObject(datasetEntity.getProperties());
|
|
||||||
properties = jobject.toMap();
|
|
||||||
}
|
|
||||||
PagedDatasetProfile pagedDatasetProfile = datasetManager.getPagedProfile(dataset, datasetEntity);
|
|
||||||
visibilityRuleService.setProperties(properties);
|
|
||||||
visibilityRuleService.buildVisibilityContext(pagedDatasetProfile.getRules());
|
|
||||||
wordBuilder.build(document, pagedDatasetProfile, visibilityRuleService);
|
|
||||||
File exportFile = new File(dataset.getLabel() + ".docx");
|
|
||||||
FileOutputStream out = new FileOutputStream(exportFile);
|
|
||||||
document.write(out);
|
|
||||||
out.close();
|
|
||||||
return exportFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FileEnvelope getXmlDocument(eu.eudat.data.entities.Dataset datasetEntity, String id, VisibilityRuleService visibilityRuleService) throws InstantiationException, IllegalAccessException, IOException {
|
|
||||||
ExportXmlBuilder xmlBuilder = new ExportXmlBuilder();
|
|
||||||
DatasetWizardModel dataset = new DatasetWizardModel();
|
|
||||||
Map<String, Object> properties = new HashMap<>();
|
|
||||||
if (datasetEntity.getProperties() != null) {
|
|
||||||
JSONObject jobject = new JSONObject(datasetEntity.getProperties());
|
|
||||||
properties = jobject.toMap();
|
|
||||||
}
|
|
||||||
PagedDatasetProfile pagedDatasetProfile = datasetManager.getPagedProfile(dataset, datasetEntity);
|
|
||||||
visibilityRuleService.setProperties(properties);
|
|
||||||
visibilityRuleService.buildVisibilityContext(pagedDatasetProfile.getRules());
|
|
||||||
File file = xmlBuilder.build(pagedDatasetProfile, UUID.fromString(id), visibilityRuleService, environment);
|
|
||||||
FileEnvelope fileEnvelope = new FileEnvelope();
|
|
||||||
fileEnvelope.setFile(file);
|
|
||||||
fileEnvelope.setFilename(datasetEntity.getLabel());
|
|
||||||
return fileEnvelope;
|
|
||||||
}
|
|
||||||
|
|
||||||
public File convertToPDF(File file, Environment environment, String label) throws IOException, InterruptedException {
|
|
||||||
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
|
|
||||||
map.add("file", new FileSystemResource(file));
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
||||||
headers.add("Content-disposition", "attachment; filename=" + label + ".docx");
|
|
||||||
|
|
||||||
headers.add("Content-type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
|
||||||
|
|
||||||
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>(
|
|
||||||
map, headers);
|
|
||||||
|
|
||||||
Map queueResult = new RestTemplate().postForObject(
|
|
||||||
environment.getProperty("pdf.converter.url") +
|
|
||||||
"api/v1/", requestEntity, Map.class);
|
|
||||||
|
|
||||||
Map mediaResult = new RestTemplate().getForObject(environment.getProperty("pdf.converter.url") +
|
|
||||||
"/api/v1/" + queueResult.get("id"), Map.class);
|
|
||||||
logger.info("Status: " + mediaResult.get("status"));
|
|
||||||
while (!mediaResult.get("status").equals("finished")) {
|
|
||||||
Thread.sleep(500);
|
|
||||||
mediaResult = new RestTemplate().getForObject(environment.getProperty("pdf.converter.url") +
|
|
||||||
"api/v1/" + queueResult.get("id"), Map.class);
|
|
||||||
logger.info("Polling");
|
|
||||||
}
|
|
||||||
RestTemplate restTemplate = new RestTemplate();
|
|
||||||
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
|
|
||||||
HttpHeaders headers2 = new HttpHeaders();
|
|
||||||
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
|
|
||||||
HttpEntity<String> entity = new HttpEntity<String>(headers2);
|
|
||||||
|
|
||||||
ResponseEntity<byte[]> response = restTemplate.exchange(environment.getProperty("pdf.converter.url") +
|
|
||||||
mediaResult.get("result_url"), HttpMethod.GET, entity, byte[].class, "1");
|
|
||||||
|
|
||||||
UUID uuid = UUID.randomUUID();
|
|
||||||
File zip = new File(uuid + ".zip");
|
|
||||||
if (response.getStatusCode().equals(HttpStatus.OK)) {
|
|
||||||
FileOutputStream output = new FileOutputStream(zip);
|
|
||||||
IOUtils.write(response.getBody(), output);
|
|
||||||
}
|
|
||||||
return extractFromZip(zip, label + ".pdf");
|
|
||||||
}
|
|
||||||
|
|
||||||
private File extractFromZip(File file, String filename) throws IOException {
|
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
File newFile = new File(filename);
|
|
||||||
ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
|
|
||||||
ZipEntry zipEntry = zis.getNextEntry();
|
|
||||||
while (zipEntry != null) {
|
|
||||||
String zippedFileName = zipEntry.getName();
|
|
||||||
if (zippedFileName.equals("pdf")) {
|
|
||||||
|
|
||||||
FileOutputStream fos = new FileOutputStream(newFile);
|
|
||||||
int len;
|
|
||||||
while ((len = zis.read(buffer)) > 0) {
|
|
||||||
fos.write(buffer, 0, len);
|
|
||||||
}
|
|
||||||
fos.close();
|
|
||||||
zipEntry = zis.getNextEntry();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
zis.closeEntry();
|
|
||||||
zis.close();
|
|
||||||
return newFile;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -63,7 +63,7 @@ public class HtmlToWorldBuilder implements NodeVisitor {
|
||||||
this.dumpRun = false;
|
this.dumpRun = false;
|
||||||
}
|
}
|
||||||
parseProperties();
|
parseProperties();
|
||||||
|
properties.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseProperties() {
|
private void parseProperties() {
|
||||||
|
@ -105,5 +105,6 @@ public class HtmlToWorldBuilder implements NodeVisitor {
|
||||||
String name = node.nodeName();
|
String name = node.nodeName();
|
||||||
properties.put(name, false);
|
properties.put(name, false);
|
||||||
parseProperties();
|
parseProperties();
|
||||||
|
properties.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,7 +124,7 @@ public class ExportXmlBuilder {
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
Map<String, Object> jsonElement = mapper.readValue(field.getValue().toString(), Map.class);
|
Map<String, Object> jsonElement = mapper.readValue(field.getValue().toString(), Map.class);
|
||||||
valueField.setTextContent((jsonElement.get("label") != null ? jsonElement.get("label").toString() : jsonElement.get("name").toString()));
|
valueField.setTextContent((jsonElement.get("label") != null ? jsonElement.get("label").toString() : jsonElement.get("name") != null ? jsonElement.get("name").toString() : ""));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
valueField.setTextContent(field.getValue().toString());
|
valueField.setTextContent(field.getValue().toString());
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,9 +127,11 @@ public class DmpRDAMapper {
|
||||||
entity.setDescription(rda.getDescription());
|
entity.setDescription(rda.getDescription());
|
||||||
DatasetProfile defaultProfile = ((DatasetProfile)entity.getAssociatedDmps().toArray()[0]);
|
DatasetProfile defaultProfile = ((DatasetProfile)entity.getAssociatedDmps().toArray()[0]);
|
||||||
entity.setDataset(rda.getDataset().stream().map(rda1 -> datasetRDAMapper.toEntity(rda1, defaultProfile)).collect(Collectors.toSet()));
|
entity.setDataset(rda.getDataset().stream().map(rda1 -> datasetRDAMapper.toEntity(rda1, defaultProfile)).collect(Collectors.toSet()));
|
||||||
|
if (rda.getProject().size() > 0) {
|
||||||
Map<String, Object> result = ProjectRDAMapper.toEntity(rda.getProject().get(0), apiContext);
|
Map<String, Object> result = ProjectRDAMapper.toEntity(rda.getProject().get(0), apiContext);
|
||||||
entity.setProject((Project) result.get("project"));
|
entity.setProject((Project) result.get("project"));
|
||||||
result.entrySet().stream().filter(entry -> entry.getKey().startsWith("grant")).forEach(entry -> entity.setGrant((Grant) entry.getValue()));
|
result.entrySet().stream().filter(entry -> entry.getKey().startsWith("grant")).forEach(entry -> entity.setGrant((Grant) entry.getValue()));
|
||||||
|
}
|
||||||
|
|
||||||
Map<String, Object> extraProperties = new HashMap<>();
|
Map<String, Object> extraProperties = new HashMap<>();
|
||||||
extraProperties.put("language", LanguageRDAMapper.mapRDAIsoToLanguageIso(rda.getLanguage()));
|
extraProperties.put("language", LanguageRDAMapper.mapRDAIsoToLanguageIso(rda.getLanguage()));
|
||||||
|
|
|
@ -17,9 +17,11 @@ public class KeywordRDAMapper {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
try {
|
try {
|
||||||
value = JavaToJson.objectStringToJson(value);
|
value = JavaToJson.objectStringToJson(value);
|
||||||
|
if (!value.isEmpty()) {
|
||||||
List<Tag> tags = Arrays.asList(mapper.readValue(value, Tag[].class));
|
List<Tag> tags = Arrays.asList(mapper.readValue(value, Tag[].class));
|
||||||
List<String> keywordNames = tags.stream().map(Tag::getName).collect(Collectors.toList());
|
List<String> keywordNames = tags.stream().map(Tag::getName).collect(Collectors.toList());
|
||||||
return keywordNames;
|
return keywordNames;
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error(e.getMessage(), e);
|
logger.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue