1 16 package org.apache.juddi.function; 17 18 import java.util.Vector ; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.apache.juddi.datastore.DataStore; 23 import org.apache.juddi.datastore.DataStoreFactory; 24 import org.apache.juddi.datatype.CategoryBag; 25 import org.apache.juddi.datatype.DiscoveryURL; 26 import org.apache.juddi.datatype.DiscoveryURLs; 27 import org.apache.juddi.datatype.IdentifierBag; 28 import org.apache.juddi.datatype.KeyedReference; 29 import org.apache.juddi.datatype.Name; 30 import org.apache.juddi.datatype.RegistryObject; 31 import org.apache.juddi.datatype.TModelBag; 32 import org.apache.juddi.datatype.request.FindBusiness; 33 import org.apache.juddi.datatype.request.FindQualifier; 34 import org.apache.juddi.datatype.request.FindQualifiers; 35 import org.apache.juddi.datatype.response.BusinessInfos; 36 import org.apache.juddi.datatype.response.BusinessList; 37 import org.apache.juddi.datatype.tmodel.TModel; 38 import org.apache.juddi.error.NameTooLongException; 39 import org.apache.juddi.error.RegistryException; 40 import org.apache.juddi.error.TooManyOptionsException; 41 import org.apache.juddi.error.UnsupportedException; 42 import org.apache.juddi.registry.RegistryEngine; 43 import org.apache.juddi.util.Config; 44 45 66 public class FindBusinessFunction extends AbstractFunction 67 { 68 private static Log log = LogFactory.getLog(FindBusinessFunction.class); 70 71 74 public FindBusinessFunction(RegistryEngine registry) 75 { 76 super(registry); 77 } 78 79 82 public RegistryObject execute(RegistryObject regObject) 83 throws RegistryException 84 { 85 FindBusiness request = (FindBusiness) regObject; 86 String generic = request.getGeneric(); 87 Vector nameVector = request.getNameVector(); 88 DiscoveryURLs discoveryURLs = request.getDiscoveryURLs(); 89 IdentifierBag identifierBag = request.getIdentifierBag(); 90 CategoryBag categoryBag = request.getCategoryBag(); 91 TModelBag tModelBag = request.getTModelBag(); 92 FindQualifiers qualifiers = request.getFindQualifiers(); 93 int maxRows = request.getMaxRows(); 94 95 if (((nameVector == null) || (nameVector.size() == 0)) 99 && ((discoveryURLs == null) || (discoveryURLs.size() == 0)) 100 && ((identifierBag == null) || (identifierBag.size() == 0)) 101 && ((categoryBag == null) || (categoryBag.size() == 0)) 102 && ((tModelBag == null) || (tModelBag.size() == 0))) 103 { 104 BusinessList list = new BusinessList(); 105 list.setGeneric(generic); 106 list.setBusinessInfos(new BusinessInfos()); 107 list.setOperator(Config.getOperator()); 108 list.setTruncated(false); 109 return list; 110 } 111 112 if (categoryBag != null) 114 { 115 Vector keyedRefVector = categoryBag.getKeyedReferenceVector(); 116 if (keyedRefVector != null) 117 { 118 int vectorSize = keyedRefVector.size(); 119 if (vectorSize > 0) 120 { 121 for (int i=0; i<vectorSize; i++) 122 { 123 KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(i); 124 String key = keyedRef.getTModelKey(); 125 126 if ((key == null) || (key.trim().length() == 0)) 131 keyedRef.setTModelKey(TModel.GENERAL_KEYWORDS_TMODEL_KEY); 132 } 133 } 134 } 135 } 136 137 DataStore dataStore = DataStoreFactory.getDataStore(); 139 140 try 141 { 142 dataStore.beginTrans(); 143 144 if (nameVector != null) 147 { 148 int maxNames = Config.getMaxNameElementsAllowed(); 151 if ((nameVector != null) && (nameVector.size() > maxNames)) 152 throw new TooManyOptionsException("find_business: "+ 153 "names="+nameVector.size()+", "+ 154 "maxNames=" + maxNames); 155 156 int maxNameLength = Config.getMaxNameLengthAllowed(); 160 for (int i = 0; i < nameVector.size(); i++) 161 { 162 String name = ((Name) nameVector.elementAt(i)).getValue(); 163 if (name.length() > maxNameLength) 164 throw new NameTooLongException("find_business: "+ 165 "name="+name+", "+ 166 "length="+name.length()+", "+ 167 "maxNameLength="+maxNameLength); 168 } 169 } 170 171 if (qualifiers != null) 174 { 175 Vector qVector = qualifiers.getFindQualifierVector(); 176 if ((qVector!=null) && (qVector.size() > 0)) 177 { 178 for (int i = 0; i < qVector.size(); i++) 179 { 180 FindQualifier qualifier = (FindQualifier) qVector.elementAt(i); 181 String qValue = qualifier.getValue(); 182 183 if ((!qValue.equals(FindQualifier.EXACT_NAME_MATCH)) 184 && (!qValue.equals(FindQualifier.CASE_SENSITIVE_MATCH)) 185 && (!qValue.equals(FindQualifier.OR_ALL_KEYS)) 186 && (!qValue.equals(FindQualifier.OR_LIKE_KEYS)) 187 && (!qValue.equals(FindQualifier.AND_ALL_KEYS)) 188 && (!qValue.equals(FindQualifier.SORT_BY_NAME_ASC)) 189 && (!qValue.equals(FindQualifier.SORT_BY_NAME_DESC)) 190 && (!qValue.equals(FindQualifier.SORT_BY_DATE_ASC)) 191 && (!qValue.equals(FindQualifier.SORT_BY_DATE_DESC)) 192 && (!qValue.equals(FindQualifier.SERVICE_SUBSET)) 193 && (!qValue 194 .equals(FindQualifier.COMBINE_CATEGORY_BAGS))) 195 throw new UnsupportedException("find_business: "+ 196 "findQualifier="+qValue); 197 } 198 } 199 } 200 201 Vector infoVector = null; 202 boolean truncatedResults = false; 203 204 Vector keyVector = 206 dataStore.findBusiness( 207 nameVector, 208 discoveryURLs, 209 identifierBag, 210 categoryBag, 211 tModelBag, 212 qualifiers); 213 if ((keyVector != null) && (keyVector.size() > 0)) 214 { 215 int rowCount = keyVector.size(); 219 if ((maxRows > 0) && (maxRows < rowCount)) 220 { 221 rowCount = maxRows; 222 truncatedResults = true; 223 } 224 225 infoVector = new Vector (rowCount); 228 for (int i = 0; i < rowCount; i++) 229 infoVector.addElement( 230 dataStore.fetchBusinessInfo( 231 (String ) keyVector.elementAt(i))); 232 } 233 234 dataStore.commit(); 235 236 BusinessInfos infos = new BusinessInfos(); 239 infos.setBusinessInfoVector(infoVector); 240 241 BusinessList list = new BusinessList(); 244 list.setBusinessInfos(infos); 245 list.setGeneric(generic); 246 list.setOperator(Config.getOperator()); 247 list.setTruncated(truncatedResults); 248 return list; 249 } 250 catch(TooManyOptionsException tmoex) 251 { 252 try { dataStore.rollback(); } catch(Exception e) { } 253 log.info(tmoex.getMessage()); 254 throw (RegistryException)tmoex; 255 } 256 catch(NameTooLongException ntlex) 257 { 258 try { dataStore.rollback(); } catch(Exception e) { } 259 log.info(ntlex.getMessage()); 260 throw (RegistryException)ntlex; 261 } 262 catch(UnsupportedException suppex) 263 { 264 try { dataStore.rollback(); } catch(Exception e) { } 265 log.info(suppex.getMessage()); 266 throw (RegistryException)suppex; 267 } 268 catch(RegistryException regex) 269 { 270 try { dataStore.rollback(); } catch(Exception e) { } 271 log.error(regex); 272 throw (RegistryException)regex; 273 } 274 catch(Exception ex) 275 { 276 try { dataStore.rollback(); } catch(Exception e) { } 277 log.error(ex); 278 throw new RegistryException(ex); 279 } 280 finally 281 { 282 if (dataStore != null) 283 dataStore.release(); 284 } 285 } 286 287 288 289 290 291 public static void main(String [] args) 292 { 293 RegistryEngine reg = new RegistryEngine(); 295 reg.init(); 296 297 try 298 { 299 Vector nameVector = new Vector (5); 301 nameVector.addElement(new Name("InflexionPoint")); 302 nameVector.addElement(new Name("SteveViens.com")); 303 nameVector.addElement(new Name("Liberty Mutual")); 304 nameVector.addElement(new Name("Bowstreet")); 305 nameVector.addElement(new Name("CMGi")); 306 308 Vector qualifierVector = new Vector (1); 309 qualifierVector.add( 310 new FindQualifier(FindQualifier.EXACT_NAME_MATCH)); 311 313 FindQualifiers qualifiers = new FindQualifiers(); 314 qualifiers.setFindQualifierVector(qualifierVector); 315 316 Vector categoryVector = new Vector (); 317 categoryVector.addElement(new KeyedReference("name1", "value1")); 318 categoryVector.addElement(new KeyedReference("name2", "value2")); 319 categoryVector.addElement(new KeyedReference("name3", "value3")); 320 321 CategoryBag categoryBag = new CategoryBag(); 322 categoryBag.setKeyedReferenceVector(categoryVector); 323 324 Vector identifierVector = new Vector (); 325 identifierVector.addElement(new KeyedReference("name1", "value1")); 326 identifierVector.addElement(new KeyedReference("name1", "value1")); 327 identifierVector.addElement(new KeyedReference("name1", "value1")); 328 329 IdentifierBag identifierBag = new IdentifierBag(); 330 identifierBag.setKeyedReferenceVector(identifierVector); 331 332 Vector tModelKeyVector = new Vector (); 333 tModelKeyVector.addElement( 334 new String ("6240b6f0-d4dd-4091-851b-d59fedbd0491")); 335 tModelKeyVector.addElement( 336 new String ("ee0a154b-43ed-47be-b24f-878ab2956a31")); 337 338 TModelBag tModelBag = new TModelBag(); 339 tModelBag.setTModelKeyVector(tModelKeyVector); 340 341 Vector discoveryURLVector = new Vector (); 342 discoveryURLVector.addElement(new DiscoveryURL()); 343 discoveryURLVector.addElement(new DiscoveryURL()); 344 discoveryURLVector.addElement(new DiscoveryURL()); 345 346 DiscoveryURLs discoveryURLs = new DiscoveryURLs(); 347 discoveryURLs.setDiscoveryURLVector(discoveryURLVector); 348 349 FindBusiness request = new FindBusiness(); 350 request.setNameVector(nameVector); 351 request.setMaxRows(10); 352 request.setCategoryBag(categoryBag); 353 request.setIdentifierBag(identifierBag); 354 request.setTModelBag(tModelBag); 355 request.setDiscoveryURLs(discoveryURLs); 356 request.setFindQualifiers(qualifiers); 357 358 BusinessList response = 360 (BusinessList) (new FindBusinessFunction(reg).execute(request)); 361 System.out.println(response); 362 } 363 catch (Exception ex) 364 { 365 ex.printStackTrace(); 367 } 368 finally 369 { 370 reg.dispose(); 372 } 373 } 374 } | Popular Tags |