/** * Copyright © 2013 Titan Framework. All Rights Reserved. * * Developed by Laboratory for Precision Livestock, Environment and Software Engineering (PLEASE Lab) * of Embrapa Beef Cattle at Campo Grande - MS - Brazil. * * @see http://please.cnpgc.embrapa.br * * @author Camilo Carromeu * @author Jairo Ricardes Rodrigues Filho * * @version -1-alpha */ package .dao; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.List; import android.content.ContentValues; import android.content.SharedPreferences; import android.content.res.AssetManager; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import .; import .contract.Contract; import .converter.Converter; import .exception.TechnicalException; import .model.; import .util.Database; import .util.Preferences; public class DAO { private static DAO dao; private SQLiteDatabase db; private DAO () { db = Database.singleton ().getWritableDatabase (); } public static DAO singleton () { if (dao == null) dao = new DAO (); return dao; } public String next () { Cursor cursor = db.query (Contract.TABLE, new String [] { Contract.json) ?> }, Contract.json) ?> + " LIKE ?", new String [] { .singleton ().getDisambiguation () + ".%" }, null, null, Contract.json) ?> + " DESC", "1"); if (cursor.getCount () == 0) return .singleton ().getDisambiguation () + ".1"; cursor.moveToFirst (); String code = cursor.getString (cursor.getColumnIndexOrThrow (Contract.json) ?>)); int next = Integer.valueOf (code.replaceFirst ("[0-9]+\\.", "")); return .singleton ().getDisambiguation () + "." + String.valueOf (next + 1); } public int load (AssetManager assets) { int lines = 0; try { SharedPreferences preferences = Preferences.singleton (); if (preferences.getLong ("lastSyncFor", 0) > 0) return 0; if (this.empty ()) { InputStream is = assets.open (Contract.TABLE + ".sql"); BufferedReader br = new BufferedReader (new InputStreamReader (is)); String line; while ((line = br.readLine ()) != null) { Log.i (getClass ().getName (), line); db.execSQL (line); lines++; } br.close (); is.close (); } Cursor cursor = db.query (Contract.TABLE, new String [] { Contract.json) ?> }, null, null, null, null, Contract.json) ?> + " DESC", "1"); cursor.moveToNext (); SharedPreferences.Editor editor = preferences.edit (); editor.putLong ("lastSyncFor", cursor.getLong (cursor.getColumnIndexOrThrow (Contract.json) ?>))); editor.commit (); cursor.close (); } catch (Exception e) { throw new TechnicalException ("Impossível carregar os dados iniciais!", e); } return lines; } public List<> list () { Cursor cursor = db.query (Contract.TABLE, Contract.columns (), null, null, null, null, null); List<> list = new LinkedList<> (); while (cursor.moveToNext ()) list.add (Converter.from (cursor)); cursor.close (); return list; } public List<> changed (long time) { Cursor cursor = db.query (Contract.TABLE, Contract.columns (), Contract.json) ?> + " > ?", new String [] { String.valueOf (time) }, null, null, null); List<> list = new LinkedList<> (); while (cursor.moveToNext ()) list.add (Converter.from (cursor)); cursor.close (); return list; } public get () { Cursor cursor = db.query (Contract.TABLE, Contract.columns (), Contract.json) ?> + " = ?", new String [] { }, null, null, null, "1"); cursor.moveToNext (); p = Converter.from (cursor); cursor.close (); return p; } public void truncate () { db.delete (Contract.TABLE, null, null); } public void insert (List<> list) { for ( item : list) insert (item); } public void insert ( item) { ContentValues v = Converter.toContentValue (item); try { db.insertOrThrow (Contract.TABLE, null, v); } catch (SQLException e) { throw new TechnicalException ("Impossível gravar os dados!", e); } } public void update (List<> list) { for ( item : list) update (item); } public void update ( item) { ContentValues v = Converter.toContentValue (item); try { db.update (Contract.TABLE, v, Contract.json) ?> + " = ?", new String [] { String.valueOf (item.getclass) ?> ()) }); } catch (SQLException e) { throw new TechnicalException ("Impossível atualizar os dados!", e); } } public void insertOrUpdate (List<> list) { for ( item : list) insertOrUpdate (item); } public void insertOrUpdate ( item) { ContentValues v = Converter.toContentValue (item); try { db.insertOrThrow (Contract.TABLE, null, v); } catch (SQLException e) { db.update (Contract.TABLE, v, Contract.json) ?> + " = ?", new String [] { String.valueOf (item.getclass) ?> ()) }); } } public void delete () { db.delete (Contract.TABLE, Contract.json) ?> + " = ?", new String [] { }); } public void delete ( item) { db.delete (Contract.TABLE, Contract.json) ?> + " = ?", new String [] { String.valueOf (item.getclass) ?> ()) }); } public void deleteNonActive (String active) { db.delete (Contract.TABLE, Contract.json) ?> + " NOT IN (" + active.replaceAll ("", "") + ")", null); } public boolean empty () { Cursor cursor = db.query (Contract.TABLE, new String [] { Contract.json) ?> }, null, null, null, null, null); return cursor.getCount () == 0; } }