2015-01-21 21:58:35 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2014, 2015 MINRES Technologies GmbH and others.
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* MINRES Technologies GmbH - initial API and implementation
|
|
|
|
*******************************************************************************/
|
2015-01-03 16:34:32 +01:00
|
|
|
package com.minres.scviewer.database.sqlite;
|
|
|
|
|
|
|
|
import java.beans.IntrospectionException;
|
|
|
|
import java.io.File;
|
2015-01-10 00:23:46 +01:00
|
|
|
import java.io.FileInputStream;
|
2015-01-03 16:34:32 +01:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
import com.minres.scviewer.database.IWaveform;
|
2015-01-10 00:23:46 +01:00
|
|
|
import com.minres.scviewer.database.IWaveformDb;
|
|
|
|
import com.minres.scviewer.database.IWaveformDbLoader;
|
2015-01-20 18:50:15 +01:00
|
|
|
import com.minres.scviewer.database.IWaveformEvent;
|
2015-01-03 16:34:32 +01:00
|
|
|
import com.minres.scviewer.database.sqlite.db.IDatabase;
|
2015-01-06 17:14:16 +01:00
|
|
|
import com.minres.scviewer.database.sqlite.db.SQLiteDatabase;
|
2015-01-03 16:34:32 +01:00
|
|
|
import com.minres.scviewer.database.sqlite.db.SQLiteDatabaseSelectHandler;
|
|
|
|
import com.minres.scviewer.database.sqlite.tables.ScvSimProps;
|
|
|
|
import com.minres.scviewer.database.sqlite.tables.ScvStream;
|
|
|
|
import com.minres.scviewer.database.sqlite.tables.ScvTxEvent;
|
|
|
|
|
2015-01-10 00:23:46 +01:00
|
|
|
public class SQLiteDbLoader implements IWaveformDbLoader {
|
2015-01-03 16:34:32 +01:00
|
|
|
|
|
|
|
protected IDatabase database;
|
|
|
|
|
2015-01-20 18:50:15 +01:00
|
|
|
protected List<IWaveform<? extends IWaveformEvent>> streams;
|
2015-01-03 16:34:32 +01:00
|
|
|
|
|
|
|
long timeResolution=1;
|
|
|
|
|
2015-01-21 21:58:35 +01:00
|
|
|
private RelationTypeFactory rtf = new RelationTypeFactory();
|
2015-01-10 00:23:46 +01:00
|
|
|
|
|
|
|
private IWaveformDb db;
|
2015-01-03 16:34:32 +01:00
|
|
|
|
2015-01-21 21:58:35 +01:00
|
|
|
private ScvSimProps scvSimProps;
|
|
|
|
|
2015-01-10 00:23:46 +01:00
|
|
|
public SQLiteDbLoader() {
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-20 18:50:15 +01:00
|
|
|
public Long getMaxTime() {
|
2015-01-03 16:34:32 +01:00
|
|
|
SQLiteDatabaseSelectHandler<ScvTxEvent> handler = new SQLiteDatabaseSelectHandler<ScvTxEvent>(ScvTxEvent.class,
|
2015-01-20 18:50:15 +01:00
|
|
|
database, "time = (SELECT MAX(time) FROM ScvTxEvent)");
|
2015-01-03 16:34:32 +01:00
|
|
|
try {
|
|
|
|
List<ScvTxEvent> event = handler.selectObjects();
|
|
|
|
if(event.size()>0)
|
2015-01-21 21:58:35 +01:00
|
|
|
return event.get(0).getTime()*scvSimProps.getTime_resolution();
|
2015-01-03 16:34:32 +01:00
|
|
|
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2015-01-20 18:50:15 +01:00
|
|
|
return 0L;
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-20 18:50:15 +01:00
|
|
|
public List<IWaveform<? extends IWaveformEvent>> getAllWaves() {
|
2015-01-03 16:34:32 +01:00
|
|
|
if(streams==null){
|
|
|
|
SQLiteDatabaseSelectHandler<ScvStream> handler = new SQLiteDatabaseSelectHandler<ScvStream>(ScvStream.class, database);
|
2015-01-20 18:50:15 +01:00
|
|
|
streams=new ArrayList<IWaveform<? extends IWaveformEvent>>();
|
2015-01-03 16:34:32 +01:00
|
|
|
try {
|
|
|
|
for(ScvStream scvStream:handler.selectObjects()){
|
2015-01-21 21:58:35 +01:00
|
|
|
TxStream stream = new TxStream(database, db, scvStream);
|
|
|
|
stream.setRelationTypeFactory(rtf);
|
|
|
|
streams.add(stream);
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException e) {
|
|
|
|
// e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return streams;
|
|
|
|
}
|
|
|
|
|
2015-01-10 00:23:46 +01:00
|
|
|
private byte[] x = "SQLite format 3".getBytes();
|
|
|
|
|
2015-01-03 16:34:32 +01:00
|
|
|
@Override
|
2015-01-10 00:23:46 +01:00
|
|
|
public boolean load(IWaveformDb db, File file) throws Exception {
|
|
|
|
this.db=db;
|
|
|
|
FileInputStream fis = new FileInputStream(file);
|
|
|
|
byte[] buffer = new byte[x.length];
|
|
|
|
int read = fis.read(buffer, 0, x.length);
|
|
|
|
fis.close();
|
|
|
|
if (read == x.length)
|
|
|
|
for (int i = 0; i < x.length; i++)
|
|
|
|
if (buffer[i] != x[i]) return false;
|
|
|
|
|
2015-01-03 16:34:32 +01:00
|
|
|
database=new SQLiteDatabase(file.getAbsolutePath());
|
2015-01-10 00:23:46 +01:00
|
|
|
database.setData("TIMERESOLUTION", 1L);
|
2015-01-03 16:34:32 +01:00
|
|
|
SQLiteDatabaseSelectHandler<ScvSimProps> handler = new SQLiteDatabaseSelectHandler<ScvSimProps>(ScvSimProps.class, database);
|
|
|
|
try {
|
2015-01-21 21:58:35 +01:00
|
|
|
for(ScvSimProps simProps:handler.selectObjects()){
|
|
|
|
scvSimProps=simProps;
|
2015-01-10 00:23:46 +01:00
|
|
|
database.setData("TIMERESOLUTION", scvSimProps.getTime_resolution());
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
2015-01-10 00:23:46 +01:00
|
|
|
return true;
|
2015-01-03 16:34:32 +01:00
|
|
|
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2015-01-10 00:23:46 +01:00
|
|
|
return false;
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
}
|