153 lines
9.3 KiB
Java
153 lines
9.3 KiB
Java
//package eu.eudat.service.remotefetcher;
|
|
//
|
|
//import com.fasterxml.jackson.databind.ObjectMapper;
|
|
//import com.jayway.jsonpath.DocumentContext;
|
|
//import eu.eudat.service.remotefetcher.config.DataUrlConfiguration;
|
|
//import eu.eudat.service.remotefetcher.criteria.ExternalReferenceCriteria;
|
|
//import eu.eudat.service.remotefetcher.models.ExternalRefernceResult;
|
|
//import net.minidev.json.JSONArray;
|
|
//import org.slf4j.Logger;
|
|
//import org.slf4j.LoggerFactory;
|
|
//
|
|
//import java.lang.reflect.InvocationTargetException;
|
|
//import java.lang.reflect.Method;
|
|
//import java.util.*;
|
|
//import java.util.stream.Collectors;
|
|
//
|
|
//public class RemoteFetcherUtils {
|
|
// private final static Logger logger = LoggerFactory.getLogger(RemoteFetcherUtils.class);
|
|
// private static final ObjectMapper mapper = new ObjectMapper();
|
|
//
|
|
// public static ExternalRefernceResult getFromJson(DocumentContext jsonContext, DataUrlConfiguration jsonDataPath) {
|
|
// return new ExternalRefernceResult(parseData(jsonContext, jsonDataPath),
|
|
// new HashMap<>(1, 1));
|
|
// }
|
|
//
|
|
// public static ExternalRefernceResult getFromJsonWithRecursiveFetching(DocumentContext jsonContext, DataUrlConfiguration jsonDataPath, RemoteFetcherServiceImpl remoteFetcherService, String requestBody, String requestType, String auth) {
|
|
// ExternalRefernceResult externalRefernceResult = new ExternalRefernceResult(parseData(jsonContext, jsonDataPath),
|
|
// new HashMap<>(1, 1));
|
|
//
|
|
// List<Map<String, String>> multiResults = externalRefernceResult.getResults().stream().map(result -> {
|
|
// ExternalReferenceCriteria externalReferenceCriteria = new ExternalReferenceCriteria();
|
|
// externalReferenceCriteria.setPath(result.get("path"));
|
|
// externalReferenceCriteria.setHost(result.get("host"));
|
|
// String replacedPath = remoteFetcherService.replaceCriteriaOnUrl(jsonDataPath.getUrlConfiguration().getUrl(), externalReferenceCriteria, jsonDataPath.getUrlConfiguration().getFirstpage(), jsonDataPath.getUrlConfiguration().getQueries());
|
|
// return remoteFetcherService.getResultsFromUrl(replacedPath, jsonDataPath.getUrlConfiguration().getData(), jsonDataPath.getUrlConfiguration().getData().getPath(), jsonDataPath.getUrlConfiguration().getContentType(), requestBody, requestType, auth);
|
|
// }).filter(Objects::nonNull).map(externalRefernceResult1 -> externalRefernceResult1.getResults().get(0)).collect(Collectors.toList());
|
|
// return new ExternalRefernceResult(multiResults, new HashMap<>(1, 1));
|
|
// }
|
|
//
|
|
// public static ExternalRefernceResult getFromJsonWithFirstAndLastName(DocumentContext jsonContext, DataUrlConfiguration jsonDataPath) {
|
|
// ExternalRefernceResult externalRefernceResult = new ExternalRefernceResult(parseData(jsonContext, jsonDataPath),
|
|
// new HashMap<>(1, 1));
|
|
// externalRefernceResult.getResults().stream().forEach(entry -> {
|
|
// String name = entry.get(jsonDataPath.getFieldsUrlConfiguration().getFirstName().replace("'", "")) + " " + entry.get(jsonDataPath.getFieldsUrlConfiguration().getLastName().replace("'", ""));
|
|
// entry.put("name", name);
|
|
// entry.remove(jsonDataPath.getFieldsUrlConfiguration().getFirstName().replace("'", ""));
|
|
// entry.remove(jsonDataPath.getFieldsUrlConfiguration().getLastName().replace("'", ""));
|
|
// });
|
|
// return externalRefernceResult;
|
|
// }
|
|
//
|
|
// private static List<Map<String, String>> parseData (DocumentContext jsonContext, DataUrlConfiguration jsonDataPath) {
|
|
// List <Map<String, Object>> rawData = jsonContext.read(jsonDataPath.getPath());
|
|
// List<Map<String, String>> parsedData = new ArrayList<>();
|
|
// rawData.forEach(stringObjectMap -> {
|
|
// parsedData.add(new LinkedHashMap<>());
|
|
// Arrays.stream(jsonDataPath.getFieldsUrlConfiguration().getClass().getDeclaredFields()).forEach(field -> {
|
|
// String getterMethodName = "get" + field.getName().substring(0, 1).toUpperCase(Locale.ROOT) + field.getName().substring(1);
|
|
// Method getterMethod = Arrays.stream(jsonDataPath.getFieldsUrlConfiguration().getClass().getDeclaredMethods()).filter(method -> method.getName().equals(getterMethodName)).collect(Collectors.toList()).get(0);
|
|
// try {
|
|
// String value = ((String) getterMethod.invoke(jsonDataPath.getFieldsUrlConfiguration()));
|
|
// if (value != null) {
|
|
// if (field.getName().equals("pid") || field.getName().equals("pidTypeField")) {
|
|
// String pid = null;
|
|
// Object pidObj = stringObjectMap.get(value.split("\\.")[0]);
|
|
// if(pidObj != null){
|
|
// if(pidObj instanceof Map){
|
|
// Object o = ((Map<String, Object>) pidObj).get(value.split("\\.")[1]);
|
|
// if(o instanceof String){
|
|
// pid = (String)o;
|
|
// }
|
|
// else if(o instanceof Integer){
|
|
// pid = String.valueOf(o);
|
|
// }
|
|
// }
|
|
// else if(pidObj instanceof List){
|
|
// Object o = ((List<Map<String,?>>) pidObj).get(0).get(value.split("\\.")[1]);
|
|
// if(o instanceof String){
|
|
// pid = (String)o;
|
|
// }
|
|
// else if(o instanceof Integer){
|
|
// pid = String.valueOf(o);
|
|
// }
|
|
// }
|
|
// }
|
|
// if(pid != null) {
|
|
// if ((field.getName().equals("pid"))){
|
|
// parsedData.get(parsedData.size() - 1).put("pid", pid);
|
|
// }
|
|
// else{
|
|
// parsedData.get(parsedData.size() - 1).put("pidTypeField", pid);
|
|
// }
|
|
// }
|
|
// } else {
|
|
// value = value.replace("'", "");
|
|
// if (value.contains(".")) {
|
|
// String[] parts = value.split("\\.");
|
|
// Map<String, Object> tempMap = stringObjectMap;
|
|
// for (int i = 0; i < parts.length; i++) {
|
|
// if (tempMap.containsKey(parts[i])) {
|
|
// if (i + 1 < parts.length) {
|
|
// tempMap = (Map<String, Object>) tempMap.get(parts[i]);
|
|
// } else {
|
|
// parsedData.get(parsedData.size() - 1).put(field.getName().equals("types") ? "tags" : value, normalizeValue(tempMap.get(parts[i]), (field.getName().equals("types") || field.getName().equals("uri"))));
|
|
// }
|
|
// }
|
|
// }
|
|
// } else {
|
|
// if (stringObjectMap.containsKey(value)) {
|
|
// parsedData.get(parsedData.size() - 1).put(field.getName().equals("types") ? "tags" : value, normalizeValue(stringObjectMap.get(value), (field.getName().equals("types") || field.getName().equals("uri"))));
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// } catch (IllegalAccessException | InvocationTargetException e) {
|
|
// logger.error(e.getLocalizedMessage(), e);
|
|
// }
|
|
// });
|
|
// });
|
|
// return parsedData;
|
|
// }
|
|
//
|
|
// private static String normalizeValue(Object value, boolean jsonString) {
|
|
// if (value instanceof JSONArray) {
|
|
// if (jsonString) {
|
|
// return ((JSONArray)value).toJSONString();
|
|
// }
|
|
// JSONArray jarr = (JSONArray) value;
|
|
// if (jarr.get(0) instanceof String) {
|
|
// return jarr.get(0).toString();
|
|
// } else {
|
|
// for (Object o : jarr) {
|
|
// if ((o instanceof Map) && ((Map) o).containsKey("content")) {
|
|
// try {
|
|
// return ((Map<String, String>) o).get("content");
|
|
// }
|
|
// catch (ClassCastException e){
|
|
// if(((Map<?, ?>) o).get("content") instanceof Integer) {
|
|
// return String.valueOf(((Map<?, ?>) o).get("content"));
|
|
// }
|
|
// return null;
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// } else if (value instanceof Map) {
|
|
// String key = ((Map<String, String>)value).containsKey("$") ? "$" : "content";
|
|
// return ((Map<String, String>)value).get(key);
|
|
// }
|
|
// return value != null ? value.toString() : null;
|
|
// }
|
|
//}
|