/[projects]/android/People/src/com/grundfos/android/people/PeopleDatabase.java
ViewVC logotype

Diff of /android/People/src/com/grundfos/android/people/PeopleDatabase.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 229 by torben, Tue Aug 4 13:16:25 2009 UTC revision 230 by torben, Wed Aug 5 08:53:33 2009 UTC
# Line 13  import android.database.SQLException; Line 13  import android.database.SQLException;
13  import android.database.sqlite.SQLiteDatabase;  import android.database.sqlite.SQLiteDatabase;
14  import android.database.sqlite.SQLiteOpenHelper;  import android.database.sqlite.SQLiteOpenHelper;
15  import android.database.sqlite.SQLiteStatement;  import android.database.sqlite.SQLiteStatement;
16    import android.os.Handler;
17    import android.os.Message;
18  import android.util.Log;  import android.util.Log;
19    
20  public class PeopleDatabase  {  public class PeopleDatabase  {
# Line 28  public class PeopleDatabase  { Line 30  public class PeopleDatabase  {
30                  {                  {
31                          super(context, DATABASE_NAME, null, DATABASE_VERSION);                            super(context, DATABASE_NAME, null, DATABASE_VERSION);  
32                  }                  }
33                    
34                  @Override                  @Override
35                  public void onCreate(SQLiteDatabase db) {                  public void onCreate(SQLiteDatabase db) {
36                          db.beginTransaction();                          db.beginTransaction();
# Line 58  public class PeopleDatabase  { Line 60  public class PeopleDatabase  {
60          }          }
61    
62          PeopleDatabaseHelper dbHelper;          PeopleDatabaseHelper dbHelper;
63            
64          public PeopleDatabase(Context context) {          public PeopleDatabase(Context context) {
65                  dbHelper = new PeopleDatabaseHelper(context);                  dbHelper = new PeopleDatabaseHelper(context);
66          }          }
# Line 135  public class PeopleDatabase  { Line 137  public class PeopleDatabase  {
137                  }                  }
138          }          }
139    
140          private void loadData(String data)  
141            class DataLoader
142          {          {
143                  SQLiteDatabase db = dbHelper.getWritableDatabase();                  SQLiteDatabase db;
144                  db.beginTransaction();                  Handler handler;
145                  db.execSQL("DELETE FROM people");                  int count = 0;
146                  StringTokenizer lines = new StringTokenizer( data, "|" );                  int maxCount = 30000;
147                    SQLiteStatement stmt;
148                  SQLiteStatement stmt = db.compileStatement("INSERT INTO people (name,inits,title,dept,company,phone,cell,email) VALUES (?,?,?,?,?,?,?,?);");  
149                  while (lines.hasMoreTokens())                  DataLoader(Handler hndl) {
150                  {                            handler = hndl;
151                          String line = lines.nextToken();                  }
152    
153                    private void sendCount() {
154                            Message msg = Message.obtain();
155                            msg.what = 1;
156                            msg.arg1 = count;
157                            handler.sendMessage(msg);
158                    }              
159                    
160                    private void sendMaxCount()
161                    {
162                            Message msg = Message.obtain();
163                            msg.what = 0;
164                            msg.arg1 = maxCount;
165                            handler.sendMessage(msg);                      
166                    }
167    
168                    private void insertLine(String line)
169                    {
170                          try                          try
171                          {                          {
172                                  StringTokenizer fields = new StringTokenizer(line, ";");                                  StringTokenizer fields = new StringTokenizer(line, ";");
# Line 162  public class PeopleDatabase  { Line 182  public class PeopleDatabase  {
182                          } catch (Exception e) {                          } catch (Exception e) {
183                                  Log.e("PeopleDataBase", "Token error: " + line, e);                                  Log.e("PeopleDataBase", "Token error: " + line, e);
184                          }                          }
   
185                  }                  }
                 stmt.close();  
                 db.setTransactionSuccessful();  
                 db.endTransaction();  
                 db.close();  
186    
187          }                  public void loadData() throws Exception
188                    {
189                            try
190                            {
191                                    db = dbHelper.getWritableDatabase();
192                                    db.beginTransaction();
193                                    db.execSQL("DELETE FROM people");                      
194                                    stmt = db.compileStatement("INSERT INTO people (name,inits,title,dept,company,phone,cell,email) VALUES (?,?,?,?,?,?,?,?);");
195    
196                                    URL url = new URL("http://t-hoerup.dk/peopledata.txt");
197    
198                                    URLConnection conn = url.openConnection();
199                                    conn.setConnectTimeout(1000);                  
200                                    InputStream is = conn.getInputStream();
201    
202                                    BufferedReader input = new BufferedReader( new InputStreamReader(is), 32768);
203                                    String line;
204    
205    
206                                    sendMaxCount();
207                                    while ( (line = input.readLine()) != null) {
208                                            if (line.charAt(0) == '#') { //Lines starting with # are comments and should not be parsed by loaddata worker func.
209                                                    if (line.contains("count:")) {
210                                                            maxCount = Integer.parseInt(line.substring(line.indexOf(':')+1).trim());
211                                                            sendMaxCount();
212                                                    }
213                                                    Log.i("PeopleDB", "Count:"+maxCount);
214    
215                                            } else {
216                                                    count ++;
217                                                    if ((count % 100) == 0)
218                                                            sendCount();
219                                                    insertLine(line);
220                                            }
221                                    }                      
222                                    is.close();
223                                    db.setTransactionSuccessful();
224                            } finally {
225                                    stmt.close();
226                                    db.endTransaction();
227                                    db.close();
228                            }
229    
230          public void loadData() throws Exception                  }                      
231          {          }
                 URL url = new URL("http://t-hoerup.dk/peopledata.txt");  
232    
                 URLConnection conn = url.openConnection();  
                 conn.setConnectTimeout(1000);                    
                 InputStream is = conn.getInputStream();  
   
                 BufferedReader input = new BufferedReader( new InputStreamReader(is), 32768);  
                 StringBuilder sb = new StringBuilder(1024000);  
                 String line;  
                   
                 while ( (line = input.readLine()) != null) {  
                         sb.append(line);  
                         sb.append("\n");  
                 }  
                   
                 is.close();  
                 Log.i("PeopleDatabase", "Parsing data");  
233    
                 loadData(sb.toString());  
234    
235                  Log.i("PeopleDatabase", "Parsing finished");          public void loadData(Handler hndl) throws Exception
236            {
237                    DataLoader dl = new DataLoader(hndl);
238                    dl.loadData();
239          }          }
240            
241          public void close()          public void close()
242          {          {
243                  dbHelper.close();                  dbHelper.close();
244          }          }
245            
246    
247  }  }

Legend:
Removed from v.229  
changed lines
  Added in v.230

  ViewVC Help
Powered by ViewVC 1.1.20