Project

General

Profile

Download (12.4 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 NewRankInstance(UUID rankMinUuid, UUID rankMaxUuid){
104
        return new TaxonNodeFilter().setRankMin(rankMinUuid).setRankMax(rankMaxUuid);
105
    }
106

    
107
    public static TaxonNodeFilter NewAreaInstance(NamedArea area){
108
        return new TaxonNodeFilter().orArea(area);
109
    }
110
    public static TaxonNodeFilter NewAreaInstance(UUID areaUuid){
111
        return new TaxonNodeFilter().orArea(areaUuid);
112
    }
113

    
114
    public static TaxonNodeFilter NewInstance(Collection<UUID> classificationUuids,
115
            Collection<UUID> subtreeUuids, Collection<UUID> taxonNodeUuids,
116
            Collection<UUID> taxonUuids, Collection<UUID> areaUuids,
117
            UUID minRank, UUID maxRank){
118

    
119
        TaxonNodeFilter result = new TaxonNodeFilter().setRankMin(minRank).setRankMax(maxRank);
120
        classificationUuids = classificationUuids == null ? new ArrayList<>(): classificationUuids;
121
        subtreeUuids = subtreeUuids == null ? new ArrayList<>(): subtreeUuids;
122
        taxonNodeUuids = taxonNodeUuids == null ? new ArrayList<>(): taxonNodeUuids;
123
        taxonUuids = taxonUuids == null ? new ArrayList<>(): taxonUuids;
124
        areaUuids = areaUuids == null ? new ArrayList<>(): areaUuids;
125

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

    
144
// ************************ CONSTRUCTOR *******************/
145

    
146
    private TaxonNodeFilter(){
147
        reset();
148
    }
149

    
150
    public TaxonNodeFilter(NamedArea area){
151
        reset();
152
        LogicFilter<NamedArea> filter = new LogicFilter<>(area);
153
        areaFilter.add(filter);
154
    }
155

    
156
// ********************** reset *****************************/
157

    
158
    public void reset(){
159
        resetSubtrees();
160
        resetAreas();
161
        resetRanks();
162
        resetDistributionStatus();
163
        resetTaxonNodes();
164
        resetClassifications();
165
        resetTaxa();
166
    }
167

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

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

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

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

    
184
    private void resetAreas() {
185
        areaFilter = new ArrayList<>();
186
    }
187

    
188
    private void resetRanks() {
189
        rankMin = null;
190
        rankMax = null;
191
    }
192

    
193
    private void resetSubtrees() {
194
        subtrees = new ArrayList<>();
195
    }
196

    
197
//*************************************
198

    
199
    public List<LogicFilter<TaxonNode>>getSubtreeFilter(){
200
        return Collections.unmodifiableList(subtrees);
201
    }
202

    
203
    public List<LogicFilter<TaxonNode>>getTaxonNodesFilter(){
204
        return Collections.unmodifiableList(taxonNodes);
205
    }
206

    
207

    
208
    public List<LogicFilter<Classification>>getClassificationFilter(){
209
        return Collections.unmodifiableList(classifications);
210
    }
211

    
212
    public List<LogicFilter<Taxon>>getTaxonFilter(){
213
        return Collections.unmodifiableList(taxa);
214
    }
215

    
216
    public List<LogicFilter<NamedArea>>getAreaFilter(){
217
        return Collections.unmodifiableList(areaFilter);
218
    }
219

    
220
    public List<LogicFilter<PresenceAbsenceTerm>>getDistributionStatusFilter(){
221
        return Collections.unmodifiableList(distributionStatusFilter);
222
    }
223

    
224
    public TaxonNodeFilter orSubtree(TaxonNode taxonNode){
225
        subtrees.add( new LogicFilter<>(taxonNode, Op.OR));
226
        return this;
227
    }
228

    
229
    public TaxonNodeFilter notSubtree(TaxonNode taxonNode){
230
        subtrees.add( new LogicFilter<>(taxonNode, Op.NOT));
231
        return this;
232
    }
233

    
234
    public TaxonNodeFilter orSubtree(UUID taxonNodeUuid){
235
        subtrees.add( new LogicFilter<>(TaxonNode.class, taxonNodeUuid, Op.OR));
236
        return this;
237
    }
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(TaxonNode taxonNode){
244
        taxonNodes.add( new LogicFilter<>(taxonNode, Op.OR));
245
        return this;
246
    }
247
    /**
248
     * Adds a single {@link TaxonNode} to the filter.<BR><BR>
249
     * NOTE: this adds only the node to the filter, not it's children!
250
     */
251
    public TaxonNodeFilter orTaxonNode(UUID uuid){
252
        taxonNodes.add( new LogicFilter<>(TaxonNode.class, uuid, Op.OR));
253
        return this;
254
    }
255

    
256
    public TaxonNodeFilter notTaxonNode(TaxonNode taxonNode){
257
        taxonNodes.add( new LogicFilter<>(taxonNode, Op.NOT));
258
        return this;
259
    }
260
    public TaxonNodeFilter andTaxonNode(TaxonNode taxonNode){
261
        taxonNodes.add(new LogicFilter<>(taxonNode, Op.AND));
262
        return this;
263
    }
264

    
265
    public TaxonNodeFilter orTaxon(Taxon taxon){
266
        taxa.add( new LogicFilter<>(taxon, Op.OR));
267
        return this;
268
    }
269

    
270
    public TaxonNodeFilter orTaxon(UUID uuid){
271
        taxa.add( new LogicFilter<>(Taxon.class, uuid, Op.OR));
272
        return this;
273
    }
274

    
275
    public TaxonNodeFilter orArea(UUID uuid){
276
        areaFilter.add( new LogicFilter<>(NamedArea.class, uuid, Op.OR));
277
        return this;
278
    }
279
    public TaxonNodeFilter orArea(NamedArea area){
280
        areaFilter.add( new LogicFilter<>(area, Op.OR));
281
        return this;
282
    }
283

    
284
//And filter on areas makes sense only in very specific cases.
285
//We remove it until it is really required
286
//    public TaxonNodeFilter andArea(UUID uuid){
287
//        areaFilter.add( new LogicFilter<>(NamedArea.class, uuid, Op.AND));
288
//        return this;
289
//    }
290
//    public TaxonNodeFilter andArea(NamedArea area){
291
//        areaFilter.add( new LogicFilter<>(area, Op.AND));
292
//        return this;
293
//    }
294

    
295

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

    
305
    public TaxonNodeFilter setRankMax(Rank rankMax){
306
        this.rankMax = rankMax == null? null : new LogicFilter<>(rankMax, Op.AND);
307
        return this;
308
    }
309
    public TaxonNodeFilter setRankMax(UUID rankMaxUuid){
310
        this.rankMax = rankMaxUuid == null? null : new LogicFilter<>(Rank.class, rankMaxUuid, Op.AND);
311
        return this;
312
    }
313

    
314
    public TaxonNodeFilter orClassification(Classification classification){
315
        classifications.add( new LogicFilter<>(classification, Op.OR));
316
        return this;
317
    }
318
    public TaxonNodeFilter orClassification(UUID uuid){
319
        classifications.add( new LogicFilter<>(Classification.class, uuid, Op.OR));
320
        return this;
321
    }
322

    
323
    public TaxonNodeFilter notTaxon(Taxon taxon){
324
        taxa.add( new LogicFilter<>(taxon, Op.NOT));
325
        return this;
326
    }
327

    
328
    public LogicFilter<Rank> getRankMax() {
329
        return rankMax;
330
    }
331
    public LogicFilter<Rank> getRankMin() {
332
        return rankMin;
333
    }
334

    
335
    public boolean isIncludeUnpublished() {
336
        return includeUnpublished;
337
    }
338
    public void setIncludeUnpublished(boolean includeUnpublished) {
339
        this.includeUnpublished = includeUnpublished;
340
    }
341

    
342

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

    
360
    public boolean hasClassificationFilter(){
361
        return getClassificationFilter() != null  //just in case, but should never be null
362
                && !getClassificationFilter().isEmpty();
363
    }
364

    
365
    public boolean hasSubtreeFilter(){
366
        return getSubtreeFilter() != null  //just in case, but should never be null
367
                && !getSubtreeFilter().isEmpty();
368
    }
369

    
370
    public ORDER getOrderBy() {
371
        return orderBy;
372
    }
373
    public void setOrder(ORDER orderBy) {
374
        this.orderBy = orderBy;
375
    }
376

    
377
// ************************** toString *********************************/
378

    
379
    @Override
380
    public String toString() {
381
        return "TaxonNodeFilter [subtrees=" + subtrees + ", taxonNodes=" + taxonNodes + ", classifications="
382
                + classifications + ", taxa=" + taxa + ", rankMin=" + rankMin + ", rankMax=" + rankMax + ", areaFilter="
383
                + areaFilter + ", distributionStatusFilter=" + distributionStatusFilter + ", includeRootNodes="
384
                + includeRootNodes + ", includeUnpublished=" + includeUnpublished + ", orderBy=" + orderBy + "]";
385
    }
386

    
387
}
(2-2/2)