View Javadoc
1   /*
2    * Copyright 2019-2026 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.comparator.spring.web;
18  
19  import static java.util.Objects.isNull;
20  import static java.util.Objects.nonNull;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Optional;
25  import org.bremersee.comparator.model.SortOrder;
26  import org.bremersee.comparator.model.SortOrderItem;
27  import org.bremersee.comparator.model.SortOrderTextSeparators;
28  import org.springframework.core.MethodParameter;
29  import org.springframework.lang.Nullable;
30  import org.springframework.web.bind.annotation.ValueConstants;
31  
32  /**
33   * The sort order handler method argument resolver support.
34   *
35   * @author Christian Bremer
36   */
37  public abstract class SortOrderHandlerMethodArgumentResolverSupport {
38  
39    private SortOrderTextSeparators textSeparators = SortOrderTextSeparators.defaults();
40  
41    /**
42     * Instantiates a new sort order handler method argument resolver support.
43     */
44    protected SortOrderHandlerMethodArgumentResolverSupport() {
45      super();
46    }
47  
48    /**
49     * Sets text separators.
50     *
51     * @param textSeparators the text separators
52     */
53    public void setTextSeparators(SortOrderTextSeparators textSeparators) {
54      if (nonNull(textSeparators)) {
55        this.textSeparators = textSeparators;
56      }
57    }
58  
59    /**
60     * Gets parameter name.
61     *
62     * @param parameter the parameter
63     * @return the parameter name
64     */
65    protected String getParameterName(@Nullable MethodParameter parameter) {
66      return Optional.ofNullable(parameter)
67          .map(p -> getParameterName(p.getParameterAnnotation(SortOrderRequestParam.class)))
68          .or(() -> Optional.ofNullable(parameter)
69              .map(MethodParameter::getParameterName))
70          .filter(name -> !name.isEmpty())
71          .orElse("sort");
72    }
73  
74    private String getParameterName(SortOrderRequestParam requestParam) {
75      if (isNull(requestParam)) {
76        return null;
77      }
78      if (!requestParam.name().isEmpty()) {
79        return requestParam.name();
80      }
81      return null;
82    }
83  
84    /**
85     * Gets default from annotation or fallback.
86     *
87     * @param parameter the parameter
88     * @return the default from annotation or fallback
89     */
90    protected SortOrder getDefaultFromAnnotationOrFallback(MethodParameter parameter) {
91      return Optional.ofNullable(parameter.getParameterAnnotation(SortOrderRequestParam.class))
92          .map(SortOrderRequestParam::defaultSort)
93          .filter(defaultValue -> !ValueConstants.DEFAULT_NONE.equals(defaultValue))
94          .map(defaultValue -> SortOrder.fromSortOrderText(defaultValue, textSeparators))
95          .orElseGet(SortOrder::unsorted);
96    }
97  
98    /**
99     * Parse parameter values.
100    *
101    * @param parameterValues the parameter values
102    * @return the sort order
103    */
104   protected SortOrder parseParameterValues(List<String> parameterValues) {
105     List<SortOrderItem> sortOrderItems = new ArrayList<>();
106     for (String parameterValue : parameterValues) {
107       sortOrderItems.addAll(SortOrder.fromSortOrderText(parameterValue, textSeparators).getItems());
108     }
109     return new SortOrder(sortOrderItems);
110   }
111 
112 }