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.data.information.oai.publisher.info.MDFInfo;
9
import eu.dnetlib.data.oai.store.sets.MongoSetCollection;
10
import org.apache.commons.lang.StringUtils;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13

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

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

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

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

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

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

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

    
80
	/**
81
	 * Update counts for configured OAI sets and all md formats.
82
	 *
83
	 * @param callback             callback to execute when the execution is successful
84
	 * @param failCallbackcallback 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
					List<MDFInfo> metadataFormats = listProcessableMDFInfo(storeId);
93

    
94
					for (MDFInfo mdFormat : metadataFormats) {
95
						oaiSetHelper.updateConfiguredSetsCount(mdFormat, dbName);
96
					}
97
					log.info("All configured sets count updated succesfully on db " + dbName);
98
					executeCallback(callback);
99
				} catch (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 (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 = Lists.newArrayList();
119
		if (StringUtils.isBlank(storeId)) {
120
			metadataFormats = configurationReader.getMetadataFormatInfo();
121
		} else {
122
			String[] splitted = storeId.split("-");
123
			String format = splitted[0];
124
			String layout = splitted[1];
125
			String inter = splitted[2];
126
			metadataFormats = configurationReader.getFormatsServedBy(format, layout, inter);
127
		}
128
		return metadataFormats;
129
	}
130

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

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

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

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

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

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

    
155
}
(9-9/9)