void handleNewChannelEvent(NewChannelEvent event) {
    final AsteriskChannelImpl channel = getChannelImplById(event.getUniqueId());

    if (channel == null) {
      if (event.getChannel() == null) {
        logger.info(
            "Ignored NewChannelEvent with empty channel name (uniqueId="
                + event.getUniqueId()
                + ")");
      } else {
        addNewChannel(
            event.getUniqueId(),
            event.getChannel(),
            event.getDateReceived(),
            event.getCallerIdNum(),
            event.getCallerIdName(),
            ChannelState.valueOf(event.getChannelState()),
            event.getAccountCode());
      }
    } else {
      // channel had already been created probably by a NewCallerIdEvent
      synchronized (channel) {
        channel.nameChanged(event.getDateReceived(), event.getChannel());
        channel.setCallerId(new CallerId(event.getCallerIdName(), event.getCallerIdNum()));
        channel.stateChanged(
            event.getDateReceived(), ChannelState.valueOf(event.getChannelState()));
      }
    }
  }
  void handleStatusEvent(StatusEvent event) {
    AsteriskChannelImpl channel;
    final Extension extension;
    boolean isNew = false;
    Map<String, String> variables = event.getVariables();

    channel = getChannelImplById(event.getUniqueId());
    if (channel == null) {
      Date dateOfCreation;

      if (event.getSeconds() != null) {
        dateOfCreation = new Date(DateUtil.getDate().getTime() - (event.getSeconds() * 1000L));
      } else {
        dateOfCreation = DateUtil.getDate();
      }
      channel =
          new AsteriskChannelImpl(server, event.getChannel(), event.getUniqueId(), dateOfCreation);
      isNew = true;
      if (variables != null) {
        for (String variable : variables.keySet()) {
          channel.updateVariable(variable, variables.get(variable));
        }
      }
    }

    if (event.getContext() == null && event.getExtension() == null && event.getPriority() == null) {
      extension = null;
    } else {
      extension = new Extension(event.getContext(), event.getExtension(), event.getPriority());
    }

    synchronized (channel) {
      channel.setCallerId(new CallerId(event.getCallerIdName(), event.getCallerIdNum()));
      channel.setAccount(event.getAccountCode());
      if (event.getChannelState() != null) {
        channel.stateChanged(
            event.getDateReceived(), ChannelState.valueOf(event.getChannelState()));
      }
      channel.extensionVisited(event.getDateReceived(), extension);

      if (event.getBridgedChannel() != null) {
        final AsteriskChannelImpl linkedChannel = getChannelImplByName(event.getBridgedChannel());
        if (linkedChannel != null) {
          // the date used here is not correct!
          channel.channelLinked(event.getDateReceived(), linkedChannel);
          synchronized (linkedChannel) {
            linkedChannel.channelLinked(event.getDateReceived(), channel);
          }
        }
      }
    }

    if (isNew) {
      logger.info("Adding new channel " + channel.getName());
      addChannel(channel);
      server.fireNewAsteriskChannel(channel);
    }
  }
  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()));
      }
    }
  }