@TransactionAttribute(TransactionAttributeType.SUPPORTS)
  public Network verifyNetworkByKey(NullableWrapper<Network> network, AccessKey key) {
    Network stored;

    // case network is not defined
    if (network == null || network.getValue() == null) {
      return null;
    }

    Network update = network.getValue();

    if (update.getId() != null) {
      stored = networkDAO.getById(update.getId());
    } else {
      stored = networkDAO.findByName(update.getName());
    }
    if (stored != null) {
      if (stored.getKey() != null) {
        if (!stored.getKey().equals(update.getKey())) {
          throw new HiveException(Messages.INVALID_NETWORK_KEY, FORBIDDEN.getStatusCode());
        }
        if (!accessKeyService.hasAccessToNetwork(key, stored)) {
          throw new HiveException(Messages.NO_ACCESS_TO_NETWORK, FORBIDDEN.getStatusCode());
        }
      }
    }
    return stored;
  }
  @TransactionAttribute(TransactionAttributeType.SUPPORTS)
  public Network createOrVeriryNetwork(NullableWrapper<Network> network) {
    Network stored;
    // case network is not defined
    if (network == null || network.getValue() == null) {
      return null;
    }
    Network update = network.getValue();

    if (update.getId() != null) {
      stored = networkDAO.getById(update.getId());
    } else {
      stored = networkDAO.findByName(update.getName());
    }

    if (stored != null) {
      if (stored.getKey() != null) {
        if (!stored.getKey().equals(update.getKey())) {
          throw new HiveException(Messages.INVALID_NETWORK_KEY, FORBIDDEN.getStatusCode());
        }
      }
    } else {
      if (update.getId() != null) {
        throw new HiveException(Messages.INVALID_REQUEST_PARAMETERS, BAD_REQUEST.getStatusCode());
      }
      stored = networkDAO.createNetwork(update);
    }
    assert (stored != null);
    return stored;
  }
  @TransactionAttribute(TransactionAttributeType.SUPPORTS)
  public Network createOrUpdateNetworkByUser(NullableWrapper<Network> network, User user) {
    Network stored;

    // case network is not defined
    if (network == null || network.getValue() == null) {
      return null;
    }

    Network update = network.getValue();

    if (update.getId() != null) {
      stored = networkDAO.getWithDevicesAndDeviceClasses(update.getId());
    } else {
      stored = networkDAO.findByName(update.getName());
    }

    if (stored != null) {
      if (stored.getKey() != null) {
        if (!stored.getKey().equals(update.getKey())) {
          throw new HiveException(Messages.INVALID_NETWORK_KEY, FORBIDDEN.getStatusCode());
        }
      }
      if (!userService.hasAccessToNetwork(user, stored)) {
        throw new HiveException(Messages.NO_ACCESS_TO_NETWORK, FORBIDDEN.getStatusCode());
      }
    } else if (user.isAdmin()) {
      if (update.getId() != null) {
        throw new HiveException(Messages.INVALID_REQUEST_PARAMETERS, BAD_REQUEST.getStatusCode());
      }
      stored = networkDAO.createNetwork(update);

    } else {
      throw new HiveException(Messages.NETWORK_CREATION_NOT_ALLOWED, FORBIDDEN.getStatusCode());
    }
    return stored;
  }