Project

General

Profile

Download (11.3 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
 *
29
 * @author a.mueller
30
 *
31
 */
32
public class TaxonNodeFilter implements Serializable{
33

    
34
    private static final long serialVersionUID = 2292886683987183999L;
35

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

    
43
    private List<LogicFilter<NamedArea>> areaFilter = new ArrayList<>();
44

    
45
    private List<LogicFilter<PresenceAbsenceTerm>> distributionStatusFilter = new ArrayList<>();
46

    
47
    private boolean includeRootNodes = false;
48

    
49
    private boolean includeUnpublished = false;
50

    
51
    //********************** FACTORY ***************************/
52

    
53
    public static TaxonNodeFilter NewTaxonNodeInstance(UUID taxonNodeUuid){
54
        return new TaxonNodeFilter().orTaxonNode(taxonNodeUuid);
55
    }
56
    public static TaxonNodeFilter NewTaxonNodeInstance(TaxonNode taxonNode){
57
        return new TaxonNodeFilter().orTaxonNode(taxonNode);
58
    }
59

    
60
    public static TaxonNodeFilter NewClassificationInstance(UUID classificationUuid){
61
        return new TaxonNodeFilter().orClassification(classificationUuid);
62
    }
63
    public static TaxonNodeFilter NewClassificationInstance(Classification classification){
64
        return new TaxonNodeFilter().orClassification(classification);
65
    }
66

    
67

    
68
    public static TaxonNodeFilter NewSubtreeInstance(UUID subtreeUuid){
69
        return new TaxonNodeFilter().orSubtree(subtreeUuid);
70
    }
71
    public static TaxonNodeFilter NewSubtreeInstance(TaxonNode subtree){
72
        return new TaxonNodeFilter().orSubtree(subtree);
73
    }
74

    
75
    public static TaxonNodeFilter NewTaxonInstance(UUID taxonUuid){
76
        return new TaxonNodeFilter().orTaxon(taxonUuid);
77
    }
78

    
79
    public static TaxonNodeFilter NewTaxonInstance(Taxon taxon){
80
        return new TaxonNodeFilter().orTaxon(taxon);
81
    }
82

    
83
    public static TaxonNodeFilter NewRankInstance(Rank rankMin, Rank rankMax){
84
        return new TaxonNodeFilter().setRankMin(rankMin).setRankMax(rankMax);
85
    }
86

    
87
    public static TaxonNodeFilter NewInstance(Collection<UUID> classificationUuids,
88
            Collection<UUID> subtreeUuids, Collection<UUID> taxonNodeUuids,
89
            Collection<UUID> taxonUuids, Collection<UUID> areaUuids,
90
            UUID minRank, UUID maxRank){
91

    
92
        TaxonNodeFilter result = new TaxonNodeFilter().setRankMin(minRank).setRankMax(maxRank);
93
        classificationUuids = classificationUuids == null ? new ArrayList<>(): classificationUuids;
94
        subtreeUuids = subtreeUuids == null ? new ArrayList<>(): subtreeUuids;
95
        taxonNodeUuids = taxonNodeUuids == null ? new ArrayList<>(): taxonNodeUuids;
96
        taxonUuids = taxonUuids == null ? new ArrayList<>(): taxonUuids;
97
        areaUuids = areaUuids == null ? new ArrayList<>(): areaUuids;
98

    
99
        for (UUID uuid : classificationUuids){
100
            result.orClassification(uuid);
101
        }
102
        for (UUID uuid : subtreeUuids){
103
            result.orSubtree(uuid);
104
        }
105
        for (UUID uuid : taxonNodeUuids){
106
            result.orTaxonNode(uuid);
107
        }
108
        for (UUID uuid : taxonUuids){
109
            result.orTaxon(uuid);
110
        }
111
        for (UUID uuid : areaUuids){
112
            result.orArea(uuid);
113
        }
114
        return result;
115

    
116
    }
117

    
118
// ************************ CONSTRUCTOR *******************/
119

    
120
    public TaxonNodeFilter(){
121
        reset();
122
    }
123

    
124
    /**
125
     * Constructor for a given subtree represented by a {@link TaxonNode}
126
     */
127
    public TaxonNodeFilter(TaxonNode node){
128
        reset();
129
        LogicFilter<TaxonNode> filter = new LogicFilter<>(node);
130
        subtrees.add(filter);
131
    }
132

    
133
    public TaxonNodeFilter(Rank rankMin, Rank rankMax){
134
        reset();
135
        if(rankMin!=null){
136
            this.rankMin = new LogicFilter<>(rankMin);
137
        }
138
        if(rankMax!=null){
139
            this.rankMax = new LogicFilter<>(rankMax);
140
        }
141
    }
142

    
143
    public TaxonNodeFilter(Classification classification){
144
        reset();
145
        LogicFilter<Classification> filter = new LogicFilter<>(classification);
146
        classifications.add(filter);
147
    }
148

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

    
155
    public TaxonNodeFilter(Taxon taxon){
156
        reset();
157
        LogicFilter<Taxon> filter = new LogicFilter<>(taxon);
158
        taxa.add(filter);
159
    }
160

    
161
//    public <T extends CdmBase> TaxonNodeFilter(Class<T> clazz, UUID uuid){
162
//        reset();
163
//        LogicFilter<T> filter = new LogicFilter<T>(clazz, uuid);
164
//        classifications.add(filter);
165
//    }
166

    
167
// ********************** reset *****************************/
168

    
169
    public void reset(){
170
        subtrees = new ArrayList<>();
171
        resetAreas();
172
        resetRanks();
173
        resetDistributionStatus();
174
        resetTaxonNodes();
175
        resetClassifications();
176
        resetTaxa();
177
    }
178

    
179
    private void resetDistributionStatus() {
180
        distributionStatusFilter = new ArrayList<>();
181
    }
182

    
183
    private void resetTaxonNodes() {
184
        taxonNodes = new ArrayList<>();
185
    }
186

    
187
    private void resetClassifications() {
188
        classifications = new ArrayList<>();
189
    }
190

    
191
    private void resetTaxa() {
192
        taxa = new ArrayList<>();
193
    }
194

    
195
    private void resetAreas() {
196
        areaFilter = new ArrayList<>();
197
    }
198

    
199
    private void resetRanks() {
200
        rankMin = null;
201
        rankMax = null;
202
    }
203

    
204
//*************************************
205

    
206
    public List<LogicFilter<TaxonNode>>getSubtreeFilter(){
207
        return Collections.unmodifiableList(subtrees);
208
    }
209

    
210
    public List<LogicFilter<TaxonNode>>getTaxonNodesFilter(){
211
        return Collections.unmodifiableList(taxonNodes);
212
    }
213

    
214

    
215
    public List<LogicFilter<Classification>>getClassificationFilter(){
216
        return Collections.unmodifiableList(classifications);
217
    }
218

    
219
    public List<LogicFilter<Taxon>>getTaxonFilter(){
220
        return Collections.unmodifiableList(taxa);
221
    }
222

    
223
    public List<LogicFilter<NamedArea>>getAreaFilter(){
224
        return Collections.unmodifiableList(areaFilter);
225
    }
226

    
227
    public List<LogicFilter<PresenceAbsenceTerm>>getDistributionStatusFilter(){
228
        return Collections.unmodifiableList(distributionStatusFilter);
229
    }
230

    
231
    public TaxonNodeFilter orSubtree(TaxonNode taxonNode){
232
        subtrees.add( new LogicFilter<>(taxonNode, Op.OR));
233
        return this;
234
    }
235

    
236
    public TaxonNodeFilter notSubtree(TaxonNode taxonNode){
237
        subtrees.add( new LogicFilter<>(taxonNode, Op.NOT));
238
        return this;
239
    }
240

    
241
    public TaxonNodeFilter orSubtree(UUID taxonNodeUuid){
242
        subtrees.add( new LogicFilter<>(TaxonNode.class, taxonNodeUuid, Op.OR));
243
        return this;
244
    }
245

    
246
    /**
247
     * Adds a single {@link TaxonNode} to the filter.<BR><BR>
248
     * NOTE: this adds only the node to the filter, not it's children!
249
     */
250
    public TaxonNodeFilter orTaxonNode(TaxonNode taxonNode){
251
        taxonNodes.add( new LogicFilter<>(taxonNode, Op.OR));
252
        return this;
253
    }
254
    /**
255
     * Adds a single {@link TaxonNode} to the filter.<BR><BR>
256
     * NOTE: this adds only the node to the filter, not it's children!
257
     */
258
    public TaxonNodeFilter orTaxonNode(UUID uuid){
259
        taxonNodes.add( new LogicFilter<>(TaxonNode.class, uuid, Op.OR));
260
        return this;
261
    }
262

    
263
    public TaxonNodeFilter notTaxonNode(TaxonNode taxonNode){
264
        taxonNodes.add( new LogicFilter<>(taxonNode, Op.NOT));
265
        return this;
266
    }
267
    public TaxonNodeFilter andTaxonNode(TaxonNode taxonNode){
268
        taxonNodes.add(new LogicFilter<>(taxonNode, Op.AND));
269
        return this;
270
    }
271

    
272
    public TaxonNodeFilter orTaxon(Taxon taxon){
273
        taxa.add( new LogicFilter<>(taxon, Op.OR));
274
        return this;
275
    }
276

    
277
    public TaxonNodeFilter orTaxon(UUID uuid){
278
        taxa.add( new LogicFilter<>(Taxon.class, uuid, Op.OR));
279
        return this;
280
    }
281

    
282
    public TaxonNodeFilter orArea(UUID uuid){
283
        areaFilter.add( new LogicFilter<>(NamedArea.class, uuid, Op.OR));
284
        return this;
285
    }
286
    public TaxonNodeFilter orArea(NamedArea area){
287
        areaFilter.add( new LogicFilter<>(area, Op.OR));
288
        return this;
289
    }
290

    
291
    public TaxonNodeFilter andArea(UUID uuid){
292
        areaFilter.add( new LogicFilter<>(NamedArea.class, uuid, Op.AND));
293
        return this;
294
    }
295
    public TaxonNodeFilter andArea(NamedArea area){
296
        areaFilter.add( new LogicFilter<>(area, Op.AND));
297
        return this;
298
    }
299

    
300

    
301
    public TaxonNodeFilter setRankMin(Rank rankMin){
302
        this.rankMin = rankMin == null? null : new LogicFilter<>(rankMin, Op.AND);
303
        return this;
304
    }
305
    public TaxonNodeFilter setRankMin(UUID rankMinUuid){
306
        this.rankMin = rankMinUuid == null? null : new LogicFilter<>(Rank.class, rankMinUuid, Op.AND);
307
        return this;
308
    }
309

    
310
    public TaxonNodeFilter setRankMax(Rank rankMax){
311
        this.rankMax = rankMax == null? null : new LogicFilter<>(rankMax, Op.AND);
312
        return this;
313
    }
314
    public TaxonNodeFilter setRankMax(UUID rankMaxUuid){
315
        this.rankMax = rankMaxUuid == null? null : new LogicFilter<>(Rank.class, rankMaxUuid, Op.AND);
316
        return this;
317
    }
318

    
319
    public TaxonNodeFilter orClassification(Classification classification){
320
        classifications.add( new LogicFilter<>(classification, Op.OR));
321
        return this;
322
    }
323
    public TaxonNodeFilter orClassification(UUID uuid){
324
        classifications.add( new LogicFilter<>(Classification.class, uuid, Op.OR));
325
        return this;
326
    }
327

    
328
    public TaxonNodeFilter notTaxon(Taxon taxon){
329
        taxa.add( new LogicFilter<>(taxon, Op.NOT));
330
        return this;
331
    }
332

    
333
    public LogicFilter<Rank> getRankMax() {
334
        return rankMax;
335
    }
336
    public LogicFilter<Rank> getRankMin() {
337
        return rankMin;
338
    }
339

    
340
    public boolean isIncludeUnpublished() {
341
        return includeUnpublished;
342
    }
343
    public void setIncludeUnpublished(boolean includeUnpublished) {
344
        this.includeUnpublished = includeUnpublished;
345
    }
346

    
347

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

    
365
}
(2-2/2)