Reintroduced map unmarshalling support for backward compatibility

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib@152925 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-09-11 13:44:39 +00:00
parent ec0e4b0ed2
commit 160a1c2cc8
1 changed files with 24 additions and 29 deletions

View File

@ -3,6 +3,8 @@
*/
package org.gcube.documentstore.records;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.HashSet;
@ -10,6 +12,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.gcube.documentstore.exception.InvalidValueException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -20,13 +23,6 @@ public class RecordUtility {
private static Logger logger = LoggerFactory.getLogger(RecordUtility.class);
/*
private final static String LINE_FREFIX = "{";
private final static String LINE_SUFFIX = "}";
private final static String KEY_VALUE_PAIR_SEPARATOR = ",";
private final static String KEY_VALUE_LINKER = "=";
*/
protected static Set<Package> recordPackages;
protected static Map<String, Class<? extends Record>> recordClassesFound;
protected static Map<String, Class<? extends AggregatedRecord<?,?>>> aggregatedRecordClassesFound;
@ -183,14 +179,19 @@ public class RecordUtility {
* }
*/
/*
private final static String LINE_FREFIX = "{";
private final static String LINE_SUFFIX = "}";
private final static String KEY_VALUE_PAIR_SEPARATOR = ",";
private final static String KEY_VALUE_LINKER = "=";
protected static Map<String, ? extends Serializable> getMapFromString(String serializedMap){
/* Checking line sanity * /
/* Checking line sanity */
if(!serializedMap.startsWith(LINE_FREFIX) && !serializedMap.endsWith(LINE_SUFFIX)){
return null;
}
/* Cleaning prefix and suffix to parse line * /
/* Cleaning prefix and suffix to parse line */
serializedMap = serializedMap.replace(LINE_FREFIX, "");
serializedMap = serializedMap.replace(LINE_SUFFIX, "");
@ -210,7 +211,6 @@ public class RecordUtility {
return map;
}
*/
protected static final String INVALID = "invalid";
@ -222,17 +222,15 @@ public class RecordUtility {
*/
@SuppressWarnings("unchecked")
public static <R extends Record> R getRecord(String json) throws Exception {
return (R) DSMapper.unmarshal(Record.class, json);
/* verify if serializedMap is a json (new serializable or old method)
if (DSMapper.isJSONValid(serializedMapOrValidJSON)){
// verify if serializedMap is a json (new serializable or old method)
if (DSMapper.isJSONValid(json)){
logger.debug("Unmarshal record with jackson");
}
else{
return (R) DSMapper.unmarshal(Record.class, json);
} else {
//old method
logger.debug("Unmarshal record custom");
Map<String,? extends Serializable> map = getMapFromString(serializedMapOrValidJSON);
Map<String,? extends Serializable> map = getMapFromString(json);
if (map!=null){
Record record = getRecord(map);
try {
@ -241,23 +239,21 @@ public class RecordUtility {
record.setResourceProperty(INVALID, true);
logger.error("Recovered record is not valid. Anyway, it will be persisted", e);
}
return (R)record;
}
else
return (R) record;
} else {
return null;
}
}
*/
}
/* *
/**
* Create a Record from a Map
* @param recordMap the Map
* @return the Record
* @throws Exception if deserialization fails
* /
*/
@SuppressWarnings("unchecked")
/*
* @SuppressWarnings("unchecked")
public static Record getRecord(Map<String, ? extends Serializable> recordMap) throws Exception {
String className = (String) recordMap.get(Record.RECORD_TYPE);
@ -267,12 +263,12 @@ public class RecordUtility {
* with usageRecordType instead recordType property.
*
* TODO Remove when all old fallback files has been elaborated
* /
*/
if(className == null){
className = (String) recordMap.get("usageRecordType");
((Map<String, Serializable>) recordMap).put(Record.RECORD_TYPE, className);
}
/* END of Patch * /
/*END of Patch */
boolean aggregated = false;
try {
@ -296,7 +292,6 @@ public class RecordUtility {
return record;
}
*/