Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring doc API for link api should accept a structured object instead of JSONObject #7585

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
Loading