Author: sdumitriu
Date: 2008-01-02 18:11:21 +0100 (Wed, 02 Jan 2008)
New Revision: 6599

Added:
   
xwiki-platform/core/trunk/xwiki-patchservice/src/main/java/org/xwiki/platform/patchservice/impl/ClassPropertyDeleteOperation.java
Modified:
   
xwiki-platform/core/trunk/xwiki-patchservice/src/main/resources/META-INF/services/org.xwiki.platform.patchservice.api.RWOperation
   
xwiki-platform/core/trunk/xwiki-patchservice/src/test/java/org/xwiki/platform/ClassPropertyOperationsTest.java
Log:
XWIKI-1977: Create a patchservice data model
Add the ClassPropertyDeleteOperation implementation.


Added: 
xwiki-platform/core/trunk/xwiki-patchservice/src/main/java/org/xwiki/platform/patchservice/impl/ClassPropertyDeleteOperation.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-patchservice/src/main/java/org/xwiki/platform/patchservice/impl/ClassPropertyDeleteOperation.java
                           (rev 0)
+++ 
xwiki-platform/core/trunk/xwiki-patchservice/src/main/java/org/xwiki/platform/patchservice/impl/ClassPropertyDeleteOperation.java
   2008-01-02 17:11:21 UTC (rev 6599)
