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. #479

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
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
4 changes: 4 additions & 0 deletions src/main/java/org/folio/persist/AbstractRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ protected AbstractRepository(PostgresClient postgresClient, String tableName,
this.recordType = recordType;
}

public Future<T> saveAndReturnUpdatedEntity(String id, T entity) {
return postgresClient.saveAndReturnUpdatedEntity(tableName, id, entity);
}

public Future<String> save(String id, T entity) {
return postgresClient.save(tableName, id, entity);
}
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/org/folio/rest/impl/CirculationSettingsAPI.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.folio.rest.impl;

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 java.util.Map;

import javax.ws.rs.core.Response;
Expand All @@ -21,7 +26,10 @@ public void postCirculationSettingsStorageCirculationSettings(String lang,

new CirculationSettingsService(vertxContext, okapiHeaders)
.create(circulationSettings)
.onComplete(asyncResultHandler);
.onSuccess(response -> asyncResultHandler.handle(
succeededFuture(respond201WithApplicationJson(circulationSettings, headersFor201()))))
.onFailure(throwable -> asyncResultHandler.handle(
succeededFuture(respond500WithTextPlain(throwable.getMessage()))));
}

@Override
Expand All @@ -39,9 +47,9 @@ public void getCirculationSettingsStorageCirculationSettingsByCirculationSetting
String circulationSettingsId, String lang, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {

new CirculationSettingsService(vertxContext, okapiHeaders)
.findById(circulationSettingsId)
.onComplete(asyncResultHandler);
new CirculationSettingsService(vertxContext, okapiHeaders)
.findById(circulationSettingsId)
.onComplete(asyncResultHandler);
}

@Override
Expand All @@ -50,18 +58,18 @@ public void putCirculationSettingsStorageCirculationSettingsByCirculationSetting
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {

new CirculationSettingsService(vertxContext, okapiHeaders)
.update(circulationSettingsId, entity)
.onComplete(asyncResultHandler);
new CirculationSettingsService(vertxContext, okapiHeaders)
.update(circulationSettingsId, entity)
.onComplete(asyncResultHandler);
}

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

new CirculationSettingsService(vertxContext, okapiHeaders)
.delete(circulationSettingsId)
.onComplete(asyncResultHandler);
new CirculationSettingsService(vertxContext, okapiHeaders)
.delete(circulationSettingsId)
.onComplete(asyncResultHandler);
}
}
68 changes: 57 additions & 11 deletions src/main/java/org/folio/service/CirculationSettingsService.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
package org.folio.service;

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;

import java.util.List;
import java.util.Map;

import javax.ws.rs.core.Response;

import lombok.extern.log4j.Log4j2;
import org.folio.persist.CirculationSettingsRepository;
import org.folio.rest.jaxrs.model.CirculationSetting;
import org.folio.rest.jaxrs.model.CirculationSettings;
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;

@Log4j2
public class CirculationSettingsService {

private final Context vertxContext;
Expand All @@ -42,10 +47,11 @@ 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) {
log.debug("create:: trying to save circulationSetting: {}", circulationSetting);
return repository.saveAndReturnUpdatedEntity(circulationSetting.getId(),
circulationSetting)
.recover(throwable -> updateSettingsValue(circulationSetting, throwable));
}

public Future<Response> findById(String circulationSettingsId) {
Expand All @@ -54,17 +60,57 @@ public Future<Response> findById(String circulationSettingsId) {
GetCirculationSettingsStorageCirculationSettingsByCirculationSettingsIdResponse.class);
}

public Future<Response> update(String circulationSettingsId, CirculationSetting circulationSetting) {
return PgUtil.put(CIRCULATION_SETTINGS_TABLE, circulationSetting, circulationSettingsId, okapiHeaders, vertxContext,
public Future<Response> update(String circulationSettingsId,
CirculationSetting circulationSetting) {

return PgUtil.put(CIRCULATION_SETTINGS_TABLE, circulationSetting, circulationSettingsId,
okapiHeaders, vertxContext,
PutCirculationSettingsStorageCirculationSettingsByCirculationSettingsIdResponse.class)
.compose(eventPublisher.publishUpdated(circulationSetting));
}

public Future<Response> delete(String circulationSettingsId) {
return repository.getById(circulationSettingsId).compose (
circulationSetting -> PgUtil.deleteById(CIRCULATION_SETTINGS_TABLE, circulationSettingsId, okapiHeaders, vertxContext,
DeleteCirculationSettingsStorageCirculationSettingsByCirculationSettingsIdResponse.class)
.compose(eventPublisher.publishRemoved(circulationSetting))
return repository.getById(circulationSettingsId)
.compose(circulationSetting -> PgUtil.deleteById(CIRCULATION_SETTINGS_TABLE,
circulationSettingsId, okapiHeaders, vertxContext,
DeleteCirculationSettingsStorageCirculationSettingsByCirculationSettingsIdResponse.class)
.compose(eventPublisher.publishRemoved(circulationSetting))
);
}

private Future<CirculationSetting> updateSettingsValue(CirculationSetting circulationSetting,
Throwable throwable) {

if (!isDuplicate(throwable.getMessage())) {
log.debug("updateSettingsValue:: error during saving circulation setting: {}. message: {}.",
circulationSetting, throwable.getMessage());
return Future.failedFuture(throwable);
}

log.info("updateSettingsValue:: setting with name: {} already exists.",
circulationSetting.getName());

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

private Future<CirculationSetting> updateSettings(List<CirculationSetting> settings,
CirculationSetting circulationSetting) {

settings.forEach(setting -> setting.setValue(circulationSetting.getValue()));
log.debug("updateSettings:: updating {} setting(s) with name '{}'",
settings::size, circulationSetting::getName);
return repository.update(settings)
.map(circulationSetting);
}

private Future<List<CirculationSetting>> getSettingsByName(String settingsName) {
log.debug("getSettingsByName:: trying to fetch setting by name: {}", settingsName);
Criterion filter = new Criterion(new Criteria()
.addField("'name'")
.setOperation("=")
.setVal(settingsName));

return repository.get(filter);
}
}
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
Loading