Project

General

Profile

1
package eu.dnetlib.msro.openaireplus.workflows.index;
2

    
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import com.fasterxml.jackson.databind.ObjectWriter;
5
import com.google.common.collect.Maps;
6
import eu.dnetlib.functionality.index.model.document.IndexDocument;
7
import eu.dnetlib.functionality.index.solr.feed.InputDocumentFactory;
8
import eu.dnetlib.functionality.index.solr.feed.StreamingInputDocumentFactory;
9
import eu.dnetlib.miscutils.datetime.DateUtils;
10
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
11
import eu.dnetlib.miscutils.functional.xml.IndentXmlString;
12
import org.apache.commons.io.IOUtils;
13
import org.apache.solr.common.SolrInputDocument;
14
import org.dom4j.Document;
15
import org.dom4j.DocumentException;
16
import org.dom4j.Node;
17
import org.dom4j.io.SAXReader;
18
import org.junit.Test;
19
import org.springframework.core.io.ClassPathResource;
20

    
21
import javax.xml.stream.XMLStreamException;
22
import javax.xml.transform.Transformer;
23
import javax.xml.transform.TransformerException;
24
import javax.xml.transform.TransformerFactory;
25
import javax.xml.transform.stream.StreamResult;
26
import javax.xml.transform.stream.StreamSource;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.StringReader;
30
import java.io.StringWriter;
31
import java.text.SimpleDateFormat;
32
import java.util.Map;
33

    
34
import static org.junit.Assert.assertFalse;
35
import static org.junit.Assert.assertNotNull;
36

    
37
public class OpenaireLayoutToRecordStylesheetTest {
38

    
39
	private static final String OPENAIRE_LAYOUT_TO_RECORD_STYLESHEET_XSL = "/eu/dnetlib/msro/openaireplus/workflows/index/openaireLayoutToRecordStylesheet.xsl";
40

    
41
	private static final String MDFORMAT_FIELDS_PROFILE = "/eu/dnetlib/test/profiles/MDFormatDSResources/MDFormatDSResourceType/2-8b9503d9-8a86-4330-93ef-7e0cd9bc87c2.xml";
42

    
43
	private static final String OAF_RECORD = "/eu/dnetlib/msro/openaireplus/workflows/index/oafRecord.xml";
44

    
45
	private final static String[] dateFormats = { "yyyy-MM-dd'T'hh:mm:ss", "yyyy-MM-dd", "dd-MM-yyyy", "dd/MM/yyyy", "yyyy" };
46

    
47
	@Test
48
	public void testConvertRecord() throws IOException, TransformerException, XMLStreamException, DocumentException {
49
		String xsl = prepareXslt("DMF");
50
		assertNotNull(xsl);
51
		assertFalse(xsl.isEmpty());
52

    
53
		System.out.println(xsl);
54

    
55
		ApplyXslt xslt = new ApplyXslt(xsl);
56

    
57
		String indexRecord = xslt.evaluate(readFromClasspath(OAF_RECORD));
58

    
59
		assertNotNull(indexRecord);
60
		assertFalse(indexRecord.isEmpty());
61

    
62
		System.out.println(IndentXmlString.apply(indexRecord));
63

    
64
		StreamingInputDocumentFactory factory = new StreamingInputDocumentFactory();
65

    
66
		SolrInputDocument doc = factory.parseDocument(DateUtils.now_ISO8601(), indexRecord, "dsId", "dnetResult");
67
		assertNotNull(doc);
68
		assertFalse(doc.isEmpty());
69

    
70
		System.out.println("SolrDocument");
71

    
72
		System.out.println(doc.values());
73

    
74
		System.out.println("SolrDocument fields");
75

    
76
		Map<String, Object> fields = Maps.newHashMap();
77

    
78
		doc.entrySet().stream()
79
				.forEach(e -> {
80
					Object value = e.getValue().getValueCount() == 1 ? e.getValue().getFirstValue() : e.getValue().getValues();
81
					fields.put(e.getKey(), value);
82
				});
83

    
84
		//new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(fields);
85

    
86
		final ObjectWriter objectWriter = new ObjectMapper()
87
				.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"))
88
				.writerWithDefaultPrettyPrinter();
89

    
90
		System.out.println(objectWriter.writeValueAsString(fields));
91
	}
92

    
93
	protected String prepareXslt(final String format) throws IOException, TransformerException, DocumentException {
94

    
95
		final Transformer layoutTransformer = TransformerFactory.newInstance().newTransformer(streamSource(OPENAIRE_LAYOUT_TO_RECORD_STYLESHEET_XSL));
96

    
97
		final StreamResult result = new StreamResult(new StringWriter());
98

    
99
		layoutTransformer.setParameter("format", format);
100

    
101
		final StreamSource fields = indexFieldsFromProfile(MDFORMAT_FIELDS_PROFILE);
102

    
103
		layoutTransformer.transform(fields , result);
104

    
105
		return result.getWriter().toString();
106
	}
107

    
108
	private StreamSource streamSource(final String s) throws IOException {
109
		return new StreamSource(new StringReader(readFromClasspath(s)));
110
	}
111

    
112
	private String readFromClasspath(final String s) throws IOException {
113
		ClassPathResource resource = new ClassPathResource(s);
114
		InputStream inputStream = resource.getInputStream();
115
		return IOUtils.toString(inputStream);
116
	}
117

    
118
	private StreamSource indexFieldsFromProfile(final String path) throws IOException, DocumentException {
119
		final Document doc = new SAXReader().read(new StringReader(readFromClasspath(path)));
120
		final Node layout = doc.selectSingleNode("//LAYOUT[@name='index']");
121
		return new StreamSource(new StringReader(layout.asXML()));
122
	}
123

    
124
}
125

    
126

    
127

    
    (1-1/1)