Skip to content

Commit

Permalink
#550 Map publication state, introduced abstract CommonType
Browse files Browse the repository at this point in the history
  • Loading branch information
Possommi committed Jun 14, 2024
1 parent 86ecd62 commit 1df72fe
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package de.uni_jena.thunibib.his.api.v1;

import com.fasterxml.jackson.annotation.JsonProperty;

abstract public class CommonType {
@JsonProperty("id")
private int id;
@JsonProperty("lockVersion")
private int lockVersion;
@JsonProperty("objGuid")
private String objGuid;
@JsonProperty("shorttext")
private String shortText;
@JsonProperty("defaulttext")
private String defaultText;
@JsonProperty("longtext")
private String longText;
@JsonProperty("text")
private String text;
@JsonProperty("hiskeyId")
private int hisKeyId;
@JsonProperty("sortorder")
private int sortOrder;
@JsonProperty("uniquename")
private String uniqueName;

public int getId() {
return id;
}

public int getLockVersion() {
return lockVersion;
}

public String getObjGuid() {
return objGuid;
}

public String getShortText() {
return shortText;
}

public String getDefaultText() {
return defaultText;
}

public String getLongText() {
return longText;
}

public String getText() {
return text;
}

public int getHisKeyId() {
return hisKeyId;
}

public int getSortOrder() {
return sortOrder;
}

public String getUniqueName() {
return uniqueName;
}

public String toString() {
return id + ":" + uniqueName;
}
}
Original file line number Diff line number Diff line change
@@ -1,73 +1,11 @@
package de.uni_jena.thunibib.his.api.v1.cs.sys.values;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.uni_jena.thunibib.his.api.v1.CommonType;

