Skip to content

Commit

Permalink
#550 Fetch project data from HISinOne
Browse files Browse the repository at this point in the history
  • Loading branch information
Possommi committed Dec 13, 2024
1 parent 029e2a1 commit 9ea745f
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@ enum AuthType {
Basic, Bearer
}

Response get(String path, Map<String, String> params);
default Response get(String path) {
return get(path, false);
}

Response get(String path);
default Response get(String path, Map<String, String> params) {
return get(path, params, false);
}

Response put(String path, String body);
default Response get(String path, boolean omitToken){
return get(path, null, omitToken);
}

Response get(String path, Map<String, String> params, boolean omitToken);

Response post(String path, String body);

Response put(String path, String body);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class HISInOneClientDefaultImpl implements HISInOneClient {
MCRShutdownHandler.getInstance().addCloseable(this);
}

public Response get(String path, Map<String, String> parameters) {
@Override
public Response get(String path, Map<String, String> parameters, boolean omitToken) {
WebTarget webTarget = getJerseyClient()
.target(HISInOneClientDefaultImpl.HIS_IN_ONE_BASE_URL + API_PATH);

Expand All @@ -46,21 +47,27 @@ public Response get(String path, Map<String, String> parameters) {

webTarget = webTarget.path(path);

Token token;
try {
token = fetchToken();
} catch (Exception e) {
LOGGER.error("Could not fetch token", e);
return Response.serverError().entity(e.getMessage()).build();
Token token = null;
if (!omitToken) {
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()));
if (token != null) {
invocationBuilder.header("Authorization",
getAuthorizationHeaderValue(AuthType.Bearer, token.getAccessToken()));
}

Response response = invocationBuilder.get();
return response;
}

@Override
public Response post(String path, String bodySource) {
WebTarget webTarget = getJerseyClient()
.target(HISInOneClientDefaultImpl.HIS_IN_ONE_BASE_URL + API_PATH)
Expand All @@ -83,6 +90,7 @@ public Response post(String path, String bodySource) {
return response;
}

@Override
public Response put(String path, String bodySource) {
WebTarget webTarget = getJerseyClient()
.target(HISInOneClientDefaultImpl.HIS_IN_ONE_BASE_URL + API_PATH)
Expand All @@ -105,10 +113,6 @@ public Response put(String path, String bodySource) {
return response;
}

public Response get(String path) {
return get(path, null);
}

protected Token fetchToken() {
WebTarget webTarget = getJerseyClient()
.target(HISInOneClientDefaultImpl.HIS_IN_ONE_BASE_URL + API_PATH)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package de.uni_jena.thunibib.his.rsc;

import de.uni_jena.thunibib.his.api.client.HISInOneClient;
import de.uni_jena.thunibib.his.api.client.HISinOneClientFactory;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mycore.frontend.jersey.access.MCRRequireLogin;
import org.mycore.frontend.jersey.filter.access.MCRRestrictedAccess;

import java.util.HashMap;

@Path("/project")
public class HISinOneProjectsResource {
private static final Logger LOGGER = LogManager.getLogger(HISinOneProjectsResource.class);

@GET
@MCRRestrictedAccess(MCRRequireLogin.class)
@Produces(MediaType.APPLICATION_JSON)
public Response searchProjects(@QueryParam("q") String q) {
HashMap<String, String> p = new HashMap<>();
p.put("q", q);

try (HISInOneClient client = HISinOneClientFactory.create();
Response resp = client.get("fs/res/project", p, true)) {
String json = resp.readEntity(String.class);
return Response.ok(json).build();
}
}

@GET
@MCRRestrictedAccess(MCRRequireLogin.class)
@Path("{projectId:.+}")
@Produces(MediaType.APPLICATION_JSON)
public Response getProject(@PathParam("projectId") String projectId) {
try (HISInOneClient client = HISinOneClientFactory.create();
Response resp = client.get("fs/res/project/" + projectId, true)) {
String json = resp.readEntity(String.class);
return Response.ok(json).build();
}
}
}
49 changes: 49 additions & 0 deletions common/src/main/resources/META-INF/resources/import-editor.xed
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,55 @@
</div>
</xed:template>

<xed:template id="projectInformation.HISinOne">
<xed:bind xpath="mods:extension/temporary_search_input">
<div class="form-group form-inline">
<label for="{xed:generate-id()}" class="mycore-form-label">
<xed:output value="concat(i18n:translate('ubo.project.search'), ': ')"/>
</label>
<input id="project-search-his-in-one" type="text" class="mycore-form-input"/>
<button type="button" class="btn btn-primary mr-2" onclick="$('#project-search-his-in-one, #project-title, #project-id, #project-shorttext').val('');">
<i class="fas fa-minus"/>
</button>
<script src="js/HISinOneProjects.js"/>
</div>
</xed:bind>

<xed:bind xpath="mods:extension[@displayLabel='project']">
<xed:bind xpath="cerif:Project">
<!-- title -->
<xed:bind xpath="cerif:Title">
<div class="form-group form-inline">
<label for="{xed:generate-id()}" class="mycore-form-label">
<xed:output value="concat(i18n:translate('ubo.project.title'), ': ')"/>
</label>
<input id="project-title" readonly="readonly" type="text" class="mycore-form-input readonly"/>
</div>
</xed:bind>

<!-- shorttext -->
<xed:bind xpath="cerif:Acronym">
<div class="form-group form-inline">
<label for="{xed:generate-id()}" class="mycore-form-label">
<xed:output value="concat(i18n:translate('ubo.project.acronym'), ': ')"/>
</label>
<input id="project-shorttext" readonly="readonly" type="text" class="mycore-form-input readonly"/>
</div>
</xed:bind>

<!-- id -->
<xed:bind xpath="cerif:Identifier[@type='https://w3id.org/cerif/vocab/IdentifierTypes#Project-MM-ID']">
<div class="form-group form-inline">
<label for="{xed:generate-id()}" class="mycore-form-label">
<xed:output value="'HIS-ID: '"/>
</label>
<input id="project-id" readonly="readonly" type="text" class="mycore-form-input readonly"/>
</div>
</xed:bind>
</xed:bind>
</xed:bind>
</xed:template>

<xed:template id="projectInformation">
<script src="{$WebApplicationBaseURL}js/project_autocomplete.js"/>
<xed:bind xpath="mods:extension/temporary_search_input">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const HisinOneProjects = {
search: async function (text) {
const q = await fetch("../rsc/project?q=" + text);
let json = await q.json();

let projects = [];
json.forEach((project) => {
let item = {};
item.id = project.id;
item.label = project.shorttext + ": " + project.defaulttext + " [" + project.id + "]";
item.defaultText = project.defaulttext;
item.shortText = project.shorttext;
projects.push(item);
})

return projects;
},

onProjectSelected: function (event, ui) {
$("#project-search-his-in-one").val('');
$("#project-id").val(ui.item.id);
$("#project-title").val(ui.item.defaultText);
$("#project-shorttext").val(ui.item.shortText);
return false;
},

onInput: async function (event) {
let input = $("#project-search-his-in-one");
input.autocomplete({
delay: 300,
minLength: 0,
source: await HisinOneProjects.search(event.target.value),
select: HisinOneProjects.onProjectSelected
});
}
};

document.addEventListener('DOMContentLoaded', function () {
document.getElementById('project-search-his-in-one').addEventListener('input', HisinOneProjects.onInput);
});
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,4 @@ MCR.ContentTransformer.mods-resolve-create-his-keys.Class = org.my
MCR.ContentTransformer.mods-resolve-create-his-keys.Steps = mods-xml-detailed,mods-resolve-his-keys,mods-create-his-keys

MCR.URIResolver.ModuleResolver.hisinone = de.uni_jena.thunibib.his.xml.HISinOneResolver
MCR.Jersey.Resource.Packages = %MCR.Jersey.Resource.Packages%,de.uni_jena.thunibib.his.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
as of https://cmswiki.rrz.uni-hamburg.de/hummel/MyCoRe/Organisation/AnwenderWorkshop2018?action=AttachFile&do=view&target=MyCoRe+XEditor+Erweiterungen+2018-11-14.pdf
-->
<xed:modify ref="editor-form-fields">
<xed:include ref="fundingType" after="projectInformation"/>
<xed:include ref="projectInformation.HISinOne" after="projectInformation"/>
<xed:include ref="fundingType" after="projectInformation.HISinOne"/>

<xed:remove ref="projectInformation"/>
</xed:modify>

<xed:template id="fundingType">
Expand Down

0 comments on commit 9ea745f

Please sign in to comment.