KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > j2eedo > bo > PollsSynchronizations


1 /*
2  * Speedo: an implementation of JDO compliant personality on top of JORM
3  * generic I/O sub-system. Copyright (C) 2001-2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Release: 1.0
20  *
21  * Created on Apr 8, 2004 @author fmillevi@yahoo.com
22  *
23  */

24 package org.objectweb.speedo.j2eedo.bo;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import javax.transaction.Synchronization JavaDoc;
30
31 /**
32  * The J2eedo application maintains three static lists storing the primary key
33  * of existing
34  * {@link org.objectweb.speedo.j2eedo.database.Department departments},
35  * {@link org.objectweb.speedo.j2eedo.database.Employee employees}and
36  * {@link org.objectweb.speedo.j2eedo.database.Project projects}. This class
37  * <ul>
38  * <li>Each time a new instance of this object is created the factory class
39  * has to add the id in the dedicated poll</li>
40  * <li>Each time an existing object is deleted, its id have to be remove from
41  * the poll</li>
42  * </ul>
43  * <p>
44  * This class allows the <b>synchronization</b> the <b>static pools</b> id
45  * content update and the <b>validation of the transaction</b>.
46  *
47  * @author fmillevi@yahoo.com
48  * @see DatabaseImpl
49  */

50 public class PollsSynchronizations implements Synchronization JavaDoc {
51     private ArrayList JavaDoc pools;
52     private ArrayList JavaDoc actionADDLists;
53     private ArrayList JavaDoc actionREMLists;
54     private boolean processed;
55
56     /**
57      * Do nothing
58      *
59      * @see javax.transaction.Synchronization#beforeCompletion()
60      */

61     public void beforeCompletion() {
62         // do nothing
63
}
64
65     /**
66      * The afterCompletion method has to update the pool content according the
67      * action list.
68      *
69      * @see javax.transaction.Synchronization#afterCompletion(int)
70      */

71     public void afterCompletion(int arg0) {
72         // allready processed
73
if (this.processed)
74             return;
75         this.processed = true;
76         // no pool defined
77
if (0 == this.pools.size())
78             return;
79         for (int i = 0; i < this.pools.size(); i++) {
80             // check if some action must be done
81
if (0 != ((Vector JavaDoc) this.actionADDLists.get(i)).size()
82                 || 0 != ((Vector JavaDoc) this.actionREMLists.get(i)).size()) {
83                 for (int j = 0;
84                     j < ((Vector JavaDoc) this.actionADDLists.get(i)).size();
85                     j++) {
86                     ((Vector JavaDoc) this.pools.get(i)).add(
87                         ((Vector JavaDoc) this.actionADDLists.get(i)).get(j));
88                 }
89                 for (int j = 0;
90                     j < ((Vector JavaDoc) this.actionREMLists.get(i)).size();
91                     j++) {
92                     ((Vector JavaDoc) this.pools.get(i)).remove(
93                         ((Vector JavaDoc) this.actionREMLists.get(i)).get(j));
94                 }
95             }
96         }
97     }
98
99     /**
100      * Remember the new id created
101      *
102      * @param id
103      */

104     public void addInPool(Vector JavaDoc pool, long id) {
105         int i = 0;
106         // search if the pool is in the current list
107
for (i = 0; i < this.pools.size(); i++) {
108             if (pool == (Vector JavaDoc) (this.pools.get(i)))
109                 break;
110         }
111         // the pool does not exist yet
112
if (i == this.pools.size()) {
113             this.pools.add(pool);
114             this.actionADDLists.add(new Vector JavaDoc());
115             this.actionREMLists.add(new Vector JavaDoc());
116         }
117
118         if (!((Vector JavaDoc) this.actionADDLists.get(i)).contains(new Long JavaDoc(id)))
119              ((Vector JavaDoc) this.actionADDLists.get(i)).add(new Long JavaDoc(id));
120     }
121
122     /**
123      * Remember the new id deleted
124      *
125      * @param id
126      */

127     public void removeFromPool(Vector JavaDoc pool, long id) {
128         int i = -1;
129         // search if the pool is in the current list
130
for (i = 0; i < this.pools.size(); i++) {
131             if (pool == (Vector JavaDoc) (this.pools.get(i)))
132                 break;
133         }
134         // the pool does not exist yet
135
if (i == this.pools.size()) {
136             this.pools.add(pool);
137             this.actionADDLists.add(new Vector JavaDoc());
138             this.actionREMLists.add(new Vector JavaDoc());
139         }
140
141         if (!((Vector JavaDoc) this.actionREMLists.get(i)).contains(new Long JavaDoc(id)))
142              ((Vector JavaDoc) this.actionREMLists.get(i)).add(new Long JavaDoc(id));
143     }
144
145     /**
146      * The constructor initialize the list of add and remove actions to be
147      * treated while performing the {@link #afterCompletion afterCompletion}
148      *
149      * @see org.objectweb.speedo.j2eedo.bo.DatabaseImpl#poolOfDepartmentId
150      * @see org.objectweb.speedo.j2eedo.bo.DatabaseImpl#poolOfEmployeeId
151      * @see org.objectweb.speedo.j2eedo.bo.DatabaseImpl#poolOfProjectId
152      */

153     public PollsSynchronizations() {
154         super();
155         this.pools = new ArrayList JavaDoc();
156         this.actionADDLists = new ArrayList JavaDoc();
157         this.actionREMLists = new ArrayList JavaDoc();
158         this.processed = false;
159     }
160 }
161
Popular Tags