View Javadoc
1 /* 2 * $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/query/filter/FilterProcessor.java,v 1.8 2002/04/05 21:13:52 nstojano Exp $ 3 */ 4 5 package com.sun.ebxml.registry.query.filter; 6 7 import java.sql.*; 8 9 import com.sun.ebxml.registry.*; 10 11 import org.oasis.ebxml.registry.bindings.query.*; 12 import org.oasis.ebxml.registry.bindings.query.types.*; 13 import org.oasis.ebxml.registry.bindings.rs.*; 14 15 16 /*** 17 * Class Declaration for FilterProcessor 18 * @see 19 * @author Nikola Stojanovic 20 */ 21 22 public class FilterProcessor implements SQLConverter { 23 24 private String selectColumn = null; 25 private boolean isNativeFilter = false; 26 private boolean isReverseSelectNeeded = false; 27 private ClauseHandler clauseHandler = new ClauseHandler(); 28 private ClauseType sqlClause = new ClauseType(); 29 30 31 public ClauseType getNewClause(FilterType filter) throws RegistryException { 32 33 sqlClause.clause = convertToSQL(filter); 34 sqlClause.isSubSelectNeeded = !isNativeFilter; 35 sqlClause.isReverseSelectNeeded = isReverseSelectNeeded; 36 return sqlClause; 37 } 38 39 40 public void setSelectColumn(String selColumn) { 41 42 selectColumn = selColumn; 43 } 44 45 46 public boolean isReverseSelectNeeded() { 47 48 return isReverseSelectNeeded; 49 } 50 51 52 public String convertToSQL(Object obj) throws RegistryException { 53 54 if (!(obj instanceof org.oasis.ebxml.registry.bindings.query.FilterType)) { 55 throw new RegistryException("Unexpected object " + obj + ". Was expecting org.oasis.ebxml.registry.bindings.query.FilterType."); 56 } 57 58 FilterType filter = (FilterType)obj; 59 String sqlQuery = null; 60 String whereClause = null; 61 62 if (!isNativeFilter) { 63 sqlQuery = "SELECT " + selectColumn + " FROM " + getTableName(filter); 64 whereClause = "WHERE "; 65 } 66 else { 67 whereClause = ""; 68 } 69 70 Clause clause = filter.getClause(); 71 SimpleClause simpleClause = clause.getSimpleClause(); 72 CompoundClause compoundClause = clause.getCompoundClause(); 73 74 if (simpleClause != null) { 75 whereClause += convertSimpleClause(simpleClause); 76 } 77 else if (compoundClause != null) { 78 whereClause += convertCompoundClause(compoundClause); 79 } 80 else { 81 throw new RegistryException("Unexpected Clause " + obj + ". Was expecting org.oasis.ebxml.registry.bindings.query.SimpleClause or org.oasis.ebxml.registry.bindings.query.CompoundClause."); 82 } 83 84 sqlClause.isReverseSelectNeeded = isReverseSelectNeeded; 85 86 if (!isNativeFilter) { 87 return sqlQuery + " " + whereClause; 88 } 89 else { 90 return whereClause; 91 } 92 } 93 94 95 public String addNativeWhereClause(String whereClause, FilterType nativeFilter) throws RegistryException { 96 97 /* adds to the existing SQL where clause a clause for Filters that are for the same object as the enclosing Query 98 like OrganizationFilter inside OrganizationQuery */ 99 100 setNativeFilter(true); 101 sqlClause.clause = convertToSQL(nativeFilter); 102 return clauseHandler.addWhereClause(whereClause, sqlClause); 103 } 104 105 106 public String addForeignWhereClause(String whereClause, FilterType foreignFilter) throws RegistryException { 107 108 /* adds to the existing SQL where clause a clause for Filters that are for the different object then the enclosing Query 109 like PostalAddressFilter inside OrganizationQuery */ 110 111 setNativeFilter(false); 112 sqlClause.clause = convertToSQL(foreignFilter); 113 return clauseHandler.addWhereClause(whereClause, sqlClause); 114 } 115 116 117 private void setNativeFilter(boolean isNatFilter) { 118 119 isNativeFilter = isNatFilter; 120 121 if (isNativeFilter) { 122 sqlClause.isSubSelectNeeded = false; 123 setSelectColumn(""); 124 } 125 else { 126 sqlClause.isSubSelectNeeded = true; 127 } 128 } 129 130 131 private String getTableName(FilterType filter) throws RegistryException { 132 133 if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.RegistryObjectFilter")) { 134 return "RegistryObject"; 135 } 136 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.RegistryEntryFilter")) { 137 return "RegistryEntry"; 138 } 139 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.AssociationFilter")) { 140 return "Association"; 141 } 142 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.AuditableEventFilter")) { 143 return "AuditableEvent"; 144 } 145 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.ClassificationFilter")) { 146 return "Classification"; 147 } 148 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.ClassificationNodeFilter")) { 149 return "ClassificationNode"; 150 } 151 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.ClassificationSchemeFilter")) { 152 return "ClassificationScheme"; 153 } 154 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.EmailAddress")) { 155 return "EmailAddress"; 156 } 157 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.ExternalIdentifierFilter")) { 158 return "ExternalIdentifier"; 159 } 160 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.ExternalLinkFilter")) { 161 return "ExternalLink"; 162 } 163 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.ExtrinsicObjectFilter")) { 164 return "ExtrinsicObject"; 165 } 166 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.OrganizationFilter")) { 167 return "Organization"; 168 } 169 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.RegistryPackageFilter")) { 170 return "RegistryPackage"; 171 } 172 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.ServiceFilter")) { 173 return "Service"; 174 } 175 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.ServiceBindingFilter")) { 176 return "ServiceBinding"; 177 } 178 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.SpecificationLinkFilter")) { 179 return "SpecificationLink"; 180 } 181 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.UserFilter")) { 182 return "User"; 183 } 184 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.SlotFilter")) { 185 return "Slot"; 186 } 187 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.SlotValueFilter")) { 188 return "SlotValue"; 189 } 190 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.PostalAddressFilter")) { 191 return "PostalAddress"; 192 } 193 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.TelephoneNumberFilter")) { 194 return "TelephoneNumber"; 195 } 196 else if (filter.getClass().getName().equals("org.oasis.ebxml.registry.bindings.query.LocalizedStringFilter")) { 197 // there are many different tables, like Name, Description, ... 198 return ""; 199 } 200 else { 201 throw new RegistryException("Unexpected filter" + filter); 202 } 203 } 204 205 206 private String convertSimpleClause(SimpleClause simpleClause) throws RegistryException { 207 208 StringClause stringClause = simpleClause.getStringClause(); 209 BooleanClause booleanClause = simpleClause.getBooleanClause(); 210 RationalClause rationalClause = simpleClause.getRationalClause(); 211 String predicate = null; 212 String whereClause = ""; 213 214 if (stringClause != null) { 215 StringPredicateType stringPredicate = stringClause.getStringPredicate(); 216 predicate = stringPredicate.toString(); 217 whereClause += simpleClause.getLeftArgument() + " " + buildStringSQLPredicate(predicate, stringClause.getContent()); 218 } 219 else if (booleanClause != null) { 220 Boolean boleanPredicate = new Boolean(booleanClause.getBooleanPredicate()); 221 whereClause += simpleClause.getLeftArgument() + " = " + boleanPredicate.toString(); 222 } 223 else if (rationalClause != null) { 224 String rightArgument = null; 225 LogicalPredicateType logicalPredicate = rationalClause.getLogicalPredicate(); 226 predicate = logicalPredicate.toString(); 227 228 if (rationalClause.hasIntClause()) { 229 Integer number = new Integer(rationalClause.getIntClause()); 230 rightArgument = number.toString(); 231 } 232 else if (rationalClause.hasFloatClause()) { 233 Float number = new Float(rationalClause.getFloatClause()); 234 rightArgument = number.toString(); 235 } 236 else { 237 Timestamp dateTime = new Timestamp(((rationalClause.getDateTimeClause()).getTime())); 238 rightArgument = dateTime.toString(); 239 } 240 241 whereClause += simpleClause.getLeftArgument() + " " + buildLogicalSQLPredicate(predicate, rightArgument); 242 } 243 244 return whereClause; 245 } 246 247 248 private String convertCompoundClause(CompoundClause compoundClause) throws RegistryException { 249 250 String whereClause = ""; 251 Clause subClause[] = compoundClause.getClause(); 252 ConnectivePredicateType predicate = compoundClause.getConnectivePredicate(); 253 String connectivePredicate = predicate.toString(); 254 255 if (subClause.length != 2) { 256 throw new RegistryException("Invalid Compound Clause: " + compoundClause); 257 } 258 else { 259 for (int i=0; i<subClause.length; i++) { 260 if (i == 0) { 261 if (subClause[i].getSimpleClause() != null) { 262 whereClause = convertSimpleClause(subClause[i].getSimpleClause()); 263 } 264 else { 265 whereClause = convertCompoundClause(subClause[i].getCompoundClause()); 266 } 267 } 268 else { 269 if (subClause[i].getSimpleClause() != null) { 270 whereClause = clauseHandler.buildConnectiveSQL(whereClause, connectivePredicate, convertSimpleClause(subClause[i].getSimpleClause())); 271 } 272 else { 273 whereClause = clauseHandler.buildConnectiveSQL(whereClause, connectivePredicate, convertCompoundClause(subClause[i].getCompoundClause())); 274 } 275 } 276 } 277 } 278 279 return whereClause; 280 } 281 282 283 private String buildStringSQLPredicate(String stringPredicate, String rightArgument) throws RegistryException { 284 285 /* for now use logic of "SOME", not "ALL/NONE" -> isReverseSelectNeeded not active */ 286 isReverseSelectNeeded = false; 287 288 if (stringPredicate.equals("Contains")) { 289 return "LIKE '%" + rightArgument + "%'"; 290 } 291 else if (stringPredicate.equals("-Contains")) { 292 isReverseSelectNeeded = true; 293 // return "LIKE '%" + rightArgument + "%'"; 294 return "NOT LIKE '%" + rightArgument + "%'"; 295 } 296 else if (stringPredicate.equals("StartsWith")) { 297 return "LIKE '" + rightArgument + "%'"; 298 } 299 else if (stringPredicate.equals("-StartsWith")) { 300 isReverseSelectNeeded = true; 301 // return "LIKE '" + rightArgument + "%'"; 302 return "NOT LIKE '" + rightArgument + "%'"; 303 } 304 else if (stringPredicate.equals("Equal")) { 305 return "= '" + rightArgument + "'"; 306 } 307 else if (stringPredicate.equals("-Equal")) { 308 isReverseSelectNeeded = true; 309 // return "= '" + rightArgument + "'"; 310 return "<> '" + rightArgument + "'"; 311 } 312 else if (stringPredicate.equals("EndsWith")) { 313 return "LIKE '%" + rightArgument + "'"; 314 } 315 else if (stringPredicate.equals("-EndsWith")) { 316 isReverseSelectNeeded = true; 317 // return "LIKE '%" + rightArgument + "'"; 318 return "NOT LIKE '%" + rightArgument + "'"; 319 } 320 else { 321 throw new RegistryException("Invalid string predicate: " + stringPredicate); 322 } 323 } 324 325 326 private String buildLogicalSQLPredicate(String logicalPredicate, String rightArgument) throws RegistryException { 327 328 /* for now use logic of "SOME", not "ALL/NONE" -> isReverseSelectNeeded not active */ 329 isReverseSelectNeeded = false; 330 331 if (logicalPredicate.equals("EQ")) { 332 return "= " + "\'" + rightArgument + "\'"; 333 } 334 else if (logicalPredicate.equals("GE")) { 335 return ">= " + "\'" + rightArgument + "\'"; 336 } 337 else if (logicalPredicate.equals("GT")) { 338 return ">" + "\'" + rightArgument + "\'"; 339 } 340 else if (logicalPredicate.equals("LE")) { 341 return "<=" + "\'" + rightArgument + "\'"; 342 } 343 else if (logicalPredicate.equals("LT")) { 344 return "<" + "\'" + rightArgument + "\'"; 345 } 346 else if (logicalPredicate.equals("NE")) { 347 isReverseSelectNeeded = true; 348 // return "=" + "\'" + rightArgument + "\'"; 349 return "<>" + "\'" + rightArgument + "\'"; 350 } 351 else { 352 throw new RegistryException("Invalid logical predicate: " + logicalPredicate); 353 } 354 } 355 }

This page was automatically generated by Maven