View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 * 10 * $Header: /cvsroot/ebxmlrr/jaxr/src/com/sun/xml/registry/client/browser/AdhocQueryPanel.java,v 1.1 2003/08/13 03:10:25 farrukh_najmi Exp $ 11 * 12 */ 13 package com.sun.xml.registry.client.browser; 14 15 import com.sun.xml.registry.ebxml.BusinessQueryManagerImpl; 16 import java.awt.GridBagConstraints; 17 import java.awt.Insets; 18 import java.awt.event.ActionEvent; 19 import java.awt.event.ActionListener; 20 import java.util.ArrayList; 21 import javax.swing.JLabel; 22 import javax.swing.JPanel; 23 import javax.swing.JScrollPane; 24 import javax.swing.SwingConstants; 25 import javax.xml.registry.JAXRException; 26 27 import java.awt.*; 28 import java.awt.event.*; 29 import java.net.*; 30 import java.io.*; 31 import java.util.*; 32 import javax.swing.*; 33 import javax.xml.registry.*; 34 import javax.xml.registry.infomodel.*; 35 36 import com.sun.xml.registry.client.browser.conf.bindings.*; 37 import com.sun.xml.registry.client.browser.conf.bindings.types.*; 38 import org.oasis.ebxml.registry.bindings.rim.*; 39 40 41 42 /*** 43 * The panel that displays UI for specifying parameters to a Ad hoc Query. 44 * 45 * @author <a href="mailto:Farrukh.Najmi@Sun.COM">Farrukh S. Najmi</a> 46 */ 47 public class AdhocQueryPanel extends QueryPanel { 48 49 private GridBagConstraints c = new GridBagConstraints(); 50 51 private FindParamsPanel findParamsPanel; 52 53 private ArrayList queries = new ArrayList(); 54 55 private JComboBox selectQueryCombo = null; 56 private JTextField queryNameText = null; 57 private JTextArea queryDescText = null; 58 59 private JPanel parentOfEntryPanel = null; 60 private ArrayList paramComponents = new ArrayList(); 61 private com.sun.xml.registry.client.browser.conf.bindings.Query query = null; 62 63 private org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(this.getClass()); 64 65 /*** 66 * Class Constructor. 67 */ 68 public AdhocQueryPanel(final FindParamsPanel findParamsPanel, Configuration cfg) { 69 super(findParamsPanel, cfg); 70 processConfiguration(); 71 72 73 GridBagLayout gb = new GridBagLayout(); 74 setLayout(gb); 75 76 JPanel querySelectionPanel = createQuerySelectionPanel(); 77 parentOfEntryPanel = createParentOfEntryPanels(); 78 79 JScrollPane querySelectionScrollPane = new JScrollPane(querySelectionPanel); 80 JScrollPane parentOfEntryScrollPane = new JScrollPane(parentOfEntryPanel); 81 JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false, 82 querySelectionScrollPane, parentOfEntryScrollPane); 83 84 setLayout(new BorderLayout()); 85 add(splitPane, BorderLayout.CENTER); 86 87 setQuery((com.sun.xml.registry.client.browser.conf.bindings.Query)queries.get(0)); 88 } 89 90 private JPanel createQuerySelectionPanel() { 91 JPanel querySelectionPanel = new JPanel(); 92 GridBagLayout gbl = new GridBagLayout(); 93 querySelectionPanel.setLayout(gbl); 94 95 //The selectQueryCombo 96 JLabel selectQueryLabel = new JLabel("Select Query:", SwingConstants.LEFT); 97 c.gridx = 0; 98 c.gridy = 0; 99 c.gridwidth = 1; 100 c.gridheight = 1; 101 c.weightx = 0.0; 102 c.weighty = 0.0; 103 c.fill = GridBagConstraints.NONE; 104 c.anchor = GridBagConstraints.WEST; 105 c.insets = new Insets(4, 4, 0, 4); 106 gbl.setConstraints(selectQueryLabel, c); 107 querySelectionPanel.add(selectQueryLabel); 108 109 selectQueryCombo = new JComboBox(getQueryNames()); 110 selectQueryCombo.addItemListener(new ItemListener() { 111 public void itemStateChanged(ItemEvent ev) { 112 if (ev.getStateChange() == ItemEvent.DESELECTED) { 113 return; 114 } 115 String item = (String) ev.getItem(); 116 int index = ((JComboBox)ev.getSource()).getSelectedIndex(); 117 118 com.sun.xml.registry.client.browser.conf.bindings.Query query = 119 (com.sun.xml.registry.client.browser.conf.bindings.Query)queries.get(index); 120 121 setQuery(query); 122 } 123 }); 124 125 c.gridx = 0; 126 c.gridy = 1; 127 c.gridwidth = 1; 128 c.gridheight = 1; 129 c.weightx = 0.5; 130 c.weighty = 0.0; 131 c.fill = GridBagConstraints.HORIZONTAL; 132 c.anchor = GridBagConstraints.EAST; 133 c.insets = new Insets(4, 4, 4, 4); 134 gbl.setConstraints(selectQueryCombo, c); 135 querySelectionPanel.add(selectQueryCombo); 136 137 //The nameTextField 138 JLabel queryNameLabel = new JLabel("Name:", SwingConstants.LEFT); 139 c.gridx = 0; 140 c.gridy = 2; 141 c.gridwidth = 1; 142 c.gridheight = 1; 143 c.weightx = 0.0; 144 c.weighty = 0.0; 145 c.fill = GridBagConstraints.NONE; 146 c.anchor = GridBagConstraints.WEST; 147 c.insets = new Insets(4, 4, 0, 4); 148 gbl.setConstraints(queryNameLabel, c); 149 querySelectionPanel.add(queryNameLabel); 150 151 queryNameText = new JTextField(); 152 queryNameText.setEditable(false); 153 c.gridx = 0; 154 c.gridy = 3; 155 c.gridwidth = 1; 156 c.gridheight = 1; 157 c.weightx = 0.5; 158 c.weighty = 0.0; 159 c.fill = GridBagConstraints.HORIZONTAL; 160 c.anchor = GridBagConstraints.EAST; 161 c.insets = new Insets(4, 4, 4, 4); 162 gbl.setConstraints(queryNameText, c); 163 querySelectionPanel.add(queryNameText); 164 165 //The description TextArea 166 JLabel queryDescLabel = new JLabel("Description:", SwingConstants.LEFT); 167 c.gridx = 0; 168 c.gridy = 4; 169 c.gridwidth = 1; 170 c.gridheight = 1; 171 c.weightx = 0.0; 172 c.weighty = 0.0; 173 c.fill = GridBagConstraints.NONE; 174 c.anchor = GridBagConstraints.WEST; 175 c.insets = new Insets(4, 4, 0, 4); 176 gbl.setConstraints(queryDescLabel, c); 177 querySelectionPanel.add(queryDescLabel); 178 179 queryDescText = new JTextArea(4, 25); 180 queryDescText.setLineWrap(true); 181 queryDescText.setEditable(false); 182 c.gridx = 0; 183 c.gridy = 5; 184 c.gridwidth = 1; 185 c.gridheight = 1; 186 c.weightx = 0.5; 187 c.weighty = 0.5; 188 c.fill = GridBagConstraints.BOTH; 189 c.anchor = GridBagConstraints.EAST; 190 c.insets = new Insets(4, 4, 4, 4); 191 gbl.setConstraints(queryDescText, c); 192 querySelectionPanel.add(queryDescText); 193 194 return querySelectionPanel; 195 } 196 197 private void setQuery(com.sun.xml.registry.client.browser.conf.bindings.Query query) { 198 if (this.query != null) { 199 String queryStr = getQuery(); 200 System.err.println("queryStr='" + queryStr + "'"); 201 } 202 203 this.query = query; 204 String name = getQueryName(query); 205 queryNameText.setText(name); 206 queryDescText.setText(getQueryDescription(query)); 207 208 JPanel parameterEntryPanel = createParameterEntryPanel(query); 209 parentOfEntryPanel.setVisible(false); 210 parentOfEntryPanel.removeAll(); 211 parentOfEntryPanel.add(parameterEntryPanel); 212 parentOfEntryPanel.setVisible(true); 213 } 214 215 protected void processConfiguration() { 216 com.sun.xml.registry.client.browser.conf.bindings.Query[] _queries = cfg.getQuery(); 217 218 for (int i=0; i<_queries.length; i++) { 219 queries.add(_queries[i]); 220 } 221 } 222 223 private String getQueryDescription(com.sun.xml.registry.client.browser.conf.bindings.Query query) { 224 String queryDesc=null; 225 226 org.oasis.ebxml.registry.bindings.rim.Description desc = query.getDescription(); 227 queryDesc = getLocalizedValue(desc); 228 229 return queryDesc; 230 } 231 232 private String getQueryName(com.sun.xml.registry.client.browser.conf.bindings.Query query) { 233 String queryName=null; 234 235 org.oasis.ebxml.registry.bindings.rim.Name name = query.getName(); 236 queryName = getLocalizedValue(name); 237 238 return queryName; 239 } 240 241 private String[] getQueryNames() { 242 String[] queryNames = new String[queries.size()]; 243 244 int i=0; 245 Iterator iter = queries.iterator(); 246 while (iter.hasNext()) { 247 com.sun.xml.registry.client.browser.conf.bindings.Query query = (com.sun.xml.registry.client.browser.conf.bindings.Query)iter.next(); 248 queryNames[i++] = getQueryName(query); 249 } 250 251 return queryNames; 252 } 253 254 private String getLocalizedValue(InternationalStringType i18n) { 255 String value = ""; 256 257 InternationalStringTypeItem[] items = i18n.getInternationalStringTypeItem(); 258 259 if (items.length > 0) { 260 LocalizedStringType l10n = items[0].getLocalizedString(); 261 value = l10n.getValue(); 262 } 263 264 return value; 265 266 267 /* 268 for (int i=0; i<items.length; i++) { 269 LocalizedStringType l10n = items[i].getLocalizedString(); 270 271 javax.xl.registry.infomodel.LocalizedString 272 } 273 */ 274 } 275 276 private JPanel createParentOfEntryPanels() { 277 278 JPanel panel = new JPanel(); 279 panel.setBorder( 280 BorderFactory.createTitledBorder("Ad Hoc Query Parameters")); 281 BorderLayout bl = new BorderLayout(); 282 panel.setLayout(bl); 283 284 return panel; 285 } 286 287 private JPanel createParameterEntryPanel(com.sun.xml.registry.client.browser.conf.bindings.Query query) { 288 289 JPanel parameterEntryPanel = new JPanel(); 290 GridBagLayout gbl = new GridBagLayout(); 291 parameterEntryPanel.setLayout(gbl); 292 293 paramComponents.clear(); 294 Parameter[] params = query.getParameter(); 295 for (int i=0; i<params.length; i++) { 296 297 Parameter param = params[i]; 298 String tooltip = getLocalizedValue(param.getDescription()); 299 JLabel label = new JLabel(getLocalizedValue(param.getName())+":", SwingConstants.RIGHT); 300 label.setToolTipText(tooltip); 301 c.gridx = 0; 302 c.gridy = i; 303 c.gridwidth = 1; 304 c.gridheight = 1; 305 c.weightx = 0.0; 306 c.weighty = 0.0; 307 c.fill = GridBagConstraints.NONE; 308 c.anchor = GridBagConstraints.EAST; 309 c.insets = new Insets(4, 4, 0, 4); 310 gbl.setConstraints(label, c); 311 parameterEntryPanel.add(label); 312 313 //Get default value for Parameter if any 314 String defaultValue = param.getDefaultValue(); 315 316 JComponent comp = getComponentForParameter(param, defaultValue); 317 //comp.setToolTipText(tooltip); 318 c.gridx = 1; 319 c.gridy = i; 320 c.gridwidth = 1; 321 c.gridheight = 1; 322 c.weightx = 0.5; 323 c.weighty = 0.0; 324 c.fill = GridBagConstraints.HORIZONTAL; 325 c.anchor = GridBagConstraints.EAST; 326 c.insets = new Insets(4, 4, 4, 4); 327 gbl.setConstraints(comp, c); 328 parameterEntryPanel.add(comp); 329 paramComponents.add(comp); 330 331 332 } 333 334 return parameterEntryPanel; 335 } 336 337 private JComponent getComponentForParameter(Parameter param, String defaultValue) { 338 JComponent comp = null; 339 340 DatatypeType type = param.getDatatype(); 341 342 if (type.getType() == DatatypeType.BOOLEAN_TYPE) { 343 boolean val = false; 344 if (defaultValue.equalsIgnoreCase("true")) { 345 val = true; 346 } 347 comp = new JCheckBox("", val); 348 } 349 else if (type.getType() == DatatypeType.TAXONOMYELEMENT_TYPE) { 350 String domainId = null; 351 //Get the domain taxonomy element id 352 SlotList slotList = param.getSlotList(); 353 org.oasis.ebxml.registry.bindings.rim.Slot[] slots = slotList.getSlot(); 354 for (int i=0; i<slots.length; i++) { 355 if (slots[i].getName().equalsIgnoreCase("domain")) { 356 ValueList valList = slots[i].getValueList(); 357 358 if (valList != null) { 359 ValueListTypeItem[] vlti = valList.getValueListTypeItem(); 360 domainId = vlti[0].getValue(); 361 } 362 363 } 364 } 365 366 javax.xml.registry.infomodel.RegistryObject ro = null; 367 368 try { 369 JAXRClient client = RegistryBrowser.getInstance().getClient(); 370 javax.xml.registry.BusinessQueryManager bqm = client.getBusinessQueryManager(); 371 ro = bqm.getRegistryObject(domainId); 372 } 373 catch (JAXRException e) { 374 log.error(e); 375 System.exit(-1); 376 } 377 378 ConceptsTreeModel domainTreeModel = new ConceptsTreeModel(true, ro); 379 comp = new com.sun.xml.registry.client.browser.TreeCombo(domainTreeModel); 380 } 381 else { 382 comp = new JTextField(defaultValue); 383 ((JTextField)comp).addActionListener(new ActionListener() { 384 public void actionPerformed(ActionEvent e) { 385 RegistryBrowser.getInstance().findAction(); 386 } 387 }); 388 } 389 390 return comp; 391 } 392 393 private String getQuery() { 394 String query = null; 395 396 QueryTypeChoice qtc = this.query.getQueryTypeChoice(); 397 query = qtc.getSQLQuery(); 398 399 if (query == null) { 400 org.oasis.ebxml.registry.bindings.query.FilterQuery fq = qtc.getFilterQuery(); 401 402 StringWriter sw = new StringWriter(); 403 try { 404 fq.marshal(sw); 405 query = sw.toString(); 406 } 407 catch (org.exolab.castor.xml.MarshalException e) { 408 e.printStackTrace(); 409 } 410 catch (org.exolab.castor.xml.ValidationException e) { 411 e.printStackTrace(); 412 } 413 } 414 415 //Now replace the positional parameters in query with values from param data entry components 416 int paramCnt = paramComponents.size(); 417 for (int i=paramCnt-1; i>=0; i--) { 418 Component comp = (Component)paramComponents.get(i); 419 query = replaceQueryParameter(query, i, comp); 420 } 421 422 //Now replace special variable in the query (e.g. $callerId, $currentTime) 423 query = replaceSpecialVariables(query); 424 425 return query; 426 } 427 428 private String replaceQueryParameter(String query, int paramPosition, Component component) { 429 String newQuery = query; 430 431 try { 432 String paramValue = null; 433 if (component instanceof javax.swing.text.JTextComponent) { 434 paramValue = ((javax.swing.text.JTextComponent)component).getText(); 435 } 436 else if (component instanceof javax.swing.AbstractButton) { 437 boolean selected = ((javax.swing.AbstractButton)component).isSelected(); 438 if (selected) { 439 paramValue = "TRUE"; 440 } 441 else { 442 paramValue = "FALSE"; 443 } 444 } 445 else if (component instanceof com.sun.xml.registry.client.browser.TreeCombo) { 446 Object conceptsTreeNode = ((com.sun.xml.registry.client.browser.TreeCombo)component).getSelectedItemsObject(); 447 Object nodeInfo = ((javax.swing.tree.DefaultMutableTreeNode)conceptsTreeNode).getUserObject(); 448 if (nodeInfo instanceof com.sun.xml.registry.client.browser.NodeInfo) { 449 Object obj = ((com.sun.xml.registry.client.browser.NodeInfo)nodeInfo).obj; 450 if (obj instanceof javax.xml.registry.infomodel.ClassificationScheme) { 451 paramValue = "/" + ((javax.xml.registry.infomodel.ClassificationScheme)obj).getKey().getId(); 452 } 453 else if (obj instanceof javax.xml.registry.infomodel.Concept) { 454 paramValue = ((Concept)obj).getPath(); 455 } 456 } 457 } 458 else { 459 RegistryBrowser.displayError("Internal error: unsupported component class: " + component.getClass().getName()); 460 } 461 462 //??Need to do type checking here in future. 463 464 String paramName = "$" + (paramPosition+1); 465 466 int index=0; 467 while (index != -1) { 468 index = query.indexOf(paramName); 469 470 if (index != -1) { 471 newQuery = query.substring(0, index); 472 newQuery += paramValue; 473 newQuery += query.substring(index+paramName.length()); 474 query = newQuery; 475 } 476 } 477 } 478 catch (JAXRException e) { 479 log.error(e); 480 } 481 482 return newQuery; 483 } 484 485 private String replaceSpecialVariables(String query) { 486 String newQuery = query; 487 488 489 String paramName = "$callerId"; 490 491 if (query.indexOf(paramName) > 0) { 492 493 String paramValue = null; 494 try { 495 Connection connection = RegistryBrowser.getInstance().client.getConnection(); 496 RegistryService service = connection.getRegistryService(); 497 DeclarativeQueryManager dqm = service.getDeclarativeQueryManager(); 498 499 paramValue =((com.sun.xml.registry.ebxml.QueryManagerImpl)(dqm)).getCallersUser().getKey().getId(); 500 } 501 catch (JAXRException e) { 502 RegistryBrowser.displayError(e); 503 } 504 505 int index=0; 506 while (index != -1) { 507 index = query.indexOf(paramName); 508 509 if (index != -1) { 510 newQuery = query.substring(0, index); 511 newQuery += paramValue; 512 newQuery += query.substring(index+paramName.length()); 513 query = newQuery; 514 } 515 } 516 } 517 518 return newQuery; 519 } 520 521 522 /*** 523 * Execute the query using parameters defined by the fields in QueryPanel. 524 */ 525 BulkResponse executeQuery() { 526 BulkResponse resp = null; 527 528 try { 529 Connection connection = RegistryBrowser.getInstance().client.getConnection(); 530 RegistryService service = connection.getRegistryService(); 531 DeclarativeQueryManager dqm = service.getDeclarativeQueryManager(); 532 533 String queryStr = getQuery(); 534 javax.xml.registry.Query query = dqm.createQuery(javax.xml.registry.Query.QUERY_TYPE_SQL, queryStr); 535 536 // make JAXR request 537 resp = dqm.executeQuery(query); 538 } 539 catch (JAXRException e) { 540 RegistryBrowser.displayError(e); 541 } 542 543 return resp; 544 } 545 546 } 547 548 549 550

This page was automatically generated by Maven