package org.osiam.addons.administration.controller;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.ModelAndView;

/**
 * Generic controller with common functionality.
 */
public abstract class GenericController {

    @Inject
    private HttpSession session;

    /**
     * Store an object into the session.
     *
     * @param key Under which key should this object stored.
     * @param toStore Object that should be stored.
     */
    public void storeInSession(String key, Object toStore){
        session.setAttribute(generateKey(key), toStore);
    }

    /**
     * Get the object from the session.
     *
     * @param key The key under which the object will be found.
     * @return The object if one was found under the given key. Otherwise <b>null</b>.
     */
    public Object restoreFromSession(String key){
        return session.getAttribute(generateKey(key));
    }

    /**
     * Remove the object from the session.
     *
     * @param key The key under which the object will be found.
     */
    public void removeFromSession(String key){
        session.removeAttribute(generateKey(key));
    }

    protected void storeBindingResultIntoSession(BindingResult result, String modelName) {
        storeInSession(generateKey(modelName), result);
    }

    protected Object restoreBindingResultFromSession(String modelName) {
        return restoreFromSession(generateKey(modelName));
    }

    protected void enrichBindingResultFromSession(String modelName, ModelAndView model){
        model.addObject(BindingResult.MODEL_KEY_PREFIX + modelName, restoreBindingResultFromSession(modelName));
    }

    protected void removeBindingResultFromSession(String modelName) {
        removeFromSession(generateKey(modelName));
    }

    private String generateKey(String key) {
        return getClass().getName() + key;
    }
}

@@ -145,232 +145,762 @@
 ork.
-validation.BindingResult;%0Aimport org.springframework.web.servlet.ModelAndView;%0A%0A/**%0A * Generic controller with common functionality.%0A */%0Apublic abstract class GenericController %7B%0A%0A    @Inject%0A    private HttpSession session;
+beans.propertyeditors.StringTrimmerEditor;%0Aimport org.springframework.validation.BindingResult;%0Aimport org.springframework.web.bind.WebDataBinder;%0Aimport org.springframework.web.bind.annotation.InitBinder;%0Aimport org.springframework.web.servlet.ModelAndView;%0A%0A/**%0A * Generic controller with common functionality.%0A */%0Apublic abstract class GenericController %7B%0A%0A    @Inject%0A    private HttpSession session;%0A%0A    /**%0A     * %0A     * @param binder Binds the web request parameters to JavaBean object%0A     * Trims the request parameters%0A     * false set them to empty%0A     * true set them to null%0A     */%0A    @InitBinder%0A    public void initBinder(WebDataBinder binder) %7B%0A        binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));%0A    %7D
 %0A%0A  

Implement trim-method for incoming requests