001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.components.controller.render;
025:
026: import java.io.IOException;
027: import java.util.ArrayList;
028: import java.util.List;
029:
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.riotfamily.common.markup.DocumentWriter;
036: import org.riotfamily.common.markup.Html;
037: import org.riotfamily.common.markup.TagWriter;
038: import org.riotfamily.common.web.util.ServletUtils;
039: import org.riotfamily.components.config.ComponentListConfiguration;
040: import org.riotfamily.components.config.ComponentRepository;
041: import org.riotfamily.components.context.PageRequestUtils;
042: import org.riotfamily.components.dao.ComponentDao;
043: import org.riotfamily.components.model.ComponentList;
044: import org.riotfamily.components.model.ComponentVersion;
045: import org.riotfamily.components.model.Location;
046: import org.riotfamily.components.model.VersionContainer;
047:
048: public class EditModeRenderStrategy extends PreviewModeRenderStrategy {
049:
050: private static final Log log = LogFactory
051: .getLog(EditModeRenderStrategy.class);
052:
053: private RenderStrategy parentStrategy;
054:
055: public EditModeRenderStrategy(ComponentDao dao,
056: ComponentRepository repository,
057: ComponentListConfiguration config) {
058:
059: super (dao, repository, config);
060: parentStrategy = new InheritingRenderStrategy(dao, repository,
061: config);
062: }
063:
064: /**
065: * Overrides the default implementation to render a DIV tag around the
066: * actual list. The DIV has attributes that are required for the
067: * Riot toolbar JavaScript.
068: */
069: protected void renderComponentList(ComponentList list,
070: HttpServletRequest request, HttpServletResponse response)
071: throws Exception {
072:
073: DocumentWriter wrapper = new DocumentWriter(response
074: .getWriter());
075:
076: boolean renderOuterDiv = PageRequestUtils
077: .createAndStoreContext(request,
078: list.getId().toString(), 120000);
079:
080: if (renderOuterDiv) {
081: wrapper.start(Html.DIV).attribute(Html.COMMON_CLASS,
082: "riot-controller").attribute("riot:contextKey",
083: list.getId().toString()).attribute(
084: "riot:controllerId",
085: ServletUtils.getPathWithinApplication(request));
086: }
087:
088: String className = "riot-list riot-component-list";
089: if (getParentContainer(request) == null) {
090: className += " riot-toplevel-list";
091: }
092: if (list.isDirty()) {
093: className += " riot-dirty";
094: }
095:
096: wrapper.start(Html.DIV).attribute(Html.COMMON_CLASS, className)
097: .attribute("riot:listId", list.getId().toString());
098:
099: if (config.getMinComponents() != null) {
100: wrapper.attribute("riot:minComponents", config
101: .getMinComponents().intValue());
102: }
103: if (config.getMaxComponents() != null) {
104: wrapper.attribute("riot:maxComponents", config
105: .getMaxComponents().intValue());
106: }
107:
108: wrapper.body();
109: super .renderComponentList(list, request, response);
110: wrapper.closeAll();
111: }
112:
113: /**
114: * Overrides the default implementation to create a new list if no existing
115: * list is found.
116: *
117: * @see #createNewList(Location)
118: */
119: protected ComponentList getComponentList(Location location,
120: HttpServletRequest request) {
121:
122: ComponentList list = super .getComponentList(location, request);
123: if (list == null) {
124: list = createNewList(location, request);
125: }
126: return list;
127: }
128:
129: /**
130: * Creates a new ComponentList with an initial component set as defined by
131: * the controller.
132: * @param request
133: */
134: protected ComponentList createNewList(Location location,
135: HttpServletRequest request) {
136:
137: ComponentList list = new ComponentList();
138: list.setLocation(new Location(location));
139: list.setParent(getParentContainer(request));
140: String[] initialTypes = config.getInitialComponentTypes();
141: if (initialTypes != null) {
142: List containers = new ArrayList();
143: for (int i = 0; i < initialTypes.length; i++) {
144: VersionContainer container = new VersionContainer();
145: ComponentVersion live = new ComponentVersion(
146: initialTypes[i]);
147: live.setContainer(container);
148: container.setList(list);
149: container.setLiveVersion(live);
150: containers.add(container);
151: }
152: list.setLiveContainers(containers);
153: }
154: dao.saveComponentList(list);
155: log.debug("New ComponentList created: " + list);
156: return list;
157: }
158:
159: /**
160: * Overrides the default implementation to render a DIV tag around the
161: * actual component. The DIV has attributes that are required for the
162: * Riot toolbar JavaScript.
163: * @throws IOException
164: */
165: protected void renderContainer(VersionContainer container,
166: String positionClassName, HttpServletRequest request,
167: HttpServletResponse response) throws Exception {
168:
169: ComponentVersion version = getVersionToRender(container);
170: String type = version.getType();
171: String formUrl = repository.getFormUrl(type, container.getId());
172:
173: String className = "riot-list-component riot-component "
174: + "riot-component-" + type;
175:
176: if (formUrl != null) {
177: className += " riot-form";
178: }
179:
180: TagWriter wrapper = new TagWriter(response.getWriter());
181: wrapper.start(Html.DIV).attribute(Html.COMMON_CLASS, className)
182: .attribute("riot:containerId",
183: container.getId().toString()).attribute(
184: "riot:componentType", type).attribute(
185: "riot:form", formUrl).body();
186:
187: renderComponentVersion(version, positionClassName, request,
188: response);
189: wrapper.end();
190: }
191:
192: protected RenderStrategy getStrategyForParentList() {
193: return parentStrategy;
194: }
195:
196: }
|