Android Open Source - makler Symbols Db






From Project

Back to project page makler.

License

The source code is released under:

GNU General Public License

If you think the Android project makler listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package pl.net.newton.Makler.db.symbol;
//from  w  w w . ja  v  a2s.co  m
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import pl.net.newton.Makler.R;
import pl.net.newton.Makler.db.Constants;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import static pl.net.newton.Makler.db.Constants.ID_EQUALS;
import static pl.net.newton.Makler.db.Constants.QUOTES;
import static pl.net.newton.Makler.db.Constants.SYMBOLS;
import static pl.net.newton.Makler.db.Constants.SYMBOL;

public class SymbolsDb {
  private static final String TAG = "MaklerSymbolsDb";

  private final Context ctx;

  private final SQLiteDatabase sqlDb;

  public SymbolsDb(SQLiteDatabase sqlDb, Context ctx) {
    this.ctx = ctx;
    this.sqlDb = sqlDb;
  }

  public boolean symbolsEmpty() {
    final boolean isEmpty;
    final Cursor c = sqlDb.query(Constants.SYMBOLS, null, null, null, null, null, null);
    isEmpty = c.moveToFirst();
    c.close();
    return isEmpty;
  }

  public Integer getSymbolId(String symbol) {
    Cursor c = sqlDb.query(SYMBOLS, new String[] { "id" }, "symbols.symbol = ? COLLATE NOCASE",
        new String[] { symbol }, null, null, null);
    if (c.getCount() == 0) {
      return null;
    }
    c.moveToFirst();
    Integer symbolId = c.getInt(0);
    c.close();
    return symbolId;
  }

  public List<Symbol> getSymbols(String name) {
    List<Symbol> symbols = new ArrayList<Symbol>();
    Cursor c;
    if (StringUtils.isNoneBlank(name)) {
      String n = "%" + name + "%";
      c = sqlDb.query(SYMBOLS, null, "deleted = 0 AND (name LIKE ? OR symbol LIKE ?)", new String[] {
          n, n }, null, null, SYMBOL);
    } else {
      c = sqlDb.query(SYMBOLS, null, "deleted = 0", null, null, null, SYMBOL);
    }
    if (c.moveToFirst()) {
      do {
        symbols.add(new SymbolBuilder().setFromCursor(c).build());
      } while (c.moveToNext());
    }
    c.close();
    return symbols;
  }

  public Symbol getSymbolBySymbol(String symbol) {
    Cursor c;
    Symbol s = null;
    c = sqlDb.query(SYMBOLS, null, "deleted = 0 AND symbol = ?", new String[] { symbol }, null, null,
        null);
    if (c.moveToFirst()) {
      s = new SymbolBuilder().setFromCursor(c).build();
    }
    c.close();
    return s;
  }

  public Symbol getSymbolByName(String name) {
    Cursor c;
    Symbol s = null;
    c = sqlDb.query(SYMBOLS, null, "deleted = 0 AND name = ?", new String[] { name }, null, null, null);
    if (c.moveToFirst()) {
      s = new SymbolBuilder().setFromCursor(c).build();
    }
    c.close();
    return s;
  }

  public Symbol findSymbolByName(String name) {
    final String wildcardName = new StringBuilder("%").append(name).append("%").toString();
    Cursor c;
    Symbol s = null;
    c = sqlDb.query(SYMBOLS, null, "deleted = 0 AND name LIKE ?", new String[] { wildcardName }, null,
        null, null);
    if (c.moveToFirst()) {
      s = new SymbolBuilder().setFromCursor(c).build();
    }
    c.close();
    return s;
  }

  public void updateSymbol(Symbol s) {
    sqlDb.beginTransaction();
    Cursor c = sqlDb.query(SYMBOLS, new String[] { "id" }, "symbol = ?", new String[] { s.getSymbol() },
        null, null, null);
    if (c.getCount() == 0) {
      sqlDb.insert(SYMBOLS, null, contentValues(s));
    } else {
      c.moveToFirst();
      Integer id = c.getInt(0);
      sqlDb.update(SYMBOLS, contentValues(s), ID_EQUALS, new String[] { id.toString() });
    }
    c.close();
    sqlDb.setTransactionSuccessful();
    sqlDb.endTransaction();
  }

  public void updateSymbols(List<Symbol> symbols) {
    Log.d(TAG, "updating symbols - start");
    long start = System.currentTimeMillis();
    sqlDb.beginTransaction();
    ContentValues cv = new ContentValues();
    cv.put("deleted", 1);
    sqlDb.update(SYMBOLS, cv, null, null);
    Cursor c = sqlDb.query(SYMBOLS, new String[] { "id", SYMBOL }, null, null, null, null, null);
    Map<String, Integer> symbolToId = new HashMap<String, Integer>();
    if (c.moveToFirst()) {
      do {
        symbolToId.put(c.getString(1), c.getInt(0));
      } while (c.moveToNext());
    }
    c.close();
    for (Symbol s : symbols) {
      if (symbolToId.containsKey(s.getSymbol())) {
        sqlDb.update(SYMBOLS, contentValues(s), ID_EQUALS,
            new String[] { symbolToId.get(s.getSymbol()).toString() });
      } else {
        sqlDb.insert(SYMBOLS, null, contentValues(s));
      }
    }
    sqlDb.setTransactionSuccessful();
    sqlDb.endTransaction();

    // remove duplicates
    sqlDb.beginTransaction();
    String sql = ctx.getString(R.string.getSymbolDuplicates);
    c = sqlDb.rawQuery(sql, null);
    while (c.moveToNext()) {
      deleteSymbol(c.getInt(0), c.getInt(1));
    }
    c.close();

    // remove orphan quotes
    c = sqlDb.query(QUOTES, new String[] { "id" }, "from_wallet = 1", null, null, null, null);
    while (c.moveToNext()) {
      int quoteId = c.getInt(0);
      Cursor wallet = sqlDb.query("wallet_items", new String[] { "id" }, "quote_id = ?",
          new String[] { String.valueOf(quoteId) }, null, null, null);
      if (!wallet.moveToFirst()) {
        sqlDb.delete(QUOTES, ID_EQUALS, new String[] { String.valueOf(quoteId) });
      }
      wallet.close();
    }
    c.close();

    sqlDb.setTransactionSuccessful();
    sqlDb.endTransaction();

    Log.d(TAG, "updating symbols took " + (System.currentTimeMillis() - start) / 1000 + " seconds");
  }

