package org.asteriskjava.pbx.asterisk.wrap.events;

import org.asteriskjava.pbx.Channel;
import org.asteriskjava.pbx.InvalidChannelName;
import org.asteriskjava.pbx.PBXFactory;
import org.asteriskjava.pbx.internal.core.AsteriskPBX;

public abstract class ChannelEventHelper extends ManagerEvent implements ChannelEvent
{
    private static final long serialVersionUID = 1L;

    /**
     * The name of the channel.
     */
    private final Channel channel;

    protected ChannelEventHelper(final org.asteriskjava.manager.event.AbstractChannelEvent event) throws InvalidChannelName
    {
        this(event.getChannel(), event.getUniqueId(), event.getCallerIdNum(), event.getCallerIdName());
    }

    protected ChannelEventHelper(final String channel, final String uniqueId, final String callerIdNum,
            final String callerIdName) throws InvalidChannelName
    {
        super(channel); // we must have a source but since don't have one pass
                        // anything.
        this.channel = ChannelEventHelper.registerChannel(channel, uniqueId, callerIdNum, callerIdName);
    }

    public ChannelEventHelper(final String channel, final String uniqueId) throws InvalidChannelName
    {
        super(channel);// we must have a source but since don't have one pass
                       // anything.
        final AsteriskPBX pbx = (AsteriskPBX) PBXFactory.getActivePBX();

        this.channel = pbx.internalRegisterChannel(channel, uniqueId);
    }

    @Override
    public Channel getChannel()
    {
        return this.channel;
    }

    public static Channel registerChannel(final String channelName, final String uniqueId, final String callerIdNum,
            final String callerIdName) throws InvalidChannelName
    {
        final AsteriskPBX pbx = (AsteriskPBX) PBXFactory.getActivePBX();

        final Channel channel = pbx.internalRegisterChannel(channelName, uniqueId);
        channel.setCallerId(pbx.buildCallerID(callerIdNum, callerIdName));
        return channel;
    }
}

@@ -882,23 +882,18 @@
   super(
-channel
+%22%22
 ); // we
@@ -953,37 +953,32 @@
                 
-     
 // anything.%0A   
@@ -966,32 +966,75 @@
    // anything.%0A
+        if (channel != null)%0A        %7B%0A    
         this.cha
@@ -1122,16 +1122,92 @@
 dName);%0A
+        %7D%0A        else%0A        %7B%0A            this.channel = null;%0A        %7D%0A
     %7D%0A%0A 
@@ -1326,23 +1326,18 @@
   super(
-channel
+%22%22
 );// we 
@@ -1377,37 +1377,32 @@
 t have one pass%0A
-     
                 
@@ -1481,33 +1481,75 @@
 getActivePBX();%0A
-%0A
+        if (channel != null)%0A        %7B%0A    
         this.cha
@@ -1599,24 +1599,100 @@
  uniqueId);%0A
+        %7D%0A        else%0A        %7B%0A            this.channel = null;%0A        %7D%0A
     %7D%0A%0A    @

handle null channel