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

Java tutorial

Introduction

Here is the source code for no.dusken.aranea.admin.control.EditBannerController.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 com.twmacinta.util.MD5;
import no.dusken.aranea.admin.editor.CalendarEditor;
import no.dusken.aranea.admin.editor.GenericEnumEditor;
import no.dusken.aranea.model.Banner;
import no.dusken.aranea.model.BannerType;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Required;
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.io.File;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Erlend Hamnaberg<erlenha@underdusken.no>
 */
public class EditBannerController extends GenericEditController<Banner> {

    private CalendarEditor calendarEditor;

    private GenericEnumEditor<BannerType> bannerTypeEditor;

    private String bannerDirectory;

    public EditBannerController() {
        super(Banner.class);
    }

    @Override
    protected Map referenceData(HttpServletRequest httpServletRequest, Object object, Errors errors)
            throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();

        Map<String, Object> themap2 = new HashMap<String, Object>();
        for (BannerType bt : BannerType.values()) {
            themap2.put(bt.name(), bt.name());
        }
        map.put("types", themap2);
        return map;
    }

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

        MultipartHttpServletRequest multipart_request = (MultipartHttpServletRequest) request;
        MultipartFile file = multipart_request.getFile("file");
        if (banner.getType().equals(BannerType.Script)) {
            banner.setHash(MD5.asHex(banner.getScript().getBytes()).substring(0, 100));
        }
        if (file != null && file.getSize() > 0 && banner.getType() != BannerType.Script) {

            String fileName = file.getOriginalFilename();
            File bannerFile = new File(bannerDirectory + "/tmp/" + fileName);
            bannerFile.mkdirs();
            file.transferTo(bannerFile);
            String hash = MD5.asHex(MD5.getHash(bannerFile));

            banner.setHash(hash);
            FileUtils.copyFile(bannerFile,
                    new File(bannerDirectory + "/" + banner.getHash() + "." + banner.getType().toString()));
            bannerFile.delete();
        } else {
            errors.reject("A file is required");
        }

        return super.onSubmit(request, response, banner, errors);
    }

    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder databinder) throws Exception {
        super.initBinder(request, databinder);
        databinder.registerCustomEditor(Calendar.class, calendarEditor);
        databinder.registerCustomEditor(BannerType.class, bannerTypeEditor);
        // 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
    }

    @Required
    public void setCalendarEditor(CalendarEditor calendarEditor) {
        this.calendarEditor = calendarEditor;
    }

    @Required
    public void setBannerTypeEditor(GenericEnumEditor<BannerType> bannerTypeEditor) {
        this.bannerTypeEditor = bannerTypeEditor;
    }

    @Required
    public void setBannerDirectory(String bannerDirectory) {
        this.bannerDirectory = bannerDirectory;
    }
}