package com.alexanderlogistics;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.SignalAdapter;
import org.imixs.workflow.WorkflowKernel;
import org.imixs.workflow.engine.WorkflowService;
import org.imixs.workflow.engine.plugins.SplitAndJoinPlugin;
import org.imixs.workflow.exceptions.AdapterException;
import org.imixs.workflow.exceptions.ModelException;
import org.imixs.workflow.exceptions.PluginException;
import org.imixs.workflow.office.util.SequenceService;
import jakarta.inject.Inject;
/**
* Der CargosoftSplitAdapter erezeugt anhand der Positionstablelle eignee
* cargosoft export workitems, getrennt für jede Buchungsperdiode. Dies ist
* notwendig, da Cargosoft selbst nicht mit unterschiedlichen Buchungsperioden
* in einer Rechnung umgehen kann.
*
* adapter exports the invoice data to a ftp server connected to cargosoft. Da
* auch eine fortlaufene Rechnungsnummer von Cargosoft zwingend vorgeschrieben
* ist, erzeugt der adapter auch für jedes weiteres cargosoft export worktiem
* ein neue Sequencenummer die im Hautpworkitem gespeichert wird.
*
* Der Adapter wird im Modell wie folgt konfiguriert
*
*
* {@code
cargosoft-export-1.0
1000
100
(?!txtworkflowhistory)(^[a-zA-Z]|^_)
}
*
*
* Because we also export the attachment data to cargosoft, the adaper lookups
* the conente of the attachment in the snapshot of the origin workitem
*
*
* @version 1.0
* @author rsoika
*/
public class CargosoftSplitAdapter implements SignalAdapter {
public static final String CONFIG_ERROR = "CONFIG_ERROR";
private static Logger logger = Logger.getLogger(CargosoftSplitAdapter.class.getName());
@Inject
WorkflowService workflowService;
@Inject
SequenceService sequenceService;
/**
* This method computes the cargosoft export data file
*
* @throws PluginException
*/
@SuppressWarnings("rawtypes")
@Override
public ItemCollection execute(ItemCollection workitem, ItemCollection event)
throws AdapterException, PluginException {
logger.info("......starting export...");
try {
// read the cargosoft split options
ItemCollection evalItemCollection = workflowService.evalWorkflowResult(event, "cargosoft", workitem, false);
if (evalItemCollection == null) {
throw new PluginException(CargosoftSplitAdapter.class.getSimpleName(), CONFIG_ERROR,
"missign cargosoft configuration in model event - please check model configuration");
}
String model = evalItemCollection.getItemValueString("model");
int taskID = evalItemCollection.getItemValueInteger("task");
int eventID = evalItemCollection.getItemValueInteger("event");
String items = evalItemCollection.getItemValueString("items");
if (model.isEmpty() || taskID == 0 || eventID == 0) {
throw new PluginException(CargosoftSplitAdapter.class.getSimpleName(), CONFIG_ERROR,
"missign cargosoft configuration 'model', 'task', 'event' - please check model configuration");
}
// Zunaechst müssen wir festelstellen, ob es mehrere unterschiedliche
// Buchungsperioden in der Positionstabelle gibt.
List buchungsPersioden = new ArrayList();
// add haupt buchungsperiode
buchungsPersioden.add(workitem.getItemValueString("invoice.period"));
List positionsTabelle = InvoiceUtil.explodeChildList(workitem);
for (ItemCollection posItem : positionsTabelle) {
String childPeriod = posItem.getItemValueString(InvoiceUtil.ITEM_INVOICE_PERIOD);
if (!childPeriod.isEmpty() && !buchungsPersioden.contains(childPeriod)) {
buchungsPersioden.add(childPeriod);
}
}
// nun wird für jede Buchungsperiode ein eigener corgosoft Export angestoßen....
boolean nextSequencenumber = false;
for (String buchungsPeriode : buchungsPersioden) {
// nun entnehmen wir aus der positionsTabelle alle einträge die zu dieser
// Periode passen...
// (die erste Periode ist die default periode welche wir nehemn wenn keine
// Periode angegeben wurde).
ItemCollection workitemSubProcess = new ItemCollection();
// now clone the field list...
copyItemList(items, workitem, workitemSubProcess);
// in die _childs Liste kommen jetzt nur passende Positionseinträge mit der
// selben Buchungsperiode
List