1
|
package eu.dnetlib.oai.sets;
|
2
|
|
3
|
import java.text.Normalizer;
|
4
|
import java.util.List;
|
5
|
|
6
|
import com.google.common.base.Function;
|
7
|
import com.google.common.collect.Iterables;
|
8
|
import com.google.common.collect.Lists;
|
9
|
import com.mongodb.BasicDBObject;
|
10
|
import com.mongodb.BasicDBObjectBuilder;
|
11
|
import com.mongodb.DBObject;
|
12
|
import com.mongodb.MongoClient;
|
13
|
import com.mongodb.client.FindIterable;
|
14
|
import com.mongodb.client.MongoCollection;
|
15
|
import com.mongodb.client.model.Filters;
|
16
|
import com.mongodb.client.model.FindOneAndReplaceOptions;
|
17
|
import com.mongodb.client.model.FindOneAndUpdateOptions;
|
18
|
import com.mongodb.client.model.IndexOptions;
|
19
|
import eu.dnetlib.data.information.oai.publisher.info.SetInfo;
|
20
|
import org.apache.commons.lang.StringEscapeUtils;
|
21
|
import org.apache.commons.lang.StringUtils;
|
22
|
import org.bson.conversions.Bson;
|
23
|
import org.springframework.beans.factory.annotation.Autowired;
|
24
|
|
25
|
public class MongoSetCollection implements SetCollection {
|
26
|
|
27
|
public static String DEFAULT_SET = "OTHER";
|
28
|
|
29
|
@Autowired
|
30
|
private MongoClient publisherMongoClient;
|
31
|
private String setCollection = "sets";
|
32
|
private String setCountCollection = "setsCount";
|
33
|
|
34
|
public void ensureIndexes(final String dbName) {
|
35
|
this.ensureIndexesOnSets(dbName);
|
36
|
this.ensureIndexesOnCount(dbName);
|
37
|
}
|
38
|
|
39
|
@Override
|
40
|
public List<SetInfo> getAllSets(final boolean enabledOnly, final String dbName) {
|
41
|
FindIterable<DBObject> iter = null;
|
42
|
if (!enabledOnly) {
|
43
|
iter = this.getSetsCollection(dbName).find();
|
44
|
} else {
|
45
|
Bson where = Filters.eq("enabled", true);
|
46
|
iter = this.getSetsCollection(dbName).find(where);
|
47
|
}
|
48
|
return Lists.newArrayList(Iterables.transform(iter, new Function<DBObject, SetInfo>() {
|
49
|
|
50
|
@Override
|
51
|
public SetInfo apply(final DBObject dbObject) {
|
52
|
return getSetFromDBObject(dbObject);
|
53
|
}
|
54
|
}));
|
55
|
}
|
56
|
|
57
|
@Override
|
58
|
public boolean containSet(final String set, final String dbName) {
|
59
|
Bson query = Filters.eq("spec", set);
|
60
|
return this.getSetsCollection(dbName).count(query) != 0;
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
public boolean containEnabledSet(final String set, final String publisherDBName) {
|
65
|
Bson query = Filters.and(Filters.eq("spec", set), Filters.eq("enabled", true));
|
66
|
return this.getSetsCollection(publisherDBName).count(query) != 0;
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
public String getSetQuery(final String set, final String dbName) {
|
71
|
Bson query = Filters.eq("spec", set);
|
72
|
BasicDBObject returnField = new BasicDBObject("query", 1);
|
73
|
DBObject obj = this.getSetsCollection(dbName).find(query).projection(returnField).first();
|
74
|
return (String) obj.get("query");
|
75
|
}
|
76
|
|
77
|
@Override
|
78
|
public int count(final String setSpec, final String mdPrefix, final String dbName) {
|
79
|
Bson query = Filters.and(Filters.eq("spec", setSpec), Filters.eq("mdPrefix", mdPrefix));
|
80
|
BasicDBObject returnField = new BasicDBObject("count", 1);
|
81
|
DBObject obj = this.getSetsCountCollection(dbName).find(query).projection(returnField).first();
|
82
|
if (obj == null) return 0;
|
83
|
return (Integer) obj.get("count");
|
84
|
}
|
85
|
|
86
|
public void updateCounts(final String setSpec, final String mdPrefix, final int count, final String dbName) {
|
87
|
BasicDBObject countUpdate = new BasicDBObject("$set", new BasicDBObject("count", count));
|
88
|
Bson query = Filters.and(Filters.eq("spec", setSpec), Filters.eq("mdPrefix", mdPrefix));
|
89
|
//DBObject setCount = BasicDBObjectBuilder.start("spec", setSpec).add("mdPrefix", mdPrefix).get();
|
90
|
//this.getSetsCountCollection(dbName).update(setCount, new BasicDBObject("$set", new BasicDBObject("count", count)), true, false);
|
91
|
this.getSetsCountCollection(dbName).findOneAndUpdate(query, countUpdate, new FindOneAndUpdateOptions().upsert(true));
|
92
|
}
|
93
|
|
94
|
public void upsertSet(final SetInfo setInfo, final boolean fromConfiguration, final String dbName) {
|
95
|
DBObject obj = this.getObjectFromSet(setInfo);
|
96
|
obj.put("fromConfiguration", fromConfiguration);
|
97
|
//this.getSetsCollection(dbName).update(new BasicDBObject("spec", setInfo.getSetSpec()), obj, true, false);
|
98
|
this.getSetsCollection(dbName).findOneAndReplace(Filters.eq("spec", setInfo.getSetSpec()), obj, new FindOneAndReplaceOptions().upsert(true));
|
99
|
}
|
100
|
|
101
|
public String normalizeSetSpec(final String setName) {
|
102
|
String s = StringEscapeUtils.unescapeXml(setName);
|
103
|
s = Normalizer.normalize(s, Normalizer.Form.NFD);
|
104
|
// replace spaces with underscores
|
105
|
s = s.replaceAll(" ", "_");
|
106
|
// remove tilde, dots... over letters
|
107
|
s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}&&[^-_]]", "");
|
108
|
// change punctuation into an underscore
|
109
|
s = s.replaceAll("[\\p{Punct}&&[^-_]]", "_");
|
110
|
// remove all non-word characters
|
111
|
s = s.replaceAll("[\\W&&[^-_]]", "");
|
112
|
// Avoiding set '___' generated when we have "strange" set names such as those in cyrillic/ukrain
|
113
|
// strips _ from the beginning and the end
|
114
|
String stripped = StringUtils.strip(s, "_ ");
|
115
|
if (StringUtils.isBlank(stripped)) {
|
116
|
stripped = DEFAULT_SET;
|
117
|
}
|
118
|
return stripped;
|
119
|
}
|
120
|
|
121
|
public List<SetInfo> getConfiguredSets(final String dbName) {
|
122
|
Bson query = Filters.eq("fromConfiguration", true);
|
123
|
return this.findSets(query, dbName);
|
124
|
}
|
125
|
|
126
|
public List<SetInfo> getSetsFromData(final String dbName) {
|
127
|
Bson query = Filters.eq("fromConfiguration", false);
|
128
|
return this.findSets(query, dbName);
|
129
|
}
|
130
|
|
131
|
public void dropOAISets(final String dbName) {
|
132
|
this.getSetsCountCollection(dbName).drop();
|
133
|
this.getSetsCollection(dbName).drop();
|
134
|
}
|
135
|
|
136
|
public void dropSet(final String dbName, final String setSpec) {
|
137
|
Bson query = Filters.eq("spec", setSpec);
|
138
|
this.getSetsCollection(dbName).deleteMany(query);
|
139
|
this.getSetsCountCollection(dbName).deleteMany(query);
|
140
|
}
|
141
|
|
142
|
public void dropConfigurationSets(final String dbName) {
|
143
|
this.getSetsCollection(dbName).deleteMany(Filters.eq("fromConfiguration", true));
|
144
|
}
|
145
|
|
146
|
protected List<SetInfo> findSets(final Bson query, final String dbName) {
|
147
|
final FindIterable<DBObject> sets = this.getSetsCollection(dbName).find(query);
|
148
|
List<SetInfo> res = Lists.newArrayList();
|
149
|
for (DBObject obj : sets) {
|
150
|
res.add(this.getSetFromDBObject(obj));
|
151
|
}
|
152
|
return res;
|
153
|
}
|
154
|
|
155
|
private SetInfo getSetFromDBObject(final DBObject obj) {
|
156
|
SetInfo setInfo = new SetInfo();
|
157
|
setInfo.setEnabled((Boolean) obj.get("enabled"));
|
158
|
setInfo.setQuery((String) obj.get("query"));
|
159
|
setInfo.setSetDescription((String) obj.get("description"));
|
160
|
setInfo.setSetName((String) obj.get("name"));
|
161
|
setInfo.setSetSpec((String) obj.get("spec"));
|
162
|
return setInfo;
|
163
|
}
|
164
|
|
165
|
private DBObject getObjectFromSet(final SetInfo s) {
|
166
|
DBObject obj = BasicDBObjectBuilder.start("spec", s.getSetSpec()).add("name", s.getSetName()).add("description", s.getSetDescription())
|
167
|
.add("query", s.getQuery()).add("enabled", s.isEnabled()).get();
|
168
|
return obj;
|
169
|
}
|
170
|
|
171
|
private void ensureIndexesOnSets(final String dbName) {
|
172
|
this.getSetsCollection(dbName).createIndex(new BasicDBObject("spec", 1), new IndexOptions().background(true));
|
173
|
this.getSetsCollection(dbName).createIndex(new BasicDBObject("fromConfiguration", 1), new IndexOptions().background(true));
|
174
|
}
|
175
|
|
176
|
private void ensureIndexesOnCount(final String dbName) {
|
177
|
BasicDBObject index = (BasicDBObject) BasicDBObjectBuilder.start("spec", 1).add("mdPrefix", 1).get();
|
178
|
this.getSetsCountCollection(dbName).createIndex(index, new IndexOptions().background(true));
|
179
|
}
|
180
|
|
181
|
public MongoCollection<DBObject> getSetsCollection(final String dbName) {
|
182
|
return this.getCollection(this.setCollection, dbName);
|
183
|
}
|
184
|
|
185
|
public MongoCollection<DBObject> getSetsCountCollection(final String dbName) {
|
186
|
return this.getCollection(this.setCountCollection, dbName);
|
187
|
}
|
188
|
|
189
|
private MongoCollection<DBObject> getCollection(final String collectionName, final String dbName) {
|
190
|
return publisherMongoClient.getDatabase(dbName).getCollection(collectionName, DBObject.class);
|
191
|
}
|
192
|
|
193
|
public String getSetCollection() {
|
194
|
return setCollection;
|
195
|
}
|
196
|
|
197
|
public void setSetCollection(final String setCollection) {
|
198
|
this.setCollection = setCollection;
|
199
|
}
|
200
|
|
201
|
public String getSetCountCollection() {
|
202
|
return setCountCollection;
|
203
|
}
|
204
|
|
205
|
public void setSetCountCollection(final String setCountCollection) {
|
206
|
this.setCountCollection = setCountCollection;
|
207
|
}
|
208
|
|
209
|
public MongoClient getPublisherMongoClient() {
|
210
|
return publisherMongoClient;
|
211
|
}
|
212
|
|
213
|
public void setPublisherMongoClient(final MongoClient publisherMongoClient) {
|
214
|
this.publisherMongoClient = publisherMongoClient;
|
215
|
}
|
216
|
|
217
|
}
|