Skip to content

Commit

Permalink
Spring doc API for link api should accept a structured object instead…
Browse files Browse the repository at this point in the history
… of JSONObject (#7585)

* API should accept a structured object instead of JSONObject
Updated link search to accept an object LinkFilter instead of JSONObject

* Fix error with Failed to convert value of type by adding a @InitBinder
  • Loading branch information
ianwallen authored Jan 4, 2024
1 parent 9285136 commit b892b26
Showing 1 changed file with 65 additions and 15 deletions.
80 changes: 65 additions & 15 deletions services/src/main/java/org/fao/geonet/api/links/LinksApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.gson.Gson;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
Expand Down Expand Up @@ -52,7 +54,6 @@
import org.fao.geonet.repository.specification.LinkSpecs;
import org.jdom.JDOMException;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Page;
Expand All @@ -64,6 +65,7 @@
import org.springframework.jmx.export.naming.SelfNaming;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

Expand All @@ -72,6 +74,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -146,9 +150,9 @@ public void iniMBeansSlidingWindowWithEmptySlot() {
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
public Page<Link> getRecordLinks(
@Parameter(description = "Filter, e.g. \"{url: 'png', lastState: 'ko', records: 'e421', groupId: 12}\", lastState being 'ok'/'ko'/'unknown'", required = false)
@Parameter(description = "Filter, e.g. \"{url: 'png', lastState: 'ko', records: 'e421'}\", lastState being 'ok'/'ko'/'unknown'", required = false)
@RequestParam(required = false)
JSONObject filter,
LinkFilter filter,
@Parameter(description = "Optional, filter links to records published in that group.", required = false)
@RequestParam(required = false)
Integer[] groupIdFilter,
Expand Down Expand Up @@ -187,9 +191,9 @@ public Page<Link> getRecordLinks(
produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
public Page<Link> getRecordLinksPost(
@Parameter(description = "Filter, e.g. \"{url: 'png', lastState: 'ko', records: 'e421', groupId: 12}\", lastState being 'ok'/'ko'/'unknown'", required = false)
@Parameter(description = "Filter, e.g. \"{url: 'png', lastState: 'ko', records: 'e421'}\", lastState being 'ok'/'ko'/'unknown'", required = false)
@RequestParam(required = false)
JSONObject filter,
LinkFilter filter,
@Parameter(description = "Optional, filter links to records published in that group.", required = false)
@RequestParam(required = false)
Integer[] groupIdFilter,
Expand All @@ -207,7 +211,7 @@ public Page<Link> getRecordLinksPost(
return getLinks(filter, groupIdFilter, groupOwnerIdFilter, pageRequest, userSession);
}
private Page<Link> getLinks(
JSONObject filter,
LinkFilter filter,
Integer[] groupIdFilter,
Integer[] groupOwnerIdFilter,
Pageable pageRequest,
Expand All @@ -228,22 +232,22 @@ private Page<Link> getLinks(
Integer stateToMatch = null;
String url = null;
List<String> associatedRecords = null;
if (filter.has("lastState")) {
if (filter.getLastState() != null) {
stateToMatch = 0;
if (filter.getString("lastState").equalsIgnoreCase("ok")) {
if (filter.getLastState().equalsIgnoreCase("ok")) {
stateToMatch = 1;
} else if (filter.getString("lastState").equalsIgnoreCase("ko")) {
} else if (filter.getLastState().equalsIgnoreCase("ko")) {
stateToMatch = -1;
}
}

if (filter.has("url")) {
url = filter.getString("url");
if (filter.getUrl() != null) {
url = filter.getUrl();
}

if (filter.has("records")) {
if (filter.getRecords() != null) {
associatedRecords = Arrays.stream(
filter.getString("records").split(" ")
filter.getRecords().split(" ")
).collect(Collectors.toList());
}

Expand Down Expand Up @@ -277,9 +281,9 @@ private Page<Link> getLinks(
@PreAuthorize("isAuthenticated()")
@ResponseBody
public void getRecordLinksAsCsv(
@Parameter(description = "Filter, e.g. \"{url: 'png', lastState: 'ko', records: 'e421', groupId: 12}\", lastState being 'ok'/'ko'/'unknown'", required = false)
@Parameter(description = "Filter, e.g. \"{url: 'png', lastState: 'ko', records: 'e421'}\", lastState being 'ok'/'ko'/'unknown'", required = false)
@RequestParam(required = false)
JSONObject filter,
LinkFilter filter,
@Parameter(description = "Optional, filter links to records published in that group.", required = false)
@RequestParam(required = false)
Integer[] groupIdFilter,
Expand Down Expand Up @@ -445,4 +449,50 @@ private MAnalyseProcess getRegistredMAnalyseProcess() {
mAnalyseProcesses.addFirst(mAnalyseProcess);
return mAnalyseProcess;
}

@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.registerCustomEditor(LinkFilter.class, new PropertyEditorSupport() {
Object value;
@Override
public Object getValue() {
return value;
}

@Override
public void setAsText(String text) throws IllegalArgumentException {
value = new Gson().fromJson(text, LinkFilter.class);
}
});
}

private static class LinkFilter {
private String url;
private String lastState;
private String records;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getLastState() {
return lastState;
}

public void setLastState(String lastState) {
this.lastState = lastState;
}

public String getRecords() {
return records;
}

public void setRecords(String records) {
this.records = records;
}
}
}

0 comments on commit b892b26

Please sign in to comment.