@ServiceAction void fetchAndSaveData(final long tvShowId) { final List<Episode> tvShowEpisodes = connection.getTvShowEpisodes(tvShowId); final List<Cast> tvShowCast = connection.getTvShowCast(tvShowId); try { TransactionManager.callInTransaction( daoEpisodes.getConnectionSource(), new Callable<Object>() { @Override public Object call() throws Exception { TvShow tvShow = daoTvShow.queryForId(tvShowId); for (Episode episode : tvShowEpisodes) { episode.setTvShow(tvShow); daoEpisodes.createOrUpdate(episode); } for (Cast cast : tvShowCast) { Person person = cast.getPerson(); daoPersona.createOrUpdate(person.getCharacter()); daoPerson.createOrUpdate(person); PersonTvShow many2many = new PersonTvShow(person, tvShow); daoPersonTvShow.createOrUpdate(many2many); } return null; } }); } catch (SQLException e) { e.printStackTrace(); } Intent intent = new Intent(ACTION_SAVE_DONE); sendBroadcast(intent); prefs.lastUpdate().put(System.currentTimeMillis()); }
public <T, ID> void save(List<T> list, Class<T> clazz) throws SQLException { openConnectionSource(); createTableIfNotExists(clazz); Callable<Void> call = createSaveListCallable(list, clazz); TransactionManager.callInTransaction(connectionSource, call); closeConnection(); }
public synchronized void insert(final List<City> list) { try { TransactionManager.callInTransaction( DatabaseHelper.getInstance(mContext).getConnectionSource(), new Callable<Void>() { @Override public Void call() throws Exception { for (City city : list) { mDao.createOrUpdate(city); } return null; } }); } catch (SQLException e) { e.printStackTrace(); } }
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); } }