dnet-applications/apps/dnet-is-application/src/main/java/eu/dnetlib/is/importer/WfHistoryImporter.java

86 lines
2.6 KiB
Java
Raw Normal View History

2022-11-23 15:32:36 +01:00
package eu.dnetlib.is.importer;
2022-06-30 14:43:26 +02:00
import java.io.File;
2023-02-27 11:43:28 +01:00
import java.time.Instant;
import java.time.LocalDateTime;
2022-06-30 14:43:26 +02:00
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
2023-02-27 11:43:28 +01:00
import java.util.TimeZone;
2022-06-30 14:43:26 +02:00
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
2022-11-28 11:11:13 +01:00
import org.springframework.stereotype.Service;
2022-06-30 14:43:26 +02:00
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
2023-03-21 15:28:18 +01:00
import eu.dnetlib.manager.history.model.WfHistoryEntry;
import eu.dnetlib.manager.history.repository.WfHistoryEntryRepository;
2022-06-30 14:43:26 +02:00
2022-11-28 11:11:13 +01:00
@Service
2022-06-30 14:43:26 +02:00
public class WfHistoryImporter {
private static final Log log = LogFactory.getLog(WfHistoryImporter.class);
@Autowired
2023-03-21 15:28:18 +01:00
private WfHistoryEntryRepository wfHistoryEntryRepository;
2022-06-30 14:43:26 +02:00
public void load(final String path) throws Exception {
final ObjectMapper mapper = new ObjectMapper();
final JsonNode rootNode = mapper.readTree(new File(path));
rootNode.forEach(wf -> saveWf(wf));
}
private void saveWf(final JsonNode node) {
2023-03-21 15:28:18 +01:00
final WfHistoryEntry wf = new WfHistoryEntry();
2022-06-30 14:43:26 +02:00
wf.setProcessId(node.get("system:processId").asText());
wf.setName(node.get("system:wfName").asText());
wf.setFamily(node.get("system:profileFamily").asText());
if (node.has("dataprovider:id")) {
wf.setDsId(node.get("dataprovider:id").asText());
}
if (node.has("dataprovider:name")) {
wf.setDsName(node.get("dataprovider:name").asText());
}
if (node.has("dataprovider:interface")) {
wf.setDsApi(node.get("dataprovider:interface").asText());
}
2023-02-27 11:43:28 +01:00
wf.setStartDate(LocalDateTime
.ofInstant(Instant.ofEpochMilli(NumberUtils.toLong(node.get("system:startDate").asText())), TimeZone.getDefault().toZoneId()));
wf.setEndDate(LocalDateTime.ofInstant(Instant.ofEpochMilli(NumberUtils.toLong(node.get("system:endDate").asText())), TimeZone.getDefault().toZoneId()));
2022-06-30 14:43:26 +02:00
if (BooleanUtils.toBoolean(node.get("system:isCompletedSuccessfully").asText())) {
wf.setStatus("success");
} else {
wf.setStatus("failure");
}
final Map<String, String> details = new LinkedHashMap<>();
final Iterator<Entry<String, JsonNode>> fields = node.fields();
while (fields.hasNext()) {
final Entry<String, JsonNode> f = fields.next();
if (f.getValue().isValueNode()) {
details.put(f.getKey(), f.getValue().asText());
}
}
wf.setDetails(details);
2023-03-21 15:28:18 +01:00
wfHistoryEntryRepository.save(wf);
2022-06-30 14:43:26 +02:00
log.info("Wf saved with id: " + wf.getProcessId());
}
}