Skip to content

Commit

Permalink
impl, refactoring (Issue #173)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoika committed Feb 18, 2025
1 parent fa4b01b commit ecb2702
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.imixs.workflow.datev.imports;
package org.imixs.workflow.datev;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand All @@ -7,7 +7,6 @@
import java.util.logging.Logger;

import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.datev.DatevException;
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.engine.scheduler.SchedulerException;
import org.imixs.workflow.engine.scheduler.SchedulerService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.logging.Logger;

import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.datev.imports.DatevService;
import org.imixs.workflow.datev.DatevService;
import org.imixs.workflow.exceptions.AccessDeniedException;
import org.imixs.workflow.faces.data.WorkflowController;
import org.imixs.workflow.faces.data.WorkflowEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import org.imixs.workflow.FileData;
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.datev.DatevException;
import org.imixs.workflow.datev.DatevService;
import org.imixs.workflow.datev.imports.DatevImportService;
import org.imixs.workflow.datev.imports.DatevService;
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.engine.index.UpdateService;
import org.imixs.workflow.engine.scheduler.Scheduler;
Expand Down Expand Up @@ -404,7 +404,8 @@ public void startImport() throws PluginException {
// test if supported CSV file?
if (file.getName().toLowerCase().endsWith(".csv")) {
ByteArrayInputStream input = new ByteArrayInputStream(file.getContent());
String result = datevImportService.importData(input, "ISO-8859-1");
String encoding = configuration.getItemValueString("datev.encoding");
String result = datevImportService.importData(input, encoding, file.getName());
getImportData().replaceItemValue("log", result);
} else {
throw new PluginException(this.getClass().getName(), DatevException.DATEV_IMPORT_ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.ItemCollectionComparator;
import org.imixs.workflow.datev.imports.DatevService;
import org.imixs.workflow.datev.DatevService;
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.engine.index.SchemaService;
import org.imixs.workflow.faces.data.WorkflowController;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.imixs.workflow.ItemCollectionComparator;
import org.imixs.workflow.SignalAdapter;
import org.imixs.workflow.datev.DatevException;
import org.imixs.workflow.datev.imports.DatevService;
import org.imixs.workflow.datev.DatevService;
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.engine.WorkflowService;
import org.imixs.workflow.exceptions.AccessDeniedException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import org.imixs.workflow.WorkflowKernel;
import org.imixs.workflow.datev.DatevException;
import org.imixs.workflow.datev.DatevHelper;
import org.imixs.workflow.datev.imports.DatevService;
import org.imixs.workflow.datev.DatevService;
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.engine.ReportService;
import org.imixs.workflow.engine.WorkflowService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.SignalAdapter;
import org.imixs.workflow.datev.DatevException;
import org.imixs.workflow.datev.imports.DatevService;
import org.imixs.workflow.datev.DatevService;
import org.imixs.workflow.exceptions.AccessDeniedException;
import org.imixs.workflow.exceptions.AdapterException;
import org.imixs.workflow.exceptions.ModelException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.SignalAdapter;
import org.imixs.workflow.datev.DatevException;
import org.imixs.workflow.datev.imports.DatevService;
import org.imixs.workflow.datev.DatevService;
import org.imixs.workflow.exceptions.AccessDeniedException;
import org.imixs.workflow.exceptions.AdapterException;
import org.imixs.workflow.exceptions.ModelException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.ItemCollectionComparator;
import org.imixs.workflow.datev.DatevException;
import org.imixs.workflow.datev.DatevService;
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.engine.index.SchemaService;
import org.imixs.workflow.exceptions.AccessDeniedException;
Expand All @@ -24,11 +25,11 @@

import jakarta.annotation.security.DeclareRoles;
import jakarta.annotation.security.RolesAllowed;
import jakarta.ejb.EJB;
import jakarta.ejb.LocalBean;
import jakarta.ejb.Stateless;
import jakarta.ejb.TransactionAttribute;
import jakarta.ejb.TransactionAttributeType;
import jakarta.inject.Inject;

/**
* This Service provides methods to import data from a DATEV file.
Expand Down Expand Up @@ -75,12 +76,15 @@ public class DatevImportService {
public final static String ISO8601_FORMAT_DATETIME = "yyyy-MM-dd'T'HH:mm:ss.SSS";
public final static String ISO8601_FORMAT_DATE = "yyyy-MM-dd";

@EJB
@Inject
DocumentService documentService;

@EJB
@Inject
SchemaService schemaService;

@Inject
DatevService datevService;

private static Logger logger = Logger.getLogger(DatevImportService.class.getName());

/**
Expand Down Expand Up @@ -196,11 +200,15 @@ public List<ItemCollection> searchEntity(String phrase, String type, String clie
*
* The method returns a log . If an error occurs a plugin exception is thrown
*
* @param imputStream - file content
* @param encoding - encoding
* @param _filename - optional filename
*
* @return ErrorMessage or empty String
* @throws PluginException
*/
@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public String importData(InputStream imputStream, String encoding) throws PluginException {
public String importData(InputStream imputStream, String encoding, String _filename) throws PluginException {
String type = null;
String clientID = null;
String consultenID = null;
Expand All @@ -218,48 +226,63 @@ public String importData(InputStream imputStream, String encoding) throws Plugin
int blockSize = 0;

log(log, "├── starte Dateimport");
if (encoding == null) {
if (encoding == null || encoding.trim().isEmpty()) {
encoding = "UTF-8";
}
encoding = encoding.trim();
log(log, "│   ├── encoding=" + encoding);

String fieldNames = null;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(imputStream, encoding));

// read first line containing the object type
String header1 = in.readLine();
String[] header1List = header1.split(";(?=([^\"]*\"[^\"]*\")*[^\"]*$)", 99);
header1List = normalizeValueList(header1List);
if (header1List == null || header1List.length < 4) {
throw new PluginException(this.getClass().getName(), DatevException.DATEV_IMPORT_ERROR,
"File Format not supported, 1st line must contain the data format in column 4 (type).");
}
type = header1List[3];

if (type == null || type.isEmpty()) {
throw new PluginException(this.getClass().getName(), DatevException.DATEV_IMPORT_ERROR,
"File Format not supported, 1st line must contain the data format in column 4 (type).");

}

clientID = header1List[10];
log(log, "│   ├── Mandant ID= " + clientID);
consultenID = header1List[11];
log(log, "│   ├── Berater ID= " + consultenID);
if (clientID == null || clientID.isEmpty() || consultenID == null || consultenID.isEmpty()) {
throw new PluginException(this.getClass().getName(), DatevException.DATEV_IMPORT_ERROR,
"File Format not supported, 1st line must contain the Mandant and Berater ID.");
String header = in.readLine();
String[] headerList = header.split(";(?=([^\"]*\"[^\"]*\")*[^\"]*$)", 99);
headerList = normalizeValueList(headerList);
// Test "DTVF" Export format
// "DTVF";700;20;"Kontenbeschriftungen";2;20180917165240335;;"RE";"Mustermann";"";217386;21010;20180101;6;;;"";"";;0;;"";;"";;141987;"04";;;"";""
if ("DTVF".endsWith(headerList[0])) {
log(log, "│   ├── Format: DTVF");
consultenID = headerList[11];
log(log, "│   ├── Berater ID= " + consultenID);
clientID = headerList[10];
log(log, "│   ├── Mandant ID= " + clientID);
if (clientID == null || clientID.isEmpty() || consultenID == null || consultenID.isEmpty()) {
throw new PluginException(this.getClass().getName(), DatevException.DATEV_IMPORT_ERROR,
"Ungültiges DTVF Importformat: Erste Zeile die Mandant ID '" + clientID
+ "' und die Berater ID '" + consultenID + "' enthalten!");
}
type = headerList[3];
type = type.trim().toLowerCase();
log(log, "│   └── Objekt Typ=" + type);
// read header
fieldNames = in.readLine();
} else {
log(log, "│   ├── Format: CSV/Excel");
// read datev config
ItemCollection datevConfig = datevService.loadConfiguration();
consultenID = datevConfig.getItemValueString(DatevService.ITEM_DATEV_CONSULTANT_ID);
log(log, "│   ├── Berater ID= " + consultenID);
clientID = datevConfig.getItemValueString(DatevService.ITEM_DATEV_CLIENT_ID);
log(log, "│   ├── Mandant ID= " + clientID);

// Excel Format. No DATEV Header
// Datei name muss mit 'DTVF_DebKred_Stamm' oder 'DTVF_SKBeschrift' beginnen
if (_filename.startsWith("DTVF_DebKred_Stamm")) {
type = "Debitoren/Kreditoren";
}
if (_filename.startsWith("DTVF_SKBeschrift")) {
type = "kontenbeschriftungen";
}
if (type == null) {
throw new PluginException(this.getClass().getName(), DatevException.DATEV_IMPORT_ERROR,
"Ungültiges Importformat: Dateiname muss entweder mit 'DTVF_DebKred_Stamm' oder 'DTVF_SKBeschrift' beginnen!");
}
log(log, "│   └── Objekt Typ=" + type);
fieldNames = header;
}

type = type.trim().toLowerCase();
log(log, "│   └── Objekt Type=" + type);
line++;

log(log, "├── Lese Daten...");
// read the 2nd first line containing the field names
String fieldnames = in.readLine();
line++;
List<String> fields = parseFieldList(fieldnames);
List<String> fields = parseFieldList(fieldNames);
// the first field is the keyfield
String keyField = fields.get(0).trim();

Expand Down Expand Up @@ -368,12 +391,12 @@ public String importData(InputStream imputStream, String encoding) throws Plugin
throw new PluginException(DatevImportService.class.getName(), DatevException.DATEV_DATA_ERROR, sError, e);
}

log(log, "└── Abgeschlossen in " + (System.currentTimeMillis() - l) + " ms");
log(log, "│   ├── " + workitemsTotal + " Einträge gelesen ");
log(log, "│   ├── " + workitemsImported + " neue Einträge ");
log(log, "│   ├── " + workitemsUpdated + " aktualisierte Einträge ");
log(log, "│   ├── " + workitemsDeleted + " gelöschte Einträge ");
log(log, "│   └── " + workitemsFailed + " fehlerhafte Einträge ");
log(log, "└── Abgeschlossen in " + (System.currentTimeMillis() - l) + " ms");
return log.toString();
}

Expand Down

0 comments on commit ecb2702

Please sign in to comment.