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

Java tutorial

Introduction

Here is the source code for no.dusken.aranea.admin.control.EditImageController.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.admin.editor.GenericCollectionEditor;
import no.dusken.aranea.admin.editor.GenericEditor;
import no.dusken.aranea.model.Image;
import no.dusken.aranea.model.Person;
import no.dusken.aranea.model.Tag;
import no.dusken.aranea.service.StoreImageService;
import no.dusken.aranea.service.TagService;
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.bind.ServletRequestUtils;
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.Map;
import java.util.Set;

/**
 * @author Jan Ove yen <janoveo@underdusken.no> Date: 12/01/2007 Time: 17:49:19
 */
public class EditImageController extends GenericEditController<Image> {

    private StoreImageService storeImageService;

    private GenericEditor<Person> personEditor;

    private GenericCollectionEditor<Tag, Set> tagsEditor;

    private TagService tagService;

    private CustomBooleanEditor booleanEditor;

    public EditImageController() {
        super(Image.class);
    }

    protected Map referenceData(HttpServletRequest request, Object object, Errors errors) throws Exception {
        Map<String, Object> map = super.referenceData(request, object, errors);

        map.put("persons", personService.getPersonsByFirstname(true));

        map.put("tags", tagService.getTags());

        // add ratio, if any
        String ratio = ServletRequestUtils.getStringParameter(request, "ratio");
        map.put("ratio", ratio);

        return map;
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object object,
            BindException errors) throws Exception {
        Image image = (Image) object;

        // set the photographer to null if externalsource is set.
        if (request.getParameter("externalsource") != null && request.getParameter("externalsource").length() > 0) {
            image.setPhotographer(null);
        }
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file_");
        if (file != null && file.getSize() != 0) {
            image = storeImageService.changeImage(file, image);
        }
        return super.onSubmit(request, response, image, errors);
    }

    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder databinder) throws Exception {
        super.initBinder(request, databinder);
        databinder.registerCustomEditor(Person.class, personEditor);
        databinder.registerCustomEditor(Set.class, "tags", tagsEditor);
        // to actually be able to convert Multipart instance to byte[]
        // we have to register a custom editor
        databinder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
        databinder.setBindEmptyMultipartFiles(false);
        // now Spring knows how to handle multipart object and convert them
        databinder.registerCustomEditor(Boolean.class, "illustration", booleanEditor);
    }

    @Override
    protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
            throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file_");
        Image i = (Image) command;
        /*if the image does not have a hash, it does not have a image
          So do not save a new image without image.
        */
        if (i.getHash() == null && (file == null || file.getSize() == 0)) {
            errors.reject("no.image.file", "Please provide a image");
            request.setAttribute("imageError", true);
        }
        super.onBindAndValidate(request, command, errors);
    }

    @Required
    public void setPersonEditor(GenericEditor<Person> personEditor) {
        this.personEditor = personEditor;
    }

    @Required
    public void setTagsEditor(GenericCollectionEditor<Tag, Set> tagsEditor) {
        this.tagsEditor = tagsEditor;
    }

    @Required
    public void setTagService(TagService tagService) {
        this.tagService = tagService;
    }

    @Required
    public void setStoreImageService(StoreImageService storeImageService) {
        this.storeImageService = storeImageService;
    }

    @Required
    public void setBooleanEditor(CustomBooleanEditor booleanEditor) {
        this.booleanEditor = booleanEditor;
    }
}