forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTenantRefAPI.java
87 lines (76 loc) · 3.55 KB
/
TenantRefAPI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.folio.rest.impl;
import java.util.Map;
import javax.ws.rs.core.Response;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.support.kafka.topic.CirculationStorageKafkaTopic;
import org.folio.kafka.services.KafkaAdminClientService;
import org.folio.rest.annotations.Validate;
import org.folio.rest.jaxrs.model.TenantAttributes;
import org.folio.rest.tools.utils.TenantLoading;
import org.folio.rest.tools.utils.TenantTool;
import org.folio.service.PubSubRegistrationService;
import org.folio.service.migration.TlrDataMigrationService;
import org.folio.service.migration.RequestSearchFieldsMigrationService;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
public class TenantRefAPI extends TenantAPI {
private static final Logger log = LogManager.getLogger();
public static final String REFERENCE_KEY = "loadReference";
public static final String REFERENCE_LEAD = "ref-data";
public static final String SAMPLE_KEY = "loadSample";
public static final String SAMPLE_LEAD = "sample-data";
@Validate
@Override
Future<Integer> loadData(TenantAttributes attributes, String tenantId,
Map<String, String> headers, Context vertxContext) {
return (new TlrDataMigrationService(attributes, vertxContext, headers).migrate())
.compose(f -> new RequestSearchFieldsMigrationService(attributes, vertxContext, headers).migrate())
.compose(r -> new KafkaAdminClientService(vertxContext.owner())
.createKafkaTopics(CirculationStorageKafkaTopic.values(), tenantId))
.compose(r -> super.loadData(attributes, tenantId, headers, vertxContext))
.compose(superRecordsLoaded -> {
log.info("Initializing of tenant's data");
Vertx vertx = vertxContext.owner();
Promise<Integer> promise = Promise.promise();
TenantLoading tl = new TenantLoading();
tl.withKey(REFERENCE_KEY).withLead(REFERENCE_LEAD)
.withIdContent()
.add("loan-policy-storage/loan-policies")
.add("request-policy-storage/request-policies")
.add("patron-notice-policy-storage/patron-notice-policies")
.add("staff-slips-storage/staff-slips")
.withIdRaw()
.add("circulation-rules-storage")
.withIdContent()
.add("cancellation-reason-storage/cancellation-reasons")
.withKey(SAMPLE_KEY).withLead(SAMPLE_LEAD)
.add("loans", "loan-storage/loans")
.add("requests", "request-storage/requests");
tl.perform(attributes, headers, vertx, res -> {
if (res.failed()) {
promise.fail(res.cause());
} else {
PubSubRegistrationService.registerModule(headers, vertx)
.whenComplete((aBoolean, throwable) -> promise.complete());
}
});
return promise.future();
});
}
@Validate
@Override
public void postTenant(TenantAttributes tenantAttributes, Map<String, String> headers,
Handler<AsyncResult<Response>> handler, Context context) {
// delete Kafka topics if tenant purged
var tenantId = TenantTool.tenantId(headers);
Future<Void> result = tenantAttributes.getPurge() != null && tenantAttributes.getPurge()
? new KafkaAdminClientService(context.owner()).deleteKafkaTopics(CirculationStorageKafkaTopic.values(), tenantId)
: Future.succeededFuture();
result.onComplete(x -> super.postTenant(tenantAttributes, headers, handler, context));
}
}