Exemplo n.º 1
0
 @Override
 public void onPostSyncItem(
     Context context, Account account, Uri uri, JSONObject item, boolean updated)
     throws SyncException, IOException {
   super.onPostSyncItem(context, account, uri, item, updated);
   for (final SyncItem child : children) {
     child.onPostSyncItem(context, account, uri, item, updated);
   }
 }
  /**
   * Auto generated method comment
   *
   * @throws Exception
   */
  @Test
  @NotTransactional
  @SkipBaseSetup
  public void shouldEquality() throws Exception {

    // setup instance 1
    String uuid1 = UUID.randomUUID().toString();
    SyncRecord syncRecord1 = new SyncRecord();
    syncRecord1.setTimestamp(new Date());
    syncRecord1.setUuid(uuid1);
    SyncItem item11 = new SyncItem();
    item11.setContent("<Person><Name>Some Person</Name></Person>");
    item11.setState(SyncItemState.NEW);
    item11.setKey(new SyncItemKey<String>(UUID.randomUUID().toString(), String.class));
    List<SyncItem> items1 = new ArrayList<SyncItem>();
    items1.add(item11);
    syncRecord1.setItems(items1);

    // setup instance 2
    SyncRecord syncRecord2 = new SyncRecord();
    syncRecord2.setTimestamp(syncRecord1.getTimestamp());
    syncRecord2.setUuid(syncRecord1.getUuid());
    SyncItem item21 = new SyncItem();
    item21.setContent("<Person><Name>Some Person</Name></Person>");
    item21.setState(SyncItemState.NEW);
    item21.setKey(item11.getKey());
    List<SyncItem> items2 = new ArrayList<SyncItem>();
    items2.add(item21);
    syncRecord2.setItems(items2);

    // assert now
    assertEquals(syncRecord1, syncRecord2);

    syncRecord1.setItems(null);
    assertNotSame(syncRecord1, syncRecord2);

    // some variations
    Date date2 = new Date();
    date2.setTime(syncRecord1.getTimestamp().getTime() + 1);
    syncRecord2.setTimestamp(date2);
    assertTrue(!syncRecord1.equals(syncRecord2));

    date2.setTime(syncRecord1.getTimestamp().getTime());
    assertTrue(syncRecord1.equals(syncRecord2));

    item21.setContent("<Person><Name>Some Person Name</Name></Person>");
    assertTrue(!syncRecord1.equals(syncRecord2));
  }
Exemplo n.º 3
0
 @Override
 public void onPostSyncItem(
     Context context, Account account, Uri uri, JSONObject item, boolean updated)
     throws SyncException, IOException {
   super.onPostSyncItem(context, account, uri, item, updated);
   chain.onPostSyncItem(context, account, uri, item, updated);
 }
Exemplo n.º 4
0
  /**
   * Given a JSON item and a sync map, create a ContentValues map to be inserted into the DB.
   *
   * @param context
   * @param localItem will be null if item is new to mobile. If it's been sync'd before, will point
   *     to local entry.
   * @param item incoming JSON item.
   * @param mySyncMap A mapping between the JSON object and the content values.
   * @return new ContentValues, ready to be inserted into the database.
   * @throws JSONException
   * @throws IOException
   * @throws NetworkProtocolException
   */
  public static final ContentValues fromJSON(
      Context context, Uri localItem, JSONObject item, SyncMap mySyncMap)
      throws JSONException, IOException, NetworkProtocolException {
    final ContentValues cv = new ContentValues();

    for (final String propName : mySyncMap.keySet()) {
      final SyncItem map = mySyncMap.get(propName);
      if (!map.isDirection(SyncItem.SYNC_FROM)) {
        continue;
      }
      if (map.isOptional() && (!item.has(map.remoteKey) || item.isNull(map.remoteKey))) {
        continue;
      }
      final ContentValues cv2 = map.fromJSON(context, localItem, item, propName);
      if (cv2 != null) {
        cv.putAll(cv2);
      }
    }
    return cv;
  }
