001    package org.apache.ojb.broker.util.collections;
002    
003    /* Copyright 2003-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import java.util.Iterator;
019    import java.util.Vector;
020    import org.apache.ojb.broker.PersistenceBroker;
021    import org.apache.ojb.broker.PersistenceBrokerException;
022    
023    /**
024     * This is a set implementation that tracks removal and addition of elements.
025     * This tracking allow the PersistenceBroker to delete elements from
026     * the database that have been removed from the collection before a
027     * PB.store() orperation occurs.
028     * This will allow to use the PB api in way pretty close to ODMG persistent
029     * collections!
030     * 
031     * @author Thomas Dudziak
032     * @version $Id: RemovalAwareSet.java,v 1.1 2007-08-24 22:17:39 ewestfal Exp $
033     */
034    public class RemovalAwareSet extends ManageableHashSet implements IRemovalAwareCollection
035    {
036        private Vector allObjectsToBeRemoved = new Vector();
037    
038        /**
039         * @see org.apache.ojb.broker.ManageableCollection#afterStore(PersistenceBroker broker)
040         */
041        public void afterStore(PersistenceBroker broker) throws PersistenceBrokerException
042        {
043            // make sure allObjectsToBeRemoved does not contain
044            // any instances that got re-added to the list
045            allObjectsToBeRemoved.removeAll(this);
046    
047            Iterator iter = allObjectsToBeRemoved.iterator();
048            while (iter.hasNext())
049            {
050                broker.delete(iter.next());
051            }
052            allObjectsToBeRemoved.clear();
053        }
054    
055        protected void registerForDeletion(Object toBeRemoved)
056        {
057            //only add objects once to avoid double deletions
058            if (!allObjectsToBeRemoved.contains(toBeRemoved))
059            {
060                this.allObjectsToBeRemoved.add(toBeRemoved);
061            }
062        }
063    
064        /**
065         * @see java.util.Collection#remove(Object)
066         */
067        public boolean remove(Object o)
068        {
069            boolean result = super.remove(o);
070            registerForDeletion(o);
071            return result;
072        }
073    
074        /**
075         * @see java.util.Vector#removeAllElements()
076         */
077        public synchronized void removeAllElements()
078        {
079            for (Iterator it = iterator(); it.hasNext();)
080            {
081                registerForDeletion(it.next());
082            }
083            super.clear();
084        }
085    
086        public synchronized void clear()
087        {
088          removeAllElements();
089        }
090    
091        public void resetDeleted()
092        {
093          this.allObjectsToBeRemoved.clear();
094        }
095        
096    }