public void delete(final Dataset dataset, final Field field, final String data) {
    try {
      DeleteBuilder<Data, Integer> deleteBuilder = getDataDao().deleteBuilder();
      Where<Data, Integer> where = deleteBuilder.where();
      where.eq("dataset_id", dataset);
      where.and();
      where.eq("field_id", field);
      where.and();
      where.eq("data", data);
      deleteBuilder.delete();

    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
 public Data getData(final Dataset dataset, final Field field, final String data) {
   try {
     QueryBuilder<Data, Integer> queryBuilder = getDataDao().queryBuilder();
     Where<Data, Integer> where = queryBuilder.where();
     where.eq("dataset_id", dataset);
     where.and();
     where.eq("field_id", field);
     where.and();
     where.eq("data", data);
     PreparedQuery<Data> preparedQuery = queryBuilder.prepare();
     return getDataDao().queryForFirst(preparedQuery);
   } catch (SQLException e) {
     e.printStackTrace();
   }
   return null;
 }
 private List<KnouNoticeInfo> getListKnouNoticeInfo(String blngDc, String blngCd, String anncNo) {
   List<KnouNoticeInfo> knouNoticeInfoList = null;
   try {
     Dao<KnouNoticeInfo, Long> knouNoticeInfoDao = this.getHelper().getKnouNoticeInfoDao();
     QueryBuilder<KnouNoticeInfo, Long> qb = knouNoticeInfoDao.queryBuilder();
     Where where = qb.where();
     where.eq(KNOU_NOTICE.blngDc, blngDc);
     where.and();
     where.eq(KNOU_NOTICE.blngCd, blngCd);
     where.and();
     where.eq(KNOU_NOTICE.anncNo, anncNo);
     System.out.println("sql:" + qb.prepareStatementString());
     knouNoticeInfoList = qb.query();
   } catch (SQLException e) {
     e.printStackTrace();
   }
   return knouNoticeInfoList;
 }
  public void update(final Dataset dataset, final Field field, final String data) {
    try {
      UpdateBuilder<Data, Integer> updateBuilder = getDataDao().updateBuilder();
      Where<Data, Integer> where = updateBuilder.where();
      where.eq("dataset_id", dataset);
      where.and();
      where.eq("field_id", field);

      updateBuilder.updateColumnValue("data" /* column */, data /* value */);
      updateBuilder.update();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
 public List<Data> getAllData(final int datasetId, final int fieldId) {
   try {
     QueryBuilder<Data, Integer> queryBuilder = getDataDao().queryBuilder();
     Where<Data, Integer> where = queryBuilder.where();
     where.eq("dataset_id", datasetId);
     where.and();
     where.eq("field_id", fieldId);
     PreparedQuery<Data> preparedQuery = queryBuilder.prepare();
     return getDataDao().query(preparedQuery);
   } catch (SQLException e) {
     e.printStackTrace();
   }
   return null;
 }
 @SuppressWarnings("unchecked")
 public int deleteMarkStarredAndUnStarred(Article article) {
   try {
     DeleteBuilder<ArticleSyncAction, Integer> deleteBuilder = deleteBuilder();
     Where<ArticleSyncAction, Integer> where = deleteBuilder.where();
     where.and(
         where.eq(COLUMN_ARTICLEID, article.getId()),
         where
             .eq(COLUMN_ACTION, ArticleSyncAction.Action.MarkStarred)
             .or()
             .eq(COLUMN_ACTION, ArticleSyncAction.Action.MarkUnstarred));
     return deleteBuilder.delete();
   } catch (SQLException e) {
     throw new RuntimeException(e);
   }
 }
예제 #7
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);
    }
  }