View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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      // Data members ------------------------------------------------------------
43      private Set mEntitySet;
44  
45      // Constructors ------------------------------------------------------------
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      // IEntityPool implementation ----------------------------------------------
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          // Break if no dirty entity exists.
78          if(!this.isDirty() || this.isEmpty())
79              return;
80          
81          // Extract default service and break if none is defined.
82          // !! Note that enties with different data sources is not supported. !!
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          // Store all entities in a single transaction
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         // Break if no entities exists.
102         if(this.isEmpty())
103             return;
104         
105         // Extract default service and break if none is defined.
106         // !! Note that enties with different data sources is not supported. !!
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         // Store all entities in a single transaction
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         // Break if no entities exists.
129         if(this.isEmpty())
130             return;
131         
132         // Extract default service and break if none is defined.
133         // !! Note that enties with different data sources is not supported. !!
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         // Store all entities in a single transaction
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     // Nested classes ----------------------------------------------------------
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 }