Skip to content

Commit

Permalink
#550 Unified creation of journals and arbitrary parent objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Possommi committed Nov 4, 2024
1 parent ecd7f52 commit 13c32ac
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 80 deletions.
37 changes: 27 additions & 10 deletions common/src/main/java/de/uni_jena/thunibib/HISinOneCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ public class HISinOneCommands {
private static final Logger LOGGER = LogManager.getLogger(HISinOneCommands.class);

@MCRCommand(syntax = "publish {0}", help = "Publishes the object given by its id to HISinOne")
public static void publish(String mcrid) {
public static SysValue publish(String mcrid) {
LOGGER.info("Publishing {}", mcrid);
if (!MCRObjectID.isValid(mcrid)) {
LOGGER.error("{} is not a valid {}", mcrid, MCRObjectID.class.getSimpleName());
return;
return SysValue.ErroneousSysValue;
}

MCRObjectID mcrObjectID = MCRObjectID.getInstance(mcrid);
if (!MCRMetadataManager.exists(mcrObjectID)) {
LOGGER.error("{} does not exist", mcrid);
return;
return SysValue.UnresolvedSysValue;
}

MCRObject mcrObject = MCRMetadataManager.retrieveMCRObject(mcrObjectID);
if (mcrObject.getService().getFlags(HISInOneServiceFlag.getName()).size() > 0) {
LOGGER.warn("{} is already published. Try command 'update {0}' instead?", mcrid);
return;
return SysValue.ErroneousSysValue;
}

HISinOneCommandConfiguration conf = new HISinOneCommandConfiguration(mcrObject);
Expand All @@ -50,43 +50,53 @@ public static void publish(String mcrid) {

try (HISInOneClient client = HISinOneClientFactory.create();
Response response = client.post(conf.getPath(), json)) {

if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
LOGGER.error("{}", response.readEntity(String.class));
return SysValue.ErroneousSysValue;
}

SysValue publication = response.readEntity(conf.getResponseEntityClass());

if (publication.getId() == 0) {
LOGGER.error("MCRObject {} was not published at {} with id {}", mcrid, HIS_IN_ONE_BASE_URL,
publication.getId());
return;
return SysValue.ErroneousSysValue;
}

LOGGER.info("MCRObject {} published at {} with id {}", mcrid, HIS_IN_ONE_BASE_URL, publication.getId());

// Update MCRObject
mcrObject.getService().addFlag(HISInOneServiceFlag.getName(), String.valueOf(publication.getId()));
MCRMetadataManager.update(mcrObject);

return publication;
}
} catch (IOException | MCRAccessException e) {
LOGGER.error("Could not publish {} to {}", mcrid, HIS_IN_ONE_BASE_URL, e);
}

return SysValue.UnresolvedSysValue;
}

@MCRCommand(syntax = "update {0}", help = "Updates the object given by its id in HISinOne")
public static void update(String mcrid) {
public static SysValue update(String mcrid) {
LOGGER.info("Updating {}", mcrid);
if (!MCRObjectID.isValid(mcrid)) {
LOGGER.error("{} is not a valid {}", mcrid, MCRObjectID.class.getSimpleName());
return;
return SysValue.ErroneousSysValue;
}

MCRObjectID mcrObjectID = MCRObjectID.getInstance(mcrid);
if (!MCRMetadataManager.exists(mcrObjectID)) {
LOGGER.error("{} does not exist", mcrid);
return;
return SysValue.UnresolvedSysValue;
}

MCRObject mcrObject = MCRMetadataManager.retrieveMCRObject(mcrObjectID);
if (mcrObject.getService().getFlags(HISInOneServiceFlag.getName()).size() == 0) {
LOGGER.warn("{} is not published. Try command 'publish {0}' first", mcrid);
return;
return SysValue.ErroneousSysValue;
}

HISinOneCommandConfiguration conf = new HISinOneCommandConfiguration(mcrObject);
Expand All @@ -98,17 +108,24 @@ public static void update(String mcrid) {
Response response = client.put(conf.getPath() + "/" + hisId, json)) {
SysValue p = response.readEntity(conf.getResponseEntityClass());

if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
LOGGER.error("{}", response.readEntity(String.class));
return SysValue.ErroneousSysValue;
}

if (p.getId() == 0) {
LOGGER.error("MCRObject {} was not updated at {}({}) with id {}", mcrid, HIS_IN_ONE_BASE_URL,
conf.getPath(), p.getId());
return;
return SysValue.ErroneousSysValue;
}
LOGGER.info("MCRObject {} updated at {}({}) with id {} and new lockVersion {}", mcrid,
HIS_IN_ONE_BASE_URL, conf.getPath(), hisId, p.getLockVersion());
return p;
}
} catch (IOException e) {
LOGGER.error("Could not update {} to {}", mcrid, HIS_IN_ONE_BASE_URL, e);
}
return SysValue.ErroneousSysValue;
}