Exemplo n.º 5
0
  /**
   * @param context
   * @param localItem Will contain the URI of the local item being referenced in the cursor
   * @param c active cursor with the item to sync selected.
   * @param mySyncMap
   * @return a new JSONObject representing the item
   * @throws JSONException
   * @throws NetworkProtocolException
   * @throws IOException
   */
  public static final JSONObject toJSON(Context context, Uri localItem, Cursor c, SyncMap mySyncMap)
      throws JSONException, NetworkProtocolException, IOException {
    final JSONObject jo = new JSONObject();

    for (final String lProp : mySyncMap.keySet()) {
      final SyncItem map = mySyncMap.get(lProp);

      if (!map.isDirection(SyncItem.SYNC_TO)) {
        continue;
      }

      final int colIndex = c.getColumnIndex(lProp);
      // if it's a real property that's optional and is null on the local side
      if (!lProp.startsWith("_") && map.isOptional()) {
        if (colIndex == -1) {
          throw new RuntimeException(
              "Programming error: Cursor does not have column '"
                  + lProp
                  + "', though sync map says it should. Sync Map: "
                  + mySyncMap);
        }
        if (c.isNull(colIndex)) {
          continue;
        }
      }

      final Object jsonObject = map.toJSON(context, localItem, c, lProp);
      if (jsonObject instanceof MultipleJsonObjectKeys) {
        for (final Entry<String, Object> entry : ((MultipleJsonObjectKeys) jsonObject).entrySet()) {
          jo.put(entry.getKey(), entry.getValue());
        }

      } else {
        jo.put(map.remoteKey, jsonObject);
      }
    }
    return jo;
  }
  /**
   * test serialization of syncRecord
   *
   * @throws Exception
   */
  @Test
  @NotTransactional
  @SkipBaseSetup
  public void shouldSerializeASyncRecord() throws Exception {

    // 'normal' state
    String uuid1 = UUID.randomUUID().toString();
    SyncRecord syncRecord1 = new SyncRecord();
    syncRecord1.setTimestamp(new Date());
    syncRecord1.setUuid(uuid1);
    SyncItem item11 = new SyncItem();
    item11.setContent("<Person><Name>Some Person</Name></Person>");
    item11.setState(SyncItemState.NEW);
    item11.setKey(new SyncItemKey<String>(UUID.randomUUID().toString(), String.class));
    SyncItem item12 = new SyncItem();
    item12.setContent("<PersonAddress><Street>Some Street</Street></PersonAddress>");
    item12.setState(SyncItemState.UPDATED);
    item12.setKey(new SyncItemKey<String>(UUID.randomUUID().toString(), String.class));
    List<SyncItem> items1 = new ArrayList<SyncItem>();
    items1.add(item11);
    items1.add(item12);
    syncRecord1.setItems(items1);

    // 'weird' end cases start here

    // no timestamp or items
    SyncRecord syncRecord2 = new SyncRecord();
    syncRecord2.setUuid(UUID.randomUUID().toString());

    // dump out the state
    Package pkg = new FilePackage();
    Record record = pkg.createRecordForWrite("SyncRecordTest");
    Item top = record.getRootItem();

    ((IItem) syncRecord1).save(record, top);
    ((IItem) syncRecord2).save(record, top);
    List<SyncRecord> originals = new ArrayList<SyncRecord>();
    originals.add(syncRecord1);
    originals.add(syncRecord2);

    // now Test deserialize - THIS DOES NOT WORK YET
    String textDes = record.toStringAsDocumentFragment();

    Package pkgDes = new FilePackage();
    Record recordDes = pkgDes.createRecordFromString(textDes);
    Item topDes = recordDes.getRootItem();

    // get items list that holds serialized sync records
    List<Item> itemsDes = recordDes.getItems(topDes);

    assertTrue(itemsDes.size() == originals.size());

    SyncRecord syncRecordDesc = null;
    Iterator<Item> iterator = itemsDes.iterator();
    Iterator<SyncRecord> iteratorOrig = originals.iterator();
    while (iterator.hasNext()) {
      syncRecordDesc = new SyncRecord();
      syncRecordDesc.load(recordDes, iterator.next());
      assertEquals(syncRecordDesc, iteratorOrig.next());
    }

    return;
  }