2015-01-03 16:34:32 +01:00
|
|
|
package com.minres.scviewer.database.sqlite;
|
|
|
|
|
|
|
|
import java.beans.IntrospectionException;
|
|
|
|
import java.io.File;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import com.minres.scviewer.database.EventTime;
|
2015-01-06 17:14:16 +01:00
|
|
|
import com.minres.scviewer.database.HierNode;
|
|
|
|
import com.minres.scviewer.database.IWaveformDb;
|
|
|
|
import com.minres.scviewer.database.IHierNode;
|
|
|
|
import com.minres.scviewer.database.ITxStream;
|
|
|
|
import com.minres.scviewer.database.IWaveform;
|
2015-01-03 16:34:32 +01:00
|
|
|
import com.minres.scviewer.database.InputFormatException;
|
|
|
|
import com.minres.scviewer.database.RelationType;
|
|
|
|
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-06 17:14:16 +01:00
|
|
|
public class SQLiteDb extends HierNode implements IWaveformDb {
|
2015-01-03 16:34:32 +01:00
|
|
|
|
|
|
|
protected IDatabase database;
|
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
protected List<IWaveform> streams;
|
2015-01-03 16:34:32 +01:00
|
|
|
|
|
|
|
long timeResolution=1;
|
|
|
|
|
|
|
|
private HashMap<String, RelationType> relationMap = new HashMap<String, RelationType>();
|
|
|
|
|
|
|
|
IDatabase getDb(){
|
|
|
|
return database;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SQLiteDb() {
|
|
|
|
super("SQLiteDb");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EventTime getMaxTime() {
|
|
|
|
SQLiteDatabaseSelectHandler<ScvTxEvent> handler = new SQLiteDatabaseSelectHandler<ScvTxEvent>(ScvTxEvent.class,
|
|
|
|
database, "time = SELECT MAX(time) FROM ScvTxEvent");
|
|
|
|
try {
|
|
|
|
List<ScvTxEvent> event = handler.selectObjects();
|
|
|
|
if(event.size()>0)
|
|
|
|
return new EventTime(event.get(0).getTime(), "fs");
|
|
|
|
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return new EventTime(0L, "s");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-06 17:14:16 +01:00
|
|
|
public List<IWaveform> getAllWaves() {
|
2015-01-03 16:34:32 +01:00
|
|
|
if(streams==null){
|
|
|
|
SQLiteDatabaseSelectHandler<ScvStream> handler = new SQLiteDatabaseSelectHandler<ScvStream>(ScvStream.class, database);
|
2015-01-06 17:14:16 +01:00
|
|
|
streams=new ArrayList<IWaveform>();
|
2015-01-03 16:34:32 +01:00
|
|
|
try {
|
|
|
|
for(ScvStream scvStream:handler.selectObjects()){
|
2015-01-06 17:14:16 +01:00
|
|
|
streams.add(new TxStream(this, scvStream));
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException e) {
|
|
|
|
// e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return streams;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void load(File file) throws InputFormatException {
|
|
|
|
database=new SQLiteDatabase(file.getAbsolutePath());
|
|
|
|
SQLiteDatabaseSelectHandler<ScvSimProps> handler = new SQLiteDatabaseSelectHandler<ScvSimProps>(ScvSimProps.class, database);
|
|
|
|
try {
|
|
|
|
for(ScvSimProps scvSimProps:handler.selectObjects()){
|
|
|
|
timeResolution=scvSimProps.getTime_resolution();
|
|
|
|
}
|
|
|
|
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2015-01-06 17:14:16 +01:00
|
|
|
buildHierarchyNodes();
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clear() {
|
|
|
|
database=null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-06 17:14:16 +01:00
|
|
|
public IWaveform getStreamByName(String name) {
|
|
|
|
for (IWaveform n : getAllWaves())
|
2015-01-09 09:16:40 +01:00
|
|
|
if (n.getFullName().equals(name))
|
2015-01-06 17:14:16 +01:00
|
|
|
return n;
|
2015-01-03 16:34:32 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
public ITxStream getStreamById(long id) {
|
|
|
|
for (IWaveform n : getAllWaves())
|
|
|
|
if (n.getId().equals(id))
|
|
|
|
return (ITxStream) n;
|
2015-01-03 16:34:32 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
private void buildHierarchyNodes() throws InputFormatException{
|
|
|
|
for(IWaveform stream:getAllWaves()){
|
2015-01-09 09:16:40 +01:00
|
|
|
String[] hier = stream.getFullName().split("\\.");
|
2015-01-06 17:14:16 +01:00
|
|
|
IHierNode node = this;
|
2015-01-03 16:34:32 +01:00
|
|
|
for(String name:hier){
|
2015-01-06 17:14:16 +01:00
|
|
|
IHierNode n1 = null;
|
|
|
|
for (IHierNode n : node.getChildNodes()) {
|
2015-01-03 16:34:32 +01:00
|
|
|
if (n.getName().equals(name)) {
|
|
|
|
n1=n;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(name == hier[hier.length-1]){ //leaf
|
|
|
|
if(n1!=null) {
|
|
|
|
if(n1 instanceof HierNode){
|
|
|
|
node.getChildNodes().remove(n1);
|
|
|
|
stream.getChildNodes().addAll(n1.getChildNodes());
|
|
|
|
} else {
|
|
|
|
throw new InputFormatException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stream.setName(name);
|
|
|
|
node.getChildNodes().add(stream);
|
|
|
|
node=stream;
|
|
|
|
} else { // intermediate
|
|
|
|
if(n1 != null) {
|
|
|
|
node=n1;
|
|
|
|
} else {
|
|
|
|
HierNode newNode = new HierNode(name);
|
|
|
|
node.getChildNodes().add(newNode);
|
|
|
|
node=newNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public RelationType getRelationType(String relationName) {
|
|
|
|
if(relationMap.containsKey(relationName)) return relationMap.get(relationName);
|
|
|
|
RelationType type = new RelationType(relationName);
|
|
|
|
relationMap.put(relationName, type);
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|