1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.juddi.query;
19
20 import java.util.List;
21 import javax.persistence.EntityManager;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.juddi.query.util.DynamicQuery;
26 import org.apache.juddi.query.util.FindQualifiers;
27 import org.uddi.api_v3.TModelBag;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class FindBusinessByTModelKeyQuery extends BusinessEntityQuery {
51
52 @SuppressWarnings("unused")
53 private final static Log log = LogFactory.getLog(FindBusinessByTModelKeyQuery.class);
54
55 public static final String ENTITY_NAME_CHILD = "TmodelInstanceInfo";
56
57 protected static final String entityAliasChild;
58
59 static {
60 entityAliasChild = buildAlias(ENTITY_NAME_CHILD);
61 }
62
63 public static List<Object> select(EntityManager em, FindQualifiers fq, TModelBag tModels, List<Object> keysIn, DynamicQuery.Parameter... restrictions) {
64
65 if ((keysIn != null) && (keysIn.isEmpty()))
66 return keysIn;
67
68 if (tModels == null)
69 return keysIn;
70
71 List<String> tmodelKeys = tModels.getTModelKey();
72 if (tmodelKeys == null || tmodelKeys.isEmpty())
73 return keysIn;
74
75 DynamicQuery dynamicQry = new DynamicQuery(selectSQL);
76 appendConditions(dynamicQry, fq, tmodelKeys);
77
78 if (restrictions != null && restrictions.length > 0)
79 dynamicQry.AND().pad().appendGroupedAnd(restrictions);
80
81 return getQueryResult(em, dynamicQry, keysIn, ENTITY_ALIAS + "." + KEY_NAME);
82 }
83
84
85
86
87
88
89
90 public static void appendConditions(DynamicQuery qry, FindQualifiers fq, List<String> tmodelKeys) {
91
92
93 appendJoinTables(qry, fq, tmodelKeys);
94 qry.AND().pad().openParen().pad();
95
96 int count = 0;
97 int tblCount = -1;
98 for(String tmodelKey : tmodelKeys) {
99
100 tblCount++;
101 String tmodelKeyTerm = (fq.isOrAllKeys()?entityAliasChild + "0":entityAliasChild + tblCount) + ".tmodelKey";
102 qry.appendGroupedAnd(new DynamicQuery.Parameter(tmodelKeyTerm, tmodelKey, DynamicQuery.PREDICATE_EQUALS));
103
104 if (count + 1 < tmodelKeys.size()) {
105 if (fq.isOrAllKeys())
106 qry.OR().pad();
107 else
108 qry.AND().pad();
109 }
110
111 count++;
112 }
113 qry.closeParen().pad();
114
115 }
116
117
118
119
120
121 public static void appendJoinTables(DynamicQuery qry, FindQualifiers fq, List<String> tmodelKeys) {
122
123
124 if (tmodelKeys != null && tmodelKeys.size() > 0) {
125 qry.comma().pad().append(BusinessServiceQuery.ENTITY_NAME + " " + BusinessServiceQuery.ENTITY_ALIAS).pad();
126 qry.comma().pad().append(BindingTemplateQuery.ENTITY_NAME + " " + BindingTemplateQuery.ENTITY_ALIAS).pad();
127
128 StringBuilder thetaJoins = new StringBuilder(200);
129 int tblCount = 0;
130 for(int count = 0; count < tmodelKeys.size(); count++) {
131 if (count != 0) {
132 if (!fq.isOrAllKeys()) {
133 tblCount++;
134 qry.comma().pad().append(ENTITY_NAME_CHILD + " " + entityAliasChild + tblCount).pad();
135 thetaJoins.append(entityAliasChild + (tblCount - 1) + "." + BindingTemplateQuery.ENTITY_FIELD + "." + BindingTemplateQuery.KEY_NAME + " = " + entityAliasChild + tblCount + "." + BindingTemplateQuery.ENTITY_FIELD + "." + BindingTemplateQuery.KEY_NAME + " ");
136 thetaJoins.append(DynamicQuery.OPERATOR_AND + " ");
137 }
138 }
139 else {
140 qry.comma().pad().append(ENTITY_NAME_CHILD + " " + entityAliasChild + tblCount).pad();
141 thetaJoins.append(BindingTemplateQuery.ENTITY_ALIAS + "." + BindingTemplateQuery.KEY_NAME + " = " + entityAliasChild + tblCount + "." + BindingTemplateQuery.ENTITY_FIELD + "." + BindingTemplateQuery.KEY_NAME + " ");
142 thetaJoins.append(DynamicQuery.OPERATOR_AND + " ");
143 }
144 }
145
146 qry.WHERE().pad().openParen().pad();
147
148 qry.append(ENTITY_ALIAS + "." + KEY_NAME + " = " + BusinessServiceQuery.ENTITY_ALIAS + "." + ENTITY_FIELD + "." + KEY_NAME).pad();
149 qry.AND().pad().append(BusinessServiceQuery.ENTITY_ALIAS + "." + BusinessServiceQuery.KEY_NAME + " = " + BindingTemplateQuery.ENTITY_ALIAS + "." + BusinessServiceQuery.ENTITY_FIELD + "." + BusinessServiceQuery.KEY_NAME).pad();
150 qry.AND().pad();
151
152 String thetaJoinsStr = thetaJoins.toString();
153 if (thetaJoinsStr.endsWith(DynamicQuery.OPERATOR_AND + " "))
154 thetaJoinsStr = thetaJoinsStr.substring(0, thetaJoinsStr.length() - (DynamicQuery.OPERATOR_AND + " ").length());
155 qry.append(thetaJoinsStr);
156
157 qry.closeParen().pad();
158 if (fq!=null && fq.isSignaturePresent()) {
159 qry.AND().pad().openParen().pad().append(BusinessEntityQuery.SIGNATURE_PRESENT).pad().closeParen().pad();
160 }
161 }
162 }
163
164 }