/**
   * Filter the PlatformComponentProfiles that match with the discoveryQueryParameters that get from
   * other component
   *
   * @param discoveryQueryParameters
   * @param clientIdentityPublicKey
   * @return List<PlatformComponentProfile>
   */
  private List<PlatformComponentProfile> applyDiscoveryQueryParametersFromOtherComponent(
      DiscoveryQueryParameters discoveryQueryParameters, String clientIdentityPublicKey) {

    System.out.println(
        "ComponentRegisteredListWebService - applyDiscoveryQueryParametersFromOtherComponent    = ");

    List<PlatformComponentProfile> filteredListFromOtherComponentType = new ArrayList<>();

    /*
     * Get the list from the cache that match with the other componet
     */
    List<PlatformComponentProfile> otherComponentList =
        (List<PlatformComponentProfile>)
            new ArrayList<>(
                    searchProfile(
                        discoveryQueryParameters.getFromOtherPlatformComponentType(),
                        discoveryQueryParameters.getFromOtherNetworkServiceType(),
                        discoveryQueryParameters.getIdentityPublicKey()))
                .clone();
    System.out.println(
        "ComponentRegisteredListWebService - otherComponentList  = " + otherComponentList.size());

    /*
     * Find the other component that match with the identity
     */
    for (PlatformComponentProfile platformComponentProfile : otherComponentList) {

      if (discoveryQueryParameters.getIdentityPublicKey() != null
          && discoveryQueryParameters.getIdentityPublicKey() != "") {
        List<PlatformComponentProfile> newList =
            searchProfileByCommunicationCloudClientIdentity(
                discoveryQueryParameters.getPlatformComponentType(),
                discoveryQueryParameters.getNetworkServiceType(),
                platformComponentProfile.getCommunicationCloudClientIdentity());
        filteredListFromOtherComponentType.addAll(newList);
      }
    }

    /*
     * Remove the requester from the list
     */
    Iterator<PlatformComponentProfile> iterator = filteredListFromOtherComponentType.iterator();
    while (iterator.hasNext()) {

      PlatformComponentProfile platformComponentProfileRegistered = iterator.next();
      if (platformComponentProfileRegistered
          .getCommunicationCloudClientIdentity()
          .equals(clientIdentityPublicKey)) {
        System.out.println(
            "ComponentRegisteredListWebService - removing ="
                + platformComponentProfileRegistered.getName());
        iterator.remove();
      }
    }

    System.out.println(
        "ComponentRegisteredListWebService - filteredListFromOtherComponentType  = "
            + filteredListFromOtherComponentType.size());

    return filteredListFromOtherComponentType;
  }
  /**
   * Return the primary list from the cache filtered by the platformComponentType or
   * networkServiceType
   *
   * @param platformComponentType
   * @param networkServiceType
   * @return List<PlatformComponentProfile>
   */
  public List<PlatformComponentProfile> getPrimaryFilteredListFromCache(
      PlatformComponentType platformComponentType,
      NetworkServiceType networkServiceType,
      String clientIdentityPublicKey) {

    /*
     * Get the list
     */
    List<PlatformComponentProfile> list = new ArrayList<>();

    /*
     * Switch between platform component type
     */
    switch (platformComponentType) {
      case COMMUNICATION_CLOUD_SERVER:
        if (!wsCommunicationCloudServer.getRegisteredCommunicationsCloudServerCache().isEmpty()) {
          list =
              (List<PlatformComponentProfile>)
                  new ArrayList<>(
                          wsCommunicationCloudServer
                              .getRegisteredCommunicationsCloudServerCache()
                              .values())
                      .clone();
        }
        break;

      case COMMUNICATION_CLOUD_CLIENT:
        if (!wsCommunicationCloudServer.getRegisteredCommunicationsCloudClientCache().isEmpty()) {
          list =
              (List<PlatformComponentProfile>)
                  new ArrayList<>(
                          wsCommunicationCloudServer
                              .getRegisteredCommunicationsCloudClientCache()
                              .values())
                      .clone();
        }
        break;

      case NETWORK_SERVICE:
        if (wsCommunicationCloudServer
                .getRegisteredNetworkServicesCache()
                .containsKey(networkServiceType)
            && !wsCommunicationCloudServer
                .getRegisteredNetworkServicesCache()
                .get(networkServiceType)
                .isEmpty()) {
          list =
              (List<PlatformComponentProfile>)
                  new ArrayList<>(
                          wsCommunicationCloudServer
                              .getRegisteredNetworkServicesCache()
                              .get(networkServiceType))
                      .clone();
        }
        break;

        // Others
      default:
        if (wsCommunicationCloudServer
                .getRegisteredOtherPlatformComponentProfileCache()
                .containsKey(platformComponentType)
            && !wsCommunicationCloudServer
                .getRegisteredOtherPlatformComponentProfileCache()
                .get(platformComponentType)
                .isEmpty()) {
          list =
              (List<PlatformComponentProfile>)
                  new ArrayList<>(
                          wsCommunicationCloudServer
                              .getRegisteredOtherPlatformComponentProfileCache()
                              .get(platformComponentType))
                      .clone();
        }
        break;
    }

    /*
     * Remove the requester from the list
     */
    Iterator<PlatformComponentProfile> iterator = list.iterator();
    while (iterator.hasNext()) {

      PlatformComponentProfile platformComponentProfileRegistered = iterator.next();
      if (platformComponentProfileRegistered
          .getCommunicationCloudClientIdentity()
          .equals(clientIdentityPublicKey)) {
        System.out.println(
            "ComponentRegisteredListWebService - removing ="
                + platformComponentProfileRegistered.getName());
        iterator.remove();
      }
    }

    return list;
  }
  /**
   * Filter the PlatformComponentProfile that match with the discoveryQueryParameters
   *
   * @param discoveryQueryParameters
   * @return List<PlatformComponentProfile>
   */
  private List<PlatformComponentProfile> applyDiscoveryQueryParameters(
      DiscoveryQueryParameters discoveryQueryParameters, String clientIdentityPublicKey) {

    int totalFilterToApply = countFilers(discoveryQueryParameters);
    int filterMatched = 0;

    List<PlatformComponentProfile> list =
        getPrimaryFilteredListFromCache(
            discoveryQueryParameters.getPlatformComponentType(),
            discoveryQueryParameters.getNetworkServiceType(),
            clientIdentityPublicKey);
    List<PlatformComponentProfile> filteredLis = new ArrayList<>();

    System.out.println(
        "ComponentRegisteredListWebService - totalFilterToApply    = " + totalFilterToApply);

    if (totalFilterToApply > 0) {

      /*
       * Apply the basic filter
       */
      for (PlatformComponentProfile platformComponentProfile : list) {

        if (discoveryQueryParameters.getIdentityPublicKey() != null
            && discoveryQueryParameters.getIdentityPublicKey() != "") {
          if (platformComponentProfile
              .getIdentityPublicKey()
              .equals(discoveryQueryParameters.getIdentityPublicKey())) {
            filterMatched += 1;
          }
        }

        if (discoveryQueryParameters.getAlias() != null
            && discoveryQueryParameters.getAlias() != "") {
          if (discoveryQueryParameters
              .getAlias()
              .toLowerCase()
              .contains(platformComponentProfile.getAlias().toLowerCase())) {
            filterMatched += 1;
          }
        }

        if (discoveryQueryParameters.getName() != null
            && discoveryQueryParameters.getName() != "") {
          if (discoveryQueryParameters
              .getName()
              .toLowerCase()
              .contains(platformComponentProfile.getName().toLowerCase())) {
            filterMatched += 1;
          }
        }

        if (discoveryQueryParameters.getExtraData() != null
            && discoveryQueryParameters.getExtraData() != "") {
          if (discoveryQueryParameters
              .getExtraData()
              .toLowerCase()
              .contains(platformComponentProfile.getExtraData().toLowerCase())) {
            filterMatched += 1;
          }
        }

        // if all filter matched
        if (totalFilterToApply == filterMatched) {
          // Add to the list
          filteredLis.add(platformComponentProfile);
        }
      }

    } else {

      filteredLis = list;
    }

    /*
     * Apply geo location filter
     */
    if (discoveryQueryParameters.getLocation() != null
        && discoveryQueryParameters.getLocation().getLatitude() != 0
        && discoveryQueryParameters.getLocation().getLongitude() != 0) {

      filteredLis = applyGeoLocationFilter(filteredLis, discoveryQueryParameters);
    }

    /*
     * Apply pagination
     */
    if ((discoveryQueryParameters.getMax() != 0) && (discoveryQueryParameters.getOffset() != 0)) {

      /*
       * Apply pagination
       */
      if (filteredLis.size() > discoveryQueryParameters.getMax()
          && filteredLis.size() > discoveryQueryParameters.getOffset()) {
        filteredLis =
            filteredLis.subList(
                discoveryQueryParameters.getOffset(), discoveryQueryParameters.getMax());
      } else if (filteredLis.size() > 100) {
        filteredLis = filteredLis.subList(discoveryQueryParameters.getOffset(), 100);
      }

    } else if (filteredLis.size() > 100) {
      filteredLis = filteredLis.subList(0, 100);
    }

    return filteredLis;
  }
  public void springTest() {

    System.out.println("Iniciando springTest() = ");

    List<PlatformComponentProfile> resultList = new ArrayList<>();

    try {

      /*
       * Construct a jsonObject whit the parameters
       */
      Gson gson = new Gson();
      JsonObject jsonObject = new JsonObject();
      jsonObject.addProperty(
          JsonAttNamesConstants.NAME_IDENTITY,
          "09A3B707D154r3B12C7CC626BCD7CF19EA8813B1B56A1B75E1C27335F8086C7ED588A7A06BCA67A289B73097FF67F5B1A0844FF2D550A6FCEFB66277EFDEB13A1");
      jsonObject.addProperty(
          JsonAttNamesConstants.DISCOVERY_PARAM,
          "{\"networkServiceType\":\"UNDEFINED\",\"platformComponentType\":\"ACTOR_INTRA_USER\"}");

      // Create a new RestTemplate instance
      HttpHeaders requestHeaders = new HttpHeaders();
      requestHeaders.setAccept(
          Collections.singletonList(new org.springframework.http.MediaType("application", "json")));
      HttpEntity<?> requestEntity = new HttpEntity<Object>(jsonObject.toString(), requestHeaders);
      RestTemplate restTemplate = new RestTemplate();
      restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

      ResponseEntity<String> responseEntity =
          restTemplate.exchange(WEB_SERVICE_URL, HttpMethod.POST, requestEntity, String.class);

      String respond = responseEntity.getBody();
      System.out.println("responseEntity = " + respond);

      /*
       * if respond have the result list
       */
      if (respond.length() > 39) {

        /*
         * Decode into a json object
         */
        JsonParser parser = new JsonParser();
        JsonObject respondJsonObject = (JsonObject) parser.parse(respond.toString());

        // JsonObject respondJsonObject = (JsonObject) parser.parse(stringRepresentation.getText());

        /*
         * Get the receivedList
         */
        resultList =
            gson.fromJson(
                respondJsonObject.get(JsonAttNamesConstants.RESULT_LIST).getAsString(),
                new TypeToken<List<PlatformComponentProfileCommunication>>() {}.getType());

        System.out.println("resultList.size() = " + resultList.size());

        for (PlatformComponentProfile componentProfile : resultList) {
          System.out.println(
              "componentProfile.getIdentityPublicKey() = "
                  + componentProfile.getIdentityPublicKey());
          System.out.println("componentProfile.getAlias() = " + componentProfile.getAlias());
          System.out.println("componentProfile.getName() = " + componentProfile.getName());
          System.out.println(
              "componentProfile.getExtraData() = " + componentProfile.getExtraData().length());
          System.out.println("----------------------------------------------------------------");
          System.out.println("\n");
        }

      } else {
        System.out.println(
            "WsCommunicationsCloudClientConnection - Requested list is not available, resultList.size() = "
                + resultList.size());
      }

    } catch (Exception e) {

      e.printStackTrace();
    }
  }