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); disable temporary
    }

//    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);
//        }
//    }
}

@@ -39,43 +39,8 @@
 r;%0A%0A
-import com.google.gson.JsonParser;%0A
 impo
@@ -145,66 +145,309 @@
 che.
-http.client.fluent.Request;%0Aimport org.apache.log4j.Logger
+log4j.Logger;%0Aimport org.elasticsearch.action.search.SearchResponse;%0Aimport org.elasticsearch.action.search.SearchType;%0Aimport org.elasticsearch.client.Client;%0Aimport org.elasticsearch.common.unit.TimeValue;%0Aimport org.elasticsearch.index.query.QueryBuilders;%0Aimport org.elasticsearch.search.SearchHit
 ;%0Aim
@@ -511,67 +511,8 @@
 ed;%0A
-import org.springframework.beans.factory.annotation.Value;%0A
 impo
@@ -1419,281 +1419,49 @@
     
-private static Logger logger = Logger.getLogger(ReleaseMessageProcessor.class);%0A    @Autowired%0A    private Indexer indexer;%0A    @Value(%22$%7BdesignSnapshotURL%7D%22)%0A    private String designSnapshotURL;%0A    @Value(%22$%7BmanifestSnapshotURL%7D%22)%0A    private String manifestSnapshotURL;
+@Autowired%0A    private Indexer indexer;%0A%0A
 %0A   
@@ -1865,1526 +1865,8 @@
 g);%0A
-//        indexSnapshot(release); disable temporary%0A    %7D%0A%0A//    public void indexSnapshot(CmsRelease release) %7B%0A//        try %7B%0A//            if (!%22closed%22.equalsIgnoreCase(release.getReleaseState())) %7B%0A//                logger.info(%22Release is not closed. Won't do snapshot%22);%0A//                return;%0A//            %7D%0A//            if (release.getNsPath().endsWith(%22bom%22)) return; // ignore bom release%0A//%0A//            String url = (release.getNsPath().endsWith(%22manifest%22) ? manifestSnapshotURL : designSnapshotURL) + release.getNsPath();  // set snapshot URL based on release type%0A//            logger.info(%22Retrieving snapshot for:%22 + url + %22 expected release:%22 + release.getReleaseId());%0A//            String message = Request.Get(url).addHeader(%22Content-Type%22, %22application/json%22).execute().returnContent().asString();%0A//            long releaseId = new JsonParser().parse(message).getAsJsonObject().get(%22release%22).getAsLong();%0A//            if (releaseId %3E release.getReleaseId()) %7B%0A//                logger.warn(%22Snapshot is dirty, so discarding. Was expecting release:%22 + release.getReleaseId() + %22 snapshot has rfcs from release:%22 + releaseId);%0A//            %7D else %7B%0A//                indexer.index(String.valueOf(release.getReleaseId()), %22snapshot%22, message);%0A//                logger.info(%22Snapshot indexed for release ID: %22 + release.getReleaseId());%0A//            %7D%0A//%0A//        %7D catch (Exception e) %7B%0A//            logger.error(%22Error while retrieving snapshot%22 + e.getMessage(), e);%0A//        %7D%0A//
     

fix for