@@ -0,0 +1,105 @@
+package org.xwiki.platform.patchservice.impl;
+
+import org.apache.commons.lang.builder.HashCodeBuilder;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xwiki.platform.patchservice.api.Operation;
+import org.xwiki.platform.patchservice.api.RWOperation;
+
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.doc.XWikiDocument;
+import com.xpn.xwiki.objects.classes.BaseClass;
+import com.xpn.xwiki.objects.classes.PropertyClass;
+
+public class ClassPropertyDeleteOperation extends AbstractOperationImpl 
implements RWOperation
+{
+    public static final String PROPERTY_NAME_ATTRIBUTE_NAME = "name";
+
+    private String propertyName;
+
+    static {
+        
OperationFactoryImpl.registerTypeProvider(Operation.TYPE_CLASS_PROPERTY_DELETE,
+            ClassPropertyDeleteOperation.class);
+    }
+
+    public ClassPropertyDeleteOperation()
+    {
+        this.setType(Operation.TYPE_CLASS_PROPERTY_DELETE);
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public void apply(XWikiDocument doc) throws XWikiException
+    {
+        BaseClass bclass = doc.getxWikiClass();
+        PropertyClass prop = (PropertyClass) bclass.get(propertyName);
+        if (prop != null) {
+            bclass.removeField(propertyName);
+        } else {
+            throw new XWikiException(XWikiException.MODULE_XWIKI_PLUGINS,
+                XWikiException.ERROR_XWIKI_UNKNOWN,
+                "Invalid property name: " + this.propertyName);
+        }
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public boolean deleteType(String propertyName)
+    {
+        this.propertyName = propertyName;
+        return true;
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public void fromXml(Element e) throws XWikiException
+    {
+        this.propertyName = e.getAttribute(PROPERTY_NAME_ATTRIBUTE_NAME);
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public Element toXml(Document doc) throws XWikiException
+    {
+        Element xmlNode = doc.createElement(AbstractOperationImpl.NODE_NAME);
+        xmlNode.setAttribute(AbstractOperationImpl.TYPE_ATTRIBUTE_NAME, 
this.getType());
+        xmlNode.setAttribute(PROPERTY_NAME_ATTRIBUTE_NAME, propertyName);
+        return xmlNode;
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public boolean equals(Object other)
+    {
+        try {
+            ClassPropertyDeleteOperation otherOperation = 
(ClassPropertyDeleteOperation) other;
+            return otherOperation.propertyName.equals(this.propertyName);
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public int hashCode()
+    {
+        return new HashCodeBuilder(13, 
17).append(this.propertyName).toHashCode();
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public String toString()
+    {
+        return this.getType() + ": [" + this.propertyName + "]";
+    }
+}


Property changes on: 
xwiki-platform/core/trunk/xwiki-patchservice/src/main/java/org/xwiki/platform/patchservice/impl/ClassPropertyDeleteOperation.java
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: 
xwiki-platform/core/trunk/xwiki-patchservice/src/main/resources/META-INF/services/org.xwiki.platform.patchservice.api.RWOperation
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-patchservice/src/main/resources/META-INF/services/org.xwiki.platform.patchservice.api.RWOperation
   2008-01-02 16:59:09 UTC (rev 6598)
+++ 
xwiki-platform/core/trunk/xwiki-patchservice/src/main/resources/META-INF/services/org.xwiki.platform.patchservice.api.RWOperation
   2008-01-02 17:11:21 UTC (rev 6599)
@@ -2,3 +2,4 @@
 org.xwiki.platform.patchservice.impl.ContentDeleteOperation
 org.xwiki.platform.patchservice.impl.PropertySetOperation
 org.xwiki.platform.patchservice.impl.ClassPropertySetOperation
+org.xwiki.platform.patchservice.impl.ClassPropertyDeleteOperation

Modified: 
xwiki-platform/core/trunk/xwiki-patchservice/src/test/java/org/xwiki/platform/ClassPropertyOperationsTest.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-patchservice/src/test/java/org/xwiki/platform/ClassPropertyOperationsTest.java
      2008-01-02 16:59:09 UTC (rev 6598)
+++ 
xwiki-platform/core/trunk/xwiki-patchservice/src/test/java/org/xwiki/platform/ClassPropertyOperationsTest.java
      2008-01-02 17:11:21 UTC (rev 6599)
@@ -26,7 +26,7 @@
     Document domDoc;
 
     XWikiDocument doc;
-    
+
     BaseClass bclass;
 
     protected void setUp()
@@ -96,7 +96,8 @@
         ((PropertyClass) bclass.get("prop1")).setPrettyName("new Property 1");
         RWOperation operation = getOperation((PropertyClass) 
bclass.get("prop1"), false);
         operation.apply(doc);
-        assertEquals("new Property 1", ((PropertyClass) 
doc.getxWikiClass().get("prop1")).getPrettyName());
+        assertEquals("new Property 1", ((PropertyClass) 
doc.getxWikiClass().get("prop1"))
+            .getPrettyName());
     }
 
     public void testXmlRoundtripPropertySetOperation() throws XWikiException
@@ -108,8 +109,80 @@
         assertEquals(loadedOperation, operation);
     }
 
-    private RWOperation getOperation(PropertyClass property, boolean create) 
throws XWikiException
+    public void testApplyPropertyDeleteOperation() throws XWikiException
     {
+        testApplyPropertyAddOperation();
+        RWOperation operation =
+            OperationFactoryImpl.getInstance().newOperation(
+                RWOperation.TYPE_CLASS_PROPERTY_DELETE);
+        operation.deleteType("prop1");
+        operation.apply(doc);
+        assertEquals(1, doc.getxWikiClass().getProperties().length);
+        assertNull(doc.getxWikiClass().get("prop1"));
+        assertNotNull(doc.getxWikiClass().get("prop2"));
+    }
+
+    public void testApplyInvalidPropertyDeleteOperation() throws XWikiException
+    {
+        RWOperation operation =
+            OperationFactoryImpl.getInstance().newOperation(
+                RWOperation.TYPE_CLASS_PROPERTY_DELETE);
+        operation.deleteType("prop3");
+        try {
+            operation.apply(doc);
+            assertTrue(false);
+        } catch (XWikiException ex) {
+            assertTrue(true);
+        }
+        assertNull(doc.getxWikiClass().get("prop3"));
+    }
+
+    public void testXmlRoundtripPropertyDeleteOperation() throws XWikiException
+    {
+        RWOperation operation =
+            OperationFactoryImpl.getInstance().newOperation(
+                RWOperation.TYPE_CLASS_PROPERTY_DELETE);
+        operation.deleteType("prop1");
+        Element e = operation.toXml(domDoc);
+        Operation loadedOperation = 
OperationFactoryImpl.getInstance().loadOperation(e);
+        assertEquals(loadedOperation, operation);
+    }
+
+    public void testConsecutiveClassOperations() throws XWikiException
+    {
+        bclass.addTextField("prop1", "Property 1", 30);
+        RWOperation operation = getOperation((PropertyClass) 
bclass.get("prop1"), true);
+        operation.apply(doc);
+        assertEquals(1, doc.getxWikiClass().getProperties().length);
+        assertNotNull(doc.getxWikiClass().getField("prop1"));
+        assertEquals("prop1", doc.getxWikiClass().get("prop1").getName());
+
+        ((PropertyClass) bclass.get("prop1")).setPrettyName("new Property 1");
+        operation = getOperation((PropertyClass) bclass.get("prop1"), false);
+        operation.apply(doc);
+        assertEquals("new Property 1", ((PropertyClass) 
doc.getxWikiClass().get("prop1"))
+            .getPrettyName());
+        assertEquals(1, doc.getxWikiClass().getProperties().length);
+
+        bclass.addBooleanField("prop2", "Property 2", "yesno");
+        operation = getOperation((PropertyClass) bclass.get("prop2"), true);
+        operation.apply(doc);
+        assertEquals(2, doc.getxWikiClass().getProperties().length);
+
+        operation =
+            OperationFactoryImpl.getInstance().newOperation(
+                RWOperation.TYPE_CLASS_PROPERTY_DELETE);
+        operation.deleteType("prop1");
+        assertEquals(2, doc.getxWikiClass().getProperties().length);
+        operation.apply(doc);
+        assertEquals(1, doc.getxWikiClass().getProperties().length);
+        assertNotNull(doc.getxWikiClass().getField("prop2"));
+        assertNull(doc.getxWikiClass().getField("prop1"));
+    }
+
+    private RWOperation getOperation(PropertyClass property, boolean create)
+        throws XWikiException
+    {
         String type = property.getClass().getCanonicalName();
         Map config = new HashMap();
         for (Iterator it2 = property.getFieldList().iterator(); 
it2.hasNext();) {
@@ -117,7 +190,9 @@
             config.put(pr.getName(), pr.getValue());
         }
         RWOperation operation =
-            OperationFactoryImpl.getInstance().newOperation(create ? 
RWOperation.TYPE_CLASS_PROPERTY_ADD : RWOperation.TYPE_CLASS_PROPERTY_CHANGE);
+            OperationFactoryImpl.getInstance().newOperation(
+                create ? RWOperation.TYPE_CLASS_PROPERTY_ADD
+                    : RWOperation.TYPE_CLASS_PROPERTY_CHANGE);
         if (create) {
             operation.createType(type, config);
         } else {

_______________________________________________
notifications mailing list
notifications@xwiki.org
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to