argos/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/MigrationTools.java

96 lines
4.4 KiB
Java

package eu.old.eudat.migration;
import eu.eudat.commons.JsonHandlingService;
import eu.eudat.convention.ConventionService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.stereotype.Service;
@Service
public class MigrationTools {
private final JsonHandlingService jsonHandlingService;
private final ConventionService conventionService;
public MigrationTools(JsonHandlingService jsonHandlingService, ConventionService conventionService) {
this.jsonHandlingService = jsonHandlingService;
this.conventionService = conventionService;
}
public <T> T tryParseJsonAsObjectString(Class<T> type, String value){
T item = this.jsonHandlingService.fromJsonSafe(type, value);
if (item == null) item = this.jsonHandlingService.fromJsonSafe(type, StringEscapeUtils.unescapeJava(value));
if (item == null) item = this.jsonHandlingService.fromJsonSafe(type, StringEscapeUtils.unescapeJson(value));
if (item == null) {
String newValue = StringEscapeUtils.unescapeJava(value);
newValue = newValue.trim().replace("[\"{", "[{");
newValue = newValue.trim().replace("}\"]", "}]");
newValue = newValue.trim().replace("}\", \"{", "}, {");
newValue = newValue.trim().replace("}\",\"{", "},{");
newValue = newValue.trim().replace(", \"{", ", {");
newValue = newValue.trim().replace(",\"{", ",{");
newValue = newValue.trim().replace("}\",", "},");
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.startsWith("\"")) newValue = newValue.substring(1);
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.endsWith("\"")) newValue = newValue.substring(0, newValue.length() - 1);
item = this.jsonHandlingService.fromJsonSafe(type, newValue);
}
if (item == null) {
String newValue = StringEscapeUtils.unescapeJson(value);
newValue = newValue.trim().replace("[\"{", "[{");
newValue = newValue.trim().replace("}\"]", "}]");
newValue = newValue.trim().replace("}\", \"{", "}, {");
newValue = newValue.trim().replace("}\",\"{", "},{");
newValue = newValue.trim().replace(", \"{", ", {");
newValue = newValue.trim().replace(",\"{", ",{");
newValue = newValue.trim().replace("}\",", "},");
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.startsWith("\"")) newValue = newValue.substring(1);
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.endsWith("\"")) newValue = newValue.substring(0, newValue.length() - 1);
item = this.jsonHandlingService.fromJsonSafe(type, newValue);
}
if (item == null) {
String newValue = value.trim().replace("\\", "");
newValue = newValue.trim().replace("[\"{", "[{");
newValue = newValue.trim().replace("}\"]", "}]");
newValue = newValue.trim().replace("}\", \"{", "}, {");
newValue = newValue.trim().replace("}\",\"{", "},{");
newValue = newValue.trim().replace(", \"{", ", {");
newValue = newValue.trim().replace(",\"{", ",{");
newValue = newValue.trim().replace("}\",", "},");
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.startsWith("\"")) newValue = newValue.substring(1);
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.endsWith("\"")) newValue = newValue.substring(0, newValue.length() - 1);
item = this.jsonHandlingService.fromJsonSafe(type, newValue);
}
if (item == null) {
String newValue = value.trim().replace("=", ":");
newValue = newValue.trim().replace("{", "{\"");
newValue = newValue.trim().replace("}", "\"}");
newValue = newValue.trim().replace(":", "\":\"");
newValue = newValue.trim().replace(", ", "\", \"");
newValue = newValue.trim().replace("}\", \"{", "}, {");
newValue = newValue.trim().replace("\"\"", "\"");
newValue = newValue.trim().replace(":\"null,", ":null,");
newValue = newValue.trim().replace(":\"null\",", ":null,");
newValue = newValue.trim().replace(":\"null\"", ":null");
newValue = newValue.trim().replace(":\"null}\"", ":null}");
newValue = newValue.trim().replace("https\":\"", "https:");
newValue = newValue.trim().replace("datarepo\":\"", "datarepo:");
item = this.jsonHandlingService.fromJsonSafe(type, newValue);
}
return item;
}
public String cleanTagString(String value){
value = value.trim().replace("=", ":");
value = value.trim().replace("{", "{\"");
value = value.trim().replace("}", "\"}");
value = value.trim().replace(":", "\":\"");
value = value.trim().replace(", ", "\", \"");
value = value.trim().replace("}\", \"{", "}, {");
return value;
}
}