1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.caleigo.core;
20
21 import java.util.*;
22
23 import org.caleigo.core.exception.*;
24
25 /*** EntityPool is the standard implementation of the IEntityPool interface.
26 * This entity collection class can store zero or more IEntity objects.
27 * The entities stored are not type specified and does not have to be defined
28 * by a single IEntityDescriptor. IEntityPool is not ordered and not accept
29 * EntityPool duplicates effectively makeing it at a "Set".
30 *
31 * @author Dennis Zikovic
32 * @version 1.00
33 *
34 *//*
35 *
36 * WHEN WHO WHY & WHAT
37 * ------------------------------------------------------------------------------
38 * 2001-10-10 Dennis Zikovic Creation
39 */
40 public class EntityPool implements IEntityPool
41 {
42
43 private Set mEntitySet;
44
45
46
47 public EntityPool()
48 {
49 mEntitySet = new HashSet();
50 }
51
52 public EntityPool(IEntityPool entityPool)
53 {
54 mEntitySet = new HashSet(entityPool.asSet());
55 }
56
57
58
59 /*** Return true if any (one or more) of the collections's contained
60 * entities has the DIRTY flag set to true that is have unsaved changes.
61 */
62 public boolean isDirty()
63 {
64 Iterator it = this.iterator();
65 boolean dirty = false;
66 while(!dirty && it.hasNext())
67 dirty = ((IEntity)it.next()).isDirty();
68 return dirty;
69 }
70
71 /*** Stores all contained entities that have the DIRTY flag set to true.
72 * The entities are stored in a single transaction meaning that if one
73 * store fails then all fails.
74 */
75 public void storeAll()
76 {
77
78 if(!this.isDirty() || this.isEmpty())
79 return;
80
81
82
83 IDataService service = ((IEntity)this.iterator().next()).getDataSource().getDataService();
84 if(service==null)
85 throw new DataServiceNotFoundException("No default data service has been set for data source: "+((IEntity)this.iterator().next()).getEntityDescriptor().getDataSourceDescriptor().getCodeName());
86
87
88 IDataTransaction trans = service.newTransaction();
89 Iterator it = this.iterator();
90 while(it.hasNext())
91 trans.addStore((IEntity)it.next());
92 trans.commit();
93 }
94
95 /*** Deletes all contained entities. The entities are deleted in a single
96 * transaction meaning that if one delete fails then all fails.
97 * USE THIS METHOD WITH CAUTION!
98 */
99 public void deleteAll()
100 {
101
102 if(this.isEmpty())
103 return;
104
105
106
107 IDataService service = ((IEntity)this.iterator().next()).getDataSource().getDataService();
108 if(service==null)
109 throw new DataServiceNotFoundException("No default data service has been set for data source: "+((IEntity)this.iterator().next()).getEntityDescriptor().getDataSourceDescriptor().getCodeName());
110
111
112 IDataTransaction trans = service.newTransaction();
113 Iterator it = this.iterator();
114 while(it.hasNext())
115 {
116 IEntity entity = (IEntity)it.next();
117 if(entity.isDirty())
118 trans.addDelete(entity);
119 }
120 trans.commit();
121 }
122
123 /*** Performs a refresh on all contained entities. The refresh is batched
124 * to save performance.
125 */
126 public void refreshAll()
127 {
128
129 if(this.isEmpty())
130 return;
131
132
133
134 IDataService service = ((IEntity)this.iterator().next()).getDataSource().getDataService();
135 if(service==null)
136 throw new DataServiceNotFoundException("No default data service has been set for data source: "+((IEntity)this.iterator().next()).getEntityDescriptor().getDataSourceDescriptor().getCodeName());
137
138
139 IDataTransaction trans = service.newTransaction();
140 Iterator it = this.iterator();
141 while(it.hasNext())
142 trans.addRefresh((IEntity)it.next());
143 trans.commit();
144 }
145
146 public boolean addEntity(IEntity entity)
147 {
148 if(this.doesAccept(entity))
149 return mEntitySet.add(entity);
150 else
151 return false;
152 }
153
154 public boolean removeEntity(IEntity entity)
155 {
156 return mEntitySet.remove(entity);
157 }
158
159 public Iterator iterator()
160 {
161 return mEntitySet.iterator();
162 }
163
164 public void clear()
165 {
166 mEntitySet.clear();
167 }
168
169 public int size()
170 {
171 return mEntitySet.size();
172 }
173
174 public boolean isEmpty()
175 {
176 return mEntitySet.isEmpty();
177 }
178
179 public boolean contains(IEntity entity)
180 {
181 return mEntitySet.contains(entity);
182 }
183
184 public boolean doesAccept(IEntity entity)
185 {
186 return !this.contains(entity);
187 }
188
189 public Set asSet()
190 {
191 return new EntityPoolSet();
192 }
193
194
195 protected class EntityPoolSet extends AbstractSet
196 {
197 public boolean add(Object obj)
198 {
199 if(obj instanceof IEntity)
200 return EntityPool.this.addEntity((IEntity)obj);
201 else
202 return false;
203 }
204
205 public int size()
206 {
207 return EntityPool.this.size();
208 }
209
210 public Iterator iterator()
211 {
212 return EntityPool.this.iterator();
213 }
214 }
215 }