Skip to content

Commit

Permalink
API Change / Migrate harvester Jeeves services to Spring MVC
Browse files Browse the repository at this point in the history
  • Loading branch information
josegar74 committed Jan 28, 2024
1 parent 59762eb commit e62e11b
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*
* @author delawen
*/
@Deprecated
public class Clear implements Service {
//--------------------------------------------------------------------------
//---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
/**
* TODO javadoc.
*/
@Deprecated
public class CreateClone implements Service {
//--------------------------------------------------------------------------
//---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* "Invoke" service will run a harvester directly, i.e. synchronously. The main purpose for this
* service is testing, i.e. get results immmediately returned in a test script.
*/
@Deprecated
public class Invoke implements Service {
//--------------------------------------------------------------------------
//---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

//=============================================================================

@Deprecated
public class Remove implements Service {
//--------------------------------------------------------------------------
//---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

//=============================================================================

@Deprecated
public class Run implements Service {
//--------------------------------------------------------------------------
//---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

//=============================================================================

@Deprecated
public class Start implements Service {
//--------------------------------------------------------------------------
//---
Expand Down
193 changes: 167 additions & 26 deletions services/src/main/java/org/fao/geonet/api/harvesting/HarvestersApi.java
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-2023 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 @@ -43,6 +43,7 @@
import org.fao.geonet.kernel.harvest.harvester.AbstractHarvester;
import org.fao.geonet.repository.HarvestHistoryRepository;
import org.fao.geonet.repository.SourceRepository;
import org.fao.geonet.services.harvesting.Util;
import org.jdom.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
Expand All @@ -51,12 +52,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
Expand Down Expand Up @@ -86,9 +82,6 @@ public class HarvestersApi {
@io.swagger.v3.oas.annotations.Operation(
summary = "Assign harvester records to a new source",
description = ""
// authorizations = {
// @Authorization(value = "basicAuth")
// })
)
@RequestMapping(
value = "/{harvesterUuid}/assign",
Expand All @@ -108,12 +101,12 @@ public HttpEntity<HttpStatus> assignHarvestedRecordToSource(
description = "The harvester UUID"
)
@PathVariable
String harvesterUuid,
String harvesterUuid,
@Parameter(
description = "The target source UUID"
)
@RequestParam
String source) throws Exception {
String source) throws Exception {
final long elapsedTime = System.currentTimeMillis();
final AbstractHarvester harvester = harvestManager.getHarvester(harvesterUuid);
if (harvester == null) {
Expand All @@ -132,20 +125,20 @@ public HttpEntity<HttpStatus> assignHarvestedRecordToSource(
final List<? extends AbstractMetadata> allHarvestedRecords = metadataRepository.findAllByHarvestInfo_Uuid(harvesterUuid);
List<String> records = new ArrayList<>(allHarvestedRecords.size());

if (allHarvestedRecords.size() < 1) {
if (allHarvestedRecords.isEmpty()) {
throw new NoResultsFoundException(String.format(
"Harvester with UUID '%s' has no record to assign to source '%s'.",
harvesterUuid,
source));
}

for (AbstractMetadata record : allHarvestedRecords) {
record.getSourceInfo().setSourceId(source);
record.getHarvestInfo().setHarvested(false)
for (AbstractMetadata metadataRecord : allHarvestedRecords) {
metadataRecord.getSourceInfo().setSourceId(source);
metadataRecord.getHarvestInfo().setHarvested(false)
.setUri(null)
.setUuid(null);
metadataManager.save(record);
records.add(record.getId() + "");
metadataManager.save(metadataRecord);
records.add(metadataRecord.getId() + "");
}

dataManager.indexMetadata(records);
Expand Down Expand Up @@ -174,13 +167,9 @@ public HttpEntity<HttpStatus> assignHarvestedRecordToSource(
@io.swagger.v3.oas.annotations.Operation(
summary = "Check if a harvester name or host already exist",
description = ""
// authorizations = {
// @Authorization(value = "basicAuth")
// })
)
@RequestMapping(
value = "/properties/{property}",
method = RequestMethod.GET
@GetMapping(
value = "/properties/{property}"
)
@ResponseStatus(value = HttpStatus.OK)
@PreAuthorize("hasAuthority('UserAdmin')")
Expand All @@ -194,12 +183,12 @@ public ResponseEntity<HttpStatus> checkHarvesterPropertyExist(
description = "The harvester property to check"
)
@PathVariable
String property,
String property,
@Parameter(
description = "The value to search"
)
@RequestParam
String exist,
String exist,
HttpServletRequest request) throws Exception {
ServiceContext context = ApiUtils.createServiceContext(request);
final Element list = harvestManager.get(null, context, "site[1]/name[1]");
Expand All @@ -212,4 +201,156 @@ public ResponseEntity<HttpStatus> checkHarvesterPropertyExist(

return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@io.swagger.v3.oas.annotations.Operation(
summary = "Remove a harvester",
description = "")
@DeleteMapping(
value = "/{harvesterIdentifier}")
@ResponseStatus(HttpStatus.NO_CONTENT)
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "Harvester removed."),
@ApiResponse(responseCode = "403", description = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_USER_ADMIN)
})
@PreAuthorize("hasAuthority('UserAdmin')")
public ResponseEntity<HttpStatus> deleteHarvester(
@Parameter(
description = "Harvester identifier",
required = true
)
@PathVariable
Integer harvesterIdentifier,
HttpServletRequest request
) throws Exception {
ServiceContext context = ApiUtils.createServiceContext(request);
Element params = new Element("params");
params.addContent(new Element("id").setText(harvesterIdentifier.toString()));

Util.exec(params, context, (hm, id) -> hm.remove(id));

return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@io.swagger.v3.oas.annotations.Operation(
summary = "Removes the harvester metadata",
description = "")
@PutMapping(
value = "/{harvesterIdentifier}/clear")
@ResponseStatus(HttpStatus.NO_CONTENT)
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "Harvester metadata removed."),
@ApiResponse(responseCode = "403", description = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_USER_ADMIN)
})
@PreAuthorize("hasAuthority('UserAdmin')")
public ResponseEntity<HttpStatus> clearHarvester(
@Parameter(
description = "Harvester identifier",
required = true
)
@PathVariable
Integer harvesterIdentifier,
HttpServletRequest request
) throws Exception {
ServiceContext context = ApiUtils.createServiceContext(request);
Element params = new Element("params");
params.addContent(new Element("id").setText(harvesterIdentifier.toString()));

Util.exec(params, context, (hm, id) -> hm.clearBatch(id));

return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@io.swagger.v3.oas.annotations.Operation(
summary = "Activate a harvester",
description = "")
@PutMapping(
value = "/{harvesterIdentifier}/start")
@ResponseStatus(HttpStatus.NO_CONTENT)
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "Harvester activated."),
@ApiResponse(responseCode = "403", description = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_USER_ADMIN)
})
@PreAuthorize("hasAuthority('UserAdmin')")
public ResponseEntity<HttpStatus> startHarvester(
@Parameter(
description = "Harvester identifier",
required = true
)
@PathVariable
Integer harvesterIdentifier,
HttpServletRequest request
) throws Exception {
ServiceContext context = ApiUtils.createServiceContext(request);
Element params = new Element("params");
params.addContent(new Element("id").setText(harvesterIdentifier.toString()));

Util.exec(params, context, (hm, id) -> hm.start(id));

return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@io.swagger.v3.oas.annotations.Operation(
summary = "Activate and run a harvester",
description = "")
@PutMapping(
value = "/{harvesterIdentifier}/run")
@ResponseStatus(HttpStatus.NO_CONTENT)
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "Harvester executed."),
@ApiResponse(responseCode = "403", description = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_USER_ADMIN)
})
@PreAuthorize("hasAuthority('UserAdmin')")
public ResponseEntity<HttpStatus> runHarvester(
@Parameter(
description = "Harvester identifier",
required = true
)
@PathVariable
Integer harvesterIdentifier,
HttpServletRequest request
) throws Exception {
ServiceContext context = ApiUtils.createServiceContext(request);
Element params = new Element("params");
params.addContent(new Element("id").setText(harvesterIdentifier.toString()));

Util.exec(params, context, (hm, id) -> hm.run(id));

return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@io.swagger.v3.oas.annotations.Operation(
summary = "Create a clone of a harvester",
description = "")
@PutMapping(
value = "/{harvesterIdentifier}/clone")
@ResponseStatus(HttpStatus.CREATED)
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Harvester cloned."),
@ApiResponse(responseCode = "403", description = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_USER_ADMIN),
@ApiResponse(responseCode = "404", description = ApiParams.API_RESPONSE_RESOURCE_NOT_FOUND)
})
@PreAuthorize("hasAuthority('UserAdmin')")
@ResponseBody
public ResponseEntity<Integer> cloneHarvester(
@Parameter(
description = "Harvester identifier",
required = true
)
@PathVariable
Integer harvesterIdentifier,
HttpServletRequest request
) throws Exception {
ServiceContext context = ApiUtils.createServiceContext(request);

String newId = context.getBean(HarvestManager.class).createClone(harvesterIdentifier.toString(), context.getUserSession().getUserId(), context);

if (newId != null) {
return new ResponseEntity<>(Integer.parseInt(newId), HttpStatus.CREATED);
} else {
//--- we get here only if the 'id' was not present or node was not found
throw new ResourceNotFoundException(String.format(
"Harvester with identifier '%d' not found. Cannot clone the harvester.",
harvesterIdentifier));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,12 @@
};

$scope.cloneHarvester = function (id) {
$http.get("admin.harvester.clone?_content_type=json&id=" + id).then(
$http.put("../api/harvesters/" + id + "/clone").then(
function (response) {
$scope.$parent.loadHarvesters().then(function () {
// Select the clone
angular.forEach($scope.$parent.harvesters, function (h) {
if (h["@id"] === response.data[0]) {
if (h["@id"] == response.data) {
$scope.selectHarvester(h);
}
});
Expand Down Expand Up @@ -472,32 +472,24 @@

$scope.deleteHarvester = function () {
$scope.deleting.push($scope.harvesterSelected["@id"]);
return $http
.get(
"admin.harvester.remove?_content_type=json&id=" +
$scope.harvesterSelected["@id"]
)
.then(
function (response) {
$scope.harvesterSelected = {};
$scope.harvesterUpdated = false;
$scope.harvesterNew = false;
$scope.$parent.loadHarvesters();
return $http.delete("../api/harvesters/" + $scope.harvesterSelected["@id"]).then(
function (response) {
$scope.harvesterSelected = {};
$scope.harvesterUpdated = false;
$scope.harvesterNew = false;
$scope.$parent.loadHarvesters();

$scope.deleting.splice($scope.deleting.indexOf(3), 1);
},
function (response) {
console.log(response.data);
}
);
$scope.deleting.splice($scope.deleting.indexOf(3), 1);
},
function (response) {
console.log(response.data);
}
);
};

$scope.deleteHarvesterRecord = function () {
return $http
.get(
"admin.harvester.clear?_content_type=json&id=" +
$scope.harvesterSelected["@id"]
)
.put("../api/harvesters/" + $scope.harvesterSelected["@id"] + "/clear")
.then(
function (response) {
$scope.harvesterSelected = {};
Expand Down Expand Up @@ -543,9 +535,7 @@
};
$scope.runHarvester = function () {
return $http
.get(
"admin.harvester.run?_content_type=json&id=" + $scope.harvesterSelected["@id"]
)
.put("../api/harvesters/" + $scope.harvesterSelected["@id"] + "/run")
.then(function (response) {
$scope.$parent.loadHarvesters().then(function () {
refreshSelectedHarvester();
Expand Down

0 comments on commit e62e11b

Please sign in to comment.