public boolean onContextItemSelected(MenuItem item) {

    AdapterView.AdapterContextMenuInfo info =
        (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    int menuItemIndex = item.getItemId();

    Interes interes = interesesAdapter.getItem(info.position);

    switch (menuItemIndex) {
      case R.id.ItemMenuBorrar:
        OrmLiteSqliteOpenHelper helper = null;
        try {
          helper = OpenHelperManager.getHelper(this, DatabaseHelper.class);
          Dao<Interes, Integer> interesesDao = helper.getDao(Interes.class);
          interesesDao.delete(interes);
          interesesAdapter.remove(interes);
        } catch (Exception e) {
          Log.e(TAG, "Error al listar los intereses", e);
          GuiUtils.mostrarToast(this, R.string.operacion_error, null);
        } finally {
          try {
            OpenHelperManager.releaseHelper();
          } catch (Exception e2) {
          }
        }

        return true;
      case R.id.ItemMenuEditar:
        mostrarDatosInteres(interes);
        return true;
      default:
        return super.onContextItemSelected(item);
    }
  }
 public void insertNoteLite(NoteLite noteLite) {
   try {
     OrmLiteSqliteOpenHelper dbHelper = NoteLiteRepo.getInstance(_context);
     Dao<NoteLite, Integer> daoNoteLite = dbHelper.getDao(NoteLite.class);
     daoNoteLite.create(noteLite);
     dbHelper.close();
   } catch (SQLException e) {
     e.printStackTrace();
   }
 }
 private void cargarListadoIntereses() {
   OrmLiteSqliteOpenHelper helper = null;
   try {
     helper = OpenHelperManager.getHelper(this, DatabaseHelper.class);
     Dao<Interes, Integer> interesDao = helper.getDao(Interes.class);
     List<Interes> intereses = interesDao.queryForAll();
     interesesAdapter =
         new ArrayAdapter<Interes>(this, android.R.layout.simple_list_item_1, intereses);
     listViewIntereses.setAdapter(interesesAdapter);
   } catch (Exception e) {
     Log.e(TAG, "Error al cargar los clientes", e);
     GuiUtils.mostrarToast(this, R.string.operacion_error, null);
   } finally {
     try {
       OpenHelperManager.releaseHelper();
     } catch (Exception e2) {
       Log.e(TAG, "Error en el release", e2);
     }
   }
 }
Пример #4
0
  public <E extends SyncEntity> void synObjects(
      final Class<E> entityClass, boolean userAware, final SyncCallback<E> callback)
      throws SyncException {
    try {
      final String parseClass = extractParseClass(entityClass);

      // get updated remote objects
      ParseQuery query = ParseQuery.getQuery(parseClass);
      query.whereGreaterThan(PARSE_UPDATED_AT_FIELD, lastSyncDate);
      if (userAware && userId != null) query.whereEqualTo(PARSE_USER_ID_FIELD, userId);
      // query.orderByAscending(PARSE_OBJECT_ID_FIELD);
      final List<ParseObject> remoteObjects = ParseTools.findAllParseObjects(query);

      // get updated local objects
      final SyncDAO<E, ?> syncDao = dbHelper.getDao(entityClass);
      QueryBuilder<E, ?> dbQuery = syncDao.queryBuilder();
      // dbQuery.orderBy(DB_OBJECT_ID_FIELD, true);
      Where<E, ?> where = dbQuery.where().gt(DB_UPDATED_AT_FIELD, lastSyncDate);
      if (userAware && userId != null) where.and().eq(DB_USER_ID_FIELD, userId);
      final List<E> localObjects = where.query();

      // create local object map
      Map<String, E> localObjectMap = new HashMap<String, E>(localObjects.size());
      for (E localObject : localObjects) {
        localObjectMap.put(localObject.getSyncId(), localObject);
      }

      List<Pair<E, ParseObject>> toSaveLocally = new ArrayList<>();
      List<Pair<E, ParseObject>> toSaveRemotely = new ArrayList<>();

      for (ParseObject remoteObject : remoteObjects) {
        String syncId = remoteObject.getObjectId();
        E localObject = localObjectMap.get(syncId);

        if (localObject == null) {
          localObject = findBySyncId(syncDao, syncId);
          if (localObject == null) {
            // this object was created on the server but doesn't exist locally
            localObject = SyncEntity.fromParseObject(remoteObject, entityClass);
          } else {
            // the object exists locally but out-of-date
            SyncEntity.fromParseObject(remoteObject, localObject);
          }
          toSaveLocally.add(new Pair<>(localObject, remoteObject));
          continue;
        }

        if (localObject != null) {
          long localTime =
              (localObject.getSyncDate() == null) ? 0L : localObject.getSyncDate().getTime();
          long remoteTime =
              (remoteObject.getUpdatedAt() == null) ? 0L : remoteObject.getUpdatedAt().getTime();

          if (remoteTime > localTime) {
            // the remote object is newer
            SyncEntity.fromParseObject(remoteObject, localObject);
            toSaveLocally.add(new Pair<>(localObject, remoteObject));
          } else if (remoteTime < localTime) {
            // the local objects is newer
            SyncEntity.toParseObject(localObject, remoteObject);
            toSaveRemotely.add(new Pair<>(localObject, remoteObject));
          }
        }
      }
      localObjectMap = null;

      // create remote object map
      Map<String, ParseObject> remoteObjectMap =
          new HashMap<String, ParseObject>(remoteObjects.size());
      for (ParseObject remoteObject : remoteObjects) {
        remoteObjectMap.put(remoteObject.getObjectId(), remoteObject);
      }

      for (E localObject : localObjects) {
        String syncId = localObject.getSyncId();
        if (syncId == null) {
          // a brand new object!
          ParseObject remoteObject = SyncEntity.toParseObject(localObject);
          toSaveRemotely.add(new Pair<>(localObject, remoteObject));
          continue;
        }

        ParseObject remoteObject = remoteObjectMap.get(syncId);
        if (remoteObject == null && !localObject.isDeleted()) {
          // object was created locally but doesn't exist or too old on the server
          // this is weird because syncId is not null

          // try to get it from server
          remoteObject = ParseObject.createWithoutData(parseClass, syncId).fetch();
          toSaveRemotely.add(new Pair<>(localObject, remoteObject));
          continue;
        }

        if (remoteObject != null) {
          long localTime =
              (localObject.getSyncDate() == null) ? 0L : localObject.getSyncDate().getTime();
          long remoteTime =
              (remoteObject.getUpdatedAt() == null) ? 0L : remoteObject.getUpdatedAt().getTime();

          if (remoteTime > localTime) {
            // the remote object is newer
            SyncEntity.fromParseObject(remoteObject, localObject);
            toSaveLocally.add(new Pair<>(localObject, remoteObject));
          } else if (remoteTime < localTime) {
            // the local objects is newer
            SyncEntity.toParseObject(localObject, remoteObject);
            toSaveRemotely.add(new Pair<>(localObject, remoteObject));
          }
        }
      }

      if (callback != null) {
        callback.beforeSync(toSaveLocally, toSaveRemotely);
      }

      for (Pair<E, ParseObject> p : toSaveLocally) {
        final E localObject = p.first;
        final ParseObject remoteObject = p.second;
        TransactionManager.callInTransaction(
            dbHelper.getConnectionSource(),
            new Callable<Object>() {
              @Override
              public Object call() throws Exception {
                if (callback != null) {
                  callback.onSaveLocally(localObject, remoteObject);
                }
                syncDao.createOrUpdate(localObject);
                return null;
              }
            });
      }

      for (Pair<E, ParseObject> p : toSaveRemotely) {
        final E localObject = p.first;
        final ParseObject remoteObject = p.second;
        TransactionManager.callInTransaction(
            dbHelper.getConnectionSource(),
            new Callable<Object>() {
              @Override
              public Object call() throws Exception {
                if (callback != null) {
                  callback.onSaveRemotely(localObject, remoteObject);
                }
                remoteObject.save();
                if (localObject.getSyncId() == null) {
                  SyncEntity.fromParseObject(remoteObject, localObject);
                  syncDao.createOrUpdate(localObject);
                }
                return null;
              }
            });
      }

      if (callback != null) {
        callback.afterSync();
      }
    } catch (Exception ex) {
      throw new SyncException("Synchronization failed", ex);
    }
  }