Skip to content

Commit

Permalink
Merge in IMOS hierarchical facets support
Browse files Browse the repository at this point in the history
  • Loading branch information
jonescc committed Nov 17, 2014
1 parent e27d933 commit e49e6fb
Show file tree
Hide file tree
Showing 93 changed files with 4,459 additions and 831 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ web/src/main/webapp/WEB-INF/data/data/metadata_data
web/src/main/webapp/WEB-INF/data/data/metadata_subversion
web/src/main/webapp/WEB-INF/data/data/resources
web/src/main/webapp/WEB-INF/data/index
web/src/main/webapp/META-INF/MANIFEST.MF
web-itest/jcs_caching
datadir/
*.factorypath
Expand All @@ -58,3 +59,4 @@ e2e-tests/node
e2e-tests/node_modules
web-ui-docs/node
web-ui-docs/node_modules

1 change: 1 addition & 0 deletions core/src/main/java/org/fao/geonet/constants/Geonet.java
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ public static final class CodeList {
public static final String GEOPUBLISH = GEONETWORK + ".geopublisher";
public static final String FORMATTER = GEONETWORK + ".formatter";
public static final String EDITOR_SESSION = GEONETWORK + ".editor.session";
public static final String CLASSIFIER = GEONETWORK + ".classifier";
/**
* Services.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//=============================================================================
//=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
//=== and United Nations Environment Programme (UNEP)
//===
//=== This program is free software; you can redistribute it and/or modify
//=== it under the terms of the GNU General Public License as published by
//=== the Free Software Foundation; either version 2 of the License, or (at
//=== your option) any later version.
//===
//=== This program is distributed in the hope that it will be useful, but
//=== WITHOUT ANY WARRANTY; without even the implied warranty of
//=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//=== General Public License for more details.
//===
//=== You should have received a copy of the GNU General Public License
//=== along with this program; if not, write to the Free Software
//=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//===
//=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
//=== Rome - Italy. email: [email protected]
//==============================================================================

package org.fao.geonet.exceptions;

public class LabelNotFoundException extends RuntimeException {

private static final long serialVersionUID = 1L;

public LabelNotFoundException(String message) {
super(message);
}

public LabelNotFoundException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//=============================================================================
//=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
//=== and United Nations Environment Programme (UNEP)
//===
//=== This program is free software; you can redistribute it and/or modify
//=== it under the terms of the GNU General Public License as published by
//=== the Free Software Foundation; either version 2 of the License, or (at
//=== your option) any later version.
//===
//=== This program is distributed in the hope that it will be useful, but
//=== WITHOUT ANY WARRANTY; without even the implied warranty of
//=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//=== General Public License for more details.
//===
//=== You should have received a copy of the GNU General Public License
//=== along with this program; if not, write to the Free Software
//=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//===
//=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
//=== Rome - Italy. email: [email protected]
//==============================================================================

package org.fao.geonet.exceptions;

public class TermNotFoundException extends RuntimeException {

private static final long serialVersionUID = 1L;

public TermNotFoundException(String message) {
super(message);
}

public TermNotFoundException(String message, Throwable cause) {
super(message, cause);
}
}
27 changes: 27 additions & 0 deletions core/src/main/java/org/fao/geonet/kernel/KeywordBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.eclipse.jetty.util.URIUtil;
import org.fao.geonet.constants.Geonet.Namespaces;
import org.fao.geonet.exceptions.LabelNotFoundException;
import org.fao.geonet.languages.IsoLanguagesMapper;
import org.jdom.Content;
import org.jdom.Element;
Expand Down Expand Up @@ -141,6 +142,32 @@ public String getDefaultValue() {
return values.get(defaultLang);
}

/**
* Get the preferred label for a given language code
*
* @param langCode
* @return preferredLabel
*/

public String getPreferredLabel(String langCode) {
String preferredLabel = values.get(langCode);

if (hasPreferredLabel(preferredLabel))
{
return preferredLabel;
} else {
throw new LabelNotFoundException(noPreferredLabelMessage(langCode));
}
}

private String noPreferredLabelMessage(String langCode) {
return "Could not find preferred label for language code " + langCode + " for the keyword uri " + getUriCode();
}

private boolean hasPreferredLabel(String preferredLabel) {
return preferredLabel != null && !preferredLabel.isEmpty();
}

/**
* Return an <em>unmodifiable</em> map of values. Key is the 3 letter code language
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public Thesaurus getThesaurusByName(String thesaurusName) {
return null;
}

@Override
public Thesaurus getThesaurusByConceptScheme(String conceptSchemeUri) {
if (thesaurus.hasConceptScheme(conceptSchemeUri)) {
return thesaurus;
}
return null;
}

@Override
public Map<String, Thesaurus> getThesauriMap() {
return Collections.singletonMap(thesaurus.getKey(), thesaurus);
Expand Down
171 changes: 164 additions & 7 deletions core/src/main/java/org/fao/geonet/kernel/Thesaurus.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@
package org.fao.geonet.kernel;

import jeeves.server.context.ServiceContext;

import org.fao.geonet.util.LangUtils;
import org.fao.geonet.utils.Log;
import org.fao.geonet.utils.Xml;

import org.fao.geonet.constants.Geonet;
import org.fao.geonet.domain.ISODate;
import org.fao.geonet.exceptions.TermNotFoundException;
import org.fao.geonet.kernel.rdf.Query;
import org.fao.geonet.kernel.rdf.QueryBuilder;
import org.fao.geonet.kernel.rdf.Selectors;
import org.fao.geonet.kernel.rdf.Wheres;
import org.fao.geonet.kernel.search.keyword.KeywordRelation;
import org.fao.geonet.languages.IsoLanguagesMapper;
import org.jdom.Element;
Expand Down Expand Up @@ -89,6 +94,8 @@ public class Thesaurus {

private String keywordUrl;

private IsoLanguagesMapper isoLanguageMapper;

/* @SuppressWarnings("unused")
private String version;
Expand All @@ -107,20 +114,19 @@ public class Thesaurus {
@SuppressWarnings("unused")
private String authority;
*/
private ApplicationContext context;

/**
* @param fname
* file name
* @param type
* @param dname category/domain name of thesaurus
*/
public Thesaurus(ApplicationContext context, String fname, String type, String dname, File thesaurusFile, String siteUrl) {
this(context, fname, null, null, type, dname, thesaurusFile, siteUrl, false);
public Thesaurus(IsoLanguagesMapper isoLanguageMapper, String fname, String type, String dname, File thesaurusFile, String siteUrl) {
this(isoLanguageMapper, fname, null, null, type, dname, thesaurusFile, siteUrl, false);
}
public Thesaurus(ApplicationContext context, String fname, String tname, String tnamespace, String type, String dname, File thesaurusFile, String siteUrl, boolean ignoreMissingError) {
public Thesaurus(IsoLanguagesMapper isoLanguageMapper, String fname, String tname, String tnamespace, String type, String dname, File thesaurusFile, String siteUrl, boolean ignoreMissingError) {
super();
this.context = context;
this.isoLanguageMapper = isoLanguageMapper;
this.fname = fname;
this.type = type;
this.dname = dname;
Expand Down Expand Up @@ -179,6 +185,7 @@ public String getDownloadUrl() {
public String getKeywordUrl() {
return keywordUrl;
}


public void retrieveThesaurusTitle() {
retrieveThesaurusTitle(thesaurusFile, dname + "." + fname, false);
Expand Down Expand Up @@ -256,6 +263,21 @@ public synchronized QueryResultsTable performRequest(String query) throws IOExce
return repository.performTableQuery(QueryLanguage.SERQL, query);
}

public boolean hasConceptScheme(String uri) {

String query = "SELECT conceptScheme"
+ " FROM {conceptScheme} rdf:type {skos:ConceptScheme}"
+ " WHERE conceptScheme = <" + uri + ">"
+ " USING NAMESPACE skos = <http://www.w3.org/2004/02/skos/core#>";

try {
return performRequest(query).getRowCount() > 0;
} catch (Exception e) {
Log.error(Geonet.THESAURUS_MAN, "Error retrieving concept scheme for " + thesaurusFile + ". Error is: " + e.getMessage());
throw new RuntimeException(e);
}
}

/**
*
* @param resultsTable
Expand Down Expand Up @@ -796,7 +818,7 @@ protected void finalize() {
}

public IsoLanguagesMapper getIsoLanguageMapper() {
return context.getBean(IsoLanguagesMapper.class);
return isoLanguageMapper;
}

/**
Expand All @@ -823,6 +845,141 @@ public synchronized void addRelation(String subject, KeywordRelation related, St
myGraph.add(relatedSubjectURI, opposteRelationURI, subjectURI);
}

/**
* Gets a keyword using its id
*
* @param subject the keyword to retrieve
* @return keyword
*/
public KeywordBean getKeyword(String uri, String... languages) {
List<KeywordBean> keywords;

try {
Query<KeywordBean> query = QueryBuilder
.keywordQueryBuilder(getIsoLanguageMapper(), languages)
.where(Wheres.ID(uri))
.build();

keywords = query.execute(this);
} catch (Exception e) {
throw new RuntimeException(e);
}

if (keywords.isEmpty()) {
throw new TermNotFoundException(getTermNotFoundMessage(uri));
}

return keywords.get(0);
}

private String getTermNotFoundMessage(String searchValue) {
return "Could not find "+searchValue+" in file "+thesaurusFile;
}

/**
* Thesaurus has keyword
*
* @param subject the keyword to check
* @return boolean
*/
public boolean hasKeyword(String uri) {
try {
getKeyword(uri);
} catch (TermNotFoundException e) {
return false;
}

return true;
}

/**
* Gets broader keywords
*
* @param the keyword whose broader terms should be retrieved
* @return keywords
*/

public List<KeywordBean> getBroader(String uri, String... languages) {
return getRelated(uri, KeywordRelation.NARROWER, languages);
}

/**
* Has broader keywords
*
* @param the keyword to check for broader terms
* @return keywords
*/

public boolean hasBroader(String uri) {
return getRelated(uri, KeywordRelation.NARROWER).size() > 0;
}

/**
* Gets related keywords
*
* @param uri the keyword whose related terms should be retrieved
* @return keyword
*/
public List<KeywordBean> getRelated(String uri, KeywordRelation request, String... languages) {
Query<KeywordBean> query = QueryBuilder
.keywordQueryBuilder(getIsoLanguageMapper(), languages)
.select(Selectors.related(uri, request), true)
.build();

try {
return query.execute(this);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* Returns whether there is a keyword for a label
*
* @param label the preferred label of the keyword
* @param langCode the language of the label
* @param languages the languages to return
* @return boolean
*/
public boolean hasKeywordWithLabel(String label, String langCode) {
try {
getKeywordWithLabel(label, langCode);
} catch (TermNotFoundException e) {
return false;
}

return true;
}

/**
* Gets a keyword using its label.
*
* @param label the preferred label of the keyword
* @param langCode the language of the label
* @return keyword
* @throws TermNotFoundException
*/
public KeywordBean getKeywordWithLabel(String label, String langCode) {
Query<KeywordBean> query = QueryBuilder
.keywordQueryBuilder(getIsoLanguageMapper(), langCode)
.where(Wheres.prefLabel(langCode, label))
.build();

List<KeywordBean> matchingKeywords;

try {
matchingKeywords = query.execute(this);
} catch (Exception e) {
throw new RuntimeException(e);
}

if (matchingKeywords.size() == 0) {
throw new TermNotFoundException(label);
}

return matchingKeywords.get(0);
}

// ------------------------------- Deprecated methods -----------------------------
/**
* @deprecated since 2.9.0. Use {@link #add(KeywordBean)}
Expand Down
Loading

0 comments on commit e49e6fb

Please sign in to comment.