  private void deleteSymbol(int oldId, int newId) {
    Log.d(TAG, "oldId: " + oldId + ", newId: " + newId);
    String[] oldIdArgs = new String[] { String.valueOf(oldId) };
    sqlDb.delete(SYMBOLS, "id = ?", oldIdArgs);
    ContentValues cv = new ContentValues();
    cv.put("symbol_id", newId);
    sqlDb.update(QUOTES, cv, "symbol_id = ?", oldIdArgs);
    sqlDb.update("wallet_items", cv, "symbol_id = ?", oldIdArgs);
  }

  public ContentValues contentValues(Symbol symbol) {
    ContentValues cv = new ContentValues();
    cv.put(SYMBOL, symbol.getSymbol());
    cv.put("name", symbol.getName());
    cv.put("code", symbol.getCode());
    cv.put("is_index", symbol.isIndex() ? 1 : 0);
    cv.put("deleted", symbol.getDeleted() ? 1 : 0);
    return cv;
  }
}




Java Source Code List

pl.net.newton.Makler.common.Configuration.java
pl.net.newton.Makler.common.DateFormatUtils.java
pl.net.newton.Makler.common.GpwUtils.java
pl.net.newton.Makler.common.LocaleUtils.java
pl.net.newton.Makler.common.NumberFormatUtils.java
pl.net.newton.Makler.db.Constants.java
pl.net.newton.Makler.db.DbHelper.java
pl.net.newton.Makler.db.SqlConnection.java
pl.net.newton.Makler.db.alert.AlertBuilder.java
pl.net.newton.Makler.db.alert.AlertChecker.java
pl.net.newton.Makler.db.alert.AlertValue.java
pl.net.newton.Makler.db.alert.Alert.java
pl.net.newton.Makler.db.alert.AlertsDao.java
pl.net.newton.Makler.db.alert.Event.java
pl.net.newton.Makler.db.alert.Subject.java
pl.net.newton.Makler.db.quote.QuoteField.java
pl.net.newton.Makler.db.quote.Quote.java
pl.net.newton.Makler.db.quote.QuotesDao.java
pl.net.newton.Makler.db.service.SqlProvider.java
pl.net.newton.Makler.db.symbol.SymbolBuilder.java
pl.net.newton.Makler.db.symbol.Symbol.java
pl.net.newton.Makler.db.symbol.SymbolsDb.java
pl.net.newton.Makler.db.wallet.WalletDb.java
pl.net.newton.Makler.db.wallet.WalletItemBuilder.java
pl.net.newton.Makler.db.wallet.WalletItem.java
pl.net.newton.Makler.gpw.DefaultQuotesReceiver.java
pl.net.newton.Makler.gpw.QuotesReceiver.java
pl.net.newton.Makler.gpw.ex.GpwException.java
pl.net.newton.Makler.gpw.service.QuotesListener.java
pl.net.newton.Makler.gpw.service.QuotesService.java
pl.net.newton.Makler.gpw.service.UpdatingThread.java
pl.net.newton.Makler.history.BossaProvider.java
pl.net.newton.Makler.history.ByteArrayUtils.java
pl.net.newton.Makler.history.Cache.java
pl.net.newton.Makler.history.ChannelTools.java
pl.net.newton.Makler.history.EntryListWithIndexes.java
pl.net.newton.Makler.history.EntryList.java
pl.net.newton.Makler.history.HistoryFilter.java
pl.net.newton.Makler.history.HistoryProvider.java
pl.net.newton.Makler.history.service.HistoryListener.java
pl.net.newton.Makler.history.service.HistoryService.java
pl.net.newton.Makler.httpClient.Connector.java
pl.net.newton.Makler.receivers.QuotesAlarmReceiver.java
pl.net.newton.Makler.receivers.StartupReceiver.java
pl.net.newton.Makler.service.ServiceManager.java
pl.net.newton.Makler.ui.About.java
pl.net.newton.Makler.ui.AbstractActivity.java
pl.net.newton.Makler.ui.Alerts.java
pl.net.newton.Makler.ui.FullScreenGraph.java
pl.net.newton.Makler.ui.Preferences.java
pl.net.newton.Makler.ui.QuoteDetails.java
pl.net.newton.Makler.ui.Quotes.java
pl.net.newton.Makler.ui.Symbols.java
pl.net.newton.Makler.ui.WalletForm.java
pl.net.newton.Makler.ui.WalletItemCalculator.java
pl.net.newton.Makler.ui.Wallet.java
pl.net.newton.Makler.ui.adapter.AlertsAdapter.java
pl.net.newton.Makler.ui.adapter.QuotesAdapter.java
pl.net.newton.Makler.ui.adapter.SymbolsAdapter.java
pl.net.newton.Makler.ui.adapter.WalletAdapter.java
pl.net.newton.Makler.ui.graph.GraphView.java
pl.net.newton.Makler.ui.graph.MaklerGraphicalView.java