Beispiel #1
0
 public URI build() {
   StringBuilder builder = new StringBuilder(url);
   try {
     if (!params.isEmpty()) {
       builder.append("?");
       boolean first = true;
       for (String key : params.keySet()) {
         if (!first) {
           builder.append("&");
         } else {
           first = false;
         }
         for (String value : params.get(key)) {
           builder.append(key + "=" + UriUtils.encodeQueryParam(value, "UTF-8"));
         }
       }
     }
     return new URI(builder.toString());
   } catch (UnsupportedEncodingException ex) {
     // should not happen, UTF-8 is always supported
     throw new IllegalStateException(ex);
   } catch (URISyntaxException ex) {
     throw new IllegalArgumentException(
         "Could not create URI from [" + builder + "]: " + ex, ex);
   }
 }
 public MultiValueMap<String, Connection<?>> findConnectionsToUsers(
     MultiValueMap<String, String> providerUsers) {
   if (providerUsers == null || providerUsers.isEmpty()) {
     throw new IllegalArgumentException("Unable to execute find: no providerUsers provided");
   }
   StringBuilder providerUsersCriteriaSql = new StringBuilder();
   MapSqlParameterSource parameters = new MapSqlParameterSource();
   parameters.addValue("userId", userId);
   for (Iterator<Entry<String, List<String>>> it = providerUsers.entrySet().iterator();
       it.hasNext(); ) {
     Entry<String, List<String>> entry = it.next();
     String providerId = entry.getKey();
     providerUsersCriteriaSql
         .append("providerId = :providerId_")
         .append(providerId)
         .append(" and providerUserId in (:providerUserIds_")
         .append(providerId)
         .append(")");
     parameters.addValue("providerId_" + providerId, providerId);
     parameters.addValue("providerUserIds_" + providerId, entry.getValue());
     if (it.hasNext()) {
       providerUsersCriteriaSql.append(" or ");
     }
   }
   List<Connection<?>> resultList =
       new NamedParameterJdbcTemplate(jdbcTemplate)
           .query(
               selectFromUserConnection()
                   + " where userId = :userId and "
                   + providerUsersCriteriaSql
                   + " order by providerId, rank",
               parameters,
               connectionMapper);
   MultiValueMap<String, Connection<?>> connectionsForUsers =
       new LinkedMultiValueMap<String, Connection<?>>();
   for (Connection<?> connection : resultList) {
     String providerId = connection.getKey().getProviderId();
     List<String> userIds = providerUsers.get(providerId);
     List<Connection<?>> connections = connectionsForUsers.get(providerId);
     if (connections == null) {
       connections = new ArrayList<Connection<?>>(userIds.size());
       for (int i = 0; i < userIds.size(); i++) {
         connections.add(null);
       }
       connectionsForUsers.put(providerId, connections);
     }
     String providerUserId = connection.getKey().getProviderUserId();
     int connectionIndex = userIds.indexOf(providerUserId);
     connections.set(connectionIndex, connection);
   }
   return connectionsForUsers;
 }
  /**
   * Returns the tempalte variables for the sort parameter.
   *
   * @param parameter must not be {@literal null}.
   * @return
   * @since 1.7
   */
  public TemplateVariables getSortTemplateVariables(
      MethodParameter parameter, UriComponents template) {

    String sortParameter = getSortParameter(parameter);
    MultiValueMap<String, String> queryParameters = template.getQueryParams();
    boolean append = !queryParameters.isEmpty();

    if (queryParameters.containsKey(sortParameter)) {
      return TemplateVariables.NONE;
    }

    String description = String.format("pagination.%s.description", sortParameter);
    VariableType type = append ? REQUEST_PARAM_CONTINUED : REQUEST_PARAM;
    return new TemplateVariables(new TemplateVariable(sortParameter, type, description));
  }
  @Override
  public MultiValueMap<String, Connection<?>> findConnectionsToUsers(
      MultiValueMap<String, String> providerUsers) {
    if (providerUsers == null || providerUsers.isEmpty()) {
      throw new IllegalArgumentException("Unable to execute find: no providerUsers provided");
    }

    List<Connection<?>> allConnections =
        providerUsers
            .entrySet()
            .stream()
            .flatMap(
                entry ->
                    entry
                        .getValue()
                        .stream()
                        .map(u -> LdoD.getInstance().getUserConnection(userId, entry.getKey(), u)))
            .sorted((uc1, uc2) -> compareByProviderIdAndRank(uc1, uc2))
            .map(uc -> mapUserConnection(uc))
            .collect(Collectors.toList());

    MultiValueMap<String, Connection<?>> connectionsForUsers =
        new LinkedMultiValueMap<String, Connection<?>>();
    for (Connection<?> connection : allConnections) {
      String providerId = connection.getKey().getProviderId();
      List<String> userIds = providerUsers.get(providerId);
      List<Connection<?>> connections = connectionsForUsers.get(providerId);
      if (connections == null) {
        connections = new ArrayList<Connection<?>>(userIds.size());
        for (int i = 0; i < userIds.size(); i++) {
          connections.add(null);
        }
        connectionsForUsers.put(providerId, connections);
      }
      String providerUserId = connection.getKey().getProviderUserId();
      int connectionIndex = userIds.indexOf(providerUserId);
      connections.set(connectionIndex, connection);
    }
    return connectionsForUsers;
  }