home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 July / 07_02.iso / software / mdmx / files / DreamweaverMXInstaller.exe / Disk1 / data1.cab / Configuration_En / Commands / ServerBeh-ASPNET-SimpRS.js < prev    next >
Encoding:
JavaScript  |  2002-05-01  |  16.1 KB  |  619 lines

  1. // Copyright 2002 Macromedia, Inc. All rights reserved.
  2.  
  3. // *************** GLOBALS VARS *****************
  4.  
  5. var helpDoc = MM.HELP_sbASPNetSimpleRecordset;
  6.  
  7. var SB_FILENAME = dwscripts.getSBFileName();
  8. var RECORDSET_SBOBJ;  // SBRecordset argument to the command.
  9. var CMD_FILENAME_ADV; // Command filename for the advanced recordset dialog.
  10. var SQL_PARAM;          // Sql parameter info.
  11.  
  12. var _RecordsetName = new TextField(SB_FILENAME, "RecordsetName");
  13. var _ConnectionName = new ConnectionMenu(SB_FILENAME, "ConnectionName");
  14. var _TableName = new ConnectionTableMenu(SB_FILENAME, "TableName");
  15. var _ColumnType = new RadioGroup(SB_FILENAME, "ColumnType");
  16. var _ColumnList = new ConnectionColumnMenu(SB_FILENAME, "ColumnList", "", false);
  17. var _FilterColumn = new ConnectionColumnMenu(SB_FILENAME, "FilterColumn", "", true);
  18. var _FilterOperator = new ListMenu(SB_FILENAME, "FilterOperator");
  19. var _FilterParameter = new ListMenu(SB_FILENAME, "FilterParameter");
  20. var _FilterParameterValue = new TextField(SB_FILENAME, "FilterParameterValue");
  21. var _SortColumn = new ConnectionColumnMenu(SB_FILENAME, "SortColumn", "", true);
  22. var _SortDirection = new ListMenu(SB_FILENAME, "SortDirection");
  23. var _FailureURL = new TextField(SB_FILENAME, "FailureURL");
  24. var _DebugInfo = new CheckBox(SB_FILENAME, "Debug");
  25.  
  26. // ******************* API **********************
  27.  
  28. //--------------------------------------------------------------------
  29. // FUNCTION:
  30. //   commandButtons
  31. //
  32. // DESCRIPTION:
  33. //   Returns the list of buttons which should appear on the right hand
  34. //   side of the dialog
  35. //
  36. // ARGUMENTS:
  37. //   none
  38. //
  39. // RETURNS:
  40. //   Array - pairs of button name and function call
  41. //--------------------------------------------------------------------
  42.  
  43. function commandButtons()
  44. {
  45.   return new Array(MM.BTN_OK,       "clickedOK()", 
  46.                    MM.BTN_Cancel,   "clickedCancel()", 
  47.                    MM.BTN_Test,     "clickedTest()", 
  48.                    MM.BTN_Advanced, "clickedAdvanced()", 
  49.                    MM.BTN_Help,     "displayHelp()"); 
  50. }
  51.  
  52. //--------------------------------------------------------------------
  53. // FUNCTION:
  54. //   clickedOK
  55. //
  56. // DESCRIPTION:
  57. //   This function is called when the user clicks OK
  58. //
  59. // ARGUMENTS:
  60. //   none
  61. //
  62. // RETURNS:
  63. //   nothing
  64. //--------------------------------------------------------------------
  65.  
  66. function clickedOK()
  67. {
  68.   // Make sure at least one column is selected
  69.  
  70.   if (_ColumnType.getValue() == "some")
  71.   {
  72.     if (_ColumnList.getValues().length == 0)
  73.     {
  74.       alert(MM.MSG_SelectColumns);
  75.       return;
  76.     }
  77.   }
  78.     
  79.   updateSBRecordsetObject();
  80.   recordsetDialog.onClickOK(window, RECORDSET_SBOBJ);
  81. }
  82.  
  83. //--------------------------------------------------------------------
  84. // FUNCTION:
  85. //   clickedCancel
  86. //
  87. // DESCRIPTION:
  88. //   This function is called when CANCEL is clicked
  89. //
  90. // ARGUMENTS:
  91. //   none
  92. //
  93. // RETURNS:
  94. //   nothing
  95. //--------------------------------------------------------------------
  96.  
  97. function clickedCancel()
  98. {
  99.   recordsetDialog.onClickCancel(window);
  100. }
  101.  
  102. //--------------------------------------------------------------------
  103. // FUNCTION:
  104. //   clickedTest
  105. //
  106. // DESCRIPTION:
  107. //   This function is called when the user clicks the TEST button
  108. //
  109. // ARGUMENTS:
  110. //   none
  111. //
  112. // RETURNS:
  113. //   nothing
  114. //--------------------------------------------------------------------
  115.  
  116. function clickedTest()
  117. {
  118.   updateSBRecordsetObject();  
  119.   recordsetDialog.displayTestDialog(RECORDSET_SBOBJ);
  120. }
  121.  
  122. //--------------------------------------------------------------------
  123. // FUNCTION:
  124. //   clickedAdvanced
  125. //
  126. // DESCRIPTION:
  127. //   This function is called when the user clicks the ADVANCED button
  128. //
  129. // ARGUMENTS:
  130. //   none
  131. //
  132. // RETURNS:
  133. //   nothing
  134. //--------------------------------------------------------------------
  135.  
  136. function clickedAdvanced()
  137. {
  138.   updateSBRecordsetObject();
  139.   recordsetDialog.onClickSwitchUI(window,
  140.                                   recordsetDialog.UI_ACTION_SWITCH_ADV, 
  141.                                   RECORDSET_SBOBJ,
  142.                                   CMD_FILENAME_ADV);
  143. }
  144.  
  145. //--------------------------------------------------------------------
  146. // FUNCTION:
  147. //   displayHelp
  148. //
  149. // DESCRIPTION:
  150. //   This function is called when the user clicks the HELP button
  151. //
  152. // ARGUMENTS:
  153. //   none
  154. //
  155. // RETURNS:
  156. //   nothing
  157. //--------------------------------------------------------------------
  158.  
  159. function displayHelp()
  160. {
  161.   dwscripts.displayDWHelp(helpDoc);
  162. }
  163.  
  164. // ***************** LOCAL FUNCTIONS  ******************
  165.  
  166. //--------------------------------------------------------------------
  167. // FUNCTION:
  168. //   initializeUI
  169. //
  170. // DESCRIPTION:
  171. //   This function is called in the onLoad event.  It is responsible
  172. //   for initializing the UI.  If we are inserting a recordset, this
  173. //   is a matter of populating the connection drop down.
  174. //
  175. //   If we are modifying a recordset, this is a matter of inspecting
  176. //   the recordset tag and setting all the form elements.
  177. //
  178. // ARGUMENTS:
  179. //   none
  180. //
  181. // RETURNS:
  182. //   nothing
  183. //--------------------------------------------------------------------
  184.  
  185. function initializeUI()
  186. {
  187.   var sqlObject = null;
  188.   var args = dwscripts.getCommandArguments();
  189.  
  190.   RECORDSET_SBOBJ = args[0];
  191.   CMD_FILENAME_ADV = args[1];
  192.   
  193.   var paramTypes = dwscripts.getParameterTypeArray(false);
  194.  
  195.   RECORDSET_SBOBJ.transformURLs(false);
  196.  
  197.   // Get the UI elements
  198.   
  199.   _RecordsetName.initializeUI();
  200.   _ConnectionName.initializeUI();
  201.   _TableName.initializeUI();
  202.   _ColumnType.initializeUI();
  203.   _ColumnList.initializeUI();
  204.   _FilterColumn.initializeUI();
  205.   _FilterOperator.initializeUI();
  206.   _FilterParameter.initializeUI(paramTypes, paramTypes);
  207.   _FilterParameterValue.initializeUI();
  208.   _SortColumn.initializeUI();
  209.   _SortDirection.initializeUI();
  210.   _FailureURL.initializeUI(); 
  211.   _DebugInfo.initializeUI(); 
  212.  
  213.   var rsName = RECORDSET_SBOBJ.getRecordsetName();
  214.   
  215.   if (!rsName)
  216.   {
  217.     rsName = RECORDSET_SBOBJ.getUniqueRecordsetName();
  218.   }
  219.   
  220.   _RecordsetName.setValue(rsName);
  221.   
  222.   var connectionName = RECORDSET_SBOBJ.getConnectionName();
  223.   
  224.   if (connectionName)
  225.   {
  226.     _ConnectionName.pickValue(RECORDSET_SBOBJ.getConnectionName());
  227.   }
  228.  
  229.   _FailureURL.setValue(RECORDSET_SBOBJ.getFailureURL());
  230.   _DebugInfo.setCheckedState(RECORDSET_SBOBJ.getDebug());
  231.  
  232.   updateUI("Debug");
  233.  
  234.   var sqlParams = new Array();
  235.   var sqlString = RECORDSET_SBOBJ.getDatabaseCall(sqlParams);
  236.  
  237.   SQL_PARAM = (sqlParams[0]) ? sqlParams[0] : (new Object());
  238.  
  239.   if (sqlString)
  240.   {
  241.     sqlObject = new SQLStatement(sqlString);
  242.   }
  243.   else
  244.   {
  245.     sqlObject = new SQLStatement("");
  246.   }
  247.  
  248.   if (sqlObject.getType() == SQLStatement.STMT_TYPE_SELECT)
  249.   {
  250.     // select the correct table
  251.  
  252.     var tableNames = sqlObject.getTableNames();
  253.     
  254.     if (tableNames && tableNames.length > 0)
  255.     {
  256.       _TableName.pickValue(tableNames[0]);
  257.     }
  258.  
  259.     // selet the correct columns
  260.     
  261.       var columnNames = sqlObject.getColumnNames();
  262.     
  263.       if (columnNames.length == 1 && columnNames[0] == "*")
  264.     {
  265.       _ColumnType.pickValue("all");
  266.     }
  267.     else
  268.     {
  269.       _ColumnType.pickValue("some");
  270.       _ColumnList.setDisabled(false);
  271.       _ColumnList.pickValues(columnNames);
  272.     }
  273.  
  274.     // Select the correct filter column
  275.     
  276.     var filterInfo = RECORDSET_SBOBJ.getSimpleWhereInfo(sqlObject, sqlParams);
  277.     
  278.     if (filterInfo != null)
  279.     {
  280.       if (filterInfo.lval)
  281.       {
  282.         _FilterColumn.pickValue(filterInfo.lval);
  283.       }
  284.  
  285.       if (filterInfo.operator)
  286.       {
  287.         _FilterOperator.pickValue(filterInfo.operator);
  288.       }
  289.       
  290.       if (filterInfo.rval)
  291.       {
  292.         var param = sqlParams[0];
  293.         var paramInfo = dwscripts.getParameterTypeFromCode(param.runtime);
  294.         if (paramInfo != null)
  295.         {
  296.           _FilterParameter.pickValue(paramInfo.varType);
  297.           _FilterParameterValue.setValue(paramInfo.varNameOrValue);
  298.         }
  299.       }
  300.     }
  301.     else
  302.     {
  303.       updateUI("FilterColumn", "onChange");
  304.     }
  305.  
  306.     var orderByInfo = RECORDSET_SBOBJ.getSimpleOrderByInfo(sqlObject);
  307.  
  308.     if (orderByInfo != null)
  309.     {
  310.       if (orderByInfo.column)
  311.       {
  312.         _SortColumn.pickValue(orderByInfo.column);
  313.       }
  314.       
  315.       if (orderByInfo.direction)
  316.       {
  317.         _SortDirection.pickValue(orderByInfo.direction);
  318.       }
  319.     }
  320.     else
  321.     {
  322.       updateUI("SortColumn", "onChange");
  323.     }
  324.   }
  325.   else
  326.   {
  327.     updateUI("ColumnType", "onChange");
  328.     updateUI("FilterColumn", "onChange");
  329.     updateUI("SortColumn", "onChange");
  330.   }
  331.  
  332.   elts = document.forms[0].elements;
  333.   if (elts && elts.length)
  334.   {
  335.     elts[0].focus();
  336.     elts[0].select();
  337.   }
  338. }
  339.  
  340. //--------------------------------------------------------------------
  341. // FUNCTION:
  342. //   updateUI
  343. //
  344. // DESCRIPTION:
  345. //   This function is called by the UI controls to handle UI updates
  346. //
  347. // ARGUMENTS:
  348. //   control - string - the name of the control sending the event
  349. //   event - string - the event which is being sent
  350. //
  351. // RETURNS:
  352. //   nothing
  353. //--------------------------------------------------------------------
  354.  
  355. function updateUI(control, event)
  356. {
  357.   if (control == "RecordsetName")
  358.   {
  359.   }
  360.   else if (control == "Define")
  361.   {
  362.     _ConnectionName.launchConnectionDialog();
  363.   }
  364.   else if (control == "ConnectionName")
  365.   {
  366.     _TableName.updateUI(_ConnectionName, event);
  367.   }
  368.   else if (control == "TableName")
  369.   {
  370.     _ColumnList.updateUI(_TableName, event);
  371.     _FilterColumn.updateUI(_TableName, event);
  372.     _SortColumn.updateUI(_TableName, event);
  373.     _ColumnType.pickValue("all");
  374.   }
  375.   else if (control == "ColumnType")
  376.   {
  377.     var value = _ColumnType.getValue();
  378.     if (value == "all")
  379.     {
  380.       _ColumnList.setDisabled(true, true);
  381.     }
  382.     else
  383.     {
  384.       _ColumnList.setDisabled(false);
  385.     }
  386.   }
  387.   else if (control == "FilterColumn")
  388.   {
  389.     if (_FilterColumn.getValue() == "")
  390.     {
  391.       _FilterOperator.setDisabled(true);
  392.       _FilterParameter.setDisabled(true);
  393.       _FilterParameterValue.setValue("");
  394.       _FilterParameterValue.setDisabled(true);
  395.     }
  396.     else
  397.     {
  398.       _FilterOperator.setDisabled(false);
  399.       _FilterParameter.setDisabled(false);
  400.       _FilterParameterValue.setDisabled(false);
  401.       _FilterParameterValue.setValue(_FilterColumn.getValue());
  402.     }
  403.   }
  404.   else if (control == "SortColumn")
  405.   {
  406.     if (_SortColumn.getValue() == "")
  407.     {
  408.       _SortDirection.setDisabled(true);
  409.     }
  410.     else
  411.     {
  412.       _SortDirection.setDisabled(false);
  413.     }
  414.   }
  415.   else if (control == "FailureURL")
  416.   {
  417.     var theFailureURL = dw.browseForFileURL("select", MM.LABEL_FileRedirect, 0, 0); 
  418.     
  419.     if (theFailureURL.length > 0)
  420.     {
  421.       _FailureURL.setValue(theFailureURL); 
  422.     }    
  423.   }
  424.   else if (control == "Debug")
  425.   {
  426.     _FailureURL.setDisabled(_DebugInfo.getCheckedState());
  427.   }
  428. }
  429.  
  430. //--------------------------------------------------------------------
  431. // FUNCTION:
  432. //   updateSBRecordsetObject
  433. //
  434. // DESCRIPTION:
  435. //   Collects information from the UI and sets the SBRecordset object
  436. //
  437. // ARGUMENTS:
  438. //   none
  439. //
  440. // RETURNS:
  441. //   boolean - true if successful, false otherwise
  442. //--------------------------------------------------------------------
  443.  
  444. function updateSBRecordsetObject()
  445. {
  446.   var connName = _ConnectionName.getValue();
  447.   var sqlObject = new SQLStatement("");
  448.   var colType = _ColumnType.getValue();
  449.  
  450.   RECORDSET_SBOBJ.setRecordsetName(_RecordsetName.getValue());  
  451.   RECORDSET_SBOBJ.setConnectionName(connName);
  452.  
  453.   sqlObject.addFrom(_TableName.getValue());
  454.  
  455.   if (colType != "all")
  456.   {
  457.     var columnNames = _ColumnList.getValues();
  458.  
  459.     for (var i=0; i < columnNames.length; i++)
  460.     {
  461.       sqlObject.addSelect(_TableName.getValue(), columnNames[i], true); // dont include table name
  462.     }
  463.   }
  464.  
  465.   var sqlParams = new Array();
  466.  
  467.   if (_FilterColumn.getValue() != "")
  468.   {
  469.     var databaseType = MMDB.getDatabaseType(connName);
  470.     var paramType = _FilterParameter.getValue();
  471.     var operatorType = _FilterOperator.getValue();
  472.     var filterValue = _FilterParameterValue.getValue();
  473.     var defaultVal = "";
  474.     var submitAs = dwscripts.getDBColumnTypeAsString(_FilterColumn.getType(), databaseType);
  475.     var runtime = dwscripts.getParameterCodeFromType(paramType, 
  476.                                                         filterValue,
  477.                                                         defaultVal,
  478.                                                         submitAs);
  479.     
  480.     // If we're filtering using begins with, ends with or contains,
  481.     // then we need to alter the runtime value to include the %(s)
  482.     
  483.     switch (operatorType)
  484.     {
  485.       case "begins with":
  486.         runtime = "(" + runtime + ") + \"%\"";
  487.         break;
  488.       case "ends with":
  489.         runtime = "\"%\" + (" + runtime + ")";
  490.         break;
  491.       case "contains":
  492.         runtime = "\"%\" + (" + runtime + ") + \"%\"";
  493.         break;
  494.     }
  495.  
  496.     var paramName = _FilterColumn.getValue();
  497.  
  498.     // If param name doesn't start with @, add one
  499.  
  500.     if (paramName[0] != '@')
  501.     {
  502.       paramName = "@" + paramName;
  503.     }
  504.  
  505.     // If the database type is OleDb, then use "?" as the parameter
  506.     // in the where clause, otherwise, use a named parameter.
  507.  
  508.     var whereParam = (databaseType.toLowerCase() == "oledb") ? "?" : paramName;
  509.  
  510.     RECORDSET_SBOBJ.addSimpleWhere(sqlObject, _FilterColumn.getValue(), operatorType, whereParam, true);
  511.  
  512.     var sqlParam = new Object();
  513.  
  514.     sqlParam.name = paramName;
  515.     sqlParam.runtime = runtime;
  516.     sqlParam.type = submitAs;
  517.       sqlParam.size = (SQL_PARAM.size) ? SQL_PARAM.size : "";
  518.       sqlParam.direction = (SQL_PARAM.direction) ? SQL_PARAM.direction : "";
  519.  
  520.     sqlParams.push(sqlParam);
  521.   }
  522.  
  523.   if (_SortColumn.getValue() != "")
  524.   {
  525.     sqlObject.addOrderBy(_TableName.getValue(), _SortColumn.getValue(), _SortDirection.getValue(), true); // dont include table name
  526.   }
  527.   
  528.   RECORDSET_SBOBJ.setDatabaseCall(sqlObject.getStatement(), sqlParams);
  529.   RECORDSET_SBOBJ.setFailureURL(_FailureURL.getValue());
  530.   RECORDSET_SBOBJ.transformURLs(true);
  531.   RECORDSET_SBOBJ.setDebug(_DebugInfo.getCheckedState());
  532. }
  533.  
  534. //--------------------------------------------------------------------
  535. // FUNCTION:
  536. //   canDisplayRecordset
  537. //
  538. // DESCRIPTION:
  539. //   Returns true if the given recordset can be displayed in this
  540. //   recordset dialog. Called by the recordsetDialog to determine which
  541. //   dialog to display.
  542. //
  543. // ARGUMENTS: 
  544. //   sbRecordset - SBRecordset. the recordset to check.
  545. //
  546. // RETURNS:
  547. //   boolean - true if can display the recordset.
  548. //--------------------------------------------------------------------
  549.  
  550. function canDisplayRecordset(sbRecordset) 
  551. {
  552.  var retVal = false;
  553.  
  554.   if (!sbRecordset)
  555.   {
  556.     retVal = true;
  557.   }
  558.  
  559.   if (!retVal)
  560.   {
  561.     var sqlParams = new Array();
  562.     var sqlString = sbRecordset.getDatabaseCall(sqlParams);
  563.  
  564.     if (!sqlString || (sqlString == ""))
  565.     {
  566.       retVal = true;
  567.       return retVal;
  568.     }
  569.     
  570.     var sqlObject = new SQLStatement(sqlString);
  571.  
  572.     // check for select statement
  573.     if (sqlObject.getType() == SQLStatement.STMT_TYPE_SELECT)
  574.     {
  575.       // check that only one table is specified
  576.       if (sqlObject.getTableNames().length == 1)
  577.       {
  578.         // check that the columns are simple values
  579.         var columns = sqlObject.getColumnNames();
  580.         var bSimpleNames = true;
  581.         for (var i=0; i < columns.length; i++)
  582.         { 
  583.           if (!sbRecordset.isSimpleColumnName(columns[i]))
  584.           {
  585.             bSimpleNames = false;
  586.             break;
  587.           }
  588.         }
  589.         
  590.         if (bSimpleNames)
  591.         {
  592.           // check for no filter column, or filter is single param
  593.           var whereInfo = sbRecordset.getSimpleWhereInfo(sqlObject, sqlParams);
  594.  
  595.           if ((sqlObject.getWhere() == "") ||
  596.                ((whereInfo != null) &&
  597.                  (!whereInfo.rval || 
  598.                  (sqlParams.length == 1))))
  599.           {
  600.             // check for a single order by column
  601.             var orderByInfo = sbRecordset.getSimpleOrderByInfo(sqlObject);
  602.  
  603.             if (orderByInfo != null)
  604.             {
  605.               // now check that the other clauses are empty
  606.               if (!sqlObject.getHaving() && !sqlObject.getGroupBy())
  607.               {
  608.                 retVal = true;
  609.               }
  610.             }
  611.           }
  612.         }
  613.       }
  614.     }
  615.   }
  616.  
  617.   return retVal;
  618. }
  619.