package org.chtijbug.drools.platform.core;

import org.chtijbug.drools.entity.DroolsFactObject;
import org.chtijbug.drools.entity.DroolsRuleObject;
import org.chtijbug.drools.entity.history.HistoryContainer;
import org.chtijbug.drools.platform.core.websocket.RuntimeWebSocketServerService;
import org.chtijbug.drools.platform.entity.JMXInfo;
import org.chtijbug.drools.platform.entity.MessageRuntimePlatform;
import org.chtijbug.drools.platform.entity.PlatformManagementKnowledgeBean;
import org.chtijbug.drools.platform.entity.RequestStatus;
import org.chtijbug.drools.runtime.DroolsChtijbugException;
import org.chtijbug.drools.runtime.RuleBaseSession;
import org.chtijbug.drools.runtime.impl.RuleBaseStatefulSession;
import org.chtijbug.drools.runtime.mbeans.StatefulSessionSupervision;

import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 * Date: 24/03/14
 * Time: 11:05
 * To change this template use File | Settings | File Templates.
 */
public class DroolsPlatformSession implements RuleBaseSession {

    private RuleBaseStatefulSession ruleBaseStatefulSession;

    private RuntimeWebSocketServerService runtimeWebSocketServerService;

    public RuleBaseStatefulSession getRuleBaseStatefulSession() {
        return ruleBaseStatefulSession;
    }

    public void setRuleBaseStatefulSession(RuleBaseStatefulSession ruleBaseStatefulSession) {
        this.ruleBaseStatefulSession = ruleBaseStatefulSession;
    }

    public RuntimeWebSocketServerService getRuntimeWebSocketServerService() {
        return runtimeWebSocketServerService;
    }

    public void setRuntimeWebSocketServerService(RuntimeWebSocketServerService runtimeWebSocketServerService) {
        this.runtimeWebSocketServerService = runtimeWebSocketServerService;
    }

    @Override
    public void insertObject(Object newObject) {
        this.ruleBaseStatefulSession.insertObject(newObject);
    }

    @Override
    public void insertByReflection(Object newObject) throws DroolsChtijbugException {
        this.ruleBaseStatefulSession.insertByReflection(newObject);
    }

    @Override
    public void setGlobal(String identifier, Object value) {
        this.ruleBaseStatefulSession.setGlobal(identifier, value);
    }

    @Override
    public void updateObject(Object updatedObject) {
        this.updateObject(updatedObject);
    }

    @Override
    public void retractObject(Object oldObject) {
        this.ruleBaseStatefulSession.retractObject(oldObject);
    }

    @Override
    public void fireAllRules() throws DroolsChtijbugException {
        this.ruleBaseStatefulSession.fireAllRules();
        PlatformManagementKnowledgeBean platformManagementKnowledgeBean = new PlatformManagementKnowledgeBean();
        platformManagementKnowledgeBean.setMessageRuntimePlatform(MessageRuntimePlatform.jmxInfos);
        platformManagementKnowledgeBean.setAlive(true);
        platformManagementKnowledgeBean.setRequestStatus(RequestStatus.SUCCESS);
        JMXInfo jmxInfo = new JMXInfo();
        platformManagementKnowledgeBean.setJmxInfo(jmxInfo);
        StatefulSessionSupervision mbeanStatefulSessionSupervision = ruleBaseStatefulSession.getMbeanStatefulSessionSupervision();
        jmxInfo.setAverageTimeExecution(mbeanStatefulSessionSupervision.getAverageTimeExecution());
        jmxInfo.setMinTimeExecution(mbeanStatefulSessionSupervision.getMinTimeExecution());
        jmxInfo.setMaxTimeExecution(mbeanStatefulSessionSupervision.getMaxTimeExecution());
        jmxInfo.setTotalTimeExecution(mbeanStatefulSessionSupervision.getTotalTimeExecution());
        jmxInfo.setTotalNumberRulesExecuted(mbeanStatefulSessionSupervision.getTotalNumberRulesExecuted());
        jmxInfo.setTotalNumberRulesExecuted(mbeanStatefulSessionSupervision.getAverageRulesExecuted());
        jmxInfo.setAverageRulesExecuted(mbeanStatefulSessionSupervision.getAverageRulesExecuted());
        jmxInfo.setMinRulesExecuted(mbeanStatefulSessionSupervision.getMinRulesExecuted());
        jmxInfo.setMaxRulesExecuted(mbeanStatefulSessionSupervision.getMaxRulesExecuted());
        jmxInfo.setNumberFireAllRulesExecuted(mbeanStatefulSessionSupervision.getNumberFireAllRulesExecuted());
        jmxInfo.setAverageRuleThroughout(mbeanStatefulSessionSupervision.getAverageRuleThroughout());
        jmxInfo.setMinRuleThroughout(mbeanStatefulSessionSupervision.getMinRuleThroughout());
        jmxInfo.setMaxRuleThroughout(mbeanStatefulSessionSupervision.getMaxRuleThroughout());
        try {
            this.runtimeWebSocketServerService.sendMessage(platformManagementKnowledgeBean);
        } catch (Exception e) {
            DroolsChtijbugException ee = new DroolsChtijbugException("JMXInfo", "Not Possible to send Infos", e);
            throw ee;
        }
    }

    @Override
    public void startProcess(String processName) {
        this.ruleBaseStatefulSession.startProcess(processName);
    }

    @Override
    public void dispose() {
        this.ruleBaseStatefulSession.dispose();
    }

    @Override
    public HistoryContainer getHistoryContainer() {
        return this.ruleBaseStatefulSession.getHistoryContainer();
    }

    @Override
    public String getHistoryContainerXML() {
        return this.ruleBaseStatefulSession.getHistoryContainerXML();
    }

    @Override
    public Collection<DroolsFactObject> listLastVersionObjects() {
        return this.ruleBaseStatefulSession.listLastVersionObjects();
    }

    @Override
    public String listLastVersionObjectsXML() {
        return this.ruleBaseStatefulSession.listLastVersionObjectsXML();
    }

    @Override
    public Collection<DroolsRuleObject> listRules() {
        return this.ruleBaseStatefulSession.listRules();
    }

    @Override
    public int getNumberRulesExecuted() {
        return this.ruleBaseStatefulSession.getNumberRulesExecuted();
    }

    @Override
    public int getSessionId() {
        return this.ruleBaseStatefulSession.getSessionId();
    }

    @Override
    public int getRuleBaseID() {
        return this.ruleBaseStatefulSession.getRuleBaseID();
    }
}

@@ -378,38 +378,47 @@
 .entity.
-MessageRuntimePlatform
+PlatformManagementKnowledgeBean
 ;%0Aimport
@@ -458,39 +458,30 @@
 ity.
+RequestRuntime
 Pla
-t
+r
 form
-ManagementKnowledgeBean
 ;%0Aim
@@ -2290,16 +2290,40 @@
    this.
+ruleBaseStatefulSession.
 updateOb
@@ -2771,23 +2771,23 @@
 Bean.set
-Message
+Request
 RuntimeP
@@ -2792,21 +2792,21 @@
 ePla
-t
+r
 form(
-Message
+Request
 Runt
@@ -2811,17 +2811,17 @@
 ntimePla
-t
+r
 form.jmx

Add Event when removing a Resource in a RuleBaseSingleton