private String getObjectOrLocalId(ParseObject object) { return object.getObjectId() != null ? object.getObjectId() : object.getOrCreateLocalId(); } }
private String getObjectOrLocalId(ParseObject object) { return object.getObjectId() != null ? object.getObjectId() : object.getOrCreateLocalId(); } }
private void removeParseObjectFromSet(ParseObject obj, Set<ParseObject> set) { if (Parse.getLocalDatastore() != null || obj.getObjectId() == null) { // There's no way there could be duplicate instances. set.remove(obj); return; } // We have to do this the hard way. for (ParseObject existingObject : set) { if (obj.getObjectId().equals(existingObject.getObjectId())) { set.remove(existingObject); } } }
private void addParseObjectToSet(ParseObject obj, Set<ParseObject> set) { if (Parse.getLocalDatastore() != null || obj.getObjectId() == null) { // There's no way there could be duplicate instances. set.add(obj); return; } // We have to do this the hard way. for (ParseObject existingObject : set) { if (obj.getObjectId().equals(existingObject.getObjectId())) { set.remove(existingObject); } } set.add(obj); }
public boolean hasSameId(ParseObject other) { synchronized (mutex) { return this.getClassName() != null && this.getObjectId() != null && this.getClassName().equals(other.getClassName()) && this.getObjectId().equals(other.getObjectId()); } }
@Override public JSONObject encodeRelatedObject(ParseObject object) { // Ensure the ParseObject has an id so it can be encoded as a pointer. if (object.getObjectId() == null) { // object that hasn't been saved. throw new IllegalStateException("unable to encode an association with an unsaved ParseObject"); } return super.encodeRelatedObject(object); } }
/* package */ ParseRelation(ParseObject parent, String key) { this.parent = new WeakReference<>(parent); this.parentObjectId = parent.getObjectId(); this.parentClassName = parent.getClassName(); this.key = key; this.targetClass = null; }
/* package */ void ensureParentAndKey(ParseObject someParent, String someKey) { synchronized (mutex) { if (parent == null) { parent = new WeakReference<>(someParent); parentObjectId = someParent.getObjectId(); parentClassName = someParent.getClassName(); } if (key == null) { key = someKey; } if (parent.get() != someParent) { throw new IllegalStateException( "Internal error. One ParseRelation retrieved from two different ParseObjects."); } if (!key.equals(someKey)) { throw new IllegalStateException( "Internal error. One ParseRelation retrieved from two different keys."); } } }
/** * This deletes all of the objects from the given List. */ private static <T extends ParseObject> Task<Void> deleteAllAsync( final List<T> objects, final String sessionToken) { if (objects.size() == 0) { return Task.forResult(null); } // Create a list of unique objects based on objectIds int objectCount = objects.size(); final List<ParseObject> uniqueObjects = new ArrayList<>(objectCount); final HashSet<String> idSet = new HashSet<>(); for (int i = 0; i < objectCount; i++) { ParseObject obj = objects.get(i); if (!idSet.contains(obj.getObjectId())) { idSet.add(obj.getObjectId()); uniqueObjects.add(obj); } } return enqueueForAll(uniqueObjects, new Continuation<Void, Task<Void>>() { @Override public Task<Void> then(Task<Void> toAwait) { return deleteAllAsync(uniqueObjects, sessionToken, toAwait); } }); }
@Override public JSONObject encodeRelatedObject(ParseObject object) { JSONObject json = new JSONObject(); try { if (object.getObjectId() != null) { json.put("__type", "Pointer"); json.put("className", object.getClassName()); json.put("objectId", object.getObjectId()); } else { json.put("__type", "Pointer"); json.put("className", object.getClassName()); json.put("localId", object.getOrCreateLocalId()); } } catch (JSONException e) { // This should not happen throw new RuntimeException(e); } return json; } }
boolean isDirty(boolean considerChildren) { synchronized (mutex) { return (isDeleted || getObjectId() == null || hasChanges() || (considerChildren && hasDirtyChildren())); } }
@Override public Task<Void> then(Task<Void> task) throws Exception { // Put the JSON in the database. String className = object.getClassName(); String objectId = object.getObjectId(); int isDeletingEventually = json.getInt(ParseObject.KEY_IS_DELETING_EVENTUALLY); final ContentValues values = new ContentValues(); values.put(OfflineSQLiteOpenHelper.KEY_CLASS_NAME, className); values.put(OfflineSQLiteOpenHelper.KEY_JSON, json.toString()); if (objectId != null) { values.put(OfflineSQLiteOpenHelper.KEY_OBJECT_ID, objectId); } values.put(OfflineSQLiteOpenHelper.KEY_IS_DELETING_EVENTUALLY, isDeletingEventually); String where = OfflineSQLiteOpenHelper.KEY_UUID + " = ?"; String[] args = {uuid}; return db.updateAsync(OfflineSQLiteOpenHelper.TABLE_OBJECTS, values, where, args).makeVoid(); } });
/* package */ void unregisterObject(ParseObject object) { synchronized (lock) { String objectId = object.getObjectId(); if (objectId != null) { classNameAndObjectIdToObjectMap.remove(Pair.create(object.getClassName(), objectId)); } } }
/** * This should be called by the ParseObject constructor notify the store that there is an object * with this className and objectId. */ /* package */ void registerNewObject(ParseObject object) { synchronized (lock) { String objectId = object.getObjectId(); if (objectId != null) { String className = object.getClassName(); Pair<String, String> classNameAndObjectId = Pair.create(className, objectId); classNameAndObjectIdToObjectMap.put(classNameAndObjectId, object); } } }
@Override protected boolean visit(Object value) { if (value instanceof ParseFile) { ParseFile file = (ParseFile) value; if (file.isDirty()) { result.set(false); } } if (value instanceof ParseObject) { ParseObject object = (ParseObject) value; if (object.getObjectId() == null) { result.set(false); } } // Continue to traverse only if it can still be serialized. return result.get(); } }.setYieldRoot(false).setTraverseParseObjects(true).traverse(this);
@Test public void testConstructorWithJSONAndDecoder() throws Exception { // Make ParseRelation JSONArray ParseObject object = mock(ParseObject.class); when(object.getClassName()).thenReturn("Test"); when(object.getObjectId()).thenReturn("objectId"); object.setObjectId("objectId"); JSONArray objectJSONArray = new JSONArray(); objectJSONArray.put(PointerEncoder.get().encode(object)); JSONObject relationJSON = new JSONObject(); relationJSON.put("className", "Test"); relationJSON.put("objects", objectJSONArray); ParseRelation relationFromJSON = new ParseRelation(relationJSON, ParseDecoder.get()); assertEquals("Test", relationFromJSON.getTargetClass()); assertEquals(1, relationFromJSON.getKnownObjects().size()); Object[] objects = relationFromJSON.getKnownObjects().toArray(); assertEquals("objectId", ((ParseObject) objects[0]).getObjectId()); }
@Test public void testParcelWithCircularReference() { ParseObject parent = new ParseObject("Parent"); ParseObject child = new ParseObject("Child"); parent.setObjectId("parentId"); parent.put("self", parent); child.setObjectId("childId"); child.put("self", child); child.put("parent", parent); parent.put("child", child); Parcel parcel = Parcel.obtain(); parent.writeToParcel(parcel, 0); parcel.setDataPosition(0); parent = ParseObject.CREATOR.createFromParcel(parcel); assertEquals(parent.getObjectId(), "parentId"); assertEquals(parent.getParseObject("self").getObjectId(), "parentId"); child = parent.getParseObject("child"); assertEquals(child.getObjectId(), "childId"); assertEquals(child.getParseObject("self").getObjectId(), "childId"); assertEquals(child.getParseObject("parent").getObjectId(), "parentId"); }
@Test public void testParcelWithCircularReferenceFromServer() { ParseObject parent = new ParseObject("Parent"); ParseObject child = new ParseObject("Child"); parent.setState(new ParseObject.State.Builder("Parent") .objectId("parentId") .put("self", parent) .put("child", child).build()); parent.setObjectId("parentId"); child.setState(new ParseObject.State.Builder("Child") .objectId("childId") .put("self", child) .put("parent", parent).build()); Parcel parcel = Parcel.obtain(); parent.writeToParcel(parcel, 0); parcel.setDataPosition(0); parent = ParseObject.CREATOR.createFromParcel(parcel); assertEquals(parent.getObjectId(), "parentId"); assertEquals(parent.getParseObject("self").getObjectId(), "parentId"); child = parent.getParseObject("child"); assertEquals(child.getObjectId(), "childId"); assertEquals(child.getParseObject("self").getObjectId(), "childId"); assertEquals(child.getParseObject("parent").getObjectId(), "parentId"); }
@Test public void testFromJSONPayload() throws JSONException { JSONObject json = new JSONObject( "{" + "\"className\":\"GameScore\"," + "\"createdAt\":\"2015-06-22T21:23:41.733Z\"," + "\"objectId\":\"TT1ZskATqS\"," + "\"updatedAt\":\"2015-06-22T22:06:18.104Z\"," + "\"score\":{" + "\"__op\":\"Increment\"," + "\"amount\":1" + "}," + "\"age\":33" + "}"); ParseObject parseObject = ParseObject.fromJSONPayload(json, ParseDecoder.get()); assertEquals("GameScore", parseObject.getClassName()); assertEquals("TT1ZskATqS", parseObject.getObjectId()); ParseDateFormat format = ParseDateFormat.getInstance(); assertTrue(parseObject.getCreatedAt().equals(format.parse("2015-06-22T21:23:41.733Z"))); assertTrue(parseObject.getUpdatedAt().equals(format.parse("2015-06-22T22:06:18.104Z"))); Set<String> keys = parseObject.getState().keySet(); assertEquals(0, keys.size()); ParseOperationSet currentOperations = parseObject.operationSetQueue.getLast(); assertEquals(2, currentOperations.size()); }
@Test public void testParcelable() { ParseFieldOperations.registerDefaultDecoders(); ParseRelation<ParseObject> relation = new ParseRelation<>("Test"); ParseObject parent = new ParseObject("Parent"); parent.setObjectId("parentId"); relation.ensureParentAndKey(parent, "key"); ParseObject inner = new ParseObject("Test"); inner.setObjectId("innerId"); relation.add(inner); Parcel parcel = Parcel.obtain(); relation.writeToParcel(parcel, 0); parcel.setDataPosition(0); //noinspection unchecked ParseRelation<ParseObject> newRelation = ParseRelation.CREATOR.createFromParcel(parcel); assertEquals(newRelation.getTargetClass(), "Test"); assertEquals(newRelation.getKey(), "key"); assertEquals(newRelation.getParent().getClassName(), "Parent"); assertEquals(newRelation.getParent().getObjectId(), "parentId"); assertEquals(newRelation.getKnownObjects().size(), 1); // This would fail assertTrue(newRelation.hasKnownObject(inner)). // That is because ParseRelation uses == to check for known objects. }