/*
 * 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.service;

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

import com.navercorp.pinpoint.web.batch.BatchConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.navercorp.pinpoint.web.alarm.checker.AlarmChecker;
import com.navercorp.pinpoint.web.alarm.vo.CheckerResult;
import com.navercorp.pinpoint.web.alarm.vo.Rule;
import com.navercorp.pinpoint.web.dao.AlarmDao;
import com.navercorp.pinpoint.web.vo.UserGroup;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author minwoo.jung
 * @author Jongjin.Bae
 */
@Service
@Transactional(rollbackFor = {Exception.class})
public class AlarmServiceImpl implements AlarmService {

    private final AlarmDao alarmDao;

    @Value("${webhook.enable:false}")
    private boolean webhookEnable;
    
    public AlarmServiceImpl(AlarmDao alarmDao) {
        this.alarmDao = Objects.requireNonNull(alarmDao, "alarmDao");
    }

    @Override
    public String insertRule(Rule rule) {
        if (webhookEnable) {
            return alarmDao.insertRule(rule);
        }
        return alarmDao.insertRuleExceptWebhookSend(rule);
    }

    @Override
    public void deleteRule(Rule rule) {
        alarmDao.deleteRule(rule);
        alarmDao.deleteCheckerResult(rule.getRuleId());
    }

    @Override
    @Transactional(readOnly = true)
    public List<Rule> selectRuleByUserGroupId(String userGroupId) {
        return alarmDao.selectRuleByUserGroupId(userGroupId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Rule> selectRuleByApplicationId(String applicationId) {
        return alarmDao.selectRuleByApplicationId(applicationId);
    }

    @Override
    public void updateRule(Rule rule) {
        if (webhookEnable) {
            alarmDao.updateRule(rule);
        }
        alarmDao.updateRuleExceptWebhookSend(rule);
        alarmDao.deleteCheckerResult(rule.getRuleId());
    }

    @Override
    @Transactional(readOnly = true)
    public Map<String, CheckerResult> selectBeforeCheckerResults(String applicationId) {
        Map<String, CheckerResult> checkerResults = new HashMap<>();
        List<CheckerResult> CheckerResultList = alarmDao.selectBeforeCheckerResultList(applicationId);
        
        if (!CheckerResultList.isEmpty()) {
            for (CheckerResult checkerResult : CheckerResultList) {
                checkerResults.put(checkerResult.getRuleId(), checkerResult);
            }
        }
        
        return checkerResults;
    }

    @Override
    public void updateBeforeCheckerResult(CheckerResult beforeCheckerResult, AlarmChecker checker) {
        alarmDao.deleteCheckerResult(beforeCheckerResult.getRuleId());
        
        if (checker.isDetected()) {
            beforeCheckerResult.setDetected(true);
            beforeCheckerResult.increseCount();
            alarmDao.insertCheckerResult(beforeCheckerResult);
        } else {
            alarmDao.insertCheckerResult(new CheckerResult(checker.getRule().getRuleId(), checker.getRule().getApplicationId(), checker.getRule().getCheckerName(), false, 0, 1));
        }
        
         
    }

    @Override
    public void deleteRuleByUserGroupId(String groupId) {
        alarmDao.deleteRuleByUserGroupId(groupId);
    }

    @Override
    public void updateUserGroupIdOfRule(UserGroup userGroup) {
        alarmDao.updateUserGroupIdOfRule(userGroup);
    }

}

@@ -1436,16 +1436,20 @@
 vice %7B%0D%0A
+    
 %0D%0A    pr
@@ -1480,16 +1480,20 @@
 rmDao;%0D%0A
+    
 %0D%0A    @V
@@ -1687,32 +1687,36 @@
 mDao%22);%0D%0A    %7D%0D%0A
+    
 %0D%0A    @Override%0D
@@ -1837,34 +1837,45 @@
 ule);%0D%0A        %7D
-%0D%0A
+ else %7B%0D%0A    
         return a
@@ -1914,31 +1914,46 @@
 end(rule);%0D%0A
+        %7D%0D%0A
     %7D%0D%0A
+    
 %0D%0A    @Overr
@@ -2090,32 +2090,36 @@
 eId());%0D%0A    %7D%0D%0A
+    
 %0D%0A    @Override%0D
@@ -2287,32 +2287,36 @@
 oupId);%0D%0A    %7D%0D%0A
+    
 %0D%0A    @Override%0D
@@ -2492,32 +2492,36 @@
 ionId);%0D%0A    %7D%0D%0A
+    
 %0D%0A    @Override%0D
@@ -2633,34 +2633,45 @@
 ule);%0D%0A        %7D
-%0D%0A
+ else %7B%0D%0A    
         alarmDao
@@ -2699,32 +2699,43 @@
 ookSend(rule);%0D%0A
+        %7D%0D%0A
         alarmDao
@@ -2774,32 +2774,36 @@
 eId());%0D%0A    %7D%0D%0A
+    
 %0D%0A    @Override%0D
@@ -3374,32 +3374,36 @@
 esults;%0D%0A    %7D%0D%0A
+    
 %0D%0A    @Override%0D
@@ -4012,25 +4012,24 @@
   %0D%0A        
- 
 %0D%0A    %7D%0D%0A%0D%0A 
@@ -4017,32 +4017,36 @@
        %0D%0A    %7D%0D%0A
+    
 %0D%0A    @Override%0D
@@ -4164,16 +4164,20 @@
 %0A    %7D%0D%0A
+    
 %0D%0A    @O
@@ -4310,13 +4310,17 @@
 %0A    %7D%0D%0A
+    
 %0D%0A%7D%0D%0A

Refactor update & insert Rule Method in AlarmServiceImpl