ckan2zenodo-library/src/main/java/org/gcube/data/publishing/ckan2zenodo/Fixer.java

139 lines
3.8 KiB
Java
Raw Normal View History

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.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.util.List;
2019-12-03 12:50:36 +01:00
import org.gcube.data.publishing.ckan2zenodo.commons.Parsing;
import org.gcube.data.publishing.ckan2zenodo.model.zenodo.Commons;
2019-11-27 18:21:01 +01:00
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
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-12-03 12:50:36 +01:00
static {
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 17:57:49 +01:00
2019-11-27 18:21:01 +01:00
public static final String fixIncoming(String toFix) {
2019-12-03 12:50:36 +01:00
DocumentContext ctx=JsonPath.using(Parsing.JSON_PATH_ALWAYS_LIST_CONFIG).parse(toFix);
DocumentContext pathCtx=JsonPath.using(Parsing.JSON_PATH_PATHS_CONFIGURATION).parse(toFix);
2019-11-27 18:21:01 +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
/**
* It seems never used to me - by Francesco M.
* @param toFix
* @return
*/
2019-11-27 18:21:01 +01:00
public static String fixSending(String toFix) {
2019-12-03 12:50:36 +01:00
DocumentContext ctx=JsonPath.using(Parsing.JSON_PATH_ALWAYS_LIST_CONFIG).parse(toFix);
DocumentContext pathCtx=JsonPath.using(Parsing.JSON_PATH_PATHS_CONFIGURATION).parse(toFix);
2019-11-29 17:57:49 +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
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
}
return valuesCtx;
}
2019-11-29 17:57:49 +01:00
/**
* Updated by Francesco M., see #26166
* @param toFix
* @return
* @throws Exception
*/
2019-11-29 17:57:49 +01:00
public static final String fixIncomingDateString(String toFix) throws Exception {
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);
//original code
//zTime=LocalDateTime.of(date, LocalTime.of(0, 0, 0, 0)).atZone(ZoneOffset.UTC);
//new code by Francesco M. returning a date without the Time
return date.toString();
}
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
private static final DocumentContext fixOutgoingDate(DocumentContext valuesCtx,DocumentContext pathCtx,String toFixPath) {
2019-11-27 18:21:01 +01:00
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, 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
}
}