@Override
  public void deleteFriend(String userId, String friendId) {
    Assert.notNull(userId, "userId must not be null");
    Assert.notNull(friendId, "friendId must not be null");

    User user = userRepository.findOne(userId);
    User friend = userRepository.findOne(friendId);

    if (user == null) {
      String msg = String.format("user not find by id %s", userId);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_FOUND);
    } else if (!user.getStatus().equals(AccountStatusEnum.ACTIVE)) {
      String msg = String.format("user status must be: %s", AccountStatusEnum.ACTIVE);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_ACTIVE);
    } else if (friend == null) {
      String msg = String.format("friend not find by id %s", friendId);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_FOUND);
    } else if (!friend.getStatus().equals(AccountStatusEnum.ACTIVE)) {
      String msg = String.format("friend status must be: %s", AccountStatusEnum.ACTIVE);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_ACTIVE);
    } else {
      // remove both relations
      relationRepository.remove(userId, friendId, RelationTypeEnum.FRIEND);
      relationRepository.remove(friendId, userId, RelationTypeEnum.FRIEND);
      accountStoreService.update(userId);
      accountStoreService.update(friendId);
    }
  }
  @Override
  public void sendFriendshipRequest(String userId, String friendId) {
    Assert.notNull(userId, "userId must not be null");
    Assert.notNull(friendId, "friendId must not be null");

    User user = userRepository.findOne(userId);
    User friend = userRepository.findOne(friendId);

    if (user == null) {
      String msg = String.format("user not find by id %s", userId);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_FOUND);
    } else if (!user.getStatus().equals(AccountStatusEnum.ACTIVE)) {
      String msg = String.format("user status must be: %s", AccountStatusEnum.ACTIVE);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_ACTIVE);
    } else if (friend == null) {
      String msg = String.format("friend not find by id %s", friendId);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_FOUND);
    } else if (!friend.getStatus().equals(AccountStatusEnum.ACTIVE)) {
      String msg = String.format("friend status must be: %s", AccountStatusEnum.ACTIVE);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_ACTIVE);
    } else {
      Notification notification = new Notification();
      notification.setFromId(userId);
      notification.setToId(friendId);
      notification.setType(NotificationTypeEnum.FRIEND);
      notificationService.addNotification(notification);
    }
  }
  @Override
  public void deleteMember(String userId, String communityId) {
    Assert.notNull(userId, "userId must not be null");
    Assert.notNull(communityId, "communityId must not be null");

    User user = userRepository.findOne(userId);
    Community community = communityRepository.findOne(communityId);

    if (user == null) {
      String msg = String.format("user not find by id %s", userId);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_FOUND);
    } else if (!user.getStatus().equals(AccountStatusEnum.ACTIVE)) {
      String msg = String.format("user status must be: %s", AccountStatusEnum.ACTIVE);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_ACTIVE);
    } else if (community == null) {
      String msg = String.format("community not find by id %s", communityId);
      logger.debug(msg);
      throw errorService.createException(
          AccountException.class, ErrorEnum.ERROR_COMMUNITY_NOT_FOUND);
    } else if (!community.getStatus().equals(AccountStatusEnum.ACTIVE)) {
      String msg = String.format("community status must be: %s", AccountStatusEnum.ACTIVE);
      logger.debug(msg);
      throw errorService.createException(
          AccountException.class, ErrorEnum.ERROR_COMMUNITY_NOT_ACTIVE);
    } else {
      relationRepository.remove(communityId, userId, RelationTypeEnum.MEMBER);
      accountStoreService.update(userId);
    }
  }
  @Override
  public void sendMemberRequest(String userId, String communityId) {
    Assert.notNull(userId, "userId must not be null");
    Assert.notNull(communityId, "communityId must not be null");

    User user = userRepository.findOne(userId);
    Community community = communityRepository.findOne(communityId);

    if (user == null) {
      String msg = String.format("user not find by id %s", userId);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_FOUND);
    } else if (!user.getStatus().equals(AccountStatusEnum.ACTIVE)) {
      String msg = String.format("user status must be: %s", AccountStatusEnum.ACTIVE);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_ACTIVE);
    } else if (community == null) {
      String msg = String.format("community not find by id %s", communityId);
      logger.debug(msg);
      throw errorService.createException(
          AccountException.class, ErrorEnum.ERROR_COMMUNITY_NOT_FOUND);
    } else if (!community.getStatus().equals(AccountStatusEnum.ACTIVE)) {
      String msg = String.format("community status must be: %s", AccountStatusEnum.ACTIVE);
      logger.debug(msg);
      throw errorService.createException(
          AccountException.class, ErrorEnum.ERROR_COMMUNITY_NOT_ACTIVE);
    } else {
      Notification notification = new Notification();
      notification.setFromId(userId);
      notification.setToId(communityId);
      notification.setType(NotificationTypeEnum.MEMBER);
      notificationService.addNotification(notification);
    }
  }
  @Override
  public void deletePublisher(String userId, String publisherId) {
    Assert.notNull(userId, "userId must not be null");
    Assert.notNull(publisherId, "publisherId must not be null");

    User user = userRepository.findOne(userId);
    User publisher = userRepository.findOne(publisherId);

    if (user == null) {
      String msg = String.format("user not find by id %s", userId);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_FOUND);
    } else if (!user.getStatus().equals(AccountStatusEnum.ACTIVE)) {
      String msg = String.format("user status must be: %s", AccountStatusEnum.ACTIVE);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_ACTIVE);
    } else if (publisher == null) {
      String msg = String.format("publisher not find by id %s", publisherId);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_FOUND);
    } else if (!publisher.getStatus().equals(AccountStatusEnum.ACTIVE)) {
      String msg = String.format("publisher status must be: %s", AccountStatusEnum.ACTIVE);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_ACTIVE);
    } else {
      if (publisher.getAccountType().toString().equals(AccountEnum.USER.toString())) {
        relationRepository.remove(userId, publisherId, RelationTypeEnum.PUBLISHER);
      } else {
        relationRepository.remove(publisherId, userId, RelationTypeEnum.SUBSCRIBER);
      }
      accountStoreService.update(userId);
    }
  }
  @Override
  public void deleteOwner(String userId, String communityId) {
    Assert.notNull(userId, "userId must not be null");
    Assert.notNull(communityId, "communityId must not be null");

    User user = userRepository.findOne(userId);
    Community community = communityRepository.findOne(communityId);

    if (user == null) {
      String msg = String.format("user not find by id %s", userId);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_FOUND);
    } else if (!user.getStatus().equals(AccountStatusEnum.ACTIVE)) {
      String msg = String.format("user status must be: %s", AccountStatusEnum.ACTIVE);
      logger.debug(msg);
      throw errorService.createException(AccountException.class, ErrorEnum.ERROR_USER_NOT_ACTIVE);
    } else if (community == null) {
      String msg = String.format("community not find by id %s", communityId);
      logger.debug(msg);
      throw errorService.createException(
          AccountException.class, ErrorEnum.ERROR_COMMUNITY_NOT_FOUND);
    } else if (!community.getStatus().equals(AccountStatusEnum.ACTIVE)) {
      String msg = String.format("community status must be: %s", AccountStatusEnum.ACTIVE);
      logger.debug(msg);
      throw errorService.createException(
          AccountException.class, ErrorEnum.ERROR_COMMUNITY_NOT_ACTIVE);
    } else {
      String[] typeRelations = new String[1];
      typeRelations[0] = RelationTypeEnum.OWNER.toString();

      List<Relation> relations =
          relationRepository.findByAccountRelationCriteria(
              new AccountRelationCriteria(communityId, typeRelations));

      if (relations != null
          && relations.size() == 1
          && relations.get(0).getUserIds() != null
          && relations.get(0).getUserIds().length > 1) {
        relationRepository.remove(communityId, userId, RelationTypeEnum.OWNER);
        accountStoreService.update(userId);
      } else {
        String msg =
            String.format("the sole owner %s of the user community active %s", userId, communityId);
        logger.debug(msg);
        throw errorService.createException(
            AccountException.class, ErrorEnum.ERROR_SOLE_OWNER_OF_THE_USER_COMMUNITY_ACTIVE);
      }
    }
  }