/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.riotfamily.components.riot.form;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.riotfamily.common.util.ResourceUtils;
import org.riotfamily.common.web.mvc.mapping.HandlerUrlUtils;
import org.riotfamily.core.dao.InvalidPropertyValueException;
import org.riotfamily.core.dao.RiotDaoException;
import org.riotfamily.forms.Form;
import org.riotfamily.forms.controller.FormContextFactory;
import org.riotfamily.forms.factory.FormRepository;
import org.riotfamily.forms.factory.RepositoryFormController;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.servlet.ModelAndView;
/**
* @author Felix Gnass [fgnass at neteye dot de]
*/
public abstract class AbstractFrontOfficeFormController
extends RepositoryFormController {
private static final String SESSION_ATTRIBUTE = "frontOfficeForm";
private static final DefaultTransactionDefinition TRANSACTION_DEFINITION =
new DefaultTransactionDefinition(
TransactionDefinition.PROPAGATION_REQUIRED);
private PlatformTransactionManager transactionManager;
private String viewName = ResourceUtils.getPath(
AbstractFrontOfficeFormController.class, "form.ftl");
private String successViewName = ResourceUtils.getPath(
AbstractFrontOfficeFormController.class, "form-success.ftl");
public AbstractFrontOfficeFormController(
FormContextFactory formContextFactory,
FormRepository formRepository,
PlatformTransactionManager transactionManager) {
super(formContextFactory, formRepository);
this.transactionManager = transactionManager;
}
public void setViewName(String viewName) {
this.viewName = viewName;
}
public void setSuccessViewName(String successViewName) {
this.successViewName = successViewName;
}
@Override
protected String getFormId(HttpServletRequest request) {
return HandlerUrlUtils.getPathWithinMapping(request);
}
@Override
protected String getSessionAttribute(HttpServletRequest request) {
return SESSION_ATTRIBUTE;
}
@Override
protected void initForm(Form form, HttpServletRequest request) {
super.initForm(form, request);
form.addButton("save");
}
@Override
protected ModelAndView showForm(final Form form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
StringWriter sw = new StringWriter();
renderForm(form, new PrintWriter(sw));
return new ModelAndView(viewName)
.addObject("html", sw.toString())
.addObject("form", form);
}
@Override
public final ModelAndView handleFormSubmission(Form form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
handleFormSubmissionInternal(form, request, response);
return new ModelAndView(successViewName);
}
catch (InvalidPropertyValueException e) {
form.getErrors().rejectValue(e.getField(), e.getCode(),
e.getArguments(), e.getMessage());
return showForm(form, request, response);
}
catch (RiotDaoException e) {
form.getErrors().reject(e.getCode(), e.getArguments(), e.getMessage());
return showForm(form, request, response);
}
}
protected void handleFormSubmissionInternal(Form form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
TransactionStatus status = transactionManager.getTransaction(TRANSACTION_DEFINITION);
try {
Object bean = form.populateBackingObject();
update(bean, request);
}
catch (Exception e) {
transactionManager.rollback(status);
throw e;
}
transactionManager.commit(status);
}
protected abstract Object update(Object object, HttpServletRequest request);
}