1 24 package org.riotfamily.components.editor; 25 26 import java.io.StringWriter ; 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.List ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.directwebremoting.WebContext; 39 import org.directwebremoting.WebContextFactory; 40 import org.riotfamily.common.util.FormatUtils; 41 import org.riotfamily.common.web.util.CapturingResponseWrapper; 42 import org.riotfamily.common.web.util.ServletUtils; 43 import org.riotfamily.components.ComponentList; 44 import org.riotfamily.components.ComponentRepository; 45 import org.riotfamily.components.ComponentVersion; 46 import org.riotfamily.components.VersionContainer; 47 import org.riotfamily.components.config.ComponentListConfiguration; 48 import org.riotfamily.components.context.PageRequestUtils; 49 import org.riotfamily.components.context.RequestContextExpiredException; 50 import org.riotfamily.components.dao.ComponentDao; 51 import org.riotfamily.riot.security.AccessController; 52 import org.riotfamily.riot.security.session.LoginManager; 53 import org.springframework.context.MessageSource; 54 import org.springframework.context.MessageSourceAware; 55 import org.springframework.util.Assert; 56 import org.springframework.util.StringUtils; 57 import org.springframework.web.servlet.support.RequestContextUtils; 58 59 62 public class ComponentEditorImpl implements ComponentEditor, MessageSourceAware { 63 64 private Log log = LogFactory.getLog(ComponentEditorImpl.class); 65 66 private ComponentDao dao; 67 68 private ComponentRepository repository; 69 70 private MessageSource messageSource; 71 72 private Map editorConfigs; 73 74 private boolean instantPublish = false; 75 76 77 public ComponentEditorImpl(ComponentDao dao, ComponentRepository repository) { 78 this.dao = dao; 79 this.repository = repository; 80 } 81 82 public void setMessageSource(MessageSource messageSource) { 83 this.messageSource = messageSource; 84 } 85 86 public Map getEditorConfigs() { 87 return this.editorConfigs; 88 } 89 90 public void setEditorConfigs(Map editorConfigs) { 91 this.editorConfigs = editorConfigs; 92 } 93 94 public void setInstantPublish(boolean instantPublish) { 95 this.instantPublish = instantPublish; 96 } 97 98 protected ComponentVersion getVersionToEdit(VersionContainer container) { 99 return dao.getOrCreateVersion(container, null, instantPublish); 100 } 101 102 protected ComponentVersion getVersionToDisplay(VersionContainer container) { 103 return instantPublish 104 ? container.getLiveVersion() 105 : container.getLatestVersion(); 106 } 107 108 protected List getContainersToEdit(ComponentList componentList) { 109 return instantPublish 110 ? componentList.getLiveContainers() 111 : dao.getOrCreatePreviewContainers(componentList); 112 } 113 114 117 public String getText(Long containerId, String property) { 118 VersionContainer container = dao.loadVersionContainer(containerId); 119 ComponentVersion version = getVersionToDisplay(container); 120 return version.getProperty(property); 121 } 122 123 126 public void updateText(Long containerId, String property, String text) { 127 VersionContainer container = dao.loadVersionContainer(containerId); 128 ComponentVersion version = getVersionToEdit(container); 129 version.setProperty(property, text); 130 dao.updateComponentVersion(version); 131 } 132 133 136 public void updateTextChunks(Long containerId, String property, 137 String [] chunks) { 138 139 log.debug("Inserting chunks " + StringUtils.arrayToCommaDelimitedString(chunks)); 140 VersionContainer container = dao.loadVersionContainer(containerId); 141 ComponentVersion version = getVersionToEdit(container); 142 version.setProperty(property, chunks[0]); 143 dao.updateComponentVersion(version); 144 145 ComponentList list = container.getList(); 146 int offset = getContainersToEdit(list).indexOf(container); 147 for (int i = 1; i < chunks.length; i++) { 148 insertComponent(list.getId(), offset + i, version.getType(), 149 Collections.singletonMap(property, chunks[i])); 150 } 151 } 152 153 157 public List getValidTypes(String controllerId) { 158 ComponentListConfiguration cfg = repository.getListConfiguration(controllerId); 159 Assert.notNull(cfg, "No such controller: " + controllerId); 160 String [] types = cfg.getValidComponentTypes(); 161 162 Locale locale = getLocale(); 163 ArrayList result = new ArrayList (); 164 for (int i = 0; i < types.length; i++) { 165 String id = types[i]; 166 String description = messageSource.getMessage("component." + id, 167 null, FormatUtils.xmlToTitleCase(id), locale); 168 169 result.add(new TypeInfo(id, description)); 170 } 171 return result; 172 } 173 174 178 public Long insertComponent(Long listId, int position, String type, 179 Map properties) { 180 181 ComponentList componentList = dao.loadComponentList(listId); 182 VersionContainer container = dao.insertContainer(componentList, type, 183 properties, position, instantPublish); 184 185 return container.getId(); 186 } 187 188 public void setType(Long containerId, String type) { 189 VersionContainer container = dao.loadVersionContainer(containerId); 190 ComponentVersion version = getVersionToEdit(container); 191 version.setType(type); 192 dao.updateComponentVersion(version); 193 } 194 195 private String getHtml(String url, Object key, boolean live) 196 throws RequestContextExpiredException { 197 198 try { 199 StringWriter sw = new StringWriter (); 200 HttpServletRequest request = getWrappedRequest(key); 201 HttpServletResponse response = getCapturingResponse(sw); 202 EditModeUtils.include(request, response, url, live); 203 return sw.toString(); 204 205 } 206 catch (RequestContextExpiredException e) { 207 throw e; 208 } 209 catch (Exception e) { 210 log.error("Error rendering component.", e); 211 throw new RuntimeException (e); 212 } 213 } 214 215 public String getLiveListHtml(String controllerId, Long listId) 216 throws RequestContextExpiredException { 217 218 Object contextKey = listId; 219 if (contextKey == null) { 220 contextKey = controllerId; 221 } 222 return getHtml(controllerId, contextKey, true); 223 } 224 225 public String getPreviewListHtml(String controllerId, Long listId) 226 throws RequestContextExpiredException { 227 228 Object contextKey = listId; 229 if (contextKey == null) { 230 contextKey = controllerId; 231 } 232 return getHtml(controllerId, contextKey, false); 233 } 234 235 public void moveComponent(Long containerId, Long nextContainerId) { 236 VersionContainer container = dao.loadVersionContainer(containerId); 237 ComponentList componentList = container.getList(); 238 List containers = getContainersToEdit(componentList); 239 containers.remove(container); 240 if (nextContainerId != null) { 241 for (int i = 0; i < containers.size(); i++) { 242 VersionContainer c = (VersionContainer) containers.get(i); 243 if (c.getId().equals(nextContainerId)) { 244 containers.add(i, container); 245 break; 246 } 247 } 248 } 249 else { 250 containers.add(container); 251 } 252 if (!instantPublish) { 253 componentList.setDirty(true); 254 } 255 dao.updateComponentList(componentList); 256 } 257 258 public void deleteComponent(Long containerId) { 259 VersionContainer container = dao.loadVersionContainer(containerId); 260 ComponentList componentList = container.getList(); 261 List containers = getContainersToEdit(componentList); 262 containers.remove(container); 263 if (!instantPublish) { 264 componentList.setDirty(true); 265 } 266 dao.updateComponentList(componentList); 267 if (!componentList.getLiveContainers().contains(container)) { 268 dao.deleteVersionContainer(container); 269 } 270 } 271 272 public void publish(Long listId, Long [] containerIds) { 273 if (listId != null) { 274 publishList(listId); 275 } 276 if (containerIds != null) { 277 publishContainers(containerIds); 278 } 279 } 280 281 public void discard(Long listId, Long [] containerIds) { 282 if (listId != null) { 283 discardList(listId); 284 } 285 if (containerIds != null) { 286 discardContainers(containerIds); 287 } 288 } 289 290 294 private void discardContainers(Long [] ids) { 295 for (int i = 0; i < ids.length; i++) { 296 VersionContainer container = dao.loadVersionContainer(ids[i]); 297 dao.discardContainer(container); 298 } 299 } 300 301 305 private void discardList(Long listId) { 306 ComponentList componentList = dao.loadComponentList(listId); 307 dao.discardList(componentList); 308 } 309 310 314 private void publishContainers(Long [] ids) { 315 for (int i = 0; i < ids.length; i++) { 316 VersionContainer container = dao.loadVersionContainer(ids[i]); 317 dao.publishContainer(container); 318 } 319 } 320 321 324 public void publishList(Long listId) { 325 ComponentList list = dao.loadComponentList(listId); 326 if (AccessController.isGranted("publish", list.getLocation())) { 327 dao.publishList(list); 328 } 329 else { 330 throw new RuntimeException (messageSource.getMessage( 331 "components.error.publishNotGranted", null, getLocale())); 332 } 333 } 334 335 339 public void keepAlive() { 340 WebContext ctx = WebContextFactory.get(); 341 HttpServletRequest request = ctx.getHttpServletRequest(); 342 String path = ServletUtils.getPath(ctx.getCurrentPage()); 343 PageRequestUtils.touchContext(request, path); 344 } 345 346 349 public void logout() { 350 WebContext ctx = WebContextFactory.get(); 351 LoginManager.logout(ctx.getHttpServletRequest(), 352 ctx.getHttpServletResponse()); 353 } 354 355 356 357 private HttpServletRequest getWrappedRequest(Object key) 358 throws RequestContextExpiredException { 359 360 WebContext ctx = WebContextFactory.get(); 361 HttpServletRequest request = ctx.getHttpServletRequest(); 362 String path = ServletUtils.getPath(ctx.getCurrentPage()); 363 return PageRequestUtils.wrapRequest(request, path, key); 364 } 365 366 private HttpServletResponse getCapturingResponse(StringWriter sw) { 367 WebContext ctx = WebContextFactory.get(); 368 return new CapturingResponseWrapper(ctx.getHttpServletResponse(), sw); 369 } 370 371 private Locale getLocale() { 372 WebContext ctx = WebContextFactory.get(); 373 HttpServletRequest request = ctx.getHttpServletRequest(); 374 return RequestContextUtils.getLocale(request); 375 } 376 377 } 378 | Popular Tags |