Project

General

Profile

1
package eu.dnetlib.oai.sets;
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.conf.OAIConfigurationReader;
9
import eu.dnetlib.oai.utils.OAIHelper;
10
import eu.dnetlib.rmi.provision.MDFInfo;
11
import org.apache.commons.lang3.StringUtils;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
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
	@Resource
27
	private MongoSetCollection mongoSetCollection;
28
	@Resource
29
	private OAIHelper oaiHelper;
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 mdPreromfix 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 failCallback
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
			@Override
50
			public void run() {
51
				try {
52
					List<MDFInfo> metadataFormats = listProcessableMDFInfo(storeId);
53
					for (MDFInfo mdFormat : metadataFormats) {
54
						oaiHelper.updateAllCounts(mdFormat, dbName);
55
					}
56
					log.info("All sets count updated succesfully on db: " + dbName);
57
					executeCallback(callback);
58
				} catch (Exception e) {
59
					log.error(e);
60
					executeCallback(failCallback);
61
				}
62
			}
63
		}.start();
64
	}
65

    
66
	public void updateCounts(final MDFInfo mdf, final String dbName, final Callable<?> callback, final Callable<?> failCallback) {
67
		new Thread(() -> {
68
			try {
69
				oaiHelper.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
		}).start();
77
	}
78

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

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

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

    
114
	protected List<MDFInfo> listProcessableMDFInfo(final String storeId) {
115
		List<MDFInfo> metadataFormats = Lists.newArrayList();
116
		if (StringUtils.isBlank(storeId)) {
117
			metadataFormats = configurationReader.getMetadataFormatInfo();
118
		} else {
119
			String[] splitted = storeId.split("-");
120
			String format = splitted[0];
121
			String layout = splitted[1];
122
			String inter = splitted[2];
123
			metadataFormats = configurationReader.getFormatsServedBy(format, layout, inter);
124
		}
125
		return metadataFormats;
126
	}
127

    
128
	public OAIConfigurationReader getConfigurationReader() {
129
		return configurationReader;
130
	}
131

    
132
	public void setConfigurationReader(final OAIConfigurationReader configurationReader) {
133
		this.configurationReader = configurationReader;
134
	}
135

    
136
	public MongoSetCollection getMongoSetCollection() {
137
		return mongoSetCollection;
138
	}
139

    
140
	public void setMongoSetCollection(final MongoSetCollection mongoSetCollection) {
141
		this.mongoSetCollection = mongoSetCollection;
142
	}
143

    
144
	public OAIHelper getOaiSetCounterHelper() {
145
		return oaiHelper;
146
	}
147

    
148
	public void setOaiSetCounterHelper(final OAIHelper oaiHelper) {
149
		this.oaiHelper = oaiHelper;
150
	}
151

    
152
}
(2-2/3)