Code Search for Developers
 
 
  

QuestionModifyController.java from crlove at Krugle


Show QuestionModifyController.java syntax highlighted

package com.witfriend.apollo.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections.keyvalue.DefaultMapEntry;
import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.witfriend.apollo.domain.Question;
import com.witfriend.apollo.domain.exception.question.QuestionExistedException;
import com.witfriend.apollo.service.QuestionService;
import com.witfriend.zeus.domain.exception.IdentifierRequiredException;
import com.witfriend.zeus.domain.exception.IllegalIdentifierException;
import com.witfriend.zeus.web.validator.IdentifierValidator;

/**
 * DOCUMENT ME!
 * @author wangyz
 * @since 2006.10.25
 * @version $Id$
 */
public class QuestionModifyController extends SimpleFormController {
	private QuestionService questionService;
	
	public QuestionModifyController() {
		Class<Question> clazz = Question.class;
		
		setBindOnNewForm(true);
		setCommandClass(clazz);
		setCommandName(StringUtils.uncapitalize(ClassUtils.getShortClassName(clazz)));
		setSessionForm(true);
		setSupportedMethods(new String[] {METHOD_GET, METHOD_POST});
		setValidator(IdentifierValidator.getInstance());
	}
	
	@Required
	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}
	
	@Override
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
		
		binder.setAllowedFields(isFormSubmission(request) ? new String[] {Question.IDENTIFIER_PROPERTY_NAME, "title"} : new String[] {Question.IDENTIFIER_PROPERTY_NAME});
		binder.setRequiredFields(binder.getAllowedFields());
	}
	
	@Override
	protected void onBindOnNewForm(HttpServletRequest request, Object command, BindException errors) throws Exception {
		onBind(request, command, errors);
		
		ValidationUtils.invokeValidator(getValidator(), command, errors);
		
		onBindAndValidate(request, command, errors);
	}
	
	@Override
	protected void onBind(HttpServletRequest request, Object command, BindException errors) throws Exception {
		if(errors.hasFieldErrors(Question.IDENTIFIER_PROPERTY_NAME)) {
			throw new IdentifierRequiredException();
		}
	}
	
	@Override
	protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) throws Exception {
		if(errors.hasFieldErrors(Question.IDENTIFIER_PROPERTY_NAME)) {
			throw new IllegalIdentifierException(Question.class.cast(command).getId());
		}
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
		try {
			return new ModelAndView(onSubmit(command, errors).getViewName()).addObject(Question.IDENTIFIER_PROPERTY_NAME, Question.class.cast(command).getId());
		} catch(QuestionExistedException qee) {
			errors.reject(qee.getCode(), qee.getArguments(), qee.getMessage());
		}
		
		return showForm(request, response, errors);
	}
	
	@Override
	protected void doSubmitAction(Object command) throws Exception {
		questionService.modify(Question.class.cast(command));
	}
	
	@Override
	protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
		Question question = questionService.load(Question.class.cast(command).getId());
		
		if(!isFormSubmission(request)) {
			request.getSession().setAttribute(getFormSessionAttributeName(request), question);
		}
		
		return MapUtils.putAll(new HashMap(), new Map.Entry[] {
			new DefaultMapEntry(getCommandName(), question)
		});
	}
}




See more files for this project here

crlove

Online psychological-test-based friend-making system.

Project homepage: http://sourceforge.net/projects/crlove
Programming language(s): Java,JavaScript,Ruby,XML
License: other

  ChoiceAddController.java
  ChoiceEditController.java
  ExamCreateController.java
  ExamModifyController.java
  ExamTakeController.java
  QuestionAddController.java
  QuestionModifyController.java
  QuestionRemoveController.java