Skip to content

Commit

Permalink
#550 Refactored getFieldValue(...) in HISinOneResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Possommi committed Dec 13, 2024
1 parent 9948e66 commit 2e86c8c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ public Source resolve(String href, String base) throws TransformerException {
*/
protected SysValue resolvePerson(String type, String value) {
Map<String, String> parameter = new HashMap<>();
parameter.put(PersonIdentifier.getTypeParamaterName(), type);
parameter.put(PersonIdentifier.getValueParamaterName(), value);
parameter.put(PersonIdentifier.getTypeParameterName(), type);
parameter.put(PersonIdentifier.getValueParameterName(), value);

try (HISInOneClient hisClient = HISinOneClientFactory.create();
Response response = hisClient.post(PersonIdentifier.getPath(), null, parameter)) {
Expand All @@ -177,7 +177,7 @@ protected SysValue resolvePerson(String type, String value) {
return SysValue.ErroneousSysValue;
}

SysValue sysValue = response.readEntity(Journal.class);
PersonIdentifier sysValue = response.readEntity(PersonIdentifier.class);
return sysValue;
} catch (Exception e) {
return SysValue.ErroneousSysValue;
Expand Down Expand Up @@ -837,16 +837,34 @@ protected SysValue resolveLanguage(String rfc5646) {
* @return the value of the requested field name or {@link SysValue#getId()}.
*/
protected Object getFieldValue(SysValue sysValue, String fieldName) {
Class clazz = sysValue.getClass();
Field field = null;

/* lookup field in class hierarchy */
while (clazz != null && field == null) {
try {
LOGGER.debug("Checking for field {} in {}", fieldName, clazz.getSimpleName());
field = clazz.getDeclaredField(fieldName);
} catch (Exception e) {
LOGGER.debug("Field {} could not be obtained from {}. Checking superclass {}", fieldName,
clazz.getSimpleName(),
clazz.getSuperclass().getSimpleName());
}
clazz = clazz.getSuperclass();
}

/* field is unresolved */
if (field == null) {
return SysValue.UnresolvedSysValue;
}

/* field is resolved */
field.setAccessible(true);
try {
final Field field = Class.forName(SysValue.class.getName()).getDeclaredField(fieldName);
field.setAccessible(true);
Object value = field.get(sysValue);
if (value == null) {
throw new RuntimeException("Could not get value from field " + fieldName);
}
return value;
} catch (Exception e) {
LOGGER.warn("Field {} could not be obtained from SysValue {} returning id instead", fieldName, sysValue);
} catch (IllegalAccessException e) {
LOGGER.error(e);
return sysValue.getId();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@
<xsl:template match="mods:name[@type='personal']">
<xsl:variable name="his-id">
<xsl:choose>
<xsl:when test="mods:nameIdentifier[@type = 'orcid']">
<xsl:value-of select="fn:document(concat('hisinone:resolve:personId:person:orcid:', mods:nameIdentifier[@type = 'orcid'][1]/text()))"/>
</xsl:when>
<xsl:when test="mods:nameIdentifier[@type = $MCR.user2.matching.lead_id]">
<xsl:value-of select="fn:document(concat('hisinone:resolve:id:person:', $MCR.user2.matching.lead_id, ':', mods:nameIdentifier[@type = $MCR.user2.matching.lead_id][1]/text()))"/>
</xsl:when>
<xsl:when test="mods:nameIdentifier[@type = 'orcid']">
<xsl:value-of select="fn:document(concat('hisinone:resolve:id:person:orcid:', mods:nameIdentifier[@type = 'orcid'][1]/text()))"/>
</xsl:when>
</xsl:choose>
</xsl:variable>

Expand Down

0 comments on commit 2e86c8c

Please sign in to comment.