com.glodon.paas.document.web.controller.ProjectTemplateController.java Source code

Java tutorial

Introduction

Here is the source code for com.glodon.paas.document.web.controller.ProjectTemplateController.java

Source

/*
 * Copyright 2012-2014 glodon paas All right reserved. This software is the
 * confidential and proprietary information of glodon paas ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with glodon paas.
 */
package com.glodon.paas.document.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.glodon.paas.consts.StringConst;
import com.glodon.paas.document.api.bean.ProjectTemplate;
import com.glodon.paas.document.api.bean.User;
import com.glodon.paas.document.exception.DocumentErrorCode;
import com.glodon.paas.document.exception.DocumentPaasValidateException;
import com.glodon.paas.document.service.ProjectService;
import com.glodon.paas.document.web.util.ModelMapKeys;
import com.glodon.paas.document.web.util.ModelViewGenerator;
import com.glodon.paas.document.web.util.ViewNames;

/**
 * 
 * @author xur 2013-3-5 ?04:26:57
 */
@Controller
@RequestMapping("/project/template")
public class ProjectTemplateController extends BaseController {
    public static final Logger LOGGER = LoggerFactory.getLogger(ProjectMemberController.class);
    @Autowired
    private ProjectService projectService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView getTemplates(@RequestParam(value = "includePublic", required = false) String hasCommon,
            HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        response.setHeader("Cache-Control", "no-cache");
        User user = getUser();
        List<ProjectTemplate> result = projectService.getAllTemplates(user.getIdStr(), hasCommon);
        model.addAttribute(StringConst.ITEMS, result);
        return ModelViewGenerator.generate(ViewNames.VIEW_NAME_PROJECT_TEMPLATES_GET, model);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ModelAndView create(@RequestBody ProjectTemplate template, HttpServletRequest request, ModelMap model) {
        String userId = getUserId();
        String projectId = template.getProjectId();
        documentPrivilege.checkProjectAdmin(userId, projectId);
        if (projectService.checkSameTemplateName(userId, template.getName())) {
            throw new DocumentPaasValidateException(DocumentErrorCode.DUPLICATE_TEMPLATE,
                    "Duplicate template name!", new Exception("Duplicate template name!"));
        }
        ProjectTemplate result = projectService.createTemplate(projectId, template.getName(), userId);
        model.addAttribute(StringConst.ITEMS, result);
        return ModelViewGenerator.generate(ViewNames.VIEW_NAME_PROJECT_TEMPLATES_GET, model);
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public ModelAndView updateTemplate(@RequestBody ProjectTemplate template, HttpServletRequest request,
            ModelMap model) {
        User user = getUser();
        documentPrivilege.checkProjectAdmin(user.getIdStr(), template.getProjectId());
        if (projectService.checkSameTemplateName(user.getIdStr(), template.getName())) {
            throw new DocumentPaasValidateException(DocumentErrorCode.DUPLICATE_TEMPLATE,
                    "Duplicate template name!", new Exception("Duplicate template name!"));
        }
        boolean result = projectService.updateTemplate(template, user.getIdStr());
        if (result) {
            template.setOwner(user);
        }

        return ModelViewGenerator.generate(ViewNames.VIEW_NAME_PROJECT_TEMPLATE_COMMON,
                ModelMapKeys.KEY_PROJECT_TEMPLATE, template, model);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ModelAndView deleteTemplate(@PathVariable("id") String templateId, HttpServletRequest request,
            ModelMap model) {
        User user = getUser();
        //check the template exist
        ProjectTemplate template = projectService.getTemplate(templateId);
        if (template == null) {
            throw new DocumentPaasValidateException(DocumentErrorCode.TEMPLATE_NOT_EXIST, "Template is not exist!",
                    new Exception("Template is not exist!"));
        }
        if (!template.getOwnerId().equals(user.getIdStr())) {
            throw new DocumentPaasValidateException(DocumentErrorCode.TEMPLATE_NOT_BELONG_TO_USER,
                    "The template is not belong to this user.",
                    new Exception("The template is not belong to this user."));
        }
        //end
        boolean result = projectService.deleteTemplate(templateId);
        if (result) {
            model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
        } else {
            model.addAttribute(StringConst.RESULT, StringConst.FAIL);
        }
        return ModelViewGenerator.generate(ViewNames.VIEW_NAME_PROJECT_TEMPLATE_DELETE, model);
    }
}