Android Open Source - makler Alerts Dao






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.alert;
/*from w w  w. ja v  a 2 s. co  m*/
import java.util.ArrayList;
import java.util.List;

import pl.net.newton.Makler.db.quote.Quote;
import pl.net.newton.Makler.db.quote.QuoteField;
import pl.net.newton.Makler.db.quote.QuotesDao;
import pl.net.newton.Makler.gpw.ex.GpwException;
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.ALERTS;
import static pl.net.newton.Makler.db.Constants.ID_EQUALS;
import static pl.net.newton.Makler.db.DbHelper._;

public class AlertsDao {
  private static final String CAN_T_GET_ALERT = "Can't get alert";

  private static final String TAG = "MaklerAlertsDb";

  private final SQLiteDatabase sqlDb;

  private final QuotesDao quotesDb;

  public AlertsDao(SQLiteDatabase sqlDb, Context ctx) {
    this.sqlDb = sqlDb;
    this.quotesDb = new QuotesDao(sqlDb, ctx);
  }

  public boolean create(Alert a) {
    ContentValues cv = getContentValues(a);
    if (cv == null) {
      return false;
    } else {
      sqlDb.beginTransaction();
      sqlDb.insert(ALERTS, null, cv);
      sqlDb.setTransactionSuccessful();
      sqlDb.endTransaction();
      return true;
    }
  }

  public List<Alert> getAll() {
    List<Alert> alerts = new ArrayList<Alert>();
    Cursor c = sqlDb.query(ALERTS, null, null, null, null, null, null);
    if (c.moveToFirst()) {
      do {
        try {
          alerts.add(new AlertBuilder().setFromCursor(c, quotesDb).build());
        } catch (GpwException e) {
          Log.e(TAG, CAN_T_GET_ALERT, e);
        }
      } while (c.moveToNext());
    }
    c.close();
    return alerts;
  }

  public List<Alert> getByQuote(Quote quote) {
    List<Alert> alerts = new ArrayList<Alert>();
    Cursor c = sqlDb.query(ALERTS, null, "quote_id = ?", _(quote.get(QuoteField.ID)), null, null, null);
    if (c.moveToFirst()) {
      do {
        try {
          alerts.add(new AlertBuilder().setFromCursor(c, quotesDb).build());
        } catch (GpwException e) {
          Log.e(TAG, CAN_T_GET_ALERT, e);
        }
      } while (c.moveToNext());
    }
    c.close();
    return alerts;
  }

  public Alert getById(int id) {
    Cursor c = sqlDb.query(ALERTS, null, ID_EQUALS, _(String.valueOf(id)), null, null, null);
    Alert alert = null;
    if (c.moveToFirst()) {
      try {
        alert = new AlertBuilder().setFromCursor(c, quotesDb).build();
      } catch (GpwException e) {
        Log.e(TAG, CAN_T_GET_ALERT, e);
      }
    }
    c.close();
    return alert;
  }

  public void update(Alert a) {
    ContentValues cv = getContentValues(a);
    if (cv != null) {
      sqlDb.beginTransaction();
      sqlDb.update(ALERTS, cv, ID_EQUALS, new String[] { String.valueOf(a.getId()) });
      sqlDb.setTransactionSuccessful();
      sqlDb.endTransaction();
    }
  }

  public void markAsUsed(Alert alert) {
    sqlDb.beginTransaction();
    ContentValues cv = new ContentValues();
    cv.put("used", 1);
    sqlDb.update(ALERTS, cv, "id = ?", _(String.valueOf(alert.getId())));
    sqlDb.setTransactionSuccessful();
    sqlDb.endTransaction();
  }

  public void delete(int id) {
    sqlDb.beginTransaction();
    sqlDb.delete(ALERTS, ID_EQUALS, _(String.valueOf(id)));
    sqlDb.setTransactionSuccessful();
    sqlDb.endTransaction();
  }

  private ContentValues getContentValues(Alert a) {
    ContentValues cv = new ContentValues();
    cv.put("quote_id", a.getQuote().get(QuoteField.ID));
    cv.put("subject", a.getSubject().toString());
    cv.put("event", a.getEvent().toString());
    cv.put("value", a.getAlertValue().getValue().toString());
    cv.put("percent", a.getAlertValue().isPercent() ? 1 : 0);
    if (a.getAlertValue().getBaseValue() != null) {
      cv.put("base_value", a.getAlertValue().getBaseValue().toString());
    } else if (!a.getEvent().isBaseValueRequired()) {
      cv.put("base_value", "0");
    } else {
      return null;
    }
    cv.put("used", a.getUsed() ? 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