| 1 24 package org.riotfamily.components.dao; 25 26 import java.util.ArrayList ; 27 import java.util.Date ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.riotfamily.cachius.Cache; 37 import org.riotfamily.common.web.event.ContentChangedEvent; 38 import org.riotfamily.components.Component; 39 import org.riotfamily.components.ComponentList; 40 import org.riotfamily.components.ComponentRepository; 41 import org.riotfamily.components.ComponentVersion; 42 import org.riotfamily.components.Location; 43 import org.riotfamily.components.PropertyProcessor; 44 import org.riotfamily.components.VersionContainer; 45 import org.riotfamily.riot.security.AccessController; 46 import org.springframework.beans.factory.InitializingBean; 47 import org.springframework.context.event.ApplicationEventMulticaster; 48 import org.springframework.util.Assert; 49 50 56 public abstract class AbstractComponentDao implements ComponentDao, 57 InitializingBean { 58 59 private static final Log log = LogFactory.getLog(AbstractComponentDao.class); 60 61 private ComponentRepository repository; 62 63 private Cache cache; 64 65 private ApplicationEventMulticaster eventMulticaster; 66 67 public AbstractComponentDao() { 68 } 69 70 public void setRepository(ComponentRepository repository) { 71 this.repository = repository; 72 } 73 74 public void setCache(Cache cache) { 75 this.cache = cache; 76 } 77 78 public void setEventMulticaster(ApplicationEventMulticaster eventMulticaster) { 79 this.eventMulticaster = eventMulticaster; 80 } 81 82 public void afterPropertiesSet() throws Exception { 83 Assert.notNull(repository, "A ComponentRepository must be set."); 84 initDao(); 85 } 86 87 protected void initDao() { 88 } 89 90 93 public ComponentList loadComponentList(Long id) { 94 return (ComponentList) loadObject(ComponentList.class, id); 95 } 96 97 100 public VersionContainer loadVersionContainer(Long id) { 101 return (VersionContainer) loadObject(VersionContainer.class, id); 102 } 103 104 108 public ComponentVersion loadComponentVersion(Long id) { 109 return (ComponentVersion) loadObject(ComponentVersion.class, id); 110 } 111 112 115 public void saveComponentList(ComponentList list) { 116 list.setLastModified(new Date ()); 117 list.setLastModifiedBy(AccessController.getCurrentUser().getUserId()); 118 saveObject(list); 119 } 120 121 124 public void updateComponentList(ComponentList list) { 125 if (list.getId() != null) { 126 list.setLastModified(new Date ()); 127 list.setLastModifiedBy(AccessController.getCurrentUser().getUserId()); 128 updateObject(list); 129 } 130 } 131 132 135 public void updateVersionContainer(VersionContainer container) { 136 if (container.getId() != null) { 137 updateObject(container); 138 } 139 } 140 141 144 public void updateComponentVersion(ComponentVersion version) { 145 if (version.getId() != null) { 146 updateObject(version); 147 } 148 } 149 150 157 public List getOrCreatePreviewContainers(ComponentList list) { 158 List previewContainers = list.getPreviewContainers(); 159 if (!list.isDirty()) { 160 if (previewContainers == null) { 161 previewContainers = new ArrayList (); 162 } 163 else { 164 previewContainers.clear(); 165 } 166 List liveContainers = list.getLiveContainers(); 167 if (liveContainers != null) { 168 previewContainers.addAll(liveContainers); 169 } 170 list.setPreviewContainers(previewContainers); 171 list.setDirty(true); 172 } 173 return previewContainers; 174 } 175 176 183 public ComponentVersion getOrCreateVersion( 184 VersionContainer container, String type, boolean live) { 185 186 if (live) { 187 ComponentVersion liveVersion = container.getLiveVersion(); 188 if (liveVersion == null) { 189 liveVersion = new ComponentVersion(type); 190 container.setLiveVersion(liveVersion); 191 saveObject(liveVersion); 192 updateVersionContainer(container); 193 } 194 return liveVersion; 195 } 196 else { 197 ComponentList list = container.getList(); 198 if (list != null && !list.isDirty()) { 199 getOrCreatePreviewContainers(list); 200 updateComponentList(list); 201 } 202 ComponentVersion previewVersion = container.getPreviewVersion(); 203 if (previewVersion == null) { 204 ComponentVersion liveVersion = container.getLiveVersion(); 205 if (liveVersion != null) { 206 previewVersion = copyComponentVersion(liveVersion); 207 } 208 else { 209 previewVersion = new ComponentVersion(type); 210 saveObject(previewVersion); 211 } 212 container.setPreviewVersion(previewVersion); 213 updateVersionContainer(container); 214 } 215 return previewVersion; 216 } 217 } 218 219 222 public VersionContainer insertContainer(ComponentList componentList, 223 String type, Map properties, int position, boolean live) { 224 225 List containers = live 226 ? componentList.getLiveContainers() 227 : getOrCreatePreviewContainers(componentList); 228 229 VersionContainer container = createVersionContainer(type, properties, live); 230 container.setList(componentList); 231 232 if (position >= 0) { 233 containers.add(position, container); 234 } 235 else { 236 containers.add(container); 237 } 238 239 if (!live) { 240 componentList.setDirty(true); 241 } 242 243 updateComponentList(componentList); 244 return container; 245 } 246 247 255 private VersionContainer createVersionContainer( 256 String type, Map properties, boolean live) { 257 258 VersionContainer container = new VersionContainer(); 259 ComponentVersion version = new ComponentVersion(type); 260 version.setProperties(properties); 261 saveObject(version); 262 if (live) { 263 container.setLiveVersion(version); 264 } 265 else { 266 container.setPreviewVersion(version); 267 } 268 saveObject(container); 269 return container; 270 } 271 272 276 public void deleteComponentLists(String type, String path) { 277 List componentLists = findComponentLists(type, path); 278 Iterator it = componentLists.iterator(); 279 while (it.hasNext()) { 280 ComponentList list = (ComponentList) it.next(); 281 deleteComponentList(list); 282 } 283 } 284 285 288 public void deleteComponentList(ComponentList list) { 289 List previewList = list.getPreviewContainers(); 290 List liveList = list.getLiveContainers(); 291 if (liveList != null) { 292 Iterator it = liveList.listIterator(); 293 while (it.hasNext()) { 294 VersionContainer component = (VersionContainer) it.next(); 295 if (previewList == null || !previewList.contains(component)) { 296 deleteVersionContainer(component); 297 } 298 it.remove(); 299 } 300 } 301 if (previewList != null) { 302 Iterator it = previewList.listIterator(); 303 while (it.hasNext()) { 304 deleteVersionContainer((VersionContainer) it.next()); 305 it.remove(); 306 } 307 } 308 deleteObject(list); 309 } 310 311 314 public void deleteVersionContainer(VersionContainer container) { 315 Iterator it = container.getChildLists().iterator(); 316 while (it.hasNext()) { 317 deleteComponentList((ComponentList) it.next()); 318 } 319 deleteComponentVersion(container.getLiveVersion()); 320 deleteComponentVersion(container.getPreviewVersion()); 321 deleteObject(container); 322 } 323 324 327 public void deleteComponentVersion(ComponentVersion version) { 328 if (version != null) { 329 Component component = repository.getComponent(version); 330 Iterator it = component.getPropertyProcessors().iterator(); 331 while (it.hasNext()) { 332 PropertyProcessor pp = (PropertyProcessor) it.next(); 333 pp.delete(version.getProperties()); 334 } 335 deleteObject(version); 336 } 337 } 338 339 343 public boolean publishContainer(VersionContainer container) { 344 boolean published = false; 345 Set childLists = container.getChildLists(); 346 if (childLists != null) { 347 Iterator it = childLists.iterator(); 348 while (it.hasNext()) { 349 ComponentList childList = (ComponentList) it.next(); 350 published |= publishList(childList); 351 } 352 } 353 ComponentVersion preview = container.getPreviewVersion(); 354 if (preview != null) { 355 ComponentVersion liveVersion = container.getLiveVersion(); 356 if (liveVersion != null) { 357 deleteComponentVersion(liveVersion); 358 } 359 container.setLiveVersion(preview); 360 container.setPreviewVersion(null); 361 updateVersionContainer(container); 362 published = true; 363 if (container.getList() == null) { 364 cache.invalidateTaggedItems(VersionContainer.class.getName() 365 + '#' + container.getId()); 366 } 367 } 368 return published; 369 } 370 371 375 public boolean publishList(ComponentList componentList) { 376 boolean published = false; 377 if (componentList.isDirty()) { 378 log.debug("List " + componentList + " is dirty and will be published."); 379 published = true; 380 List previewList = componentList.getPreviewContainers(); 381 List liveList = componentList.getLiveContainers(); 382 if (liveList == null) { 383 liveList = new ArrayList (); 384 } 385 else { 386 Iterator it = liveList.iterator(); 387 while (it.hasNext()) { 388 VersionContainer container = (VersionContainer) it.next(); 389 if (!previewList.contains(container)) { 390 deleteVersionContainer(container); 391 it.remove(); 392 } 393 } 394 liveList.clear(); 395 } 396 liveList.addAll(previewList); 397 previewList.clear(); 398 componentList.setDirty(false); 399 updateComponentList(componentList); 400 } 401 402 Iterator it = componentList.getLiveContainers().iterator(); 403 while (it.hasNext()) { 404 VersionContainer container = (VersionContainer) it.next(); 405 published |= publishContainer(container); 406 } 407 408 if (eventMulticaster != null) { 409 String path = repository.getUrl(componentList); 410 eventMulticaster.multicastEvent(new ContentChangedEvent(this, path)); 411 } 412 413 if (published && cache != null) { 414 String tag = componentList.getLocation().toString(); 415 log.debug("Invalidating items tagged as " + tag); 416 cache.invalidateTaggedItems(tag); 417 } 418 419 return published; 420 } 421 422 425 public boolean discardList(ComponentList componentList) { 426 boolean discarded = false; 427 List previewList = componentList.getPreviewContainers(); 428 List liveList = componentList.getLiveContainers(); 429 if (componentList.isDirty()) { 430 discarded = true; 431 componentList.setPreviewContainers(null); 432 componentList.setDirty(false); 433 Iterator it = previewList.iterator(); 434 while (it.hasNext()) { 435 VersionContainer container = (VersionContainer) it.next(); 436 if (liveList == null || !liveList.contains(container)) { 437 deleteVersionContainer(container); 438 it.remove(); 439 } 440 } 441 updateComponentList(componentList); 442 } 443 Iterator it = liveList.iterator(); 444 while (it.hasNext()) { 445 VersionContainer container = (VersionContainer) it.next(); 446 discarded |= discardContainer(container); 447 } 448 return discarded; 449 } 450 451 455 public boolean discardContainer(VersionContainer container) { 456 boolean discarded = false; 457 Set childLists = container.getChildLists(); 458 if (childLists != null) { 459 Iterator it = childLists.iterator(); 460 while (it.hasNext()) { 461 ComponentList childList = (ComponentList) it.next(); 462 discarded |= discardList(childList); 463 } 464 } 465 ComponentVersion preview = container.getPreviewVersion(); 466 if (preview != null) { 467 container.setPreviewVersion(null); 468 updateVersionContainer(container); 469 deleteComponentVersion(preview); 470 discarded = true; 471 } 472 return discarded; 473 } 474 475 479 public void copyComponentLists(String type, String oldPath, String newPath) { 480 List lists = findComponentLists(type, oldPath); 481 if (lists != null) { 482 Iterator it = lists.iterator(); 483 while (it.hasNext()) { 484 ComponentList list = (ComponentList) it.next(); 485 ComponentList copy = copyComponentList(list, newPath); 486 saveComponentList(copy); 487 } 488 } 489 } 490 491 private ComponentList copyComponentList(ComponentList list, String path) { 492 ComponentList copy = new ComponentList(); 493 Location location = new Location(list.getLocation()); 494 location.setPath(path); 495 copy.setLocation(location); 496 copy.setDirty(list.isDirty()); 497 copy.setLiveContainers(copyContainers(list.getLiveContainers(), path)); 498 copy.setPreviewContainers(copyContainers(list.getPreviewContainers(), path)); 499 return copy; 500 } 501 502 private List copyContainers(List source, String path) { 503 if (source == null) { 504 return null; 505 } 506 List dest = new ArrayList (source.size()); 507 Iterator it = source.iterator(); 508 while (it.hasNext()) { 509 VersionContainer container = (VersionContainer) it.next(); 510 dest.add(copyVersionContainer(container, path)); 511 } 512 return dest; 513 } 514 515 private VersionContainer copyVersionContainer(VersionContainer container, String path) { 516 VersionContainer copy = new VersionContainer(); 517 if (container.getLiveVersion() != null) { 518 copy.setLiveVersion(copyComponentVersion( 519 container.getLiveVersion())); 520 } 521 if (container.getPreviewVersion() != null) { 522 copy.setPreviewVersion(copyComponentVersion( 523 container.getPreviewVersion())); 524 } 525 Set childLists = container.getChildLists(); 526 if (childLists != null) { 527 HashSet clonedLists = new HashSet (); 528 Iterator it = childLists.iterator(); 529 while (it.hasNext()) { 530 ComponentList list = (ComponentList) it.next(); 531 ComponentList clonedList = copyComponentList(list, path); 532 clonedList.setParent(copy); 533 clonedLists.add(clonedList); 534 } 535 copy.setChildLists(clonedLists); 536 } 537 return copy; 538 } 539 540 private ComponentVersion copyComponentVersion(ComponentVersion version) { 541 Component component = repository.getComponent(version); 542 ComponentVersion copy = new ComponentVersion(version); 543 if (component.getPropertyProcessors() != null) { 544 Iterator it = component.getPropertyProcessors().iterator(); 545 while (it.hasNext()) { 546 PropertyProcessor pp = (PropertyProcessor) it.next(); 547 pp.copy(version.getProperties(), copy.getProperties()); 548 } 549 } 550 return copy; 551 } 552 553 protected abstract Object loadObject(Class clazz, Long id); 554 555 protected abstract void saveObject(Object object); 556 557 protected abstract void updateObject(Object object); 558 559 protected abstract void deleteObject(Object object); 560 561 } 562 | Popular Tags |