<%
/*
 ~ Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 ~
 ~ WSO2 Inc. licenses this file to you 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.
 */

/* Includes */
var utils = require('/modules/utils.js');
var carbon = require("carbon");
var HTTPS_TRANSPORT = "https";
var EVENT_PROCESSOR_SERVICE = "/services/EventProcessorAdminService";
var HTTPConstants = Packages.org.apache.axis2.transport.http.HTTPConstants;
var EventProcessorAdminServiceStub = Packages.org.wso2.carbon.event.processor.stub.EventProcessorAdminServiceStub;
var eventProcessorAdminServiceWSUrl = utils.getCarbonServerAddress(HTTPS_TRANSPORT) + EVENT_PROCESSOR_SERVICE;
var eventprocessorStub = new EventProcessorAdminServiceStub(eventProcessorAdminServiceWSUrl);
var eventsProcessorServiceClient = eventprocessorStub._getServiceClient();
var eventProcessorOption = eventsProcessorServiceClient.getOptions();
eventProcessorOption.setManageSession(true);
eventProcessorOption.setProperty(HTTPConstants.COOKIE_STRING, session.get('authToken'));


var cepInfo = require('../include/commons.jag').cepInfo;
var removeAlert = require('../include/store_alerts.jag').remove;
var ws = require('ws');
var process = require("process"), host = process.getProperty('server.host'),
        ip = process.getProperty('carbon.local.ip');
var httpsPort = process.getProperty('mgt.transport.https.proxyPort');
if (!httpsPort) {
    //can use http.port as well
    httpsPort = process.getProperty('mgt.transport.https.port');
}

var log = new Log();

var verb = request.getMethod();
var executionPlanName = request.getParameter('executionPlanName');
var queryName = request.getParameter('queryName');
var cepAction = request.getParameter('cepAction'); // i.e 'deploy' new executionPlan , 'edit' executionPlan and etc..
var type = request.getParameter('Type');

if (verb == "POST") {
    removeAlert(queryName, type);
    if (cepAction == "undeploy") {
        var responseJson = {"status": "", "message": ""};
        try {
            //eventprocessorStub.undeployActiveExecutionPlan("geo_" + executionPlanName + "_alert");
            eventprocessorStub.undeployActiveExecutionPlan(executionPlanName);

            responseJson.status = "success";
            responseJson.message = "Query deployed successfully";
        } catch (ex) {
            msg = ex;
            responseJson.status = "failed";
            responseJson.message = "Error occurred while deploying the execution plan";
            log.error(msg);
        }
        print(responseJson);
    }
}

%>
