Project

General

Profile

1
package eu.dnetlib.data.oai.store.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 eu.dnetlib.data.information.oai.sets.SetCollection;
21
import org.apache.commons.lang3.StringEscapeUtils;
22
import org.apache.commons.lang3.StringUtils;
23
import org.bson.conversions.Bson;
24
import org.springframework.beans.factory.annotation.Autowired;
25

    
26
public class MongoSetCollection implements SetCollection {
27

    
28
	public static String DEFAULT_SET = "OTHER";
29

    
30
	@Autowired
31
	private MongoClient publisherMongoClient;
32
	private String setCollection = "sets";
33
	private String setCountCollection = "setsCount";
34

    
35
	public void ensureIndexes(final String dbName) {
36
		this.ensureIndexesOnSets(dbName);
37
		this.ensureIndexesOnCount(dbName);
38
	}
39

    
40
	@Override
41
	public List<SetInfo> getAllSets(final boolean enabledOnly, final String dbName) {
42
		FindIterable<DBObject> iter = null;
43
		if (!enabledOnly) {
44
			iter = this.getSetsCollection(dbName).find();
45
		} else {
46
			Bson where = Filters.eq("enabled", true);
47
			iter = this.getSetsCollection(dbName).find(where);
48
		}
49
		return Lists.newArrayList(Iterables.transform(iter, new Function<DBObject, SetInfo>() {
50

    
51
			@Override
52
			public SetInfo apply(final DBObject dbObject) {
53
				return getSetFromDBObject(dbObject);
54
			}
55
		}));
56
	}
57

    
58
	@Override
59
	public boolean containSet(final String set, final String dbName) {
60
		Bson query = Filters.eq("spec", set);
61
		return this.getSetsCollection(dbName).count(query) != 0;
62
	}
63

    
64
	@Override
65
	public boolean containEnabledSet(final String set, final String publisherDBName) {
66
		Bson query = Filters.and(Filters.eq("spec", set), Filters.eq("enabled", true));
67
		return this.getSetsCollection(publisherDBName).count(query) != 0;
68
	}
69

    
70
	@Override
71
	public String getSetQuery(final String set, final String dbName) {
72
		Bson query = Filters.eq("spec", set);
73
		BasicDBObject returnField = new BasicDBObject("query", 1);
74
		DBObject obj = this.getSetsCollection(dbName).find(query).projection(returnField).first();
75
		return (String) obj.get("query");
76
	}
77

    
78
	@Override
79
	public int count(final String setSpec, final String mdPrefix, final String dbName) {
80
		Bson query = Filters.and(Filters.eq("spec", setSpec), Filters.eq("mdPrefix", mdPrefix));
81
		BasicDBObject returnField = new BasicDBObject("count", 1);
82
		DBObject obj = this.getSetsCountCollection(dbName).find(query).projection(returnField).first();
83
		if (obj == null) return 0;
84
		return (Integer) obj.get("count");
85
	}
86

    
87
	public void updateCounts(final String setSpec, final String mdPrefix, final int count, final String dbName) {
88
		BasicDBObject countUpdate = new BasicDBObject("$set", new BasicDBObject("count", count));
89
		Bson query = Filters.and(Filters.eq("spec", setSpec), Filters.eq("mdPrefix", mdPrefix));
90
		this.getSetsCountCollection(dbName).findOneAndUpdate(query, countUpdate, new FindOneAndUpdateOptions().upsert(true));
91
	}
92

    
93
	public void upsertSet(final SetInfo setInfo, final boolean fromConfiguration, final String dbName) {
94
		DBObject obj = this.getObjectFromSet(setInfo);
95
		obj.put("fromConfiguration", fromConfiguration);
96
		//this.getSetsCollection(dbName).update(new BasicDBObject("spec", setInfo.getSetSpec()), obj, true, false);
97
		this.getSetsCollection(dbName).findOneAndReplace(Filters.eq("spec", setInfo.getSetSpec()), obj, new FindOneAndReplaceOptions().upsert(true));
98
	}
99

    
100
	public String normalizeSetSpec(final String setName) {
101
		String s = StringEscapeUtils.unescapeXml(setName);
102
		s = Normalizer.normalize(s, Normalizer.Form.NFD);
103
		// replace spaces with underscores
104
		s = s.replaceAll(" ", "_");
105
		// remove tilde, dots... over letters
106
		s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}&&[^-_]]", "");
107
		// change punctuation into an underscore
108
		s = s.replaceAll("[\\p{Punct}&&[^-_]]", "_");
109
		// remove all non-word characters
110
		s = s.replaceAll("[\\W&&[^-_]]", "");
