no.dusken.aranea.admin.control.GenericEditController.java Source code

Java tutorial

Introduction

Here is the source code for no.dusken.aranea.admin.control.GenericEditController.java

Source

/*
 Copyright 2006 - 2010 Under Dusken
    
 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 no.dusken.aranea.admin.control;

import no.dusken.aranea.model.AraneaObject;
import no.dusken.aranea.model.Person;
import no.dusken.aranea.service.PersonService;
import no.dusken.common.service.GenericService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * A generic edit controller. Reference to the object as $entity
 */
public class GenericEditController<T extends AraneaObject> extends SimpleFormController {

    protected GenericService<T> genericService;

    protected PersonService personService;

    private Class type;

    protected Logger log = LoggerFactory.getLogger(this.getClass());

    public GenericEditController(Class type) {
        this.type = type;
        this.setCommandClass(type);
        this.setCommandName("entity");
        this.setSessionForm(true);
    }

    @Override
    @SuppressWarnings("unchecked")
    protected Object formBackingObject(HttpServletRequest request) {

        Long id = ServletRequestUtils.getLongParameter(request, "ID", 0);
        T entity = genericService.getEntity(id);
        if (entity == null) {
            try {
                entity = (T) type.newInstance();
            } catch (Exception e) {
                log.error("Could not create AraneaObject!", e);
            }
        }
        return entity;
    }

    @Override
    protected Map referenceData(HttpServletRequest httpServletRequest, Object o, Errors errors) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        Person p = personService.getPersonByUsername(username);
        if (p != null) {
            map.put("loggedInUser", p);
        }
        return map;
    }

    @Override
    @SuppressWarnings("unchecked")
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object object,
            BindException errors) throws Exception {
        // default is to go back to the form
        setSuccessView(getFormView());

        T entity = (T) object;

        ModelAndView mav = super.onSubmit(request, response, entity, errors);

        // check if we should delete the object
        if (request.getParameter("delete") != null) {
            // ensure entached
            entity = genericService.getEntity(entity.getID());
            genericService.remove(entity);
            mav.getModel().put("entity", entity);
            mav.setViewName("no/dusken/aranea/base/admin/common/deleted");
            return mav;
        }

        entity = genericService.saveOrUpdate(entity);
        mav.getModel().put("entity", entity);
        String customRedirect;
        if ((customRedirect = (String) request
                .getAttribute("no.dusken.aranea.customGenericEditRedirect")) != null) {
            response.sendRedirect(customRedirect);
        } else {
            // redirect to the same form
            response.sendRedirect(request.getRequestURL() + "?ID=" + entity.getID());
        }
        return mav;
    }

    @Required
    public void setGenericService(GenericService<T> genericService) {
        this.genericService = genericService;
    }

    @Autowired
    @Required
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }

    protected GenericService<T> getGenericService() {
        return genericService;
    }
}