Project

General

Profile

« Previous | Next » 

Revision 53777

branch for solr 7.5.0

View differences:

modules/dnet-index-client/branches/solr75/deploy.info
1
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet45/modules/dnet-index-client/trunk/", "deploy_repository": "dnet45-snapshots", "version": "4", "mail": "sandro.labruzzo@isti.cnr.it,michele.artini@isti.cnr.it, claudio.atzori@isti.cnr.it, alessia.bardi@isti.cnr.it", "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-snapshots", "name": "dnet-index-client"}
modules/dnet-index-client/branches/solr75/src/test/java/eu/dnetlib/functionality/index/query/SolrIndexQueryFactoryTest.java
1
package eu.dnetlib.functionality.index.query;
2

  
3
import java.util.List;
4

  
5
import com.google.common.collect.BiMap;
6
import com.google.common.collect.HashBiMap;
7
import com.google.common.collect.Lists;
8
import eu.dnetlib.functionality.index.client.IndexClientException;
9
import org.junit.Assert;
10
import org.junit.Before;
11
import org.junit.Test;
12

  
13
public class SolrIndexQueryFactoryTest {
14

  
15
	IndexQueryFactory factory;
16
	BiMap<String, String> browsingAliases = HashBiMap.create();
17

  
18
	@Before
19
	public void setUp() throws Exception {
20
		factory = new SolrIndexQueryFactory();
21
		browsingAliases.put("field1", "field1ForBrowsing");
22
		browsingAliases.put("field2", "field2ForBrowsing");
23
	}
24

  
25
	@Test
26
	public void testGetBrowsableFields() throws IndexClientException {
27
		List<String> browsables = factory.getBrowsableFields(Lists.newArrayList("field1", "field3", "field2"), browsingAliases);
28
		Assert.assertEquals(Lists.newArrayList("field1ForBrowsing", "field3", "field2ForBrowsing"), browsables);
29
	}
30

  
31
}
modules/dnet-index-client/branches/solr75/src/main/java/eu/dnetlib/functionality/index/solr/cql/SolrTypeBasedCqlValueTransformerMapFactory.java
1
package eu.dnetlib.functionality.index.solr.cql;
2

  
3
import java.util.Map;
4

  
5
import eu.dnetlib.functionality.index.model.Any.ValueType;
6
import eu.dnetlib.miscutils.functional.UnaryFunction;
7
import org.springframework.beans.factory.annotation.Required;
8

  
9
/**
10
 * Factory for the SolrTypeBasedCqlValueTransformerMap class objects
11
 * 
12
 * @author claudio
13
 * 
14
 */
15
public class SolrTypeBasedCqlValueTransformerMapFactory {
16

  
17
	/**
18
	 * Map of functions, injected via spring.
19
	 */
20
	private Map<String, UnaryFunction<String, String>> transformerMap;
21

  
22
	/**
23
	 * Method returns a new instance of SolrTypeBasedCqlValueTransformerMap.
24
	 * 
25
	 * @param schema
26
	 * @return the SolrTypeBasedCqlValueTransformerMap
27
	 */
28
	public SolrTypeBasedCqlValueTransformerMap getIt(final Map<String, ValueType> schema) {
29
		return new SolrTypeBasedCqlValueTransformerMap(schema, getTransformerMap());
30
	}
31

  
32
	@Required
33
	public void setTransformerMap(Map<String, UnaryFunction<String, String>> transformerMap) {
34
		this.transformerMap = transformerMap;
35
	}
36

  
37
	public Map<String, UnaryFunction<String, String>> getTransformerMap() {
38
		return transformerMap;
39
	}
40

  
41
}
modules/dnet-index-client/branches/solr75/src/main/java/eu/dnetlib/functionality/index/solr/cql/SimpleDateValueTransformer.java
1
package eu.dnetlib.functionality.index.solr.cql;
2

  
3
import eu.dnetlib.miscutils.functional.UnaryFunction;
4

  
5
/**
6
 * Simply and not very roboust normalizer for solr dates. Basically it handles well yyyy-mm-dd and
7
 * yyyy-mm-ddThh:mm:ssZ
8
 * 
9
 * @author marko
10
 * 
11
 */
12
public class SimpleDateValueTransformer implements UnaryFunction<String, String> {
13
	@Override
14
	public String evaluate(final String value) {
15
		if (!value.endsWith("Z"))
16
			return value + "T00:00:00Z";
17
		return value;
18
	}
19
}
modules/dnet-index-client/branches/solr75/src/main/java/eu/dnetlib/functionality/index/solr/cql/SolrTypeBasedCqlValueTransformerMap.java
1
package eu.dnetlib.functionality.index.solr.cql;
2

  
3
import java.util.Map;
4

  
5
import eu.dnetlib.functionality.cql.CqlValueTransformerMap;
6
import eu.dnetlib.functionality.index.model.Any.ValueType;
7
import eu.dnetlib.miscutils.functional.IdentityFunction;
8
import eu.dnetlib.miscutils.functional.UnaryFunction;
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11
import org.apache.solr.common.SolrException;
12

  
13
/**
14
 * This class maps the fields in the given index schema with a transformation rule.
15
 * 
16
 * @author marko
17
 * 
18
 */
