Project

General

Profile

Download (12.2 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.filter;
11

    
12
import java.io.Serializable;
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import java.util.Collections;
16
import java.util.List;
17
import java.util.UUID;
18

    
19
import eu.etaxonomy.cdm.filter.LogicFilter.Op;
20
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
21
import eu.etaxonomy.cdm.model.location.NamedArea;
22
import eu.etaxonomy.cdm.model.name.Rank;
23
import eu.etaxonomy.cdm.model.taxon.Classification;
24
import eu.etaxonomy.cdm.model.taxon.Taxon;
25
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
26

    
27
/**
28
 * @author a.mueller
29
 */
30
public class TaxonNodeFilter implements Serializable{
31

    
32
    private static final long serialVersionUID = 2292886683987183999L;
33

    
34
    private List<LogicFilter<TaxonNode>> subtrees = new ArrayList<>();
35
    private List<LogicFilter<TaxonNode>> taxonNodes = new ArrayList<>();
36
    private List<LogicFilter<Classification>> classifications = new ArrayList<>();
37
    private List<LogicFilter<Taxon>> taxa = new ArrayList<>();
38
    private LogicFilter<Rank> rankMin = null;
39
    private LogicFilter<Rank> rankMax = null;
40

    
41
    private List<LogicFilter<NamedArea>> areaFilter = new ArrayList<>();
42

    
43
    private List<LogicFilter<PresenceAbsenceTerm>> distributionStatusFilter = new ArrayList<>();
44

    
45
    private boolean includeRootNodes = false;
46

    
47
    private boolean includeUnpublished = false;
48

    
49
    private ORDER orderBy = null;
50

    
51
    public enum ORDER{
52
        ID("tn.id"),
53
        TREEINDEX("tn.treeIndex"),
54
        TREEINDEX_DESC("tn.treeIndex DESC");
55
        String hql;
56
        private ORDER(String hql){
57
            this.hql = hql;
58
        }
59
        public String getHql(){
60
            return hql;
61
        }
62
    }
63

    
64
//************************ FACTORY ***************************/
65

    
66
    public static TaxonNodeFilter NewInstance(){
67
        return NewInstance(null, null, null, null, null, null, null);
68
    }
69

    
70
    public static TaxonNodeFilter NewTaxonNodeInstance(UUID taxonNodeUuid){
71
        return new TaxonNodeFilter().orTaxonNode(taxonNodeUuid);
72
    }
73
    public static TaxonNodeFilter NewTaxonNodeInstance(TaxonNode taxonNode){
74
        return new TaxonNodeFilter().orTaxonNode(taxonNode);
75
    }
76

    
77
    public static TaxonNodeFilter NewClassificationInstance(UUID classificationUuid){
78
        return new TaxonNodeFilter().orClassification(classificationUuid);
79
    }
80
    public static TaxonNodeFilter NewClassificationInstance(Classification classification){
81
        return new TaxonNodeFilter().orClassification(classification);
82
    }
83

    
84
    public static TaxonNodeFilter NewSubtreeInstance(UUID subtreeUuid){
85
        return new TaxonNodeFilter().orSubtree(subtreeUuid);
86
    }
87
    public static TaxonNodeFilter NewSubtreeInstance(TaxonNode subtree){
88
        return new TaxonNodeFilter().orSubtree(subtree);
89
    }
90

    
91
    public static TaxonNodeFilter NewTaxonInstance(UUID taxonUuid){
92
        return new TaxonNodeFilter().orTaxon(taxonUuid);
93
    }
94

    
95
    public static TaxonNodeFilter NewTaxonInstance(Taxon taxon){
96
        return new TaxonNodeFilter().orTaxon(taxon);
97
    }
98

    
99
    public static TaxonNodeFilter NewRankInstance(Rank rankMin, Rank rankMax){
100
        return new TaxonNodeFilter().setRankMin(rankMin).setRankMax(rankMax);
101
    }
102

    
103
    public static TaxonNodeFilter NewAreaInstance(NamedArea area){
104
        return new TaxonNodeFilter().orArea(area);
105
    }
106
    public static TaxonNodeFilter NewAreaInstance(UUID areaUuid){
107
        return new TaxonNodeFilter().orArea(areaUuid);
108
    }
109

    
110
    public static TaxonNodeFilter NewInstance(Collection<UUID> classificationUuids,
111
            Collection<UUID> subtreeUuids, Collection<UUID> taxonNodeUuids,
112
            Collection<UUID> taxonUuids, Collection<UUID> areaUuids,
113
            UUID minRank, UUID maxRank){
114

    
115
        TaxonNodeFilter result = new TaxonNodeFilter().setRankMin(minRank).setRankMax(maxRank);
116
        classificationUuids = classificationUuids == null ? new ArrayList<>(): classificationUuids;
117
        subtreeUuids = subtreeUuids == null ? new ArrayList<>(): subtreeUuids;
118
        taxonNodeUuids = taxonNodeUuids == null ? new ArrayList<>(): taxonNodeUuids;
119
        taxonUuids = taxonUuids == null ? new ArrayList<>(): taxonUuids;
120
        areaUuids = areaUuids == null ? new ArrayList<>(): areaUuids;
121

    
122
        for (UUID uuid : classificationUuids){
123
            result.orClassification(uuid);
124
        }
125
        for (UUID uuid : subtreeUuids){
126
            result.orSubtree(uuid);
127
        }
128
        for (UUID uuid : taxonNodeUuids){
129
            result.orTaxonNode(uuid);
130
        }
131
        for (UUID uuid : taxonUuids){
132
            result.orTaxon(uuid);
133
        }
134
        for (UUID uuid : areaUuids){
135
            result.orArea(uuid);
136
        }
137
        return result;
138
    }
139

    
140
// ************************ CONSTRUCTOR *******************/
141

    
142
    private TaxonNodeFilter(){
143
        reset();
144
    }
145

    
146
    public TaxonNodeFilter(NamedArea area){
147
        reset();
148
        LogicFilter<NamedArea> filter = new LogicFilter<>(area);
149
        areaFilter.add(filter);
150
    }
151

    
152
// ********************** reset *****************************/
153

    
154
    public void reset(){
155
        subtrees = new ArrayList<>();
156
        resetAreas();
157
        resetRanks();
158
        resetDistributionStatus();
159
        resetTaxonNodes();
160
        resetClassifications();
161
        resetTaxa();
162
    }
163

    
164
    private void resetDistributionStatus() {
165
        distributionStatusFilter = new ArrayList<>();
166
    }
167

    
168
    private void resetTaxonNodes() {
169
        taxonNodes = new ArrayList<>();
170
    }
171

    
172
    private void resetClassifications() {
173
        classifications = new ArrayList<>();
174
    }
175

    
176
    private void resetTaxa() {
177
        taxa = new ArrayList<>();
178
    }
179

    
180
    private void resetAreas() {
181
        areaFilter = new ArrayList<>();
182
    }
183

    
184
    private void resetRanks() {
185
        rankMin = null;
186
        rankMax = null;
187
    }
188

    
189
//*************************************
190

    
191
    public List<LogicFilter<TaxonNode>>getSubtreeFilter(){
192
        return Collections.unmodifiableList(subtrees);
193
    }
194

    
195
    public List<LogicFilter<TaxonNode>>getTaxonNodesFilter(){
196
        return Collections.unmodifiableList(taxonNodes);
197
    }
198

    
199

    
200
    public List<LogicFilter<Classification>>getClassificationFilter(){
201
        return Collections.unmodifiableList(classifications);
202
    }
203

    
204
    public List<LogicFilter<Taxon>>getTaxonFilter(){
205
        return Collections.unmodifiableList(taxa);
206
    }
207

    
208
    public List<LogicFilter<NamedArea>>getAreaFilter(){
209
        return Collections.unmodifiableList(areaFilter);
210
    }
211

    
212
    public List<LogicFilter<PresenceAbsenceTerm>>getDistributionStatusFilter(){
213
        return Collections.unmodifiableList(distributionStatusFilter);
214
    }
215

    
216
    public TaxonNodeFilter orSubtree(TaxonNode taxonNode){
217
        subtrees.add( new LogicFilter<>(taxonNode, Op.OR));
218
        return this;
219
    }
220

    
221
    public TaxonNodeFilter notSubtree(TaxonNode taxonNode){
222
        subtrees.add( new LogicFilter<>(taxonNode, Op.NOT));
223
        return this;
224
    }
225

    
226
    public TaxonNodeFilter orSubtree(UUID taxonNodeUuid){
227
        subtrees.add( new LogicFilter<>(TaxonNode.class, taxonNodeUuid, Op.OR));
228
        return this;
229
    }
230

    
231
    /**
232
     * Adds a single {@link TaxonNode} to the filter.<BR><BR>
233
     * NOTE: this adds only the node to the filter, not it's children!
234
     */
235
    public TaxonNodeFilter orTaxonNode(TaxonNode taxonNode){
236
        taxonNodes.add( new LogicFilter<>(taxonNode, Op.OR));
237
        return this;
238
    }
239
    /**
240
     * Adds a single {@link TaxonNode} to the filter.<BR><BR>
241
     * NOTE: this adds only the node to the filter, not it's children!
242
     */
243
    public TaxonNodeFilter orTaxonNode(UUID uuid){
244
        taxonNodes.add( new LogicFilter<>(TaxonNode.class, uuid, Op.OR));
245
        return this;
246
    }
247

    
248
    public TaxonNodeFilter notTaxonNode(TaxonNode taxonNode){
249
        taxonNodes.add( new LogicFilter<>(taxonNode, Op.NOT));
250
        return this;
251
    }
252
    public TaxonNodeFilter andTaxonNode(TaxonNode taxonNode){
253
        taxonNodes.add(new LogicFilter<>(taxonNode, Op.AND));
254
        return this;
255
    }
256

    
257
    public TaxonNodeFilter orTaxon(Taxon taxon){
258
        taxa.add( new LogicFilter<>(taxon, Op.OR));
259
        return this;
260
    }
261

    
262
    public TaxonNodeFilter orTaxon(UUID uuid){
263
        taxa.add( new LogicFilter<>(Taxon.class, uuid, Op.OR));
264
        return this;
265
    }
266

    
267
    public TaxonNodeFilter orArea(UUID uuid){
268
        areaFilter.add( new LogicFilter<>(NamedArea.class, uuid, Op.OR));
269
        return this;
270
    }
271
    public TaxonNodeFilter orArea(NamedArea area){
272
        areaFilter.add( new LogicFilter<>(area, Op.OR));
273
        return this;
274
    }
275

    
276
//And filter on areas makes sense only in very specific cases.
277
//We remove it until it is really required
278
//    public TaxonNodeFilter andArea(UUID uuid){
279
//        areaFilter.add( new LogicFilter<>(NamedArea.class, uuid, Op.AND));
280
//        return this;
281
//    }
282
//    public TaxonNodeFilter andArea(NamedArea area){
283
//        areaFilter.add( new LogicFilter<>(area, Op.AND));
284
//        return this;
285
//    }
286

    
287

    
288
    public TaxonNodeFilter setRankMin(Rank rankMin){
289
        this.rankMin = rankMin == null? null : new LogicFilter<>(rankMin, Op.AND);
290
        return this;
291
    }
292
    public TaxonNodeFilter setRankMin(UUID rankMinUuid){
293
        this.rankMin = rankMinUuid == null? null : new LogicFilter<>(Rank.class, rankMinUuid, Op.AND);
294
        return this;
295
    }
296

    
297
    public TaxonNodeFilter setRankMax(Rank rankMax){
298
        this.rankMax = rankMax == null? null : new LogicFilter<>(rankMax, Op.AND);
299
        return this;
300
    }
301
    public TaxonNodeFilter setRankMax(UUID rankMaxUuid){
302
        this.rankMax = rankMaxUuid == null? null : new LogicFilter<>(Rank.class, rankMaxUuid, Op.AND);
303
        return this;
304
    }
305

    
306
    public TaxonNodeFilter orClassification(Classification classification){
307
        classifications.add( new LogicFilter<>(classification, Op.OR));
308
        return this;
309
    }
310
    public TaxonNodeFilter orClassification(UUID uuid){
311
        classifications.add( new LogicFilter<>(Classification.class, uuid, Op.OR));
312
        return this;
313
    }
314

    
315
    public TaxonNodeFilter notTaxon(Taxon taxon){
316
        taxa.add( new LogicFilter<>(taxon, Op.NOT));
317
        return this;
318
    }
319

    
320
    public LogicFilter<Rank> getRankMax() {
321
        return rankMax;
322
    }
323
    public LogicFilter<Rank> getRankMin() {
324
        return rankMin;
325
    }
326

    
327
    public boolean isIncludeUnpublished() {
328
        return includeUnpublished;
329
    }
330
    public void setIncludeUnpublished(boolean includeUnpublished) {
331
        this.includeUnpublished = includeUnpublished;
332
    }
333

    
334

    
335
    /**
336
     * If <code>true</code> the result will include the root node of
337
     * a classification.
338
     * Note: As a root node per se has no taxon all filters with filter
339
     * on taxon related data have no effect for the root node. If the
340
     * root node is returned only depends on the
341
     * {@link TaxonNodeFilter#isIncludeRootNodes() includeRootNodes}
342
     * parameter.
343
     */
344
    public boolean isIncludeRootNodes() {
345
        return includeRootNodes;
346
    }
347
    public TaxonNodeFilter setIncludeRootNodes(boolean includeRootNodes) {
348
        this.includeRootNodes = includeRootNodes;
349
        return this;
350
    }
351

    
352
    public boolean hasClassificationFilter(){
353
        return getClassificationFilter() != null  //just in case, but should never be null
354
                && !getClassificationFilter().isEmpty();
355
    }
356

    
357
    public boolean hasSubtreeFilter(){
358
        return getSubtreeFilter() != null  //just in case, but should never be null
359
                && !getSubtreeFilter().isEmpty();
360
    }
361

    
362
    public ORDER getOrderBy() {
363
        return orderBy;
364
    }
365
    public void setOrder(ORDER orderBy) {
366
        this.orderBy = orderBy;
367
    }
368

    
369
// ************************** toString *********************************/
370

    
371
    @Override
372
    public String toString() {
373
        return "TaxonNodeFilter [subtrees=" + subtrees + ", taxonNodes=" + taxonNodes + ", classifications="
374
                + classifications + ", taxa=" + taxa + ", rankMin=" + rankMin + ", rankMax=" + rankMax + ", areaFilter="
375
                + areaFilter + ", distributionStatusFilter=" + distributionStatusFilter + ", includeRootNodes="
376
                + includeRootNodes + ", includeUnpublished=" + includeUnpublished + ", orderBy=" + orderBy + "]";
377
    }
378

    
379
}
(2-2/2)