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.gson.Gson;
import com.google.gson.GsonBuilder;
import eu.dnetlib.dhp.selectioncriteria.InterfaceAdapter;
import eu.dnetlib.dhp.selectioncriteria.Selection;
import eu.dnetlib.dhp.selectioncriteria.VerbResolver;
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.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -17,15 +20,7 @@ import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by miriam on 03/08/2018.
*/
/** Created by miriam on 03/08/2018. */
public class CommunityConfigurationFactory {
private static final Log log = LogFactory.getLog(CommunityConfigurationFactory.class);
@ -38,9 +33,9 @@ public class CommunityConfigurationFactory {
final Document doc = new SAXReader().read(new StringReader(xml));
final Map<String,Community> communities = Maps.newHashMap();
final Map<String, Community> communities = Maps.newHashMap();
for(final Object o : doc.selectNodes("//community")) {
for (final Object o : doc.selectNodes("//community")) {
final Node node = (Node) o;
@ -54,8 +49,7 @@ public class CommunityConfigurationFactory {
log.info(String.format("loaded %s community configuration profiles", communities.size()));
log.debug(String.format("loaded community configuration:\n%s", communities.toString()));
return new CommunityConfiguration(communities);
return new CommunityConfiguration(communities);
}
public static CommunityConfiguration fromJson(final String json) {
@ -88,9 +82,9 @@ public class CommunityConfigurationFactory {
final List<String> subjects = Lists.newArrayList();
final List <Node> list = node.selectNodes("./subjects/subject");
final List<Node> list = node.selectNodes("./subjects/subject");
for(Node n : list){
for (Node n : list) {
log.debug("text of the node " + n.getText());
subjects.add(StringUtils.trim(n.getText()));
}
@ -98,11 +92,10 @@ public class CommunityConfigurationFactory {
return subjects;
}
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<>();
for(Node n : list){
for (Node n : list) {
Datasource d = new Datasource();
d.setOpenaireId(n.selectSingleNode("./openaireId").getText());
d.setSelCriteria(n.selectSingleNode("./selcriteria"), resolver);
@ -115,23 +108,21 @@ public class CommunityConfigurationFactory {
private static List<ZenodoCommunity> parseZenodoCommunities(final Node node) {
final Node oacommunitynode = node.selectSingleNode("./oacommunity");
String oacommunity = null;
if (oacommunitynode != null){
if (oacommunitynode != null) {
String tmp = oacommunitynode.getText();
if(StringUtils.isNotBlank(tmp))
oacommunity = tmp;
if (StringUtils.isNotBlank(tmp)) oacommunity = tmp;
}
final List<Node> list = node.selectNodes("./zenodocommunities/zenodocommunity");
final List<ZenodoCommunity> zenodoCommunityList = new ArrayList<>();
for(Node n : list){
for (Node n : list) {
ZenodoCommunity zc = new ZenodoCommunity();
zc.setZenodoCommunityId(n.selectSingleNode("./zenodoid").getText());
zc.setSelCriteria(n.selectSingleNode("./selcriteria"));
zenodoCommunityList.add(zc);
}
if(oacommunity != null){
if (oacommunity != null) {
ZenodoCommunity zc = new ZenodoCommunity();
zc.setZenodoCommunityId(oacommunity);
zenodoCommunityList.add(zc);
@ -139,8 +130,4 @@ public class CommunityConfigurationFactory {
log.info("size of the zenodo community list " + zenodoCommunityList.size());
return zenodoCommunityList;
}
}
}

View File

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

View File

@ -1,32 +1,25 @@
package eu.dnetlib.dhp.community;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import eu.dnetlib.dhp.selectioncriteria.VerbResolver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
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 {
private static final Log log = LogFactory.getLog(Constraints.class);
//private ConstraintEncapsulator ce;
// private ConstraintEncapsulator ce;
private List<Constraint> constraint;
public Constraints() {}
public Constraints() {
}
public List<Constraint> getConstraint() {
return constraint;
}
@ -35,14 +28,13 @@ public class Constraints implements Serializable {
this.constraint = constraint;
}
public void setSc(String json){
Type collectionType = new TypeToken<Collection<Constraint>>(){}.getType();
public void setSc(String json) {
Type collectionType = new TypeToken<Collection<Constraint>>() {}.getType();
constraint = new Gson().fromJson(json, collectionType);
}
void setSelection(VerbResolver resolver) {
for(Constraint st: constraint){
for (Constraint st : constraint) {
try {
st.setSelection(resolver);
@ -56,24 +48,20 @@ public class Constraints implements Serializable {
log.error(e.getMessage());
}
}
}
//Constraint in and
// Constraint in and
public boolean verifyCriteria(final Map<String, List<String>> param) {
for(Constraint sc : constraint) {
for (Constraint sc : constraint) {
boolean verified = false;
for(String value : param.get(sc.getField())){
if (sc.verifyCriteria(value.trim())){
for (String value : param.get(sc.getField())) {
if (sc.verifyCriteria(value.trim())) {
verified = true;
}
}
if(!verified)
return verified;
if (!verified) return verified;
}
return true;
}
}
}

View File

@ -4,63 +4,59 @@ import com.google.common.base.Joiner;
import eu.dnetlib.dhp.utils.ISLookupClientFactory;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
import java.util.List;
import org.dom4j.DocumentException;
import java.util.List;
public class QueryInformationSystem {
private static final String XQUERY = "for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType') " +
" let $subj := $x//CONFIGURATION/context/param[./@name='subject']/text() " +
" let $datasources := $x//CONFIGURATION/context/category[./@id=concat($x//CONFIGURATION/context/@id,'::contentproviders')]/concept " +
" let $organizations := $x//CONFIGURATION/context/category[./@id=concat($x//CONFIGURATION/context/@id,'::resultorganizations')]/concept " +
" let $communities := $x//CONFIGURATION/context/category[./@id=concat($x//CONFIGURATION/context/@id,'::zenodocommunities')]/concept " +
" where $x//CONFIGURATION/context[./@type='community' or ./@type='ri'] " +
" return " +
" <community> " +
" { $x//CONFIGURATION/context/@id} " +
" <subjects> " +
" {for $y in tokenize($subj,',') " +
" return " +
" <subject>{$y}</subject>} " +
" </subjects> " +
" <datasources> " +
" {for $d in $datasources " +
" where $d/param[./@name='enabled']/text()='true' " +
" return " +
" <datasource> " +
" <openaireId> " +
" {$d//param[./@name='openaireId']/text()} " +
" </openaireId> " +
" <selcriteria> " +
" {$d/param[./@name='selcriteria']/text()} " +
" </selcriteria> " +
" </datasource> } " +
" </datasources> " +
" <zenodocommunities> " +
" {for $zc in $communities " +
" return " +
" <zenodocommunity> " +
" <zenodoid> " +
" {$zc/param[./@name='zenodoid']/text()} " +
" </zenodoid> " +
" <selcriteria> " +
" {$zc/param[./@name='selcriteria']/text()} " +
" </selcriteria> " +
" </zenodocommunity>} " +
" </zenodocommunities> " +
" </community>";
private static final String XQUERY =
"for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType') "
+ " let $subj := $x//CONFIGURATION/context/param[./@name='subject']/text() "
+ " let $datasources := $x//CONFIGURATION/context/category[./@id=concat($x//CONFIGURATION/context/@id,'::contentproviders')]/concept "
+ " let $organizations := $x//CONFIGURATION/context/category[./@id=concat($x//CONFIGURATION/context/@id,'::resultorganizations')]/concept "
+ " let $communities := $x//CONFIGURATION/context/category[./@id=concat($x//CONFIGURATION/context/@id,'::zenodocommunities')]/concept "
+ " where $x//CONFIGURATION/context[./@type='community' or ./@type='ri'] "
+ " return "
+ " <community> "
+ " { $x//CONFIGURATION/context/@id} "
+ " <subjects> "
+ " {for $y in tokenize($subj,',') "
+ " return "
+ " <subject>{$y}</subject>} "
+ " </subjects> "
+ " <datasources> "
+ " {for $d in $datasources "
+ " where $d/param[./@name='enabled']/text()='true' "
+ " return "
+ " <datasource> "
+ " <openaireId> "
+ " {$d//param[./@name='openaireId']/text()} "
+ " </openaireId> "
+ " <selcriteria> "
+ " {$d/param[./@name='selcriteria']/text()} "
+ " </selcriteria> "
+ " </datasource> } "
+ " </datasources> "
+ " <zenodocommunities> "
+ " {for $zc in $communities "
+ " return "
+ " <zenodocommunity> "
+ " <zenodoid> "
+ " {$zc/param[./@name='zenodoid']/text()} "
+ " </zenodoid> "
+ " <selcriteria> "
+ " {$zc/param[./@name='selcriteria']/text()} "
+ " </selcriteria> "
+ " </zenodocommunity>} "
+ " </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);
final List<String> res = isLookUp.quickSearchProfile(XQUERY);
final String xmlConf = "<communities>" + Joiner.on(" ").join(res) + "</communities>";
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
// from all the zenodo communities
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
@ -118,7 +119,15 @@ public class ResultTagger implements Serializable {
.map(
c -> {
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()))
dataInfoList.add(
getDataInfo(
@ -153,7 +162,7 @@ public class ResultTagger implements Serializable {
c -> {
Context context = new Context();
context.setId(c);
List<DataInfo> dataInfoList = Arrays.asList();
List<DataInfo> dataInfoList = new ArrayList<>();
if (subjects.contains(c))
dataInfoList.add(
getDataInfo(

View File

@ -3,8 +3,6 @@ package eu.dnetlib.dhp.community;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import eu.dnetlib.dhp.selectioncriteria.VerbResolver;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.Collection;
@ -14,9 +12,7 @@ import java.util.Map;
public class SelectionConstraints implements Serializable {
private List<Constraints> criteria;
public SelectionConstraints() {
}
public SelectionConstraints() {}
public List<Constraints> getCriteria() {
return criteria;
@ -26,24 +22,25 @@ public class SelectionConstraints implements Serializable {
this.criteria = criteria;
}
public void setSc(String json){
Type collectionType = new TypeToken<Collection<Constraints>>(){}.getType();
public void setSc(String json) {
Type collectionType = new TypeToken<Collection<Constraints>>() {}.getType();
criteria = new Gson().fromJson(json, collectionType);
}
//Constraints in or
public boolean verifyCriteria(final Map<String, List<String>> param){
for(Constraints selc : criteria) {
if(selc.verifyCriteria(param)){
// Constraints in or
public boolean verifyCriteria(final Map<String, List<String>> param) {
for (Constraints selc : criteria) {
if (selc.verifyCriteria(param)) {
return true;
}
}
return false;
}
public void setSelection(VerbResolver resolver) {
for(Constraints cs : criteria){
for (Constraints cs : criteria) {
cs.setSelection(resolver);
}
}
}
}

View File

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

View File

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

View File

@ -1,7 +1,6 @@
package eu.dnetlib.dhp.selectioncriteria;
import com.google.gson.*;
import java.lang.reflect.Type;
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 DATA = "DATA";
public Object deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
public Object deserialize(
JsonElement jsonElement,
Type type,
JsonDeserializationContext jsonDeserializationContext)
throws JsonParseException {
JsonObject jsonObject = jsonElement.getAsJsonObject();
JsonPrimitive prim = (JsonPrimitive) jsonObject.get(CLASSNAME);
@ -18,20 +20,21 @@ public class InterfaceAdapter implements JsonSerializer, JsonDeserializer {
Class klass = getObjectClass(className);
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.addProperty(CLASSNAME, jsonElement.getClass().getName());
jsonObject.add(DATA, jsonSerializationContext.serialize(jsonElement));
return jsonObject;
}
/****** Helper method to get the className of the object to be deserialized *****/
/** **** Helper method to get the className of the object to be deserialized **** */
public Class getObjectClass(String className) {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
//e.printStackTrace();
// e.printStackTrace();
throw new JsonParseException(e.getMessage());
}
}
}

View File

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

View File

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

View File

@ -3,4 +3,4 @@ package eu.dnetlib.dhp.selectioncriteria;
public interface Selection {
boolean apply(String value);
}
}

View File

@ -1,29 +1,57 @@
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.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.stream.Collectors;
import org.reflections.Reflections;
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() {
this.map =
new Reflections("eu.dnetlib")
.getTypesAnnotatedWith(VerbClass.class).stream()
.collect(
Collectors.toMap(
v -> v.getAnnotation(VerbClass.class).value(),
v -> (Class<Selection>) v));
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 =
routeClassInfoList.stream()
.collect(
Collectors.toMap(
value ->
(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)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
InstantiationException {
// return Class.forName(tmp_map.get(name)).
return map.get(name).getDeclaredConstructor((String.class)).newInstance(param);
}
}

View File

@ -2,9 +2,8 @@ package eu.dnetlib.dhp.selectioncriteria;
public class VerbResolverFactory {
public static VerbResolver newInstance(){
public static VerbResolver newInstance() {
return new VerbResolver();
}
}
}