19
public class SolrTypeBasedCqlValueTransformerMap implements CqlValueTransformerMap {
20

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

  
26
	/**
27
	 * Index schema.
28
	 */
29
	private final Map<String, ValueType> schema;
30

  
31
	/**
32
	 * Map of functions.
33
	 */
34
	private final Map<String, UnaryFunction<String, String>> transformerMap;
35

  
36
	/**
37
	 * Create value transformer map bound to a specific schema
38
	 * @param schema
39
	 * @param transformerMap
40
	 */
41
	public SolrTypeBasedCqlValueTransformerMap(final Map<String, ValueType> schema, final Map<String, UnaryFunction<String, String>> transformerMap) {
42
		this.schema = schema;
43
		this.transformerMap = transformerMap;
44
	}
45

  
46
	/**
47
	 * {@inheritDoc}
48
	 * 
49
	 * @see CqlValueTransformerMap#transformerFor(String)
50
	 */
51
	@Override
52
	public UnaryFunction<String, String> transformerFor(final String fieldName) {
53
		try {
54
			final ValueType field = schema.get(fieldName);
55

  
56
			if (field != null) {
57
				UnaryFunction<String, String> res = transformerMap.get(field.name());
58
				if (res != null) {
59
					return res;
60
				}
61
			}
62
		} catch (SolrException e) {
63
			log.debug("cannot find field", e);
64
		}
65
		return new IdentityFunction<String>();
66
	}
67

  
68
}
modules/dnet-index-client/branches/solr75/src/main/java/eu/dnetlib/functionality/index/solr/utils/HighlightUtils.java
1
package eu.dnetlib.functionality.index.solr.utils;
2

  
3
import eu.dnetlib.miscutils.functional.UnaryFunction;
4
import org.apache.oro.text.perl.Perl5Util;
5

  
6
public class HighlightUtils implements UnaryFunction<String, String> {
7

  
8
	public final static String DEFAULT_HL_PRE = "[hl]";
9

  
10
	public final static String DEFAULT_HL_POST = "[/hl]";
11

  
12
	private static String CLEAN_HEADER = "s#\\[/?hl\\]##gm";
13
	private static String CLEAN_REGEX_OPEN = "<([^>]*)\\[hl\\]([^>]*)>";
14
	private static String CLEAN_REGEX_CLOSE = "<([^>]*)\\[\\/hl\\]([^>]*)>";
15

  
16
	// private static String CLEAN_REGEX_OPEN = "s#<([^>]*)\\[hl\\]([^>]*)>#<$1$2>#gm";
17
	// private static String CLEAN_REGEX_CLOSE = "s#<([^>]*)\\[\\/hl\\]([^>]*)>#<$1$2>#gm";
18

  
19
	private Perl5Util p5util = new Perl5Util();
20

  
21
	@Override
22
	public String evaluate(final String doc) {
23
		String[] chunk = doc.split("</header>");
24
		String string = cleanHeader(chunk[0]) + "</header>" + cleanBody(chunk[1]);
25
		return string;
26
	}
27

  
28
	private String cleanHeader(final String header) {
29
		return p5util.substitute(CLEAN_HEADER, header);
30
	}
31

  
32
	// TODO: implement a faster way to do this
33
	private String cleanBody(final String body) {
34
		String res = body.replaceAll(CLEAN_REGEX_OPEN, "<$1$2>").replaceAll(CLEAN_REGEX_CLOSE, "<$1$2>");
35

  
36
		if (res.equals(body)) return res;
37

  
38
		return cleanBody(res);
39
	}
40

  
41
}
modules/dnet-index-client/branches/solr75/src/main/java/eu/dnetlib/functionality/index/solr/feed/StreamingInputDocumentFactory.java
1
package eu.dnetlib.functionality.index.solr.feed;
2

  
3
import java.io.StringReader;
4
import java.io.StringWriter;
5
import java.util.HashMap;
6
import java.util.Iterator;
7
import java.util.List;
8
import javax.xml.stream.*;
9
import javax.xml.stream.events.Namespace;
10
import javax.xml.stream.events.StartElement;
11
import javax.xml.stream.events.XMLEvent;
12

  
13
import com.google.common.collect.Lists;
14
import eu.dnetlib.functionality.index.solr.feed.ResultTransformer.Mode;
15
import org.apache.solr.common.SolrInputDocument;
16

  
17
/**
18
 * Optimized version of the document parser, drop in replacement of InputDocumentFactory.
19
 *
20
 * <p>
21
 * Faster because:
22
 * </p>
23
 * <ul>
24
 * <li>Doesn't create a DOM for the full document</li>
25
 * <li>Doesn't execute xpaths agains the DOM</li>
26
 * <li>Quickly serialize the 'result' element directly in a string.</li>
27
 * <li>Uses less memory: less pressure on GC and allows more threads to process this in parallel</li>
28
 * </ul>
29
 *
30
 * <p>
31
 * This class is fully reentrant and can be invoked in parallel.
32
 * </p>
33
 *
34
 * @author marko
35
 *
36
 */
