/*
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.admin.editor.GenericCollectionEditor;
import no.dusken.aranea.model.Image;
import no.dusken.aranea.model.Person;
import no.dusken.aranea.model.Role;
import no.dusken.aranea.service.RoleService;
import no.dusken.aranea.service.StoreImageService;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author Marvin B. Lillehaug<lillehau@underdusken.no>
*/
public class EditPersonController extends GenericEditController<Person> {
private RoleService roleService;
private GenericCollectionEditor<Role, List> rolesEditor;
private StoreImageService storeImageService;
private CustomBooleanEditor booleanEditor;
public EditPersonController() {
super(Person.class);
}
@Override
protected Map referenceData(HttpServletRequest request, Object object,
Errors errors) throws Exception {
//noinspection unchecked
Map<String, Object> map = super.referenceData(request, object, errors);
map.put("roles", roleService.getRoles());
return map;
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object object,
BindException errors) throws Exception {
Person person = (Person) object;
// should we upload a new image?
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartRequest.getFile("file");
if (file != null && file.getSize() > 0) {
Image image = storeImageService.createImage(file);
image.setCreated(new Date());
image.setDescription("portrait of " + person.getFirstname() + " " + person.getSurname());
person.setPortrait(image);
}
if(person.getEmailAddress().equals("")){//default to username@underdusken.no
person.setEmailAddress(person.getUsername() + "@underdusken.no");
}
return super.onSubmit(request, response, person, errors);
}
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
super.initBinder(request, binder);
binder.registerCustomEditor(List.class, "roles", rolesEditor);
binder.registerCustomEditor(Boolean.class, "showPhoneNumber", booleanEditor);
binder.registerCustomEditor(Boolean.class, "showPortrait", booleanEditor);
// to actually be able to convert Multipart instance to byte[]
// we have to register a custom editor
binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
binder.setBindEmptyMultipartFiles(false);
}
@Required
public void setRoleService(RoleService roleService) {
this.roleService = roleService;
}
@Required
public void setRolesEditor(GenericCollectionEditor<Role, List> rolesEditor) {
this.rolesEditor = rolesEditor;
}
@Required
public void setStoreImageService(StoreImageService storeImageService) {
this.storeImageService = storeImageService;
}
@Required
public void setBooleanEditor(CustomBooleanEditor booleanEditor) {
this.booleanEditor = booleanEditor;
}
}