summaryrefslogtreecommitdiff
path: root/src/main/java/design/model/course_search/CurrentSearchQuery.java
blob: 3916f0a6a48d2ca759596ca0a3f8010c20238475 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package design.model.course_search;

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

import design.persistence.MasterDatabase;

/* 
 * Represents the state of our current search.
 */
public class CurrentSearchQuery {
    public static final CurrentSearchQuery INSTANCE = new CurrentSearchQuery();
    

    MasterDatabase db = MasterDatabase.INSTANCE;
    private CourseList query = db.getCourseList();
    private final List<CourseSorter> filters = new ArrayList<CourseSorter>();

    // add a new filter
    public void addFilter(CourseSorter filter)
    {
        filters.add(filter);
    }

    // clear the filters
    public void clearFilters()
    {
        filters.clear();
    }

    // print out the filters ( this is for the model checking used filters )
    public String printFilters()
    {
        String filterResult = "";

        // no filters? let the user know.
        if(filters.size() == 0)
        {
            return "nothing";
        }

        for( CourseSorter f : filters)
        {
            filterResult += f.toString() + " --> ";
        }

        // very silly way of removing the last arrow from our filter list. it's kind of dumb but it works fine.
        filterResult = filterResult.substring(0, filterResult.length() - 5); 
        filterResult += "\n";

        return filterResult;
    }

    // get all the filters
    public List<CourseSorter> getFilters()
    {
        return filters;
    }

    // get our current query.
    public CourseList getQueryResult()
    {
        return query;
    }

    public void search(String searchQuery) 
    {
        List<ICourse> courses = db.getCourseList().getCourses().stream()
            .filter(s -> s != null && s.toString().toLowerCase().contains(searchQuery.toLowerCase()))
            .collect(Collectors.toList());

        // Start with filtered base
        CourseList current = new CourseList();
        current.setCourses(courses);

        // Sequentially apply filters
        for (CourseSorter filter : filters) 
        {
            current.setSorter(filter);
            current = current.groupByCurrentSorter(); // regroup after each
        }

        query = current;
    }
}