public <T extends Message> List<DeliveryAddress> validateMessageRecipients(MessageWrapper<T> msg)
      throws InvalidAddressException {
    LinkedList<DeliveryAddress> outList = new LinkedList<DeliveryAddress>();

    // Ok Here we go
    Set<String> validServices = services.getServiceNames();

    Set<Recipient> sendTo = msg.getMessage().getHeader().getRecipientsList();
    Iterator<Recipient> itr = sendTo.iterator();
    while (itr.hasNext()) {
      Recipient r = itr.next();
      DeliveryAddress adr = r.getDeliveryAddress();

      AddressType type = adr.getAddressType();

      if (type == null) {

        InvalidAddressException exp =
            new InvalidAddressException("Recipient passed with unknown/type");
        eventLogger.logUserExceptionEvent("validateMessageRecipients", exp);
        throw exp;
      }
      switch (type) {
        case Physical:
          {
            PhysicalAddress a = adr.getPhysicalAddress();
            if (!validServices.contains(a.getServiceId())) {
              outList.add(adr);
            }
            break;
          }
        case Group:
          {
            // TODO: Should we do a deep validation? - For not no - We
            // assume Group integrity

            if (groupMgr.findGroup(adr.getGroupAddress().getName()) == null) {
              outList.add(adr);
            }
            break;
          }
        case Party:
          {
            // TODO: Should we do a deep Validation? - For not no - We
            // assume user integrity;
            if (userMgr.findUser(adr.getPartyAddress().getName()) == null) {
              outList.add(adr);
            }
            break;
          }
        default:
          {
            outList.add(adr);
          }
      }
    }

    // Loop Address - Switch by type
    return outList;
  }
  /**
   * @param adr
   * @param addressMap
   * @param exclude - Set of Delivery addresses already seen to avoid loops
   * @param msg - The Message wrapper for context dependent addressing
   */
  public void simpleResolveAddress(
      DeliveryAddress adr, List<PhysicalAddress> addressList, Set<DeliveryAddress> exclude) {
    if (exclude == null) {
      exclude = new HashSet<DeliveryAddress>();
    }

    if (!exclude.contains(adr)) {
      exclude.add(adr);

      AddressType type = adr.getAddressType();

      switch (type) {
        case Physical:
          {
            PhysicalAddress a = adr.getPhysicalAddress();
            addressList.add(a);
            break;
          }
        case Group:
          {
            DeliveryGroup grp = groupMgr.findGroup(adr.getGroupAddress().getName());
            if (grp != null) {
              // Loop the group
              LinkedList<DeliveryAddress> grpList = grp.getGroup();
              Iterator<DeliveryAddress> itr = grpList.iterator();
              while (itr.hasNext()) {
                simpleResolveAddress(itr.next(), addressList, exclude);
              }
            }
            break;
          }
        case Party:
          {
            UserContactInfo userInfo = userMgr.findUser(adr.getPartyAddress().getName());
            if (userInfo != null) {

              // TODO Extend for context and better address selection - ay need more context for the
              // operation.
              // TODO Extend for presence

              // Resolve the user preferred address
              PhysicalAddress a = userInfo.getPreferredAddress();
              addressList.add(a);
            }
            break;
          }
      }
    }
  }
  public List<DeliveryAddress> validateAddress(DeliveryAddress adr) throws InvalidAddressException {
    LinkedList<DeliveryAddress> outList = new LinkedList<DeliveryAddress>();

    switch (adr.getAddressType()) {
      case Physical:
        {
          PhysicalAddress a = adr.getPhysicalAddress();
          if (!services.getServiceNames().contains(a.getServiceId())) {
            outList.add(adr);
          }
          break;
        }
      case Group:
        {
          // TODO: Should we do a deep validation? - For now no - We assume
          // Group integrity

          if (groupMgr.findGroup(adr.getGroupAddress().getName()) == null) {
            outList.add(adr);
          }
          break;
        }
      case Party:
        {
          // TODO: Should we do a deep Validation? - For now no - We assume
          // user integrity;
          if (userMgr.findUser(adr.getPartyAddress().getName()) == null) {
            outList.add(adr);
          }
          break;
        }
      default:
        {
          outList.add(adr);
        }
    }

    // Loop Address - Switch by type
    return outList;
  }
  /**
   * @param adr
   * @param addressMap
   * @param exclude - Set of Delivery addresses already seen to avoid loops
   * @param msg - The Message wrapper for context dependent addressing
   */
  public <T extends Message> void resolveAddress(
      DeliveryAddress adr,
      HashMap<String, List<PhysicalAddress>> addressMap,
      Set<DeliveryAddress> exclude,
      MessageWrapper<T> msg,
      Recipient r) {
    if (!exclude.contains(adr)) {
      exclude.add(adr);

      AddressType type = adr.getAddressType();

      if (type == null) {
        eventLogger.logSummaryEvent(
            LogEntryType.User_SendMessage,
            EventLevel.warn,
            "resolveAddress",
            "RecipientAddress Type Unknown",
            "Recipient passed with unknown/type");
        return;
      }

      switch (adr.getAddressType()) {
        case Physical:
          {
            PhysicalAddress a = adr.getPhysicalAddress();
            List<PhysicalAddress> list = addressMap.get(a.getServiceId());
            if (list == null) {
              list = new LinkedList<PhysicalAddress>();
              addressMap.put(a.getServiceId(), list);
            }
            list.add(a);

            break;
          }
        case Group:
          {
            DeliveryGroup grp = groupMgr.findGroup(adr.getGroupAddress().getName());
            if (grp != null) {
              // Loop the group
              LinkedList<DeliveryAddress> grpList = grp.getGroup();
              Iterator<DeliveryAddress> itr = grpList.iterator();
              while (itr.hasNext()) {
                resolveAddress(itr.next(), addressMap, exclude, msg, r);
              }
            }
            break;
          }
        case Party:
          {
            UserContactInfo userInfo = userMgr.findUser(adr.getPartyAddress().getName());
            if (userInfo != null) {

              // TODO Extend for context and better address selection - ay need more context for the
              // operation.
              // TODO Extend for presence

              // Resolve the user preferred address
              PhysicalAddress a = userInfo.getPreferredAddress();

              List<PhysicalAddress> list = addressMap.get(a.getServiceId());
              if (list == null) {
                list = new LinkedList<PhysicalAddress>();
                addressMap.put(a.getServiceId(), list);
              }
              list.add(a);
            }
            break;
          }
      }
    }
  }