/**
   * @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;
          }
      }
    }
  }
  @Override
  public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
      return;
    }

    final ProcessorLog logger = getLogger();

    final ObjectHolder<Throwable> errorHolder = new ObjectHolder<>(null);
    final ObjectHolder<MessageWrapper> messageWrapperHolder = new ObjectHolder<>(null);

    session.read(
        flowFile,
        (final InputStream rawIn) -> {
          try {
            messageWrapperHolder.set(MessageSerializer.deserializeMessageWrapper(rawIn));
          } catch (MessageSerializationException ex) {
            errorHolder.set(
                new RuntimeException(
                    "Error deserializing FlowFile content into a MessageWrapper instance. Routing to FAILURE",
                    ex));
          }
        });

    if (errorHolder.get() != null) {
      UCSCreateException.routeFlowFileToException(
          context,
          session,
          logger,
          flowFile,
          REL_FAILURE,
          null,
          "Error in message deserialization: " + errorHolder.get().getCause() != null
              ? errorHolder.get().getCause().getMessage()
              : errorHolder.get().getMessage(),
          ExceptionType.InvalidMessage,
          null,
          null);
      return;
    }

    Message message = messageWrapperHolder.get().getMessage();

    // resolve the sender. We couldn't resolved it before because we needed
    // the specific serviceId.
    UCSController ucsService =
        context.getProperty(UCS_CONTROLLER_SERVICE).asControllerService(UCSController.class);
    UserContactInfo uci =
        ucsService.resolveUserContactInfo(
            message.getHeader().getSender().getPhysicalAddress().getAddress());

    if (uci == null) {
      UCSCreateException.routeFlowFileToException(
          context,
          session,
          logger,
          flowFile,
          REL_FAILURE,
          null,
          "Unknown User: "******"Unknown Service "
              + context.getProperty(SERVICE_ID).getValue()
              + " for  User: "******"GROUP:").
    Map<Boolean, List<Recipient>> chatRecipients =
        message
            .getHeader()
            .getRecipientsList()
            .stream()
            .filter(
                r ->
                    r.getDeliveryAddress() != null
                        && r.getDeliveryAddress().getPhysicalAddress() != null)
            .filter(
                r ->
                    context
                        .getProperty(SERVICE_ID)
                        .getValue()
                        .equals(r.getDeliveryAddress().getPhysicalAddress().getServiceId()))
            .collect(
                Collectors.groupingBy(
                    r ->
                        r.getDeliveryAddress()
                            .getPhysicalAddress()
                            .getAddress()
                            .startsWith("GROUP:")));

    Map<String, String> generatedReferences = new HashMap<>();

    if (chatRecipients.containsKey(Boolean.TRUE)) {
      generatedReferences.putAll(
          this.sendMessagesToPreStablishedGroups(
              context, session, flowFile, logger, chatRecipients.get(Boolean.TRUE)));
    }

    if (chatRecipients.containsKey(Boolean.FALSE)) {
      List<Recipient> recipients = chatRecipients.get(Boolean.FALSE);
      if (recipients.size() == 1) {
        generatedReferences.putAll(
            this.sendDirectMessage(context, session, flowFile, logger, message, recipients.get(0)));
      } else {
        generatedReferences.putAll(
            this.sendMessageToDynamicGroup(
                context, session, flowFile, logger, message, recipients));
      }
    }

    logger.debug("Removing original FlowFile");
    session.remove(flowFile);

    // keep track of the generated references
    // TODO: is this check correct/enough?
    if (message.getHeader().isReceiptNotification()) {
      logger.debug(
          "The message has ReceiptNotification flag enabled -> We are persisting its references.");
      generatedReferences
          .entrySet()
          .stream()
          .forEach(
              (gr) -> {
                ucsService.saveMessageReference(message, gr.getKey(), gr.getValue());
              });
    } else {
      logger.debug(
          "The message doesn't have ReceiptNotification flag enabled -> We are not persisting its references.");
    }
  }
  /**
   * @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;
          }
      }
    }
  }