Update of /cvsroot/roller/roller/src/org/roller/persistence
In directory sc8-pr-cvs1:/tmp/cvs-serv21369/src/org/roller/persistence
Modified Files:
PersistentObject.java
Added Files:
HierarchicalPersistentObject.java
Log Message:
FolderData now extends HierarchicalPersistentObject
Almost done!
--- NEW FILE: HierarchicalPersistentObject.java ---
/*
* Created on Jan 13, 2004
*/
package org.roller.persistence;
import org.roller.RollerException;
import org.roller.model.RollerFactory;
import org.roller.pojos.Assoc;
import java.util.Iterator;
import java.util.List;
/**
* Abstract base class for hierarchical persistent objects. Provides generic
* implementations of save and remove that know how to handle parents,
children,
* and descendents.
*
* @author David M Johnson
*/
public abstract class HierarchicalPersistentObject extends PersistentObject
{
protected HierarchicalPersistentObject mNewParent = null;
/** Create an association between object and ancestor. */
protected abstract Assoc createAssoc(
HierarchicalPersistentObject object,
HierarchicalPersistentObject ancestor,
String relation ) throws RollerException;
/** Name of association class which must implement Assoc. */
public abstract String getAssocClassName();
/** Name of object propery in association class */
public abstract String getObjectPropertyName();
/** Name of ancestor propery in association class */
public abstract String getAncestorPropertyName();
/** Save this object and ancestoral associations. */
public void save()
throws RollerException
{
boolean fresh = (getId() == null || "".equals(getId()));
PersistenceStrategy pstrategy =
RollerFactory.getRoller().getPersistenceStrategy();
pstrategy.store(this);
if (fresh)
{
// Every fresh cat needs a parent assoc
Assoc parentAssoc = createAssoc(
this, mNewParent, Assoc.PARENT);
parentAssoc.save();
}
else if (null != mNewParent)
{
// New parent must be added to parentAssoc
Assoc parentAssoc = getParentAssoc();
parentAssoc.setAncestor(mNewParent);
parentAssoc.save();
}
if (null != mNewParent)
{
// Clear out existing grandparent associations
Iterator ancestors = getAncestorAssocs().iterator();
while (ancestors.hasNext())
{
Assoc assoc = (Assoc)ancestors.next();
if (assoc.getRelation().equals(Assoc.GRANDPARENT))
{
assoc.remove();
}
}
// Walk parent assocations, creating new grandparent associations
int count = 0;
Assoc currentAssoc = getParentAssoc();
while (null != currentAssoc.getAncestor())
{
if (count > 0)
{
Assoc assoc = createAssoc(this,
currentAssoc.getAncestor(),
Assoc.GRANDPARENT);
assoc.save();
}
currentAssoc = currentAssoc.getAncestor().getParentAssoc();
count++;
}
}
// Clear new parent now that new parent has been saved
mNewParent = null;
}
/** Remove self, all decendent children and associations. */
public void remove()
throws RollerException
{
PersistenceStrategy pstrategy =
RollerFactory.getRoller().getPersistenceStrategy();
// loop to remove all of my descendent categories and associations
Iterator catIter = this.getAllDescendentAssocs().iterator();
while (catIter.hasNext())
{
Assoc assoc = (Assoc)catIter.next();
HierarchicalPersistentObject cat = assoc.getObject();
// remove my descendent's parent and grandparent associations
Iterator ancestors = cat.getAncestorAssocs().iterator();
while (ancestors.hasNext())
{
Assoc dassoc = (Assoc)ancestors.next();
dassoc.remove();
}
// remove decendent association and descendent category
assoc.remove();
//pstrategy.remove(cat);
}
// loop to remove my own parent and grandparent associations
Iterator ancestors = getAncestorAssocs().iterator();
while (ancestors.hasNext())
{
Assoc assoc = (Assoc)ancestors.next();
assoc.remove();
}
// remove myself
pstrategy.remove(this);
}
/** Query database to get parent association. */
protected Assoc getParentAssoc()
throws RollerException
{
String className = getAssocClassName();
String objectColName = getObjectPropertyName();
QueryFactory factory =
RollerFactory.getRoller().getPersistenceStrategy().getQueryFactory();
Query query = factory.createQuery(className);
Condition catCond =
factory.createCondition(objectColName, Query.EQ, this);
Condition parentCond =
factory.createCondition("relation",Query.EQ,Assoc.PARENT);
query.setWhere(factory.createCondition(catCond, Query.AND, parentCond));
List parents = query.execute();
if (parents.size() > 1)
{
throw new RollerException("ERROR: more than one parent");
}
else if (parents.size() == 1)
{
return (Assoc) parents.get(0);
}
else
{
return null;
}
}
/** Get child associations, those whose parent is this category. */
protected List getChildAssocs()
throws RollerException
{
String className = getAssocClassName();
String assocColName = getAncestorPropertyName();
QueryFactory factory =
RollerFactory
.getRoller()
.getPersistenceStrategy()
.getQueryFactory();
Query query = factory.createQuery(className);
Condition catCond = factory.createCondition(assocColName, Query.EQ,
this);
Condition parentCond =
factory.createCondition(
"relation",
Query.EQ,
Assoc.PARENT);
query.setWhere(factory.createCondition(catCond, Query.AND, parentCond));
return query.execute();
}
/**
* Get all descendent associations, those that have this category as an
* ancestor, public for testing purposes only.
*/
public List getAllDescendentAssocs()
throws RollerException
{
String className = getAssocClassName();
String assocColName = getAncestorPropertyName();
QueryFactory factory =
RollerFactory.getRoller().getPersistenceStrategy().getQueryFactory();
Query query = factory.createQuery(className);
query.setWhere(factory.createCondition(assocColName, Query.EQ, this));
return query.execute();
}
/**
* Get all ancestor associations, public for testing purposes only.
*/
public List getAncestorAssocs()
throws RollerException
{
String className = getAssocClassName();
String objectColName = getObjectPropertyName();
QueryFactory factory =
RollerFactory.getRoller().getPersistenceStrategy().getQueryFactory();
Query query = factory.createQuery(className);
query.setWhere(factory.createCondition(objectColName, Query.EQ, this));
return query.execute();
}
}
Index: PersistentObject.java
===================================================================
RCS file:
/cvsroot/roller/roller/src/org/roller/persistence/PersistentObject.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** PersistentObject.java 1 Dec 2003 01:18:50 -0000 1.1
--- PersistentObject.java 17 Jan 2004 22:46:57 -0000 1.2
***************
*** 3,6 ****
--- 3,8 ----
import org.exolab.castor.jdo.TimeStampable;
+ import org.roller.RollerException;
+ import org.roller.model.RollerFactory;
import java.io.Serializable;
***************
*** 39,42 ****
--- 41,58 ----
mTimeStamp = timeStamp;
}
+
+ public void save() throws RollerException
+ {
+ PersistenceStrategy pstrategy =
+ RollerFactory.getRoller().getPersistenceStrategy();
+ pstrategy.store(this);
+ }
+
+ public void remove() throws RollerException
+ {
+ PersistenceStrategy pstrategy =
+ RollerFactory.getRoller().getPersistenceStrategy();
+ pstrategy.remove(this);
+ }
}
-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
|