@MCRCommand(syntax = "delete {0}", help = "Deletes the object given by its id from HISinOne")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/*
* This file is part of *** M y C o R e ***
* See http://www.mycore.de/ for details.
*
* MyCoRe 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 3 of the License, or
* (at your option) any later version.
*
* MyCoRe 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 MyCoRe. If not, see <http://www.gnu.org/licenses/>.
*/

package de.uni_jena.thunibib.his.content.transformer;

import com.google.gson.JsonArray;
Expand All @@ -29,7 +11,6 @@
import org.jdom2.filter.Filters;
import org.mycore.common.content.MCRContent;
import org.mycore.common.content.transformer.MCRToJSONTransformer;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -59,6 +40,8 @@ protected JsonObject toJSON(MCRContent source) throws IOException {
return jsonObject;
}

addParent(jsonObject, xml);

addPropertyInt(jsonObject, "//servflag[@type='" + HISInOneServiceFlag.getName() + "']", xml, "id");
addPropertyInt(jsonObject, "//servflag[@type='" + HISInOneServiceFlag.getName() + "-lockVersion']", xml, "lockVersion");

Expand All @@ -85,7 +68,6 @@ protected JsonObject toJSON(MCRContent source) throws IOException {
addQualifiedObjectID(jsonObject, "//mods:mods/mods:genre[@authorityURI='" + HIS_IN_ONE_BASE_URL + "'][contains(@valueURI, 'publicationTypeValue')]", xml, "publicationType");
addQualifiedObjectID(jsonObject, "//mods:mods/mods:genre[@authorityURI='" + HIS_IN_ONE_BASE_URL + "'][contains(@valueURI, 'documentTypes')]", xml, "documentType");
addQualifiedObjectID(jsonObject, "//mods:mods/mods:genre[@authorityURI='" + HIS_IN_ONE_BASE_URL + "'][contains(@valueURI, 'qualificationThesisValue')]", xml, "qualificationThesis");
addQualifiedObjectID(jsonObject, "//mods:mods/mods:relatedItem[contains(@otherTypeAuthURI, 'fs/res/journal')][1]", xml, "journal");

addQualifiedObjectIDs(jsonObject, "//mods:mods/mods:classification[contains(@valueURI, 'researchAreaKdsfValue')]", xml,"researchAreasKdsf", "id");
addQualifiedObjectIDs(jsonObject, "//mods:mods/mods:classification[contains(@valueURI, 'subjectAreaValue')]", xml, "subjectAreas", "id");
Expand All @@ -101,6 +83,19 @@ protected JsonObject toJSON(MCRContent source) throws IOException {
}
}

/**
* Adds the id of superordinate/parent (if any) to the {@link JsonObject}.
*
* @param jsonObject the {@link JsonObject}
* @param xml the source mycoreobject as {@link Document}
*/
protected void addParent(JsonObject jsonObject, Document xml) {
// add journal
addQualifiedObjectID(jsonObject, "//mods:mods/mods:relatedItem[contains(@otherTypeAuthURI, 'fs/res/journal')][1]", xml, "journal");
// add publication
addQualifiedObjectID(jsonObject, "//mods:mods/mods:relatedItem[contains(@otherTypeAuthURI, 'fs/res/publication')][1]", xml, "parent");
}

