added the implements Serializable to each class

This commit is contained in:
Miriam Baglioni 2020-04-23 11:48:47 +02:00
parent 24fa81d7e8
commit e431fe4f5b
14 changed files with 173 additions and 175 deletions

View File

@ -4,11 +4,14 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import eu.dnetlib.dhp.selectioncriteria.InterfaceAdapter; import eu.dnetlib.dhp.selectioncriteria.InterfaceAdapter;
import eu.dnetlib.dhp.selectioncriteria.Selection; import eu.dnetlib.dhp.selectioncriteria.Selection;
import eu.dnetlib.dhp.selectioncriteria.VerbResolver; import eu.dnetlib.dhp.selectioncriteria.VerbResolver;
import eu.dnetlib.dhp.selectioncriteria.VerbResolverFactory; import eu.dnetlib.dhp.selectioncriteria.VerbResolverFactory;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -17,15 +20,7 @@ import org.dom4j.DocumentException;
import org.dom4j.Node; import org.dom4j.Node;
import org.dom4j.io.SAXReader; import org.dom4j.io.SAXReader;
/** Created by miriam on 03/08/2018. */
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by miriam on 03/08/2018.
*/
public class CommunityConfigurationFactory { public class CommunityConfigurationFactory {
private static final Log log = LogFactory.getLog(CommunityConfigurationFactory.class); private static final Log log = LogFactory.getLog(CommunityConfigurationFactory.class);
@ -54,7 +49,6 @@ public class CommunityConfigurationFactory {
log.info(String.format("loaded %s community configuration profiles", communities.size())); log.info(String.format("loaded %s community configuration profiles", communities.size()));
log.debug(String.format("loaded community configuration:\n%s", communities.toString())); log.debug(String.format("loaded community configuration:\n%s", communities.toString()));
return new CommunityConfiguration(communities); return new CommunityConfiguration(communities);
} }
@ -98,7 +92,6 @@ public class CommunityConfigurationFactory {
return subjects; return subjects;
} }
private static List<Datasource> parseDatasources(final Node node) { private static List<Datasource> parseDatasources(final Node node) {
final List<Node> list = node.selectNodes("./datasources/datasource"); final List<Node> list = node.selectNodes("./datasources/datasource");
final List<Datasource> datasourceList = new ArrayList<>(); final List<Datasource> datasourceList = new ArrayList<>();
@ -117,11 +110,9 @@ public class CommunityConfigurationFactory {
String oacommunity = null; String oacommunity = null;
if (oacommunitynode != null) { if (oacommunitynode != null) {
String tmp = oacommunitynode.getText(); String tmp = oacommunitynode.getText();
if(StringUtils.isNotBlank(tmp)) if (StringUtils.isNotBlank(tmp)) oacommunity = tmp;
oacommunity = tmp;
} }
final List<Node> list = node.selectNodes("./zenodocommunities/zenodocommunity"); final List<Node> list = node.selectNodes("./zenodocommunities/zenodocommunity");
final List<ZenodoCommunity> zenodoCommunityList = new ArrayList<>(); final List<ZenodoCommunity> zenodoCommunityList = new ArrayList<>();
for (Node n : list) { for (Node n : list) {
@ -139,8 +130,4 @@ public class CommunityConfigurationFactory {
log.info("size of the zenodo community list " + zenodoCommunityList.size()); log.info("size of the zenodo community list " + zenodoCommunityList.size());
return zenodoCommunityList; return zenodoCommunityList;
} }
} }

View File

@ -1,22 +1,17 @@
package eu.dnetlib.dhp.community; package eu.dnetlib.dhp.community;
import eu.dnetlib.dhp.selectioncriteria.Selection; import eu.dnetlib.dhp.selectioncriteria.Selection;
import eu.dnetlib.dhp.selectioncriteria.VerbResolver; import eu.dnetlib.dhp.selectioncriteria.VerbResolver;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
public class Constraint implements Serializable { public class Constraint implements Serializable {
private String verb; private String verb;
private String field; private String field;
private String value; private String value;
private Selection selection; private Selection selection;
public Constraint() { public Constraint() {}
}
public String getVerb() { public String getVerb() {
return verb; return verb;
@ -42,21 +37,17 @@ public class Constraint implements Serializable {
this.value = value; this.value = value;
} }
public void setSelection(Selection sel) { public void setSelection(Selection sel) {
selection = sel; selection = sel;
} }
public void setSelection(VerbResolver resolver) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { public void setSelection(VerbResolver resolver)
throws InvocationTargetException, NoSuchMethodException, InstantiationException,
IllegalAccessException {
selection = resolver.getSelectionCriteria(verb, value); selection = resolver.getSelectionCriteria(verb, value);
} }
public boolean verifyCriteria(String metadata) { public boolean verifyCriteria(String metadata) {
return selection.apply(metadata); return selection.apply(metadata);
} }
} }

View File

@ -1,32 +1,25 @@
package eu.dnetlib.dhp.community; package eu.dnetlib.dhp.community;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import eu.dnetlib.dhp.selectioncriteria.VerbResolver; import eu.dnetlib.dhp.selectioncriteria.VerbResolver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/** /** Created by miriam on 02/08/2018. */
* Created by miriam on 02/08/2018.
*/
public class Constraints implements Serializable { public class Constraints implements Serializable {
private static final Log log = LogFactory.getLog(Constraints.class); private static final Log log = LogFactory.getLog(Constraints.class);
// private ConstraintEncapsulator ce; // private ConstraintEncapsulator ce;
private List<Constraint> constraint; private List<Constraint> constraint;
public Constraints() {}
public Constraints() {
}
public List<Constraint> getConstraint() { public List<Constraint> getConstraint() {
return constraint; return constraint;
} }
@ -38,7 +31,6 @@ public class Constraints implements Serializable {
public void setSc(String json) { public void setSc(String json) {
Type collectionType = new TypeToken<Collection<Constraint>>() {}.getType(); Type collectionType = new TypeToken<Collection<Constraint>>() {}.getType();
constraint = new Gson().fromJson(json, collectionType); constraint = new Gson().fromJson(json, collectionType);
} }
void setSelection(VerbResolver resolver) { void setSelection(VerbResolver resolver) {
@ -56,10 +48,8 @@ public class Constraints implements Serializable {
log.error(e.getMessage()); log.error(e.getMessage());
} }
} }
} }
// Constraint in and // Constraint in and
public boolean verifyCriteria(final Map<String, List<String>> param) { public boolean verifyCriteria(final Map<String, List<String>> param) {
@ -70,10 +60,8 @@ public class Constraints implements Serializable {
verified = true; verified = true;
} }
} }
if(!verified) if (!verified) return verified;
return verified;
} }
return true; return true;
} }
} }

