View Javadoc
1   /*
2    * Copyright 2024-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.boot;
18  
19  import java.util.Optional;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.bremersee.comparator.model.SortOrderTextSeparators;
23  import org.bremersee.comparator.spring.converter.SortOrderConverter;
24  import org.bremersee.comparator.spring.converter.SortOrderItemConverter;
25  import org.bremersee.comparator.spring.mapper.SortMapper;
26  import org.springframework.boot.autoconfigure.AutoConfiguration;
27  import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
28  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
29  import org.springframework.boot.context.event.ApplicationReadyEvent;
30  import org.springframework.boot.context.properties.EnableConfigurationProperties;
31  import org.springframework.context.annotation.Bean;
32  import org.springframework.context.event.EventListener;
33  import org.springframework.util.ClassUtils;
34  import org.springframework.util.StringUtils;
35  
36  /**
37   * The sort order autoconfiguration.
38   *
39   * @author Christian Bremer
40   */
41  @ConditionalOnClass(name = {
42      "org.bremersee.comparator.spring.converter.SortOrderConverter",
43      "org.bremersee.comparator.spring.converter.SortOrderItemConverter",
44      "org.bremersee.comparator.spring.mapper.SortMapper"
45  })
46  @AutoConfiguration
47  @EnableConfigurationProperties(SortOrderConverterProperties.class)
48  public class SortOrderAutoConfiguration {
49  
50    private static final Log log = LogFactory.getLog(SortOrderAutoConfiguration.class);
51  
52    private final SortOrderTextSeparators separators;
53  
54    /**
55     * Instantiates a new sort order converter autoconfiguration.
56     *
57     * @param properties the properties
58     */
59    public SortOrderAutoConfiguration(SortOrderConverterProperties properties) {
60      SortOrderTextSeparators defaults = SortOrderTextSeparators.defaults();
61      this.separators = SortOrderTextSeparators.builder()
62          .argumentSeparator(Optional.ofNullable(properties.getArgumentSeparator())
63              .filter(StringUtils::hasText)
64              .orElse(defaults.getArgumentSeparator()))
65          .chainSeparator(Optional.ofNullable(properties.getChainSeparator())
66              .filter(StringUtils::hasText)
67              .orElse(defaults.getChainSeparator()))
68          .build();
69    }
70  
71    /**
72     * Init.
73     */
74    @EventListener(ApplicationReadyEvent.class)
75    public void init() {
76      log.info(String.format("""
77              
78              *********************************************************************************
79              * %s
80              *********************************************************************************""",
81          ClassUtils.getUserClass(getClass()).getSimpleName()));
82    }
83  
84    /**
85     * Creates sort order converter.
86     *
87     * @return the sort order converter
88     */
89    @ConditionalOnMissingBean
90    @Bean
91    public SortOrderConverter sortOrderConverter() {
92      return new SortOrderConverter(separators);
93    }
94  
95    /**
96     * Creates sort order item converter.
97     *
98     * @return the sort order item converter
99     */
100   @ConditionalOnMissingBean
101   @Bean
102   public SortOrderItemConverter sortOrderItemConverter() {
103     return new SortOrderItemConverter(separators);
104   }
105 
106   /**
107    * Creates sort mapper.
108    *
109    * @param sortOrderConverter the sort order converter
110    * @return the sort mapper
111    */
112   @ConditionalOnMissingBean
113   @Bean
114   public SortMapper defaultSortMapper(SortOrderConverter sortOrderConverter) {
115     return SortMapper.defaultSortMapper(sortOrderConverter);
116   }
117 
118 }