/*
Copyright 2009 - 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.barweb.admin;
import no.dusken.common.model.DuskenObject;
import no.dusken.common.service.GenericService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.validation.BindException;
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;
/**
* A generic edit controller. Reference to the object as $entity
*/
public class GenericEditController<T extends DuskenObject> extends
SimpleFormController {
protected GenericService<T> genericService;
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.findOne(id);
if (entity == null) {
try {
entity = (T) type.newInstance();
} catch (Exception e) {
log.error("Could not create BarwebObject!", e);
}
}
return entity;
}
@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) {
try {
genericService.delete(entity.getId());
mav.getModel().put("entity", entity);
mav.setViewName("no/dusken/barweb/common/deleted");
return mav;
} catch (Exception e) {
mav.getModel().put("exception", e);
mav.setViewName("no/dusken/barweb/admin/common/error");
return mav;
}
}
try {
entity = genericService.save(entity);
} catch (Exception e) {
mav.getModel().put("exception", e);
mav.setViewName("no/dusken/barweb/admin/common/error");
}
// redirect to the same form
response.sendRedirect(request.getRequestURL() + "?ID=" + entity.getId());
return null;
}
@Required
public void setGenericService(GenericService<T> genericService) {
this.genericService = genericService;
}
protected GenericService<T> getGenericService() {
return genericService;
}
}