String driver = HBCIDBService.SETTINGS.getString("database.driver",DBSupportH2Impl.class.getName());
String sql = (String) statements.get(driver);
if (sql == null)
throw new ApplicationException(i18n.tr("Datenbank {0} nicht wird unterst�tzt",driver));
HBCIDBService service = null;
try
{
//////////////////////////////////////////////////////////////////////////
// Schritt 1: Neue Spalten anlegen
Logger.info("update sql tables");
ScriptExecutor.execute(new StringReader(sql),myProvider.getConnection(),myProvider.getProgressMonitor());
myProvider.getProgressMonitor().log(i18n.tr("Tabellen aktualisiert"));
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Schritt 2: Kopieren der bisherigen Zeilen
Logger.info("copying data");
myProvider.getProgressMonitor().log(i18n.tr("Kopiere Daten"));
service = new HBCIDBServiceImpl();
service.start();
List<Line> lines = (List<Line>) service.execute("select * from verwendungszweck order by typ,auftrag_id,id",null,new ResultSetExtractor() {
public Object extract(ResultSet rs) throws RemoteException, SQLException
{
List<Line> result = new ArrayList<Line>();
while (rs.next())
{
Line line = new Line();
line.id = rs.getInt("auftrag_id");
line.typ = rs.getInt("typ");
line.zweck = rs.getString("zweck");
result.add(line);
}
return result;
}
});
List<String> l = new ArrayList<String>();
int currentType = 0;
int currentId = 0;
for (Line z:lines)
{
// Erstes Objekt oder noch das gleiche
if (currentId == 0 || currentType == 0 || (z.id == currentId && z.typ == currentType))
{
if (currentId == 0 || currentType == 0)
{
currentId = z.id;
currentType = z.typ;
}
// Zeile sammeln
l.add(z.zweck);
continue;
}
// Objekt-Wechsel -> flush
if (l.size() > 0)
{
String[] sl = l.toArray(new String[l.size()]);
String s = Integer.toString(currentId);
Logger.info("copying " + sl.length + " usage lines for type: " + currentType + ", id: " + s);
for (int i=0;i<sl.length;++i)
Logger.debug(" " + sl[i]);
boolean ausgefuehrt = false;
try
{
switch (currentType)
{
case 1: // Transfer.TYP_UEBERWEISUNG:
Ueberweisung tu = (Ueberweisung) service.createObject(Ueberweisung.class,s);
tu.setWeitereVerwendungszwecke(sl);
ausgefuehrt = tu.ausgefuehrt();
if (ausgefuehrt) ((AbstractDBObject)tu).setAttribute("ausgefuehrt",new Integer(0));
tu.store();
if (ausgefuehrt) tu.setAusgefuehrt(true);
break;
case 2: // Transfer.TYP_LASTSCHRIFT:
Lastschrift tl = (Lastschrift) service.createObject(Lastschrift.class,s);
tl.setWeitereVerwendungszwecke(sl);
ausgefuehrt = tl.ausgefuehrt();
if (ausgefuehrt) ((AbstractDBObject)tl).setAttribute("ausgefuehrt",new Integer(0));
tl.store();
if (ausgefuehrt) tl.setAusgefuehrt(true);
break;
case 3: // Transfer.TYP_DAUERAUFTRAG:
Dauerauftrag td = (Dauerauftrag) service.createObject(Dauerauftrag.class,s);
td.setWeitereVerwendungszwecke(sl);
td.store();
break;
case 4: // Transfer.TYP_UMSATZ:
Umsatz tum = (Umsatz) service.createObject(Umsatz.class,s);
tum.setWeitereVerwendungszwecke(sl);
tum.store();
break;
case 5: // Transfer.TYP_SUEB_BUCHUNG:
SammelUeberweisungBuchung tub = (SammelUeberweisungBuchung) service.createObject(SammelUeberweisungBuchung.class,s);
tub.setWeitereVerwendungszwecke(sl);
tub.store();
break;
case 6: // Transfer.TYP_SLAST_BUCHUNG:
SammelLastBuchung tsb = (SammelLastBuchung) service.createObject(SammelLastBuchung.class,s);
tsb.setWeitereVerwendungszwecke(sl);
tsb.store();
break;
}
}
catch (ObjectNotFoundException onf)
{
Logger.warn(onf.getMessage() + ", skipping");
}
}
currentId = 0;
currentType = 0;
l.clear();
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Schritt 3: Tabelle loeschen
Logger.info("drop table");
ScriptExecutor.execute(new StringReader("drop table verwendungszweck;\n"),myProvider.getConnection(),myProvider.getProgressMonitor());
myProvider.getProgressMonitor().log(i18n.tr("Tabellen aktualisiert"));
//////////////////////////////////////////////////////////////////////////
}
catch (ApplicationException ae)
{
throw ae;
}
catch (Exception e)
{
Logger.error("unable to execute update",e);
throw new ApplicationException(i18n.tr("Fehler beim Ausf�hren des Updates"),e);
}
finally
{
if (service != null)
{
try
{
service.stop(true);
}
catch (Exception e)
{
Logger.error("error while closing db service",e);
}