Revision 55912
Added by Miriam Baglioni almost 5 years ago
modules/dnet-openaireplus-mapping-utils/branches/solr75/src/main/java/eu/dnetlib/data/bulktag/SelectionCriteria.java | ||
---|---|---|
1 |
package eu.dnetlib.data.bulktag; |
|
2 |
|
|
3 |
import org.dom4j.Node; |
|
4 |
|
|
5 |
/** |
|
6 |
* Created by miriam on 02/08/2018. |
|
7 |
*/ |
|
8 |
public class SelectionCriteria { |
|
9 |
|
|
10 |
public SelectionCriteria(Node n){ |
|
11 |
|
|
12 |
} |
|
13 |
|
|
14 |
} |
modules/dnet-openaireplus-mapping-utils/branches/solr75/src/main/java/eu/dnetlib/data/bulktag/ZenodoCommunity.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.bulktag; |
2 | 2 |
|
3 |
import com.google.common.reflect.TypeToken; |
|
4 |
import com.google.gson.Gson; |
|
5 |
import org.dom4j.Node; |
|
6 |
|
|
7 |
import java.lang.reflect.Type; |
|
8 |
import java.util.Collection; |
|
9 |
import java.util.List; |
|
10 |
|
|
3 | 11 |
/** |
4 | 12 |
* Created by miriam on 01/08/2018. |
5 | 13 |
*/ |
... | ... | |
7 | 15 |
|
8 | 16 |
private String zenodoCommunityId; |
9 | 17 |
|
10 |
private SelectionCriteria selCriteria;
|
|
18 |
private SelectionConstraints selCriteria;
|
|
11 | 19 |
|
12 | 20 |
public String getZenodoCommunityId() { |
13 | 21 |
return zenodoCommunityId; |
... | ... | |
17 | 25 |
this.zenodoCommunityId = zenodoCommunityId; |
18 | 26 |
} |
19 | 27 |
|
20 |
public SelectionCriteria getSelCriteria() {
|
|
28 |
public SelectionConstraints getSelCriteria() {
|
|
21 | 29 |
return selCriteria; |
22 | 30 |
} |
23 | 31 |
|
24 |
public void setSelCriteria(SelectionCriteria selCriteria) {
|
|
32 |
public void setSelCriteria(SelectionConstraints selCriteria) {
|
|
25 | 33 |
this.selCriteria = selCriteria; |
26 | 34 |
} |
27 | 35 |
|
36 |
private void setSelCriteria(String json){ |
|
37 |
//Type collectionType = new TypeToken<Collection<Constraints>>(){}.getType(); |
|
38 |
selCriteria = new Gson().fromJson(json, SelectionConstraints.class); |
|
39 |
|
|
40 |
} |
|
41 |
|
|
42 |
public void setSelCriteria(Node n){ |
|
43 |
if (n==null){ |
|
44 |
selCriteria = null; |
|
45 |
}else{ |
|
46 |
setSelCriteria(n.getText()); |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
28 | 50 |
} |
modules/dnet-openaireplus-mapping-utils/branches/solr75/src/main/java/eu/dnetlib/data/bulktag/CommunityConfigurationFactory.java | ||
---|---|---|
3 | 3 |
import com.google.common.collect.Lists; |
4 | 4 |
import com.google.common.collect.Maps; |
5 | 5 |
import com.google.gson.Gson; |
6 |
import eu.dnetlib.data.bulktag.selectioncriteria.VerbResolver; |
|
7 |
import eu.dnetlib.data.bulktag.selectioncriteria.VerbResolverFactory; |
|
6 | 8 |
import org.apache.commons.lang3.StringUtils; |
7 | 9 |
import org.apache.commons.logging.Log; |
8 | 10 |
import org.apache.commons.logging.LogFactory; |
... | ... | |
23 | 25 |
|
24 | 26 |
private static final Log log = LogFactory.getLog(CommunityConfigurationFactory.class); |
25 | 27 |
|
28 |
private static VerbResolver resolver = VerbResolverFactory.newInstance(); |
|
29 |
|
|
26 | 30 |
public static CommunityConfiguration newInstance(final String xml) throws DocumentException { |
27 | 31 |
|
28 | 32 |
log.debug(String.format("parsing community configuration from:\n%s", xml)); |
... | ... | |
91 | 95 |
for(Node n : list){ |
92 | 96 |
Datasource d = new Datasource(); |
93 | 97 |
d.setOpenaireId(n.selectSingleNode("./openaireId").getText()); |
94 |
d.setSelCriteria(new SelectionCriteria(n.selectSingleNode("./selcriteria")));
|
|
98 |
d.setSelCriteria(n.selectSingleNode("./selcriteria"),resolver);
|
|
95 | 99 |
datasourceList.add(d); |
96 | 100 |
} |
97 | 101 |
log.info("size of the datasource list " + datasourceList.size()); |
... | ... | |
113 | 117 |
for(Node n : list){ |
114 | 118 |
ZenodoCommunity zc = new ZenodoCommunity(); |
115 | 119 |
zc.setZenodoCommunityId(n.selectSingleNode("./zenodoid").getText()); |
116 |
zc.setSelCriteria(new SelectionCriteria(n.selectSingleNode("./selcriteria")));
|
|
120 |
zc.setSelCriteria(n.selectSingleNode("./selcriteria"));
|
|
117 | 121 |
|
118 | 122 |
zenodoCommunityList.add(zc); |
119 | 123 |
} |
modules/dnet-openaireplus-mapping-utils/branches/solr75/src/main/java/eu/dnetlib/data/bulktag/Constraints.java | ||
---|---|---|
1 |
package eu.dnetlib.data.bulktag; |
|
2 |
|
|
3 |
import com.google.common.reflect.TypeToken; |
|
4 |
import com.google.gson.Gson; |
|
5 |
import eu.dnetlib.data.bulktag.selectioncriteria.VerbResolver; |
|
6 |
import org.apache.commons.logging.Log; |
|
7 |
import org.apache.commons.logging.LogFactory; |
|
8 |
|
|
9 |
|
|
10 |
import java.io.Serializable; |
|
11 |
import java.lang.reflect.InvocationTargetException; |
|
12 |
import java.lang.reflect.Type; |
|
13 |
import java.util.Collection; |
|
14 |
import java.util.List; |
|
15 |
import java.util.Map; |
|
16 |
|
|
17 |
/** |
|
18 |
* Created by miriam on 02/08/2018. |
|
19 |
*/ |
|
20 |
public class Constraints implements Serializable { |
|
21 |
private static final Log log = LogFactory.getLog(Constraints.class); |
|
22 |
private List<Constraint> sc; |
|
23 |
|
|
24 |
|
|
25 |
public Constraints() { |
|
26 |
} |
|
27 |
|
|
28 |
public List<Constraint> getSc() { |
|
29 |
return sc; |
|
30 |
} |
|
31 |
|
|
32 |
public void setSc(List<Constraint> sc) { |
|
33 |
this.sc = sc; |
|
34 |
} |
|
35 |
|
|
36 |
public void setSc(String json){ |
|
37 |
Type collectionType = new TypeToken<Collection<Constraint>>(){}.getType(); |
|
38 |
sc = new Gson().fromJson(json, collectionType); |
|
39 |
|
|
40 |
} |
|
41 |
|
|
42 |
void setSelection(VerbResolver resolver) { |
|
43 |
for(Constraint st: sc){ |
|
44 |
|
|
45 |
try { |
|
46 |
st.setSelection(resolver); |
|
47 |
} catch (NoSuchMethodException e) { |
|
48 |
log.error(e.getMessage()); |
|
49 |
} catch (IllegalAccessException e) { |
|
50 |
log.error(e.getMessage()); |
|
51 |
} catch (InvocationTargetException e) { |
|
52 |
log.error(e.getMessage()); |
|
53 |
} catch (InstantiationException e) { |
|
54 |
log.error(e.getMessage()); |
|
55 |
} |
|
56 |
} |
|
57 |
|
|
58 |
} |
|
59 |
|
|
60 |
// //Constraint in and |
|
61 |
// public boolean verifyCriteria(Map<String,String> param){ |
|
62 |
// for(Constraint sc: sc){ |
|
63 |
// if(!sc.verifyCriteria(param.get(sc.getField()))) |
|
64 |
// return false; |
|
65 |
// } |
|
66 |
// return true; |
|
67 |
// } |
|
68 |
|
|
69 |
//Constraint in and |
|
70 |
public boolean verifyCriteria(Map<String,List<String>> param){ |
|
71 |
|
|
72 |
for(Constraint sc: sc){ |
|
73 |
boolean verified = false; |
|
74 |
for(String value : param.get(sc.getField())){ |
|
75 |
if (sc.verifyCriteria(value)){ |
|
76 |
verified = true; |
|
77 |
} |
|
78 |
} |
|
79 |
if(!verified) |
|
80 |
return verified; |
|
81 |
} |
|
82 |
return true; |
|
83 |
} |
|
84 |
} |
modules/dnet-openaireplus-mapping-utils/branches/solr75/src/main/java/eu/dnetlib/data/bulktag/Datasource.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.bulktag; |
2 | 2 |
|
3 | 3 |
|
4 |
import com.google.common.reflect.TypeToken; |
|
5 |
import com.google.gson.Gson; |
|
6 |
import eu.dnetlib.data.bulktag.selectioncriteria.VerbResolver; |
|
7 |
import org.dom4j.Node; |
|
8 |
|
|
9 |
import java.lang.reflect.Type; |
|
10 |
import java.util.Collection; |
|
11 |
import java.util.List; |
|
12 |
import java.util.Map; |
|
13 |
|
|
4 | 14 |
/** |
5 | 15 |
* Created by miriam on 01/08/2018. |
6 | 16 |
*/ |
... | ... | |
8 | 18 |
|
9 | 19 |
private String openaireId; |
10 | 20 |
|
11 |
private SelectionCriteria selCriteria;
|
|
21 |
private SelectionConstraints sc;
|
|
12 | 22 |
|
23 |
|
|
24 |
public SelectionConstraints getSelCriteria() { |
|
25 |
return sc; |
|
26 |
} |
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
public void setSelCriteria(SelectionConstraints selCriteria) { |
|
31 |
this.sc = selCriteria; |
|
32 |
} |
|
33 |
|
|
13 | 34 |
public String getOpenaireId() { |
14 | 35 |
return openaireId; |
15 | 36 |
} |
... | ... | |
18 | 39 |
this.openaireId = openaireId; |
19 | 40 |
} |
20 | 41 |
|
21 |
public SelectionCriteria getSelCriteria() { |
|
22 |
return selCriteria; |
|
42 |
private void setSelCriteria(String json,VerbResolver resolver){ |
|
43 |
sc = new Gson().fromJson(json, SelectionConstraints.class); |
|
44 |
sc.setSelection(resolver); |
|
23 | 45 |
} |
24 | 46 |
|
25 |
public void setSelCriteria(SelectionCriteria selCriteria) { |
|
26 |
this.selCriteria = selCriteria; |
|
47 |
public void setSelCriteria(Node n, VerbResolver resolver){ |
|
48 |
try{ |
|
49 |
setSelCriteria(n.getText(),resolver); |
|
50 |
}catch(Exception e) { |
|
51 |
sc=null; |
|
52 |
} |
|
53 |
|
|
27 | 54 |
} |
28 | 55 |
|
56 |
|
|
57 |
|
|
29 | 58 |
} |
modules/dnet-openaireplus-mapping-utils/branches/solr75/src/main/java/eu/dnetlib/data/bulktag/CommunityConfiguration.java | ||
---|---|---|
3 | 3 |
import com.google.common.collect.Lists; |
4 | 4 |
import com.google.common.collect.Maps; |
5 | 5 |
import com.google.gson.Gson; |
6 |
|
|
7 |
import eu.dnetlib.data.bulktag.selectioncriteria.VerbResolver; |
|
6 | 8 |
import org.apache.commons.logging.Log; |
7 | 9 |
import org.apache.commons.logging.LogFactory; |
8 | 10 |
|
11 |
|
|
9 | 12 |
import java.util.ArrayList; |
10 | 13 |
import java.util.HashMap; |
11 | 14 |
import java.util.List; |
12 | 15 |
import java.util.Map; |
13 | 16 |
import java.util.stream.Collectors; |
17 |
import java.util.stream.Stream; |
|
14 | 18 |
|
15 | 19 |
/** |
16 | 20 |
* Created by miriam on 02/08/2018. |
... | ... | |
19 | 23 |
|
20 | 24 |
private static final Log log = LogFactory.getLog(CommunityConfiguration.class); |
21 | 25 |
|
26 |
|
|
22 | 27 |
enum MapModes{ |
23 | 28 |
SUBJECT_MAP, |
24 | 29 |
DATASOURCE_MAP, |
... | ... | |
27 | 32 |
|
28 | 33 |
private Map<String,Community> communities; |
29 | 34 |
|
35 |
|
|
30 | 36 |
//map subject -> communityid |
31 |
private transient Map<String,List<Pair<String,SelectionCriteria>>> subjectMap = new HashMap<>();
|
|
37 |
private transient Map<String,List<Pair<String,SelectionConstraints>>> subjectMap = new HashMap<>();
|
|
32 | 38 |
//map datasourceid -> communityid |
33 |
private transient Map<String,List<Pair<String,SelectionCriteria>>> datasourceMap = new HashMap<>();
|
|
39 |
private transient Map<String,List<Pair<String,SelectionConstraints>>> datasourceMap = new HashMap<>();
|
|
34 | 40 |
//map zenodocommunityid -> communityid |
35 |
private transient Map<String,List<Pair<String,SelectionCriteria>>> zenodocommunityMap = new HashMap<>();
|
|
41 |
private transient Map<String,List<Pair<String,SelectionConstraints>>> zenodocommunityMap = new HashMap<>();
|
|
36 | 42 |
|
37 |
public CommunityConfiguration(final Map<String, Community> communities) {
|
|
43 |
CommunityConfiguration(final Map<String, Community> communities) { |
|
38 | 44 |
this.communities = communities; |
39 | 45 |
init(); |
40 | 46 |
} |
41 | 47 |
|
42 |
public void init() {
|
|
48 |
void init() { |
|
43 | 49 |
|
44 | 50 |
if (subjectMap == null) { |
45 | 51 |
subjectMap = Maps.newHashMap(); |
... | ... | |
55 | 61 |
//get subjects |
56 | 62 |
final String id = c.getId(); |
57 | 63 |
for(String sbj : c.getSubjects()){ |
58 |
Pair<String,SelectionCriteria> p = new Pair<>(id,new SelectionCriteria(null));
|
|
64 |
Pair<String,SelectionConstraints> p = new Pair<>(id,new SelectionConstraints());
|
|
59 | 65 |
add(sbj.toLowerCase().trim() , p, subjectMap); |
60 | 66 |
} |
61 | 67 |
//get datasources |
62 | 68 |
for(Datasource d: c.getDatasources()){ |
69 |
|
|
63 | 70 |
add(d.getOpenaireId(),new Pair<>(id,d.getSelCriteria()),datasourceMap); |
64 | 71 |
} |
65 | 72 |
//get zenodo communities |
... | ... | |
70 | 77 |
} |
71 | 78 |
} |
72 | 79 |
|
73 |
private void add(String key,Pair<String,SelectionCriteria> value, Map<String,List<Pair<String,SelectionCriteria>>> map){
|
|
74 |
List<Pair<String,SelectionCriteria>> values = map.get(key);
|
|
80 |
private void add(String key, Pair<String,SelectionConstraints> value, Map<String,List<Pair<String,SelectionConstraints>>> map){
|
|
81 |
List<Pair<String,SelectionConstraints>> values = map.get(key);
|
|
75 | 82 |
|
76 | 83 |
if (values == null){ |
77 | 84 |
values = new ArrayList<>(); |
... | ... | |
80 | 87 |
values.add(value); |
81 | 88 |
} |
82 | 89 |
|
83 |
public List<Pair<String,SelectionCriteria>> getCommunityForSubject(String sbj){
|
|
90 |
public List<Pair<String,SelectionConstraints>> getCommunityForSubject(String sbj){
|
|
84 | 91 |
return subjectMap.get(sbj); |
85 | 92 |
} |
86 | 93 |
|
87 |
public List<Pair<String,SelectionCriteria>> getCommunityForDatasource(String dts){
|
|
94 |
public List<Pair<String,SelectionConstraints>> getCommunityForDatasource(String dts){
|
|
88 | 95 |
return datasourceMap.get(dts); |
89 | 96 |
} |
90 | 97 |
|
91 |
public List<Pair<String,SelectionCriteria>> getCommunityForZenodoCommunity(String zc){ |
|
98 |
// public List<String> getCommunityForDatasource(String dts, Map<String,String>param){ |
|
99 |
// List<Pair<String,SelectionConstraints>> lp = datasourceMap.get(dts); |
|
100 |
// if (lp==null) |
|
101 |
// return Lists.newArrayList(); |
|
102 |
// return lp.stream().map(p -> { |
|
103 |
// if (p.getSnd() == null) |
|
104 |
// return p.getFst(); |
|
105 |
// if (((SelectionConstraints) p.getSnd()).verifyCriteria(param)) |
|
106 |
// return p.getFst(); |
|
107 |
// else |
|
108 |
// return null; |
|
109 |
// }).collect(Collectors.toList()); |
|
110 |
// |
|
111 |
// } |
|
112 |
|
|
113 |
public List<String> getCommunityForDatasource(String dts, Map<String,List<String>>param){ |
|
114 |
List<Pair<String,SelectionConstraints>> lp = datasourceMap.get(dts); |
|
115 |
if (lp==null) |
|
116 |
return Lists.newArrayList(); |
|
117 |
return lp.stream().map(p -> { |
|
118 |
if (p.getSnd() == null) |
|
119 |
return p.getFst(); |
|
120 |
if (((SelectionConstraints) p.getSnd()).verifyCriteria(param)) |
|
121 |
return p.getFst(); |
|
122 |
else |
|
123 |
return null; |
|
124 |
}).collect(Collectors.toList()); |
|
125 |
|
|
126 |
} |
|
127 |
|
|
128 |
public List<Pair<String,SelectionConstraints>> getCommunityForZenodoCommunity(String zc){ |
|
92 | 129 |
return zenodocommunityMap.get(zc); |
93 | 130 |
} |
94 | 131 |
|
... | ... | |
107 | 144 |
return getContextIds(zenodocommunityMap.get(value.toLowerCase())); |
108 | 145 |
} |
109 | 146 |
|
110 |
private List<String> getContextIds(List<Pair<String, SelectionCriteria>> list) {
|
|
147 |
private List<String> getContextIds(List<Pair<String, SelectionConstraints>> list) {
|
|
111 | 148 |
if (list != null) { |
112 | 149 |
return list.stream().map(p -> p.getFst()).collect(Collectors.toList()); |
113 | 150 |
} |
114 | 151 |
return Lists.newArrayList(); |
115 | 152 |
} |
116 | 153 |
|
154 |
|
|
117 | 155 |
/* |
118 |
public SelectionCriteria getSelCriteria(String value, String community, MapModes map_mode){
|
|
156 |
public Constraints getSelCriteria(String value, String community, MapModes map_mode){
|
|
119 | 157 |
|
120 |
Map<String,List<Pair<String,SelectionCriteria>>> map = null;
|
|
158 |
Map<String,List<Pair<String,Constraints>>> map = null;
|
|
121 | 159 |
if(map_mode == MapModes.DATASOURCE_MAP) |
122 | 160 |
map = datasourceMap; |
123 | 161 |
else |
... | ... | |
126 | 164 |
else |
127 | 165 |
new Throwable("Impossible to have Selection Criteria over subjects"); |
128 | 166 |
|
129 |
List<Pair<String, SelectionCriteria>> lst = map.get(value);
|
|
130 |
List<SelectionCriteria> selectionList = lst.stream().map(p -> {
|
|
167 |
List<Pair<String, Constraints>> lst = map.get(value);
|
|
168 |
List<Constraints> selectionList = lst.stream().map(p -> {
|
|
131 | 169 |
if (p.fst == community) |
132 | 170 |
return p.snd; |
133 | 171 |
return null; |
Also available in: Unified diff
Update for the addition of selection criteria in the community context configuration