Project

General

Profile

1 42181 sandro.lab
package eu.dnetlib.oai.conf;
2
3
import java.util.List;
4
import java.util.concurrent.Callable;
5
import javax.annotation.Resource;
6
7 44144 alessia.ba
import com.google.common.base.Splitter;
8 42621 sandro.lab
import eu.dnetlib.oai.sets.MongoSetCollection;
9
import eu.dnetlib.rmi.provision.MDFInfo;
10 42184 michele.ar
import org.apache.commons.lang3.StringUtils;
11 42181 sandro.lab
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13 42621 sandro.lab
import org.springframework.beans.factory.annotation.Autowired;
14 42181 sandro.lab
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 42621 sandro.lab
	@Autowired
27 42181 sandro.lab
	private MongoSetCollection mongoSetCollection;
28 42621 sandro.lab
	@Autowired
29 42181 sandro.lab
	private OAISetHelper oaiSetHelper;
30
31 44144 alessia.ba
	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 42181 sandro.lab
	/**
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 42184 michele.ar
	 * @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 44144 alessia.ba
	 * @param failCallback
52 42184 michele.ar
	 *            to execute when the execution fails
53 42181 sandro.lab
	 */
54
	public void updateCounts(final String storeId, final String dbName, final Callable<?> callback, final Callable<?> failCallback) {
55 44144 alessia.ba
		final List<MDFInfo> metadataFormats = listProcessableMDFInfo(storeId);
56
		for (final MDFInfo mdFormat : metadataFormats) {
57
			updateCounts(mdFormat, dbName, callback, failCallback);
58
		}
59 42181 sandro.lab
	}
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 42184 michele.ar
					OAISetsCounter.this.oaiSetHelper.updateAllCounts(mdf, dbName);
68 42181 sandro.lab
					log.info("All sets count updated succesfully for metadata format " + mdf + " on db " + dbName);
69
					executeCallback(callback);
70 42184 michele.ar
				} catch (final Exception e) {
71 42181 sandro.lab
					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 42184 michele.ar
	 * @param callback
82
	 *            callback to execute when the execution is successful
83 44144 alessia.ba
	 * @param failCallback
84 42184 michele.ar
	 *            to execute when the execution fails
85 42181 sandro.lab
	 */
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 42184 michele.ar
					final List<MDFInfo> metadataFormats = listProcessableMDFInfo(storeId);
93 42181 sandro.lab
94 42184 michele.ar
					for (final MDFInfo mdFormat : metadataFormats) {
95
						OAISetsCounter.this.oaiSetHelper.updateConfiguredSetsCount(mdFormat, dbName);
96 42181 sandro.lab
					}
97
					log.info("All configured sets count updated succesfully on db " + dbName);
98
					executeCallback(callback);
99 42184 michele.ar
				} catch (final Exception e) {
100 42181 sandro.lab
					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 42184 michele.ar
			} catch (final Exception e) {
112 42181 sandro.lab
				log.error("Error executing callback", e);
113
			}
114
		}
115
	}
116
117
	protected List<MDFInfo> listProcessableMDFInfo(final String storeId) {
118 44144 alessia.ba
		List<MDFInfo> metadataFormats;
119 42181 sandro.lab
		if (StringUtils.isBlank(storeId)) {
120 42184 michele.ar
			metadataFormats = this.configurationReader.getMetadataFormatInfo();
121 42181 sandro.lab
		} else {
122 44144 alessia.ba
			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 42184 michele.ar
			metadataFormats = this.configurationReader.getFormatsServedBy(format, layout, inter);
127 42181 sandro.lab
		}
128
		return metadataFormats;
129
	}
130
131
	public OAIConfigurationReader getConfigurationReader() {
132 42184 michele.ar
		return this.configurationReader;
133 42181 sandro.lab
	}
134
135
	public void setConfigurationReader(final OAIConfigurationReader configurationReader) {
136
		this.configurationReader = configurationReader;
137
	}
138
139
	public MongoSetCollection getMongoSetCollection() {
140 42184 michele.ar
		return this.mongoSetCollection;
141 42181 sandro.lab
	}
142
143
	public void setMongoSetCollection(final MongoSetCollection mongoSetCollection) {
144
		this.mongoSetCollection = mongoSetCollection;
145
	}
146
147
	public OAISetHelper getOaiSetCounterHelper() {
148 42184 michele.ar
		return this.oaiSetHelper;
149 42181 sandro.lab
	}
150
151
	public void setOaiSetCounterHelper(final OAISetHelper oaiSetHelper) {
152
		this.oaiSetHelper = oaiSetHelper;
153
	}
154
155
}