37
public class StreamingInputDocumentFactory extends InputDocumentFactory {
38

  
39
	protected static final String DEFAULTDNETRESULT = "dnetResult";
40

  
41
	protected static final String TARGETFIELDS = "targetFields";
42

  
43
	protected static final String INDEX_RECORD_ID_ELEMENT = "indexRecordIdentifier";
44

  
45
	protected static final String ROOT_ELEMENT = "indexRecord";
46

  
47
	protected ThreadLocal<XMLInputFactory> inputFactory = new ThreadLocal<XMLInputFactory>() {
48

  
49
		@Override
50
		protected XMLInputFactory initialValue() {
51
			return XMLInputFactory.newInstance();
52
		}
53
	};
54

  
55
	protected ThreadLocal<XMLOutputFactory> outputFactory = new ThreadLocal<XMLOutputFactory>() {
56

  
57
		@Override
58
		protected XMLOutputFactory initialValue() {
59
			return XMLOutputFactory.newInstance();
60
		}
61
	};
62

  
63
	protected ThreadLocal<XMLEventFactory> eventFactory = new ThreadLocal<XMLEventFactory>() {
64

  
65
		@Override
66
		protected XMLEventFactory initialValue() {
67
			return XMLEventFactory.newInstance();
68
		}
69
	};
70

  
71
	@Override
72
	public SolrInputDocument parseDocument(final String version, final String inputDocument, final String dsId, final String resultName)
73
			throws XMLStreamException {
74
		return parseDocument(version, inputDocument, dsId, resultName, null);
75
	}
76

  
77
	@Override
78
	public SolrInputDocument parseDocument(final String version,
79
			final String inputDocument,
80
			final String dsId,
81
			final String resultName,
82
			final ResultTransformer resultTransformer) {
83

  
84
		final StringWriter results = new StringWriter();
85
		final List<Namespace> nsList = Lists.newLinkedList();
86
		try {
87

  
88
			XMLEventReader parser = inputFactory.get().createXMLEventReader(new StringReader(inputDocument));
89

  
90
			final SolrInputDocument indexDocument = new SolrInputDocument(new HashMap<>());
91

  
92
			while (parser.hasNext()) {
93
				final XMLEvent event = parser.nextEvent();
94
				if ((event != null) && event.isStartElement()) {
95
					final String localName = event.asStartElement().getName().getLocalPart();
96

  
97
					if (ROOT_ELEMENT.equals(localName)) {
98
						nsList.addAll(getNamespaces(event));
99
					} else if (INDEX_RECORD_ID_ELEMENT.equals(localName)) {
100
						final XMLEvent text = parser.nextEvent();
101
						String recordId = getText(text);
102
						indexDocument.addField(INDEX_RECORD_ID, recordId);
103
					} else if (TARGETFIELDS.equals(localName)) {
104
						parseTargetFields(indexDocument, parser);
105
					} else if (resultName.equals(localName)) {
106
						if (resultTransformer == null || !(Mode.empty.equals(resultTransformer.getMode()))) {
107
							copyResult(indexDocument, results, parser, nsList, resultName, resultTransformer);
108
						}
109
					}
110
				}
111
			}
112

  
113
			if (version != null) {
114
				indexDocument.addField(DS_VERSION, version);
115
			}
116

  
117
			if (dsId != null) {
118
				indexDocument.addField(DS_ID, dsId);
119
			}
120

  
121
			if (!indexDocument.containsKey(INDEX_RECORD_ID)) {
122
				indexDocument.clear();
123
				System.err.println("missing indexrecord id:\n" + inputDocument);
124
			}
125

  
126
			return indexDocument;
127
		} catch (XMLStreamException e) {
128
			return new SolrInputDocument();
129
		}
130
	}
131

  
132
	private List<Namespace> getNamespaces(final XMLEvent event) {
133
		final List<Namespace> res = Lists.newLinkedList();
134
		@SuppressWarnings("unchecked")
135
		Iterator<Namespace> nsIter = event.asStartElement().getNamespaces();
136
		while (nsIter.hasNext()) {
137
			Namespace ns = nsIter.next();
138
			res.add(ns);
139
		}
140
		return res;
141
	}
142

  
143
	/**
144
	 * Parse the targetFields block and add fields to the solr document.
145
	 *
146
	 * @param indexDocument
147
	 * @param parser
148
	 * @throws XMLStreamException
149
	 */
150
	protected void parseTargetFields(final SolrInputDocument indexDocument, final XMLEventReader parser) throws XMLStreamException {
151

  
152
		boolean hasFields = false;
153

  
154
		while (parser.hasNext()) {
155
			final XMLEvent targetEvent = parser.nextEvent();
156
			if (targetEvent.isEndElement() && targetEvent.asEndElement().getName().getLocalPart().equals(TARGETFIELDS)) {
157
				break;
158
			}
159

  
160
			if (targetEvent.isStartElement()) {
161
				final String fieldName = targetEvent.asStartElement().getName().getLocalPart();
162
				final XMLEvent text = parser.nextEvent();
163

  
164
				String data = getText(text);
165

  
166
				addField(indexDocument, fieldName, data);
167
				hasFields = true;
168
			}
169
		}
170

  
171
		if (!hasFields) {
172
			indexDocument.clear();
173
		}
174
	}
175

  
176
	/**
177
	 * Copy the /indexRecord/result element and children, preserving namespace declarations etc.
178
	 *
179
	 * @param indexDocument
180
	 * @param results
181
	 * @param parser
182
	 * @param nsList
183
	 * @throws XMLStreamException
184
	 */
185
	protected void copyResult(final SolrInputDocument indexDocument,
186
			final StringWriter results,
187
			final XMLEventReader parser,
188
			final List<Namespace> nsList,
189
			final String dnetResult,
190
			final ResultTransformer resultTransformer) throws XMLStreamException {
191
		final XMLEventWriter writer = outputFactory.get().createXMLEventWriter(results);
192

  
193
		for (Namespace ns : nsList) {
194
			eventFactory.get().createNamespace(ns.getPrefix(), ns.getNamespaceURI());
195
		}
196

  
197
		StartElement newRecord = eventFactory.get().createStartElement("", null, RESULT, null, nsList.iterator());
198

  
199
		// new root record
200
		writer.add(newRecord);
201

  
202
		// copy the rest as it is
203
		while (parser.hasNext()) {
204
			final XMLEvent resultEvent = parser.nextEvent();
205

  
206
			// TODO: replace with depth tracking instead of close tag tracking.
207
			if (resultEvent.isEndElement() && resultEvent.asEndElement().getName().getLocalPart().equals(dnetResult)) {
208
				writer.add(eventFactory.get().createEndElement("", null, RESULT));
209
				break;
210
			}
211

  
212
			writer.add(resultEvent);
213
		}
214
		writer.close();
215

  
216
		if (resultTransformer != null) {
217
			indexDocument.addField(INDEX_RESULT, resultTransformer.apply(results.toString()));
218
		} else {
219
			indexDocument.addField(INDEX_RESULT, results.toString());
220
		}
221
	}
222

  
223
	/**
224
	 * Helper used to add a field to a solr doc. It avoids to add empy fields
225
	 *
226
	 * @param indexDocument
227
	 * @param field
228
	 * @param value
229
	 */
230
	private final void addField(final SolrInputDocument indexDocument, final String field, final String value) {
231
		String cleaned = value.trim();
232
		if (!cleaned.isEmpty()) {
233
			// log.info("\n\n adding field " + field.toLowerCase() + " value: " + cleaned + "\n");
234
			indexDocument.addField(field.toLowerCase(), cleaned);
235
		}
236
	}
237

  
238
	/**
239
	 * Helper used to get the string from a text element.
240
	 *
241
	 * @param text
242
	 * @return the
243
	 */
244
	protected final String getText(final XMLEvent text) {
245
		if (text.isEndElement()) // log.warn("skipping because isEndOfElement " + text.asEndElement().getName().getLocalPart());
246
			return "";
247

  
248
		return text.asCharacters().getData();
249
	}
250

  
251
}
modules/dnet-index-client/branches/solr75/src/main/java/eu/dnetlib/functionality/index/solr/feed/ResultTransformer.java
1
package eu.dnetlib.functionality.index.solr.feed;
2

  
3
import com.google.common.base.Function;
4

  
5
/**
6
 * Created by claudio on 17/11/15.
7
 */
