diff --git a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/api/CargosoftMigrationRestService.java b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/api/CargosoftMigrationRestService.java index 7bfb48c..295de04 100644 --- a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/api/CargosoftMigrationRestService.java +++ b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/api/CargosoftMigrationRestService.java @@ -27,15 +27,8 @@ package com.alexanderlogistics.api; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.io.Serializable; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.logging.Logger; import org.imixs.marty.team.TeamService; @@ -203,131 +196,220 @@ public class CargosoftMigrationRestService implements Serializable { * * */ - @GET - @Path("/cargosoft-renew_cdtr_number") - @Produces({ MediaType.TEXT_PLAIN }) - public String migrationAdresswandlungOpenInvoices(@QueryParam("maxcount") int maxcount, - @QueryParam("type") String type) - throws QueryException, AccessDeniedException, ProcessingErrorException, PluginException, ModelException { - StringBuffer messageBuffer = new StringBuffer(); - if (maxcount <= 0) { - maxcount = 10; - } - if (type == null || type.isEmpty()) { - type = "workitem"; - } + // @GET + // @Path("/cargosoft-renew_cdtr_number") + // @Produces({ MediaType.TEXT_PLAIN }) + // public String migrationAdresswandlungOpenInvoices(@QueryParam("maxcount") int + // maxcount, + // @QueryParam("type") String type) + // throws QueryException, AccessDeniedException, ProcessingErrorException, + // PluginException, ModelException { + // StringBuffer messageBuffer = new StringBuffer(); + // if (maxcount <= 0) { + // maxcount = 10; + // } + // if (type == null || type.isEmpty()) { + // type = "workitem"; + // } - if (isRunning) { - log("├── sync process already running!", messageBuffer); - return messageBuffer.toString(); - } - isRunning = true; + // if (isRunning) { + // log("├── sync process already running!", messageBuffer); + // return messageBuffer.toString(); + // } + // isRunning = true; - log("├── read cargosoft migration file....", messageBuffer); - Map mapping = readAdressMapping(); - log("├── found " + mapping.size() + " mappings", messageBuffer); + // log("├── read cargosoft migration file....", messageBuffer); + // Map mapping = readAdressMapping(); + // log("├── found " + mapping.size() + " mappings", messageBuffer); - String query = "(type:" + type + ") AND ($modelversion:rechnungseingang*)"; - // int syncs = 0; - long l = System.currentTimeMillis(); - int batchSize = 100; - int totalUpdates = 0; + // String query = "(type:" + type + ") AND ($modelversion:rechnungseingang*)"; + // // int syncs = 0; + // long l = System.currentTimeMillis(); + // int batchSize = 100; + // int totalUpdates = 0; - log("├── migration cargosoft addresses....", messageBuffer); - log("│ ├── query=" + query, messageBuffer); + // log("├── migration cargosoft addresses....", messageBuffer); + // log("│ ├── query=" + query, messageBuffer); - int totalCount = documentService.count(query); - log("│ ├── found " + totalCount + " invoices", messageBuffer); + // int totalCount = documentService.count(query); + // log("│ ├── found " + totalCount + " invoices", messageBuffer); - // Berechne Anzahl der benötigten Pages - int totalPages = (int) Math.ceil((double) totalCount / batchSize); + // // Berechne Anzahl der benötigten Pages + // int totalPages = (int) Math.ceil((double) totalCount / batchSize); - // Verarbeite Page für Page - for (int pageIndex = 0; pageIndex < totalPages; pageIndex++) { - log("│ ├── read page " + pageIndex, messageBuffer); - List invoiceList = documentService.find(query, batchSize, pageIndex); - long l1 = System.currentTimeMillis(); - log("│ ├── ...verifying " + invoiceList.size() + " invoices...", messageBuffer); - int updates = migratateInvioceBPAddress(invoiceList, mapping); - totalUpdates = totalUpdates + updates; - log("│ ├── " + updates + " invoices migrated in " + - (System.currentTimeMillis() - l1) + "ms ", messageBuffer); - // break; - if (totalUpdates >= maxcount) { - break; - } - } - long duration = System.currentTimeMillis() - l; - double objectsPerSecond = totalUpdates / (duration / 1000.0); - log("├── Successfully " + totalUpdates + " invoices migrated in " + duration + "ms (" - + String.format("%.1f", objectsPerSecond) + " objects/sec)", messageBuffer); + // // Verarbeite Page für Page + // for (int pageIndex = 0; pageIndex < totalPages; pageIndex++) { + // log("│ ├── read page " + pageIndex, messageBuffer); + // List invoiceList = documentService.find(query, batchSize, + // pageIndex); + // long l1 = System.currentTimeMillis(); + // log("│ ├── ...verifying " + invoiceList.size() + " invoices...", + // messageBuffer); + // int updates = migratateInvioceBPAddress(invoiceList, mapping); + // totalUpdates = totalUpdates + updates; + // log("│ ├── " + updates + " invoices migrated in " + + // (System.currentTimeMillis() - l1) + "ms ", messageBuffer); + // // break; + // if (totalUpdates >= maxcount) { + // break; + // } + // } + // long duration = System.currentTimeMillis() - l; + // double objectsPerSecond = totalUpdates / (duration / 1000.0); + // log("├── Successfully " + totalUpdates + " invoices migrated in " + duration + // + "ms (" + // + String.format("%.1f", objectsPerSecond) + " objects/sec)", messageBuffer); - isRunning = false; - return messageBuffer.toString(); - } + // isRunning = false; + // return messageBuffer.toString(); + // } - @TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW) - private int migratateInvioceBPAddress(List invoices, Map map) { - int count = 0; - for (ItemCollection invoice : invoices) { - String cdtrNumber = invoice.getItemValueString("cdtr.number"); - if (cdtrNumber.isBlank()) { - logger.warning(" ..Invoice " + invoice.getUniqueID() + " has no cdtr.number!"); - continue; - } + // @TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW) + // private int migratateInvioceBPAddress(List invoices, + // Map map) { + // int count = 0; + // for (ItemCollection invoice : invoices) { + // String cdtrNumber = invoice.getItemValueString("cdtr.number"); + // if (cdtrNumber.isBlank()) { + // logger.warning(" ..Invoice " + invoice.getUniqueID() + " has no + // cdtr.number!"); + // continue; + // } - // logger.info("==> check cdtr.number: " + cdtrNumber); - // K überspringen - String newNumber = map.get(cdtrNumber.substring(1)); - if (newNumber != null) { - // migrate number - invoice.setItemValue("cdtr.number.old", cdtrNumber); - invoice.setItemValue("cdtr.number", "K" + newNumber); + // // logger.info("==> check cdtr.number: " + cdtrNumber); + // // K überspringen + // String newNumber = map.get(cdtrNumber.substring(1)); + // if (newNumber != null) { + // // migrate number + // invoice.setItemValue("cdtr.number.old", cdtrNumber); + // invoice.setItemValue("cdtr.number", "K" + newNumber); - logger.info("│ ├── replace " + cdtrNumber + " -> K" + newNumber + " : " + invoice.getUniqueID()); - documentService.save(invoice); - count++; + // logger.info("│ ├── replace " + cdtrNumber + " -> K" + newNumber + " : " + + // invoice.getUniqueID()); + // documentService.save(invoice); + // count++; - } - } - return count; - } + // } + // } + // return count; + // } + + /** + * Migriert die Rechnungen mit den neuen cdtr. Nummern + * + * + */ + // @GET + // @Path("/fix-separun") + // @Produces({ MediaType.TEXT_PLAIN }) + // public String fixSepaRun(@QueryParam("maxcount") int maxcount) + // throws QueryException, AccessDeniedException, ProcessingErrorException, + // PluginException, ModelException { + // StringBuffer messageBuffer = new StringBuffer(); + // int totalUpdates = 0; + // if (maxcount <= 0) { + // maxcount = 10; + + // if (isRunning) { + // log("├── sync process already running!", messageBuffer); + // return messageBuffer.toString(); + // } + // isRunning = true; + + // String query = "(type:workitem) AND ($modelversion:rechnungseingang*) AND + // ($taskid:5500)"; + // // int syncs = 0; + // long l = System.currentTimeMillis(); + + // log("├── migration sepa run....", messageBuffer); + // log("│ ├── query=" + query, messageBuffer); + // int totalCount = documentService.count(query); + // log("│ ├── found " + totalCount + " invoices", messageBuffer); + // List invoiceList = documentService.find(query, 999, 0); + // long l1 = System.currentTimeMillis(); + // log("│ ├── ...verifying " + invoiceList.size() + " invoices...", + // messageBuffer); + + // for (ItemCollection invoice : invoiceList) { + + // String cdtrNumber = invoice.getItemValueString("cdtr.number"); + // String cdtrNumberOld = invoice.getItemValueString("cdtr.number.old"); + // String bpPartnerID = invoice.getItemValueString("partner.id"); + // if (bpPartnerID.isEmpty()) { + // continue; + // } + // if (cdtrNumberOld.isEmpty()) { + // continue; + // } + + // // lookup bp .... + // ItemCollection bpPartner = + // businessPartnerService.getBusinessPartnerByID(InvoiceUtil.getBPID(invoice)); + // String fixPartnerName = bpPartner.getItemValueString("partner.name"); + // String fixPartnerId = bpPartner.getItemValueString("partner.id"); + + // if (!bpPartnerID.equals(fixPartnerId)) { + // logger.info("Problem found with invoice: " + invoice.getUniqueID() + " wrong + // partnerID=" + // + bpPartnerID + " correct= " + fixPartnerId); + // logger.info(" correct name= " + fixPartnerName); + + // invoice.setItemValue("partner.id", fixPartnerId); + // invoice.setItemValue("cdtr.name", fixPartnerName); + // invoice.setItemValue("partner.name", fixPartnerName); + // documentService.save(invoice); + // // workflowService.processWorkItem(invoice.event(10)); + // totalUpdates++; + // } + // } + + // log("│ ├── " + totalUpdates + " invoices migrated in " + + // (System.currentTimeMillis() - l1) + "ms ", messageBuffer); + + // } + + // log("├── Successfully " + totalUpdates + " invoices migrated", + // messageBuffer); + + // isRunning = false; + // return messageBuffer.toString(); + // } /** * Diese hilfsmethode liest die Cargosoft Datei mit den Mapping - alt->neu ein * * @return */ - private Map readAdressMapping() { - Map result = new HashMap<>(); - try (InputStream inputStream = getClass().getClassLoader() - .getResourceAsStream("agl_wandlung_20250822.csv"); - BufferedReader reader = new BufferedReader( - new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { + // private Map readAdressMapping() { + // Map result = new HashMap<>(); + // try (InputStream inputStream = getClass().getClassLoader() + // .getResourceAsStream("agl_wandlung_20250822.csv"); + // BufferedReader reader = new BufferedReader( + // new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { - String line; - boolean firstLine = true; + // String line; + // boolean firstLine = true; - while ((line = reader.readLine()) != null) { - if (firstLine) { - firstLine = false; - continue; // Header überspringen - } + // while ((line = reader.readLine()) != null) { + // if (firstLine) { + // firstLine = false; + // continue; // Header überspringen + // } - String[] parts = line.split(";"); - if (parts.length == 2) { - String oldID = parts[0].trim(); - String newID = parts[1].trim(); - result.put(oldID, newID); + // String[] parts = line.split(";"); + // if (parts.length == 2) { + // String oldID = parts[0].trim(); + // String newID = parts[1].trim(); + // result.put(oldID, newID); - } - } - } catch (IOException e) { - // Logging framework verwenden - System.err.println("Fehler beim Laden der CSV: " + e.getMessage()); - } - return result; - } + // } + // } + // } catch (IOException e) { + // // Logging framework verwenden + // System.err.println("Fehler beim Laden der CSV: " + e.getMessage()); + // } + // return result; + // } /** * Hilfsmethode speichert eine cargoosft kreditor object...