View File

@ -4,63 +4,59 @@ import com.google.common.base.Joiner;
import eu.dnetlib.dhp.utils.ISLookupClientFactory; import eu.dnetlib.dhp.utils.ISLookupClientFactory;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException; import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService; import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
import java.util.List;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
import java.util.List;
public class QueryInformationSystem { public class QueryInformationSystem {
private static final String XQUERY = "for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType') " + private static final String XQUERY =
" let $subj := $x//CONFIGURATION/context/param[./@name='subject']/text() " + "for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType') "
" let $datasources := $x//CONFIGURATION/context/category[./@id=concat($x//CONFIGURATION/context/@id,'::contentproviders')]/concept " + + " let $subj := $x//CONFIGURATION/context/param[./@name='subject']/text() "
" let $organizations := $x//CONFIGURATION/context/category[./@id=concat($x//CONFIGURATION/context/@id,'::resultorganizations')]/concept " + + " let $datasources := $x//CONFIGURATION/context/category[./@id=concat($x//CONFIGURATION/context/@id,'::contentproviders')]/concept "
" let $communities := $x//CONFIGURATION/context/category[./@id=concat($x//CONFIGURATION/context/@id,'::zenodocommunities')]/concept " + + " let $organizations := $x//CONFIGURATION/context/category[./@id=concat($x//CONFIGURATION/context/@id,'::resultorganizations')]/concept "
" where $x//CONFIGURATION/context[./@type='community' or ./@type='ri'] " + + " let $communities := $x//CONFIGURATION/context/category[./@id=concat($x//CONFIGURATION/context/@id,'::zenodocommunities')]/concept "
" return " + + " where $x//CONFIGURATION/context[./@type='community' or ./@type='ri'] "
" <community> " + + " return "
" { $x//CONFIGURATION/context/@id} " + + " <community> "
" <subjects> " + + " { $x//CONFIGURATION/context/@id} "
" {for $y in tokenize($subj,',') " + + " <subjects> "
" return " + + " {for $y in tokenize($subj,',') "
" <subject>{$y}</subject>} " + + " return "
" </subjects> " + + " <subject>{$y}</subject>} "
" <datasources> " + + " </subjects> "
" {for $d in $datasources " + + " <datasources> "
" where $d/param[./@name='enabled']/text()='true' " + + " {for $d in $datasources "
" return " + + " where $d/param[./@name='enabled']/text()='true' "
" <datasource> " + + " return "
" <openaireId> " + + " <datasource> "
" {$d//param[./@name='openaireId']/text()} " + + " <openaireId> "
" </openaireId> " + + " {$d//param[./@name='openaireId']/text()} "
" <selcriteria> " + + " </openaireId> "
" {$d/param[./@name='selcriteria']/text()} " + + " <selcriteria> "
" </selcriteria> " + + " {$d/param[./@name='selcriteria']/text()} "
" </datasource> } " + + " </selcriteria> "
" </datasources> " + + " </datasource> } "
" <zenodocommunities> " + + " </datasources> "
" {for $zc in $communities " + + " <zenodocommunities> "
" return " + + " {for $zc in $communities "
" <zenodocommunity> " + + " return "
" <zenodoid> " + + " <zenodocommunity> "
" {$zc/param[./@name='zenodoid']/text()} " + + " <zenodoid> "
" </zenodoid> " + + " {$zc/param[./@name='zenodoid']/text()} "
" <selcriteria> " + + " </zenodoid> "
" {$zc/param[./@name='selcriteria']/text()} " + + " <selcriteria> "
" </selcriteria> " + + " {$zc/param[./@name='selcriteria']/text()} "
" </zenodocommunity>} " + + " </selcriteria> "
" </zenodocommunities> " + + " </zenodocommunity>} "
" </community>"; + " </zenodocommunities> "
+ " </community>";
public static CommunityConfiguration getCommunityConfiguration(final String isLookupUrl) throws ISLookUpException, DocumentException { public static CommunityConfiguration getCommunityConfiguration(final String isLookupUrl)
throws ISLookUpException, DocumentException {
ISLookUpService isLookUp = ISLookupClientFactory.getLookUpService(isLookupUrl); ISLookUpService isLookUp = ISLookupClientFactory.getLookUpService(isLookupUrl);
final List<String> res = isLookUp.quickSearchProfile(XQUERY); final List<String> res = isLookUp.quickSearchProfile(XQUERY);
final String xmlConf = "<communities>" + Joiner.on(" ").join(res) + "</communities>"; final String xmlConf = "<communities>" + Joiner.on(" ").join(res) + "</communities>";
return CommunityConfigurationFactory.newInstance(xmlConf); return CommunityConfigurationFactory.newInstance(xmlConf);
} }
} }

