/*
 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
 *
 * 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.
 */

package org.drools.core.command.runtime.rule;

import org.drools.core.command.impl.GenericCommand;
import org.drools.core.command.impl.KnowledgeCommandContext;
import org.drools.core.command.runtime.UnpersistableCommand;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.AgendaFilter;
import org.kie.api.runtime.rule.StatefulRuleSession;
import org.kie.internal.command.Context;


public class FireUntilHaltCommand
	implements
	GenericCommand<Void>, UnpersistableCommand {
    private static final long serialVersionUID = 510l;

    private AgendaFilter agendaFilter = null;

    public FireUntilHaltCommand() {
    }

    public FireUntilHaltCommand(AgendaFilter agendaFilter) {
        this.agendaFilter = agendaFilter;
    }

    public AgendaFilter getAgendaFilter() {
        return agendaFilter;
    }

    public void setAgendaFilter(AgendaFilter agendaFilter) {
        this.agendaFilter = agendaFilter;
    }

    public Void execute(Context context) {
        KieSession ksession = ((KnowledgeCommandContext) context).getKieSession();
        final StatefulRuleSession session = (StatefulRuleSession)ksession;
        
        new Thread(new Runnable() {
            public void run() {
                if ( agendaFilter != null ) {
                    session.fireUntilHalt( agendaFilter );
                } else {
                    session.fireUntilHalt();
                }
            }
        }).start();

        return null;
    }

    public String toString() {
        if ( agendaFilter != null ) {
            return "session.fireUntilHalt( " + agendaFilter + " );";
        } else {
            return "session.fireUntilHalt();";
        }
    }
}

@@ -723,32 +723,33 @@
 .GenericCommand;
+%0D
 %0Aimport org.droo
@@ -793,16 +793,17 @@
 Context;
+%0D
 %0Aimport 
@@ -855,16 +855,17 @@
 Command;
+%0D
 %0Aimport 
@@ -887,32 +887,33 @@
 time.KieSession;
+%0D
 %0Aimport org.kie.
@@ -942,16 +942,17 @@
 aFilter;
+%0D
 %0Aimport 
@@ -996,16 +996,17 @@
 Session;
+%0D
 %0Aimport 
@@ -1038,18 +1038,273 @@
 Context;
-%0A%0A
+%0D%0A%0D%0Aimport javax.xml.bind.annotation.XmlAccessType;%0D%0Aimport javax.xml.bind.annotation.XmlAccessorType;%0D%0Aimport javax.xml.bind.annotation.XmlRootElement;%0D%0Aimport javax.xml.bind.annotation.XmlTransient;%0D%0A%0D%0A@XmlRootElement%0D%0A@XmlAccessorType(XmlAccessType.NONE)
 %0D%0Apublic
@@ -1446,24 +1446,129 @@
  = 510l;%0D%0A%0D%0A
+    @XmlTransient%0D%0A    // TODO: make sure that all drools AgendaFilter implementations are serializable%0D%0A
     private 
@@ -1635,24 +1635,25 @@
 tCommand() %7B
+%0D
 %0A    %7D%0D%0A%0D%0A  
@@ -1710,16 +1710,17 @@
 ilter) %7B
+%0D
 %0A       
@@ -1753,16 +1753,17 @@
 aFilter;
+%0D
 %0A    %7D%0D%0A
@@ -2004,16 +2004,17 @@
 ntext) %7B
+%0D
 %0A       
@@ -2088,16 +2088,17 @@
 ssion();
+%0D
 %0A       
@@ -2164,16 +2164,17 @@
 session;
+%0D
 %0A       
@@ -2174,16 +2174,17 @@
         
+%0D
 %0A       
@@ -2207,24 +2207,25 @@
 Runnable() %7B
+%0D
 %0A           
@@ -2240,24 +2240,25 @@
 void run() %7B
+%0D
 %0A           
@@ -2283,32 +2283,33 @@
 lter != null ) %7B
+%0D
 %0A               
@@ -2351,16 +2351,17 @@
 ilter );
+%0D
 %0A       
@@ -2369,32 +2369,33 @@
         %7D else %7B
+%0D
 %0A               
@@ -2423,16 +2423,17 @@
 lHalt();
+%0D
 %0A       
@@ -2434,32 +2434,33 @@
                %7D
+%0D
 %0A            %7D%0A 
@@ -2453,24 +2453,25 @@
            %7D
+%0D
 %0A        %7D).
@@ -2478,17 +2478,19 @@
 start();
-%0A
+%0D%0A%0D
 %0A       
@@ -2502,16 +2502,17 @@
 rn null;
+%0D
 %0A    %7D%0D%0A
@@ -2543,16 +2543,17 @@
 ring() %7B
+%0D
 %0A       
@@ -2582,16 +2582,17 @@
 null ) %7B
+%0D
 %0A       
@@ -2648,24 +2648,25 @@
 ter + %22 );%22;
+%0D
 %0A        %7D e
@@ -2670,16 +2670,17 @@
 %7D else %7B
+%0D
 %0A       
@@ -2718,16 +2718,17 @@
 alt();%22;
+%0D
 %0A       
@@ -2729,16 +2729,17 @@
        %7D
+%0D
 %0A    %7D%0D%0A

add JAXB annotations to FireUntilHaltCommand