1 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 ; 11 import java.util.LinkedList ; 12 import java.util.List ; 13 14 21 public abstract class HierarchicalPersistentObject extends PersistentObject 22 { 23 protected HierarchicalPersistentObject mNewParent = null; 24 25 26 protected abstract Assoc createAssoc( 27 HierarchicalPersistentObject object, 28 HierarchicalPersistentObject ancestor, 29 String relation ) throws RollerException; 30 31 32 public abstract Class getAssocClass(); 33 34 35 public abstract String getObjectPropertyName(); 36 37 38 public abstract String getAncestorPropertyName(); 39 40 41 public abstract void setParent(HierarchicalPersistentObject parent); 42 43 protected abstract Assoc getParentAssoc() throws RollerException; 44 45 protected abstract List getChildAssocs() throws RollerException; 46 47 public abstract List getAllDescendentAssocs() throws RollerException; 48 49 public abstract List getAncestorAssocs() throws RollerException; 50 51 52 public abstract boolean isInUse() throws RollerException; 53 54 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 Assoc parentAssoc = createAssoc( 65 this, mNewParent, Assoc.PARENT); 66 parentAssoc.save(); 67 } 68 else if (null != mNewParent) 69 { 70 Assoc parentAssoc = getParentAssoc(); 72 parentAssoc.setAncestor(mNewParent); 73 parentAssoc.save(); 74 } 75 76 Iterator 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 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 Iterator children = getChildAssocs().iterator(); 115 while (children.hasNext()) 116 { 117 Assoc assoc = (Assoc) children.next(); 118 119 assoc.getObject().setParent(this); 121 122 assoc.getObject().save(); 124 } 125 126 mNewParent = null; 128 } 129 130 131 132 public void remove() throws RollerException 133 { 134 PersistenceStrategy pstrategy = 135 RollerFactory.getRoller().getPersistenceStrategy(); 136 137 List toRemove = new LinkedList (); 139 Iterator catIter = this.getAllDescendentAssocs().iterator(); 140 while (catIter.hasNext()) 141 { 142 Assoc assoc = (Assoc)catIter.next(); 143 HierarchicalPersistentObject hpo = assoc.getObject(); 144 145 Iterator ancestors = hpo.getAncestorAssocs().iterator(); 147 while (ancestors.hasNext()) 148 { 149 Assoc dassoc = (Assoc)ancestors.next(); 150 dassoc.remove(); 151 } 152 153 assoc.remove(); 155 toRemove.add(hpo); 156 } 157 Iterator removeIterator = toRemove.iterator(); 158 while (removeIterator.hasNext()) 159 { 160 PersistentObject po = (PersistentObject) removeIterator.next(); 161 removeDescendent(pstrategy, po); 162 } 163 164 Iterator ancestors = getAncestorAssocs().iterator(); 166 while (ancestors.hasNext()) 167 { 168 Assoc assoc = (Assoc)ancestors.next(); 169 assoc.remove(); 170 } 171 172 removeDescendent(pstrategy, this); 174 } 175 176 179 protected void removeDescendent( 180 PersistenceStrategy pstrategy, PersistentObject po) throws RollerException 181 { 182 pstrategy.remove(po); 183 } 184 185 186 public HierarchicalPersistentObject getNewParent() 187 { 188 return mNewParent; 189 } 190 191 } 278 | Popular Tags |