001 package org.apache.ojb.broker.util.collections;
002
003 /* Copyright 2004-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 org.apache.ojb.broker.PersistenceBroker;
019 import org.apache.ojb.broker.PersistenceBrokerException;
020
021 import java.util.Iterator;
022 import java.util.Vector;
023
024 /**
025 * This is a list that tracks removal and addition of elements.
026 * This tracking allow the PersistenceBroker to delete elements from
027 * the database that have been removed from the collection before a
028 * PB.store() orperation occurs.
029 * This will allow to use the PB api in way pretty close to ODMG persistent
030 * collections!
031 * @author Thomas Mahler, adapted to ManageableArrayList by Edson C. E. Richter
032 * @version $Id: RemovalAwareList.java,v 1.1 2007-08-24 22:17:39 ewestfal Exp $
033 */
034 public class RemovalAwareList extends ManageableArrayList 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 /**
056 * @see java.util.List#remove(int)
057 */
058 public Object remove(int index)
059 {
060 Object toBeRemoved = super.remove(index);
061 registerForDeletion(toBeRemoved);
062 return toBeRemoved;
063 }
064
065 protected void registerForDeletion(Object toBeRemoved)
066 {
067 //only add objects once to avoid double deletions
068 if (!allObjectsToBeRemoved.contains(toBeRemoved))
069 {
070 this.allObjectsToBeRemoved.add(toBeRemoved);
071 }
072 }
073
074 /**
075 * @see java.util.Collection#remove(Object)
076 */
077 public boolean remove(Object o)
078 {
079 boolean result = super.remove(o);
080 registerForDeletion(o);
081 return result;
082 }
083
084 /**
085 * @see java.util.Vector#removeAllElements()
086 */
087 public synchronized void removeAllElements()
088 {
089 for (int i = 0; i < this.size(); i++)
090 {
091 registerForDeletion(this.get(i));
092 }
093 super.clear();
094 }
095
096
097 /**
098 * @see java.util.Vector#removeElementAt(int)
099 */
100 public synchronized void removeElementAt(int index)
101 {
102 Object toBeDeleted = this.get(index);
103 registerForDeletion(toBeDeleted);
104 super.remove(index);
105 }
106
107 public synchronized void clear() {
108 removeAllElements();
109 }
110
111 /**
112 * @see java.util.AbstractList#removeRange(int, int)
113 */
114 protected void removeRange(int fromIndex, int toIndex)
115 {
116 for (int i = fromIndex; i < toIndex; i++)
117 {
118 registerForDeletion(this.get(i));
119 }
120 super.removeRange(fromIndex, toIndex);
121 }
122
123 public void resetDeleted() {
124 this.allObjectsToBeRemoved.clear();
125 }
126
127 }