1 24 package org.riotfamily.components.render; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.riotfamily.components.Component; 37 import org.riotfamily.components.ComponentList; 38 import org.riotfamily.components.ComponentRepository; 39 import org.riotfamily.components.ComponentVersion; 40 import org.riotfamily.components.Location; 41 import org.riotfamily.components.VersionContainer; 42 import org.riotfamily.components.component.AbstractComponent; 43 import org.riotfamily.components.config.ComponentListConfiguration; 44 import org.riotfamily.components.dao.ComponentDao; 45 46 public class AbstractRenderStrategy implements RenderStrategy { 47 48 public static final String INHERTING_COMPONENT = "inherit"; 49 50 protected Log log = LogFactory.getLog(getClass()); 51 52 protected ComponentDao dao; 53 54 protected ComponentRepository repository; 55 56 protected ComponentListConfiguration config; 57 58 protected HttpServletRequest request; 59 60 protected HttpServletResponse response; 61 62 protected PrintWriter out; 63 64 protected VersionContainer parent; 65 66 public AbstractRenderStrategy(ComponentDao dao, 67 ComponentRepository repository, ComponentListConfiguration config, 68 HttpServletRequest request, HttpServletResponse response) 69 throws IOException { 70 71 this.dao = dao; 72 this.repository = repository; 73 this.config = config; 74 this.request = request; 75 this.response = response; 76 this.out = response.getWriter(); 77 this.parent = (VersionContainer) request.getAttribute( 78 AbstractComponent.CONTAINER); 79 } 80 81 public final void render() throws IOException { 82 render(getLocation()); 83 } 84 85 protected void render(Location location) throws IOException { 86 ComponentList list = getComponentList(location); 87 if (list != null) { 88 renderComponentList(list); 89 } 90 else { 91 onListNotFound(location); 92 } 93 } 94 95 public void render(ComponentList list) throws IOException { 96 renderComponentList(list); 97 } 98 99 protected final Location getLocation() { 100 Location location = config.getLocator().getLocation(request); 101 log.debug("List location: " + location); 102 return location; 103 } 104 105 protected void onListNotFound(Location location) throws IOException { 106 log.debug("No ComponentList found for " + location); 107 } 108 109 114 protected ComponentList getComponentList(Location location) { 115 if (parent != null) { 116 return dao.findComponentList(parent, location.getSlot()); 117 } 118 return dao.findComponentList(location); 119 } 120 121 126 protected void renderComponentList(ComponentList list) throws IOException { 127 List components = getComponentsToRender(list); 128 renderComponents(components); 129 } 130 131 137 protected final void renderComponents(List components) throws IOException { 138 if (components == null || components.isEmpty()) { 139 onEmptyComponentList(); 140 return; 141 } 142 143 int i = 0; 144 Iterator it = components.iterator(); 145 while (it.hasNext()) { 146 VersionContainer container = (VersionContainer) it.next(); 147 renderContainer(container, getPositionalClassName(i++, !it.hasNext())); 148 } 149 } 150 151 protected void onEmptyComponentList() throws IOException { 152 } 153 154 158 protected List getComponentsToRender(ComponentList list) { 159 return list.getLiveContainers(); 160 } 161 162 168 protected void renderContainer(VersionContainer container, 169 String positionClassName) throws IOException { 170 171 ComponentVersion version = getVersionToRender(container); 172 if (version != null) { 173 renderComponentVersion(version, positionClassName); 174 } 175 } 176 177 181 protected ComponentVersion getVersionToRender(VersionContainer container) { 182 return container.getLiveVersion(); 183 } 184 185 189 protected final void renderComponentVersion(ComponentVersion version, 190 String positionClassName) throws IOException { 191 192 String type = version.getType(); 193 if (INHERTING_COMPONENT.equals(type)) { 194 renderParentList(version.getContainer().getList()); 195 } 196 else { 197 Component component = repository.getComponent(type); 198 renderComponent(component, version, positionClassName); 199 } 200 } 201 202 protected final void renderParentList(ComponentList list) 203 throws IOException { 204 205 Location location = list.getLocation(); 206 Location parentLocation = config.getLocator().getParentLocation(location); 207 log.debug("Parent location: " + parentLocation); 208 209 if (parentLocation != null) { 210 if (location.equals(parentLocation)) { 211 log.warn("Parent location is the same"); 212 return; 213 } 214 ComponentList parentList = getComponentList(parentLocation); 215 if (parentList != null) { 216 getStrategyForParentList().render(parentList); 217 } 218 else { 219 onListNotFound(parentLocation); 220 } 221 } 222 } 223 224 protected RenderStrategy getStrategyForParentList() throws IOException { 225 return this; 226 } 227 228 protected void renderComponent(Component component, 229 ComponentVersion version, String positionClassName) 230 throws IOException { 231 232 component.render(version, positionClassName, request, response); 233 } 234 235 protected String getPositionalClassName(int position, boolean last) { 236 StringBuffer sb = new StringBuffer ("component-").append(position + 1); 237 if (last) { 238 sb.append(" last-component"); 239 } 240 return sb.toString(); 241 } 242 243 } 244 | Popular Tags |