001: /*
002: * Speedo: an implementation of JDO compliant personality on top of JORM
003: * generic I/O sub-system. Copyright (C) 2001-2004 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Release: 1.0
020: *
021: * Created on Apr 8, 2004 @author fmillevi@yahoo.com
022: *
023: */
024: package org.objectweb.speedo.j2eedo.bo;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Vector;
029:
030: import javax.transaction.Synchronization;
031:
032: /**
033: * The J2eedo application maintains three static lists storing the primary key
034: * of existing
035: * {@link org.objectweb.speedo.j2eedo.database.Department departments},
036: * {@link org.objectweb.speedo.j2eedo.database.Employee employees}and
037: * {@link org.objectweb.speedo.j2eedo.database.Project projects}. This class
038: * <ul>
039: * <li>Each time a new instance of this object is created the factory class
040: * has to add the id in the dedicated poll</li>
041: * <li>Each time an existing object is deleted, its id have to be remove from
042: * the poll</li>
043: * </ul>
044: * <p>
045: * This class allows the <b>synchronization</b> the <b>static pools</b> id
046: * content update and the <b>validation of the transaction</b>.
047: *
048: * @author fmillevi@yahoo.com
049: * @see DatabaseImpl
050: */
051: public class PollsSynchronizations implements Synchronization {
052: private ArrayList pools;
053: private ArrayList actionADDLists;
054: private ArrayList actionREMLists;
055: private boolean processed;
056:
057: /**
058: * The constructor initialize the list of add and remove actions to be
059: * treated while performing the {@link #afterCompletion afterCompletion}
060: *
061: * @see org.objectweb.speedo.j2eedo.bo.DatabaseImpl#poolOfDepartmentId
062: * @see org.objectweb.speedo.j2eedo.bo.DatabaseImpl#poolOfEmployeeId
063: * @see org.objectweb.speedo.j2eedo.bo.DatabaseImpl#poolOfProjectId
064: */
065: public PollsSynchronizations() {
066: this .pools = new ArrayList();
067: this .actionADDLists = new ArrayList();
068: this .actionREMLists = new ArrayList();
069: this .processed = false;
070: }
071:
072: /**
073: * Do nothing
074: *
075: * @see javax.transaction.Synchronization#beforeCompletion()
076: */
077: public void beforeCompletion() {
078: }
079:
080: /**
081: * The afterCompletion method has to update the pool content according the
082: * action list.
083: *
084: * @see javax.transaction.Synchronization#afterCompletion(int)
085: */
086: public void afterCompletion(int arg0) {
087: if (processed)
088: return;
089: processed = true;
090: for (int i = 0; i < pools.size(); i++) {
091: Vector pool = (Vector) pools.get(i);
092: ArrayList v = (ArrayList) actionADDLists.get(i);
093: for (int j = 0; j < v.size(); j++) {
094: pool.add(v.get(j));
095: }
096: v = (ArrayList) actionREMLists.get(i);
097: for (int j = 0; j < v.size(); j++) {
098: pool.remove(v.get(j));
099: }
100: }
101: }
102:
103: /**
104: * Remember the new id created
105: *
106: * @param id
107: */
108: public void addInPool(Vector pool, long id) {
109: Collection[] ps = getPoolIndex(pool);
110: Long lid = new Long(id);
111: if (!ps[1].contains(lid)) {
112: ps[1].add(lid);
113: }
114: }
115:
116: /**
117: * Remember the new id deleted
118: *
119: * @param id
120: */
121: public void removeFromPool(Vector pool, long id) {
122: Collection[] ps = getPoolIndex(pool);
123: Long lid = new Long(id);
124: if (!ps[2].contains(lid)) {
125: ps[2].add(lid);
126: }
127: }
128:
129: private Collection[] getPoolIndex(Vector pool) {
130: int i = 0;
131: while (i < pools.size() && pool != pools.get(i)) {
132: i++;
133: }
134: if (i == pools.size()) {
135: pools.add(pool);
136: actionADDLists.add(new ArrayList());
137: actionREMLists.add(new ArrayList());
138: }
139: return new Collection[] { (Collection) pools.get(i),
140: (Collection) actionADDLists.get(i),
141: (Collection) actionREMLists.get(i) };
142: }
143:
144: }
|