Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JanisSaldabols committed Nov 23, 2023
1 parent e8d8b53 commit 17e3fb5
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 355 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ GRANT ALL PRIVILEGES ON DATABASE folio_modules TO folio;
The module's database connection is then configured by setting environment
variables:
`DB_HOST`, `DB_PORT`, `DB_USERNAME`, `DB_PASSWORD`, `DB_DATABASE`,
`DB_MAXPOOLSIZE`, `DB_SERVER_PEM`.
`DB_MAXPOOLSIZE`.

Once configured, start the module with:

Expand Down
17 changes: 16 additions & 1 deletion descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@
],
"requires": [],
"permissionSets": [
{
"permissionName": "mod-batch-print.print.write",
"displayName": "batch print - write print entries",
"description": "Write print entries",
"visible": false
},
{
"permissionName": "mod-batch-print.print.read",
"displayName": "batch print - read print entries",
"description": "Read print entries",
"visible": false
},
{
"permissionName": "mod-batch-print.entries.mail.post",
"displayName": "batch print - send mail",
Expand Down Expand Up @@ -163,7 +175,10 @@
"mod-batch-print.entries.collection.get",
"mod-batch-print.entries.item.get",
"mod-batch-print.entries.item.put",
"mod-batch-print.entries.item.delete"
"mod-batch-print.entries.item.delete",
"mod-batch-print.entries.mail.post",
"mod-batch-print.print.write",
"mod-batch-print.print.read"
]
}
],
Expand Down
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.folio</groupId>
<artifactId>mod-batch-print</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>mod-batch-print</name>
Expand Down Expand Up @@ -65,6 +65,10 @@
<groupId>io.vertx</groupId>
<artifactId>vertx-pg-client</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-sql-client-templates</artifactId>
</dependency>
<dependency>
<groupId>org.folio</groupId>
<artifactId>vertx-lib</artifactId>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/folio/print/server/data/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class Message {
private String deliveryChannel;
private String notificationId;
private String from;
private String to;
private String outputFormat;
private String header;
private String body;
Expand Down
38 changes: 6 additions & 32 deletions src/main/java/org/folio/print/server/data/PrintEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import java.time.ZonedDateTime;
import java.util.UUID;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PrintEntry {

Expand All @@ -13,37 +17,7 @@ public class PrintEntry {

private PrintEntryType type;

private String content;

public UUID getId() {
return id;
}

public void setId(UUID id) {
this.id = id;
}

public ZonedDateTime getCreated() {
return created;
}

public void setCreated(ZonedDateTime created) {
this.created = created;
}
private String sortingField;

public PrintEntryType getType() {
return type;
}

public void setType(PrintEntryType type) {
this.type = type;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
private String content;
}
5 changes: 1 addition & 4 deletions src/main/java/org/folio/print/server/main/MainVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.apache.logging.log4j.Logger;
import org.folio.okapi.common.Config;
import org.folio.okapi.common.ModuleVersionReporter;
import org.folio.print.server.resources.BatchCreationResource;
import org.folio.print.server.service.PrintService;
import org.folio.tlib.RouterCreator;
import org.folio.tlib.api.HealthApi;
Expand All @@ -33,13 +32,11 @@ public void start(Promise<Void> promise) {
log.info("Listening on port {}", port);

var printServiceService = new PrintService();
var batchCreationResource = new BatchCreationResource();

RouterCreator[] routerCreators = {
printServiceService,
batchCreationResource,
new Tenant2Api(printServiceService),
new HealthApi(),
new HealthApi()
};

RouterCreator.mountAll(vertx, routerCreators)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.folio.print.server.service;

import io.vertx.ext.web.RoutingContext;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.UUID;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.pdfbox.util.Hex;
import org.folio.okapi.common.XOkapiHeaders;
import org.folio.print.server.data.PrintEntry;
import org.folio.print.server.data.PrintEntryType;
import org.folio.print.server.storage.PrintStorage;

public class BatchCreationService {
private static final Logger LOGGER = LogManager.getLogger(BatchCreationService.class);
private static final int MAX_COUNT_IN_BATCH = 1000;

private BatchCreationService() {
}

/**
* Process batch creation request.
* @param ctx Batch creation request context
*/
public static void process(RoutingContext ctx) {
String tenant = ctx.request().getHeader(XOkapiHeaders.TENANT);
LOGGER.debug("process:: tenant " + tenant);
PrintStorage printStorage = new PrintStorage(ctx.vertx(), tenant);
LocalDateTime localDateTime = LocalDateTime.now().with(LocalTime.MIDNIGHT);

printStorage.getEntriesByQuery("type=\"SINGLE\" and created > " + localDateTime
+ " sortby sortingField created", 0, MAX_COUNT_IN_BATCH)
.onSuccess(l -> processListAndSaveResult(l, printStorage))
.onFailure(e -> LOGGER.error("Failed to create print batch", e));
ctx.response().setStatusCode(204);
ctx.response().end();
}

private static void processListAndSaveResult(List<PrintEntry> entries, PrintStorage storage) {
if (!entries.isEmpty()) {
byte[] merged = PdfService.combinePdfFiles(entries);
PrintEntry batch = new PrintEntry();
batch.setId(UUID.randomUUID());
batch.setCreated(ZonedDateTime.now().withZoneSameInstant(ZoneOffset.UTC));
batch.setType(PrintEntryType.BATCH);
batch.setContent(Hex.getString(merged));
storage.createEntry(batch);
}
}
}
22 changes: 8 additions & 14 deletions src/main/java/org/folio/print/server/service/PrintService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
Expand Down Expand Up @@ -113,24 +112,18 @@ private void handlers(RouterBuilder routerBuilder) {
.onFailure(cause -> commonError(ctx, cause))
)
.failureHandler(this::failureHandler);

routerBuilder
.operation("createBatch")
.handler(BatchCreationService::process)
.failureHandler(this::failureHandler);
}

static PrintStorage createFromParams(Vertx vertx, RequestParameters params) {
// get tenant
RequestParameter tenantParameter = params.headerParameter(XOkapiHeaders.TENANT);
String tenant = tenantParameter.getString();

// get user Id
RequestParameter userIdParameter = params.headerParameter(XOkapiHeaders.USER_ID);
UUID currentUserId = null;
if (userIdParameter != null) {
currentUserId = UUID.fromString(userIdParameter.getString());
}

// get permissions which is required in OpenAPI spec
RequestParameter okapiPermissions = params.headerParameter(XOkapiHeaders.PERMISSIONS);
JsonArray permissions = new JsonArray(okapiPermissions.getString());
return new PrintStorage(vertx, tenant, currentUserId, permissions);
return new PrintStorage(vertx, tenantParameter.getString());
}

public static PrintStorage create(RoutingContext ctx) {
Expand Down Expand Up @@ -159,6 +152,7 @@ Future<Void> saveMail(RoutingContext ctx) {
entry.setId(UUID.randomUUID());
entry.setType(PrintEntryType.SINGLE);
entry.setCreated(ZonedDateTime.now().withZoneSameInstant(ZoneOffset.UTC));
entry.setSortingField(message.getTo());
entry.setContent(Hex.getString(PdfService.createPdfFile(message.getBody())));
return storage.createEntry(entry)
.map(entity -> {
Expand Down Expand Up @@ -226,7 +220,7 @@ public Future<Void> postInit(Vertx vertx, String tenant, JsonObject tenantAttrib
if (!tenantAttributes.containsKey("module_to")) {
return Future.succeededFuture(); // doing nothing for disable
}
PrintStorage storage = new PrintStorage(vertx, tenant, null, null);
PrintStorage storage = new PrintStorage(vertx, tenant);
return storage.init();
}

Expand Down
Loading

0 comments on commit 17e3fb5

Please sign in to comment.