Skip to content

Commit

Permalink
Metadata extents API - fix service for metadata with working copy
Browse files Browse the repository at this point in the history
The API accepts an optional parameter to retrieve the approved or the working copy versions.
  • Loading branch information
josegar74 authored and fxprunayre committed Jun 19, 2024
1 parent 3605e9f commit ab94f23
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2001-2021 Food and Agriculture Organization of the
* Copyright (C) 2001-2024 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
Expand Down Expand Up @@ -45,7 +45,6 @@
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;
Expand All @@ -65,7 +64,7 @@
})
@Tag(name = API_CLASS_RECORD_TAG,
description = API_CLASS_RECORD_OPS)
@Controller("recordExtent")
@RestController("recordExtent")
@ReadWriteController
public class MetadataExtentApi {

Expand Down Expand Up @@ -118,12 +117,11 @@ public class MetadataExtentApi {
@io.swagger.v3.oas.annotations.Operation(
summary = "Get record extents as image",
description = API_EXTENT_DESCRIPTION)
@RequestMapping(
@GetMapping(
value = "/{metadataUuid}/extents.png",
produces = {
MediaType.IMAGE_PNG_VALUE
},
method = RequestMethod.GET)
})
public HttpEntity<byte[]> getAllRecordExtentAsImage(
@Parameter(
description = API_PARAM_RECORD_UUID,
Expand All @@ -144,6 +142,8 @@ public HttpEntity<byte[]> getAllRecordExtentAsImage(
@Parameter(description = API_PARAM_STROKE_DESCRIPTION)
@RequestParam(value = "", required = false, defaultValue = "0,0,0,255")
String strokeColor,
@RequestParam(required = false, defaultValue = "true")
Boolean approved,
@Parameter(hidden = true)
NativeWebRequest nativeWebRequest,
@Parameter(hidden = true)
Expand All @@ -157,21 +157,19 @@ public HttpEntity<byte[]> getAllRecordExtentAsImage(
"EPSG:4326");
}

return getExtent(metadataUuid, srs, width, height, background, fillColor, strokeColor, null, nativeWebRequest, request);
return getExtent(metadataUuid, srs, width, height, background, fillColor, strokeColor, null, approved, nativeWebRequest, request);
}



@io.swagger.v3.oas.annotations.Operation(
summary = "Get list of record extents",
description = API_EXTENT_DESCRIPTION)
@RequestMapping(
@GetMapping(
value = "/{metadataUuid}/extents.json",
produces = {
MediaType.APPLICATION_JSON_VALUE
},
method = RequestMethod.GET)
@ResponseBody
})
public List<ExtentDto> getAllRecordExtentAsJson(
@Parameter(
description = API_PARAM_RECORD_UUID,
Expand Down Expand Up @@ -215,7 +213,7 @@ public List<ExtentDto> getAllRecordExtentAsJson(
String.format("%sapi/records/%s/extents/%d.png",
settingManager.getNodeURL(), metadataUuid, index),
extentElement.getName(),
XPath.getXPath(xmlData, (Element) extent),
XPath.getXPath(xmlData, extent),
description));
index ++;
}
Expand All @@ -227,12 +225,11 @@ public List<ExtentDto> getAllRecordExtentAsJson(
@io.swagger.v3.oas.annotations.Operation(
summary = "Get one record extent as image",
description = API_EXTENT_DESCRIPTION)
@RequestMapping(
@GetMapping(
value = "/{metadataUuid}/extents/{geometryIndex}.png",
produces = {
MediaType.IMAGE_PNG_VALUE
},
method = RequestMethod.GET)
})
public HttpEntity<byte[]> getOneRecordExtentAsImage(
@Parameter(
description = API_PARAM_RECORD_UUID,
Expand All @@ -256,6 +253,8 @@ public HttpEntity<byte[]> getOneRecordExtentAsImage(
@Parameter(description = API_PARAM_STROKE_DESCRIPTION)
@RequestParam(value = "", required = false, defaultValue = "0,0,0,255")
String strokeColor,
@RequestParam(required = false, defaultValue = "true")
Boolean approved,
@Parameter(hidden = true)
NativeWebRequest nativeWebRequest,
@Parameter(hidden = true)
Expand All @@ -269,11 +268,13 @@ public HttpEntity<byte[]> getOneRecordExtentAsImage(
"EPSG:4326");
}

return getExtent(metadataUuid, srs, width, height, background, fillColor, strokeColor, geometryIndex, nativeWebRequest, request);
return getExtent(metadataUuid, srs, width, height, background, fillColor, strokeColor, geometryIndex, approved, nativeWebRequest, request);
}

private HttpEntity<byte[]> getExtent(String metadataUuid, String srs, Integer width, Integer height, String background, String fillColor, String strokeColor, Integer extentOrderOfAppearance, NativeWebRequest nativeWebRequest, HttpServletRequest request) throws Exception {
AbstractMetadata metadata = ApiUtils.canViewRecord(metadataUuid, request);
private HttpEntity<byte[]> getExtent(String metadataUuid, String srs, Integer width, Integer height, String background, String fillColor, String strokeColor,
Integer extentOrderOfAppearance, Boolean approved,
NativeWebRequest nativeWebRequest, HttpServletRequest request) throws Exception {
AbstractMetadata metadata = ApiUtils.canViewRecord(metadataUuid, approved, request);
ServiceContext context = ApiUtils.createServiceContext(request);

if (width != null && height != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2001-2016 Food and Agriculture Organization of the
* Copyright (C) 2001-2024 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
Expand All @@ -24,24 +24,22 @@
package org.fao.geonet.api.regions.metadata;

import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import jeeves.server.context.ServiceContext;
import org.fao.geonet.ApplicationContextHolder;
import org.fao.geonet.GeonetContext;
import org.fao.geonet.api.regions.MetadataRegion;
import org.fao.geonet.constants.Geonet;
import org.fao.geonet.domain.ISODate;
import org.fao.geonet.domain.Metadata;
import org.fao.geonet.domain.ReservedOperation;
import org.fao.geonet.kernel.DataManager;
import org.fao.geonet.kernel.datamanager.IMetadataSchemaUtils;
import org.fao.geonet.kernel.datamanager.IMetadataUtils;
import org.fao.geonet.kernel.region.Region;
import org.fao.geonet.kernel.region.Request;
import org.fao.geonet.kernel.schema.MetadataSchema;
import org.fao.geonet.kernel.search.EsSearchManager;
import org.fao.geonet.kernel.search.spatial.ErrorHandler;
import org.fao.geonet.lib.Lib;
import org.fao.geonet.repository.MetadataRepository;
import org.fao.geonet.util.GMLParsers;
import org.fao.geonet.utils.Log;
import org.fao.geonet.utils.Xml;
Expand Down Expand Up @@ -97,16 +95,16 @@ public Collection<Region> execute() throws Exception {
if (label == null && id == null || (id != null && !id.startsWith(PREFIX))) {
return Collections.emptySet();
}
List<Region> regions = new ArrayList<Region>();
List<Region> regions = new ArrayList<>();
if (label != null) {
loadAll(regions, Id.create(label));
} else if (id != null) {
String[] parts = id.split(":", 3);
String mdId = parts[1];
String id;
String identifier;
if (parts.length > 2) {
id = parts[2];
loadOnly(regions, Id.create(mdId), id);
identifier = parts[2];
loadOnly(regions, Id.create(mdId), identifier);
} else {
loadSpatialExtent(regions, Id.create(mdId));
}
Expand Down Expand Up @@ -189,13 +187,13 @@ private double getCoordinateValue(String coord, Element bbox) {
}
}
return 0;
};
}

Region parseRegion(Id mdId, Element extentObj) throws Exception {
GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
gc.getBean(DataManager.class).getEditLib().removeEditingInfo(extentObj);

String id = null;
String regionId = null;
Geometry geometry = null;
Namespace objNamespace = extentObj.getNamespace();
if ("polygon".equals(extentObj.getName())) {
Expand Down Expand Up @@ -227,9 +225,9 @@ Region parseRegion(Id mdId, Element extentObj) throws Exception {
if (geometry != null) {
Element element = extentObj.getChild("element", Geonet.Namespaces.GEONET);
if (element != null) {
id = element.getAttributeValue("ref");
regionId = element.getAttributeValue("ref");
}
return new MetadataRegion(mdId, id, geometry);
return new MetadataRegion(mdId, regionId, geometry);
} else {
return null;
}
Expand Down Expand Up @@ -281,9 +279,7 @@ public Optional<Long> getLastModified() throws Exception {
final String mdId = MetadataRegionSearchRequest.Id.create(idParts[0]).getMdId();

if (mdId != null) {
Metadata metadata = ApplicationContextHolder.get().getBean(MetadataRepository.class).findOneById(Integer.valueOf(mdId));

final ISODate docChangeDate = metadata.getDataInfo().getChangeDate();
final ISODate docChangeDate = ApplicationContextHolder.get().getBean(EsSearchManager.class).getDocChangeDate(mdId);
if (docChangeDate != null) {
return Optional.of(docChangeDate.toDate().getTime());
}
Expand Down

0 comments on commit ab94f23

Please sign in to comment.