Package org.internna.iwebmvc.spring.mvc.controllers

Source Code of org.internna.iwebmvc.spring.mvc.controllers.EntityController

/*
* Copyright 2002-2007 the original author or authors.
*
* 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.internna.iwebmvc.spring.mvc.controllers;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.internna.iwebmvc.model.DomainEntity;
import org.internna.iwebmvc.model.Displayable;
import org.internna.iwebmvc.model.UUID;
import org.internna.iwebmvc.spring.binding.AbstractBinderRegister;
import org.internna.iwebmvc.spring.i18n.I18nDataLoader;
import org.internna.iwebmvc.spring.util.RequestUtils;
import org.internna.iwebmvc.utils.Assert;
import org.internna.iwebmvc.utils.ClassUtils;
import org.internna.iwebmvc.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.support.ServletContextResource;

/**
* Automatically provides create / read / update operations for any domain entity.
*
* @author Jose Noheda
* @since 1.0
*/
@Controller
@RequestMapping("/crud.iwebmvc")
public class EntityController extends AbstractBinderRegister implements ServletContextAware {

    @Autowired protected Validator validator;
    @Autowired protected I18nDataLoader i18nDataLoader;

    private ServletContext servletContext;

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    protected String getView(String className) {
        String path = "crud/" + className.replaceAll("\\.", "/").toLowerCase();
        ServletContextResource resource = new ServletContextResource(servletContext, "WEB-INF/jsp/" + path + ".jsp");
        return resource.exists() ? path : "crud/template";
    }

    @ModelAttribute("entity")
    @SuppressWarnings("unchecked")
    protected Object bindEntity() {
        HttpServletRequest request = RequestUtils.getActiveRequest();
        String className = request.getParameter("className");
        return StringUtils.hasText(className) ? request.getSession().getAttribute(decipherer.decrypt(className)) : null;
    }

    @SuppressWarnings("unchecked")
    @RequestMapping(method = RequestMethod.GET)
    public String showForm(@RequestParam("className") String className, @RequestParam(value = "id", required = false) String id, @RequestParam(value = "redirect", required = false) String redirect, ModelMap model, HttpSession session) throws Exception {
        Class<? extends DomainEntity> clazz = ClassUtils.forName(className);
        Object entity = StringUtils.hasText(id) ? dao.find(clazz, new UUID(id)) : ClassUtils.instantiateClass(clazz);
        if (entity == null) return "crud/deleted";
        model.addAttribute("entity", entity);
        model.addAttribute("className", cipherer.encrypt(className));
        session.setAttribute(className, entity);
        if (StringUtils.hasText(redirect)) model.addAttribute("redirect", redirect);
        return getView(className);
    }

    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(@ModelAttribute("entity") DomainEntity entity, BindingResult result, String redirect, ModelMap model, HttpSession session) throws Exception {
        Assert.notNull(entity);
        String className = entity.getClass().getName();
        validator.validate(entity, result);
        if (result.hasErrors()) {
            model.addAttribute("className", cipherer.encrypt(className));
            if (StringUtils.hasText(redirect)) model.addAttribute("redirect", redirect);
            return getView(className);
        }
        if(entity.getId() == null) dao.create(entity);
        else dao.update(entity);
        session.removeAttribute(className);
        if (entity.isI18n()) i18nDataLoader.generate();
        if (StringUtils.hasText(redirect)) {
            model.addAttribute("redirect", redirect);
            model.addAttribute("pk", entity.getId().toString());
            model.addAttribute("display", ((Displayable) entity).getValue());
        }
        return StringUtils.hasText(redirect) ? "crud/autoclose" : "redirect:/filter.iwebmvc?className=" + cipherer.encrypt(className);
    }

}
TOP

Related Classes of org.internna.iwebmvc.spring.mvc.controllers.EntityController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.