2015-01-21 21:58:35 +01:00
|
|
|
/*******************************************************************************
|
2015-10-22 00:25:12 +02:00
|
|
|
* Copyright (c) 2015 MINRES Technologies GmbH and others.
|
2015-01-21 21:58:35 +01:00
|
|
|
* 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;
|
2018-11-05 18:21:54 +01:00
|
|
|
import java.io.FileNotFoundException;
|
2015-01-03 16:34:32 +01:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.ArrayList;
|
2015-11-15 22:15:37 +01:00
|
|
|
import java.util.Collection;
|
2015-01-03 16:34:32 +01:00
|
|
|
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-11-15 22:15:37 +01:00
|
|
|
import com.minres.scviewer.database.RelationType;
|
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-11-15 22:15:37 +01:00
|
|
|
private List<RelationType> usedRelationsList = new ArrayList<>();
|
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
|
2018-11-05 18:21:54 +01:00
|
|
|
public List<IWaveform> getAllWaves() {
|
2015-10-22 00:02:58 +02:00
|
|
|
SQLiteDatabaseSelectHandler<ScvStream> handler = new SQLiteDatabaseSelectHandler<ScvStream>(ScvStream.class, database);
|
2018-11-05 18:21:54 +01:00
|
|
|
List<IWaveform> streams=new ArrayList<IWaveform>();
|
2015-10-22 00:02:58 +02:00
|
|
|
try {
|
|
|
|
for(ScvStream scvStream:handler.selectObjects()){
|
|
|
|
TxStream stream = new TxStream(database, db, scvStream);
|
2015-11-15 22:15:37 +01:00
|
|
|
stream.setRelationTypeList(usedRelationsList);
|
2015-10-22 00:02:58 +02:00
|
|
|
streams.add(stream);
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
2015-10-22 00:02:58 +02:00
|
|
|
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException e) {
|
|
|
|
// e.printStackTrace();
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
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;
|
2018-11-05 18:21:54 +01:00
|
|
|
try {
|
|
|
|
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;
|
|
|
|
} catch(FileNotFoundException e) {
|
|
|
|
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
|
|
|
}
|
2015-11-15 22:15:37 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Collection<RelationType> getAllRelationTypes(){
|
|
|
|
return usedRelationsList;
|
|
|
|
}
|
|
|
|
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|