-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New methods for searching and retrieving HEI attributes
Fix #1
- Loading branch information
Showing
9 changed files
with
449 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/eu/erasmuswithoutpaper/registryclient/HeiEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package eu.erasmuswithoutpaper.registryclient; | ||
|
||
import java.util.Collection; | ||
|
||
|
||
/** | ||
* Describes a single HEI entry, as found in the EWP Registry's catalogue. | ||
* | ||
* <p> | ||
* Note, that the EWP Registry keeps only the very import attributes of each HEI (identifiers and | ||
* name). If you are looking for more information on HEIs, then you should make use of the | ||
* <a href='https://github.com/erasmus-without-paper/ewp-specs-api-institutions'>Institutions | ||
* API</a>. | ||
* </p> | ||
* | ||
* @since 1.2.0 | ||
*/ | ||
public interface HeiEntry { | ||
|
||
/** | ||
* @return SCHAC ID of this HEI. | ||
*/ | ||
String getId(); | ||
|
||
/** | ||
* Get the name of this HEI. | ||
* | ||
* @return We will try to return the name in English. If we cannot find it, we will return the | ||
* name in any other language. If we fail this too, we will return the HEI's ID, so you | ||
* will never get <code>null</code> here. | ||
*/ | ||
String getName(); | ||
|
||
/** | ||
* Get a name in the given language. | ||
* | ||
* @param langCode An ISO 639-1 code of the language (2 lower-case letters). | ||
* @return String (if the name was found), or null (if it hasn't). | ||
*/ | ||
String getName(String langCode); | ||
|
||
/** | ||
* Retrieve all <code><other-id></code> values of certain type. | ||
* | ||
* @param type type identifier, see {@link RegistryClient#findHei(String, String)} for more | ||
* information on these types. | ||
* @return A collection of all matched values for the given type. In no matches were found, an | ||
* empty collection will be returned. | ||
*/ | ||
Collection<String> getOtherIds(String type); | ||
} |
113 changes: 113 additions & 0 deletions
113
src/main/java/eu/erasmuswithoutpaper/registryclient/HeiEntryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package eu.erasmuswithoutpaper.registryclient; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.xml.XMLConstants; | ||
|
||
import org.w3c.dom.Element; | ||
import org.w3c.dom.Node; | ||
|
||
|
||
class HeiEntryImpl implements HeiEntry { | ||
|
||
private static class Extras { | ||
private final String primaryName; | ||
private final Map<String, String> allNames; | ||
private final Map<String, List<String>> otherIds; | ||
|
||
private Extras(HeiEntryImpl hei) { | ||
this.allNames = new HashMap<>(); | ||
this.otherIds = new HashMap<>(); | ||
for (Node node : Utils.asNodeList(hei.elem.getChildNodes())) { | ||
if (node.getNodeType() != Node.ELEMENT_NODE) { | ||
continue; | ||
} | ||
Element elem = (Element) node; | ||
String value = elem.getTextContent(); | ||
switch (elem.getTagName()) { | ||
case "name": | ||
String lang = elem.getAttributeNS(XMLConstants.XML_NS_URI, "lang"); | ||
if (value.length() > 0) { | ||
this.allNames.put(lang, value); | ||
} | ||
break; | ||
|
||
case "other-id": | ||
String idType = elem.getAttribute("type"); | ||
List<String> lst = this.otherIds.get(idType); | ||
if (lst == null) { | ||
lst = new ArrayList<>(); | ||
this.otherIds.put(idType, lst); | ||
} | ||
lst.add(value); | ||
break; | ||
|
||
default: | ||
// Ingore. | ||
} | ||
} | ||
String primaryName = this.allNames.get("en"); | ||
if (primaryName == null) { | ||
// No English name found. We'll use any name we have. | ||
Collection<String> names = this.allNames.values(); | ||
if (names.size() > 0) { | ||
primaryName = names.iterator().next(); | ||
} else { | ||
// No name at all! | ||
primaryName = hei.id; | ||
} | ||
} | ||
this.primaryName = primaryName; | ||
} | ||
} | ||
|
||
private final String id; | ||
private final Element elem; | ||
|
||
private volatile Extras extras = null; | ||
|
||
HeiEntryImpl(String id, Element heiElem) { | ||
this.id = id; | ||
this.elem = heiElem; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return this.id; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return this.getExtras().primaryName; | ||
} | ||
|
||
@Override | ||
public String getName(String langCode) { | ||
return this.getExtras().allNames.get(langCode); | ||
} | ||
|
||
@Override | ||
public Collection<String> getOtherIds(String type) { | ||
List<String> values = this.getExtras().otherIds.get(type); | ||
if (values == null) { | ||
return Collections.emptyList(); | ||
} | ||
return Collections.unmodifiableCollection(values); | ||
} | ||
|
||
private Extras getExtras() { | ||
if (this.extras == null) { | ||
synchronized (this) { | ||
if (this.extras == null) { | ||
this.extras = new Extras(this); | ||
} | ||
} | ||
} | ||
return this.extras; | ||
} | ||
} |
Oops, something went wrong.