com.gantzgulch.sharing.controller.SharedFileEditController.java Source code

Java tutorial

Introduction

Here is the source code for com.gantzgulch.sharing.controller.SharedFileEditController.java

Source

/*
 * Copyright 2011 GantzGulch, Inc.
 * 
 * This file is part of GantzFileSharing.
 * 
 * GantzFileSharing is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GantzFileSharing is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GantzFileSharing.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.gantzgulch.sharing.controller;

import java.io.IOException;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.gantzgulch.sharing.domain.SharedFile;
import com.gantzgulch.sharing.form.SharedFileEditForm;
import com.gantzgulch.sharing.service.SharedFileManager;
import com.gantzgulch.sharing.service.UserManager;

@Controller
@RequestMapping("/secure/fileEdit.htm")
public class SharedFileEditController {

    private final SharedFileManager sharedFileManager;
    private final UserManager userManager;

    @Autowired
    public SharedFileEditController(SharedFileManager sharedFileManager, UserManager userManager) {
        this.sharedFileManager = sharedFileManager;
        this.userManager = userManager;
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView showEditForm(@RequestParam(required = false, defaultValue = "") final String id) {

        SharedFile sharedFile = sharedFileManager.findById(id);

        if (sharedFile == null) {
            return new ModelAndView("redirect:/secure/files.htm");
        }

        SharedFileEditForm form = SharedFileEditForm.create(sharedFile);

        return new ModelAndView(".fileEdit", "sharedFileEditForm", form);
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView processForm(@Valid SharedFileEditForm sharedFileEditForm, BindingResult result,
            Map<String, Object> model) throws IOException {

        if (result.hasErrors()) {
            return new ModelAndView(".fileEdit");
        }

        sharedFileManager.update(sharedFileEditForm, userManager.getCurrentUser());

        return new ModelAndView("redirect:/secure/file.htm", "id", sharedFileEditForm.getId());
    }
}