CsvPreference prefs = CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
String sep = p.getSeparatorChar();
String quo = p.getQuotingChar();
if (sep != null && sep.length() == 1) prefs.setDelimiterChar(sep.charAt(0));
if (quo != null && quo.length() == 1) prefs.setQuoteChar(quo.charAt(0));
ICsvListReader csv = new CsvListReader(new InputStreamReader(new ByteArrayInputStream(data),p.getFileEncoding()),prefs);
List<String> line = csv.read();
// Ggf. erste Zeilen ueberspringen
for (int i=0;i<p.getSkipLines();++i)
{
line = csv.read();
}
// Nochmal checken, ob wir nach dem Ueberspringen ueberhaupt noch Zeilen uebrig haben
if (line == null || line.size() == 0)
throw new ApplicationException(i18n.tr("CSV-Datei enth�lt keine weiteren Daten"));
int created = 0;
int error = 0;
int skipped = 0;
DBObject object = null;
String value = null;
List<Column> cols = p.getColumns();
ImportListener l = f.getImportListener();
DBService service = (DBService) Application.getServiceFactory().lookup(HBCI.class,"database");
do
{
monitor.log(i18n.tr("Importiere Zeile {0}",Integer.toString(csv.getLineNumber())));
monitor.addPercentComplete(1);
try
{
object = service.createObject(f.getType(),null);
// Spalten zuordnen
Map<String, Object> values = new HashMap<String,Object>();
for (int i=0;i<line.size();++i)
{
Column column = null;
for (Column c:cols)
{
if (c.getColumn() == i)
{
column = c;
break;
}
}
if (column == null)
continue; // Spalte nicht zugeordnet
// Checken, ob in der Spalte was steht
value = line.get(i);
if (value == null)
continue;
value = value.trim();
if (value.length() == 0)
continue;
try
{
// Werte zwischenspeichern
Object prev = values.get(column.getProperty());
values.put(column.getProperty(),column.getSerializer().unserialize(prev,value));
}
catch (Exception e)
{
Logger.error("unable to unserialize " + column.getProperty() + " for line " + csv.getLineNumber() + ", value: " + value,e);
monitor.log(" " + i18n.tr("Ung�ltiger Wert \"{0}\" in Spalte \"{1}\": {2}",value,column.getName(),e.getMessage()));
}
}
// beforeSet-Listener ausloesen
if (l != null)
{
ImportEvent e = new ImportEvent();
e.context = context;
e.data = values;
try
{
l.beforeSet(e);
}
catch (OperationCanceledException oce)
{
skipped++;
String msg = oce.getMessage();
if (msg != null && msg.length() > 0)
monitor.log(" " + msg);
continue;
}
}
// Werte in die Bean uebernehmen
Iterator<String> it = values.keySet().iterator();
while (it.hasNext())
{
String name = it.next();
Object o = values.get(name);
try
{
BeanUtil.set(object,name,o);
}
catch (Exception e)
{
Logger.error("unable to apply property " + name + " for line " + csv.getLineNumber() + ", value: " + o,e);
String[] s = new String[]{value,
name,
e.getMessage()};
monitor.log(" " + i18n.tr("Ung�ltiger Wert \"{0}\" in Spalte \"{1}\": {2}",s));
}
}
if (l != null)
{
ImportEvent e = new ImportEvent();
e.context = context;
e.data = object;
try
{
l.beforeStore(e);
}
catch (OperationCanceledException oce)
{
skipped++;
String msg = oce.getMessage();
if (msg != null && msg.length() > 0)
monitor.log(" " + msg);
continue;
}
}
object.store();
Application.getMessagingFactory().sendMessage(new ImportMessage(object));
created++;
}
catch (Exception e)
{
if (!(e instanceof ApplicationException))
Logger.error("unable to import line",e);
monitor.log(" " + i18n.tr("Fehler in Zeile {0}: {1}",new String[]{Integer.toString(csv.getLineNumber()),e.getMessage()}));
error++;
}
}
while ((line = csv.read()) != null);
// Fertig.
monitor.setStatusText(i18n.tr("{0} importiert, {1} fehlerhaft, {2} �bersprungen", new String[]{Integer.toString(created),Integer.toString(error),Integer.toString(skipped)}));
}
catch (OperationCanceledException oce)