Skip to content

Commit

Permalink
#550 Refactored HISinOneCommands
Browse files Browse the repository at this point in the history
  • Loading branch information
Possommi committed Dec 13, 2024
1 parent 4f02132 commit 36c16f5
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions common/src/main/java/de/uni_jena/thunibib/HISinOneCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.mycore.frontend.cli.annotation.MCRCommandGroup;

import java.io.IOException;
import java.util.Optional;

import static de.uni_jena.thunibib.his.api.client.HISInOneClient.HIS_IN_ONE_BASE_URL;

Expand All @@ -25,18 +26,13 @@ public class HISinOneCommands {
@MCRCommand(syntax = "publish {0}", help = "Publishes the object given by its id to HISinOne")
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 SysValue.ErroneousSysValue;
}

MCRObjectID mcrObjectID = MCRObjectID.getInstance(mcrid);
if (!MCRMetadataManager.exists(mcrObjectID)) {
LOGGER.error("{} does not exist", mcrid);
return SysValue.UnresolvedSysValue;
Optional<SysValue> sysVal = isValid(mcrid);
if (sysVal.isPresent()) {
return sysVal.get();
}

MCRObject mcrObject = MCRMetadataManager.retrieveMCRObject(mcrObjectID);
MCRObject mcrObject = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(mcrid));
if (mcrObject.getService().getFlags(HISInOneServiceFlag.getName()).size() > 0) {
LOGGER.warn("{} is already published. Try command 'update {0}' instead?", mcrid);
return SysValue.ErroneousSysValue;
Expand All @@ -57,7 +53,6 @@ public static SysValue publish(String mcrid) {
}

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());
Expand All @@ -82,18 +77,12 @@ public static SysValue publish(String mcrid) {
@MCRCommand(syntax = "update {0}", help = "Updates the object given by its id in HISinOne")
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 SysValue.ErroneousSysValue;
}

MCRObjectID mcrObjectID = MCRObjectID.getInstance(mcrid);
if (!MCRMetadataManager.exists(mcrObjectID)) {
LOGGER.error("{} does not exist", mcrid);
return SysValue.UnresolvedSysValue;
Optional<SysValue> sysVal = isValid(mcrid);
if (sysVal.isPresent()) {
return sysVal.get();
}

MCRObject mcrObject = MCRMetadataManager.retrieveMCRObject(mcrObjectID);
MCRObject mcrObject = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(mcrid));
if (mcrObject.getService().getFlags(HISInOneServiceFlag.getName()).size() == 0) {
LOGGER.warn("{} is not published. Try command 'publish {0}' first", mcrid);
return SysValue.ErroneousSysValue;
Expand Down Expand Up @@ -133,4 +122,25 @@ public static void delete(String mcrid) {
LOGGER.info("Deleting {}", mcrid);
}

/**
* Checks if the provided mcr id is valid and the related mycoreobject actually exists.
*
* @param mcrid the id of the mycoreobject
*
* @return either an empty {@link Optional} or an {@link Optional} wrapping a {@link SysValue}
*/
protected final static Optional<SysValue> isValid(String mcrid) {
if (!MCRObjectID.isValid(mcrid)) {
LOGGER.error("{} is not a valid {}", mcrid, MCRObjectID.class.getSimpleName());
return Optional.of(SysValue.ErroneousSysValue);
}

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

return Optional.empty();
}
}

0 comments on commit 36c16f5

Please sign in to comment.