/**
   * 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;
  }
  /**
   * 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;
  }