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.4.2.1 2005/12/21 22:28:15 tomdz Exp $
033: */
034: public class RemovalAwareList extends ManageableArrayList implements
035: IRemovalAwareCollection {
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)
042: throws PersistenceBrokerException {
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: broker.delete(iter.next());
050: }
051: allObjectsToBeRemoved.clear();
052: }
053:
054: /**
055: * @see java.util.List#remove(int)
056: */
057: public Object remove(int index) {
058: Object toBeRemoved = super .remove(index);
059: registerForDeletion(toBeRemoved);
060: return toBeRemoved;
061: }
062:
063: protected void registerForDeletion(Object toBeRemoved) {
064: //only add objects once to avoid double deletions
065: if (!allObjectsToBeRemoved.contains(toBeRemoved)) {
066: this .allObjectsToBeRemoved.add(toBeRemoved);
067: }
068: }
069:
070: /**
071: * @see java.util.Collection#remove(Object)
072: */
073: public boolean remove(Object o) {
074: boolean result = super .remove(o);
075: registerForDeletion(o);
076: return result;
077: }
078:
079: /**
080: * @see java.util.Vector#removeAllElements()
081: */
082: public synchronized void removeAllElements() {
083: for (int i = 0; i < this .size(); i++) {
084: registerForDeletion(this .get(i));
085: }
086: super .clear();
087: }
088:
089: /**
090: * @see java.util.Vector#removeElementAt(int)
091: */
092: public synchronized void removeElementAt(int index) {
093: Object toBeDeleted = this .get(index);
094: registerForDeletion(toBeDeleted);
095: super .remove(index);
096: }
097:
098: public synchronized void clear() {
099: removeAllElements();
100: }
101:
102: /**
103: * @see java.util.AbstractList#removeRange(int, int)
104: */
105: protected void removeRange(int fromIndex, int toIndex) {
106: for (int i = fromIndex; i < toIndex; i++) {
107: registerForDeletion(this .get(i));
108: }
109: super .removeRange(fromIndex, toIndex);
110: }
111:
112: public void resetDeleted() {
113: this.allObjectsToBeRemoved.clear();
114: }
115:
116: }
|