Multiple Date Formats
This commit is contained in:
parent
ad5755c7e3
commit
e07e1ed958
|
@ -4,7 +4,8 @@
|
|||
### Enhancements
|
||||
- Default Ckan2Zenodo translation to comply with mandatory Zenodo fields (https://support.d4science.org/issues/19489)
|
||||
- Ckan2Zenodo library to provide means to apply default translation only (https://support.d4science.org/issues/19490)
|
||||
-Support to "split" on source values
|
||||
|
||||
-Support to "split" on source values (https://support.d4science.org/issues/19534)
|
||||
-Support to "append" to target elements (https://support.d4science.org/issues/19535)
|
||||
-Multiple Date Format parsing (https://support.d4science.org/issues/19540)
|
||||
|
||||
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
@ -0,0 +1,41 @@
|
|||
package org.gcube.data.publishing.ckan2zenodo.commons;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import org.gcube.data.publishing.ckan2zenodo.model.zenodo.Commons;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
public class DateParser extends JsonDeserializer<Date>{
|
||||
|
||||
private static final String[] DATE_FORMATS=new String[] {
|
||||
Commons.ISO_DATE_PATTERN,
|
||||
"yyyy-MM-dd"
|
||||
};
|
||||
|
||||
|
||||
@Override
|
||||
public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
JsonNode node = p.getCodec().readTree(p);
|
||||
final String date = node.textValue();
|
||||
|
||||
for (String DATE_FORMAT : DATE_FORMATS) {
|
||||
try {
|
||||
return new SimpleDateFormat(DATE_FORMAT).parse(date);
|
||||
} catch (ParseException e) {
|
||||
}
|
||||
}
|
||||
throw new JsonParseException("Unable to parse date "+date,null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -2,19 +2,27 @@ package org.gcube.data.publishing.ckan2zenodo.model.zenodo;
|
|||
|
||||
import java.util.Date;
|
||||
|
||||
import org.gcube.data.publishing.ckan2zenodo.commons.DateParser;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
|
||||
public class DateInterval {
|
||||
public static enum Type{
|
||||
Collected, Valid, Withdrawn
|
||||
}
|
||||
//TODO multiple patterns
|
||||
// @JsonFormat(pattern = Commons.ISO_DATE_PATTERN)
|
||||
|
||||
@JsonFormat(pattern = Commons.ISO_DATE_PATTERN)
|
||||
@JsonDeserialize(using = DateParser.class)
|
||||
private Date start;
|
||||
// @JsonFormat(pattern = Commons.ISO_DATE_PATTERN)
|
||||
@JsonFormat(pattern = Commons.ISO_DATE_PATTERN)
|
||||
@JsonDeserialize(using = DateParser.class)
|
||||
private Date end;
|
||||
// @NonNull
|
||||
private Type type;
|
||||
|
|
|
@ -3,6 +3,8 @@ package org.gcube.tests;
|
|||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.data.publishing.ckan2zenodo.Fixer;
|
||||
import org.gcube.data.publishing.ckan2zenodo.Translator;
|
||||
|
@ -20,13 +22,19 @@ public class TestCommons {
|
|||
static ZenodoCredentials loadCredentials() throws IOException {
|
||||
String BASE_URL="https://sandbox.zenodo.org/api/";
|
||||
|
||||
String key=Files.readAllLines(Paths.get("/home/fabio/workspaces/zenodo.key")).get(0);
|
||||
String key=Files.readAllLines(Paths.get("/Users/FabioISTI/eclipse-workspace/zenodo-sandbox.key")).get(0);
|
||||
|
||||
System.out.println("Using key : "+key);
|
||||
return new ZenodoCredentials(key,BASE_URL);
|
||||
}
|
||||
|
||||
|
||||
static Map<String,String> getDefinedMappings(){
|
||||
HashMap<String,String> toReturn=new HashMap<String,String>();
|
||||
toReturn.put("/blue_cloud_dataset.json", "/blue_cloud_dataset.xml");
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
static String convertStreamToString(java.io.InputStream is) {
|
||||
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
|
||||
return s.hasNext() ? s.next() : "";
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package org.gcube.tests;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
|
@ -19,13 +18,13 @@ public class TransformationTests {
|
|||
|
||||
static ObjectMapper mapper=null;
|
||||
|
||||
static Map<String,String> mappings=new HashMap<String,String>();
|
||||
static Map<String,String> mappings;
|
||||
|
||||
@BeforeClass
|
||||
public static void init () {
|
||||
mapper=TestCommons.getMapper();
|
||||
|
||||
mappings.put("/blue_cloud_dataset.json", "/blue_cloud_dataset.xml");
|
||||
mappings=TestCommons.getDefinedMappings();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -3,6 +3,8 @@ package org.gcube.tests;
|
|||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.gcube.common.resources.gcore.GenericResource;
|
||||
import org.gcube.common.resources.gcore.Resources;
|
||||
|
@ -34,11 +36,12 @@ import com.jayway.jsonpath.JsonPath;
|
|||
public class ZenodoTests {
|
||||
|
||||
private static ZenodoCredentials credentials;
|
||||
|
||||
private static Map<String,String> mappings;
|
||||
|
||||
@BeforeClass
|
||||
public static final void init() throws IOException {
|
||||
credentials=TestCommons.loadCredentials();
|
||||
mappings=TestCommons.getDefinedMappings();
|
||||
}
|
||||
|
||||
|
||||
|
@ -144,4 +147,19 @@ public class ZenodoTests {
|
|||
Assert.assertEquals(fakeTitle, dep.getTitle());
|
||||
System.out.println(dep);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testMappings() throws Exception {
|
||||
Zenodo z=new Zenodo(credentials);
|
||||
|
||||
for(Entry<String,String> entry:mappings.entrySet()) {
|
||||
GenericResource resource=Resources.unmarshal(GenericResource.class, TransformationTests.class.getResourceAsStream(entry.getValue()));
|
||||
Translator translator=new Translator(IS.readMappings(resource));
|
||||
ZenodoDeposition dep=TestCommons.readAndTransform(entry.getKey(), translator,z.createNew());
|
||||
dep=z.updateMetadata(dep);
|
||||
System.out.println("Created "+dep);
|
||||
// System.out.println("Published "+z.publish(dep));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,35 +41,7 @@
|
|||
<targetElement>type</targetElement>
|
||||
</mapping>
|
||||
|
||||
|
||||
<!-- Identity:Creator -->
|
||||
<mapping>
|
||||
<source>
|
||||
<value type="jsonPath">$.extras[?(@.key=='Identity:Creator')].value</value>
|
||||
</source>
|
||||
<targetPath>$.metadata.creators[2]</targetPath>
|
||||
<targetElement>name</targetElement>
|
||||
</mapping>
|
||||
|
||||
|
||||
|
||||
<!-- Identity:Owner -->
|
||||
<mapping>
|
||||
<source>
|
||||
<value type="jsonPath">$.extras[?(@.key=='Identity:Owner')].value</value>
|
||||
</source>
|
||||
<targetPath>$.metadata.contributors[*]</targetPath>
|
||||
<targetElement>name</targetElement>
|
||||
</mapping>
|
||||
|
||||
<mapping>
|
||||
<source>
|
||||
<value type="constant">ContactPerson</value>
|
||||
</source>
|
||||
<targetPath>$.metadata.contributors[*]</targetPath>
|
||||
<targetElement>type</targetElement>
|
||||
</mapping>
|
||||
|
||||
|
||||
|
||||
<!-- Identity:RelatedPaper-->
|
||||
|
||||
|
|
Loading…
Reference in New Issue