Skip to content

Commit

Permalink
#550 Create Publisher in HISinOne
Browse files Browse the repository at this point in the history
  • Loading branch information
Possommi committed Jul 17, 2024
1 parent 754ad03 commit 4781cc8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ enum AuthType {
Response get(String path, Map<String, String> params);

Response get(String path);

Response post(String path, String body);
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ public Response get(String path, Map<String, String> parameters) {
return response;
}

public Response post(String path, String bodySource) {
WebTarget webTarget = getJerseyClient()
.target(HISInOneClientDefaultImpl.HIS_IN_ONE_BASE_URL + API_PATH)
.path(path);

Token token;
try {
token = fetchToken();
} catch (Exception e) {
LOGGER.error("Could not fetch token", e);
return Response.serverError().entity(e.getMessage()).build();
}

Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
invocationBuilder.header("Authorization", getAuthorizationHeaderValue(AuthType.Bearer, token.getAccessToken()));

Entity<String> body = Entity.entity(bodySource, MediaType.APPLICATION_JSON);
Response response = invocationBuilder.post(body);

return response;
}

public Response get(String path) {
return get(path, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@

abstract public class SysValue implements HisValue {
@JsonProperty("id")
private int id;
protected int id;
@JsonProperty("lockVersion")
private int lockVersion;
@JsonProperty("objGuid")
private String objGuid;
@JsonProperty("shorttext")
private String shortText;
protected String shortText;
@JsonProperty("defaulttext")
private String defaultText;
protected String defaultText;
@JsonProperty("longtext")
private String longText;
protected String longText;
@JsonProperty("text")
private String text;
protected String text;
@JsonProperty("hiskeyId")
private int hisKeyId;
protected int hisKeyId;
@JsonProperty("sortorder")
private int sortOrder;
@JsonProperty("uniquename")
Expand Down Expand Up @@ -69,8 +69,11 @@ public String toString() {
return id + ":" + uniqueName;
}

/**
* This {@link SysValue} indicates an unresolved value in case a proper hisKeyId could not be obtained from HISinOne.
* */
@JsonIgnore
public static final SysValue EmptySysValue = new SysValue() {
public static final SysValue UnresolvedSysValue = new SysValue() {
@Override
public int getHisKeyId() {
return -1;
Expand All @@ -82,8 +85,12 @@ public int getId() {
}
};

/**
* This {@link SysValue} indicates an unresolved value in case an error occured during the resolving of the key from
* HISinOne.
* */
@JsonIgnore
public static final SysValue NotObtainedSysValue = new SysValue() {
public static final SysValue ErroneousSysValue = new SysValue() {
@Override
public int getHisKeyId() {
return -2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* */
@JsonIgnoreProperties(ignoreUnknown = true)
public class PublisherWrappedValue extends SysValue {
public enum PathType {
search, create
}

@JsonProperty("_foreigntext")
String _foreigntext;

Expand All @@ -39,6 +43,13 @@ public String getLabel() {
}

public static String getPath() {
return "fs/res/wrapped/publisher";
return getPath(PathType.search);
}

public static String getPath(PathType type) {
return switch (type) {
case create -> "fs/res/publisher";
case search -> "fs/res/wrapped/publisher";
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.uni_jena.thunibib.his.xml;

import com.google.gson.JsonObject;
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 @@ -97,7 +98,7 @@ public Source resolve(String href, String base) throws TransformerException {
case genre -> resolveGenre(value);
case globalIdentifiers -> resolveIdentifierType(value);
case language -> resolveLanguage(value);
case publisher -> Mode.resolve.equals(mode) ? resolvePublisher(value) : SysValue.NotObtainedSysValue;
case publisher -> Mode.resolve.equals(mode) ? resolvePublisher(value) : createPublisher(value);
case peerReviewed -> resolvePeerReviewedType(value);
case publicationAccessType -> resolvePublicationAccessType(value);
case researchAreaKdsf -> resolveResearchAreaKdsf(value);
Expand Down Expand Up @@ -125,25 +126,56 @@ private SysValue resolvePublisher(String value) {
Response response = hisClient.get(PublisherWrappedValue.getPath(), params)) {

if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
return SysValue.NotObtainedSysValue;
return SysValue.ErroneousSysValue;
}

List<PublisherWrappedValue> publishers = response.readEntity(
new GenericType<List<PublisherWrappedValue>>() {
});

List<PublisherWrappedValue> resultList = publishers.stream()
.filter(pwv -> pwv.getUniqueName().equals(decodedValue))
.filter(pwv -> decodedValue.equals(pwv.getUniqueName()))
.toList();

SysValue r = !resultList.isEmpty() ? resultList.get(0) : SysValue.EmptySysValue;
SysValue r = !resultList.isEmpty() ? resultList.get(0) : SysValue.UnresolvedSysValue;
if (r instanceof PublisherWrappedValue) {
PUBLISHER_MAP.put(decodedValue, r);
}
return r;
}
}

/**
* Creates a new publisher. Default language is <em>German</em> and default place is <em>unknown/unbekannt</em>.
* */
private SysValue createPublisher(String value) {
String decodedValue = URLDecoder.decode(value, StandardCharsets.UTF_8);

if (PUBLISHER_MAP.containsKey(decodedValue)) {
return PUBLISHER_MAP.get(decodedValue);
}

LanguageValue languageValue = resolveLanguage("de");

JsonObject language = new JsonObject();
language.addProperty("id", languageValue.getId());

JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("defaulttext", decodedValue);
jsonObject.addProperty("uniquename", decodedValue);
jsonObject.add("language", language);
jsonObject.addProperty("place", "unbekannt");

try (HISInOneClient hisClient = HISinOneClientFactory.create();
Response resp = hisClient.post(PublisherWrappedValue.getPath(PublisherWrappedValue.PathType.create),
jsonObject.toString())) {

PublisherWrappedValue publisher = resp.readEntity(PublisherWrappedValue.class);
PUBLISHER_MAP.put(decodedValue, publisher);
return publisher;
}
}

/**
* Resolves the his-id by the given peerreviewed category id.
*
Expand All @@ -160,7 +192,7 @@ private SysValue resolvePeerReviewedType(String peerReviewedCategId) {
Response response = hisClient.get(PeerReviewedValue.getPath())) {

if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
return SysValue.NotObtainedSysValue;
return SysValue.ErroneousSysValue;
}

List<PeerReviewedValue> prTypes = response.readEntity(
Expand All @@ -178,7 +210,7 @@ private SysValue resolvePeerReviewedType(String peerReviewedCategId) {
}
}

return SysValue.EmptySysValue;
return SysValue.UnresolvedSysValue;
}

/**
Expand Down Expand Up @@ -206,7 +238,7 @@ private SysValue resolvePublicationAccessType(String accessRightsCategId) {
Optional<MCRLabel> label = mcrCategory.getLabel("en");

if (!label.isPresent()) {
return SysValue.EmptySysValue;
return SysValue.UnresolvedSysValue;
}

String text = label.get().getText().toLowerCase(Locale.ROOT);
Expand All @@ -216,7 +248,7 @@ private SysValue resolvePublicationAccessType(String accessRightsCategId) {
.findFirst();

if (first.isEmpty()) {
return SysValue.EmptySysValue;
return SysValue.UnresolvedSysValue;
}

PUBLICATION_ACCESS_TYPE_MAP.put(accessRightsCategId, first.get());
Expand Down Expand Up @@ -253,7 +285,7 @@ private SysValue resolveResearchAreaKdsf(String areaCategId) {
}
}

return SysValue.EmptySysValue;
return SysValue.UnresolvedSysValue;
}

/**
Expand Down Expand Up @@ -284,7 +316,7 @@ private SysValue resolveIdentifierType(String identifierType) {
IDENTIFIER_TYPE_MAP.put(identifierType, type.get());
return type.get();
}
return SysValue.EmptySysValue;
return SysValue.UnresolvedSysValue;
}
}

Expand Down Expand Up @@ -371,7 +403,7 @@ private SysValue resolveSubjectArea(String destatisId) {
SUBJECT_AREA_TYPE_MAP.put(destatisId, areaValue.get());
return areaValue.get();
}
return SysValue.EmptySysValue;
return SysValue.UnresolvedSysValue;
}
}

Expand Down

0 comments on commit 4781cc8

Please sign in to comment.