8
public abstract class ResultTransformer implements Function<String, String> {
9

  
10
	public enum Mode {compress, empty, xslt, base64}
11

  
12
	protected Mode mode;
13

  
14
	public ResultTransformer(final Mode mode) {
15
		this.mode = mode;
16
	}
17

  
18
	public Mode getMode() {
19
		return mode;
20
	}
21

  
22
	public void setMode(final Mode mode) {
23
		this.mode = mode;
24
	}
25

  
26
}
modules/dnet-index-client/branches/solr75/src/main/java/eu/dnetlib/functionality/index/solr/feed/InputDocumentFactory.java
1
package eu.dnetlib.functionality.index.solr.feed;
2

  
3
import java.text.ParseException;
4
import java.text.SimpleDateFormat;
5
import java.util.Arrays;
6
import java.util.List;
7
import javax.xml.stream.XMLStreamException;
8

  
9
import org.apache.solr.common.SolrInputDocument;
10
import org.dom4j.DocumentException;
11

  
12
/**
13
 *
14
 * @author claudio
15
 *
16
 */
17
public abstract class InputDocumentFactory {
18

  
19
	public static final String INDEX_FIELD_PREFIX = "__";
20

  
21
	public static final String DS_VERSION = INDEX_FIELD_PREFIX + "dsversion";
22

  
23
	public static final String DS_ID = INDEX_FIELD_PREFIX + "dsid";
24

  
25
	public static final String RESULT = "result";
26

  
27
	public static final String INDEX_RESULT = INDEX_FIELD_PREFIX + RESULT;
28

  
29
	public static final String INDEX_RECORD_ID = INDEX_FIELD_PREFIX + "indexrecordidentifier";
30

  
31
	private static final String outFormat = new String("yyyy-MM-dd'T'hh:mm:ss'Z'");
32

  
33
	private final static List<String> dateFormats = Arrays.asList("yyyy-MM-dd'T'hh:mm:ss", "yyyy-MM-dd", "dd-MM-yyyy", "dd/MM/yyyy", "yyyy");
34

  
35
	public abstract SolrInputDocument parseDocument(final String version,
36
			final String inputDocument,
37
			final String dsId,
38
			final String resultName) throws XMLStreamException;
39

  
40
	public abstract SolrInputDocument parseDocument(final String version,
41
			final String inputDocument,
42
			final String dsId,
43
			final String resultName,
44
			final ResultTransformer resultTransformer) throws XMLStreamException;
45

  
46
	/**
47
	 * method return a solr-compatible string representation of a date
48
	 *
49
	 * @param date
50
	 * @return the parsed date
51
	 * @throws DocumentException
52
	 * @throws ParseException
53
	 */
54
	public static String getParsedDateField(final String date) {
55
		for (String formatString : dateFormats) {
56
			try {
57
				return new SimpleDateFormat(outFormat).format(new SimpleDateFormat(formatString).parse(date));
58
			} catch (ParseException e) {}
59
		}
60
		throw new IllegalStateException("unable to parse date: " + date);
61
	}
62

  
63
	public String parseDate(final String date) {
64
		return getParsedDateField(date);
65
	}
66

  
67
}
modules/dnet-index-client/branches/solr75/src/main/java/eu/dnetlib/functionality/index/model/AnySeq.java
1
package eu.dnetlib.functionality.index.model;
2

  
3
import java.util.Date;
4
import java.util.List;
5

  
6
/**
7
 * Interface for a sequence of Any objects.
8
 * 
9
 * <p>
10
 * AnySeq does <b>not</b> allow <code>null</code> values!
11
 * </p>
12
 * 
13
 */
