Filter.java 7.99 KB
Newer Older
1
package de.hftstuttgart.unifiedticketing.core;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * <b>Filtered Ticket Request</b><br>
 *<br>
 * This class provides a builder like interface,
 * to get a filtered list of Tickets.<br>
 * <br>
 * It provides some basic filter options by itself.
 * The request logic has to be provided from each System specific
 * implementation for it.
 *
 * @param <T> The type of Ticket this Filter implementation delivers
 * @param <F> The type of the System specific implementation of this class
 */
public abstract class Filter<T extends Ticket, F extends Filter<?,?>>
{
    private static Logger logger = Logging.getLogger(Filter.class.getName());

24
    protected int lastReceivedItemCount;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    protected Map<String, Object> setFilters;// = new HashMap<>();

    protected enum FilterNames
    {
        ASSIGNEEID,
        ASSIGNEENAME,
        DESCRIPTION_CONTAIN,
        DESCRIPTION_MATCH,
        IDS,
        LABELS,
        OPEN,
        PAGE,
        PAGINATION,
        TITLE_CONTAINS,
        TITLE_MATCH,
    }

    public Filter()
    {
        logger.log(Level.FINEST, String.format(
            "%s request builder started",
            this.getClass().getSimpleName()
        ));
        setFilters = new HashMap<>();
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

        lastReceivedItemCount = -1;
    }

    /**
     * Counter how many items received on last {@link #get()} call.
     *
     * As local filtering can lower the items delivered from {@link #get()},
     * you can't check that no more pages are available comparing the delivered item
     * count with your set page size.<br>
     *
     * @return item count received from external Ticketsystem on last call of {@link #get()}
     */
    public int getLastReceivedItemCount()
    {
        return lastReceivedItemCount;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    }

    /**
     * Add assignee id to query
     * @return this query builder
     */
    public F withAssigneeId(String id)
    {
        addToSet(FilterNames.ASSIGNEEID, this.setFilters, id);
        return (F) this;
    }

    /**
     * Add assignee name to query
     * @return this query builder
     */
    public F withAssigneeName(String name)
    {
        addToSet(FilterNames.ASSIGNEENAME, this.setFilters, name);
        return (F) this;
    }

    /**
     * adds description contain constraint
     * @param substring element to be contained in the description
     * @return this query builder
     */
    public F withDescriptionContain(String substring)
    {
        setFilters.put(FilterNames.DESCRIPTION_CONTAIN.name(), substring);
        logger.log(Level.FINEST, String.format("added constraint: %s %s", FilterNames.DESCRIPTION_CONTAIN.name(), substring));
        return (F) this;
    }

    /**
     * regex that the whole description must match
     * @param regex fully qualified regex
     * @return this query builder
     */
    public F withDescriptionMatch(String regex)
    {
        setFilters.put(FilterNames.DESCRIPTION_MATCH.name(), regex);
        logger.log(Level.FINEST, String.format("added constraint: %s %s", FilterNames.DESCRIPTION_MATCH.name(), regex));
        return (F) this;
    }

    /**
     * adds an ticket id to the constraint list
     * @param id
     * @return this query builder
     */
    public F withId(String id)
    {
        addToSet(FilterNames.IDS, this.setFilters, id);
        return (F) this;
    }

    /**
     * adds a constraint for only open considered tickets
     * @return this query builder
     */
    public F isOpen()
    {
        setFilters.put(FilterNames.OPEN.name(), true);
        logger.log(Level.FINEST, String.format("added constraint: %s %s", FilterNames.OPEN.name(), true));
        return (F) this;
    }

    /**
     * adds a constraint for only closed considered tickets
     * @return this query builder
     */
    public F isClosed()
    {
        setFilters.put(FilterNames.OPEN.name(), false);
        logger.log(Level.FINEST, String.format("added constraint: %s %s", FilterNames.OPEN.name(), false));
        return (F) this;
    }

    /**
     * add a label to the constraints list<br>
     *<br>
     * multiple calls of this delivers only tickets holding ALL given labels.
     * @param label
     * @return this query builder
     */
    public F withLabel(String label)
    {
        addToSet(FilterNames.LABELS, this.setFilters, label);
        return (F) this;
    }

    /**
     * set the page number of the pagination<br>
     *<br>
     * <b>Attention:</b> some systems paginate by default.
     * You can check that with {@link TicketSystem}s "has..." methods.
     * @param page
     * @return this query builder
     */
    public F setPage(int page)
    {
        this.setFilters.put(FilterNames.PAGE.name(), page);
        logger.log(Level.FINEST, String.format("set page: %d", page));
        return (F) this;
    }

    /**
     * defines the page size of a single pagination page<br>
     *<br>
     * <b>Attention:</b> some systems have a default in place, if nothing custom is set.
     * You can check that with {@link TicketSystem}s "has..." methods.
     * @param size
     * @return this query builder
     */
    public F setPageSize(int size)
    {
        this.setFilters.put(FilterNames.PAGINATION.name(), size);
        logger.log(Level.FINEST, String.format("set pagination size: %d", size));
        return (F) this;
    }

    /**
     * adds a constraint for title containing the given string
     * @param substring string the title must contain
     * @return this query builder
     */
    public F withTitleContain(String substring)
    {
        setFilters.put(FilterNames.TITLE_CONTAINS.name(), substring);
        logger.log(Level.FINEST, String.format("added constraint: %s %s", FilterNames.TITLE_CONTAINS.name(), substring));
        return (F) this;
    }

    /**
     * adds a regex constraint, that the title has to match
     * @param regex fully qualified regex
     * @return this query builder
     */
    public F withTitleMatch(String regex)
    {
        setFilters.put(FilterNames.TITLE_MATCH.name(), regex);
        logger.log(Level.FINEST, String.format("added constraint: %s %s", FilterNames.TITLE_MATCH.name(), regex));
        return (F) this;
    }

    /**
     * <b>Requesting the tickets</b><br>
     *<br>
     * This will  prepare the request according to the system specific implementation,
     * run the request, if needed post-filter the results and form it into a List
     * @return List of Tickets, matching all previously set constraints
     */
    public abstract List<T> get();

    protected static void addToSet(FilterNames filterName, Map<String, Object> map, String newValue)
    {
        logger.log(Level.FINEST, String.format("attempting to add new %s constraint", filterName.name()));
        Set<String> values;
        if (map.containsKey(filterName.name()))
        {
            logger.log(Level.FINEST, String.format("existing %s constraints found", filterName.name()));
            Object stored = map.get(filterName.name());
            if (stored instanceof Set<?>)
            {
                values = (Set<String>) stored;
            } else
            {
                logger.log(Level.WARNING, String.format(
                    "found filter object under key %s was not from Type %s but %s",
                    filterName.name(),
                    Set.class.getName(),
                    stored.getClass().getName()));

                values = new HashSet<>();
                map.put(filterName.name(), values);

                logger.log(Level.INFO, String.format(
                    "replaced wrong typed filter object under key %s with new instance of type %s",
                    filterName.name(),
                    Set.class.getName()));
            }
        } else
        {
            logger.log(Level.FINEST, String.format("no previous %s constraints found", filterName));
            values = new HashSet<>();
            map.put(filterName.name(), values);
        }

        values.add(newValue);
        logger.log(Level.FINEST, String.format("added constraint: %s %s", filterName.name(), newValue));
    }
}