2015-01-21 21:58:35 +01:00
|
|
|
/*******************************************************************************
|
2021-01-09 14:26:49 +01:00
|
|
|
* Copyright (c) 2015-2021 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;
|
2020-03-21 11:31:59 +01:00
|
|
|
import java.io.IOException;
|
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;
|
2020-11-29 10:25:48 +01:00
|
|
|
import com.minres.scviewer.database.InputFormatException;
|
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-03 16:34:32 +01:00
|
|
|
|
2015-01-21 21:58:35 +01:00
|
|
|
private ScvSimProps scvSimProps;
|
|
|
|
|
2015-01-03 16:34:32 +01:00
|
|
|
@Override
|
2015-01-20 18:50:15 +01:00
|
|
|
public Long getMaxTime() {
|
2020-11-29 10:25:48 +01:00
|
|
|
SQLiteDatabaseSelectHandler<ScvTxEvent> handler = new SQLiteDatabaseSelectHandler<>(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();
|
2020-11-29 10:25:48 +01:00
|
|
|
if(!event.isEmpty())
|
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
|
2020-04-02 14:13:39 +02:00
|
|
|
public Collection<IWaveform> getAllWaves() {
|
2020-11-29 10:25:48 +01:00
|
|
|
SQLiteDatabaseSelectHandler<ScvStream> handler = new SQLiteDatabaseSelectHandler<>(ScvStream.class, database);
|
|
|
|
List<IWaveform> streams=new ArrayList<>();
|
2015-10-22 00:02:58 +02:00
|
|
|
try {
|
|
|
|
for(ScvStream scvStream:handler.selectObjects()){
|
2021-01-02 17:02:05 +01:00
|
|
|
TxStream stream = new TxStream(database, 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) {
|
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
|
2020-11-29 10:25:48 +01:00
|
|
|
public boolean load(IWaveformDb db, File file) throws InputFormatException {
|
2021-01-08 15:04:30 +01:00
|
|
|
dispose();
|
2020-03-29 16:10:36 +02:00
|
|
|
if(file.isDirectory() || !file.exists()) return false;
|
2020-11-29 10:25:48 +01:00
|
|
|
try(FileInputStream fis = new FileInputStream(file)) {
|
2018-11-05 18:21:54 +01:00
|
|
|
byte[] buffer = new byte[x.length];
|
|
|
|
int read = fis.read(buffer, 0, x.length);
|
|
|
|
if (read == x.length)
|
|
|
|
for (int i = 0; i < x.length; i++)
|
|
|
|
if (buffer[i] != x[i]) return false;
|
2020-11-29 10:25:48 +01:00
|
|
|
} catch(IOException e) {
|
2020-03-21 11:31:59 +01:00
|
|
|
return false;
|
2018-11-05 18:21:54 +01:00
|
|
|
}
|
2021-01-02 17:02:05 +01:00
|
|
|
database=new SQLiteDatabase(file.getAbsolutePath(), db);
|
2015-01-10 00:23:46 +01:00
|
|
|
database.setData("TIMERESOLUTION", 1L);
|
2020-11-29 10:25:48 +01:00
|
|
|
SQLiteDatabaseSelectHandler<ScvSimProps> handler = new SQLiteDatabaseSelectHandler<>(ScvSimProps.class, database);
|
2015-01-03 16:34:32 +01:00
|
|
|
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
|
|
|
}
|
2021-01-08 15:04:30 +01:00
|
|
|
|
|
|
|
public void dispose() {
|
|
|
|
database=null;
|
|
|
|
usedRelationsList=null;
|
|
|
|
}
|
|
|
|
|
2015-11-15 22:15:37 +01:00
|
|
|
@Override
|
|
|
|
public Collection<RelationType> getAllRelationTypes(){
|
|
|
|
return usedRelationsList;
|
|
|
|
}
|
|
|
|
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|