/*
   * @see org.societies.api.internal.privacytrust.trust.ITrustBroker#removeTrustRelationships(org.societies.api.privacytrust.trust.TrustQuery)
   */
  @Override
  @Async
  public Future<Boolean> removeTrustRelationships(final TrustQuery query) throws TrustException {

    if (query == null) {
      throw new NullPointerException("query can't be null");
    }

    LOG.debug("Removing trust relationship matching query '{}'", query);

    if (!this.trustNodeMgr.getMyIds().contains(query.getTrustorId())) {
      throw new TrustAccessControlException(
          "Trustor '" + query.getTrustorId() + "' is not recognised as a local CSS");
    }

    if (this.isLocalQuery(query)) {
      // L O C A L
      LOG.debug("query '{}' is LOCAL", query);
      return new AsyncResult<Boolean>(this.removeLocalTrustRelationships(query));
    } else {
      // R E M O T E  ( I N T R A - C S S )
      LOG.debug("query '{}' is REMOTE", query);
      return new AsyncResult<Boolean>(this.removeRemoteTrustRelationships(query));
    }
  }
  private Set<ITrustedEntity> retrieveTrustedEntities(final TrustQuery query)
      throws TrustException {

    final Set<ITrustedEntity> result = new LinkedHashSet<ITrustedEntity>();

    try {
      if (this.trustRepo == null) {
        throw new TrustBrokerException("ITrustRepository service is not available");
      }
      if (query.getTrusteeId() != null) {
        final ITrustedEntity trustedEntity =
            this.trustRepo.retrieveEntity(query.getTrustorId(), query.getTrusteeId());
        if (trustedEntity != null) result.add(trustedEntity);
      } else {
        result.addAll(
            this.trustRepo.retrieveEntities(
                query.getTrustorId(), query.getTrusteeType(), query.getTrustValueType()));
      }

    } catch (ServiceUnavailableException sue) {
      throw new TrustBrokerException(sue.getLocalizedMessage(), sue);
    }

    return result;
  }
  /*
   * @see org.societies.api.internal.privacytrust.trust.ITrustBroker#unregisterTrustUpdateListener(org.societies.api.privacytrust.trust.event.ITrustUpdateEventListener, org.societies.api.privacytrust.trust.TrustQuery)
   */
  @Override
  public void unregisterTrustUpdateListener(
      final ITrustUpdateEventListener listener, final TrustQuery query) throws TrustException {

    if (query == null) throw new NullPointerException("query can't be null");

    if (query.getTrusteeType() != null) {
      this.doUnregisterTrustUpdateListenerByType(
          null, listener, query.getTrustorId(), query.getTrusteeType(), query.getTrustValueType());
    } else {
      this.doUnregisterTrustUpdateListener(
          null, listener, query.getTrustorId(), query.getTrusteeId(), query.getTrustValueType());
    }
  }
  private boolean removeLocalTrustRelationships(final TrustQuery query) throws TrustException {

    try {
      if (this.trustRepo == null) {
        throw new TrustBrokerException("ITrustRepository service is not available");
      }
      if (query.getTrusteeId() != null) {
        return this.trustRepo.removeEntity(query.getTrustorId(), query.getTrusteeId());
      } else {
        return this.trustRepo.removeEntities(
            query.getTrustorId(), query.getTrusteeType(), query.getTrustValueType());
      }
    } catch (ServiceUnavailableException sue) {
      throw new TrustBrokerException(sue.getLocalizedMessage(), sue);
    }
  }
  private boolean isLocalQuery(final TrustQuery query) {

    return this.trustNodeMgr.getMyIds().contains(query.getTrustorId())
        && this.trustNodeMgr.isMaster();
  }