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

CIRCSTORE- 519 Update instead save with the same name circ settings. #477

Closed
wants to merge 8 commits into from
2 changes: 1 addition & 1 deletion descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
},
{
"id": "request-storage",
"version": "6.0",
"version": "6.1",
"handlers": [
{
"methods": ["GET"],
Expand Down
5 changes: 5 additions & 0 deletions ramls/request.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
"type": "string",
"enum": ["Hold", "Recall", "Page"]
},
"ecsRequestPhase": {
"description": "Stage in ECS request process, absence of this field means this is a single-tenant request",
"type": "string",
"enum": ["Primary", "Secondary"]
},
"requestDate": {
"description": "Date the request was made",
"type": "string",
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/org/folio/rest/impl/CirculationSettingsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@
import io.vertx.core.Context;
import io.vertx.core.Handler;

import static io.vertx.core.Future.succeededFuture;
import static org.folio.rest.jaxrs.resource.CirculationSettingsStorage.PostCirculationSettingsStorageCirculationSettingsResponse.headersFor201;
import static org.folio.rest.jaxrs.resource.CirculationSettingsStorage.PostCirculationSettingsStorageCirculationSettingsResponse.respond201WithApplicationJson;
import static org.folio.rest.jaxrs.resource.CirculationSettingsStorage.PostCirculationSettingsStorageCirculationSettingsResponse.respond500WithTextPlain;
import static org.folio.rest.tools.utils.ValidationHelper.isDuplicate;

public class CirculationSettingsAPI implements CirculationSettingsStorage {

@Override
public void postCirculationSettingsStorageCirculationSettings(String lang,
CirculationSetting circulationSettings, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {

CirculationSetting circulationSettings, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
new CirculationSettingsService(vertxContext, okapiHeaders)
.create(circulationSettings)
.onComplete(asyncResultHandler);
.onSuccess(reply -> asyncResultHandler.handle(succeededFuture(respond201WithApplicationJson(circulationSettings, headersFor201()))))
.onFailure(errorReply -> asyncResultHandler.handle(succeededFuture(respond500WithTextPlain(errorReply.getMessage()))));
}

@Override
Expand Down
55 changes: 41 additions & 14 deletions src/main/java/org/folio/service/CirculationSettingsService.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package org.folio.service;

import static org.folio.service.event.EntityChangedEventPublisherFactory.circulationSettingsEventPublisher;
import static org.folio.support.ModuleConstants.CIRCULATION_SETTINGS_TABLE;

import java.util.Map;

import javax.ws.rs.core.Response;

import io.vertx.core.Context;
import io.vertx.core.Future;;
import org.folio.persist.CirculationSettingsRepository;
import org.folio.rest.jaxrs.model.CirculationSetting;
import org.folio.rest.jaxrs.model.CirculationSettings;
import org.folio.rest.jaxrs.model.Value;
import org.folio.rest.jaxrs.resource.CirculationSettingsStorage.DeleteCirculationSettingsStorageCirculationSettingsByCirculationSettingsIdResponse;
import org.folio.rest.jaxrs.resource.CirculationSettingsStorage.GetCirculationSettingsStorageCirculationSettingsByCirculationSettingsIdResponse;
import org.folio.rest.jaxrs.resource.CirculationSettingsStorage.GetCirculationSettingsStorageCirculationSettingsResponse;
import org.folio.rest.jaxrs.resource.CirculationSettingsStorage.PostCirculationSettingsStorageCirculationSettingsResponse;
import org.folio.rest.jaxrs.resource.CirculationSettingsStorage.PutCirculationSettingsStorageCirculationSettingsByCirculationSettingsIdResponse;
import org.folio.rest.persist.Criteria.Criteria;
import org.folio.rest.persist.Criteria.Criterion;
import org.folio.rest.persist.PgUtil;
import org.folio.service.event.EntityChangedEventPublisher;

import io.vertx.core.Context;
import io.vertx.core.Future;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;

import static org.folio.rest.tools.utils.ValidationHelper.isDuplicate;
import static org.folio.service.event.EntityChangedEventPublisherFactory.circulationSettingsEventPublisher;
import static org.folio.support.ModuleConstants.CIRCULATION_SETTINGS_TABLE;

public class CirculationSettingsService {

Expand All @@ -42,10 +44,35 @@ public Future<Response> getAll(int offset, int limit, String query) {
GetCirculationSettingsStorageCirculationSettingsResponse.class);
}

public Future<Response> create(CirculationSetting circulationSetting) {
return PgUtil.post(CIRCULATION_SETTINGS_TABLE, circulationSetting, okapiHeaders, vertxContext,
PostCirculationSettingsStorageCirculationSettingsResponse.class)
.compose(eventPublisher.publishCreated());
public Future<CirculationSetting> create(CirculationSetting circulationSetting) {
return repository.save(circulationSetting.getId(), circulationSetting)
.map(circulationSetting)
.recover(throwable -> updateSettingsValue(circulationSetting, throwable));
}

private Future<List<CirculationSetting>> getSettingsByName(String settingsName) {
Criterion filter = new Criterion(new Criteria()
.addField("'name'")
.setOperation("=")
.setVal(settingsName));

return repository.get(filter);
}

private Future<CirculationSetting> updateSettings(List<CirculationSetting> settings, CirculationSetting circulationSetting) {
Value value = circulationSetting.getValue();
settings.forEach(setting -> setting.setValue(value));
return repository.update(settings)
.map(circulationSetting);
}

private Future<CirculationSetting> updateSettingsValue(CirculationSetting circulationSetting, Throwable throwable) {
if (!isDuplicate(throwable.getMessage())) {
return Future.failedFuture(throwable);
}

return getSettingsByName(circulationSetting.getName())
.compose(settings -> updateSettings(settings, circulationSetting));
}

public Future<Response> findById(String circulationSettingsId) {
Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/templates/db_scripts/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,14 @@
{
"tableName": "circulation_settings",
"withMetadata": true,
"withAuditing": false
"withAuditing": false,
"uniqueIndex": [
{
"fieldName": "name",
"tOps": "ADD",
"caseSensitive": false
}
]
},
{
"tableName": "request",
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/org/folio/rest/api/RequestsApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,46 @@ public void cannotCreateRequestWithoutStatus()
)));
}

@Test
public void canCreateRequestWithEcsRequestPhase() throws MalformedURLException,
ExecutionException, InterruptedException, TimeoutException {

JsonObject representation = createEntity(
new RequestRequestBuilder()
.page()
.primary()
.withId(UUID.randomUUID())
.create(),
requestStorageUrl()).getJson();
assertThat(representation.getString("ecsRequestPhase"), is("Primary"));

representation = createEntity(
new RequestRequestBuilder()
.page()
.secondary()
.withId(UUID.randomUUID())
.create(),
requestStorageUrl()).getJson();
assertThat(representation.getString("ecsRequestPhase"), is("Secondary"));
}

@Test
public void shouldReturn400IfInvalidEcsRequestPhase() throws MalformedURLException,
ExecutionException, InterruptedException, TimeoutException {

var request = new RequestRequestBuilder()
.page()
.withEcsRequestPhase("Invalid")
.withId(UUID.randomUUID())
.create();

CompletableFuture<JsonResponse> createCompleted = new CompletableFuture<>();
client.post(requestStorageUrl(), request, TENANT_ID, ResponseHandler.json(createCompleted));

assertThat(createCompleted.get(5, TimeUnit.SECONDS).getStatusCode(), is(400));
}


private RequestDto.RequestDtoBuilder holdShelfOpenRequest() {
return RequestDto.builder()
.requesterId(UUID.randomUUID().toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class RequestRequestBuilder extends JsonBuilder {
private final String patronComments;
private final UUID holdingsRecordId;
private final SearchIndex searchIndex;
private final String ecsRequestPhase;

public RequestRequestBuilder() {
this(UUID.randomUUID(),
Expand Down Expand Up @@ -79,6 +80,7 @@ public RequestRequestBuilder() {
null,
null,
UUID.randomUUID(),
null,
null);
}

Expand All @@ -101,6 +103,7 @@ public JsonObject create() {
put(request, "requestExpirationDate", this.requestExpirationDate);
put(request, "holdShelfExpirationDate", this.holdShelfExpirationDate);
put(request, "pickupServicePointId", this.pickupServicePointId);
put(request, "ecsRequestPhase", this.ecsRequestPhase);

if (this.itemSummary != null) {
final JsonObject item = new JsonObject();
Expand Down Expand Up @@ -204,7 +207,8 @@ public RequestRequestBuilder withNoId() {
this.tags,
this.patronComments,
this.holdingsRecordId,
this.searchIndex);
this.searchIndex,
this.ecsRequestPhase);
}

public RequestRequestBuilder toHoldShelf() {
Expand Down Expand Up @@ -247,7 +251,8 @@ public RequestRequestBuilder withItem(RequestItemSummary item) {
this.tags,
this.patronComments,
this.holdingsRecordId,
this.searchIndex);
this.searchIndex,
this.ecsRequestPhase);
}

public RequestRequestBuilder withRequester(
Expand Down Expand Up @@ -282,7 +287,8 @@ public RequestRequestBuilder withRequester(
this.tags,
this.patronComments,
this.holdingsRecordId,
this.searchIndex);
this.searchIndex,
this.ecsRequestPhase);
}

public RequestRequestBuilder withRequester(
Expand Down Expand Up @@ -316,7 +322,8 @@ public RequestRequestBuilder withRequester(
this.tags,
this.patronComments,
this.holdingsRecordId,
this.searchIndex);
this.searchIndex,
this.ecsRequestPhase);
}

public RequestRequestBuilder withProxy(
Expand Down Expand Up @@ -350,7 +357,8 @@ public RequestRequestBuilder withProxy(
this.tags,
this.patronComments,
this.holdingsRecordId,
this.searchIndex);
this.searchIndex,
this.ecsRequestPhase);
}
public RequestRequestBuilder withNoPosition() {
return withPosition(null);
Expand All @@ -375,4 +383,12 @@ private class PatronSummary {
}
}

public RequestRequestBuilder primary() {
return withEcsRequestPhase("Primary");
}

public RequestRequestBuilder secondary() {
return withEcsRequestPhase("Secondary");
}

}