protected void addExtent(JsonObject jsonObject, Document xml) {
Optional
.ofNullable(XPATH_FACTORY
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.uni_jena.thunibib.his.xml;

import com.google.gson.JsonObject;
import de.uni_jena.thunibib.Utilities;
import de.uni_jena.thunibib.HISinOneCommands;
import de.uni_jena.thunibib.his.api.client.HISInOneClient;
import de.uni_jena.thunibib.his.api.client.HISinOneClientFactory;
import de.uni_jena.thunibib.his.api.v1.cs.sys.values.LanguageValue;
Expand Down Expand Up @@ -39,7 +39,6 @@
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -133,10 +132,10 @@ public Source resolve(String href, String base) throws TransformerException {
case creatorType -> resolveCreatorType(fromValue);
case documentType -> resolveDocumentType(fromValue, hostGenre);
case globalIdentifiers -> resolveIdentifierType(fromValue);
case journal -> Mode.resolve.equals(mode) ? resolveJournal(fromValue) : createJournal(fromValue);
case journal -> Mode.resolve.equals(mode) ? resolveJournal(fromValue) : createParent(fromValue);
case language -> resolveLanguage(fromValue);
case peerReviewed -> resolvePeerReviewedType(fromValue);
case publication -> resolvePublication(fromValue);
case publication -> Mode.resolve.equals(mode) ? resolvePublication(fromValue) : createParent(fromValue);
case publicationAccessType -> resolvePublicationAccessType(fromValue);
case publicationResource -> resolvePublicationResourceType(fromValue);
case publicationType -> resolvePublicationType(fromValue, hostGenre);
Expand All @@ -153,40 +152,7 @@ public Source resolve(String href, String base) throws TransformerException {
return new JDOMSource(new Element("int").setText(String.valueOf(getFieldValue(sysValue, field))));
}

protected SysValue createJournal(String fromValue) {
if (!exists(fromValue)) {
return SysValue.UnresolvedSysValue;
}

MCRObject mcrObject = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(fromValue));
String json = null;
try {
json = Utilities.transform(mcrObject, JOURNAL_TRANSFORMER);
} catch (IOException e) {
LOGGER.error("Could not transform journal {} to json", fromValue, e);
return SysValue.ErroneousSysValue;
}

try (HISInOneClient hisClient = HISinOneClientFactory.create();
Response response = hisClient.post(Journal.getPath(), json)) {

if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
return SysValue.ErroneousSysValue;
}
Journal journal = response.readEntity(Journal.class);

//Update MCRObject
mcrObject.getService().addFlag(HISInOneServiceFlag.getName(), String.valueOf(journal.getId()));
MCRMetadataManager.update(mcrObject);

return journal;
} catch (Exception e) {
LOGGER.error("Could not post journal {} ({}) to {}", fromValue, json, Journal.getPath(), e);
return SysValue.ErroneousSysValue;
}
}

private SysValue resolveJournal(String fromValue) {
protected SysValue resolveJournal(String fromValue) {
if (!exists(fromValue)) {
LOGGER.warn("{} does not exist", fromValue);
return SysValue.UnresolvedSysValue;
Expand Down Expand Up @@ -265,6 +231,10 @@ private SysValue resolvePublicationResourceType(String resourceTypeText) {
}
}

protected SysValue createParent(String mcrid) {
return HISinOneCommands.publish(mcrid);
}

/**
* Resolves the {@link SysValue} with id in HISinOne of the given mycoreobject id.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,46 @@
<xsl:template match="mods:mods">
<xsl:copy>
<xsl:comment>Begin - transformer 'mods-create-unresolved-his-keys'</xsl:comment>

<!-- TODO Check user role -->
<xsl:apply-templates select="mods:classification[@authorityURI = $ThUniBib.HISinOne.BaseURL][text() = '-1']"
mode="create"/>
<xsl:apply-templates select="mods:relatedItem[@otherTypeAuth = $ThUniBib.HISinOne.BaseURL][text() = '-1']"
mode="create"/>
<xsl:apply-templates select="mods:classification[@authorityURI = $ThUniBib.HISinOne.BaseURL][text() = '-1']" mode="create"/>
<xsl:apply-templates select="mods:relatedItem[@otherTypeAuth = $ThUniBib.HISinOne.BaseURL][text() = '-1']" mode="create"/>

<xsl:comment>End - mods-create-unresolved-his-keys'</xsl:comment>
<!-- Retain mods from previous step, but exclude unresolved values -->
<xsl:apply-templates select="@*|node()[not(text() = '-1')]"/>
</xsl:copy>
</xsl:template>

<!-- Create unresolved journal-->
<xsl:template mode="create"
match="mods:relatedItem[@xlink:href][@otherType='host'][@otherTypeAuth = $ThUniBib.HISinOne.BaseURL][contains(@otherTypeAuthURI, 'journal')][1]">
<!-- Create unresolved host -->
<xsl:template match="mods:relatedItem[@xlink:href][@otherType='host'][@otherTypeAuth = $ThUniBib.HISinOne.BaseURL][@otherTypeAuthURI][1]" mode="create">

<xsl:variable name="host" select="@xlink:href"/>
<xsl:variable name="host-genre" select="fn:substring-after(@otherTypeAuthURI, '/fs/res/')"/>

<xsl:variable name="resolve-of-type">
<xsl:choose>
<xsl:when test="contains('journal', $host-genre)">
<xsl:value-of select="'journal'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'publication'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<xsl:variable name="journal-id" select="fn:document(concat('hisinone:create:id:journal:', @xlink:href))"/>
<xsl:variable name="host-his-id" select="fn:document(concat('hisinone:create:id:', $resolve-of-type, ':', @xlink:href))"/>

<xsl:if test="fn:number($journal-id) &gt; 0">
<xsl:if test="fn:number($host-his-id) &gt; 0">
<mods:relatedItem>
<xsl:copy-of select="@*"/>
<xsl:value-of select="$journal-id"/>
<xsl:value-of select="$host-his-id"/>
</mods:relatedItem>
</xsl:if>
</xsl:template>

<!-- Create unresolved publisher -->
<xsl:template mode="create"
match="mods:classification[fn:contains(@valueURI, 'fs/res/publisher') and @authorityURI = $ThUniBib.HISinOne.BaseURL]">
<xsl:template mode="create" match="mods:classification[fn:contains(@valueURI, 'fs/res/publisher') and @authorityURI = $ThUniBib.HISinOne.BaseURL]">

<xsl:variable name="publisher-text" select="fn:encode-for-uri(../mods:originInfo/mods:publisher)"/>
<xsl:variable name="publisher-id" select="fn:document(concat('hisinone:create:id:publisher:', $publisher-text))"/>
Expand Down

0 comments on commit 13c32ac

Please sign in to comment.