Skip to content

Commit

Permalink
MODBATPRNT-1 Deletion of multiple jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
JanisSaldabols committed Jan 16, 2024
1 parent 0c01040 commit 92b6d75
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 59 deletions.
76 changes: 44 additions & 32 deletions descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
],
"pathPattern": "/print/entries",
"permissionsRequired": [
"mod-batch-print.entries.item.post"
"batch-print.entries.item.post"
],
"permissionsDesired": [
"mod-batch-print.print.write"
"batch-print.print.write"
]
},
{
Expand All @@ -44,10 +44,22 @@
],
"pathPattern": "/print/entries",
"permissionsRequired": [
"mod-batch-print.entries.collection.get"
"batch-print.entries.collection.get"
],
"permissionsDesired": [
"mod-batch-print.print.read"
"batch-print.print.read"
]
},
{
"methods": [
"DELETE"
],
"pathPattern": "/print/entries",
"permissionsRequired": [
"batch-print.entries.item.delete"
],
"permissionsDesired": [
"batch-print.print.write"
]
},
{
Expand All @@ -56,10 +68,10 @@
],
"pathPattern": "/print/entries/{id}",
"permissionsRequired": [
"mod-batch-print.entries.item.get"
"batch-print.entries.item.get"
],
"permissionsDesired": [
"mod-batch-print.print.read"
"batch-print.print.read"
]
},
{
Expand All @@ -68,10 +80,10 @@
],
"pathPattern": "/print/entries/{id}",
"permissionsRequired": [
"mod-batch-print.entries.item.put"
"batch-print.entries.item.put"
],
"permissionsDesired": [
"mod-batch-print.print.write"
"batch-print.print.write"
]
},
{
Expand All @@ -80,10 +92,10 @@
],
"pathPattern": "/print/entries/{id}",
"permissionsRequired": [
"mod-batch-print.entries.item.delete"
"batch-print.entries.item.delete"
],
"permissionsDesired": [
"mod-batch-print.print.write"
"batch-print.print.write"
]
},
{
Expand All @@ -92,10 +104,10 @@
],
"pathPattern": "/mail",
"permissionsRequired": [
"mod-batch-print.entries.mail.post"
"batch-print.entries.mail.post"
],
"permissionsDesired": [
"mod-batch-print.print.write"
"batch-print.print.write"
]
}
]
Expand All @@ -111,11 +123,11 @@
],
"pathPattern": "/print/batch-creation",
"modulePermissions": [
"mod-batch-print.print.write",
"mod-batch-print.print.read"
"batch-print.print.write",
"batch-print.print.read"
],
"schedule": {
"cron": "1 7 * * *",
"cron": "1 6 * * *",
"zone": "CET"
}
}
Expand All @@ -125,60 +137,60 @@
"requires": [],
"permissionSets": [
{
"permissionName": "mod-batch-print.print.write",
"permissionName": "batch-print.print.write",
"displayName": "batch print - write print entries",
"description": "Write print entries",
"visible": false
},
{
"permissionName": "mod-batch-print.print.read",
"permissionName": "batch-print.print.read",
"displayName": "batch print - read print entries",
"description": "Read print entries",
"visible": false
},
{
"permissionName": "mod-batch-print.entries.mail.post",
"permissionName": "batch-print.entries.mail.post",
"displayName": "batch print - send mail",
"description": "Send mail to print"
},
{
"permissionName": "mod-batch-print.entries.item.post",
"permissionName": "batch-print.entries.item.post",
"displayName": "batch print - create print entry",
"description": "Create print entry"
},
{
"permissionName": "mod-batch-print.entries.item.put",
"permissionName": "batch-print.entries.item.put",
"displayName": "batch print - update print entry",
"description": "Update print entry"
},
{
"permissionName": "mod-batch-print.entries.collection.get",
"permissionName": "batch-print.entries.collection.get",
"displayName": "batch print - get print entries",
"description": "Get batch print"
},
{
"permissionName": "mod-batch-print.entries.item.get",
"permissionName": "batch-print.entries.item.get",
"displayName": "batch print - get print entry",
"description": "Get print entry"
},
{
"permissionName": "mod-batch-print.entries.item.delete",
"permissionName": "batch-print.entries.item.delete",
"displayName": "batch print - delete print entry",
"description": "Delete print entry"
},
{
"permissionName": "mod-batch-print.entries.all",
"permissionName": "batch-print.entries.all",
"displayName": "batch print - all batch print permissions",
"description": "All batch print permissions",
"subPermissions": [
"mod-batch-print.entries.item.post",
"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.mail.post",
"mod-batch-print.print.write",
"mod-batch-print.print.read"
"batch-print.entries.item.post",
"batch-print.entries.collection.get",
"batch-print.entries.item.get",
"batch-print.entries.item.put",
"batch-print.entries.item.delete",
"batch-print.entries.mail.post",
"batch-print.print.write",
"batch-print.print.read"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<vertxlib.version>3.1.3</vertxlib.version>
<vertx.version>4.4.6</vertx.version>
<lombok.version>1.18.30</lombok.version>
<pdfbox.version>3.0.1</pdfbox.version>
<pdfbox.version>2.0.30</pdfbox.version>
<jackson.version>2.16.1</jackson.version>
<flying-saucer-pdf.version>9.4.1</flying-saucer-pdf.version>
<vertx.launcher>io.vertx.core.Launcher</vertx.launcher>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ private BatchCreationService() {
*/
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().minusDays(1).minusMinutes(5);
LOGGER.info("process:: tenant {}, from {}", tenant, localDateTime);

printStorage.getEntriesByQuery("type=\"SINGLE\" and created > " + localDateTime
+ " sortby sortingField created", 0, MAX_COUNT_IN_BATCH)
Expand All @@ -41,6 +41,7 @@ public static void process(RoutingContext ctx) {
}

private static void processListAndSaveResult(List<PrintEntry> entries, PrintStorage storage) {
LOGGER.info("processListAndSaveResult:: {} entries will be processed", entries.size());
if (!entries.isEmpty()) {
byte[] merged = PdfService.combinePdfFiles(entries);
PrintEntry batch = new PrintEntry();
Expand All @@ -49,6 +50,8 @@ private static void processListAndSaveResult(List<PrintEntry> entries, PrintStor
batch.setType(PrintEntryType.BATCH);
batch.setContent(Hex.getString(merged));
storage.createEntry(batch);
List<UUID> ids = entries.stream().map(PrintEntry::getId).toList();
storage.deleteEntries(ids);
}
}
}
29 changes: 25 additions & 4 deletions src/main/java/org/folio/print/server/service/PdfService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.folio.print.server.service;

import com.lowagie.text.DocumentException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.pdfbox.io.RandomAccessReadBuffer;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
Expand All @@ -30,8 +30,7 @@ public static byte[] createPdfFile(String htmlContent) {
if (htmlContent != null && !htmlContent.isBlank()) {
try (PDDocument document = new PDDocument();
ByteArrayOutputStream os = new ByteArrayOutputStream()) {
htmlContent = "<div>" + htmlContent + "</div>";
htmlContent = htmlContent.replace("<br>", "<br/>");
htmlContent = cleanHtmlData(htmlContent);
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
ITextRenderer renderer = new ITextRenderer();
Expand All @@ -46,6 +45,24 @@ public static byte[] createPdfFile(String htmlContent) {
return new byte[0];
}

private static String cleanHtmlData(String htmlContent) {
return "<div>" + htmlContent
.replace("<br>", "<br/>")
.replace("&nbsp;", "&#160;")
.replace("&lt;", "&#60;")
.replace("&gt;", "&#62;")
.replace("&amp;", "&#38;")
.replace("&quot;", "&#34;")
.replace("&apos;", "&#39;")
.replace("&ndash;", "&#8211;")
.replace("&mdash;", "&#8212;")
.replace("&copy;", "&#169;")
.replace("&reg;", "&#174;")
.replace("&nbsp;", "&#160;")
.replace("&trade;", "&#8482;")
+ "</div>";
}

/**
* Combine single print entries in batch print file.
* @param entries Entries to combine
Expand All @@ -57,7 +74,11 @@ public static byte[] combinePdfFiles(List<PrintEntry> entries) {
PDFMergerUtility pdfMerger = new PDFMergerUtility();
entries.forEach(e -> {
if (e.getContent() != null && !e.getContent().isBlank()) {
pdfMerger.addSource(new RandomAccessReadBuffer(Hex.decodeHex(e.getContent())));
try {
pdfMerger.addSource(new ByteArrayInputStream(Hex.decodeHex(e.getContent())));
} catch (IOException ex) {
LOGGER.error("Failed to merge entry: " + e.getId(), ex);
}
}
});
pdfMerger.setDestinationStream(mergedOutputStream);
Expand Down
38 changes: 37 additions & 1 deletion src/main/java/org/folio/print/server/service/PrintService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import io.vertx.ext.web.validation.ValidationHandler;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -76,7 +78,12 @@ private void handlers(RouterBuilder routerBuilder) {
.onFailure(cause -> commonError(ctx, cause))
)
.failureHandler(this::failureHandler);

routerBuilder
.operation("deletePrintEntries")
.handler(ctx -> deletePrintEntries(ctx)
.onFailure(cause -> commonError(ctx, cause))
)
.failureHandler(this::failureHandler);
routerBuilder
.operation("postPrintEntry")
.handler(ctx -> postPrintEntry(ctx)
Expand Down Expand Up @@ -128,10 +135,13 @@ public static PrintStorage create(RoutingContext ctx) {
}

Future<Void> postPrintEntry(RoutingContext ctx) {
log.info("postPrintEntry:: create entry");
PrintStorage storage = create(ctx);
RequestParameters params = ctx.get(ValidationHandler.REQUEST_CONTEXT_KEY);
RequestParameter body = params.body();
PrintEntry entry = body.getJsonObject().mapTo(PrintEntry.class);
log.info("postPrintEntry:: entry with type {}, sorting field{}",
entry.getType(), entry.getSortingField());
return storage.createEntry(entry)
.map(entity -> {
ctx.response().setStatusCode(204);
Expand All @@ -140,7 +150,26 @@ Future<Void> postPrintEntry(RoutingContext ctx) {
});
}

Future<Void> deletePrintEntries(RoutingContext ctx) {
PrintStorage storage = create(ctx);
RequestParameters params = ctx.get(ValidationHandler.REQUEST_CONTEXT_KEY);
RequestParameter idsParameter = params.queryParameter("ids");
String ids = idsParameter != null ? idsParameter.getString() : "";
log.info("deletePrintEntries:: delete entries by ids: {}", ids);
List<UUID> uuids = Arrays.stream(ids.split(","))
.filter(id -> id != null && !id.isBlank())
.map(UUID::fromString)
.toList();
return storage.deleteEntries(uuids)
.map(r -> {
ctx.response().setStatusCode(204);
ctx.response().end();
return null;
});
}

Future<Void> saveMail(RoutingContext ctx) {
log.info("saveMail:: create single entry");
final PrintStorage storage = create(ctx);
RequestParameters params = ctx.get(ValidationHandler.REQUEST_CONTEXT_KEY);
RequestParameter body = params.body();
Expand All @@ -151,6 +180,8 @@ Future<Void> saveMail(RoutingContext ctx) {
entry.setCreated(ZonedDateTime.now().withZoneSameInstant(ZoneOffset.UTC));
entry.setSortingField(message.getTo());
entry.setContent(Hex.getString(PdfService.createPdfFile(message.getBody())));
log.info("saveMail:: entry with type {}, sorting field{}",
entry.getType(), entry.getSortingField());
return storage.createEntry(entry)
.map(entity -> {
ctx.response().setStatusCode(HttpResponseStatus.OK.code());
Expand All @@ -163,6 +194,7 @@ Future<Void> getPrintEntry(RoutingContext ctx) {
PrintStorage storage = create(ctx);
RequestParameters params = ctx.get(ValidationHandler.REQUEST_CONTEXT_KEY);
String id = params.pathParameter("id").getString();
log.info("getPrintEntry:: get single entry by id: {}", id);
return storage.getEntry(UUID.fromString(id))
.map(entity -> {
HttpResponse.responseJson(ctx, 200)
Expand All @@ -175,6 +207,7 @@ Future<Void> deletePrintEntry(RoutingContext ctx) {
PrintStorage printStorage = create(ctx);
RequestParameters params = ctx.get(ValidationHandler.REQUEST_CONTEXT_KEY);
String id = params.pathParameter("id").getString();
log.info("deletePrintEntry:: delete single entry by id: {}", id);
return printStorage.deleteEntry(UUID.fromString(id))
.map(res -> {
ctx.response().setStatusCode(204);
Expand All @@ -189,6 +222,7 @@ Future<Void> updatePrintEntry(RoutingContext ctx) {
RequestParameter body = params.body();
PrintEntry entry = body.getJsonObject().mapTo(PrintEntry.class);
UUID id = UUID.fromString(params.pathParameter("id").getString());
log.info("updatePrintEntry:: update single entry by id: {}", id);
if (!id.equals(entry.getId())) {
return Future.failedFuture(new EntryException("id mismatch"));
}
Expand All @@ -207,6 +241,8 @@ Future<Void> getPrintEntries(RoutingContext ctx) {
String query = queryParameter != null ? queryParameter.getString() : null;
int limit = params.queryParameter("limit").getInteger();
int offset = params.queryParameter("offset").getInteger();
log.info("getPrintEntries:: get entries by query: {}, limit {}, offset {}",
query, limit, offset);
return storage.getEntries(ctx.response(), query, offset, limit);
}

Expand Down
Loading

0 comments on commit 92b6d75

Please sign in to comment.