home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 86 / PIWD86.iso / pc / contents / dreamweaver / software / dwmx2004.exe / Disk1 / data1.cab / Configuration_En / Commands / ServerBeh-ASPNET-SimpRS.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  16.2 KB  |  620 lines

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