package com.oneops.search.msg.processor;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.oneops.cms.dj.domain.CmsRelease;
import com.oneops.cms.util.CmsConstants;
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);
        try {
            if ("closed".equalsIgnoreCase(release.getReleaseState())) {
                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());
                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());
                }
            } else {
                logger.info("Release is not closed. Won't do snapshot");
            }
        } catch (Exception e) {
            logger.error("Error while retrieving snapshot" + e.getMessage(), e);
        }
    }
}

@@ -39,108 +39,8 @@
 r;%0A%0A
-import com.google.gson.Gson;%0Aimport com.google.gson.JsonElement;%0Aimport com.google.gson.JsonObject;%0A
 impo
@@ -118,49 +118,8 @@
 se;%0A
-import com.oneops.cms.util.CmsConstants;%0A
 impo
@@ -1936,32 +1936,123 @@
 E, releaseMsg);%0A
+        indexSnapshot(release);%0A    %7D%0A%0A    public void indexSnapshot(CmsRelease release) %7B%0A
         try %7B%0A  
@@ -2065,16 +2065,17 @@
     if (
+!
 %22closed%22
@@ -2118,32 +2118,139 @@
 State())) %7B%0A    
+            logger.info(%22Release is not closed. Won't do snapshot%22);%0A                return;%0A            %7D%0A
             if (
@@ -2316,37 +2316,17 @@
 release%0A
-                %0A    
+%0A
         
@@ -2387,9 +2387,11 @@
 st%22)
-?
+ ? 
 mani
@@ -2405,17 +2405,19 @@
 pshotURL
-:
+ : 
 designSn
@@ -2497,29 +2497,8 @@
 ype%0A
-                %0A    
     
@@ -2612,19 +2612,22 @@
         
-   
+String
  message
@@ -2742,20 +2742,16 @@
         
-  
-  
 long rel
@@ -2841,36 +2841,32 @@
 ();%0A            
-    
 if (releaseId %3E 
@@ -2887,28 +2887,24 @@
 easeId()) %7B%0A
-    
             
@@ -3058,36 +3058,32 @@
 d);%0A            
-  
-  
 %7D else %7B%0A       
@@ -3067,36 +3067,32 @@
        %7D else %7B%0A
-    
                 
@@ -3175,36 +3175,32 @@
                 
-    
 logger.info(%22Sna
@@ -3274,121 +3274,10 @@
     
-  
-  %7D%0A            %7D else %7B%0A                logger.info(%22Release is not closed. Won't do snapshot%22);%0A            
 %7D
+%0A
 %0A   

fix for