package com.minres.scviewer.database.sqlite; import java.beans.IntrospectionException; import java.io.File; import java.io.FileInputStream; 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.IWaveform; import com.minres.scviewer.database.IWaveformDb; import com.minres.scviewer.database.IWaveformDbLoader; import com.minres.scviewer.database.RelationType; import com.minres.scviewer.database.sqlite.db.IDatabase; 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; protected List streams; long timeResolution=1; private HashMap relationMap = new HashMap(); private IWaveformDb db; public SQLiteDbLoader() { } @Override public EventTime getMaxTime() { SQLiteDatabaseSelectHandler handler = new SQLiteDatabaseSelectHandler(ScvTxEvent.class, database, "time = SELECT MAX(time) FROM ScvTxEvent"); try { List event = handler.selectObjects(); if(event.size()>0) return new EventTime(event.get(0).getTime()); } catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException | InvocationTargetException | SQLException | IntrospectionException e) { e.printStackTrace(); } return EventTime.ZERO; } @Override public List getAllWaves() { if(streams==null){ SQLiteDatabaseSelectHandler handler = new SQLiteDatabaseSelectHandler(ScvStream.class, database); streams=new ArrayList(); try { for(ScvStream scvStream:handler.selectObjects()){ streams.add(new TxStream(database, db, scvStream)); } } catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException | InvocationTargetException | SQLException | IntrospectionException e) { // e.printStackTrace(); } } return streams; } private byte[] x = "SQLite format 3".getBytes(); @Override public boolean load(IWaveformDb db, File file) throws Exception { this.db=db; 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; database=new SQLiteDatabase(file.getAbsolutePath()); database.setData("TIMERESOLUTION", 1L); SQLiteDatabaseSelectHandler handler = new SQLiteDatabaseSelectHandler(ScvSimProps.class, database); try { for(ScvSimProps scvSimProps:handler.selectObjects()){ database.setData("TIMERESOLUTION", scvSimProps.getTime_resolution()); } return true; } catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException | InvocationTargetException | SQLException | IntrospectionException e) { e.printStackTrace(); } return false; } public RelationType getRelationType(String relationName) { if(relationMap.containsKey(relationName)) return relationMap.get(relationName); RelationType type = new RelationType(relationName); relationMap.put(relationName, type); return type; } }