void handleNewCallerIdEvent(NewCallerIdEvent event) {
    AsteriskChannelImpl channel = getChannelImplById(event.getUniqueId());

    if (channel == null) {
      // NewCallerIdEvent can occur for an existing channel that now has a different unique id
      // (originate with Local/)
      channel = getChannelImplByNameAndActive(event.getChannel());
      if (channel != null) {
        logger.info(
            "Changing unique id for '"
                + channel.getName()
                + "' from "
                + channel.getId()
                + " to "
                + event.getUniqueId());
        channel.idChanged(event.getDateReceived(), event.getUniqueId());
      }

      if (channel == null) {
        // NewCallerIdEvent can occur before NewChannelEvent
        channel =
            addNewChannel(
                event.getUniqueId(),
                event.getChannel(),
                event.getDateReceived(),
                event.getCallerIdNum(),
                event.getCallerIdName(),
                ChannelState.DOWN,
                null /* account code not available */);
      }
    }

    synchronized (channel) {
      channel.setCallerId(new CallerId(event.getCallerIdName(), event.getCallerIdNum()));
    }
  }
  void handleNewStateEvent(NewStateEvent event) {
    AsteriskChannelImpl channel = getChannelImplById(event.getUniqueId());

    if (channel == null) {
      // NewStateEvent can occur for an existing channel that now has a different unique id
      // (originate with Local/)
      channel = getChannelImplByNameAndActive(event.getChannel());
      if (channel != null) {
        logger.info(
            "Changing unique id for '"
                + channel.getName()
                + "' from "
                + channel.getId()
                + " to "
                + event.getUniqueId());
        channel.idChanged(event.getDateReceived(), event.getUniqueId());
      }

      if (channel == null) {
        logger.info(
            "Creating new channel due to NewStateEvent '"
                + event.getChannel()
                + "' unique id "
                + event.getUniqueId());
        // NewStateEvent can occur instead of a NewChannelEvent
        channel =
            addNewChannel(
                event.getUniqueId(),
                event.getChannel(),
                event.getDateReceived(),
                event.getCallerIdNum(),
                event.getCallerIdName(),
                ChannelState.valueOf(event.getChannelState()),
                null /* account code not available */);
      }
    }

    // NewStateEvent can provide a new CallerIdNum or CallerIdName not previously received through a
    // NewCallerIdEvent. This happens at least on outgoing legs from the queue application to
    // agents.
    if (event.getCallerIdNum() != null || event.getCallerIdName() != null) {
      String cidnum = "";
      String cidname = "";
      CallerId currentCallerId = channel.getCallerId();

      if (currentCallerId != null) {
        cidnum = currentCallerId.getNumber();
        cidname = currentCallerId.getName();
      }

      if (event.getCallerIdNum() != null) {
        cidnum = event.getCallerIdNum();
      }

      if (event.getCallerIdName() != null) {
        cidname = event.getCallerIdName();
      }

      CallerId newCallerId = new CallerId(cidname, cidnum);
      logger.debug("Updating CallerId (following NewStateEvent) to: " + newCallerId.toString());
      channel.setCallerId(newCallerId);

      // Also, NewStateEvent can return a new channel name for the same channel uniqueid, indicating
      // the channel has been
      // renamed but no related RenameEvent has been received.
      // This happens with mISDN channels (see AJ-153)
      if (event.getChannel() != null && !event.getChannel().equals(channel.getName())) {
        logger.info(
            "Renaming channel (following NewStateEvent) '"
                + channel.getName()
                + "' to '"
                + event.getChannel()
                + "'");
        synchronized (channel) {
          channel.nameChanged(event.getDateReceived(), event.getChannel());
        }
      }
    }

    if (event.getChannelState() != null) {
      synchronized (channel) {
        channel.stateChanged(
            event.getDateReceived(), ChannelState.valueOf(event.getChannelState()));
      }
    }
  }