This commit is contained in:
macgregor 2012-10-24 19:39:43 +05:30
parent 7c6f612bfd
commit 89605e1bc5
4 changed files with 211 additions and 32 deletions

0
android/abc Normal file
View File

View File

@ -37,10 +37,11 @@ public class DBHandle {
private static final String TAG = DBHandle.class.getName();
public static Context me = null;
public static String _DB_NAME = "chalobest";
public static String _DB_NAME = "chalobest_5"; //NOTE: dont forget to change DATABASE_NAME in BEST_DB
// public static String _DB_NAME = "chalobest";
public static String _DATA_DIR = "data";
public static String _DB_PATH = "data";
public static int _DB_FILE_IDENTIFIER = com.best.ui.R.raw.chalobest_sqlite;
public static int _DB_FILE_IDENTIFIER = com.best.ui.R.raw.chalobest;
public static void init(Context cont)
{me = cont;}
@ -92,6 +93,7 @@ public class DBHandle {
db.open( _DB_PATH, jsqlite.Constants.SQLITE_OPEN_READONLY );
Stmt resHandle = db.prepare( query );
int resCounter = 0;
System.out.println("==db.prepare Complete===");
while ( resHandle.step() )
{
no_col = resHandle.column_count();
@ -115,19 +117,86 @@ public class DBHandle {
} catch (Exception e) { System.out.println("DBHandle Excep:"+e.getMessage()); e.printStackTrace();}
return( ret );
}
public static void execInsDelQuery( String query )
{
try
{
jsqlite.Database db = new jsqlite.Database();
db.open( _DB_PATH, jsqlite.Constants.SQLITE_OPEN_READWRITE );
Stmt resHandle = db.prepare( query );
System.out.println("DBHANDLE:resHandle="+resHandle.toString());
int no_col = resHandle.column_count();
System.out.println("EXEC QUERY: "+no_col);
}
catch (Exception e) { System.out.println("DBHandle execInsDelQuery Excep:"+e.getMessage()); e.printStackTrace();}
}
public static void execInsertQuery( String query )
{
//the zip file is opened only to read i.e. input mode...so for insert we need to do tht as output mode line no: 183 and 184
try
{
jsqlite.Database db = new jsqlite.Database();
db.open( _DB_PATH, jsqlite.Constants.SQLITE_INSERT );
Stmt resHandle = db.prepare( query );
String[] args = {"a","b"};
//db.exec(query, null ,args );
// System.out.println("DBHANDLE:resHandle="+resHandle.toString());
// int no_col = resHandle.column_count();
// System.out.println("EXEC QUERY: "+no_col);
}
catch (Exception e) { System.out.println("DBHandle execInsDelQuery Excep:"+e.getMessage()); e.printStackTrace();}
}
// public static void execInsDelQuery( String query )
// {
// try
// {
// jsqlite.Database db = new jsqlite.Database();
// db.open( _DB_PATH, jsqlite.Constants.SQLITE_OPEN_READWRITE );
// Stmt resHandle = db.prepare( query );
// System.out.println("DBHANDLE:resHandle="+resHandle.toString());
// int no_col = resHandle.column_count();
// System.out.println("EXEC QUERY: "+no_col);
// }
// catch (Exception e) { System.out.println("DBHandle execInsDelQuery Excep:"+e.getMessage()); e.printStackTrace();}
// }
public static boolean dbFileReady()
{
boolean Retvalue = false;
try{
File dataPath = me.getDir( _DATA_DIR, 0 );
File DBFile = new File( dataPath, _DB_NAME );
//database folder object
File DBFolder = new File( "//data//data//com.best.ui//databases//" );
if(!DBFile.exists())
{
DBFile.createNewFile();
if( !DBFolder.exists() )
{
if( !DBFolder.mkdir() )
return( false );
System.out.println("DB FOLDER CREATED ");
}
File DBFile = new File( DBFolder, _DB_NAME );
System.out.println("DBFile DATAPATH: "+DBFile.getAbsolutePath());
if( !DBFile.exists() )
{
System.out.println("DB FILE "+_DB_NAME+" does not exist");
//remove old databases...
File[] _oldFiles = DBFolder.listFiles();
for( File _oldFile : _oldFiles )
{
_oldFile.delete();
System.out.println("Deleted old file :"+_oldFile.getName());
}
Retvalue = DBFile.createNewFile();
///////////
InputStream dbin = me.getResources().openRawResource( _DB_FILE_IDENTIFIER );
GZIPInputStream gzin = new GZIPInputStream( dbin );
OutputStream os = new FileOutputStream( DBFile );
//////////
int r;
byte []b = new byte[ 1024 ];
@ -138,8 +207,11 @@ public class DBHandle {
}
_DB_PATH = DBFile.getAbsolutePath();
System.out.println( "DATABASE PATH:" + _DB_PATH );
System.out.println("DATABASE SIZE:"+DBFile.length());
return( true );
}catch(java.lang.Exception e){ Best.log( " DBHandle ERR : " +e.getMessage() ); e.printStackTrace(); }
}catch(java.lang.Exception e){ Best.log( " DBHandle ERR : " +e.getMessage() ); e.printStackTrace();
System.out.println("RETURN VALUE:"+Retvalue);}
return( false );
}
}

View File

@ -44,7 +44,9 @@ public class GTStore{
public static String[][] getAllStopsByText(String matchString, Double lat, Double lon)
{
try{
String query = "select g._stopID, _stopName, Distance( _stopLoc ,ST_GeomFromText('POINT("+ lat +" "+ lon +" )'))*111 distance from stop_names n, stop_geo g where n._stopID = g._stopID and ( _stopName like '"+matchString+"%' or _stopName like '% "+matchString+"%' ) limit "+M_MAX_STOP_SEARCH_ROW_BY_TEXT+";";
//String query = "select g._stopID, _stopName, Distance( _stopLoc ,ST_GeomFromText('POINT("+ lat +" "+ lon +" )'))*111 distance from stop_names n, stop_geo g where n._stopID = g._stopID and ( _stopName like '"+matchString+"%' or _stopName like '% "+matchString+"%' ) limit "+M_MAX_STOP_SEARCH_ROW_BY_TEXT+";";
String query = "select g._stopID, _stopName, Distance( _stopLoc ,ST_GeomFromText('POINT("+ lat +" "+ lon +" )'))*111 distance from stop_names n, stop_geo g where n._stopID = g._stopID and ( _stopName like '"+matchString+"%' or _stopName like '% "+matchString+"%' ) UNION select g._stopID, _stopName, Distance( _stopLoc ,ST_GeomFromText('POINT("+ lat +" "+ lon +" )'))*111 distance from stop_names n, stop_geo g where n._stopID = g._stopID and ( _stopDesc like '"+matchString+"%' or _stopDesc like '% "+matchString+"%' ) order by distance limit "+M_MAX_STOP_SEARCH_ROW_BY_TEXT+";";
System.out.println("===query=="+query);
String[][] resultSet=DBHandle.execQuery( query, M_MAX_STOP_SEARCH_ROW_BY_TEXT );
@ -56,7 +58,12 @@ public class GTStore{
public static String[][] getNearByStops(Double lat, Double lon)
{
String[][] resultSet = DBHandle.execQuery( "select g._stopID,_stopName, Distance( _stopLoc, ST_GeomFromText('POINT(" + lat + " " + lon + ")') )*111 distance from stop_geo g, stop_names n where g._stopID = n._stopID and PtDistWithin( _stopLoc, ST_GeomFromText('POINT(" + lat + " " + lon + ")', 4326),"+M_MAX_RANGE+") order by distance asc limit 30;", M_MAX_STOP_SEARCH_ROW_BY_TEXT );
String nearByStopQuery = "select distinct g._stopID,_stopName, Distance( _stopLoc, ST_GeomFromText('POINT(" + lat + " " + lon + ")') )*111 distance from stop_geo g, stop_names n where g._stopID = n._stopID and Distance( _stopLoc, ST_GeomFromText('POINT(" + lat + " " + lon + ")') )*111 <= 0.5 order by distance asc limit 30 ;";
//"select g._stopID,_stopName, Distance( _stopLoc, ST_GeomFromText('POINT(" + lat + " " + lon + ")') )*111 distance from stop_geo g, stop_names n where g._stopID = n._stopID and PtDistWithin( _stopLoc, ST_GeomFromText('POINT(" + lat + " " + lon + ")', 4326),"+M_MAX_RANGE+") order by distance asc limit 30;";
Best.log("nearByStopQuery=="+nearByStopQuery);
String[][] resultSet = DBHandle.execQuery(nearByStopQuery,M_MAX_STOP_SEARCH_ROW_BY_TEXT);
return(resultSet);
}
@ -67,8 +74,35 @@ public class GTStore{
return(resultSet);
}
public static void insertIntoPreviousSearch(String sourceID, String destID)
{
String currentDateTime = Funcs.getCurrentDateTime();
String delete_query = "delete from previous_search where _sourceStopID = '"+sourceID+"' and _destStopID = '"+destID+"';";
Best.log(delete_query);
//DBHandle.execInsDelQuery( delete_query );
String insert_query = "insert into previous_search (_sourceStopID,_destStopID,_searchDate) values ('"+sourceID+"','"+destID+"','"+currentDateTime+"');";
Best.log(insert_query);
DBHandle.execInsertQuery( insert_query );
String[][] res = DBHandle.execQuery("select * from previous_search;");
//System.out.println("RESULT SIZE = "+res.length+" NUMBER OF ENTERIES = "+res[0][0]);
if(res != null && res.length > 0)
{
for(int i =0 ;i < res.length; i++)
{
for(int j = 0 ; j < res[i].length ;j++)
{System.out.println("RESULT = "+res[i][j]);}
}
}
}
public static String[][] getPreviousSearches()
{
String query = "select _searchID, _sourceStopID,(select _stopName from stop_names where _stopID = _sourceStopID), _destStopID,(select _stopName from stop_names where _stopID = _destStopID), _searchDate from previous_search;";
return null ;
}
/* public static String[][] getRoutes(String fromStopID, String toStopID, String day, String timeStart,String timeEnd)
/*public static String[][] getRoutes(String fromStopID, String toStopID, String day, String timeStart,String timeEnd)
{
try{
// String _qryForAllRouteForFromStop = "select stops_on_trip._tripid , stops_on_trip._stopid, stops_on_trip._stopseq, stops_on_trip._stopdept,stops_on_trip._stopDeptOnNextDay from stops_on_trip, trips, schedule_rules where stops_on_trip._tripid = trips._tripid and trips._serviceid = schedule_rules._serviceid and stops_on_trip._stopid='"+fromStopID+"' and schedule_rules._serviceday='"+day+"' and schedule_rules._startperioddate<=date('now') and date('now')<=schedule_rules._endperioddate and stops_on_trip._stopDept > strftime('%H:%M:%S', '"+timeStart+"') and strftime('%H:%M:%S', '"+timeEnd+"') >(stops_on_trip._stopDept) and stops_on_trip._stopDeptOnNextDay=0";
@ -146,9 +180,9 @@ public class GTStore{
public static String[][] getTripInfo(String tripID)
{
String q = "select _stopName, stop_names._stopID _stopID, _stopSeq ,X(_stopLoc),Y(_stopLoc) from trips, stops_on_trip, stop_names, stop_geo where trips._tripID = '"+tripID+"' and trips._tripID = stops_on_trip._tripID and stops_on_trip._stopID = stop_names._stopID and stop_geo._stopID = stop_names._stopID order by _stopSeq asc";
String q = "select distinct _stopName, stop_names._stopID _stopID, _stopSeq ,X(_stopLoc),Y(_stopLoc) from trips, stops_on_trip, stop_names, stop_geo where trips._tripID = '"+tripID+"' and trips._tripID = stops_on_trip._tripID and stops_on_trip._stopID = stop_names._stopID and stop_geo._stopID = stop_names._stopID order by _stopSeq asc;";
System.out.println("==Query:getTripInfo=="+q);
return ( DBHandle.execQuery( q ) );
}
@ -536,14 +570,14 @@ public class GTStore{
public static String[][] getDirectRoutes(String fromStopID, String toStopID)
{
String _currDay = Funcs.getCurrentDay();
String acceptable_trips = "";
String acceptable_trips_by_id = "";
String working_trips = "";
System.out.println("-----CurrentDAy----"+_currDay);
String q = "select distinct stops_on_trip._tripID, stops_on_trip._stopSeq, trips_freq._start, trips_freq._end, trips_freq._end_next_day, trips_freq._headways, trips_freq._freq, routes._routeLongName , routes._routeName from stops_on_trip, trips_freq, trips, routes, schedule_rules where stops_on_trip._stopID='"+fromStopID +"' and stops_on_trip._tripID = trips_freq._tripID and stops_on_trip._tripID = trips._tripID and schedule_rules._serviceID = trips._serviceID and routes._routeID = trips._routeID and schedule_rules._serviceDay='"+ _currDay +"' order by stops_on_trip._tripID asc, _start asc;" ;
String q = "select stops_on_trip._tripID, stops_on_trip._stopSeq, trips_freq._start, trips_freq._end, trips_freq._end_next_day, trips_freq._headways, trips_freq._freq, routes._routeLongName , routes._routeName from stops_on_trip, trips_freq, trips, routes, schedule_rules where stops_on_trip._stopID='"+fromStopID +"' and stops_on_trip._tripID = trips_freq._tripID and stops_on_trip._tripID = trips._tripID and schedule_rules._serviceID = trips._serviceID and routes._routeID = trips._routeID and schedule_rules._serviceDay='"+ _currDay +"' order by stops_on_trip._tripID asc, _start asc;" ;
String[][] resultSet = DBHandle.execQuery( q );
Best.log( q );
String[][] resultSet = DBHandle.execQuery( q );
Best.log("===resultSet.Length=="+resultSet.length);
if(resultSet != null )
{
@ -565,10 +599,8 @@ public class GTStore{
Best.log("===_tripId=="+_tripId+"==_seq=="+_seq+"==_startTime=="+_startTime+"==_freq=="+_freq+"==_routeLongName=="+_routeLongName+"==_routeName=="+_routeName);
long _startTimeInSecs = Funcs.timeToSeconds( _startTime );
Best.log("===_startTime=="+_startTime);
long _endTimeInSecs = Funcs.timeToSeconds( _endTime );
Best.log("===_endTime=="+_endTime);
long _currTimeInSecs = Funcs.timeToSeconds( _currTime );
Best.log("===_currTime=="+_currTime);
@ -602,28 +634,20 @@ public class GTStore{
Best.log( "STORRING : " + _tripId + " -> " + _seq + " -- " + _freq + " -- " + _startTime + " -- " + _tripId );
if(_passingFromStopsTrips.containsKey( _tripId ) )
{
<<<<<<< .mine
Vector _t = new Vector( 6 );
_t.add( Integer.toString( _seq ) );
_t.add( Integer.toString( _freq ) );
_t.add( _startTime );
_t.add( _headSign );
_t.add( _tripId );
_t.add( _routeName );
=======
System.out.println("KEY EXIST");
Vector _tripVec = (Vector)_passingFromStopsTrips.get(_tripId);
_tripVec.add(_trip_info);
}
else
{
Vector _tripVec = new Vector();
System.out.println("KEY DOESNT EXIST");
_tripVec.add(_trip_info);
_passingFromStopsTrips.put( _tripId, _tripVec );
}
if(((Vector)(_passingFromStopsTrips.get(_tripId))).size() == 1)
_allTripId += ( ( _allTripId.length() > 0 )?( "," ):( "" ) ) + "'"+_tripId+"'";
>>>>>>> .r34
}
}
@ -632,7 +656,7 @@ public class GTStore{
if( _passingFromStopsTrips.size() > 0 && _allTripId.length() > 0 )
{
String q2 = "select _tripID, _stopSeq from stops_on_trip where _tripID in ("+_allTripId+") and _stopID = '"+toStopID+"';";
String q2 = "select distinct _tripID, _stopSeq from stops_on_trip where _tripID in ("+_allTripId+") and _stopID = '"+toStopID+"';";
Best.log( "2nd query======="+q2 );
resultSet = DBHandle.execQuery( q2 );
@ -723,6 +747,7 @@ public class GTStore{
Vector _trip_vec = ( Vector ) _passingFromStopsTrips.get( _trip_id );
for(int i =0; i < _trip_vec.size() ; i++)
{
System.out.println("TRIP_VEC_SIZE="+_trip_vec.size());
if( _passeIndex < _passedTrips )
{
Vector _t = (Vector)(_trip_vec.elementAt(i));
@ -755,5 +780,51 @@ public class GTStore{
Best.log("getDirectRoutes:=====Resultset null=====");
return( null );
}
public static String[][] getAllStops()
{
String _currDay = Funcs.getCurrentDay();
String allStopsQuery = "select _stopID, _stopName from stop_names group by _stopName order by _stopName asc limit 50;";
System.out.println("allStops QUERY==:"+allStopsQuery);
String[][] allStops = DBHandle.execQuery( allStopsQuery );
return (allStops);
}
public static String[][] getAllTrips()
{
String _currDay = Funcs.getCurrentDay();
String allTripsQuery = "select distinct routes._routeID, trips._tripID, _routeLongName ,_routeName , trips_freq._freq from routes, trips, schedule_rules, trips_freq where trips._routeID = routes._routeID and trips._tripID = trips_freq._tripID and trips._serviceID = schedule_rules._serviceID and _serviceDay = '"+_currDay+"' group by routes._routeID order by routes._routeID asc limit 30;";
System.out.println("allTrips QUERY==:"+allTripsQuery);
String[][] allTrips = DBHandle.execQuery( allTripsQuery );
return (allTrips);
}
//------------------------------------
public static String[][] getNearByTripInfo(Double lat, Double lon)
{
String _currDay = Funcs.getCurrentDay();
String nearByStopQuery = "select g._stopID from stop_geo g, stop_names n where g._stopID = n._stopID and Distance( _stopLoc, ST_GeomFromText('POINT(" + lat + " " + lon + ")') )*111 <= 0.5 limit 30 ";
//String nearByPassingTrips = "select distinct _routeLongName from routes, trips tr where tr._routeID = routes._routeID and tr._tripID in (select _tripID from stops_on_trip where _stopID in ("+nearByStopQuery+"));";
String nearByPassingTrips = "select distinct routes._routeID, trips._tripID, _routeLongName ,_routeName ,trips_freq._freq from routes, trips, schedule_rules, trips_freq where trips._routeID = routes._routeID and trips._tripID in (select _tripID from stops_on_trip where _stopID in ( select stop_geo._stopID from stop_geo where Distance( _stopLoc, ST_GeomFromText('POINT("+lat+" "+lon+")') )*111 <= 0.5 limit 30)) and trips_freq._tripID = trips._tripID and trips._serviceID = schedule_rules._serviceID and _serviceDay = '"+_currDay+"' group by routes._routeID order by routes._routeID asc ;";
System.out.println("QUERY==:"+nearByPassingTrips);
//String[][] nearByTrips = DBHandle.execQuery( nearByPassingTrips );
//String _tripIDS = "";
// for(int i = 0; i<nearByTrips.length; i++)
// {
// _tripIDS += ( ( _tripIDS.length() > 0 )?( "," ):( "" ) ) + "'"+nearByTrips[i][1]+"'";
// }
// System.out.println("===_tripIDS=="+_tripIDS);
// String tripWithStopNames = "select stop_names._stopID, stop_names._stopName from stop_names ,stop_geo, stops_on_trip where stop_geo._stopID = stop_names._stopID and Distance( _stopLoc, ST_GeomFromText('POINT(" + lat + " " + lon + ")') )*111 <= 0.5 and stop_names._stopID = stops_on_trip._stopID and stops_on_trip._tripID in ("+_tripIDS+");";
String[][] nearbyTrips_Name = DBHandle.execQuery( nearByPassingTrips );
return (nearbyTrips_Name);
}
}

View File

@ -119,6 +119,38 @@ public class Funcs{
return(hr+":"+min+":"+seconds);
}
public static String secondsToMinHr( int seconds)
{
int hr = seconds/3600;
seconds = seconds%3600;
int min = seconds/60;
seconds = seconds%60;
if(hr > 0)
{
if(min > 0)
{
if(seconds > 0)
return (hr+"hr "+min+"min "+seconds+"sec");
else
return (hr+"hr "+min+"min ");
}
else
return (hr+"hr ");
}
else if(min > 0)
{
if(seconds > 0)
return (min+"min "+seconds+"sec");
else
return (min+"min ");
}
else
return (seconds+"sec");
}
public static String subtractTime(String timeStart, String timeEnd)
{
@ -215,7 +247,11 @@ public class Funcs{
else
return( ( new Formatter() ).format( "%tr", Calendar.getInstance() ).toString() );
}
public static String getCurrentDateTime()
{
return( ( new Formatter() ).format( "%tF %tT", Calendar.getInstance(), Calendar.getInstance() ).toString());
}
public static Calendar getNextDay(Calendar calendar){
calendar.add( Calendar.DAY_OF_MONTH, +1 );
return( calendar );