Skip to content
Browse files

Adding deep copy and remove methods for entities.

  • Loading branch information...
1 parent 85db826 commit 968300b14fb096e46f2d9c695f796a61dedd4deb @tHerrmann tHerrmann committed Apr 26, 2012
View
22 src-gwt/com/alkacon/vie/client/Entity.java
@@ -115,6 +115,28 @@ public HandlerRegistration addValueChangeHandler(ValueChangeHandler<I_Entity> ha
}
/**
+ * @see com.alkacon.vie.shared.I_Entity#createDeepCopy(java.lang.String)
+ */
+ public I_Entity createDeepCopy(String entityId) {
+
+ I_Entity result = Vie.getInstance().createEntity(entityId, getTypeName());
+ for (I_EntityAttribute attribute : getAttributes()) {
+ if (attribute.isSimpleValue()) {
+ List<String> values = attribute.getSimpleValues();
+ for (String value : values) {
+ result.addAttributeValue(attribute.getAttributeName(), value);
+ }
+ } else {
+ List<I_Entity> values = attribute.getComplexValues();
+ for (I_Entity value : values) {
+ result.addAttributeValue(attribute.getAttributeName(), value.createDeepCopy(null));
+ }
+ }
+ }
+ return result;
+ }
+
+ /**
* @see com.google.gwt.event.shared.HasHandlers#fireEvent(com.google.gwt.event.shared.GwtEvent)
*/
public void fireEvent(GwtEvent<?> event) {
View
7 src-gwt/com/alkacon/vie/client/I_Vie.java
@@ -176,4 +176,11 @@
*/
void registerTypes(I_Type type, Map<String, I_Type> types);
+ /**
+ * Removes the given entity from VIE.<p>
+ *
+ * @param entityId the entity id
+ */
+ void removeEntity(String entityId);
+
}
View
27 src-gwt/com/alkacon/vie/client/Vie.java
@@ -326,6 +326,33 @@ public void registerTypes(I_Type type, Map<String, I_Type> types) {
}
/**
+ * @see com.alkacon.vie.client.I_Vie#removeEntity(java.lang.String)
+ */
+ public native void removeEntity(String entityId) /*-{
+ var self = this;
+ var removeEntity = function(entity) {
+ if (entity != null && entity.isEntity) {
+ var attributes = entity.attributes;
+ if (attributes != null) {
+ for (key in attributes) {
+ var attribute = entity.get(key);
+ if (attribute != null && attribute.isCollection) {
+ for ( var i = 0; i < attribute.size(); i++) {
+ var child = attribute.at(i);
+ removeEntity(child);
+ }
+ }
+ }
+ }
+ self.entities.remove(entity);
+ }
+ }
+ var entity = self.entities
+ .get(@com.alkacon.vie.client.Vie::addPointyBrackets(Ljava/lang/String;)(entityId));
+ removeEntity(entity);
+ }-*/;
+
+ /**
* Returns a list of elements matching the given CSS selector.<p>
*
* @param selector the selector
View
9 src/com/alkacon/vie/shared/I_Entity.java
@@ -51,6 +51,15 @@
void addAttributeValue(String attributeName, String value);
/**
+ * Creates a deep copy of this entity.<p>
+ *
+ * @param entityId the id of the new entity, if <code>null</code> a generic id will be used
+ *
+ * @return the entity copy
+ */
+ I_Entity createDeepCopy(String entityId);
+
+ /**
* Returns an attribute.<p>
*
* @param attributeName the attribute name
View
30 test/com/alkacon/vie/client/VieTest.java
@@ -145,6 +145,36 @@ public void testEntityAttribute() {
}
/**
+ * Tests the deep copy and remove methods.<p>
+ */
+ public void testCopyAndRemoveEntity() {
+
+ I_Vie vie = getVieInstance();
+ String typeId = "type:very-complex";
+ I_Type veryComplex = vie.createType(typeId);
+ // using types defined in previous test
+ String complexAttributeName = "complexAttribute";
+ veryComplex.addAttribute(complexAttributeName, COMPLEX_TYPE_ID, 0, 1);
+ String attributeEntityId = "my-attribute-entity";
+ I_Entity attributeEntity = vie.createEntity(attributeEntityId, COMPLEX_TYPE_ID);
+ String value = "my attribute value";
+ attributeEntity.addAttributeValue(ATTRIBUTE_NAME, value);
+ String testEntityId = "my-test-entity";
+ I_Entity testEntity = vie.createEntity(testEntityId, typeId);
+ testEntity.addAttributeValue(complexAttributeName, attributeEntity);
+
+ I_Entity copy = testEntity.createDeepCopy("my-entity-copy");
+ I_EntityAttribute attribute = copy.getAttribute(complexAttributeName);
+ assertNotNull("The entity should have an attribute: " + complexAttributeName, attribute);
+ assertTrue("The attribute should be of the a complex type", attribute.isComplexValue());
+ System.out.println("trying to remove");
+ // test remove entity function
+ vie.removeEntity(testEntityId);
+ assertNull("The entity should no longer be registered", vie.getEntity(testEntityId));
+ assertNull("The child entity should also be removed", vie.getEntity(attributeEntityId));
+ }
+
+ /**
* Tests the event handling on entities.<p>
* Relies on the types created in {@link #testEntityAttribute()}.<p>
*/

0 comments on commit 968300b

Please sign in to comment.
Something went wrong with that request. Please try again.