14
public interface AnySeq extends List<Any>, Any {
15

  
16
	/**
17
	 * @param element
18
	 *            The string object to add
19
	 * @return true if successfully added, false else
20
	 */
21
	boolean add(String element);
22

  
23
	/**
24
	 * Long, Integer, Short and Byte values will be converted to Value object of type LONG, all others to Value object
25
	 * of type DOUBLE.
26
	 * 
27
	 * @param number
28
	 *            The number object to add
29
	 * @return true if successfully added, false else
30
	 */
31
	boolean add(Number number);
32

  
33
	/**
34
	 * @param index
35
	 *            The index where to add the any object
36
	 * @param element
37
	 *            The any object to add
38
	 */
39
	@Override
40
	void add(int index, Any element);
41

  
42
	/**
43
	 * @param index
44
	 *            The index of the object to return
45
	 * @return The AnyMap matching to the index, an InvalidValueTypeException is thrown if the value is not of type
46
	 *         AnyMap
47
	 */
48
	AnyMap getMap(int index);
49

  
50
	/**
51
	 * @param index
52
	 *            The index of the object to return
53
	 * @return The AnySeq matching to this index, an InvalidValueTypeException is thrown if the value is not of type
54
	 */
55
	AnySeq getSeq(int index);
56

  
57
	/**
58
	 * @param index
59
	 *            The index of the object to return
60
	 * @return The value matching to this index, an InvalidValueTypeException is thrown if the value is no value type.
61
	 */
62
	Value getValue(int index);
63

  
64
	/**
65
	 * @param index
66
	 *            The index of the object to return
67
	 * @return The string value matching to this index, an InvalidValueTypeException is thrown if the value is not of
68
	 *         type string
69
	 */
70
	String getStringValue(int index);
71

  
72
	/**
73
	 * @param index
74
	 *            The index of the object to return
75
	 * @return The double value matching to this index, an InvalidValueTypeException is thrown if the value is not of
76
	 *         type double
77
	 */
78
	Double getDoubleValue(int index);
79

  
80
	/**
81
	 * @param index
82
	 *            The index of the object to return
83
	 * @return The long value matching to this index, an InvalidValueTypeException is thrown if the value is not of type
84
	 *         long
85
	 */
86
	Long getLongValue(int index);
87

  
88
	/**
89
	 * @param index
90
	 *            The index of the object to return
91
	 * @return The boolean value matching to this index, an InvalidValueTypeException is thrown if the value is not of
92
	 *         type boolean
93
	 */
94
	Boolean getBooleanValue(int index);
95

  
96
	/**
97
	 * @param index
98
	 *            The index of the object to return
99
	 * @return The date value matching to this index, an InvalidValueTypeException is thrown if the value is not of type
100
	 *         date
101
	 */
102
	Date getDateValue(int index);
103

  
104
	/**
105
	 * @param index
106
	 *            The index of the object to return
107
	 * @return The date time value matching to this index, an InvalidValueTypeException is thrown if the value is not of
108
	 *         type date time
109
	 */
110
	Date getDateTimeValue(int index);
111

  
112
	/**
113
	 * returns all values as a List of Strings.
114
	 * 
115
	 * @throws InvalidValueTypeException
116
	 *             if not all contained values are strings.
117
	 */
118
	List<String> asStrings();
119

  
120
	/**
121
	 * returns all values as a List of Long.
122
	 * 
123
	 * @throws InvalidValueTypeException
124
	 *             if not all contained values are Longs.
125
	 */
126
	List<Long> asLongs();
127

  
128
}
0 129

  
modules/dnet-index-client/branches/solr75/src/main/java/eu/dnetlib/functionality/index/model/DataFactory.java
1
package eu.dnetlib.functionality.index.model;
2

  
3
import java.util.Date;
4

  
5
import eu.dnetlib.functionality.index.model.Any.ValueType;
6

  
7
/**
8
 * Interface for creation of data objects.
9
 */
10
public interface DataFactory {
11

  
12
	/** default instance. */
13
	DataFactory DEFAULT = DataFactoryCreator.newInstance();
14

  
15
	/**
16
	 * @return The created AnyMap object
17
	 */
18
	AnyMap createAnyMap();
19

  
20
	/**
21
	 * @return The created AnySeq object
22
	 */
23
	AnySeq createAnySeq();
24

  
25
	/**
26
	 * @param value
27
	 *            the String to create the Value from.
28
	 * @return The created Value object.
29
	 */
30
	Value createStringValue(String value);
31

  
32
	/**
33
	 * @param value
34
	 *            the Boolean to create the Value from.
35
	 * @return The created Value object.
36
	 */
37
	Value createBooleanValue(Boolean value);
38

  
39
	/**
40
	 * @param value
41
	 *            the Long to create the Value from.
42
	 * @return The created Value object.
43
	 */
44
	Value createLongValue(Long value);
45

  
46
	/**
47
	 * @param value
48
	 *            the int to create the Value from.
49
	 * @return The created Value object.
50
	 */
51
	Value createLongValue(int value);
52

  
53
	/**
54
	 * @param value
55
	 *            the Double to create the Value from.
56
	 * @return The created Value object.
57
	 */
58
	Value createDoubleValue(Double value);
59

  
60
	/**
61
	 * @param value
62
	 *            the float to create the Value from.
63
	 * @return The created Value object.
64
	 */
65
	Value createDoubleValue(float value);
66

  
67
	/**
68
	 * @param value
69
	 *            the Date to create the Value from.
70
	 * @return The created Value object.
71
	 */
72
	Value createDateValue(Date value);
73

  
74
	/**
75
	 * @param value
76
	 *            the DateTime to create the Value from.
77
	 * @return The created Value object.
78
	 */
79
	Value createDateTimeValue(Date value);
80

  
81
	/**
82
	 * @param value
83
	 *            The value
84
	 * @param type
85
	 *            The type
86
	 * @return The Value object with correct type, InvalidvalueTypeException else.
87
	 */
88
	Value parseFromString(String value, String type);
89

  
90
	/**
91
	 * @param value
92
	 *            The value
93
	 * @param valueType
94
	 *            The value's type
95
	 * @return The Value object with correct type, InvalidvalueTypeException else.
96
	 */
97
	Value parseFromString(final String value, final ValueType valueType);
98

  
99
	/**
100
	 * Tries to convert the String to a Date or Timestamp Value, if not possible return a String Value.
101
	 * 
102
	 * @param value
103
	 *            The value to check for Date/Timestamp
104
	 * @return The Value object with guessed type, InvalidvalueTypeException if value is null.
105
	 */
106
	Value tryDateTimestampParsingFromString(String value);
107

  
108
	/**
109
	 * @param object
110
	 *            The object
111
	 * @return The value matching the class of given object, InvalidValueTypeException otherwise.
112
	 * @deprecated Use {@link #autoConvertValue(Object)} instead
113
	 */
114
	@Deprecated
115
	Value parseFromObject(final Object object);
116

  
117
	/**
118
	 * auto converts the given object into the object's corresponding Value.
119
	 * 
120
	 * @param object
121
	 *            The object, must be one of the simple types
122
	 * @return The value matching the class of given object, InvalidValueTypeException otherwise.
123
	 * 
124
	 */
125
	Value autoConvertValue(final Object object);
126

  
127
	/**
128
	 * Clone Any object.
129
	 * 
130
	 * @param source
131
	 *            the source
132
	 * 
133
	 * @return the attribute
134
	 */
135
	Any cloneAny(final Any source);
136

  
137
	/**
138
	 * Clone AnyMap object.
139
	 * 
140
	 * @param source
141
	 *            the source
142
	 * 
143
	 * @return the attribute
144
	 */
145
	AnyMap cloneAnyMap(final AnyMap source);
146

  
147
	/**
148
	 * Clone AnySeq object.
149
	 * 
150
	 * @param source
151
	 *            the source
152
	 * 
153
	 * @return the attribute
154
	 */
155
	AnySeq cloneAnySeq(final AnySeq source);
156

  
157
}
modules/dnet-index-client/branches/solr75/src/main/java/eu/dnetlib/functionality/index/model/DataFactoryCreator.java
1
package eu.dnetlib.functionality.index.model;
2

  
3
import eu.dnetlib.functionality.index.model.impl.DefaultDataFactoryImpl;
4

  
5
/**
6
 * Helper class to decouple Any and Record interfaces better from default implementation.
7
 */
