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) 2007
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.riot.list.command.dialog.ui;
025:
026: import java.io.PrintWriter;
027: import java.io.StringWriter;
028: import java.util.Map;
029:
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import org.riotfamily.common.collection.FlatMap;
034: import org.riotfamily.common.util.ResourceUtils;
035: import org.riotfamily.forms.Form;
036: import org.riotfamily.forms.controller.AjaxFormController;
037: import org.riotfamily.forms.controller.ButtonFactory;
038: import org.riotfamily.forms.controller.FormSubmissionHandler;
039: import org.riotfamily.riot.list.ListRepository;
040: import org.riotfamily.riot.list.command.dialog.DialogCommand;
041: import org.springframework.transaction.PlatformTransactionManager;
042: import org.springframework.transaction.TransactionDefinition;
043: import org.springframework.transaction.TransactionStatus;
044: import org.springframework.transaction.support.DefaultTransactionDefinition;
045: import org.springframework.web.servlet.ModelAndView;
046:
047: /**
048: * @author Felix Gnass [fgnass at neteye dot de]
049: * @since 6.4
050: */
051: public class DialogFormController extends AjaxFormController implements
052: FormSubmissionHandler {
053:
054: private static final DefaultTransactionDefinition TRANSACTION_DEFINITION = new DefaultTransactionDefinition(
055: TransactionDefinition.PROPAGATION_REQUIRED);
056:
057: private ListRepository listRepository;
058:
059: private String commandIdAttribute = "commandId";
060:
061: private PlatformTransactionManager transactionManager;
062:
063: private String viewName = ResourceUtils.getPath(
064: DialogFormController.class, "DialogFormView.ftl");
065:
066: public DialogFormController(ListRepository listRepository,
067: PlatformTransactionManager transactionManager) {
068:
069: this .listRepository = listRepository;
070: this .transactionManager = transactionManager;
071: ButtonFactory buttonFactory = new ButtonFactory(this );
072: buttonFactory.setLabelKey("label.dialog.button.execute");
073: buttonFactory.setCssClass("button button-execute");
074: addButton(buttonFactory);
075: }
076:
077: protected String getCommandId(HttpServletRequest request) {
078: return (String) request.getAttribute(commandIdAttribute);
079: }
080:
081: protected DialogCommand getCommand(HttpServletRequest request) {
082: return (DialogCommand) listRepository
083: .getCommand(getCommandId(request));
084: }
085:
086: /**
087: * Delegates the form creation to the DialogCommand.
088: */
089: protected Form createForm(HttpServletRequest request) {
090: return getForm(request);
091: }
092:
093: /**
094: * Overwrites the super implementation to do nothing since the form
095: * created by the command is already populated by contract.
096: */
097: protected void populateForm(Form form, HttpServletRequest request) {
098: }
099:
100: protected String getSessionAttribute(HttpServletRequest request) {
101: return getCommand(request).getFormSessionAttribute();
102: }
103:
104: protected String getTitle(Form form, HttpServletRequest request) {
105: String commandId = getCommandId(request);
106: return form.getFormContext().getMessageResolver().getMessage(
107: "label.dialog." + commandId, commandId);
108: }
109:
110: protected ModelAndView showForm(final Form form,
111: HttpServletRequest request, HttpServletResponse response) {
112:
113: StringWriter sw = new StringWriter();
114: renderForm(form, new PrintWriter(sw));
115:
116: Map model = new FlatMap();
117: model.put("form", sw.toString());
118: model.put("title", getTitle(form, request));
119: return new ModelAndView(viewName, model);
120: }
121:
122: public ModelAndView handleFormSubmission(Form form,
123: HttpServletRequest request, HttpServletResponse response)
124: throws Exception {
125:
126: ModelAndView modelAndView;
127: TransactionStatus status = transactionManager
128: .getTransaction(TRANSACTION_DEFINITION);
129: try {
130: Object input = form.populateBackingObject();
131: modelAndView = getCommand(request).handleInput(input);
132: } catch (Exception e) {
133: transactionManager.rollback(status);
134: throw e;
135: }
136: transactionManager.commit(status);
137: return modelAndView;
138: }
139:
140: }
|