/*
 * Copyright 2014 NAVER Corp.
 *
 * 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 com.navercorp.pinpoint.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
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 com.navercorp.pinpoint.web.alarm.CheckerCategory;
import com.navercorp.pinpoint.web.alarm.vo.Rule;
import com.navercorp.pinpoint.web.service.AlarmService;

/**
 * @author minwoo.jung
 */
@Controller
@RequestMapping(value="/alarmRule")
public class AlarmController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    public final static String USER_GROUP_ID = "userGroupId";

    @Autowired
    AlarmService alarmService;
    
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> insertRule(@RequestBody Rule rule) {
        Map<String, String> result = new HashMap<>();

        if (StringUtils.isEmpty(rule.getApplicationId()) || StringUtils.isEmpty(rule.getCheckerName()) || StringUtils.isEmpty(rule.getUserGroupId()) || StringUtils.isEmpty(rule.getThreshold())) {
            result.put("errorCode", "500");
            result.put("errorMessage", "there is not applicationId/checkerName/userGroupId/threashold to insert alarm rule");
            return result;
        }
        
        String ruleId = alarmService.insertRule(rule);

        result.put("result", "SUCCESS");
        result.put("ruleId", ruleId);
        return result;
    }
    
    @RequestMapping(method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String, String> deleteRule(@RequestBody Rule rule) {
        if (StringUtils.isEmpty(rule.getRuleId())) {
            Map<String, String> result = new HashMap<>();
            result.put("errorCode", "500");
            result.put("errorMessage", "there is not ruleId to delete alarm rule");
            return result;
        }
        
        alarmService.deleteRule(rule);

        Map<String, String> result = new HashMap<>();
        result.put("result", "SUCCESS");
        return result;
    }
    
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Object getRule(@RequestParam(USER_GROUP_ID) String userGroupId) {
        if (StringUtils.isEmpty(userGroupId)) {
            Map<String, String> result = new HashMap<>();
            result.put("errorCode", "500");
            result.put("errorMessage", "there is not userGroupId to get alarm rule");
            return result;
        }
        
        return alarmService.selectRuleByUserGroupId(userGroupId);
    }
    
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, String> updateRule(@RequestBody Rule rule) {
        Map<String, String> result = new HashMap<>();

        if (StringUtils.isEmpty(rule.getRuleId()) || StringUtils.isEmpty(rule.getApplicationId()) || StringUtils.isEmpty(rule.getCheckerName()) || StringUtils.isEmpty(rule.getUserGroupId())) {
            result.put("errorCode", "500");
            result.put("errorMessage", "there is not ruleId/userGroupId/applicationid/checkerName to get alarm rule");
            return result;
        }
        
        alarmService.updateRule(rule);
        
        result.put("result", "SUCCESS");
        return result;
    }
    
    @RequestMapping(value = "/checker", method = RequestMethod.GET)
    @ResponseBody
    public List<String> getCheckerName() {
        return CheckerCategory.getNames();
    }
    
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Map<String, String> handleException(Exception e) {
        logger.error(" Exception occurred while trying to CRUD Alarm Rule information", e);
        
        Map<String, String> result = new HashMap<>();
        result.put("errorCode", "500");
        result.put("errorMessage", "Exception occurred while trying to Alarm Rule information");
        return result;
    }
    
}

@@ -1567,16 +1567,17 @@
 g(value=
+%7B
 %22/alarmR
@@ -1580,16 +1580,43 @@
 armRule%22
+, %22/application/alarmRule%22%7D
 )%0D%0Apubli
@@ -1787,16 +1787,82 @@
 oupId%22;%0D
+%0A    public final static String APPLICATION_ID = %22applicationId%22;%0D
 %0A%0D%0A    @
@@ -3415,16 +3415,22 @@
 stParam(
+value=
 USER_GRO
@@ -3434,16 +3434,32 @@
 GROUP_ID
+, required=false
 ) String
@@ -3470,16 +3470,90 @@
 rGroupId
+, @RequestParam(value=APPLICATION_ID, required=false) String applicationId
 ) %7B%0D%0A   
@@ -3593,16 +3593,54 @@
 GroupId)
+ && StringUtils.isEmpty(applicationId)
 ) %7B%0D%0A   
@@ -3805,16 +3805,33 @@
 GroupId 
+or applicationID 
 to get a
@@ -3905,61 +3905,207 @@
     
-return alarmService.selectRuleByUserGroupId(userGroup
+if (!StringUtils.isEmpty(userGroupId)) %7B%0D%0A            return alarmService.selectRuleByUserGroupId(userGroupId);%0D%0A        %7D%0D%0A        %0D%0A        return alarmService.selectRuleByApplicationId(application
 Id);

add paramter for alarmRule api (#1888)