Project

General

Profile

« Previous | Next » 

Revision 56035

include synonyms in the published information

View differences:

modules/dnet-vocabulary-publisher/trunk/src/main/java/eu/dnetlib/vocabularies/publisher/VocabularyRetriever.java
1 1
package eu.dnetlib.vocabularies.publisher;
2 2

  
3
import java.io.StringReader;
3 4
import java.util.List;
4 5

  
6
import eu.dnetlib.vocabularies.publisher.model.Synonym;
5 7
import org.apache.commons.logging.Log;
6 8
import org.apache.commons.logging.LogFactory;
9
import org.dom4j.Document;
10
import org.dom4j.DocumentException;
11
import org.dom4j.Element;
12
import org.dom4j.io.SAXReader;
7 13
import org.springframework.beans.factory.annotation.Autowired;
8 14
import org.springframework.cache.annotation.Cacheable;
9 15

  
......
31 37

  
32 38
	@Cacheable(value = "terms", key = "#id")
33 39
	public Vocabulary getVocabulary(final String id) throws VocabularyNotFoundException {
34
		log.debug("getVocabulary(): not using cache");
40
		log.warn(String.format("getVocabulary(%s): not using cache", id));
35 41
		// get the vocabulary info:
36 42
		String queryVoc = "let $x := /*[.//RESOURCE_IDENTIFIER/@value='"
37 43
				+ id
......
45 51

  
46 52
	@Cacheable(value = "termsByCode", key = "#code")
47 53
	public Vocabulary getVocabularyByCode(final String code) throws VocabularyNotFoundException {
48
		log.debug("getVocabularyByCode(): not using cache");
54
		log.warn(String.format("getVocabularyByCode(%s): not using cache", code));
49 55
		String queryVoc = "let $x := //RESOURCE_PROFILE[.//VOCABULARY_NAME/@code='"
50 56
				+ code
51 57
				+ "']"
......
55 61
		else return voc;
56 62
	}
57 63

  
64
	@Cacheable(value = "synonymsByCode", key = "#termCode")
65
	public VocabularyTerm getTermSynonyms(final String vocabularyCode, final String termCode) throws VocabularyNotFoundException {
66
		log.warn(String.format("getTermSynonyms(%s, %s): not using cache", vocabularyCode, termCode));
67
		final Vocabulary voc = getVocabularyByCode(vocabularyCode);
68

  
69
		final VocabularyTerm vt = voc.getTerms().stream()
70
				.filter(t -> t.getCode().equalsIgnoreCase(termCode))
71
				.findFirst()
72
				.get();
73

  
74
		String querySynonyms =
75
				String.format("for $x in /*[.//RESOURCE_IDENTIFIER/@value='%s']//TERM[./@code = '%s']//SYNONYM " +
76
						"return concat ($x/@term,':-:',$x/@encoding)", voc.getId(), termCode);
77

  
78
		vt.setSynonyms(Lists.newArrayList(queryExecutor.query(Synonym.class, querySynonyms)));
79

  
80
		return vt;
81
	}
82

  
58 83
	private Vocabulary getVocabularyByQuery(final String query) {
59 84
		Iterable<Vocabulary> vocs = this.queryExecutor.query(Vocabulary.class, query);
60 85
		if (vocs.iterator().hasNext()) {
......
69 94
		} else return null;
70 95
	}
71 96

  
97

  
98
	/*
99
	private Vocabulary getTermByQuery(final String query) throws DocumentException {
100
		Iterable<Vocabulary> vocs = this.queryExecutor.query(Vocabulary.class, query);
101
		if (vocs.iterator().hasNext()) {
102
			Vocabulary vocabulary = vocs.iterator().next();
103
			if (vocabulary.getId() == null) return null;
104
			// now the terms
105
			String queryTerms = String.format(
106
					"for $x in collection('/db/DRIVER/VocabularyDSResources/VocabularyDSResourceType') \n" +
107
							"where $x[./RESOURCE_PROFILE/HEADER/RESOURCE_IDENTIFIER/@value/string() = '%s']\n" +
108
							"return $x/RESOURCE_PROFILE/BODY/CONFIGURATION", vocabulary.getId());
109

  
110
			List<String> vocabProfiles = queryExecutor.performQuery(queryTerms);
111
			if (vocabProfiles != null && vocabProfiles.size() == 1) {
112
				final String termsXml = vocabProfiles.get(0);
113
				log.debug("got terms: " + termsXml);
114

  
115
				final Document doc = new SAXReader().read(new StringReader(termsXml));
116

  
117
				final List<VocabularyTerm> terms = Lists.newArrayList();
118
				for(Object t : doc.selectNodes("//TERM")) {
119
					final Element term = (Element) t;
120
					final List<Synonym> synonyms = Lists.newArrayList();
121
					for(Object s : term.selectNodes("//SYNONYM")) {
122
						Element syn = (Element) s;
123
						synonyms.add(new Synonym(syn.valueOf("./@term"), syn.valueOf("./@encoding")));
124
					}
125
					terms.add(new VocabularyTerm(
126
							term.valueOf("./@english_name"),
127
							term.valueOf("./@native_name"),
128
							term.valueOf("./@encoding"),
129
							term.valueOf("./@code"),
130
							synonyms));
131
				}
132
				vocabulary.setTerms(terms);
133
				return vocabulary;
134
			} else {
135
				throw new IllegalStateException(String.format("found more than one Vocabulary with id '%s'", vocabulary.getId()));
136
			}
137

  
138
		} else return null;
139
	}
140
	*/
141

  
72 142
}
modules/dnet-vocabulary-publisher/trunk/src/main/java/eu/dnetlib/vocabularies/publisher/model/VocabularyTerm.java
1 1
package eu.dnetlib.vocabularies.publisher.model;
2 2

  
3 3
import java.io.Serializable;
4
import java.util.List;
4 5

  
5 6
public class VocabularyTerm implements Serializable {
6 7

  
......
9 10
	private String nativeName;
10 11
	private String encoding;
11 12
	private String code;
13
	private List<Synonym> synonyms;
12 14

  
13 15
	public String getEnglishName() {
14 16
		return englishName;
......
46 48
		super();
47 49
	}
48 50

  
51
	public List<Synonym> getSynonyms() {
52
		return synonyms;
53
	}
54

  
55
	public void setSynonyms(List<Synonym> synonyms) {
56
		this.synonyms = synonyms;
57
	}
58

  
49 59
	/**
50 60
	 * This constructor is used by the SplittedQueryExecutor for the retrieval of terms from the IS when called by Vocabularyretriever.
51 61
	 * 
......
64 74

  
65 75
	@Override
66 76
	public String toString() {
67
		return "VocabularyTerm [englishName=" + englishName + ", nativeName=" + nativeName + ", encoding=" + encoding + ", code=" + code + "]";
77
		return "VocabularyTerm [englishName=" + englishName + ", nativeName=" + nativeName + ", encoding=" + encoding + ", code=" + code + ", synonyms=" + synonyms + "]";
68 78
	}
69 79

  
70 80
}
modules/dnet-vocabulary-publisher/trunk/src/main/java/eu/dnetlib/vocabularies/publisher/model/Synonym.java
1
package eu.dnetlib.vocabularies.publisher.model;
2

  
3
import java.io.Serializable;
4

  
5
public class Synonym implements Serializable {
6

  
7
    private static final long serialVersionUID = -1661921400669796428L;
8

  
9
    private String term;
10
    private String encoding;
11

  
12
    public Synonym() {
13

  
14
    }
15

  
16
    public Synonym(String term, String encoding) {
17
        this.term = term;
18
        this.encoding = encoding;
19
    }
20

  
21
    public String getTerm() {
22
        return term;
23
    }
24

  
25
    public void setTerm(String term) {
26
        this.term = term;
27
    }
28

  
29
    public String getEncoding() {
30
        return encoding;
31
    }
32

  
33
    public void setEncoding(String encoding) {
34
        this.encoding = encoding;
35
    }
36
}
modules/dnet-vocabulary-publisher/trunk/src/main/java/eu/dnetlib/vocabularies/publisher/controller/VocabularyPublisherController.java
7 7
import eu.dnetlib.vocabularies.publisher.VocabularyNotFoundException;
8 8
import eu.dnetlib.vocabularies.publisher.VocabularyRetriever;
9 9
import eu.dnetlib.vocabularies.publisher.model.Vocabulary;
10
import eu.dnetlib.vocabularies.publisher.model.VocabularyTerm;
10 11
import org.apache.commons.logging.Log;
11 12
import org.apache.commons.logging.LogFactory;
13
import org.dom4j.DocumentException;
12 14
import org.springframework.beans.factory.annotation.Value;
13 15
import org.springframework.stereotype.Controller;
14 16
import org.springframework.ui.ModelMap;
......
44 46
		return this.vocabularyRetriever.getVocabularyByCode(code);
45 47
	}
46 48

  
49
	@RequestMapping(value = "/vocabularies/{code}/{term}", method = RequestMethod.GET, produces = { "application/json" })
50
	public @ResponseBody
51
	VocabularyTerm getTermSynonyms(@PathVariable("code") final String code,
52
									 @PathVariable("term") final String term) throws VocabularyNotFoundException {
53
		log.debug(String.format("getVocabulary with code = %s, term = %s", code, term));
54
		return this.vocabularyRetriever.getTermSynonyms(code, term);
55
	}
56

  
47 57
	// View-based methods for html responses
48 58
	@RequestMapping(value = "/vocabularies", method = RequestMethod.GET, produces = { "text/html" })
49 59
	public void listVocabularies(final ModelMap map) {
......
61 71
		return "displayVocabulary";
62 72
	}
63 73

  
74
	@RequestMapping(value = "/vocabularies/{code}/{term}", method = RequestMethod.GET, produces = { "text/html" })
75
	public String getTermSynonyms(
76
			@PathVariable("code") final String code,
77
			@PathVariable("term") final String term,
78
			final ModelMap map) throws VocabularyNotFoundException {
79
		log.debug(String.format("getVocabulary with code = %s, term = %s", code, term));
80

  
81
		map.addAttribute("vocabulary", getVocabulary(code));
82
		map.addAttribute("term", getTermSynonyms(code, term));
83
		map.addAttribute("baseURL", baseURL);
84
		map.addAttribute("context", context);
85
		return "displaySynonyms";
86
	}
87

  
64 88
}
modules/dnet-vocabulary-publisher/trunk/src/main/resources/eu/dnetlib/vocabularies/publisher/cache/ehcache.xml
37 37
	<cache name="termsByCode" maxElementsInMemory="300" eternal="false"
38 38
		timeToIdleSeconds="1800" timeToLiveSeconds="1800" overflowToDisk="false" />
39 39

  
40
	<cache name="synonymsByCode" maxElementsInMemory="1500" eternal="false"
41
		   timeToIdleSeconds="1800" timeToLiveSeconds="1800" overflowToDisk="false" />
42

  
40 43
</ehcache>
41 44

  
modules/dnet-vocabulary-publisher/trunk/src/main/resources/eu/dnetlib/vocabularies/views/displayVocabulary.st
14 14
			</thead>
15 15
			<tbody>
16 16
			$vocabulary.terms:{ term | 
17
				<tr><td>$term.englishName$</td><td>$term.code$</td></tr>
17
				<tr>
18
				    <td>$term.englishName$</td><td><a class="btn btn-link" href="$baseURL$/$vocabulary.code$/$term.code$" role="button">$term.code$</a></td>
19
				</tr>
18 20
			}$
19 21
			</tbody>
20 22
		</table>
modules/dnet-vocabulary-publisher/trunk/src/main/resources/eu/dnetlib/vocabularies/views/displaySynonyms.st
1
$master( title={$vocabulary.name$},
2
navbars={
3
	<a class="navbar-brand" href="$baseURL$/$vocabulary.code$">$vocabulary.name$/$term.englishName$/$term.code$ </a>
4
	},
5
	content = {
6
	<div><h4>Term: $term.englishName$</h4></div>
7
	<div><h4>Code: $term.code$</h4></div>
8

  
9
		<hr>
10
		<div class="table-responsive">
11
		<table class="table table-condensed">
12
    		<thead>
13
				<tr><th>Synonym</th><th>Encoding</th></tr>
14
			</thead>
15
			<tbody>
16
			$term.synonyms:{ syn |
17
				<tr>
18
				    <td>$syn.term$</td><td>$syn.encoding$</td>
19
				</tr>
20
			}$
21
			</tbody>
22
		</table>
23
		</div>
24
 		<hr>
25
	})$
26

  

Also available in: Unified diff