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

Java tutorial

Introduction

Here is the source code for com.gantzgulch.sharing.controller.SharedFileCommentController.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.domain.SharedFileComment;
import com.gantzgulch.sharing.form.SharedFileCommentForm;
import com.gantzgulch.sharing.service.SharedFileManager;
import com.gantzgulch.sharing.service.UserManager;

@Controller
@RequestMapping("/secure/fileComment.htm")
public class SharedFileCommentController {

    private final SharedFileManager sharedFileManager;
    private final UserManager userManager;

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

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

        SharedFile sharedFile = sharedFileManager.findById(id);

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

        SharedFileComment parentComment = sharedFileManager.findComment(commentId);

        SharedFileCommentForm form = new SharedFileCommentForm(sharedFile);
        if (parentComment != null) {
            form.setParentId(commentId);
            form.setTitle(parentComment.getTitle());
        }

        ModelAndView modelAndView = new ModelAndView(".fileComment", "sharedFileCommentForm", form);
        modelAndView.addObject("sharedFile", sharedFile);

        return modelAndView;
    }

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

        if (result.hasErrors()) {
            return new ModelAndView(".fileEdit", "sharedFile",
                    sharedFileManager.findById(sharedFileCommentForm.getSharedFileId()));
        }

        sharedFileManager.addComment(sharedFileCommentForm, userManager.getCurrentUser());

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