diff --git a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportAdapter.java b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportAdapter.java index c708b66..6befc60 100644 --- a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportAdapter.java +++ b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportAdapter.java @@ -93,6 +93,7 @@ public class OPListExportAdapter extends POIFindReplaceAdapter { appendExcelTemplate(document, textblock, template, targetName); + logger.info("... loading poi configuration.."); // Process POI instructions // read the config ItemCollection poiConfig = workflowService.evalWorkflowResult(event, "poi-update", document, false); @@ -103,14 +104,17 @@ public class OPListExportAdapter extends POIFindReplaceAdapter { List replaceDevList = poiConfig.getItemValue("findreplace"); String eval = poiConfig.getItemValueString("eval"); try { + logger.info("... update template with normal poi information.."); this.updateFileData(document.getFileData(targetName), document, replaceDevList, eval); // now we add separate lines for each invoice.... + logger.info("... insert invoices.."); insertInvoiceRows(document, targetName); } catch (PluginException | IOException | QueryException e) { throw new PluginException(OPListExportAdapter.class.getSimpleName(), ERROR_CONFIG, "failed to update op-liste: " + e.getMessage()); } + logger.info("... completed!"); return document; } @@ -132,75 +136,80 @@ public class OPListExportAdapter extends POIFindReplaceAdapter { // load dummy rechnungen String spaceID = document.getItemValueString("space.ref"); + logger.info("... space.ref=" + spaceID + " ...grouping invoices by spaceid...."); Map> invoiceMap = groupInvoicesBySpaceID(spaceID); FileData fileData = document.getFileData(fileName); - + logger.info("... processing workbook... DEBUG MODE"); // load XSSFWorkbook + XSSFWorkbook doc = null; try (InputStream imputStream = new ByteArrayInputStream(fileData.getContent())) { - XSSFWorkbook doc = new XSSFWorkbook(imputStream); + doc = new XSSFWorkbook(imputStream); // NOTE: we only take the first sheet ! XSSFSheet sheet = doc.getSheetAt(0); - CellReference cr = new CellReference("A12"); - XSSFRow referenceRow = sheet.getRow(cr.getRow()); + CellReference categoryRefCell = new CellReference("A12"); + XSSFRow referenceRowCategory = sheet.getRow(categoryRefCell.getRow()); + CellReference invoiceRefCell = new CellReference("A13"); + XSSFRow referenceRowInvoice = sheet.getRow(invoiceRefCell.getRow()); + int referenceRowPos = 12; int rowPos = 12; - // int lastRow = sheet.getLastRowNum(); - int lastRow = 999; + int lastRow = 2999; logger.finest("Last rownum=" + lastRow); int totalRowCount = invoiceMap.size(); for (List list : invoiceMap.values()) { totalRowCount = totalRowCount + list.size(); } - + if (totalRowCount == 0) { + logger.warning("No Invoices found - skip update workbook!"); + return; + } sheet.shiftRows(rowPos, lastRow, totalRowCount, true, true); for (Map.Entry> entry : invoiceMap.entrySet()) { String dbtrNumber = entry.getKey(); + logger.info("......add debitor " + dbtrNumber); List invoices = entry.getValue(); // erzeuge eine Zwischenüberschrift für den Debitor.... XSSFRow categoryRow = sheet.createRow(rowPos); - categoryRow.copyRowFrom(referenceRow, new CellCopyPolicy()); + categoryRow.copyRowFrom(referenceRowCategory, new CellCopyPolicy()); // insert values categoryRow.getCell(0).setCellValue(dbtrNumber); String debitorName = invoices.get(0).getItemValueString("dbtr.name"); categoryRow.getCell(1).setCellValue(debitorName); - + // calculate summ of all invoices double catSum = 0; for (ItemCollection invoice : invoices) { catSum = catSum + invoice.getItemValueDouble("invoice.saldo"); } - if (catSum < 0) { - // SOLL - categoryRow.getCell(6).setCellValue(catSum); - } else { - categoryRow.getCell(8).setCellValue(catSum); - } + logger.fine("......... sum=" + catSum); + categoryRow.getCell(10).setCellValue(catSum); rowPos++; // jetzt füge alle Rechnungen an. for (ItemCollection invoice : invoices) { - logger.finest("......copy row..."); + logger.fine("......add invoice " + invoice.getUniqueID()); // now create a new line.. XSSFRow row = sheet.createRow(rowPos); - row.copyRowFrom(referenceRow, new CellCopyPolicy()); + row.copyRowFrom(referenceRowInvoice, new CellCopyPolicy()); // insert values row.getCell(2).setCellValue(invoice.getItemValueString("invoice.number")); row.getCell(3).setCellValue(invoice.getItemValueString("invoice.text")); row.getCell(4).setCellValue(invoice.getItemValueDate("invoice.date")); row.getCell(5).setCellValue(invoice.getItemValueDate("invoice.duedate")); double total = invoice.getItemValueDouble("invoice.saldo"); - if (total < 0) { - // SOLL + String currency=invoice.getItemValueString("invoice.currency"); + if ("EUR".equals(currency)) { + // EUR row.getCell(6).setCellValue(total); - row.getCell(7).setCellValue(invoice.getItemValueString("invoice.currency")); + row.getCell(7).setCellValue(currency); } else { - // HABEN + // USD row.getCell(8).setCellValue(total); - row.getCell(9).setCellValue(invoice.getItemValueString("invoice.currency")); + row.getCell(9).setCellValue(currency); } row.getCell(10).setCellValue(invoice.getItemValueString("$workflowstatus")); saldo = saldo + total; @@ -211,13 +220,14 @@ public class OPListExportAdapter extends POIFindReplaceAdapter { sheet.shiftRows(referenceRowPos, lastRow + totalRowCount, -1, true, true); // finally update the total formula... - evalXSSFSheet(doc, sheet, "TOTAL"); + evalXSSFSheet(doc, sheet, "TOTALEUR"); + evalXSSFSheet(doc, sheet, "TOTALUSD"); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // write back the file doc.write(byteArrayOutputStream); - doc.close(); + byte[] newContent = byteArrayOutputStream.toByteArray(); FileData fileDataNew = new FileData(fileData.getName(), newContent, fileData.getContentType(), null); // update the fileData @@ -228,6 +238,15 @@ public class OPListExportAdapter extends POIFindReplaceAdapter { } catch (IOException e) { throw new PluginException(OPListExportAdapter.class.getSimpleName(), ERROR_CONFIG, "failed to update opliste: " + e.getMessage()); + } finally { + if (doc != null) { + try { + doc.close(); + } catch (IOException e) { + logger.severe("Failed to close workbook: " + e.getMessage()); + e.printStackTrace(); + } + } } } diff --git a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportController.java b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportController.java index ff053d1..3d4f176 100644 --- a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportController.java +++ b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportController.java @@ -25,10 +25,11 @@ package com.alexanderlogistics; import java.util.logging.Logger; +import javax.ejb.Timer; import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; import javax.inject.Named; - import org.imixs.workflow.engine.scheduler.SchedulerController; /** @@ -60,6 +61,10 @@ public class OPListExportController extends SchedulerController { private static final long serialVersionUID = 1L; private static Logger logger = Logger.getLogger(OPListExportController.class.getName()); + + @Inject + OPListExportScheduler opListExportScheduler; + @Override public String getName() { return OPLIST_EXPORT_CONFIGURATION; @@ -78,4 +83,8 @@ public class OPListExportController extends SchedulerController { return schedulerClass; } + + + + } diff --git a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportScheduler.java b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportScheduler.java index 5fcc169..1cb1694 100644 --- a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportScheduler.java +++ b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportScheduler.java @@ -26,18 +26,16 @@ import java.text.DateFormat; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.text.SimpleDateFormat; -import java.util.Date; import java.util.List; import java.util.logging.Logger; import javax.ejb.EJB; +import javax.ejb.TransactionAttribute; +import javax.ejb.TransactionAttributeType; import org.imixs.marty.team.TeamService; -import org.imixs.workflow.FileData; import org.imixs.workflow.ItemCollection; import org.imixs.workflow.engine.DocumentService; -import org.imixs.workflow.engine.ModelService; -import org.imixs.workflow.engine.ReportService; import org.imixs.workflow.engine.WorkflowService; import org.imixs.workflow.engine.scheduler.Scheduler; import org.imixs.workflow.engine.scheduler.SchedulerException; @@ -69,21 +67,15 @@ public class OPListExportScheduler implements Scheduler { public static DecimalFormat decimalFormat = new DecimalFormat("0.00", new DecimalFormatSymbols(java.util.Locale.GERMANY)); - @EJB - DocumentService documentService; - @EJB WorkflowService workflowService; @EJB - ModelService modelService; + DocumentService documentService; @EJB TeamService teamService; - @EJB - ReportService reportService; - private static Logger logger = Logger.getLogger(OPListExportScheduler.class.getName()); /** @@ -96,45 +88,73 @@ public class OPListExportScheduler implements Scheduler { * @throws QueryException */ public ItemCollection run(ItemCollection configuration) throws SchedulerException { - List invoices = null; - StringBuffer buffer = new StringBuffer(); - int lines = 0; + try { + configuration.removeItem("_scheduler_logmessage"); + logMessage("....export OP-Listen...", configuration, null); + // Hole dir alle Bereiche + List spaces = teamService.getSpaces(); + // Jezt erzeugen wir für jeden Bereich ein neues Workitem . + for (ItemCollection space : spaces) { - configuration.removeItem("_scheduler_logmessage"); - logMessage(configuration, "....export OP-Listen..."); + // Gibt es Rechnungen? + try { + List invoices = documentService + .find("$modelversion:rechnungsausgang-de-1.0 AND type:workitem AND $uniqueidref:" + + space.getUniqueID(), 1, 0); + if (invoices.size() > 0) { + // erzeuge eine OP-Liste + ItemCollection workitem = new ItemCollection(); + workitem.setItemValue("space.ref", space.getUniqueID()); + processOPListe(workitem); + } + } catch (QueryException e) { + // not possible + e.printStackTrace(); + } - // Hole dir alle Bereiche - - List spaces = teamService.getSpaces(); - // Jezt erzeugen wir für jeden Berich ein neues Workitem falls offene Rechnungen - for (ItemCollection space : spaces) { - - ItemCollection workitem = new ItemCollection(); - workitem.setItemValue("space.ref", space.getUniqueID()); - - try { - workflowService.processWorkItem( // - workitem.task(1000). // - event(100). // - model("opliste-de-1.0")); - } catch (AccessDeniedException | ProcessingErrorException | PluginException | ModelException e) { - e.printStackTrace(); } + + logMessage("...completed", configuration, null); + } catch (RuntimeException e) { + // continue with log message + logger.severe("...OPList Error"); + logMessage("...Failed to create OPLists: " + e.getMessage(), configuration, null); + } catch (PluginException | ModelException e2) { + logger.severe("Error processing OP-List for space: " + e2.getMessage()); + throw new SchedulerException("OP-LIST-ERROR", "OPList Processing failed : " + e2.getMessage(), e2); + } - // transfer file via FTP... - DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm"); - // MMM d, yyyy HH:mm a - FileData fileData = new FileData("OPD_" + formatter.format(new Date()) + ".txt", - String.valueOf(buffer).getBytes(), null, null); - - logMessage(configuration, "...completed: " + lines + " lines"); return configuration; } - private void logMessage(ItemCollection configuration, String message) { - configuration.appendItemValue("_scheduler_logmessage", message); + @TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW) + public void processOPListe(ItemCollection workitem) + throws PluginException, AccessDeniedException, ProcessingErrorException, ModelException { + workflowService.processWorkItem( // + workitem.task(1000). // + event(100). // + model("opliste-de-1.0")); + + } + + /** + * Creates a new log entry stored in the item _scheduler_log. The log can be + * writen optional to the configuraiton and the workitem + * + * @param message + * @param configuration + */ + public void logMessage(String message, ItemCollection configuration, ItemCollection workitem) { + if (configuration != null) { + configuration.appendItemValue(Scheduler.ITEM_LOGMESSAGE, message); + } + if (workitem != null) { + workitem.appendItemValue(Scheduler.ITEM_LOGMESSAGE, message); + } + logger.info(message); + } } diff --git a/office-alexander-logistics-app/src/main/webapp/pages/admin/oplist_export.xhtml b/office-alexander-logistics-app/src/main/webapp/pages/admin/oplist_export.xhtml index ccf45a9..8e83f88 100644 --- a/office-alexander-logistics-app/src/main/webapp/pages/admin/oplist_export.xhtml +++ b/office-alexander-logistics-app/src/main/webapp/pages/admin/oplist_export.xhtml @@ -56,7 +56,7 @@