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 org.apache.ojb.broker.PersistenceBroker;
019: import org.apache.ojb.broker.PersistenceBrokerException;
020: import org.apache.ojb.broker.metadata.ClassDescriptor;
021:
022: import java.util.Iterator;
023: import java.util.Vector;
024:
025: /**
026: * This is a collection that tracks removal and addition of elements.
027: * This tracking allow the PersistenceBroker to delete elements from
028: * the database that have been removed from the collection before a
029: * PB.store() orperation occurs.
030: * This will allow to use the PB api in way pretty close to ODMG persistent
031: * collections!
032: * @author Thomas Mahler
033: * @version $Id: RemovalAwareCollection.java,v 1.7.2.3 2005/12/21 22:28:15 tomdz Exp $
034: */
035: public class RemovalAwareCollection extends ManageableVector implements
036: IRemovalAwareCollection {
037: private Vector allObjectsToBeRemoved = new Vector();
038:
039: /**
040: * @see org.apache.ojb.broker.ManageableCollection#afterStore(PersistenceBroker broker)
041: */
042: public void afterStore(PersistenceBroker broker)
043: throws PersistenceBrokerException {
044: // make sure allObjectsToBeRemoved does not contain
045: // any instances that got re-added to the list
046: allObjectsToBeRemoved.removeAll(this );
047:
048: Iterator iter = allObjectsToBeRemoved.iterator();
049: while (iter.hasNext()) {
050: Object obj = iter.next();
051: ClassDescriptor cld = broker.getClassDescriptor(obj
052: .getClass());
053: if (broker.serviceBrokerHelper().assertValidPkForDelete(
054: cld, obj)) {
055: broker.delete(obj);
056: }
057: }
058: allObjectsToBeRemoved.clear();
059: }
060:
061: /**
062: * @see java.util.List#remove(int)
063: */
064: public Object remove(int index) {
065: Object toBeRemoved = super .remove(index);
066: registerForDeletion(toBeRemoved);
067: return toBeRemoved;
068: }
069:
070: protected void registerForDeletion(Object toBeRemoved) {
071: //only add objects once to avoid double deletions
072: if (!allObjectsToBeRemoved.contains(toBeRemoved)) {
073: this .allObjectsToBeRemoved.add(toBeRemoved);
074: }
075: }
076:
077: /**
078: * @see java.util.Collection#remove(Object)
079: */
080: public boolean remove(Object o) {
081: boolean result = super .remove(o);
082: registerForDeletion(o);
083: return result;
084: }
085:
086: /**
087: * @see java.util.Vector#clear()
088: */
089: public synchronized void clear() {
090: removeAllElements();
091: }
092:
093: /**
094: * @see java.util.Vector#removeAllElements()
095: */
096: public synchronized void removeAllElements() {
097: for (int i = 0; i < this .size(); i++) {
098: registerForDeletion(this .get(i));
099: }
100: super .removeAllElements();
101: }
102:
103: /**
104: * @see java.util.Vector#removeElementAt(int)
105: */
106: public synchronized void removeElementAt(int index) {
107: Object toBeDeleted = this .get(index);
108: registerForDeletion(toBeDeleted);
109: super .removeElementAt(index);
110: }
111:
112: /**
113: * @see java.util.AbstractList#removeRange(int, int)
114: */
115: protected void removeRange(int fromIndex, int toIndex) {
116: for (int i = fromIndex; i < toIndex; i++) {
117: registerForDeletion(this.get(i));
118: }
119: super.removeRange(fromIndex, toIndex);
120: }
121:
122: }
|