1 24 package org.riotfamily.pages.riot.dao; 25 26 import java.util.Collection ; 27 import java.util.List ; 28 import java.util.Locale ; 29 30 import org.riotfamily.pages.Page; 31 import org.riotfamily.pages.PageNode; 32 import org.riotfamily.pages.Site; 33 import org.riotfamily.pages.dao.PageDao; 34 import org.riotfamily.riot.dao.CutAndPasteEnabledDao; 35 import org.riotfamily.riot.dao.ListParams; 36 import org.riotfamily.riot.dao.ParentChildDao; 37 import org.riotfamily.riot.dao.SwappableItemDao; 38 import org.springframework.beans.factory.InitializingBean; 39 import org.springframework.dao.DataAccessException; 40 import org.springframework.util.Assert; 41 42 46 public class PageRiotDao implements ParentChildDao, SwappableItemDao, 47 CutAndPasteEnabledDao, InitializingBean { 48 49 private PageDao pageDao; 50 51 private Locale masterLocale; 52 53 public PageRiotDao() { 54 } 55 56 public void setPageDao(PageDao pageDao) { 57 this.pageDao = pageDao; 58 } 59 60 public void afterPropertiesSet() throws Exception { 61 Assert.notNull(pageDao, "A PageDao must be set."); 62 } 63 64 public Locale getMasterLocale() { 65 return this.masterLocale; 66 } 67 68 public void setMasterLocale(Locale masterLocale) { 69 this.masterLocale = masterLocale; 70 } 71 72 public Object getParent(Object entity) { 73 Page page = (Page) entity; 74 Page parentPage = page.getParentPage(); 75 if (parentPage != null) { 76 return parentPage; 77 } 78 return new SiteLocale(page.getNode().getSite(), page.getLocale()); 79 } 80 81 public void delete(Object entity, Object parent) throws DataAccessException { 82 Page page = (Page) entity; 83 pageDao.deletePage(page); 84 } 85 86 public Class getEntityClass() { 87 return Page.class; 88 } 89 90 public int getListSize(Object parent, ListParams params) 91 throws DataAccessException { 92 93 return -1; 94 } 95 96 public String getObjectId(Object entity) { 97 Page page = (Page) entity; 98 return page.getId().toString(); 99 } 100 101 public Collection list(Object parent, ListParams params) 102 throws DataAccessException { 103 104 if (parent instanceof Page) { 105 Page parentPage = (Page) parent; 106 return parentPage.getChildPages(masterLocale); 107 } 108 else { 109 Site site = null; 110 Locale locale = null; 111 if (parent instanceof SiteLocale) { 112 SiteLocale siteLocale = (SiteLocale) parent; 113 site = siteLocale.getSite(); 114 locale = siteLocale.getLocale(); 115 } 116 else { 117 site = pageDao.getDefaultSite(); 118 locale = getFixedLocale(); 119 } 120 return pageDao.findRootNode(site).getChildPages(locale, masterLocale); 121 } 122 } 123 124 public Object load(String id) throws DataAccessException { 125 return pageDao.loadPage(new Long (id)); 126 } 127 128 public void save(Object entity, Object parent) throws DataAccessException { 129 Page page = (Page) entity; 130 if (parent instanceof Page) { 131 Page parentPage = (Page) parent; 132 pageDao.savePage(parentPage, page); 133 } 134 else { 135 Site site = null; 136 Locale locale = null; 137 if (parent instanceof SiteLocale) { 138 SiteLocale siteLocale = (SiteLocale) parent; 139 site = siteLocale.getSite(); 140 locale = siteLocale.getLocale(); 141 } 142 else { 143 site = pageDao.getDefaultSite(); 144 locale = getFixedLocale(); 145 } 146 page.setLocale(locale); 147 pageDao.savePage(site, page); 148 } 149 } 150 151 public void update(Object entity) throws DataAccessException { 152 pageDao.updatePage((Page) entity); 153 } 154 155 public void swapEntity(Object entity, Object parent, ListParams params, 156 int swapWith) { 157 158 Page page = (Page) entity; 159 PageNode node = page.getNode(); 160 PageNode parentNode = node.getParent(); 161 List nodes = parentNode.getChildNodes(); 162 163 int pos = nodes.indexOf(node); 164 Object otherNode = nodes.get(swapWith); 165 166 nodes.remove(node); 167 nodes.remove(otherNode); 168 169 if (pos < swapWith) { 170 nodes.add(pos, otherNode); 171 nodes.add(swapWith, node); 172 } 173 else { 174 nodes.add(swapWith, node); 175 nodes.add(pos, otherNode); 176 } 177 178 pageDao.updateNode(parentNode); 179 } 180 181 public void addChild(Object entity, Object parent) { 182 Page page = (Page) entity; 183 PageNode node = page.getNode(); 184 PageNode parentNode = null; 185 if (parent instanceof Page) { 186 Page parentPage = (Page) parent; 187 parentNode = parentPage.getNode(); 188 } 189 else { 190 Site site = null; 191 if (parent instanceof SiteLocale) { 192 SiteLocale siteLocale = (SiteLocale) parent; 193 site = siteLocale.getSite(); 194 } 195 else { 196 site = pageDao.getDefaultSite(); 197 } 198 parentNode = pageDao.findRootNode(site); 199 } 200 pageDao.moveNode(node, parentNode); 201 } 202 203 public void removeChild(Object entity, Object parent) { 204 } 205 206 private Locale getFixedLocale() { 207 List locales = pageDao.getLocales(); 208 if (locales.size() > 1) { 209 throw new IllegalStateException ("More than one Locale is " + 210 "configured but null was passed as parent"); 211 } 212 return (Locale ) locales.get(0); 213 } 214 215 } 216 | Popular Tags |