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 RESULT = "result";
|
22
|
|
23
|
public static final String INDEX_RESULT = INDEX_FIELD_PREFIX + RESULT;
|
24
|
|
25
|
public static final String INDEX_RECORD_ID = INDEX_FIELD_PREFIX + "indexrecordidentifier";
|
26
|
|
27
|
private static final String outFormat = new String("yyyy-MM-dd'T'hh:mm:ss'Z'");
|
28
|
|
29
|
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");
|
30
|
|
31
|
public abstract SolrInputDocument parseDocument(final String version,
|
32
|
final String inputDocument,
|
33
|
final String dsId,
|
34
|
final String resultName) throws XMLStreamException;
|
35
|
|
36
|
public abstract SolrInputDocument parseDocument(final String version,
|
37
|
final String inputDocument,
|
38
|
final String dsId,
|
39
|
final String resultName,
|
40
|
final ResultTransformer resultTransformer) throws XMLStreamException;
|
41
|
|
42
|
/**
|
43
|
* method return a solr-compatible string representation of a date
|
44
|
*
|
45
|
* @param date
|
46
|
* @return the formatted date string
|
47
|
* @throws DocumentException
|
48
|
* @throws ParseException
|
49
|
*/
|
50
|
public static String getParsedDateField(final String date) {
|
51
|
for (String formatString : dateFormats) {
|
52
|
try {
|
53
|
return new SimpleDateFormat(outFormat).format(new SimpleDateFormat(formatString).parse(date));
|
54
|
} catch (ParseException e) {}
|
55
|
}
|
56
|
throw new IllegalStateException("unable to parse date: " + date);
|
57
|
}
|
58
|
|
59
|
public String parseDate(final String date) {
|
60
|
return getParsedDateField(date);
|
61
|
}
|
62
|
|
63
|
}
|