SCViewer/plugins/com.minres.scviewer.databas.../src/com/minres/scviewer/database/sqlite/SQLiteDbLoader.java

148 lines
4.9 KiB
Java
Raw Normal View History

/*******************************************************************************
2021-01-09 14:26:49 +01:00
* Copyright (c) 2015-2021 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
*******************************************************************************/
package com.minres.scviewer.database.sqlite;
import java.beans.IntrospectionException;
2021-01-09 20:10:58 +01:00
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.io.FileInputStream;
2021-01-09 20:10:58 +01:00
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
2015-01-06 17:14:16 +01:00
import com.minres.scviewer.database.IWaveform;
import com.minres.scviewer.database.IWaveformDb;
import com.minres.scviewer.database.IWaveformDbLoader;
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;
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 SQLiteDbLoader implements IWaveformDbLoader {
protected IDatabase database;
private List<RelationType> usedRelationsList = new ArrayList<>();
private ScvSimProps scvSimProps;
2021-01-09 20:10:58 +01:00
private static final byte[] x = "SQLite format 3".getBytes();
/** The pcs. */
protected PropertyChangeSupport pcs = new PropertyChangeSupport(this);
@Override
2015-01-20 18:50:15 +01:00
public Long getMaxTime() {
SQLiteDatabaseSelectHandler<ScvTxEvent> handler = new SQLiteDatabaseSelectHandler<>(ScvTxEvent.class,
2015-01-20 18:50:15 +01:00
database, "time = (SELECT MAX(time) FROM ScvTxEvent)");
try {
List<ScvTxEvent> event = handler.selectObjects();
if(!event.isEmpty())
return event.get(0).getTime()*scvSimProps.getTime_resolution();
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
| InvocationTargetException | SQLException | IntrospectionException e) {
e.printStackTrace();
}
2015-01-20 18:50:15 +01:00
return 0L;
}
@Override
2020-04-02 14:13:39 +02:00
public Collection<IWaveform> getAllWaves() {
SQLiteDatabaseSelectHandler<ScvStream> handler = new SQLiteDatabaseSelectHandler<>(ScvStream.class, database);
List<IWaveform> streams=new ArrayList<>();
try {
for(ScvStream scvStream:handler.selectObjects()){
2021-01-02 17:02:05 +01:00
TxStream stream = new TxStream(database, scvStream);
stream.setRelationTypeList(usedRelationsList);
streams.add(stream);
}
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
| InvocationTargetException | SQLException | IntrospectionException e) {
}
return streams;
}
2021-01-09 20:10:58 +01:00
@Override
public boolean canLoad(File inputFile) {
if (!inputFile.isDirectory() && inputFile.exists()) {
try(InputStream stream = new FileInputStream(inputFile)){
byte[] buffer = new byte[x.length];
int readCnt = stream.read(buffer, 0, x.length);
if (readCnt == x.length) {
for (int i = 0; i < x.length; i++)
if (buffer[i] != x[i])
return false;
}
return true;
} catch (Exception e) {
return false;
}
}
return false;
}
@Override
2021-01-09 20:10:58 +01:00
public void load(IWaveformDb db, File file) throws InputFormatException {
2021-01-02 17:02:05 +01:00
database=new SQLiteDatabase(file.getAbsolutePath(), db);
database.setData("TIMERESOLUTION", 1L);
SQLiteDatabaseSelectHandler<ScvSimProps> handler = new SQLiteDatabaseSelectHandler<>(ScvSimProps.class, database);
try {
for(ScvSimProps simProps:handler.selectObjects()){
scvSimProps=simProps;
database.setData("TIMERESOLUTION", scvSimProps.getTime_resolution());
}
2021-01-09 20:10:58 +01:00
pcs.firePropertyChange(IWaveformDbLoader.LOADING_FINISHED, null, null);
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
| InvocationTargetException | SQLException | IntrospectionException e) {
2021-01-09 23:24:00 +01:00
throw new InputFormatException(e.toString());
}
}
2021-01-08 15:04:30 +01:00
public void dispose() {
database=null;
usedRelationsList=null;
}
@Override
public Collection<RelationType> getAllRelationTypes(){
return usedRelationsList;
}
2021-01-09 20:10:58 +01:00
/**
* Adds the property change listener.
*
* @param l the l
*/
@Override
public void addPropertyChangeListener(PropertyChangeListener l) {
pcs.addPropertyChangeListener(l);
}
/**
* Removes the property change listener.
*
* @param l the l
*/
@Override
public void removePropertyChangeListener(PropertyChangeListener l) {
pcs.removePropertyChangeListener(l);
}
}