Commits

Anonymous committed f706a9d

Added remove implementation to propertysets

git-svn-id: http://svn.opensymphony.com/svn/propertyset/trunk@122bf3cbcdd-1c1a-0410-9a68-d6f521e3fa7b

Comments (0)

Files changed (17)

src/java/com/opensymphony/module/propertyset/AbstractPropertySet.java

 
     //~ Methods ////////////////////////////////////////////////////////////////
 
-    public abstract Collection getKeys(String prefix, int type) throws PropertyException;
-
-    public abstract int getType(String key) throws PropertyException;
-
-    public abstract boolean exists(String key) throws PropertyException;
-
-    public abstract void remove(String key) throws PropertyException;
-
     public void setAsActualType(String key, Object value) throws PropertyException {
         int type;
 

src/java/com/opensymphony/module/propertyset/PropertySet.java

     void remove(String key) throws PropertyException;
 
     /**
+     * Remove the propertyset and all it associated keys.
+     * @throws PropertyException if there is an error removing the propertyset.
+     */
+    void remove() throws PropertyException;
+
+    /**
     * Whether this PropertySet implementation allows the type specified
     * to be stored or retrieved.
     */

src/java/com/opensymphony/module/propertyset/aggregate/AggregatePropertySet.java

 
 import org.w3c.dom.Document;
 
-import java.util.*;
 import java.io.Serializable;
 
+import java.util.*;
+
 
 /**
  * PropertySet composed of a collection of other propertysets.
         }
     }
 
+    public void remove() throws PropertyException {
+        if (propertySets != null) {
+            Iterator iter = propertySets.iterator();
+
+            while (iter.hasNext()) {
+                PropertySet ps = (PropertySet) iter.next();
+                ps.remove();
+            }
+        }
+    }
+
     public void remove(String key) throws PropertyException {
         Iterator i = propertySets.iterator();
 

src/java/com/opensymphony/module/propertyset/cached/CachingPropertySet.java

         }
     }
 
+    public void remove() throws PropertyException {
+        cachePS.remove();
+        decoratedPS.remove();
+    }
+
     public void remove(String key) throws PropertyException {
         cachePS.remove(key);
         decoratedPS.remove(key);

src/java/com/opensymphony/module/propertyset/database/JDBCPropertySet.java

         colNumber = (String) config.get("col.number");
     }
 
+    public void remove() throws PropertyException {
+        Connection conn = null;
+
+        try {
+            conn = ds.getConnection();
+
+            String sql = "DELETE FROM " + tableName + " WHERE " + colGlobalKey + " = ?";
+            PreparedStatement ps = conn.prepareStatement(sql);
+            ps.setString(1, globalKey);
+            ps.executeUpdate();
+            ps.close();
+        } catch (SQLException e) {
+            throw new PropertyException(e.getMessage());
+        } finally {
+            closeConnection(conn);
+        }
+    }
+
     public void remove(String key) throws PropertyException {
         Connection conn = null;
 

src/java/com/opensymphony/module/propertyset/ejb/EJBPropertySet.java

     }
 
     /**
-    * Proxy to {@link com.opensymphony.module.propertyset.ejb.PropertyStore#remove(java.lang.String,long,java.lang.String)}
+     * Proxy to {@link PropertyStore#removeEntry(String, long, String)}
+     */
+    public void remove() throws PropertyException {
+        try {
+            store.removeEntry(entityName, entityId);
+        } catch (RemoteException re) {
+            throw new PropertyImplementationException(re);
+        }
+    }
+
+    /**
+    * Proxy to {@link PropertyStore#removeEntry(String, long, String)}
     */
     public void remove(String key) throws PropertyException {
         try {

src/java/com/opensymphony/module/propertyset/ejb/PropertyStoreEJB.java

 
     /**
      * @ejb.interface-method
+     * @ejb.transaction type="Required"
+     */
+    public void removeEntry(String entityName, long entityId) {
+        try {
+            Collection items = entryHome.findByNameAndId(entityName, entityId);
+            Iterator iter = items.iterator();
+
+            while (iter.hasNext()) {
+                PropertyEntryLocal entry = (PropertyEntryLocal) iter.next();
+                entry.remove();
+            }
+        } catch (FinderException e) {
+            if (logger.isDebugEnabled()) {
+                logger.debug("Value did not exist anyway.");
+            }
+        } catch (PropertyException e) {
+            throw e;
+        } catch (Exception e) {
+            logger.error("Could not remove value.", e);
+            throw new PropertyImplementationException("Could not remove value.", e);
+        }
+    }
+
+    /**
+     * @ejb.interface-method
      */
     public void removeEntry(String entityName, long entityId, String key) {
         if (logger.isDebugEnabled()) {

src/java/com/opensymphony/module/propertyset/hibernate/HibernatePropertySet.java

         configProvider.getPropertySetDAO().remove(entityName, entityId, key);
     }
 
+    public void remove() throws PropertyException {
+        configProvider.getPropertySetDAO().remove(entityName, entityId);
+    }
+
     protected void setImpl(int type, String key, Object value) throws PropertyException {
         PropertySetItem item = null;
 

src/java/com/opensymphony/module/propertyset/hibernate/HibernatePropertySetDAO.java

     PropertySetItem findByKey(String entityName, Long entityId, String key);
 
     void remove(String entityName, Long entityId, String key);
+
+    void remove(String entityName, Long entityId);
 }

src/java/com/opensymphony/module/propertyset/hibernate/HibernatePropertySetDAOImpl.java

 import net.sf.hibernate.Session;
 import net.sf.hibernate.SessionFactory;
 
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
 
 
 /**
         return item;
     }
 
+    public void remove(String entityName, Long entityId) {
+        Session session = null;
+
+        try {
+            session = this.sessionFactory.openSession();
+
+            //hani: todo this needs to be optimised rather badly, but I have no idea how
+            Collection keys = getKeys(entityName, entityId, null, 0);
+            Iterator iter = keys.iterator();
+
+            while (iter.hasNext()) {
+                String key = (String) iter.next();
+                session.delete(HibernatePropertySetDAOUtils.getItem(session, entityName, entityId, key));
+            }
+
+            session.flush();
+        } catch (HibernateException e) {
+            throw new PropertyException("Could not remove all keys: " + e.getMessage());
+        } finally {
+            try {
+                if (session != null) {
+                    if (!session.connection().getAutoCommit()) {
+                        session.connection().commit();
+                    }
+
+                    session.close();
+                }
+            } catch (Exception e) {
+            }
+        }
+    }
+
     public void remove(String entityName, Long entityId, String key) {
         Session session = null;
 

src/java/com/opensymphony/module/propertyset/javabeans/BeanIntrospectorPropertySet.java

         setBean(bean);
     }
 
+    public void remove() throws PropertyException {
+        //no-op, doesn't make sense to remove bean properties
+    }
+
     public void remove(String key) throws PropertyException {
         throw new PropertyImplementationException("Remove not supported in BeanIntrospectorPropertySet, use setXXX(null) instead");
     }

src/java/com/opensymphony/module/propertyset/map/MapPropertySet.java

  * ====================================================================
  */
 import com.opensymphony.module.propertyset.AbstractPropertySet;
+import com.opensymphony.module.propertyset.PropertyException;
 
 import java.util.*;
 
         map.remove(key);
     }
 
+    public void remove() throws PropertyException {
+        map.clear();
+    }
+
     /**
     * Returns false.
     */

src/java/com/opensymphony/module/propertyset/memory/MemoryPropertySet.java

         getMap().remove(key);
     }
 
+    public void remove() throws PropertyException {
+        map.clear();
+    }
+
     protected synchronized void setImpl(int type, String key, Object value) throws DuplicatePropertyKeyException {
         if (exists(key)) {
             ValueEntry v = (ValueEntry) getMap().get(key);

src/java/com/opensymphony/module/propertyset/memory/SerializablePropertySet.java

         serialMap = new HashMap();
     }
 
+    public void remove() throws PropertyException {
+        super.remove();
+        serialMap.clear();
+    }
+
     protected synchronized void setImpl(int type, String key, Object value) throws IllegalPropertyException, DuplicatePropertyKeyException {
         if ((value != null) && !(value instanceof Serializable)) {
             throw new IllegalPropertyException("Cannot set " + key + ". Value type " + value.getClass() + " not Serializable");

src/java/com/opensymphony/module/propertyset/ofbiz/OFBizPropertySet.java

         entityName = (String) args.get("entityName");
     }
 
+    public void remove() throws PropertyException {
+        //hani: todo this needs to be optimised rather badly, but I have no idea how
+        Collection keys = getKeys();
+        Iterator iter = keys.iterator();
+
+        while (iter.hasNext()) {
+            String key = (String) iter.next();
+            remove(key);
+        }
+    }
+
     public void remove(String key) throws PropertyException {
         try {
             // remove actual property

src/java/com/opensymphony/module/propertyset/ojb/OJBPropertySet.java

 
             Iterator iter = broker.getReportQueryIteratorByQuery(q);
 
-            Object[] obj = null;
-
             while (iter.hasNext()) {
-                obj = (Object[]) iter.next();
+                Object[] obj = (Object[]) iter.next();
 
                 String itemKey = (String) obj[0];
 
 
             throw new PropertyException(e.getMessage());
         } finally {
-            if (broker != null) {
-                broker.close();
-            }
+            broker.close();
         }
 
         return list;
         try {
             broker = this.getBroker();
 
-            Criteria critere = new Criteria();
-            critere.addEqualTo("globalKey", globalKey);
-            critere.addLike("itemKey", key);
+            Criteria criteria = new Criteria();
+            criteria.addEqualTo("globalKey", globalKey);
+            criteria.addLike("itemKey", key);
 
-            ReportQueryByCriteria q = QueryFactory.newReportQuery(OJBPropertySetItem.class, critere);
+            ReportQueryByCriteria q = QueryFactory.newReportQuery(OJBPropertySetItem.class, criteria);
             q.setColumns(new String[] {"itemType"});
 
             Iterator iter = broker.getReportQueryIteratorByQuery(q);
 
-            Object[] obj = null;
-
             if (iter.hasNext()) {
-                obj = (Object[]) iter.next();
+                Object[] obj = (Object[]) iter.next();
 
                 type = ((Integer) obj[0]).intValue();
             }
 
             throw new PropertyException(e.getMessage());
         } finally {
-            if (broker != null) {
-                broker.close();
-            }
+            broker.close();
         }
 
         return type;
         globalKey = (String) args.get("globalKey");
     }
 
+    public void remove() throws PropertyException {
+        PersistenceBroker broker = null;
+
+        try {
+            broker = this.getBroker();
+
+            Criteria criteria = new Criteria();
+            criteria.addEqualTo("globalKey", globalKey);
+
+            Query query = new QueryByCriteria(OJBPropertySetItem.class, criteria);
+
+            broker.delete(query);
+        } catch (Exception e) {
+            log.error("An exception occured", e);
+
+            throw new PropertyException(e.getMessage());
+        } finally {
+            broker.close();
+        }
+    }
+
     public void remove(String key) throws PropertyException {
         PersistenceBroker broker = null;
 
         try {
             broker = this.getBroker();
 
-            Criteria critere = new Criteria();
-            critere.addEqualTo("globalKey", globalKey);
-            critere.addEqualTo("itemKey", key);
+            Criteria criteria = new Criteria();
+            criteria.addEqualTo("globalKey", globalKey);
+            criteria.addEqualTo("itemKey", key);
 
-            Query requete = new QueryByCriteria(OJBPropertySetItem.class, critere);
+            Query query = new QueryByCriteria(OJBPropertySetItem.class, criteria);
 
-            broker.delete(requete);
+            broker.delete(query);
         } catch (Exception e) {
             log.error("An exception occured", e);
 
             throw new PropertyException(e.getMessage());
         } finally {
-            if (broker != null) {
-                broker.close();
-            }
+            broker.close();
         }
     }
 
 
             throw new PropertyException(e.getMessage());
         } finally {
-            if (broker != null) {
-                broker.close();
-            }
+            broker.close();
         }
     }
 
         try {
             broker = this.getBroker();
 
-            Criteria critere = new Criteria();
-            critere.addEqualTo("globalKey", globalKey);
-            critere.addLike("itemKey", key);
+            Criteria criteria = new Criteria();
+            criteria.addEqualTo("globalKey", globalKey);
+            criteria.addLike("itemKey", key);
 
-            Query requete = new QueryByCriteria(OJBPropertySetItem.class, critere);
+            Query query = new QueryByCriteria(OJBPropertySetItem.class, criteria);
 
-            OJBPropertySetItem item = (OJBPropertySetItem) broker.getObjectByQuery(requete);
+            OJBPropertySetItem item = (OJBPropertySetItem) broker.getObjectByQuery(query);
 
             switch (type) {
             case PropertySet.BOOLEAN:
             log.error("Could not get value for key " + key + " of type " + type, e);
             throw new PropertyException(e.getMessage());
         } finally {
-            if (broker != null) {
-                broker.close();
-            }
+            broker.close();
         }
 
         return value;

src/test/com/opensymphony/module/propertyset/DatabaseHelper.java

         return sessionFactory;
     }
 
-    /**
-     * Use the default Hibernate *.hbm.xml files.  These build the primary keys
-     * based on an identity or sequence, whatever is native to the database.
-     * @throws Exception
-     */
-    public static void exportSchemaForHibernate() throws Exception {
-        configuration = new Configuration();
-
-        //cfg.addClass(HibernateHistoryStep.class);
-        File fileHibernatePropertySetItem = new File("src/java/com/opensymphony/module/propertyset/hibernate/PropertySetItem.hbm.xml");
-
-        Assert.assertTrue(fileHibernatePropertySetItem.exists());
-        configuration.addFile(fileHibernatePropertySetItem);
-
-		// Use SchemaExport to see what Hibernate would have created!
-        createDatabase("src/etc/deployment/hibernate/mckoi.sql");
-
-        //new SchemaExport(configuration).create(true, true);
-        sessionFactory = configuration.buildSessionFactory();
-        System.out.println("done");
-    }
-
-    private static String getDatabaseCreationScript(String scriptFile) throws Exception {
-        File file = new File(scriptFile);
-        Assert.assertTrue(file.exists());
-
-        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
-
-        return readTextStream(bis);
-    }
-
     public static void createDatabase(String scriptFile) {
         Connection connection;
         Statement statement = null;
                 }
             }
         }
+    }
+
+    /**
+     * Use the default Hibernate *.hbm.xml files.  These build the primary keys
+     * based on an identity or sequence, whatever is native to the database.
+     * @throws Exception
+     */
+    public static void exportSchemaForHibernate() throws Exception {
+        configuration = new Configuration();
+
+        //cfg.addClass(HibernateHistoryStep.class);
+        File fileHibernatePropertySetItem = new File("src/java/com/opensymphony/module/propertyset/hibernate/PropertySetItem.hbm.xml");
+
+        Assert.assertTrue(fileHibernatePropertySetItem.exists());
+        configuration.addFile(fileHibernatePropertySetItem);
 
+        // Use SchemaExport to see what Hibernate would have created!
+        createDatabase("src/etc/deployment/hibernate/mckoi.sql");
+
+        //new SchemaExport(configuration).create(true, true);
+        sessionFactory = configuration.buildSessionFactory();
+        System.out.println("done");
+    }
+
+    private static String getDatabaseCreationScript(String scriptFile) throws Exception {
+        File file = new File(scriptFile);
+        Assert.assertTrue(file.exists());
+
+        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
+
+        return readTextStream(bis);
     }
 
     private static String readTextStream(InputStream is) throws Exception {