1 24 package org.riotfamily.components; 25 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.riotfamily.cachius.TaggingContext; 30 import org.riotfamily.cachius.spring.AbstractCacheableController; 31 import org.riotfamily.components.dao.ComponentDao; 32 import org.springframework.transaction.PlatformTransactionManager; 33 import org.springframework.transaction.TransactionStatus; 34 import org.springframework.transaction.support.TransactionCallback; 35 import org.springframework.transaction.support.TransactionTemplate; 36 import org.springframework.util.Assert; 37 import org.springframework.web.servlet.ModelAndView; 38 39 46 public class ComponentController extends AbstractCacheableController { 47 48 private PlatformTransactionManager transactionManager; 49 50 private ComponentDao componentDao; 51 52 private ComponentRepository componentRepository; 53 54 private String requiredType; 55 56 private String viewName; 57 58 59 public void setTransactionManager(PlatformTransactionManager transactionManager) { 60 this.transactionManager = transactionManager; 61 } 62 63 public ComponentDao getComponentDao() { 64 return this.componentDao; 65 } 66 67 public void setComponentDao(ComponentDao componentDao) { 68 this.componentDao = componentDao; 69 } 70 71 public ComponentRepository getComponentRepository() { 72 return this.componentRepository; 73 } 74 75 public void setComponentRepository(ComponentRepository componentRepository) { 76 this.componentRepository = componentRepository; 77 } 78 79 public void setRequiredType(String requiredType) { 80 this.requiredType = requiredType; 81 } 82 83 public void setViewName(String viewName) { 84 this.viewName = viewName; 85 } 86 87 protected Long getVersionId(HttpServletRequest request) { 88 String s = request.getParameter("id"); 89 return s != null? new Long (s) : null; 90 } 91 92 protected void appendCacheKey(StringBuffer key, 93 HttpServletRequest request) { 94 95 super.appendCacheKey(key, request); 96 key.append("?id=").append(getVersionId(request)); 97 } 98 99 public long getTimeToLive(HttpServletRequest request) { 100 return CACHE_ETERNALLY; 101 } 102 103 public ModelAndView handleRequest(final HttpServletRequest request, 104 final HttpServletResponse response) throws Exception { 105 106 return (ModelAndView) new TransactionTemplate(transactionManager) 107 .execute(new TransactionCallback() { 108 109 public Object doInTransaction(TransactionStatus status) { 110 return handleRequestInTransaction(request, response); 111 } 112 }); 113 } 114 115 protected ModelAndView handleRequestInTransaction( 116 HttpServletRequest request, HttpServletResponse response) { 117 118 Long id = getVersionId(request); 119 ComponentVersion version = componentDao.loadComponentVersion(id); 120 Assert.notNull(version, "No such component: " + id); 121 122 ComponentList list = version.getContainer().getList(); 123 TaggingContext.tag(request, list.getLocation().toString()); 124 125 Component component = componentRepository.getComponent(version); 126 127 Assert.isTrue(requiredType == null || 128 version.getType().equals(requiredType), 129 "Component must be of type " + requiredType); 130 131 return new ModelAndView(viewName, component.buildModel(version)); 132 } 133 134 } 135 | Popular Tags |