class GetFactHandlesCommand implements
    ExecutableCommand<Collection<FactHandle>> {

    public Collection<FactHandle> execute( Context context ) {
        KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );
        Collection<FactHandle> disconnectedFactHandles = new ArrayList<FactHandle>();
        if ( filter != null ) {
            Collection<InternalFactHandle> factHandles = ksession.getFactHandles( this.filter );
            if(factHandles != null && disconnected){
                for(InternalFactHandle factHandle: factHandles){
                    InternalFactHandle handle = factHandle.clone();
                    handle.disconnect();
                    disconnectedFactHandles.add(handle);
                }
                if (outIdentifier != null) {
                    ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult( this.outIdentifier, disconnectedFactHandles );
                }
                return disconnectedFactHandles;
            }
            else {

                Collection<FactHandle> ksessionFactHandles = ksession.getFactHandles( this.filter );
                if (outIdentifier != null) {
                    ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult(this.outIdentifier, new ArrayList<FactHandle>(ksessionFactHandles));
                }
                return ksessionFactHandles;
            }
        } else {
            Collection<InternalFactHandle> factHandles = ksession.getFactHandles( );
            if(factHandles != null && disconnected){
                for(InternalFactHandle factHandle: factHandles){
                    InternalFactHandle handle = factHandle.clone();
                    handle.disconnect();
                    disconnectedFactHandles.add(handle);
                }
                if (outIdentifier != null) {
                    ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult(this.outIdentifier, disconnectedFactHandles);
                }
                return disconnectedFactHandles;
            }
            else {
                Collection<FactHandle> ksessionFactHandles =  ksession.getFactHandles();
                if (outIdentifier != null) {
                    ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult(this.outIdentifier, new ArrayList<FactHandle>(ksessionFactHandles));
                }
                return ksessionFactHandles;
            }
        }
    }

    public  GetFactHandlesCommand();
    public  GetFactHandlesCommand(ObjectFilter filter);
    public  GetFactHandlesCommand(ObjectFilter filter, boolean disconnected);
    public  GetFactHandlesCommand(boolean disconnected);

}