111
		// Avoiding set '___' generated when we have "strange" set names such as those in cyrillic/ukrain
112
		// strips _ from the beginning and the end
113
		String stripped = StringUtils.strip(s, "_ ");
114
		if (StringUtils.isBlank(stripped)) {
115
			stripped = DEFAULT_SET;
116
		}
117
		return stripped;
118
	}
119

    
120
	public List<SetInfo> getConfiguredSets(final String dbName) {
121
		Bson query = Filters.eq("fromConfiguration", true);
122
		return this.findSets(query, dbName);
123
	}
124

    
125
	public List<SetInfo> getSetsFromData(final String dbName) {
126
		Bson query = Filters.eq("fromConfiguration", false);
127
		return this.findSets(query, dbName);
128
	}
129

    
130
	public void dropOAISets(final String dbName) {
131
		this.getSetsCountCollection(dbName).drop();
132
		this.getSetsCollection(dbName).drop();
133
	}
134

    
135
	public void dropSet(final String dbName, final String setSpec) {
136
		Bson query = Filters.eq("spec", setSpec);
137
		this.getSetsCollection(dbName).deleteMany(query);
138
		this.getSetsCountCollection(dbName).deleteMany(query);
139
	}
140

    
141
	public void dropConfigurationSets(final String dbName) {
142
		this.getSetsCollection(dbName).deleteMany(Filters.eq("fromConfiguration", true));
143
	}
144

    
145
	protected List<SetInfo> findSets(final Bson query, final String dbName) {
146
		final FindIterable<DBObject> sets = this.getSetsCollection(dbName).find(query);
147
		List<SetInfo> res = Lists.newArrayList();
148
		for (DBObject obj : sets) {
149
			res.add(this.getSetFromDBObject(obj));
150
		}
151
		return res;
152
	}
153

    
154
	private SetInfo getSetFromDBObject(final DBObject obj) {
155
		SetInfo setInfo = new SetInfo();
156
		setInfo.setEnabled((Boolean) obj.get("enabled"));
157
		setInfo.setQuery((String) obj.get("query"));
158
		setInfo.setSetDescription((String) obj.get("description"));
159
		setInfo.setSetName((String) obj.get("name"));
160
		setInfo.setSetSpec((String) obj.get("spec"));
161
		return setInfo;
162
	}
163

    
164
	private DBObject getObjectFromSet(final SetInfo s) {
165
		DBObject obj = BasicDBObjectBuilder.start("spec", s.getSetSpec()).add("name", s.getSetName()).add("description", s.getSetDescription())
166
				.add("query", s.getQuery()).add("enabled", s.isEnabled()).get();
167
		return obj;
168
	}
169

    
170
	private void ensureIndexesOnSets(final String dbName) {
171
		this.getSetsCollection(dbName).createIndex(new BasicDBObject("spec", 1), new IndexOptions().background(true));
172
		this.getSetsCollection(dbName).createIndex(new BasicDBObject("fromConfiguration", 1), new IndexOptions().background(true));
173
	}
174

    
175
	private void ensureIndexesOnCount(final String dbName) {
176
		BasicDBObject index = (BasicDBObject) BasicDBObjectBuilder.start("spec", 1).add("mdPrefix", 1).get();
177
		this.getSetsCountCollection(dbName).createIndex(index, new IndexOptions().background(true));
178
	}
179

    
180
	public MongoCollection<DBObject> getSetsCollection(final String dbName) {
181
		return this.getCollection(this.setCollection, dbName);
182
	}
183

    
184
	public MongoCollection<DBObject> getSetsCountCollection(final String dbName) {
185
		return this.getCollection(this.setCountCollection, dbName);
186
	}
187

    
188
	private MongoCollection<DBObject> getCollection(final String collectionName, final String dbName) {
189
		return publisherMongoClient.getDatabase(dbName).getCollection(collectionName, DBObject.class);
190
	}
191

    
192
	public String getSetCollection() {
193
		return setCollection;
194
	}
195

    
196
	public void setSetCollection(final String setCollection) {
197
		this.setCollection = setCollection;
198
	}
199

    
200
	public String getSetCountCollection() {
201
		return setCountCollection;
202
	}
203

    
204
	public void setSetCountCollection(final String setCountCollection) {
205
		this.setCountCollection = setCountCollection;
206
	}
207

    
208
	public MongoClient getPublisherMongoClient() {
209
		return publisherMongoClient;
210
	}
211

    
212
	public void setPublisherMongoClient(final MongoClient publisherMongoClient) {
213
		this.publisherMongoClient = publisherMongoClient;
214
	}
215

    
216
}
    (1-1/1)