/
AbstractSepaImporter.java
168 lines (145 loc) · 5.33 KB
/
AbstractSepaImporter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**********************************************************************
*
* Copyright (c) by Olaf Willuhn
* All rights reserved
*
**********************************************************************/
package de.willuhn.jameica.hbci.io;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.kapott.hbci.GV.parsers.ISEPAParser;
import org.kapott.hbci.GV.parsers.SEPAParserFactory;
import org.kapott.hbci.sepa.PainVersion;
import de.willuhn.io.IOUtil;
import de.willuhn.jameica.hbci.gui.dialogs.KontoAuswahlDialog;
import de.willuhn.jameica.hbci.gui.filter.KontoFilter;
import de.willuhn.jameica.hbci.rmi.Konto;
import de.willuhn.jameica.hbci.server.KontoUtil;
import de.willuhn.jameica.system.OperationCanceledException;
import de.willuhn.logging.Logger;
import de.willuhn.util.ApplicationException;
import de.willuhn.util.ProgressMonitor;
/**
* Abstrakte Basis-Klasse fuer SEPA-Import.
*/
public abstract class AbstractSepaImporter extends AbstractImporter
{
private Map<String,Konto> kontenCache = new HashMap<String,Konto>();
/**
* @see de.willuhn.jameica.hbci.io.IO#getName()
*/
@Override
public String getName()
{
return i18n.tr("SEPA-XML");
}
/**
* @see de.willuhn.jameica.hbci.io.AbstractExporter#getFileExtensions()
*/
@Override
String[] getFileExtensions()
{
return new String[]{"*.xml"};
}
/**
* @see de.willuhn.jameica.hbci.io.AbstractImporter#setup(java.lang.Object, de.willuhn.jameica.hbci.io.IOFormat, java.io.InputStream, de.willuhn.util.ProgressMonitor)
*/
@Override
Object[] setup(Object context, IOFormat format, InputStream is, ProgressMonitor monitor) throws Exception
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IOUtil.copy(is,bos);
PainVersion version = PainVersion.autodetect(new ByteArrayInputStream(bos.toByteArray()));
if (version == null)
throw new ApplicationException(i18n.tr("SEPA-Version der XML-Datei nicht ermittelbar"));
monitor.log(i18n.tr("SEPA-Version: {0}",version.getURN()));
// Überprüfe PAIN Typ
PainVersion.Type[] types = this.getSupportedPainTypes();
PainVersion.Type type = version.getType();
boolean found = false;
for (int i = 0; types != null && i < types.length; i++) {
if (types[i] == type) {
found = true;
}
}
if (!found) {
Logger.error("invalid file PAIN type");
monitor.log(i18n.tr("Ungültiger PAIN Typ: {0}",type.getName()));
if (!this.useForce) {
throw new ApplicationException(i18n.tr("Unzulässige SEPA-Version in der XML-Datei - Überweisung und Lastschrift verwechselt?"));
}
}
List<Properties> props = new ArrayList<Properties>();
ISEPAParser parser = SEPAParserFactory.get(version);
parser.parse(new ByteArrayInputStream(bos.toByteArray()),props);
return props.toArray(new Properties[props.size()]);
}
/**
* @see de.willuhn.jameica.hbci.io.AbstractImporter#commit(java.lang.Object[], de.willuhn.jameica.hbci.io.IOFormat, java.io.InputStream, de.willuhn.util.ProgressMonitor)
*/
@Override
void commit(Object[] objects, IOFormat format, InputStream is, ProgressMonitor monitor) throws Exception
{
kontenCache.clear();
super.commit(objects,format,is,monitor);
}
/**
* Sucht nach dem Konto mit der angegebenen IBAN.
* @param iban
* @return das gefundene Konto oder wenn es nicht gefunden wurde, dann das vom Benutzer ausgewaehlte.
* Die Funktion liefert nie <code>null</code> sondern wirft eine ApplicationException, wenn kein Konto ausgewaehlt wurde.
* @throws RemoteException
* @throws ApplicationException
* @throws OperationCanceledException
*/
protected Konto findKonto(String iban) throws RemoteException, ApplicationException
{
// Erstmal schauen, ob der User das Konto schonmal ausgewaehlt hat:
Konto k = (Konto) kontenCache.get(iban);
// Haben wir im Cache
if (k != null)
return k;
// In der Datenbank suchen
k = KontoUtil.findByIBAN(iban);
// Nichts gefunden. Dann fragen wir den User
if (k == null)
{
// Das Konto existiert nicht im Hibiscus-Datenbestand. Also soll der
// User eines auswaehlen
KontoAuswahlDialog d = new KontoAuswahlDialog(null,KontoFilter.FOREIGN,KontoAuswahlDialog.POSITION_CENTER);
d.setText(i18n.tr("Konto {0} nicht gefunden\n" +
"Bitte wählen Sie das zu verwendende Konto aus.",iban == null || iban.length() == 0 ? i18n.tr("<unbekannt>") : iban));
try
{
k = (Konto) d.open();
}
catch (OperationCanceledException oce)
{
throw new ApplicationException(i18n.tr("Auftrag wird übersprungen"));
}
catch (Exception e)
{
throw new ApplicationException(i18n.tr("Fehler beim Auswählen des Kontos"),e);
}
}
if (k != null)
{
kontenCache.put(iban,k);
return k;
}
throw new ApplicationException(i18n.tr("Kein Konto ausgewählt"));
}
/**
* Liste der zulässigen XML Daten
* Wird benötigt, damit eine Lastschrift nicht als Überweisung importiert werden kann.
* @return erlaubte PAIN Typen
*/
abstract PainVersion.Type[] getSupportedPainTypes();
}