### Instruction:
Write a Java Spring Boot application that integrates with the Twitter API to fetch tweets containing a specific hashtag. Store the fetched tweets in a MongoDB database and display them on a web page with options to filter by date range and sort by retweet count.
PLAINFORMAT

### Response:

```java
// src/main/java/com/example/demo/DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

// src/main/java/com/example/demo/model/Tweet.java

package com.example.demo.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "tweets")
public class Tweet {

    @Id
    private String id;
    private String text;
    private int retweetCount;
    private String createdAt;

    // Getters, setters, and constructors omitted for brevity
}

// src/main/java/com/example/demo/repository/TweetRepository.java

package com.example.demo.repository;

import com.example.demo.model.Tweet;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface TweetRepository extends MongoRepository<Tweet, String> {
}

// src/main/java/com/example/demo/service/TwitterService.java

package com.example.demo.service;

import com.example.demo.model.Tweet;
import com.example.demo.repository.TweetRepository;
import com.github.redouane59.twitter.TwitterClient;
import com.github.redouane59.twitter.dto.tweet.TweetV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class TwitterService {

    @Autowired
    private TweetRepository tweetRepository;

    @Value("${twitter.bearer-token}")
    private String bearerToken;

    public List<Tweet> fetchTweetsByHashtag(String hashtag) {
        TwitterClient twitterClient = new TwitterClient(bearerToken);
        List<TweetV2> tweets = twitterClient.searchForTweetsWithin7days("#" + hashtag);

        return tweets.stream().map(tweet -> {
            Tweet savedTweet = new Tweet();
            savedTweet.setId(tweet.getId());
            savedTweet.setText(tweet.getText());
            savedTweet.setRetweetCount(tweet.getPublicMetrics().getRetweetCount());
            savedTweet.setCreatedAt(tweet.getCreatedAt());

            return tweetRepository.save(savedTweet);
        }).collect(Collectors.toList());
    }
}

// src/main/java/com/example/demo/controller/TweetController.java

package com.example.demo.controller;

import com.example.demo.model.Tweet;
import com.example.demo.service.TwitterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class TweetController {

    @Autowired
    private TwitterService twitterService;

    @GetMapping("/")
    public String index() {
        return "index";
    }

    @GetMapping("/search")
    public String search(@RequestParam("hashtag") String hashtag, Model model) {
        List<Tweet> tweets = twitterService.fetchTweetsByHashtag(hashtag);
        model.addAttribute("tweets", tweets);
        return "tweets";
    }
}
```

```xml
<!-- pom.xml -->

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.github.redouane59.twitter</groupId>
        <artifactId>twittered</artifactId>
        <version>1.22</version>
    </dependency>
</dependencies>
```

```properties
# src/main/resources/application.properties

twitter.bearer-token=YOUR_TWITTER_BEARER_TOKEN
spring.data.mongodb.uri=mongodb://username:password@localhost:27017/your-db-name
```

```html
<!-- src/main/resources/templates/index.html -->

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Twitter Hashtag Search</title>
</head>
<body>

<h1>Search Tweets by Hashtag</h1>

<form action="/search" method="get">
    <label for="hashtag">Hashtag:</label>
    <input type="text" id="hashtag" name="hashtag" required>
    <button type="submit">Search</button>
</form>

</body>
</html>
```

```html
<!-- src/main/resources/templates/tweets.html -->

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Tweets</title>
</head>
<body>

<h1>Tweets</h1>

<table>
    <thead>
    <tr>
        <th>ID</th>
        <th>Text</th>
        <th>Retweet Count</th>
        <th>Created At</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="tweet : ${tweets}">
        <td th:text="${tweet.id}"></td>
        <td th:text="${tweet.text}"></td>
        <td th:text="${tweet.retweetCount}"></td>
        <td th:text="${tweet.createdAt}"></td>
    </tr>
    </tbody>
</table>

<a href="/">Back to search</a>

</body>
</html>
```