me.doshou.admin.maintain.notification.web.controller.NotificationTemplateController.java Source code

Java tutorial

Introduction

Here is the source code for me.doshou.admin.maintain.notification.web.controller.NotificationTemplateController.java

Source

/**
 * Copyright (c) 2005-2012 https://github.com/zhangkaitao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package me.doshou.admin.maintain.notification.web.controller;

import me.doshou.common.entity.search.Searchable;
import me.doshou.common.service.BaseService;
import me.doshou.common.web.bind.annotation.PageableDefaults;
import me.doshou.common.web.bind.annotation.SearchableDefaults;
import me.doshou.common.web.controller.BaseCRUDController;
import me.doshou.common.web.validate.ValidateResponse;
import me.doshou.admin.maintain.notification.entity.NotificationSystem;
import me.doshou.admin.maintain.notification.entity.NotificationTemplate;
import me.doshou.admin.maintain.notification.service.NotificationTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
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.bind.annotation.ResponseBody;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 13-1-28 ?4:29
 * <p>Version: 1.0
 */
@Controller
@RequestMapping(value = "/admin/maintain/notification/template")
public class NotificationTemplateController extends BaseCRUDController<NotificationTemplate, Long> {

    private NotificationTemplateService getNotificationTemplateService() {
        return (NotificationTemplateService) baseService;
    }

    public NotificationTemplateController() {
        setResourceIdentity("maintain:notificationTemplate");
    }

    @Override
    protected void setCommonData(Model model) {
        model.addAttribute("notificationSystems", NotificationSystem.values());
    }

    @Override
    @RequestMapping(method = RequestMethod.GET)
    @PageableDefaults(sort = "id=desc")
    @SearchableDefaults(value = "deleted_eq=true", merge = true)
    public String list(Searchable searchable, Model model) {
        return super.list(searchable, model);
    }

    /**
     * ?true
     *
     * @param m
     * @param result
     * @return
     */
    @Override
    protected boolean hasError(NotificationTemplate m, BindingResult result) {
        Assert.notNull(m);

        NotificationTemplate template = getNotificationTemplateService().findByName(m.getName());
        if (template == null || (template.getId().equals(m.getId()) && template.getName().equals(m.getName()))) {
            //success
        } else {
            result.rejectValue("name", "???");
        }

        return result.hasErrors();
    }

    /**
     * ??
     * ?[fieldId, 1|0, msg]
     * [[fieldId, 1|0, msg],[fieldId, 1|0, msg]]
     *
     * @param fieldId
     * @param fieldValue
     * @return
     */
    @RequestMapping(value = "validate", method = RequestMethod.GET)
    @ResponseBody
    public Object validate(@RequestParam("fieldId") String fieldId, @RequestParam("fieldValue") String fieldValue,
            @RequestParam(value = "id", required = false) Long id) {
        ValidateResponse response = ValidateResponse.newInstance();

        if ("name".equals(fieldId)) {
            NotificationTemplate template = getNotificationTemplateService().findByName(fieldValue);
            if (template == null || (template.getId().equals(id) && template.getName().equals(fieldValue))) {
                //msg ? ??
                response.validateSuccess(fieldId, "");
            } else {
                response.validateFail(fieldId, "???");
            }
        }
        return response.result();
    }

}