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

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jca.cci.connection.NotSupportedRecordFactory;
import org.springframework.stereotype.Repository;

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.dao.UserGroupDao;
import com.navercorp.pinpoint.web.vo.UserGroup;

/**
 * @author minwoo.jung
 */
@Repository
public class MemoryAlarmDao implements AlarmDao {
    
    private final Map<String, Rule> alarmRule = new ConcurrentHashMap<String, Rule>();
    private final AtomicInteger ruleIdGenerator  = new AtomicInteger(); 
    
    @Override
    public String insertRule(Rule rule) {
        String ruleId = String.valueOf(ruleIdGenerator.getAndIncrement());
        rule.setRuleId(ruleId);
        alarmRule.put(ruleId, rule);
        return rule.getRuleId();
    }

    @Override
    public void deleteRule(Rule rule) {
        alarmRule.remove(rule.getRuleId());
    }

    @Override
    public void deleteRuleByUserGroupId(String userGroupId) {
        for (Entry<String, Rule> entry : alarmRule.entrySet()) {
            if (entry.getValue().getUserGroupId().equals(userGroupId)) {
                alarmRule.remove(entry.getKey());
            }
        }
    }

    @Override
    public List<Rule> selectRuleByUserGroupId(String userGroupId) {
        List<Rule> ruleList = new LinkedList<>();

        for (Entry<String, Rule> entry : alarmRule.entrySet()) {
            if (entry.getValue().getUserGroupId().equals(userGroupId)) {
                ruleList.add(entry.getValue());
            }
        }
        
        return ruleList;
    }
    
    @Override
    public List<Rule> selectRuleByApplicationId(String applicationId) {
        List<Rule> ruleList = new LinkedList<>();
        
        for (Entry<String, Rule> entry : alarmRule.entrySet()) {
            if (entry.getValue().getApplicationId().equals(applicationId)) {
                alarmRule.remove(entry.getKey());
            }
        }
        
        return ruleList;
    }

    @Override
    public void updateRule(Rule rule) {
        alarmRule.put(rule.getRuleId(), rule);
    }

    @Override
    public void updateUserGroupIdOfRule(UserGroup userGroup) {
    }

    @Override
    public List<CheckerResult> selectBeforeCheckerResultList(String applicationId) {
        return new ArrayList<CheckerResult>();
    }

    @Override
    public void deleteCheckerResult(CheckerResult checkerResult) {
    }

    @Override
    public void insertCheckerResult(CheckerResult checkerResult) {
    }

}

@@ -957,82 +957,8 @@
 d;%0D%0A
-import org.springframework.jca.cci.connection.NotSupportedRecordFactory;%0D%0A
 impo
@@ -1532,32 +1532,86 @@
 eger(); %0D%0A    %0D%0A
+    @Autowired%0D%0A    UserGroupDao userGroupDao;%0D%0A    %0D%0A
     @Override%0D%0A 
@@ -3251,16 +3251,23 @@
 rGroup u
+pdatedU
 serGroup
@@ -3267,24 +3267,589 @@
 erGroup) %7B%0D%0A
+        List%3CUserGroup%3E userGroupList = userGroupDao.selectUserGroup();%0D%0A        %0D%0A        String beforeUserGroupId = %22%22;%0D%0A        for (UserGroup userGroup : userGroupList) %7B%0D%0A            if (userGroup.getNumber().equals(updatedUserGroup.getNumber())) %7B%0D%0A                beforeUserGroupId = userGroup.getId();%0D%0A                break;%0D%0A            %7D%0D%0A        %7D%0D%0A        %0D%0A        List%3CRule%3E ruleList = selectRuleByUserGroupId(beforeUserGroupId);%0D%0A        %0D%0A        for (Rule rule : ruleList) %7B%0D%0A            rule.setuserGroupId(updatedUserGroup.getId());%0D%0A        %7D%0D%0A
     %7D%0D%0A%0D%0A   

add logic to update UserGroupId Of Rule in memoryAlarmDAO