KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > pojos > HierarchicalPersistentObject


1 /*
2  * Created on Jan 13, 2004
3  */

4 package org.roller.pojos;
5
6 import org.roller.RollerException;
7 import org.roller.business.PersistenceStrategy;
8 import org.roller.model.RollerFactory;
9
10 import java.util.Iterator JavaDoc;
11 import java.util.LinkedList JavaDoc;
12 import java.util.List JavaDoc;
13
14 /**
15  * Abstract base class for hierarchical persistent objects. Provides generic
16  * implementations of save and remove that know how to handle parents, children,
17  * and descendents.
18  *
19  * @author David M Johnson
20  */

21 public abstract class HierarchicalPersistentObject extends PersistentObject
22 {
23     protected HierarchicalPersistentObject mNewParent = null;
24     
25     /** Create an association between object and ancestor. */
26     protected abstract Assoc createAssoc(
27         HierarchicalPersistentObject object,
28         HierarchicalPersistentObject ancestor,
29         String JavaDoc relation ) throws RollerException;
30         
31     /** Name of association class which must implement Assoc. */
32     public abstract Class JavaDoc getAssocClass();
33     
34     /** Name of object propery in association class */
35     public abstract String JavaDoc getObjectPropertyName();
36     
37     /** Name of ancestor propery in association class */
38     public abstract String JavaDoc getAncestorPropertyName();
39     
40     /** Set new parent - invalidates getPath() until object is saved(). */
41     public abstract void setParent(HierarchicalPersistentObject parent);
42     
43     protected abstract Assoc getParentAssoc() throws RollerException;
44
45     protected abstract List JavaDoc getChildAssocs() throws RollerException;
46     
47     public abstract List JavaDoc getAllDescendentAssocs() throws RollerException;
48     
49     public abstract List JavaDoc getAncestorAssocs() throws RollerException;
50     
51     /** Returns true if this object is in use and should not be deleted */
52     public abstract boolean isInUse() throws RollerException;
53
54     /** Save this object and ancestoral associations. */
55     public void save() throws RollerException
56     {
57         boolean fresh = (getId() == null || "".equals(getId()));
58         PersistenceStrategy pstrategy =
59             RollerFactory.getRoller().getPersistenceStrategy();
60         pstrategy.store(this);
61         if (fresh)
62         {
63             // Every fresh cat needs a parent assoc
64
Assoc parentAssoc = createAssoc(
65                 this, mNewParent, Assoc.PARENT);
66             parentAssoc.save();
67         }
68         else if (null != mNewParent)
69         {
70             // New parent must be added to parentAssoc
71
Assoc parentAssoc = getParentAssoc();
72             parentAssoc.setAncestor(mNewParent);
73             parentAssoc.save();
74         }
75         
76         // Clear out existing grandparent associations
77
Iterator JavaDoc ancestors = getAncestorAssocs().iterator();
78         while (ancestors.hasNext())
79         {
80             Assoc assoc = (Assoc)ancestors.next();
81             if (assoc.getRelation().equals(Assoc.GRANDPARENT))
82             {
83                 assoc.remove();
84             }
85         }
86         
87         // Walk parent assocations, creating new grandparent associations
88
int count = 0;
89         Assoc currentAssoc = getParentAssoc();
90         while (null != currentAssoc.getAncestor())
91         {
92             if (count > 0)
93             {
94                 Assoc assoc = createAssoc(this,
95                     currentAssoc.getAncestor(),
96                     Assoc.GRANDPARENT);
97                 assoc.save();
98             }
99             currentAssoc = currentAssoc.getAncestor().getParentAssoc();
100             count++;
101         }
102         
103         // all descendents must also reset their ancestor links
104
// Query childQuery =
105
// pstrategy.getQueryFactory().createQuery(getAssocClass());
106
// childQuery.setWhere(
107
// pstrategy.getQueryFactory().createCondition(
108
// pstrategy.getQueryFactory().createCondition(
109
// getAncestorPropertyName(), Query.EQ, this),
110
// Query.AND,
111
// pstrategy.getQueryFactory().createCondition(
112
// "relation", Query.EQ, Assoc.PARENT)));
113
//
114
Iterator JavaDoc children = getChildAssocs().iterator();
115         while (children.hasNext())
116         {
117             Assoc assoc = (Assoc) children.next();
118             
119             // resetting parent will cause reset of ancestors links
120
assoc.getObject().setParent(this);
121             
122             // recursively...
123
assoc.getObject().save();
124         }
125         
126         // Clear new parent now that new parent has been saved
127
mNewParent = null;
128     }
129
130
131     /** Remove self, all decendent children and associations. */
132     public void remove() throws RollerException
133     {
134         PersistenceStrategy pstrategy =
135             RollerFactory.getRoller().getPersistenceStrategy();
136
137         // loop to remove all of my descendents and associations
138
List JavaDoc toRemove = new LinkedList JavaDoc();
139         Iterator JavaDoc catIter = this.getAllDescendentAssocs().iterator();
140         while (catIter.hasNext())
141         {
142             Assoc assoc = (Assoc)catIter.next();
143             HierarchicalPersistentObject hpo = assoc.getObject();
144             
145             // remove my descendent's parent and grandparent associations
146
Iterator JavaDoc ancestors = hpo.getAncestorAssocs().iterator();
147             while (ancestors.hasNext())
148             {
149                 Assoc dassoc = (Assoc)ancestors.next();
150                 dassoc.remove();
151             }
152             
153             // remove decendent association and descendents
154
assoc.remove();
155             toRemove.add(hpo);
156         }
157         Iterator JavaDoc removeIterator = toRemove.iterator();
158         while (removeIterator.hasNext())
159         {
160             PersistentObject po = (PersistentObject) removeIterator.next();
161             removeDescendent(pstrategy, po);
162         }
163
164         // loop to remove my own parent and grandparent associations
165
Iterator JavaDoc ancestors = getAncestorAssocs().iterator();
166         while (ancestors.hasNext())
167         {
168             Assoc assoc = (Assoc)ancestors.next();
169             assoc.remove();
170         }
171         
172         // remove myself
173
removeDescendent(pstrategy, this);
174     }
175
176     /**
177      * Override this if you want to handle descendant removal yourself.
178      */

179     protected void removeDescendent(
180        PersistenceStrategy pstrategy, PersistentObject po) throws RollerException
181     {
182         pstrategy.remove(po);
183     }
184
185     /** Should be needed only be manager objects */
186     public HierarchicalPersistentObject getNewParent()
187     {
188         return mNewParent;
189     }
190     
191 // /** Query database to get parent association. */
192
// protected Assoc getParentAssoc()
193
// throws RollerException
194
// {
195
// Class clazz = getAssocClass();
196
// String objectColName = getObjectPropertyName();
197
//
198
// QueryFactory factory =
199
// RollerFactory.getRoller().getPersistenceStrategy().getQueryFactory();
200
// Query query = factory.createQuery(clazz);
201
//
202
// Condition catCond =
203
// factory.createCondition(objectColName, Query.EQ, this);
204
// Condition parentCond =
205
// factory.createCondition("relation",Query.EQ,Assoc.PARENT);
206
// query.setWhere(factory.createCondition(catCond, Query.AND, parentCond));
207
// List parents = query.execute();
208
//
209
// if (parents.size() > 1)
210
// {
211
// throw new RollerException("ERROR: more than one parent");
212
// }
213
// else if (parents.size() == 1)
214
// {
215
// return (Assoc) parents.get(0);
216
// }
217
// else
218
// {
219
// return null;
220
// }
221
// }
222
//
223
// /** Get child associations, those whose parent is this category. */
224
// protected List getChildAssocs()
225
// throws RollerException
226
// {
227
// Class clazz = getAssocClass();
228
// String assocColName = getAncestorPropertyName();
229
//
230
// QueryFactory factory =
231
// RollerFactory
232
// .getRoller()
233
// .getPersistenceStrategy()
234
// .getQueryFactory();
235
// Query query = factory.createQuery(clazz);
236
//
237
// Condition catCond =
238
// factory.createCondition(assocColName, Query.EQ, this);
239
//
240
// Condition parentCond =
241
// factory.createCondition("relation", Query.EQ, Assoc.PARENT);
242
//
243
// query.setWhere(factory.createCondition(catCond, Query.AND, parentCond));
244
// return query.execute();
245
// }
246
//
247
// /**
248
// * Get all descendent associations, those that have this category as an
249
// * ancestor, public for testing purposes only.
250
// */
251
// public List getAllDescendentAssocs()
252
// throws RollerException
253
// {
254
// Class clazz = getAssocClass();
255
// String assocColName = getAncestorPropertyName();
256
// QueryFactory factory =
257
// RollerFactory.getRoller().getPersistenceStrategy().getQueryFactory();
258
// Query query = factory.createQuery(clazz);
259
// query.setWhere(factory.createCondition(assocColName, Query.EQ, this));
260
// return query.execute();
261
// }
262
//
263
// /**
264
// * Get all ancestor associations, public for testing purposes only.
265
// */
266
// public List getAncestorAssocs()
267
// throws RollerException
268
// {
269
// Class clazz = getAssocClass();
270
// String objectColName = getObjectPropertyName();
271
// QueryFactory factory =
272
// RollerFactory.getRoller().getPersistenceStrategy().getQueryFactory();
273
// Query query = factory.createQuery(clazz);
274
// query.setWhere(factory.createCondition(objectColName, Query.EQ, this));
275
// return query.execute();
276
// }
277
}
278
Popular Tags