/**
* Path: <code>/api/v1/cs/sys/values/publicationTypeValue</code>
* */
public class PublicationTypeValue {
@JsonProperty("id")
private int id;
@JsonProperty("lockVersion")
private int lockVersion;
@JsonProperty("objGuid")
private String objGuid;
@JsonProperty("shorttext")
private String shortText;
@JsonProperty("defaulttext")
private String defaultText;
@JsonProperty("longtext")
private String longText;
@JsonProperty("text")
private String text;
@JsonProperty("hiskeyId")
private int hisKeyId;
@JsonProperty("sortorder")
private int sortOrder;
@JsonProperty("uniquename")
private String uniqueName;

public int getId() {
return id;
}

public int getLockVersion() {
return lockVersion;
}

public String getObjGuid() {
return objGuid;
}

public String getShortText() {
return shortText;
}

public String getDefaultText() {
return defaultText;
}

public String getLongText() {
return longText;
}

public String getText() {
return text;
}

public int getHisKeyId() {
return hisKeyId;
}

public int getSortOrder() {
return sortOrder;
}

public String getUniqueName() {
return uniqueName;
}

public String toString() {
return id + ":" + uniqueName;
public class PublicationTypeValue extends CommonType {
public PublicationTypeValue() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.uni_jena.thunibib.his.api.v1.fs.res.state;

import de.uni_jena.thunibib.his.api.v1.CommonType;

/**
* Path: <code>/api/v1/fs/res/state/publication</code>
* */
public class PublicationState extends CommonType {
public PublicationState() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,23 @@ protected JsonObject toJSON(MCRContent source) throws IOException {
Document xml = source.asXML();
JsonObject jsonObject = new JsonObject();

add(jsonObject, "//modsContainer/mods:mods/mods:abstract", xml, "textAbstract");
add(jsonObject, "//modsContainer/mods:mods/mods:originInfo/mods:dateIssued[1]", xml, "releaseYear");
add(jsonObject, "//modsContainer/mods:mods/mods:originInfo/mods:edition", xml, "edition");
add(jsonObject, "//modsContainer/mods:mods/mods:originInfo/mods:publisher", xml, "publisher");
add(jsonObject, "//modsContainer/mods:mods/mods:titleInfo/mods:subTitle", xml, "subtitle");
add(jsonObject, "//modsContainer/mods:mods/mods:titleInfo/mods:title", xml, "title");
add(jsonObject, "//modsContainer/mods:physicalDescription/mods:extent", xml, "numberOfPages");
addProperty(jsonObject, "//modsContainer/mods:mods/mods:abstract", xml, "textAbstract");
addProperty(jsonObject, "//modsContainer/mods:mods/mods:originInfo/mods:dateIssued[1]", xml, "releaseYear");
addProperty(jsonObject, "//modsContainer/mods:mods/mods:originInfo/mods:edition", xml, "edition");
addProperty(jsonObject, "//modsContainer/mods:mods/mods:originInfo/mods:publisher", xml, "publisher");
addProperty(jsonObject, "//modsContainer/mods:mods/mods:titleInfo/mods:subTitle", xml, "subtitle");
addProperty(jsonObject, "//modsContainer/mods:mods/mods:titleInfo/mods:title", xml, "title");
addProperty(jsonObject, "//modsContainer/mods:physicalDescription/mods:extent", xml, "numberOfPages");

addCreators(jsonObject, xml);
addLanguages(jsonObject, xml);
addPublicationType(jsonObject, xml);
addQualifiedObjectID(jsonObject, "//mods:genre[@authorityURI='" + HISInOneClient.HIS_IN_ONE_BASE_URL + "']",
xml, "publicationType");
addQualifiedObjectID(jsonObject,
"//servflag[@type='status'][@authorityURI='" + HISInOneClient.HIS_IN_ONE_BASE_URL + "']",
xml, "status");
addQualifiedObjectIDs(jsonObject,
"//mods:language/mods:languageTerm[@authorityURI='" + HISInOneClient.HIS_IN_ONE_BASE_URL + "']", xml,
"languages");

return jsonObject;
} catch (JDOMException | SAXException e) {
Expand All @@ -63,38 +69,6 @@ protected JsonObject toJSON(MCRContent source) throws IOException {
}
}

private void addPublicationType(JsonObject jsonObject, Document xml) {
Optional.ofNullable(
XPATH_FACTORY.compile("//mods:genre[@authorityURI='" + HISInOneClient.HIS_IN_ONE_BASE_URL + "']/text()",
Filters.text(), null, MODS_NAMESPACE).evaluateFirst(xml))
.ifPresent(genre -> {
JsonObject documentType = new JsonObject();
documentType.addProperty("id", Integer.parseInt(genre.getText()));
jsonObject.add("publicationType", documentType);
});
}

private void addLanguages(JsonObject jsonObject, Document xml) {
final JsonArray languages = new JsonArray();

XPATH_FACTORY.compile(
"//mods:language/mods:languageTerm[@authorityURI='" + HISInOneClient.HIS_IN_ONE_BASE_URL + "']/text()",
Filters.text(), null, MODS_NAMESPACE).evaluate(xml)
.forEach(text -> {
try {
JsonObject item = new JsonObject();
item.addProperty("id", Integer.parseInt(text.getText()));
languages.add(item);
} catch (NumberFormatException e) {
LOGGER.error(e);
}
});

if (languages.size() > 0) {
jsonObject.add("languages", languages);
}
}

private void addCreators(JsonObject jsonObject, Document xml) {
final JsonArray creators = new JsonArray();

Expand Down Expand Up @@ -155,9 +129,66 @@ private void addCreators(JsonObject jsonObject, Document xml) {
}
}

private void add(JsonObject jsonObject, String xpath, Document xml, String name) {
private void addProperty(JsonObject jsonObject, String xpath, Document xml, String name) {
XPATH_FACTORY.compile(xpath, Filters.element(), null, MODS_NAMESPACE)
.evaluate(xml)
.forEach(e -> jsonObject.addProperty(name, e.getText()));
}

/**
* @param jsonObject the json object
* @param xpath the xpath
* @param xml the xml
* @param propertyName the name of the property
*
* <pre>
* "propertyName": [
* {
* "id": 5
* },
* {
* "id
* }, ...]
* </pre>
* */
private void addQualifiedObjectIDs(JsonObject jsonObject, String xpath, Document xml, String propertyName) {
final JsonArray jsonArray = new JsonArray();

XPATH_FACTORY.compile(xpath, Filters.element(), null, MODS_NAMESPACE).evaluate(xml)
.forEach(text -> {
try {
JsonObject item = new JsonObject();
item.addProperty("id", Integer.parseInt(text.getText()));
jsonArray.add(item);
} catch (NumberFormatException e) {
LOGGER.error(e);
}
});

if (jsonArray.size() > 0) {
jsonObject.add(propertyName, jsonArray);
}
}

/**
* @param jsonObject the json object
* @param xpath the xpath
* @param xml the xml
* @param propertyName the name of the property
*
* <pre>
* "propertyName": {
* "id": integer value determined by xpath
* }
* </pre>
* */
private void addQualifiedObjectID(JsonObject jsonObject, String xpath, Document xml, String propertyName) {
Optional.ofNullable(
XPATH_FACTORY.compile(xpath, Filters.element(), null, MODS_NAMESPACE).evaluateFirst(xml))
.ifPresent(genre -> {
JsonObject documentType = new JsonObject();
documentType.addProperty("id", Integer.parseInt(genre.getText()));
jsonObject.add(propertyName, documentType);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.uni_jena.thunibib.his.api.HISinOneClientFactory;
import de.uni_jena.thunibib.his.api.v1.cs.sys.values.LanguageValue;
import de.uni_jena.thunibib.his.api.v1.cs.sys.values.PublicationTypeValue;
import de.uni_jena.thunibib.his.api.v1.fs.res.state.PublicationState;
import jakarta.ws.rs.core.GenericType;
import jakarta.ws.rs.core.Response;
import org.apache.logging.log4j.LogManager;
Expand All @@ -21,7 +22,7 @@
public class HISinOneResolver implements URIResolver {

public enum SUPPORTED_URI_PARTS {
language, genre
language, genre, state
}

private static final Logger LOGGER = LogManager.getLogger(HISinOneResolver.class);
Expand All @@ -39,11 +40,35 @@ public Source resolve(String href, String base) throws TransformerException {
return new JDOMSource(new Element("int").setText(String.valueOf(resolveLanguage(value))));
case genre:
return new JDOMSource(new Element("int").setText(String.valueOf(resolveGenre(value))));
case state:
return new JDOMSource(new Element("int").setText(String.valueOf(resolveState(value))));
}

throw new TransformerException("Unknown entity: " + entity);
}

private int resolveState(String value) {
try (HISInOneClient hisClient = HISinOneClientFactory.create();
Response response = hisClient.get("fs/res/state/publication")) {

List<PublicationState> pubState = response.readEntity(
new GenericType<List<PublicationState>>() {
});

return switch (value) {
case "confirmed", "unchecked" ->
pubState.stream().filter(state -> "validiert" .equals(state.getUniqueName())).findFirst().get()
.getId();
case "review" ->
pubState.stream().filter(state -> "Dateneingabe" .equals(state.getUniqueName())).findFirst().get()
.getId();
default ->
pubState.stream().filter(state -> "zur Validierung" .equals(state.getUniqueName())).findFirst()
.get().getId();
};
}
}

private int resolveGenre(String ubogenre) {
try (HISInOneClient hisClient = HISinOneClientFactory.create();
Response response = hisClient.get("cs/sys/values/publicationTypeValue")) {
Expand All @@ -60,7 +85,7 @@ private int resolveGenre(String ubogenre) {
int id;
if (tpv.isEmpty()) {
id = pubTypeValues.stream()
.filter(pubType -> "Sonstiger Publikationstyp".equals(pubType.getUniqueName()))
.filter(pubType -> "Sonstiger Publikationstyp" .equals(pubType.getUniqueName()))
.findFirst().get().getId();
} else {
id = tpv.get().getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,20 @@
</xsl:if>
</xsl:copy>
</xsl:template>

<xsl:template match="//servflags/servflag[@type='status']">
<xsl:copy>
<xsl:copy-of select="*|@*"/>
</xsl:copy>

<xsl:variable name="status" select="."/>
<xsl:variable name="his-key" select="fn:document(concat('HISinOne:state:', $status))"/>

<xsl:if test="$his-key">
<servflag type="status" authorityURI="{$ThUniBib.HISinOne.BaseURL}">
<xsl:value-of select="$his-key"/>
</servflag>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

0 comments on commit 1df72fe

Please sign in to comment.