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