package com.oneops.search.msg.processor;

import com.google.gson.JsonParser;
import com.oneops.cms.dj.domain.CmsRelease;
import com.oneops.search.msg.index.Indexer;
import org.apache.http.client.fluent.Request;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/*******************************************************************************
 *
 *   Copyright 2016 Walmart, Inc.
 *
 *   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.
 *
 *******************************************************************************/

@Service
public class ReleaseMessageProcessor implements MessageProcessor {
    private static Logger logger = Logger.getLogger(ReleaseMessageProcessor.class);
    @Autowired
    private Indexer indexer;
    @Value("${designSnapshotURL}")
    private String designSnapshotURL;
    @Value("${manifestSnapshotURL}")
    private String manifestSnapshotURL;
    private static final String RELEASE = "release";

    @Override
    public void processMessage(String message, String msgType, String msgId) {
        CmsRelease release = GSON.fromJson(message, CmsRelease.class);
        String releaseMsg = GSON_ES.toJson(release);
        indexer.indexEvent(RELEASE, releaseMsg);
        indexer.index(String.valueOf(release.getReleaseId()), RELEASE, releaseMsg);
        indexSnapshot(release);
    }

    public void indexSnapshot(CmsRelease release) {
        try {
            if (!"closed".equalsIgnoreCase(release.getReleaseState())) {
                logger.info("Release is not closed. Won't do snapshot");
                return;
            }
            if (release.getNsPath().endsWith("bom")) return; // ignore bom release

            String url = (release.getNsPath().endsWith("manifest") ? manifestSnapshotURL : designSnapshotURL) + release.getNsPath();  // set snapshot URL based on release type
            logger.info("Retrieving snapshot for:" + url + " expected release:" + release.getReleaseId());
            String message = Request.Get(url).addHeader("Content-Type", "application/json").execute().returnContent().asString();
            long releaseId = new JsonParser().parse(message).getAsJsonObject().get("release").getAsLong();
            if (releaseId > release.getReleaseId()) {
                logger.warn("Snapshot is dirty, so discarding. Was expecting release:" + release.getReleaseId() + " snapshot has rfcs from release:" + releaseId);
            } else {
                indexer.index(String.valueOf(release.getReleaseId()), "snapshot", message);
                logger.info("Snapshot indexed for release ID: " + release.getReleaseId());
            }

        } catch (Exception e) {
            logger.error("Error while retrieving snapshot" + e.getMessage(), e);
        }
    }
}

@@ -1936,32 +1936,34 @@
 E, releaseMsg);%0A
+//
         indexSna
@@ -1981,16 +1981,34 @@
 se);
+ disable temporary
 %0A    %7D%0A%0A
     
@@ -1999,24 +1999,26 @@
 rary%0A    %7D%0A%0A
+//
     public v
@@ -2053,24 +2053,26 @@
  release) %7B%0A
+//
         try 
@@ -2073,16 +2073,18 @@
   try %7B%0A
+//
         
@@ -2144,24 +2144,26 @@
 State())) %7B%0A
+//
             
@@ -2223,16 +2223,18 @@
 shot%22);%0A
+//
         
@@ -2249,16 +2249,18 @@
 return;%0A
+//
         
@@ -2261,24 +2261,26 @@
           %7D%0A
+//
             
@@ -2350,17 +2350,21 @@
 release%0A
-%0A
+//%0A//
         
@@ -2531,16 +2531,18 @@
 se type%0A
+//
         
@@ -2632,32 +2632,34 @@
 etReleaseId());%0A
+//
             Stri
@@ -2768,24 +2768,26 @@
 asString();%0A
+//
             
@@ -2881,16 +2881,18 @@
 Long();%0A
+//
         
@@ -2933,24 +2933,26 @@
 easeId()) %7B%0A
+//
             
@@ -3102,16 +3102,18 @@
 aseId);%0A
+//
         
@@ -3125,16 +3125,18 @@
  else %7B%0A
+//
         
@@ -3215,24 +3215,26 @@
 , message);%0A
+//
             
@@ -3312,16 +3312,18 @@
 eId());%0A
+//
         
@@ -3328,17 +3328,21 @@
       %7D%0A
-%0A
+//%0A//
         
@@ -3361,24 +3361,26 @@
 eption e) %7B%0A
+//
             
@@ -3444,24 +3444,26 @@
 sage(), e);%0A
+//
         %7D%0A  
@@ -3460,16 +3460,18 @@
       %7D%0A
+//
     %7D%0A%7D%0A

fix for