cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject [1/6] cayenne git commit: extends CayenneDataObject with a setToManyTarget(String, Collection<? extends DataObject>, boolean) method
Date Sat, 02 Jan 2016 13:36:16 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master c05ad576c -> fcecf5801


extends CayenneDataObject with a setToManyTarget(String, Collection<? extends DataObject>,
boolean) method


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/1358dad4
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/1358dad4
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/1358dad4

Branch: refs/heads/master
Commit: 1358dad4e3ae2cf2735aa223b869e4b85f18508e
Parents: ee4f506
Author: jotpe <jotpe@posteo.de>
Authored: Sun Dec 6 23:04:41 2015 +0100
Committer: jotpe <jotpe@posteo.de>
Committed: Sun Dec 6 23:04:41 2015 +0100

----------------------------------------------------------------------
 .../org/apache/cayenne/CayenneDataObject.java   | 86 ++++++++++++++++++++
 1 file changed, 86 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/1358dad4/cayenne-server/src/main/java/org/apache/cayenne/CayenneDataObject.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/CayenneDataObject.java b/cayenne-server/src/main/java/org/apache/cayenne/CayenneDataObject.java
index 43f7853..b950257 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/CayenneDataObject.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/CayenneDataObject.java
@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.lang.reflect.Array;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -259,6 +260,91 @@ public class CayenneDataObject extends PersistentObject implements DataObject,
V
 		}
 	}
 
+	/**
+	 * Sets the relationships to the specified <code>DataObject</code> objects.
+	 * 
+	 * <p>
+	 * New relationships will be created with
+	 * {@link #addToManyTarget(String, org.apache.cayenne.DataObject, boolean)},
+	 * already established relationships stay untouched. Missing relationships
+	 * will be removed with
+	 * {@link #removeToManyTarget(String, org.apache.cayenne.DataObject, boolean)}
+	 * and returnd as List. You may delete them manually.
+	 * </p>
+	 * 
+	 * <p>
+	 * Notice: Moving an object relationship to another object, is still needing
+	 * an manually "unregister" from the first object by
+	 * {@link #removeToManyTarget(String, org.apache.cayenne.DataObject, boolean)}
+	 * </p>
+	 * 
+	 * @param relName
+	 *            name of the relation
+	 * @param values
+	 *            <code>DataObject</code> objects of this
+	 *            <code>Collection</code> are set to the object. No changes will
+	 *            be made to the the <code>Collection</code>, a copy is used. It
+	 *            is safe to pass a persisted <code>Collection</code> of another
+	 *            object.
+	 * @param setReverse
+	 *            update reverse relationships
+	 * @return <code>List&lt;? extends DataObject&gt;</code> of unrelated
+	 *         DataObjects. If no relationship was removed an empty List is
+	 *         returned.
+	 * @throws NullPointerException
+	 *             if no relationship could be read by relName, or if the passed
+	 *             <code>Collection</code> is null. To clear all relationships
+	 *             use an empty <code>Collection</code>
+	 * @since 4.0.M3
+	 */
+	public List<? extends DataObject> setToManyTarget(String relName, Collection<?
extends DataObject> values,
+			boolean setReverse) {
+		if (values == null) {
+			throw new NullPointerException();
+		}
+
+		Object property = readProperty(relName);
+		if(property == null) {
+			throw new NullPointerException("unknown relName " + relName);
+		}
+		Collection<DataObject> old = null;
+		if (property instanceof Map) {
+			old = ((Map) property).values();
+		} else if (dataObjects instanceof Collection) {
+			old = (Collection) property;
+		} else {
+			throw new RuntimeException("setToManyTarget only operates with Map and Collection types");
+		}
+
+		// operate on a copy of passed collection
+		values = new ArrayList<DataObject>(values);
+
+		List<DataObject> removedObjects = new ArrayList<DataObject>();
+
+		// remove all relationships, which are missing in passed collection
+		Object[] oldValues = old.toArray();
+		for (Object obj : oldValues) {
+			if (!values.contains(obj)) {
+				DataObject obj2 = (DataObject) obj;
+				removeToManyTarget(relName, obj2, setReverse);
+				// collect objects whose relationship was removed
+				removedObjects.add((DataObject) obj2);
+			}
+		}
+
+		// dont add elements which are already present
+		for (Object obj : old) {
+			values.remove(obj);
+		}
+
+		// add new elements
+		for (DataObject obj : values) {
+			addToManyTarget(relName, obj, setReverse);
+		}
+
+		return removedObjects;
+	}
+
 	public void setToOneTarget(String relationshipName, DataObject value, boolean setReverse)
{
 
 		willConnect(relationshipName, value);


Mime
View raw message