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.collect.Lists;
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
	/**
32
	 * Update counts for all OAI sets.
33
	 * <p>
34
	 * If a non blank storeId is given, counts are updated only for those mdPrefix served by the given store.
35
	 * </p>
36
	 * <p>
37
	 * Otherwise all counts are updated.
38
	 * </p>
39
	 *
40
	 * @param storeId
41
	 *            oai store id. Can be blank to execute over all metadata served by OAI.
42
	 * @param callback
43
	 *            callback to execute when the execution is successful
44
	 * @param failCallbackcallback
45
	 *            to execute when the execution fails
46
	 */
47
	public void updateCounts(final String storeId, final String dbName, final Callable<?> callback, final Callable<?> failCallback) {
48
		new Thread() {
49

    
50
			@Override
51
			public void run() {
52
				try {
53
					final List<MDFInfo> metadataFormats = listProcessableMDFInfo(storeId);
54
					for (final MDFInfo mdFormat : metadataFormats) {
55
						OAISetsCounter.this.oaiSetHelper.updateAllCounts(mdFormat, dbName);
56
					}
57
					log.info("All sets count updated succesfully on db: " + dbName);
58
					executeCallback(callback);
59
				} catch (final Exception e) {
60
					log.error(e);
61
					executeCallback(failCallback);
62
				}
63
			}
64
		}.start();
65
	}
66

    
67
	public void updateCounts(final MDFInfo mdf, final String dbName, final Callable<?> callback, final Callable<?> failCallback) {
68
		new Thread() {
69

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

    
84
	/**
85
	 * Update counts for configured OAI sets and all md formats.
86
	 *
87
	 * @param callback
88
	 *            callback to execute when the execution is successful
89
	 * @param failCallbackcallback
90
	 *            to execute when the execution fails
91
	 */
92
	public void updateConfigurationCounts(final String storeId, final String dbName, final Callable<?> callback, final Callable<?> failCallback) {
93
		new Thread() {
94

    
95
			@Override
96
			public void run() {
97
				try {
98
					final List<MDFInfo> metadataFormats = listProcessableMDFInfo(storeId);
99

    
100
					for (final MDFInfo mdFormat : metadataFormats) {
101
						OAISetsCounter.this.oaiSetHelper.updateConfiguredSetsCount(mdFormat, dbName);
102
					}
103
					log.info("All configured sets count updated succesfully on db " + dbName);
104
					executeCallback(callback);
105
				} catch (final Exception e) {
106
					log.error(e);
107
					executeCallback(failCallback);
108
				}
109
			}
110
		}.start();
111
	}
112

    
113
	protected void executeCallback(final Callable<?> callback) {
114
		if (callback != null) {
115
			try {
116
				callback.call();
117
			} catch (final Exception e) {
118
				log.error("Error executing callback", e);
119
			}
120
		}
121
	}
122

    
123
	protected List<MDFInfo> listProcessableMDFInfo(final String storeId) {
124
		List<MDFInfo> metadataFormats = Lists.newArrayList();
125
		if (StringUtils.isBlank(storeId)) {
126
			metadataFormats = this.configurationReader.getMetadataFormatInfo();
127
		} else {
128
			final String[] splitted = storeId.split("-");
129
			final String format = splitted[0];
130
			final String layout = splitted[1];
131
			final String inter = splitted[2];
132
			metadataFormats = this.configurationReader.getFormatsServedBy(format, layout, inter);
133
		}
134
		return metadataFormats;
135
	}
136

    
137
	public OAIConfigurationReader getConfigurationReader() {
138
		return this.configurationReader;
139
	}
140

    
141
	public void setConfigurationReader(final OAIConfigurationReader configurationReader) {
142
		this.configurationReader = configurationReader;
143
	}
144

    
145
	public MongoSetCollection getMongoSetCollection() {
146
		return this.mongoSetCollection;
147
	}
148

    
149
	public void setMongoSetCollection(final MongoSetCollection mongoSetCollection) {
150
		this.mongoSetCollection = mongoSetCollection;
151
	}
152

    
153
	public OAISetHelper getOaiSetCounterHelper() {
154
		return this.oaiSetHelper;
155
	}
156

    
157
	public void setOaiSetCounterHelper(final OAISetHelper oaiSetHelper) {
158
		this.oaiSetHelper = oaiSetHelper;
159
	}
160

    
161
}
(9-9/9)