Project

General

Profile

1
package eu.dnetlib.oai.conf;
2

    
3
import java.util.List;
4

    
5
import javax.annotation.Resource;
6

    
7
import org.apache.commons.lang3.StringUtils;
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10

    
11
import eu.dnetlib.oai.info.SetInfo;
12
import eu.dnetlib.oai.mongo.MongoPublisherStore;
13
import eu.dnetlib.oai.mongo.MongoPublisherStoreDAO;
14
import eu.dnetlib.oai.sets.MongoSetCollection;
15
import eu.dnetlib.rmi.provision.MDFInfo;
16
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
17

    
18
public class OAISetHelper {
19

    
20
	private static final Log log = LogFactory.getLog(OAISetHelper.class); // NOPMD by marko on 11/24/08 5:02 PM
21

    
22
	@Resource(name = "oaiConfigurationExistReader")
23
	private OAIConfigurationReader configurationReader;
24
	@Resource
25
	private MongoPublisherStoreDAO mongoPublisherStoreDAO;
26
	@Resource
27
	private MongoSetCollection mongoSetCollection;
28

    
29
	public void loadConfiguration(final String dbName) {
30
		loadConfigurationSets(dbName);
31
	}
32

    
33
	private void loadConfigurationSets(final String dbName) {
34
		log.debug("*****Dropping and re-creating configuration sets******");
35
		this.mongoSetCollection.dropConfigurationSets(dbName);
36
		final List<SetInfo> oaiConfigSets = this.configurationReader.getSets();
37
		for (final SetInfo setInfo : oaiConfigSets) {
38
			this.mongoSetCollection.upsertSet(setInfo, true, dbName);
39
		}
40
		log.info("Configuration Sets updated succesfully on db: " + dbName);
41
	}
42

    
43
	/*
44
	 * Helper method to count elements in OAI sets.
45
	 */
46
	public void updateAllCounts(final MDFInfo mdFormat, final String dbName) {
47
		this.mongoSetCollection.ensureIndexes(dbName);
48

    
49
		final MongoPublisherStore store = getStore(mdFormat, dbName);
50
		if (store == null) { throw new OaiPublisherRuntimeException("Can't count elements for not yet created store (" + mdFormat + ") on db " + dbName); }
51

    
52
		updateTotalCount(store, mdFormat, dbName);
53

    
54
		updateProvenanceSetsCount(store, mdFormat, dbName);
55

    
56
		updateConfiguredSetsCount(store, mdFormat, dbName);
57
	}
58

    
59
	public void updateConfiguredSetsCount(final MDFInfo mdFormat, final String dbName) {
60
		this.mongoSetCollection.ensureIndexes(dbName);
61

    
62
		final MongoPublisherStore store = getStore(mdFormat, dbName);
63
		if (store == null) { throw new OaiPublisherRuntimeException("Can't count elements for not yet created store (" + mdFormat + ") on db " + dbName); }
64

    
65
		updateConfiguredSetsCount(store, mdFormat, dbName);
66
	}
67

    
68
	protected void updateConfiguredSetsCount(final MongoPublisherStore store, final MDFInfo mdFormat, final String dbName) {
69
		final List<SetInfo> sets = this.mongoSetCollection.getConfiguredSets(dbName);
70
		this.updateCountForSets(store, sets, mdFormat, dbName);
71
	}
72

    
73
	protected void updateProvenanceSetsCount(final MongoPublisherStore store, final MDFInfo mdFormat, final String dbName) {
74
		// now we need to get all distinct set names in the store:
75
		final List<String> distinctSetSpecs = store.getDistinctSetNamesFromRecords();
76
		store.upsertSets(distinctSetSpecs);
77
		final List<SetInfo> sets = this.mongoSetCollection.getSetsFromData(dbName);
78
		this.updateCountForSets(store, sets, mdFormat, dbName);
79
	}
80

    
81
	protected void updateTotalCount(final MongoPublisherStore store, final MDFInfo mdFormat, final String dbName) {
82
		final String baseQuery = mdFormat.getBaseQuery();
83
		final int total = store.count(baseQuery);
84
		this.mongoSetCollection.updateCounts("ALL", mdFormat.getPrefix(), total, dbName);
85
		log.info("Got total for " + mdFormat.getPrefix() + " with query: " + baseQuery + " on db " + dbName);
86
	}
87

    
88
	protected void updateCountForSets(final MongoPublisherStore store, final List<SetInfo> oaiSets, final MDFInfo mdFormat, final String dbName) {
89

    
90
		final String baseQuery = mdFormat.getBaseQuery();
91
		final boolean hasBaseQuery = !StringUtils.isBlank(baseQuery);
92
		for (final SetInfo setInfo : oaiSets) {
93
			String setQuery = "(" + setInfo.getQuery() + ")";
94
			if (hasBaseQuery) {
95
				setQuery += " AND (" + baseQuery + ")";
96
			}
97
			log.info("Counting total for " + mdFormat.getPrefix() + " set " + setInfo + " with query: " + setQuery + " on db " + dbName);
98
			final int setCount = store.count(setQuery);
99
			this.mongoSetCollection.updateCounts(setInfo.getSetSpec(), mdFormat.getPrefix(), setCount, dbName);
100
		}
101
	}
102

    
103
	private MongoPublisherStore getStore(final MDFInfo mdFormat, final String dbName) {
104
		final String format = mdFormat.getSourceFormatName();
105
		final String layout = mdFormat.getSourceFormatLayout();
106
		final String interpretation = mdFormat.getSourceFormatInterpretation();
107
		final String sourceKey = format + "-" + layout + "-" + interpretation;
108
		final MongoPublisherStore store = this.mongoPublisherStoreDAO.getStore(format, interpretation, layout, dbName);
109
		log.info("Got OAI store " + sourceKey + " via metadata prefix " + mdFormat.getPrefix() + " on db " + dbName);
110

    
111
		return store;
112
	}
113

    
114
	public OAIConfigurationReader getConfigurationReader() {
115
		return this.configurationReader;
116
	}
117

    
118
	public void setConfigurationReader(final OAIConfigurationReader configurationReader) {
119
		this.configurationReader = configurationReader;
120
	}
121

    
122
	public MongoPublisherStoreDAO getMongoPublisherStoreDAO() {
123
		return this.mongoPublisherStoreDAO;
124
	}
125

    
126
	public void setMongoPublisherStoreDAO(final MongoPublisherStoreDAO mongoPublisherStoreDAO) {
127
		this.mongoPublisherStoreDAO = mongoPublisherStoreDAO;
128
	}
129

    
130
	public MongoSetCollection getMongoSetCollection() {
131
		return this.mongoSetCollection;
132
	}
133

    
134
	public void setMongoSetCollection(final MongoSetCollection mongoSetCollection) {
135
		this.mongoSetCollection = mongoSetCollection;
136
	}
137

    
138
}
(8-8/9)