Project

General

Profile

1
package eu.dnetlib.oai.conf;
2

    
3
import java.util.List;
4
import java.util.concurrent.Callable;
5
import javax.annotation.Resource;
6

    
7
import com.google.common.base.Splitter;
8
import eu.dnetlib.oai.sets.MongoSetCollection;
9
import eu.dnetlib.rmi.provision.MDFInfo;
10
import org.apache.commons.lang3.StringUtils;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13
import org.springframework.beans.factory.annotation.Autowired;
14

    
15
/**
16
 * Updates the counts for all sets in the special sets collection of OAI store.
17
 *
18
 * @author alessia
19
 */
20
public class OAISetsCounter {
21

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

    
24
	@Resource(name = "oaiConfigurationExistReader")
25
	private OAIConfigurationReader configurationReader;
26
	@Autowired
27
	private MongoSetCollection mongoSetCollection;
28
	@Autowired
29
	private OAISetHelper oaiSetHelper;
30

    
31
	public void updateSetCount(final String storeId, final String dbName, final String set){
32
		final List<MDFInfo> metadataFormats = listProcessableMDFInfo(storeId);
33
		for (final MDFInfo mdFormat : metadataFormats) {
34
			oaiSetHelper.updateSetCount(mdFormat, dbName, set);
35
		}
36
	}
37

    
38
	/**
39
	 * Update counts for all OAI sets.
40
	 * <p>
41
	 * If a non blank storeId is given, counts are updated only for those mdPrefix served by the given store.
42
	 * </p>
43
	 * <p>
44
	 * Otherwise all counts are updated.
45
	 * </p>
46
	 *
47
	 * @param storeId
48
	 *            oai store id. Can be blank to execute over all metadata served by OAI.
49
	 * @param callback
50
	 *            callback to execute when the execution is successful
51
	 * @param failCallback
52
	 *            to execute when the execution fails
53
	 */
54
	public void updateCounts(final String storeId, final String dbName, final Callable<?> callback, final Callable<?> failCallback) {
55
		final List<MDFInfo> metadataFormats = listProcessableMDFInfo(storeId);
56
		for (final MDFInfo mdFormat : metadataFormats) {
57
			updateCounts(mdFormat, dbName, callback, failCallback);
58
		}
59
	}
60

    
61
	public void updateCounts(final MDFInfo mdf, final String dbName, final Callable<?> callback, final Callable<?> failCallback) {
62
		new Thread() {
63

    
64
			@Override
65
			public void run() {
66
				try {
67
					OAISetsCounter.this.oaiSetHelper.updateAllCounts(mdf, dbName);
68
					log.info("All sets count updated succesfully for metadata format " + mdf + " on db " + dbName);
69
					executeCallback(callback);
70
				} catch (final Exception e) {
71
					log.error(e);
72
					executeCallback(failCallback);
73
				}
74
			}
75
		}.start();
76
	}
77

    
78
	/**
79
	 * Update counts for configured OAI sets and all md formats.
80
	 *
81
	 * @param callback
82
	 *            callback to execute when the execution is successful
83
	 * @param failCallback
84
	 *            to execute when the execution fails
85
	 */
86
	public void updateConfigurationCounts(final String storeId, final String dbName, final Callable<?> callback, final Callable<?> failCallback) {
87
		new Thread() {
88

    
89
			@Override
90
			public void run() {
91
				try {
92
					final List<MDFInfo> metadataFormats = listProcessableMDFInfo(storeId);
93

    
94
					for (final MDFInfo mdFormat : metadataFormats) {
95
						OAISetsCounter.this.oaiSetHelper.updateConfiguredSetsCount(mdFormat, dbName);
96
					}
97
					log.info("All configured sets count updated succesfully on db " + dbName);
98
					executeCallback(callback);
99
				} catch (final Exception e) {
100
					log.error(e);
101
					executeCallback(failCallback);
102
				}
103
			}
104
		}.start();
105
	}
106

    
107
	protected void executeCallback(final Callable<?> callback) {
108
		if (callback != null) {
109
			try {
110
				callback.call();
111
			} catch (final Exception e) {
112
				log.error("Error executing callback", e);
113
			}
114
		}
115
	}
116

    
117
	protected List<MDFInfo> listProcessableMDFInfo(final String storeId) {
118
		List<MDFInfo> metadataFormats;
119
		if (StringUtils.isBlank(storeId)) {
120
			metadataFormats = this.configurationReader.getMetadataFormatInfo();
121
		} else {
122
			List<String> splitted = Splitter.on('-').trimResults().splitToList(storeId);
123
			final String format = splitted.get(0);
124
			final String layout =splitted.get(1);
125
			final String inter = splitted.get(2);
126
			metadataFormats = this.configurationReader.getFormatsServedBy(format, layout, inter);
127
		}
128
		return metadataFormats;
129
	}
130

    
131
	public OAIConfigurationReader getConfigurationReader() {
132
		return this.configurationReader;
133
	}
134

    
135
	public void setConfigurationReader(final OAIConfigurationReader configurationReader) {
136
		this.configurationReader = configurationReader;
137
	}
138

    
139
	public MongoSetCollection getMongoSetCollection() {
140
		return this.mongoSetCollection;
141
	}
142

    
143
	public void setMongoSetCollection(final MongoSetCollection mongoSetCollection) {
144
		this.mongoSetCollection = mongoSetCollection;
145
	}
146

    
147
	public OAISetHelper getOaiSetCounterHelper() {
148
		return this.oaiSetHelper;
149
	}
150

    
151
	public void setOaiSetCounterHelper(final OAISetHelper oaiSetHelper) {
152
		this.oaiSetHelper = oaiSetHelper;
153
	}
154

    
155
}
(9-9/9)