2019-11-27 18:21:01 +01:00
|
|
|
package org.gcube.data.publishing.ckan2zenodo;
|
|
|
|
|
2019-11-29 17:57:49 +01:00
|
|
|
import java.time.LocalDate;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.time.LocalTime;
|
|
|
|
import java.time.ZoneOffset;
|
|
|
|
import java.time.ZonedDateTime;
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
import java.time.temporal.ChronoField;
|
|
|
|
import java.time.temporal.TemporalAccessor;
|
2019-11-29 15:52:17 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.gcube.data.publishing.ckan2zenodo.model.zenodo.Commons;
|
|
|
|
|
|
|
|
import com.jayway.jsonpath.Configuration;
|
2019-11-27 18:21:01 +01:00
|
|
|
import com.jayway.jsonpath.DocumentContext;
|
|
|
|
import com.jayway.jsonpath.JsonPath;
|
2019-11-29 15:52:17 +01:00
|
|
|
import com.jayway.jsonpath.Option;
|
2019-11-27 18:21:01 +01:00
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
public class Fixer {
|
2019-11-29 17:57:49 +01:00
|
|
|
|
|
|
|
|
2019-11-27 18:21:01 +01:00
|
|
|
|
2019-11-29 17:57:49 +01:00
|
|
|
private static DateTimeFormatter INCOMING_FORMATTER=null;
|
|
|
|
private static DateTimeFormatter INTERNAL_FORMATTER=null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-11-29 15:52:17 +01:00
|
|
|
private static Configuration PATH_CONFIGURATION=null;
|
|
|
|
private static Configuration ALWAYS_LIST_CONFIG=null;
|
|
|
|
static {
|
2019-11-29 18:05:44 +01:00
|
|
|
INCOMING_FORMATTER=DateTimeFormatter.ofPattern("[yyyy-MM-dd['T'HH:mm:ss[.SSSSSS[z][Z][XXX]]]]");
|
2019-11-29 17:57:49 +01:00
|
|
|
INTERNAL_FORMATTER=DateTimeFormatter.ofPattern(Commons.ISO_DATE_PATTERN);
|
2019-11-29 15:52:17 +01:00
|
|
|
|
2019-11-29 17:57:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PATH_CONFIGURATION = Configuration.builder().options(Option.AS_PATH_LIST,Option.SUPPRESS_EXCEPTIONS,Option.DEFAULT_PATH_LEAF_TO_NULL).build();
|
|
|
|
ALWAYS_LIST_CONFIG= Configuration.builder().options(Option.ALWAYS_RETURN_LIST,Option.SUPPRESS_EXCEPTIONS,Option.DEFAULT_PATH_LEAF_TO_NULL).build();
|
2019-11-29 15:52:17 +01:00
|
|
|
}
|
2019-11-29 17:57:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-11-27 18:21:01 +01:00
|
|
|
public static final String fixIncoming(String toFix) {
|
2019-11-29 15:52:17 +01:00
|
|
|
DocumentContext ctx=JsonPath.using(ALWAYS_LIST_CONFIG).parse(toFix);
|
|
|
|
DocumentContext pathCtx=JsonPath.using(PATH_CONFIGURATION).parse(toFix);
|
2019-11-27 18:21:01 +01:00
|
|
|
|
2019-11-29 15:52:17 +01:00
|
|
|
ctx=fixIncomingDate(ctx,pathCtx, "$.created");
|
|
|
|
ctx=fixIncomingDate(ctx,pathCtx, "$.modified");
|
|
|
|
ctx=fixIncomingDate(ctx,pathCtx, "$..publication_date");
|
|
|
|
ctx=fixIncomingDate(ctx,pathCtx, "$..embargo_date");
|
2019-11-29 17:57:49 +01:00
|
|
|
|
2019-11-27 18:21:01 +01:00
|
|
|
return ctx.jsonString();
|
|
|
|
}
|
2019-11-29 17:57:49 +01:00
|
|
|
|
|
|
|
|
2019-11-27 18:21:01 +01:00
|
|
|
public static String fixSending(String toFix) {
|
2019-11-29 15:52:17 +01:00
|
|
|
DocumentContext ctx=JsonPath.using(ALWAYS_LIST_CONFIG).parse(toFix);
|
|
|
|
DocumentContext pathCtx=JsonPath.using(PATH_CONFIGURATION).parse(toFix);
|
2019-11-29 17:57:49 +01:00
|
|
|
|
2019-11-29 15:52:17 +01:00
|
|
|
ctx=fixOutgoingDate(ctx,pathCtx, "$.created");
|
|
|
|
ctx=fixOutgoingDate(ctx,pathCtx, "$.modified");
|
|
|
|
ctx=fixOutgoingDate(ctx,pathCtx, "$..publication_date");
|
|
|
|
ctx=fixOutgoingDate(ctx,pathCtx, "$..embargo_date");
|
2019-11-29 17:57:49 +01:00
|
|
|
|
|
|
|
|
2019-11-27 18:21:01 +01:00
|
|
|
return ctx.jsonString();
|
|
|
|
}
|
2019-11-29 17:57:49 +01:00
|
|
|
|
2019-11-27 18:21:01 +01:00
|
|
|
//*** INCOMINGs
|
2019-11-29 15:52:17 +01:00
|
|
|
private static final DocumentContext fixIncomingDate(DocumentContext valuesCtx,DocumentContext pathCtx,String toFixPath) {
|
|
|
|
try {
|
|
|
|
List<String> values=valuesCtx.read(toFixPath);
|
|
|
|
List<String> paths=pathCtx.read(toFixPath);
|
2019-11-29 17:57:49 +01:00
|
|
|
if(values!=null)
|
|
|
|
for(int i=0;i<values.size();i++) {
|
|
|
|
String toFix=values.get(i);
|
|
|
|
if(toFix!=null) {
|
|
|
|
JsonPath path=JsonPath.compile(paths.get(i));
|
|
|
|
valuesCtx.set(path, fixIncomingDateString(toFix));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:21:01 +01:00
|
|
|
}catch(Throwable e) {
|
2019-11-29 17:57:49 +01:00
|
|
|
log.warn("Unable to fix "+toFixPath+". Cause : ",e);
|
2019-11-27 18:21:01 +01:00
|
|
|
}
|
2019-11-29 15:52:17 +01:00
|
|
|
return valuesCtx;
|
|
|
|
}
|
2019-11-29 17:57:49 +01:00
|
|
|
|
|
|
|
public static final String fixIncomingDateString(String toFix) throws Exception {
|
2019-11-29 15:52:17 +01:00
|
|
|
|
2019-11-29 17:57:49 +01:00
|
|
|
TemporalAccessor accessor=INCOMING_FORMATTER.parse(toFix);
|
|
|
|
ZonedDateTime zTime=null;
|
|
|
|
if(accessor.isSupported(ChronoField.HOUR_OF_DAY))
|
|
|
|
zTime = LocalDateTime.from(accessor).atZone(ZoneOffset.UTC);
|
|
|
|
else {
|
|
|
|
LocalDate date=LocalDate.from(accessor);
|
|
|
|
zTime=LocalDateTime.of(date, LocalTime.of(0, 0, 0, 0)).atZone(ZoneOffset.UTC);
|
2019-11-29 15:52:17 +01:00
|
|
|
}
|
2019-11-29 17:57:49 +01:00
|
|
|
return zTime.format(INTERNAL_FORMATTER);
|
|
|
|
|
2019-11-27 18:21:01 +01:00
|
|
|
}
|
2019-11-29 17:57:49 +01:00
|
|
|
|
|
|
|
|
2019-11-27 18:21:01 +01:00
|
|
|
//*** OUTGOINGs
|
2019-11-29 17:57:49 +01:00
|
|
|
|
2019-11-29 15:52:17 +01:00
|
|
|
private static final DocumentContext fixOutgoingDate(DocumentContext valuesCtx,DocumentContext pathCtx,String toFixPath) {
|
2019-11-27 18:21:01 +01:00
|
|
|
try{
|
2019-11-29 15:52:17 +01:00
|
|
|
List<String> values=valuesCtx.read(toFixPath);
|
|
|
|
List<String> paths=pathCtx.read(toFixPath);
|
2019-11-29 17:57:49 +01:00
|
|
|
if(values!=null)
|
|
|
|
for(int i=0;i<values.size();i++) {
|
|
|
|
String toFix=values.get(i);
|
|
|
|
if(toFix!=null) {
|
|
|
|
JsonPath path=JsonPath.compile(paths.get(i));
|
|
|
|
valuesCtx.set(path, toFix.substring(0,toFix.length()-2)+":"+
|
|
|
|
toFix.substring(toFix.length()-2, toFix.length()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:21:01 +01:00
|
|
|
}catch(Throwable t) {
|
2019-11-29 17:57:49 +01:00
|
|
|
log.warn("Unable to fix "+toFixPath+".Cause : ",t);
|
2019-11-27 18:21:01 +01:00
|
|
|
}
|
2019-11-29 17:57:49 +01:00
|
|
|
return valuesCtx;
|
2019-11-27 18:21:01 +01:00
|
|
|
}
|
|
|
|
}
|