1 |
42181
|
sandro.lab
|
package eu.dnetlib.oai.conf;
|
2 |
|
|
|
3 |
|
|
import java.util.List;
|
4 |
|
|
import javax.annotation.Resource;
|
5 |
|
|
|
6 |
42184
|
michele.ar
|
import eu.dnetlib.oai.info.SetInfo;
|
7 |
|
|
import eu.dnetlib.oai.mongo.MongoPublisherStore;
|
8 |
|
|
import eu.dnetlib.oai.mongo.MongoPublisherStoreDAO;
|
9 |
|
|
import eu.dnetlib.oai.sets.MongoSetCollection;
|
10 |
|
|
import eu.dnetlib.rmi.provision.MDFInfo;
|
11 |
|
|
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
|
12 |
42621
|
sandro.lab
|
import org.apache.commons.lang3.StringUtils;
|
13 |
|
|
import org.apache.commons.logging.Log;
|
14 |
|
|
import org.apache.commons.logging.LogFactory;
|
15 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
16 |
42184
|
michele.ar
|
|
17 |
42181
|
sandro.lab
|
public class OAISetHelper {
|
18 |
|
|
|
19 |
|
|
private static final Log log = LogFactory.getLog(OAISetHelper.class); // NOPMD by marko on 11/24/08 5:02 PM
|
20 |
|
|
|
21 |
|
|
@Resource(name = "oaiConfigurationExistReader")
|
22 |
|
|
private OAIConfigurationReader configurationReader;
|
23 |
42621
|
sandro.lab
|
@Autowired
|
24 |
42181
|
sandro.lab
|
private MongoPublisherStoreDAO mongoPublisherStoreDAO;
|
25 |
42621
|
sandro.lab
|
@Autowired
|
26 |
42181
|
sandro.lab
|
private MongoSetCollection mongoSetCollection;
|
27 |
|
|
|
28 |
|
|
public void loadConfiguration(final String dbName) {
|
29 |
|
|
loadConfigurationSets(dbName);
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
private void loadConfigurationSets(final String dbName) {
|
33 |
|
|
log.debug("*****Dropping and re-creating configuration sets******");
|
34 |
|
|
this.mongoSetCollection.dropConfigurationSets(dbName);
|
35 |
42184
|
michele.ar
|
final List<SetInfo> oaiConfigSets = this.configurationReader.getSets();
|
36 |
|
|
for (final SetInfo setInfo : oaiConfigSets) {
|
37 |
42181
|
sandro.lab
|
this.mongoSetCollection.upsertSet(setInfo, true, dbName);
|
38 |
|
|
}
|
39 |
|
|
log.info("Configuration Sets updated succesfully on db: " + dbName);
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
/*
|
43 |
|
|
* Helper method to count elements in OAI sets.
|
44 |
|
|
*/
|
45 |
|
|
public void updateAllCounts(final MDFInfo mdFormat, final String dbName) {
|
46 |
|
|
this.mongoSetCollection.ensureIndexes(dbName);
|
47 |
|
|
|
48 |
42184
|
michele.ar
|
final MongoPublisherStore store = getStore(mdFormat, dbName);
|
49 |
|
|
if (store == null) { throw new OaiPublisherRuntimeException("Can't count elements for not yet created store (" + mdFormat + ") on db " + dbName); }
|
50 |
42181
|
sandro.lab
|
|
51 |
|
|
updateTotalCount(store, mdFormat, dbName);
|
52 |
|
|
|
53 |
|
|
updateProvenanceSetsCount(store, mdFormat, dbName);
|
54 |
|
|
|
55 |
|
|
updateConfiguredSetsCount(store, mdFormat, dbName);
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
public void updateConfiguredSetsCount(final MDFInfo mdFormat, final String dbName) {
|
59 |
|
|
this.mongoSetCollection.ensureIndexes(dbName);
|
60 |
|
|
|
61 |
42184
|
michele.ar
|
final MongoPublisherStore store = getStore(mdFormat, dbName);
|
62 |
|
|
if (store == null) { throw new OaiPublisherRuntimeException("Can't count elements for not yet created store (" + mdFormat + ") on db " + dbName); }
|
63 |
42181
|
sandro.lab
|
|
64 |
|
|
updateConfiguredSetsCount(store, mdFormat, dbName);
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
protected void updateConfiguredSetsCount(final MongoPublisherStore store, final MDFInfo mdFormat, final String dbName) {
|
68 |
42184
|
michele.ar
|
final List<SetInfo> sets = this.mongoSetCollection.getConfiguredSets(dbName);
|
69 |
42181
|
sandro.lab
|
this.updateCountForSets(store, sets, mdFormat, dbName);
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
protected void updateProvenanceSetsCount(final MongoPublisherStore store, final MDFInfo mdFormat, final String dbName) {
|
73 |
|
|
// now we need to get all distinct set names in the store:
|
74 |
42184
|
michele.ar
|
final List<String> distinctSetSpecs = store.getDistinctSetNamesFromRecords();
|
75 |
42181
|
sandro.lab
|
store.upsertSets(distinctSetSpecs);
|
76 |
42184
|
michele.ar
|
final List<SetInfo> sets = this.mongoSetCollection.getSetsFromData(dbName);
|
77 |
42181
|
sandro.lab
|
this.updateCountForSets(store, sets, mdFormat, dbName);
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
protected void updateTotalCount(final MongoPublisherStore store, final MDFInfo mdFormat, final String dbName) {
|
81 |
42184
|
michele.ar
|
final String baseQuery = mdFormat.getBaseQuery();
|
82 |
|
|
final int total = store.count(baseQuery);
|
83 |
|
|
this.mongoSetCollection.updateCounts("ALL", mdFormat.getPrefix(), total, dbName);
|
84 |
42181
|
sandro.lab
|
log.info("Got total for " + mdFormat.getPrefix() + " with query: " + baseQuery + " on db " + dbName);
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
protected void updateCountForSets(final MongoPublisherStore store, final List<SetInfo> oaiSets, final MDFInfo mdFormat, final String dbName) {
|
88 |
|
|
|
89 |
42184
|
michele.ar
|
final String baseQuery = mdFormat.getBaseQuery();
|
90 |
|
|
final boolean hasBaseQuery = !StringUtils.isBlank(baseQuery);
|
91 |
|
|
for (final SetInfo setInfo : oaiSets) {
|
92 |
42181
|
sandro.lab
|
String setQuery = "(" + setInfo.getQuery() + ")";
|
93 |
|
|
if (hasBaseQuery) {
|
94 |
|
|
setQuery += " AND (" + baseQuery + ")";
|
95 |
|
|
}
|
96 |
|
|
log.info("Counting total for " + mdFormat.getPrefix() + " set " + setInfo + " with query: " + setQuery + " on db " + dbName);
|
97 |
42184
|
michele.ar
|
final int setCount = store.count(setQuery);
|
98 |
|
|
this.mongoSetCollection.updateCounts(setInfo.getSetSpec(), mdFormat.getPrefix(), setCount, dbName);
|
99 |
42181
|
sandro.lab
|
}
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
private MongoPublisherStore getStore(final MDFInfo mdFormat, final String dbName) {
|
103 |
42184
|
michele.ar
|
final String format = mdFormat.getSourceFormatName();
|
104 |
|
|
final String layout = mdFormat.getSourceFormatLayout();
|
105 |
|
|
final String interpretation = mdFormat.getSourceFormatInterpretation();
|
106 |
|
|
final String sourceKey = format + "-" + layout + "-" + interpretation;
|
107 |
|
|
final MongoPublisherStore store = this.mongoPublisherStoreDAO.getStore(format, interpretation, layout, dbName);
|
108 |
42181
|
sandro.lab
|
log.info("Got OAI store " + sourceKey + " via metadata prefix " + mdFormat.getPrefix() + " on db " + dbName);
|
109 |
|
|
|
110 |
|
|
return store;
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
public OAIConfigurationReader getConfigurationReader() {
|
114 |
42184
|
michele.ar
|
return this.configurationReader;
|
115 |
42181
|
sandro.lab
|
}
|
116 |
|
|
|
117 |
|
|
public void setConfigurationReader(final OAIConfigurationReader configurationReader) {
|
118 |
|
|
this.configurationReader = configurationReader;
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
public MongoPublisherStoreDAO getMongoPublisherStoreDAO() {
|
122 |
42184
|
michele.ar
|
return this.mongoPublisherStoreDAO;
|
123 |
42181
|
sandro.lab
|
}
|
124 |
|
|
|
125 |
|
|
public void setMongoPublisherStoreDAO(final MongoPublisherStoreDAO mongoPublisherStoreDAO) {
|
126 |
|
|
this.mongoPublisherStoreDAO = mongoPublisherStoreDAO;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
public MongoSetCollection getMongoSetCollection() {
|
130 |
42184
|
michele.ar
|
return this.mongoSetCollection;
|
131 |
42181
|
sandro.lab
|
}
|
132 |
|
|
|
133 |
|
|
public void setMongoSetCollection(final MongoSetCollection mongoSetCollection) {
|
134 |
|
|
this.mongoSetCollection = mongoSetCollection;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
}
|