8
public final class DataFactoryCreator {
9

  
10
	/**
11
	 * Private default constructor to avoid instance creation.
12
	 */
13
	private DataFactoryCreator() {
14
	}
15

  
16
	/**
17
	 * @return instance of the default DataFactory.
18
	 */
19
	public static DataFactory newInstance() {
20
		return new DefaultDataFactoryImpl();
21
	}
22
}
modules/dnet-index-client/branches/solr75/src/main/java/eu/dnetlib/functionality/index/model/impl/AnySeqImpl.java
1
package eu.dnetlib.functionality.index.model.impl;
2

  
3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.Date;
6
import java.util.Iterator;
7
import java.util.List;
8
import java.util.ListIterator;
9

  
10
import eu.dnetlib.functionality.index.model.Any;
11
import eu.dnetlib.functionality.index.model.AnyMap;
12
import eu.dnetlib.functionality.index.model.AnySeq;
13
import eu.dnetlib.functionality.index.model.InvalidValueTypeException;
14
import eu.dnetlib.functionality.index.model.Value;
15

  
16
/**
17
 * Sequence of Any objects.
18
 */
19
public final class AnySeqImpl extends AbstractAny implements AnySeq {
20

  
21
	/** version. */
22
	private static final long serialVersionUID = 1L;
23

  
24
	/** holds the Any objects of this AnySeq. */
25
	private final List<Any> _anyList;
26

  
27
	/**
28
	 * constructs a new instance of AnySeqImpl.
29
	 */
30
	AnySeqImpl() {
31
		super(ValueType.SEQ);
32
		_anyList = new ArrayList<Any>();
33
	}
34

  
35
	/** {@inheritDoc} */
36
	@Override
37
	public boolean add(final Any value) {
38
		if (value == null) {
39
			throw new IllegalArgumentException("The value of any Any must not be null.");
40
		}
41
		return _anyList.add(value);
42
	}
43

  
44
	/** {@inheritDoc} */
45
	@Override
46
	public boolean add(final String e) {
47
		return add(new ValueImpl(ValueType.STRING, e));
48
	}
49

  
50
	/** {@inheritDoc} */
51
	@Override
52
	public boolean add(final Number n) {
53
		if (n instanceof Double) {
54
			return add(new ValueImpl(ValueType.DOUBLE, n));
55
		} else if (n instanceof Long) {
56
			return add(new ValueImpl(ValueType.LONG, n));
57
		} else if (n instanceof Integer || n instanceof Short || n instanceof Byte) {
58
			return add(new ValueImpl(ValueType.LONG, Long.valueOf(n.longValue())));
59
		} else { // default: DOUBLE
60
			return add(new ValueImpl(ValueType.DOUBLE, Double.valueOf(n.doubleValue())));
61
		}
62
	}
63

  
64
	/** {@inheritDoc} */
65
	@Override
66
	public void add(final int index, final Any element) {
67
		if (element == null) {
68
			throw new IllegalArgumentException("The value of any Any must not be null.");
69
		}
70
		_anyList.add(index, element);
71
	}
72

  
73
	/** {@inheritDoc} */
74
	@Override
75
	public boolean addAll(final Collection<? extends Any> c) {
76
		return _anyList.addAll(c);
77
	}
78

  
79
	/** {@inheritDoc} */
80
	@Override
81
	public boolean addAll(final int index, final Collection<? extends Any> c) {
82
		return _anyList.addAll(index, c);
83
	}
84

  
85
	/** {@inheritDoc} */
86
	@Override
87
	public void clear() {
88
		_anyList.clear();
89
	}
90

  
91
	/** {@inheritDoc} */
92
	@Override
93
	public boolean contains(final Object o) {
94
		return _anyList.contains(o);
95
	}
96

  
97
	/** {@inheritDoc} */
98
	@Override
99
	public boolean containsAll(final Collection<?> c) {
100
		return _anyList.containsAll(c);
101
	}
102

  
103
	/** {@inheritDoc} */
104
	@Override
105
	public Any get(final int index) {
106
		return _anyList.get(index);
107
	}
108

  
109
	/** {@inheritDoc} */
110
	@Override
111
	public Any set(final int index, final Any element) {
112
		if (element == null) {
113
			throw new IllegalArgumentException("The value of any Any must not be null.");
114
		}
115
		return _anyList.set(index, element);
116
	}
117

  
118
	/** {@inheritDoc} */
119
	@Override
120
	public int indexOf(final Object o) {
121
		return _anyList.indexOf(o);
122
	}
123

  
124
	/** {@inheritDoc} */
125
	@Override
126
	public boolean isEmpty() {
127
		return _anyList.isEmpty();
128
	}
129

  
130
	/** {@inheritDoc} */
131
	@Override
132
	public Iterator<Any> iterator() {
133
		return _anyList.iterator();
134
	}
135

  
136
	/** {@inheritDoc} */
137
	@Override
138
	public int lastIndexOf(final Object o) {
139
		return _anyList.lastIndexOf(o);
140
	}
141

  
142
	/** {@inheritDoc} */
143
	@Override
144
	public ListIterator<Any> listIterator() {
145
		return _anyList.listIterator();
146
	}
147

  
148
	/** {@inheritDoc} */
149
	@Override
150
	public ListIterator<Any> listIterator(final int index) {
151
		return _anyList.listIterator(index);
152
	}
153

  
154
	/** {@inheritDoc} */
155
	@Override
156
	public Any remove(final int index) {
157
		return _anyList.remove(index);
158
	}
159

  
160
	/** {@inheritDoc} */
161
	@Override
162
	public boolean remove(final Object o) {
163
		return _anyList.remove(o);
164
	}
165

  
166
	/** {@inheritDoc} */
167
	@Override
168
	public boolean removeAll(final Collection<?> c) {
169
		return _anyList.removeAll(c);
170
	}
171

  
172
	/** {@inheritDoc} */
173
	@Override
174
	public boolean retainAll(final Collection<?> c) {
175
		return _anyList.retainAll(c);
176
	}
177

  
178
	/** {@inheritDoc} */
179
	@Override
180
	public int size() {
181
		return _anyList.size();
182
	}
183

  
184
	/** {@inheritDoc} */
185
	@Override
186
	public List<Any> subList(final int fromIndex, final int toIndex) {
187
		return _anyList.subList(fromIndex, toIndex);
188
	}
189

  
190
	/** {@inheritDoc} */
191
	@Override
192
	public Object[] toArray() {
193
		return _anyList.toArray();
194
	}
195

  
196
	/** {@inheritDoc} */
197
	@Override
198
	public <T> T[] toArray(final T[] a) {
199
		return _anyList.toArray(a);
200
	}
201

  
202
	/** {@inheritDoc} */
203
	@Override
204
	public AnyMap getMap(final int index) {
205
		final Any anyValue = get(index);
206
		if (anyValue != null) {
207
			if (anyValue.isMap()) {
208
				return (AnyMap) anyValue;
209
			} else {
210
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to AnyMap.");
211
			}
212
		}
213
		return null;
214
	}
215

  
216
	/** {@inheritDoc} */
217
	@Override
218
	public AnySeq getSeq(final int index) {
219
		final Any anyValue = get(index);
220
		if (anyValue != null) {
221
			if (anyValue.isSeq()) {
222
				return (AnySeq) anyValue;
223
			} else {
224
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to AnySeq.");
225
			}
226
		}
227
		return null;
228
	}
229

  
230
	/** {@inheritDoc} */
231
	@Override
232
	public Value getValue(final int index) {
233
		final Any anyValue = get(index);
234
		if (anyValue != null) {
235
			if (anyValue instanceof Value) {
236
				return (Value) anyValue;
237
			} else {
238
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to Value.");
239
			}
240
		}
241
		return null;
242
	}
243

  
244
	/** {@inheritDoc} */
245
	@Override
246
	public String getStringValue(final int index) {
247
		final Any anyValue = get(index);
248
		if (anyValue != null) {
249
			if (anyValue instanceof Value) {
250
				return ((Value) anyValue).asString();
251
			} else {
252
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to String.");
253
			}
254
		}
255
		return null;
256
	}
257

  
258
	/** {@inheritDoc} */
259
	@Override
260
	public Double getDoubleValue(final int index) {
261
		final Any anyValue = get(index);
262
		if (anyValue != null) {
263
			if (anyValue instanceof Value) {
264
				return ((Value) anyValue).asDouble();
265
			} else {
266
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to double.");
267
			}
268
		}
269
		return null;
270
	}
271

  
272
	/** {@inheritDoc} */
273
	@Override
274
	public Long getLongValue(final int index) {
275
		final Any anyValue = get(index);
276
		if (anyValue != null) {
277
			if (anyValue instanceof Value) {
278
				return ((Value) anyValue).asLong();
279
			} else {
280
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to long.");
281
			}
282
		}
283
		return null;
284
	}
285

  
286
	/** {@inheritDoc} */
287
	@Override
288
	public Boolean getBooleanValue(final int index) {
289
		final Any anyValue = get(index);
290
		if (anyValue != null) {
291
			if (anyValue instanceof Value) {
292
				return ((Value) anyValue).asBoolean();
293
			} else {
294
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to boolean.");
295
			}
296
		}
297
		return null;
298
	}
299

  
300
	/** {@inheritDoc} */
301
	@Override
302
	public Date getDateValue(final int index) {
303
		final Any anyValue = get(index);
304
		if (anyValue != null) {
305
			if (anyValue instanceof Value) {
306
				return ((Value) anyValue).asDate();
307
			} else {
308
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to Date.");
309
			}
310
		}
311
		return null;
312
	}
313

  
314
	/** {@inheritDoc} */
315
	@Override
316
	public Date getDateTimeValue(final int index) {
317
		final Any anyValue = get(index);
318
		if (anyValue != null) {
319
			if (anyValue instanceof Value) {
320
				return ((Value) anyValue).asDateTime();
321
			} else {
322
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to DateTime.");
323
			}
324
		}
325
		return null;
326
	}
327

  
328
	/** {@inheritDoc} */
329
	@Override
330
	public int hashCode() {
331
		final int prime = 31;
332
		int result = 1;
333
		result = prime * result + (_anyList == null ? 0 : _anyList.hashCode());
334
		return result;
335
	}
336

  
337
	/** {@inheritDoc} */
338
	@Override
339
	public boolean equals(final Object obj) {
340
		if (this == obj) {
341
			return true;
342
		}
343
		if (obj == null) {
344
			return false;
345
		}
346
		if (!(obj instanceof AnySeqImpl)) {
347
			return false;
348
		}
349
		final AnySeqImpl other = (AnySeqImpl) obj;
350
		if (_anyList == null) {
351
			if (other._anyList != null) {
352
				return false;
353
			}
354
		} else if (!_anyList.equals(other._anyList)) {
355
			return false;
356
		}
357
		return true;
358
	}
359

  
360
	/**
361
	 * {@inheritDoc}
362
	 */
363
	@Override
364
	public String toString() {
365
		return _anyList.toString();
366
	}
367

  
368
	@Override
369
	public AnySeq asSeq() {
370
		return this;
371
	}
372

  
373
	@Override
374
	public List<String> asStrings() {
375
		final List<String> values = new ArrayList<String>(_anyList.size());
376
		for (final Any any : _anyList) {
377
			values.add(any.asValue().asString());
378
		}
379
		return values;
380
	}
381

  
382
	@Override
383
	public List<Long> asLongs() {
384
		final List<Long> values = new ArrayList<Long>(_anyList.size());
385
		for (final Any any : _anyList) {
386
			values.add(any.asValue().asLong());
387
		}
388
		return values;
389
	}
390

  
391
}
0 392

  
modules/dnet-index-client/branches/solr75/src/main/java/eu/dnetlib/functionality/index/model/impl/AnyMapImpl.java
1
package eu.dnetlib.functionality.index.model.impl;
2

  
3
import java.util.Collection;
4
import java.util.Date;
5
import java.util.Iterator;
6
import java.util.LinkedHashMap;
7
import java.util.Map;
8
import java.util.Set;
9

  
10
import eu.dnetlib.functionality.index.model.Any;
11
import eu.dnetlib.functionality.index.model.AnyMap;
12
import eu.dnetlib.functionality.index.model.AnySeq;
13
import eu.dnetlib.functionality.index.model.InvalidValueTypeException;
14
import eu.dnetlib.functionality.index.model.Value;
15

  
16
/**
17
 * Class implementing AnyMap.
18
 */
