Project

General

Profile

« Previous | Next » 

Revision 39190

fixing iteration issue

View differences:

MultipleMdStoreIterator.java
10 10
import org.apache.commons.logging.LogFactory;
11 11
import org.springframework.beans.factory.annotation.Autowired;
12 12

  
13
import com.google.common.base.Function;
14
import com.google.common.collect.Iterables;
15

  
13 16
import eu.dnetlib.data.mdstore.MDStoreService;
14 17
import eu.dnetlib.data.mdstore.MDStoreServiceException;
15 18
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
......
27 30
	@Resource
28 31
	private UniqueServiceLocator serviceLocator;
29 32

  
30
	/** The md i ds. */
31
	private List<String> mdIDs;
32

  
33
	/** The current id. */
34
	private String currentId = null;
35

  
36 33
	/** The current iterator. */
37 34
	private Iterator<String> currentIterator;
38 35

  
......
50 47
	 */
51 48
	public MultipleMdStoreIterator(final UniqueServiceLocator serviceLocator, final List<String> mdIds, final ResultSetClientFactory resultSetClientFactory) {
52 49
		this.serviceLocator = serviceLocator;
53
		this.mdIDs = mdIds;
54 50
		this.resultSetClientFactory = resultSetClientFactory;
55
		getNextMDStoreRecords();
56 51

  
52
		log.info(String.format("iterating over mdIds: '%s'", mdIds));
53

  
54
		this.currentIterator = Iterables.concat(Iterables.transform(mdIds, new Function<String, Iterable<String>>() {
55

  
56
			@Override
57
			public Iterable<String> apply(final String mdId) {
58

  
59
				log.debug(String.format("current mdId '%s'", mdId));
60
				try {
61
					W3CEndpointReference epr = serviceLocator.getService(MDStoreService.class, mdId).deliverMDRecords(mdId, "", "", "");
62
					return resultSetClientFactory.getClient(epr);
63
				} catch (MDStoreServiceException e) {
64
					log.error(e);
65
					throw new RuntimeException(String.format("unable to iterate over %s", mdId), e);
66
				}
67
			}
68
		})).iterator();
57 69
	}
58 70

  
59 71
	/*
......
63 75
	 */
64 76
	@Override
65 77
	public boolean hasNext() {
66
		if ((currentId == null) || (currentIterator == null)) return false;
78
		if (currentIterator == null) return false;
79

  
67 80
		return currentIterator.hasNext();
68 81
	}
69 82

  
......
74 87
	 */
75 88
	@Override
76 89
	public String next() {
77
		String nextElement = currentIterator.next();
78
		if (!currentIterator.hasNext()) {
79
			getNextMDStoreRecords();
80
		}
81
		return nextElement;
90
		return currentIterator.next();
82 91
	}
83 92

  
84 93
	/*
......
88 97
	 */
89 98
	@Override
90 99
	public void remove() {
91
		currentIterator.remove();
100
		throw new UnsupportedOperationException("Cannot remove");
92 101
	}
93 102

  
94
	/**
95
	 * Gets the next md store records.
96
	 *
97
	 * @return the next md store records
98
	 */
99
	private void getNextMDStoreRecords() {
100
		if (mdIDs.size() > 0) {
101
			currentId = mdIDs.remove(0);
102
			currentIterator = getIterableResultset(currentId);
103
		}
104
	}
105

  
106
	/**
107
	 * Gets the iterable resultset.
108
	 *
109
	 * @param id
110
	 *            the id
111
	 * @return the iterable resultset
112
	 */
113
	private Iterator<String> getIterableResultset(final String id) {
114
		log.debug(String.format("iterating over mdId '%s'", id));
115
		try {
116
			W3CEndpointReference epr = serviceLocator.getService(MDStoreService.class, id).deliverMDRecords(id, "", "", "");
117
			Iterable<String> input = resultSetClientFactory.getClient(epr);
118
			return input.iterator();
119
		} catch (MDStoreServiceException e) {
120
			log.error(e);
121
			return null;
122
		}
123
	}
124 103
}

Also available in: Unified diff