View File

@ -56,7 +56,8 @@ public class ResultTagger implements Serializable {
// Verify if the entity is deletedbyinference. In case verify if to clean the context list // Verify if the entity is deletedbyinference. In case verify if to clean the context list
// from all the zenodo communities // from all the zenodo communities
if (result.getDataInfo().getDeletedbyinference()) { if (result.getDataInfo().getDeletedbyinference()) {
if (clearContext(result)) return result; clearContext(result);
return result;
} }
// communities contains all the communities to be added as context for the result // communities contains all the communities to be added as context for the result
@ -118,7 +119,15 @@ public class ResultTagger implements Serializable {
.map( .map(
c -> { c -> {
if (communities.contains(c.getId())) { if (communities.contains(c.getId())) {
List<DataInfo> dataInfoList = c.getDataInfo(); Optional<List<DataInfo>> opt_dataInfoList =
Optional.ofNullable(c.getDataInfo());
List<DataInfo> dataInfoList;
if (opt_dataInfoList.isPresent())
dataInfoList = opt_dataInfoList.get();
else {
dataInfoList = new ArrayList<>();
c.setDataInfo(dataInfoList);
}
if (subjects.contains(c.getId())) if (subjects.contains(c.getId()))
dataInfoList.add( dataInfoList.add(
getDataInfo( getDataInfo(
@ -153,7 +162,7 @@ public class ResultTagger implements Serializable {
c -> { c -> {
Context context = new Context(); Context context = new Context();
context.setId(c); context.setId(c);
List<DataInfo> dataInfoList = Arrays.asList(); List<DataInfo> dataInfoList = new ArrayList<>();
if (subjects.contains(c)) if (subjects.contains(c))
dataInfoList.add( dataInfoList.add(
getDataInfo( getDataInfo(

View File

@ -3,8 +3,6 @@ package eu.dnetlib.dhp.community;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import eu.dnetlib.dhp.selectioncriteria.VerbResolver; import eu.dnetlib.dhp.selectioncriteria.VerbResolver;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Collection; import java.util.Collection;
@ -14,9 +12,7 @@ import java.util.Map;
public class SelectionConstraints implements Serializable { public class SelectionConstraints implements Serializable {
private List<Constraints> criteria; private List<Constraints> criteria;
public SelectionConstraints() { public SelectionConstraints() {}
}
public List<Constraints> getCriteria() { public List<Constraints> getCriteria() {
return criteria; return criteria;
@ -40,6 +36,7 @@ public class SelectionConstraints implements Serializable {
} }
return false; return false;
} }
public void setSelection(VerbResolver resolver) { public void setSelection(VerbResolver resolver) {
for (Constraints cs : criteria) { for (Constraints cs : criteria) {

View File

@ -1,12 +1,13 @@
package eu.dnetlib.dhp.selectioncriteria; package eu.dnetlib.dhp.selectioncriteria;
import java.io.Serializable;
@VerbClass("contains") @VerbClass("contains")
public class ContainsVerb implements Selection { public class ContainsVerb implements Selection, Serializable {
private String param; private String param;
public ContainsVerb() { public ContainsVerb() {}
}
public ContainsVerb(final String param) { public ContainsVerb(final String param) {
this.param = param; this.param = param;

View File

@ -1,19 +1,18 @@
package eu.dnetlib.dhp.selectioncriteria; package eu.dnetlib.dhp.selectioncriteria;
import java.io.Serializable;
@VerbClass("equals") @VerbClass("equals")
public class EqualVerb implements Selection { public class EqualVerb implements Selection, Serializable {
private String param; private String param;
public EqualVerb() { public EqualVerb() {}
}
public EqualVerb(final String param) { public EqualVerb(final String param) {
this.param = param; this.param = param;
} }
@Override @Override
public boolean apply(String value) { public boolean apply(String value) {
return value.equalsIgnoreCase(param); return value.equalsIgnoreCase(param);

View File

@ -1,7 +1,6 @@
package eu.dnetlib.dhp.selectioncriteria; package eu.dnetlib.dhp.selectioncriteria;
import com.google.gson.*; import com.google.gson.*;
import java.lang.reflect.Type; import java.lang.reflect.Type;
public class InterfaceAdapter implements JsonSerializer, JsonDeserializer { public class InterfaceAdapter implements JsonSerializer, JsonDeserializer {
@ -9,8 +8,11 @@ public class InterfaceAdapter implements JsonSerializer, JsonDeserializer {
private static final String CLASSNAME = "CLASSNAME"; private static final String CLASSNAME = "CLASSNAME";
private static final String DATA = "DATA"; private static final String DATA = "DATA";
public Object deserialize(JsonElement jsonElement, Type type, public Object deserialize(
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { JsonElement jsonElement,
Type type,
JsonDeserializationContext jsonDeserializationContext)
throws JsonParseException {
JsonObject jsonObject = jsonElement.getAsJsonObject(); JsonObject jsonObject = jsonElement.getAsJsonObject();
JsonPrimitive prim = (JsonPrimitive) jsonObject.get(CLASSNAME); JsonPrimitive prim = (JsonPrimitive) jsonObject.get(CLASSNAME);
@ -18,7 +20,9 @@ public class InterfaceAdapter implements JsonSerializer, JsonDeserializer {
Class klass = getObjectClass(className); Class klass = getObjectClass(className);
return jsonDeserializationContext.deserialize(jsonObject.get(DATA), klass); return jsonDeserializationContext.deserialize(jsonObject.get(DATA), klass);
} }
public JsonElement serialize(Object jsonElement, Type type, JsonSerializationContext jsonSerializationContext) {
public JsonElement serialize(
Object jsonElement, Type type, JsonSerializationContext jsonSerializationContext) {
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(CLASSNAME, jsonElement.getClass().getName()); jsonObject.addProperty(CLASSNAME, jsonElement.getClass().getName());
jsonObject.add(DATA, jsonSerializationContext.serialize(jsonElement)); jsonObject.add(DATA, jsonSerializationContext.serialize(jsonElement));
@ -34,4 +38,3 @@ public class InterfaceAdapter implements JsonSerializer, JsonDeserializer {
} }
} }
} }

View File

@ -1,12 +1,13 @@
package eu.dnetlib.dhp.selectioncriteria; package eu.dnetlib.dhp.selectioncriteria;
import java.io.Serializable;
@VerbClass("not_contains") @VerbClass("not_contains")
public class NotContainsVerb implements Selection { public class NotContainsVerb implements Selection, Serializable {
private String param; private String param;
public NotContainsVerb() { public NotContainsVerb() {}
}
public NotContainsVerb(final String param) { public NotContainsVerb(final String param) {
this.param = param; this.param = param;

View File

@ -1,18 +1,17 @@
package eu.dnetlib.dhp.selectioncriteria; package eu.dnetlib.dhp.selectioncriteria;
import java.io.Serializable;
@VerbClass("not_equals") @VerbClass("not_equals")
public class NotEqualVerb implements Selection { public class NotEqualVerb implements Selection, Serializable {
private String param; private String param;
public NotEqualVerb(final String param) { public NotEqualVerb(final String param) {
this.param = param; this.param = param;
} }
public NotEqualVerb() { public NotEqualVerb() {}
}
public String getParam() { public String getParam() {
return param; return param;

View File

@ -1,29 +1,57 @@
package eu.dnetlib.dhp.selectioncriteria; package eu.dnetlib.dhp.selectioncriteria;
import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfo;
import io.github.classgraph.ClassInfoList;
import io.github.classgraph.ScanResult;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.reflections.Reflections;
public class VerbResolver implements Serializable { public class VerbResolver implements Serializable {
private final Map<String, Class<Selection>> map; private Map<String, Class<Selection>> map = null; // = new HashMap<>();
private final ClassGraph classgraph = new ClassGraph();
public VerbResolver() { public VerbResolver() {
try (ScanResult scanResult = // Assign scanResult in try-with-resources
classgraph // Create a new ClassGraph instance
.verbose() // If you want to enable logging to stderr
.enableAllInfo() // Scan classes, methods, fields, annotations
.whitelistPackages(
"eu.dnetlib.dhp.selectioncriteria") // Scan com.xyz and subpackages
.scan()) { // Perform the scan and return a ScanResult
ClassInfoList routeClassInfoList =
scanResult.getClassesWithAnnotation(
"eu.dnetlib.dhp.selectioncriteria.VerbClass");
this.map = this.map =
new Reflections("eu.dnetlib") routeClassInfoList.stream()
.getTypesAnnotatedWith(VerbClass.class).stream()
.collect( .collect(
Collectors.toMap( Collectors.toMap(
v -> v.getAnnotation(VerbClass.class).value(), value ->
v -> (Class<Selection>) v)); (String)
((ClassInfo) value)
.getAnnotationInfo()
.get(0)
.getParameterValues()
.get(0)
.getValue(),
value ->
(Class<Selection>)
((ClassInfo) value).loadClass()));
} catch (Exception e) {
e.printStackTrace();
}
} }
public Selection getSelectionCriteria(String name, String param) public Selection getSelectionCriteria(String name, String param)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
InstantiationException { InstantiationException {
// return Class.forName(tmp_map.get(name)).
return map.get(name).getDeclaredConstructor((String.class)).newInstance(param); return map.get(name).getDeclaredConstructor((String.class)).newInstance(param);
} }
} }

View File

@ -6,5 +6,4 @@ public class VerbResolverFactory {
return new VerbResolver(); return new VerbResolver();
} }
} }