19
public final class AnyMapImpl extends AbstractAny implements AnyMap {
20

  
21
	/** version. */
22
	private static final long serialVersionUID = 1L;
23

  
24
	/** internal representation of the AnyMap. */
25
	private final Map<String, Any> _anyMap;
26

  
27
	/** Constructs a new AnyMapImpl. */
28
	AnyMapImpl() {
29
		super(ValueType.MAP);
30
		_anyMap = new LinkedHashMap<String, Any>();
31
	}
32

  
33
	/** {@inheritDoc} */
34
	@Override
35
	public void add(final String key, final Any value) {
36
		final Any any = _anyMap.get(key);
37
		AnySeq anySeq = null;
38
		if (any == null) {
39
			anySeq = getFactory().createAnySeq();
40
			_anyMap.put(key, anySeq);
41
		} else if (any.isValue() || any.isMap()) {
42
			anySeq = getFactory().createAnySeq();
43
			anySeq.add(any);
44
			_anyMap.put(key, anySeq);
45
		} else { // any.isSeq()
46
			anySeq = (AnySeq) any;
47
		}
48
		anySeq.add(value);
49
	}
50

  
51
	/** {@inheritDoc} */
52
	@Override
53
	public void clear() {
54
		_anyMap.clear();
55
	}
56

  
57
	/** {@inheritDoc} */
58
	@Override
59
	public boolean isEmpty() {
60
		return _anyMap.isEmpty();
61
	}
62

  
63
	/** {@inheritDoc} */
64
	@Override
65
	public Set<String> keySet() {
66
		return _anyMap.keySet();
67
	}
68

  
69
	/** {@inheritDoc} */
70
	@Override
71
	public Any put(final String key, final String value) {
72
		try {
73
			return put(key, new ValueImpl(ValueType.STRING, value));
74
		} catch (final Exception e) {
75
			throw new IllegalArgumentException("cannot add value for key: " + key, e);
76
		}
77
	}
78

  
79
	/** {@inheritDoc} */
80
	@Override
81
	public Any put(final String key, final Number value) {
82
		if (value == null) {
83
			throw new IllegalArgumentException("The value of any Any must not be null.");
84
		}
85
		if (value instanceof Double) {
86
			return put(key, new ValueImpl(ValueType.DOUBLE, value));
87
		} else if (value instanceof Long) {
88
			return put(key, new ValueImpl(ValueType.LONG, value));
89
		} else if (value instanceof Integer) {
90
			return put(key, new ValueImpl(ValueType.LONG, Long.valueOf(value.longValue())));
91
		} else if (value instanceof Short) {
92
			return put(key, new ValueImpl(ValueType.LONG, Long.valueOf(value.longValue())));
93
		} else if (value instanceof Byte) {
94
			return put(key, new ValueImpl(ValueType.LONG, Long.valueOf(value.longValue())));
95
		} else { // default: DOUBLE
96
			return put(key, new ValueImpl(ValueType.DOUBLE, Double.valueOf(value.doubleValue())));
97
		}
98
	}
99

  
100
	/** {@inheritDoc} */
101
	@Override
102
	public Any put(final String key, final Boolean value) {
103
		return put(key, new ValueImpl(ValueType.BOOLEAN, value));
104
	}
105

  
106
	/** {@inheritDoc} */
107
	@Override
108
	public Any put(final String key, final Any value) {
109
		if (value == null) {
110
			throw new IllegalArgumentException("The value of any Any must not be null.");
111
		}
112
		return _anyMap.put(key, value);
113
	}
114

  
115
	/** {@inheritDoc} */
116
	@Override
117
	public int size() {
118
		return _anyMap.size();
119
	}
120

  
121
	/** {@inheritDoc} */
122
	@Override
123
	public Collection<Any> values() {
124
		return _anyMap.values();
125
	}
126

  
127
	/** {@inheritDoc} */
128
	@Override
129
	public Iterator<Any> iterator() {
130
		return _anyMap.values().iterator();
131
	}
132

  
133
	/** {@inheritDoc} */
134
	@Override
135
	public AnyMap getMap(final String key) {
136
		final Any anyValue = get(key);
137
		if (anyValue != null) {
138
			if (anyValue.isMap()) {
139
				return (AnyMap) anyValue;
140
			} else {
141
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to AnyMap.");
142
			}
143
		}
144
		return null;
145
	}
146

  
147
	/** {@inheritDoc} */
148
	@Override
149
	public AnySeq getSeq(final String key) {
150
		final Any anyValue = get(key);
151
		if (anyValue != null) {
152
			if (anyValue.isSeq()) {
153
				return (AnySeq) anyValue;
154
			} else {
155
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to AnySeq.");
156
			}
157
		}
158
		return null;
159
	}
160

  
161
	/** {@inheritDoc} */
162
	@Override
163
	public Value getValue(final String key) {
164
		final Any anyValue = get(key);
165
		if (anyValue != null) {
166
			if (anyValue instanceof Value) {
167
				return (Value) anyValue;
168
			} else {
169
				throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to Value.");
170
			}
171
		}
172
		return null;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff