1 package org.apache.ojb.broker.query;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.util.*;
19
20
21
22
23
24
25
26 public class CriteriaUtils
27 {
28
29
30
31
32
33
34 public static List getDNF(Criteria crit)
35 {
36 List dnf = new ArrayList();
37 Enumeration e = crit.getElements();
38 Criteria tmpCrit;
39
40 while (e.hasMoreElements())
41 {
42 Object o = e.nextElement();
43 if (o instanceof Criteria)
44 {
45 Criteria pc = (Criteria) o;
46 switch (pc.getType())
47 {
48 case (Criteria.OR):
49 {
50 dnf.addAll(getDNF(pc));
51 break;
52 }
53 case (Criteria.AND):
54 {
55 dnf = getDnfAndDnf(dnf, getDNF(pc));
56 break;
57 }
58 }
59 }
60 else
61 {
62 SelectionCriteria c = (SelectionCriteria) o;
63 tmpCrit = new Criteria();
64 tmpCrit.getCriteria().add(c);
65 if (dnf.isEmpty())
66 {
67 dnf.add(tmpCrit);
68 }
69 else
70 {
71
72 dnf = getDnfAndDnf(dnf, Collections.singletonList(tmpCrit));
73
74
75
76
77
78
79
80
81
82
83 }
84 }
85 }
86
87 return dnf;
88 }
89
90
91
92
93 private static List getDnfAndDnf(List dnf1, List dnf2)
94 {
95 ArrayList dnf = new ArrayList();
96
97 for (Iterator it1 = dnf1.iterator(); it1.hasNext(); )
98 {
99 Criteria crit1 = (Criteria) it1.next();
100
101 for (Iterator it2 = dnf2.iterator(); it2.hasNext(); )
102 {
103 Criteria crit2 = (Criteria) it2.next();
104 Criteria crit = new Criteria();
105 crit.getCriteria().addAll(crit1.getCriteria());
106 crit.getCriteria().addAll(crit2.getCriteria());
107 dnf.add(crit);
108 }
109 }
110
111 return dnf;
112 }
113 }
114