PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/v3.2/nimbits-android/src/com/nimbits/android/dao/LocalDatabaseDaoImpl.java

http://nimbits-server.googlecode.com/
Java | 142 lines | 101 code | 21 blank | 20 comment | 1 complexity | cc5d11ae26ff44f23ba1b846c13b9815 MD5 | raw file
  1/*
  2 * Copyright (c) 2010 Tonic Solutions LLC.
  3 *
  4 * http://www.nimbits.com
  5 *
  6 *
  7 * Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  8 *
  9 * http://www.gnu.org/licenses/gpl.html
 10 *
 11 * Unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 12 */
 13
 14package com.nimbits.android.dao;
 15
 16import android.content.ContentValues;
 17import android.content.Context;
 18import android.database.Cursor;
 19import android.database.sqlite.SQLiteDatabase;
 20import android.widget.ListAdapter;
 21import com.nimbits.android.ImageCursorAdapter;
 22import com.nimbits.android.R;
 23import com.nimbits.android.database.DatabaseHelperFactory;
 24import com.nimbits.client.model.Const;
 25import com.nimbits.client.model.point.PointName;
 26
 27import java.util.ArrayList;
 28import java.util.List;
 29
 30/**
 31 * Created by bsautner
 32 * User: benjamin
 33 * Date: 7/6/11
 34 * Time: 4:50 PM
 35 */
 36public class LocalDatabaseDaoImpl implements LocalDatabaseDao {
 37    @Override
 38    public void insertPoints(final Context aContext,final  ContentValues values) {
 39        final SQLiteDatabase db1 = DatabaseHelperFactory.getInstance(aContext).getDB(false);
 40        db1.insert(Const.ANDROID_TABLE_LEVEL_TWO_DISPLAY, null,values);
 41        db1.close();
 42    }
 43    @Override
 44    public String getSetting(final Context aContext,final String settingName) {
 45        final SQLiteDatabase db1 = DatabaseHelperFactory.getInstance(aContext).getDB(false);
 46        final Cursor c = db1.query(Const.ANDROID_TABLE_SETTINGS, new String[] {Const.ANDROID_COL_ID, Const.ANDROID_COL_VALUE}, Const.ANDROID_COL_NAME + "=?" , new String[] {settingName}, null, null, null);
 47        c.moveToFirst();
 48        final String retVal = c.getString(c.getColumnIndex(Const.ANDROID_COL_VALUE));
 49        c.close();
 50        db1.close();
 51        return retVal;
 52    }
 53
 54    public List<String> getServers(final Context aContext) {
 55        final List<String> serverList = new ArrayList<String>();
 56        final SQLiteDatabase db1 = DatabaseHelperFactory.getInstance(aContext).getDB(false);
 57        final Cursor c = db1.query(Const.ANDROID_TABLE_SERVERS, new String[] {Const.ANDROID_COL_ID,Const.ANDROID_COL_URL},  null , null, null, null, null);
 58        c.moveToFirst();
 59        //l.add("New Connection");
 60
 61        while (! c.isAfterLast())
 62        {
 63            final String url = c.getString(c.getColumnIndex(Const.ANDROID_COL_URL));
 64            serverList.add(url);
 65            c.moveToNext();
 66        }
 67        c.close();
 68        db1.close();
 69        return serverList;
 70    }
 71
 72    @Override
 73    public void insertMain(final Context aContext,final  ContentValues values) {
 74        final SQLiteDatabase db1 = DatabaseHelperFactory.getInstance(aContext).getDB(false);
 75        db1.insert(Const.ANDROID_TABLE_LEVEL_ONE_DISPLAY, null,values);
 76        db1.close();
 77    }
 78    @Override
 79    public ListAdapter mainListCursor(final Context aContext) {
 80        final SQLiteDatabase db1 = DatabaseHelperFactory.getInstance(aContext).getDB(false);
 81        final Cursor listCursor = db1.query(Const.ANDROID_TABLE_LEVEL_ONE_DISPLAY, new String[] {Const.ANDROID_COL_ID, Const.ANDROID_COL_NAME, Const.ANDROID_COL_DESCRIPTION, Const.ANDROID_COL_DISPLAY_TYPE}, Const.ANDROID_COL_NAME + " != ?",  new String[] {Const.CONST_HIDDEN_CATEGORY}, null, null, Const.ANDROID_COL_DISPLAY_TYPE);
 82     //   int r = listCursor.getCount();
 83
 84
 85        return new ImageCursorAdapter(
 86                aContext, // Context.
 87                R.layout.main_list,  // Specify the row template to use (here, two columns bound to the two retrieved cursor
 88                listCursor,                                              // Pass in the cursor to bind to.
 89                new String[] {Const.ANDROID_COL_NAME,Const.ANDROID_COL_DESCRIPTION},           // Array of cursor columns to bind to.
 90                new int[] {R.id.text1, R.id.text2});
 91    }
 92
 93    @Override
 94    public void updatePointValuesByName(Context aContext,final  ContentValues u,final  PointName pointName) {
 95        final  SQLiteDatabase db1 = DatabaseHelperFactory.getInstance(aContext).getDB(false);
 96        db1.update(Const.ANDROID_TABLE_LEVEL_TWO_DISPLAY, u, Const.ANDROID_COL_NAME + "=?", new String[] {pointName.getValue()});
 97        db1.close();
 98
 99    }
100
101    public void updateSetting(final Context aContext,final  String settingName,final  String  newValue) {
102        final SQLiteDatabase db1 = DatabaseHelperFactory.getInstance(aContext).getDB(false);
103        ContentValues u = new ContentValues();
104        u.put(Const.PARAM_VALUE, newValue);
105        db1.update(Const.ANDROID_TABLE_SETTINGS, u, Const.ANDROID_COL_NAME + "=?", new String[] {settingName});
106        db1.close();
107    }
108    public void addServer(Context aContext, String url) {
109
110        ContentValues u = new ContentValues();
111        u.put(Const.ANDROID_COL_URL,url);
112        SQLiteDatabase db1;
113        db1 = DatabaseHelperFactory.getInstance(aContext).getDB(true);
114        db1.insert(Const.ANDROID_TABLE_SERVERS, null, u);
115        db1.close();
116    }
117
118    public String getSelectedChildTableJsonByName(Context aContext, String name) {
119        Cursor c;
120        SQLiteDatabase db1=  DatabaseHelperFactory.getInstance(aContext).getDB(false);
121        c = db1.query(Const.ANDROID_TABLE_LEVEL_TWO_DISPLAY, new String[] {Const.ANDROID_COL_ID, Const.ANDROID_COL_JSON},  Const.ANDROID_COL_NAME + "='" + name +"'" , null, null, null, null);
122        c.moveToFirst();
123        String retVal = c.getString(c.getColumnIndex(Const.ANDROID_COL_JSON));
124        c.close();
125        db1.close();
126        return retVal;
127
128
129
130
131    }
132
133    @Override
134    public void deleteAll(Context aContext) {
135        SQLiteDatabase db1=  DatabaseHelperFactory.getInstance(aContext).getDB(true);
136        db1.execSQL("delete from " + Const.ANDROID_TABLE_LEVEL_ONE_DISPLAY);
137        db1.execSQL("delete from " + Const.ANDROID_TABLE_LEVEL_TWO_DISPLAY);
138        db1.close();
139    }
140
141
142}