- added SQLite back end
- reworked graphical representation to use widgets
This commit is contained in:
@ -0,0 +1,38 @@
|
||||
package com.minres.scviewer.database.sqlite;
|
||||
|
||||
import com.minres.scviewer.database.AssociationType;
|
||||
import com.minres.scviewer.database.DataType;
|
||||
import com.minres.scviewer.database.ITrAttribute;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvTxAttribute;
|
||||
|
||||
public class Attribute implements ITrAttribute{
|
||||
|
||||
Transaction trTransaction;
|
||||
ScvTxAttribute scvAttribute;
|
||||
|
||||
public Attribute(Transaction trTransaction, ScvTxAttribute scvAttribute) {
|
||||
this.trTransaction=trTransaction;
|
||||
this.scvAttribute=scvAttribute;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return scvAttribute.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataType getDataType() {
|
||||
return DataType.values()[scvAttribute.getData_type()];
|
||||
}
|
||||
|
||||
@Override
|
||||
public AssociationType getType() {
|
||||
return AssociationType.values()[scvAttribute.getType()];
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValue() {
|
||||
return scvAttribute.getData_value();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.minres.scviewer.database.sqlite;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.minres.scviewer.database.ITrGenerator;
|
||||
import com.minres.scviewer.database.ITrStream;
|
||||
import com.minres.scviewer.database.ITransaction;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvGenerator;
|
||||
|
||||
public class Generator implements ITrGenerator {
|
||||
|
||||
private ITrStream stream;
|
||||
private ScvGenerator scvGenerator;
|
||||
public Generator(ITrStream stream, ScvGenerator scvGenerator) {
|
||||
this.stream=stream;
|
||||
this.scvGenerator=scvGenerator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getId() {
|
||||
return (long) scvGenerator.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITrStream getStream() {
|
||||
return stream;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return scvGenerator.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ITransaction> getTransactions() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.minres.scviewer.database.sqlite;
|
||||
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.minres.scviewer.database.ITrHierNode;
|
||||
|
||||
public class HierNode implements ITrHierNode {
|
||||
|
||||
protected String name;
|
||||
|
||||
protected ArrayList<ITrHierNode> childs;
|
||||
|
||||
public HierNode(String name) {
|
||||
this.name=name;
|
||||
childs = new ArrayList<ITrHierNode>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPropertyChangeListener(PropertyChangeListener l) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePropertyChangeListener(PropertyChangeListener l) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
this.name=name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ITrHierNode> getChildNodes() {
|
||||
return childs;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.minres.scviewer.database.sqlite;
|
||||
|
||||
import com.minres.scviewer.database.ITrRelation;
|
||||
import com.minres.scviewer.database.ITransaction;
|
||||
import com.minres.scviewer.database.RelationType;
|
||||
|
||||
public class Relation implements ITrRelation {
|
||||
|
||||
RelationType relationType;
|
||||
Transaction source, target;
|
||||
|
||||
public Relation(RelationType relationType, Transaction source, Transaction target) {
|
||||
this.source = source;
|
||||
this.target = target;
|
||||
this.relationType = relationType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RelationType getRelationType() {
|
||||
return relationType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITransaction getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITransaction getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.minres.scviewer.database.sqlite;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import com.minres.scviewer.database.sqlite.db.IDatabase;
|
||||
|
||||
public class SQLiteDatabase implements IDatabase {
|
||||
|
||||
protected String dbFileName;
|
||||
|
||||
public SQLiteDatabase(String dbFileName) {
|
||||
super();
|
||||
this.dbFileName = dbFileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection createConnection() throws SQLException {
|
||||
// create a database connection and return it
|
||||
return DriverManager.getConnection(getConnectionUrl() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getConnectionUrl() {
|
||||
// now we set up a set of fairly basic string variables to use in the body of the code proper
|
||||
String sJdbc = "jdbc:sqlite";
|
||||
String sDbUrl = sJdbc + ":" + dbFileName;
|
||||
// which will produce a legitimate Url for SqlLite JDBC :
|
||||
// jdbc:sqlite:hello.db
|
||||
return sDbUrl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(ResultSet resultSet, Statement statement, Connection connection) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(PreparedStatement preparedStatement, Connection connection) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,157 @@
|
||||
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;
|
||||
import com.minres.scviewer.database.ITrAttribute;
|
||||
import com.minres.scviewer.database.ITrDb;
|
||||
import com.minres.scviewer.database.ITrHierNode;
|
||||
import com.minres.scviewer.database.ITrStream;
|
||||
import com.minres.scviewer.database.InputFormatException;
|
||||
import com.minres.scviewer.database.RelationType;
|
||||
import com.minres.scviewer.database.sqlite.db.IDatabase;
|
||||
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;
|
||||
|
||||
public class SQLiteDb extends HierNode implements ITrDb {
|
||||
|
||||
protected IDatabase database;
|
||||
|
||||
protected List<ITrStream> streams;
|
||||
|
||||
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
|
||||
public List<ITrStream> getAllStreams() {
|
||||
if(streams==null){
|
||||
SQLiteDatabaseSelectHandler<ScvStream> handler = new SQLiteDatabaseSelectHandler<ScvStream>(ScvStream.class, database);
|
||||
streams=new ArrayList<ITrStream>();
|
||||
try {
|
||||
for(ScvStream scvStream:handler.selectObjects()){
|
||||
streams.add(new Stream(this, scvStream));
|
||||
}
|
||||
} 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();
|
||||
}
|
||||
addHierarchyNodes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
database=null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITrStream getStreamByName(String name) {
|
||||
for (ITrStream n : getAllStreams()) {
|
||||
if (n.getName().equals(name)) {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ITrStream getStreamById(long id) {
|
||||
for (ITrStream n : getAllStreams()) {
|
||||
if (n.getId().equals(id)) {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void addHierarchyNodes() throws InputFormatException{
|
||||
for(ITrStream stream:getAllStreams()){
|
||||
String[] hier = stream.getFullName().split("\\./");
|
||||
ITrHierNode node = this;
|
||||
for(String name:hier){
|
||||
ITrHierNode n1 = null;
|
||||
for (ITrHierNode n : node.getChildNodes()) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.minres.scviewer.database.sqlite;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
||||
import com.minres.scviewer.database.ITrDb;
|
||||
import com.minres.scviewer.database.ITransactionDbFactory;
|
||||
|
||||
public class SQLiteDbFactory implements ITransactionDbFactory {
|
||||
|
||||
private byte[] x = "SQLite format 3".getBytes();
|
||||
|
||||
public SQLiteDbFactory() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITrDb createDatabase(File file) {
|
||||
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 null;
|
||||
SQLiteDb db = new SQLiteDb();
|
||||
db.load(file);
|
||||
return db;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
package com.minres.scviewer.database.sqlite;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import com.minres.scviewer.database.ITrDb;
|
||||
import com.minres.scviewer.database.ITrGenerator;
|
||||
import com.minres.scviewer.database.ITrStream;
|
||||
import com.minres.scviewer.database.ITransaction;
|
||||
import com.minres.scviewer.database.sqlite.db.SQLiteDatabaseSelectHandler;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvGenerator;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvStream;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvTx;
|
||||
|
||||
public class Stream extends HierNode implements ITrStream {
|
||||
|
||||
private String fullName;
|
||||
private SQLiteDb db;
|
||||
private ScvStream scvStream;
|
||||
private HashMap<Integer, Generator> generators;
|
||||
|
||||
private List<ITransaction> transactions;
|
||||
|
||||
public Stream(SQLiteDb trSQLiteDb, ScvStream scvStream) {
|
||||
super(scvStream.getName());
|
||||
fullName=scvStream.getName();
|
||||
this.scvStream=scvStream;
|
||||
db=trSQLiteDb;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName() {
|
||||
return fullName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getId() {
|
||||
return (long) scvStream.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKind() {
|
||||
return scvStream.getKind();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLiteDb getDb() {
|
||||
return db;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ITrGenerator> getGenerators() {
|
||||
if(generators==null){
|
||||
SQLiteDatabaseSelectHandler<ScvGenerator> handler = new SQLiteDatabaseSelectHandler<ScvGenerator>(
|
||||
ScvGenerator.class, db.getDb(), "stream="+scvStream.getId());
|
||||
generators=new HashMap<Integer, Generator>();
|
||||
try {
|
||||
for(ScvGenerator scvGenerator:handler.selectObjects()){
|
||||
generators.put(scvGenerator.getId(), new Generator(this, scvGenerator));
|
||||
}
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return new ArrayList<ITrGenerator>(generators.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ITransaction> getTransactions() {
|
||||
checkTransactions();
|
||||
return transactions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITransaction getTransactionById(long id) {
|
||||
checkTransactions();
|
||||
for(ITransaction trans:transactions){
|
||||
if(trans.getId()==id)
|
||||
return trans;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void checkTransactions() {
|
||||
if(transactions==null){
|
||||
if(generators==null) getGenerators();
|
||||
SQLiteDatabaseSelectHandler<ScvTx> handler = new SQLiteDatabaseSelectHandler<ScvTx>(ScvTx.class, db.getDb(),
|
||||
"stream="+scvStream.getId());
|
||||
transactions=new ArrayList<ITransaction>();
|
||||
try {
|
||||
for(ScvTx scvTx:handler.selectObjects()){
|
||||
transactions.add(new Transaction(this, generators.get(scvTx.getGenerator()), scvTx));
|
||||
}
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,154 @@
|
||||
package com.minres.scviewer.database.sqlite;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import com.minres.scviewer.database.AssociationType;
|
||||
import com.minres.scviewer.database.EventTime;
|
||||
import com.minres.scviewer.database.ITrAttribute;
|
||||
import com.minres.scviewer.database.ITrGenerator;
|
||||
import com.minres.scviewer.database.ITrRelation;
|
||||
import com.minres.scviewer.database.ITrStream;
|
||||
import com.minres.scviewer.database.ITransaction;
|
||||
import com.minres.scviewer.database.sqlite.db.SQLiteDatabaseSelectHandler;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvTx;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvTxAttribute;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvTxEvent;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvTxRelation;
|
||||
|
||||
public class Transaction implements ITransaction {
|
||||
|
||||
private Stream trStream;
|
||||
private Generator trGenerator;
|
||||
private ScvTx scvTx;
|
||||
private List<ITrAttribute> attributes;
|
||||
private EventTime begin, end;
|
||||
private List<ITrRelation> incoming, outgoing;
|
||||
|
||||
public Transaction(Stream trStream, Generator trGenerator, ScvTx scvTx) {
|
||||
this.trStream=trStream;
|
||||
this.trGenerator=trGenerator;
|
||||
this.scvTx=scvTx;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getId() {
|
||||
return (long) scvTx.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITrStream getStream() {
|
||||
return trStream;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITrGenerator getGenerator() {
|
||||
return trGenerator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EventTime getBeginTime() {
|
||||
if(begin==null){
|
||||
SQLiteDatabaseSelectHandler<ScvTxEvent> handler = new SQLiteDatabaseSelectHandler<ScvTxEvent>(ScvTxEvent.class,
|
||||
trStream.getDb().getDb(), "tx="+scvTx.getId()+" AND type="+ AssociationType.BEGIN.ordinal());
|
||||
try {
|
||||
for(ScvTxEvent scvEvent:handler.selectObjects()){
|
||||
begin= new EventTime(scvEvent.getTime()*trStream.getDb().timeResolution, "fs");
|
||||
}
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException e) {
|
||||
}
|
||||
}
|
||||
return begin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EventTime getEndTime() {
|
||||
if(end==null){
|
||||
SQLiteDatabaseSelectHandler<ScvTxEvent> handler = new SQLiteDatabaseSelectHandler<ScvTxEvent>(ScvTxEvent.class,
|
||||
trStream.getDb().getDb(), "tx="+scvTx.getId()+" AND type="+ AssociationType.END.ordinal());
|
||||
try {
|
||||
for(ScvTxEvent scvEvent:handler.selectObjects()){
|
||||
end = new EventTime(scvEvent.getTime()*trStream.getDb().timeResolution, "fs");
|
||||
}
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException e) {
|
||||
}
|
||||
}
|
||||
return end;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ITrAttribute> getAttributes() {
|
||||
if(attributes==null){
|
||||
SQLiteDatabaseSelectHandler<ScvTxAttribute> handler = new SQLiteDatabaseSelectHandler<ScvTxAttribute>(
|
||||
ScvTxAttribute.class, trStream.getDb().getDb(), "tx="+scvTx.getId());
|
||||
try {
|
||||
attributes = new ArrayList<ITrAttribute>();
|
||||
for(ScvTxAttribute scvAttribute:handler.selectObjects()){
|
||||
attributes.add(new Attribute(this, scvAttribute));
|
||||
|
||||
}
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException e) {
|
||||
}
|
||||
}
|
||||
return attributes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ITrRelation> getIncomingRelations() {
|
||||
if(incoming==null){
|
||||
SQLiteDatabaseSelectHandler<ScvTxRelation> handler = new SQLiteDatabaseSelectHandler<ScvTxRelation>(
|
||||
ScvTxRelation.class, trStream.getDb().getDb(), "sink="+scvTx.getId());
|
||||
try {
|
||||
incoming = new ArrayList<ITrRelation>();
|
||||
for(ScvTxRelation scvRelation:handler.selectObjects()){
|
||||
incoming.add(createRelation(scvRelation, false));
|
||||
}
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException e) {
|
||||
}
|
||||
}
|
||||
return incoming;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ITrRelation> getOutgoingRelations() {
|
||||
if(outgoing==null){
|
||||
SQLiteDatabaseSelectHandler<ScvTxRelation> handler = new SQLiteDatabaseSelectHandler<ScvTxRelation>(
|
||||
ScvTxRelation.class, trStream.getDb().getDb(), "src="+scvTx.getId());
|
||||
try {
|
||||
outgoing = new ArrayList<ITrRelation>();
|
||||
for(ScvTxRelation scvRelation:handler.selectObjects()){
|
||||
outgoing.add(createRelation(scvRelation, true));
|
||||
}
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException e) {
|
||||
}
|
||||
}
|
||||
return outgoing;
|
||||
}
|
||||
|
||||
private ITrRelation createRelation(ScvTxRelation rel, boolean outgoing) {
|
||||
long otherId = outgoing?rel.getSink():rel.getSrc();
|
||||
try {
|
||||
List<ScvTx> scvTx=new SQLiteDatabaseSelectHandler<ScvTx>(ScvTx.class, trStream.getDb().getDb(), "id="+otherId).selectObjects();
|
||||
assert(scvTx.size()==1);
|
||||
ITrStream stream = trStream.getDb().getStreamById(scvTx.get(0).getStream());
|
||||
Transaction that=(Transaction) stream.getTransactionById(otherId);
|
||||
if(outgoing)
|
||||
return new Relation(trStream.getDb().getRelationType(rel.getName()), this, that);
|
||||
else
|
||||
return new Relation(trStream.getDb().getRelationType(rel.getName()), that, this);
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException e) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package com.minres.scviewer.database.sqlite.db;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
|
||||
/**
|
||||
* An abstract class that handles insert/select-operations into/from a database
|
||||
*
|
||||
* @author Tino for http://www.java-blog.com
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public abstract class AbstractDatabaseHandler<T> {
|
||||
|
||||
/**
|
||||
* The type of the objects that should be created and filled with values
|
||||
* from the database or inserted into the database
|
||||
*/
|
||||
protected Class<T> type;
|
||||
|
||||
/**
|
||||
* Contains the settings to create a connection to the database like
|
||||
* host/port/database/user/password
|
||||
*/
|
||||
protected IDatabase databaseConnectionFactory;
|
||||
|
||||
/** The SQL-select-query */
|
||||
protected final String query;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param type
|
||||
* The type of the objects that should be created and filled with
|
||||
* values from the database or inserted into the database
|
||||
* @param databaseConnecter
|
||||
* Contains the settings to create a connection to the database
|
||||
* like host/port/database/user/password
|
||||
*/
|
||||
protected AbstractDatabaseHandler(Class<T> type,
|
||||
IDatabase databaseConnectionFactory, String criterion) {
|
||||
|
||||
this.databaseConnectionFactory = databaseConnectionFactory;
|
||||
this.type = type;
|
||||
this.query = createQuery(criterion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the SQL-String to insert into / select from the database
|
||||
*
|
||||
* @return the SQL-String
|
||||
*/
|
||||
protected abstract String createQuery(String criterion);
|
||||
|
||||
/**
|
||||
*
|
||||
* Creates a comma-separated-String with the names of the variables in this
|
||||
* class
|
||||
*
|
||||
* @param usePlaceHolders
|
||||
* true, if PreparedStatement-placeholders ('?') should be used
|
||||
* instead of the names of the variables
|
||||
* @return
|
||||
*/
|
||||
protected String getColumns(boolean usePlaceHolders) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
boolean first = true;
|
||||
/* Iterate the column-names */
|
||||
for (Field f : type.getDeclaredFields()) {
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
sb.append(", ");
|
||||
|
||||
if (usePlaceHolders)
|
||||
sb.append("?");
|
||||
else
|
||||
sb.append(f.getName());
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.minres.scviewer.database.sqlite.db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
*
|
||||
* Creates a connection to a database.
|
||||
*
|
||||
* @author Tino
|
||||
* @created 03.12.2008
|
||||
*
|
||||
*/
|
||||
public interface IDatabase {
|
||||
|
||||
/**
|
||||
* Establishes a new connection to the database
|
||||
*
|
||||
* @return A new connection to the database
|
||||
* @throws SQLException
|
||||
*/
|
||||
public Connection createConnection() throws SQLException;
|
||||
|
||||
/**
|
||||
* Returns the connection url
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getConnectionUrl();
|
||||
|
||||
/**
|
||||
* releases the result set
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public void close(ResultSet resultSet, Statement statement, Connection connection);
|
||||
|
||||
/**
|
||||
* releases the preparedStatement
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public void close(PreparedStatement preparedStatement, Connection connection);
|
||||
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
package com.minres.scviewer.database.sqlite.db;
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class that inserts a list of <T>s into the corresponding database-table.
|
||||
*
|
||||
* @author Tino for http://www.java-blog.com
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class SQLiteDatabaseInsertHandler<T> extends AbstractDatabaseHandler<T> {
|
||||
|
||||
public SQLiteDatabaseInsertHandler(Class<T> type,
|
||||
IDatabase databaseConnecter) {
|
||||
super(type, databaseConnecter, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createQuery(String criterion) {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append("INSERT INTO ");
|
||||
sb.append(type.getSimpleName());
|
||||
sb.append("(");
|
||||
sb.append(super.getColumns(false));
|
||||
sb.append(")");
|
||||
sb.append(" VALUES (");
|
||||
sb.append(super.getColumns(true));
|
||||
sb.append(")");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a list of <T>s into the corresponding database-table
|
||||
*
|
||||
* @param list
|
||||
* List of <T>s that should be inserted into the corresponding
|
||||
* database-table
|
||||
*
|
||||
* @throws SQLException
|
||||
* @throws SecurityException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws InstantiationException
|
||||
* @throws IllegalAccessException
|
||||
* @throws IntrospectionException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public void insertObjects(List<T> list) throws SQLException,
|
||||
SecurityException, IllegalArgumentException,
|
||||
InstantiationException, IllegalAccessException,
|
||||
IntrospectionException, InvocationTargetException {
|
||||
|
||||
Connection connection = null;
|
||||
PreparedStatement preparedStatement = null;
|
||||
|
||||
try {
|
||||
connection = databaseConnectionFactory.createConnection();
|
||||
preparedStatement = connection.prepareStatement(query);
|
||||
|
||||
for (T instance : list) {
|
||||
int i = 0;
|
||||
|
||||
for (Field field : type.getDeclaredFields()) {
|
||||
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
|
||||
field.getName(), type);
|
||||
|
||||
Method method = propertyDescriptor
|
||||
.getReadMethod();
|
||||
|
||||
Object value = method.invoke(instance);
|
||||
|
||||
preparedStatement.setObject(++i, value);
|
||||
}
|
||||
|
||||
preparedStatement.addBatch();
|
||||
}
|
||||
preparedStatement.executeBatch();
|
||||
|
||||
} finally {
|
||||
databaseConnectionFactory.close(preparedStatement, connection);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,135 @@
|
||||
package com.minres.scviewer.database.sqlite.db;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class that creates a list of <T>s filled with values from the corresponding
|
||||
* database-table.
|
||||
*
|
||||
* @author Tino for http://www.java-blog.com
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class SQLiteDatabaseSelectHandler<T> extends AbstractDatabaseHandler<T> {
|
||||
|
||||
public SQLiteDatabaseSelectHandler(Class<T> type,
|
||||
IDatabase databaseConnectionFactory) {
|
||||
super(type, databaseConnectionFactory, null);
|
||||
}
|
||||
|
||||
public SQLiteDatabaseSelectHandler(Class<T> type,
|
||||
IDatabase databaseConnectionFactory, String criteria) {
|
||||
super(type, databaseConnectionFactory, criteria);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createQuery(String criterion) {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append("SELECT ");
|
||||
sb.append(super.getColumns(false));
|
||||
sb.append(" FROM ");
|
||||
|
||||
/* We assume the table-name exactly matches the simpleName of T */
|
||||
sb.append(type.getSimpleName());
|
||||
if(criterion!=null){
|
||||
sb.append(" WHERE ( ");
|
||||
sb.append(criterion);
|
||||
sb.append(" )");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a list of <T>s filled with values from the corresponding
|
||||
* database-table
|
||||
*
|
||||
* @return List of <T>s filled with values from the corresponding
|
||||
* database-table
|
||||
*
|
||||
* @throws SQLException
|
||||
* @throws SecurityException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws InstantiationException
|
||||
* @throws IllegalAccessException
|
||||
* @throws IntrospectionException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public List<T> selectObjects() throws SQLException,
|
||||
SecurityException, IllegalArgumentException,
|
||||
InstantiationException, IllegalAccessException,
|
||||
IntrospectionException, InvocationTargetException {
|
||||
|
||||
Connection connection = null;
|
||||
Statement statement = null;
|
||||
ResultSet resultSet = null;
|
||||
|
||||
try {
|
||||
connection = databaseConnectionFactory.createConnection();
|
||||
statement = connection.createStatement();
|
||||
resultSet = statement.executeQuery(query);
|
||||
|
||||
return createObjects(resultSet);
|
||||
|
||||
} finally {
|
||||
databaseConnectionFactory.close(resultSet, statement, connection);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Creates a list of <T>s filled with values from the provided ResultSet
|
||||
*
|
||||
* @param resultSet
|
||||
* ResultSet that contains the result of the
|
||||
* database-select-query
|
||||
*
|
||||
* @return List of <T>s filled with values from the provided ResultSet
|
||||
*
|
||||
* @throws SecurityException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws SQLException
|
||||
* @throws InstantiationException
|
||||
* @throws IllegalAccessException
|
||||
* @throws IntrospectionException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
private List<T> createObjects(ResultSet resultSet)
|
||||
throws SecurityException, IllegalArgumentException,
|
||||
SQLException, InstantiationException,
|
||||
IllegalAccessException, IntrospectionException,
|
||||
InvocationTargetException {
|
||||
|
||||
List<T> list = new ArrayList<T>();
|
||||
|
||||
while (resultSet.next()) {
|
||||
|
||||
T instance = type.newInstance();
|
||||
|
||||
for (Field field : type.getDeclaredFields()) {
|
||||
|
||||
/* We assume the table-column-names exactly match the variable-names of T */
|
||||
Object value = resultSet.getObject(field.getName());
|
||||
if(value!=null){
|
||||
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), type);
|
||||
propertyDescriptor.getWriteMethod().invoke(instance, value);
|
||||
}
|
||||
}
|
||||
|
||||
list.add(instance);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.minres.scviewer.database.sqlite.tables;
|
||||
|
||||
public class ScvGenerator {
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getStream() {
|
||||
return stream;
|
||||
}
|
||||
|
||||
public void setStream(int stream) {
|
||||
this.stream = stream;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getBegin_attr() {
|
||||
return begin_attr;
|
||||
}
|
||||
|
||||
public void setBegin_attr(int begin_attr) {
|
||||
this.begin_attr = begin_attr;
|
||||
}
|
||||
|
||||
public int getEnd_attr() {
|
||||
return end_attr;
|
||||
}
|
||||
|
||||
public void setEnd_attr(int end_attr) {
|
||||
this.end_attr = end_attr;
|
||||
}
|
||||
|
||||
private int id;
|
||||
private int stream;
|
||||
private String name;
|
||||
private int begin_attr;
|
||||
private int end_attr;
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.minres.scviewer.database.sqlite.tables;
|
||||
|
||||
public class ScvSimProps {
|
||||
|
||||
private long time_resolution;
|
||||
|
||||
public ScvSimProps() {
|
||||
super();
|
||||
}
|
||||
|
||||
public long getTime_resolution() {
|
||||
return time_resolution;
|
||||
}
|
||||
|
||||
public void setTime_resolution(long time_resolution) {
|
||||
this.time_resolution = time_resolution;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.minres.scviewer.database.sqlite.tables;
|
||||
|
||||
public class ScvStream {
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getKind() {
|
||||
return kind;
|
||||
}
|
||||
|
||||
public void setKind(String kind) {
|
||||
this.kind = kind;
|
||||
}
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
private String kind;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.minres.scviewer.database.sqlite.tables;
|
||||
|
||||
public class ScvTx {
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getGenerator() {
|
||||
return generator;
|
||||
}
|
||||
|
||||
public void setGenerator(int generator) {
|
||||
this.generator = generator;
|
||||
}
|
||||
|
||||
public int getStream() {
|
||||
return stream;
|
||||
}
|
||||
|
||||
public void setStream(int stream) {
|
||||
this.stream = stream;
|
||||
}
|
||||
|
||||
private int id;
|
||||
private int generator;
|
||||
private int stream;
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.minres.scviewer.database.sqlite.tables;
|
||||
|
||||
public class ScvTxAttribute {
|
||||
|
||||
public int getTx() {
|
||||
return tx;
|
||||
}
|
||||
|
||||
public void setTx(int tx) {
|
||||
this.tx = tx;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(int type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getData_type() {
|
||||
return data_type;
|
||||
}
|
||||
|
||||
public void setData_type(int data_type) {
|
||||
this.data_type = data_type;
|
||||
}
|
||||
|
||||
public String getData_value() {
|
||||
return data_value;
|
||||
}
|
||||
|
||||
public void setData_value(String data_value) {
|
||||
this.data_value = data_value;
|
||||
}
|
||||
|
||||
private int tx;
|
||||
private int type;
|
||||
private String name;
|
||||
private int data_type;
|
||||
private String data_value;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.minres.scviewer.database.sqlite.tables;
|
||||
|
||||
public class ScvTxEvent {
|
||||
public int getTx() {
|
||||
return tx;
|
||||
}
|
||||
|
||||
public void setTx(int tx) {
|
||||
this.tx = tx;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(int type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(long time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
private int tx;
|
||||
private int type;
|
||||
private long time;
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.minres.scviewer.database.sqlite.tables;
|
||||
|
||||
public class ScvTxRelation {
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getSrc() {
|
||||
return src;
|
||||
}
|
||||
|
||||
public void setSrc(int src) {
|
||||
this.src = src;
|
||||
}
|
||||
|
||||
public int getSink() {
|
||||
return sink;
|
||||
}
|
||||
|
||||
public void setSink(int sink) {
|
||||
this.sink = sink;
|
||||
}
|
||||
|
||||
private String name;
|
||||
private int src;
|
||||
private int sink;
|
||||
|
||||
}
|
Reference in New Issue
Block a user