using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using Resources;
namespace SiteOffice.UI{
/// <summary>
/// Author: Joe Audette
/// Created: 2007-06-21
/// Last Modified: 2007-12-02
///
/// The use and distribution terms for this software are covered by the
/// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
/// which can be found in the file CPL.TXT at the root of this distribution.
/// By using this software in any fashion, you are agreeing to be bound by
/// the terms of this license.
///
/// You must not remove this notice, or any other, from this software.
/// </summary>
public partial class GearsDBAdmin : UserControl
{
private string overrideDB = "";
public string OverrideDB
{
get { return overrideDB; }
set { overrideDB = value; }
}
private string dbName = "dbUser";
private SiteUser siteUser;
protected void Page_Load(object sender, EventArgs e)
{
LoadSettings();
PopulateLabels();
//if (WebUser.IsAdmin)
//{
SetupScripts();
//}
}
private void SetupScripts()
{
SetupInlcudeScripts();
SetupMainScripts();
// SetupInitScripts();
}
private void SetupMainScripts()
{
StringBuilder dbScript = new StringBuilder();
dbScript.Append("<script language='javascript' type='text/javascript'>\n<!--\n");
// global javascript vars
dbScript.Append("\n var db;");
dbScript.Append("\n var timerId;");
dbScript.Append("\n var timeout = 2000; "); //2 seconds
dbScript.Append("\n var spanGears = document.getElementById('" + spnGearsLink.ClientID + "'); ");
dbScript.Append("\n var spanStatus = document.getElementById('" + lblStatus.ClientID + "'); ");
dbScript.Append("\n var queryTextArea = document.getElementById('" + txtQuery.ClientID + "'); ");
dbScript.Append("\n var outputDiv = document.getElementById('" + pnlOutput.ClientID + "'); ");
dbScript.Append("\n var executedNoDataReturnedMessage = '" + GearsDBToolResources.ExecutedNoDataReturnedMessage + "';");
//dbScript.Append("\n var noGearsMessage = \"" + GearsDBToolResources.GearsNeededMessage + "\"; ");
//dbScript.Append("\n var noGearsMessage = 'no gears';");
dbScript.Append("\n var createDBFailureMessage = \"" + GearsDBToolResources.CouldNotCreateDBMessage + "\"; ");
dbScript.Append("\n var executeSQLFailureMessage = \"" + GearsDBToolResources.ExecuteSQLFailureMessage + "\"; ");
dbScript.Append("\n var couldNotRetrieveQueryMessage = \"" + GearsDBToolResources.CouldNotRetrieveQueryMessage + "\"; ");
dbScript.Append("\n var noQuerySelectedMessage = \"" + GearsDBToolResources.NoQuerySelectedMessage + "\"; ");
dbScript.Append("\n var couldNotSaveQueryMessage = \"" + GearsDBToolResources.CouldNotSaveQueryMessage + "\"; ");
dbScript.Append("\n var couldNotDeleteQueryMessage = \"" + GearsDBToolResources.CouldNotDeleteQueryMessage + "\"; ");
dbScript.Append("\n var noTableSelectedMessage = \"" + GearsDBToolResources.NoTableSelectedMessage + "\"; ");
dbScript.Append("\n scheduleFunction(timerId,timeout,doSetup);");
WriteEnsureGearsDBFunction(dbScript);
WriteExecuteSqlFunction(dbScript);
WritePopulateTableListFunction(dbScript);
WritePopulateSavedQueriesListFunction(dbScript);
WritePopulateListsFunction(dbScript);
WriteSaveQueryFunction(dbScript);
WriteDeleteQueryFunction(dbScript);
WriteSetQueryFunction(dbScript);
WriteSelectTableFunction(dbScript);
WriteDoSetupFunction(dbScript);
WriteGetHideGearsLinkFunction(dbScript);
dbScript.Append("\n//-->\n</script>");
Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"gears_dbAdmin",
dbScript.ToString());
}
private void WriteEnsureGearsDBFunction(StringBuilder dbScript)
{
dbScript.Append("function ensureGearsDB() { ");
// dbScript.Append("scheduleFunction(timerId,timeout,getControls); ");
dbScript.Append("if (!window.google || !google.gears) ");
dbScript.Append("{ ");
//dbScript.Append("outputDiv.innerHTML = noGearsMessage; ");
dbScript.Append(" ");
dbScript.Append("return; ");
dbScript.Append("} ");
dbScript.Append("try { ");
// Open this page's local database.
dbScript.Append("db = new GearsDB('" + dbName + "'); ");
dbScript.Append("db.run('create table if not exists savedqueries (' + ");
dbScript.Append("'id integer not null primary key autoincrement,' + ");
dbScript.Append("'name varchar(255),' + ");
dbScript.Append("'query text)'); ");
dbScript.Append(" ");
dbScript.Append(" ");
dbScript.Append(" ");
//dbScript.Append("alert('loaded'); ");
dbScript.Append("} catch (ex) { ");
dbScript.Append("setError(spanStatus, createDBFailureMessage + ex.message); ");
dbScript.Append(" } ");
dbScript.Append(" } ");
}
private void WriteExecuteSqlFunction(StringBuilder dbScript)
{
dbScript.Append("function executeSql() { ");
dbScript.Append("if (!google.gears.factory || !db) { return; } ");
dbScript.Append("var query = queryTextArea.value; ");
dbScript.Append("var args = []; ");
dbScript.Append("try { ");
dbScript.Append("if(query.length > 0) ");
dbScript.Append("{ ");
dbScript.Append("var rs = db.run(query, args); ");
dbScript.Append("printRecordset(rs, outputDiv, executedNoDataReturnedMessage); ");
dbScript.Append("} ");
dbScript.Append("} catch (ex) { ");
dbScript.Append("setError(spanStatus, executeSQLFailureMessage + ex.message); } ");
dbScript.Append(" } ");
}
private void WritePopulateListsFunction(StringBuilder dbScript)
{
dbScript.Append("function populateLists() { ");
dbScript.Append("populateTableList(); ");
dbScript.Append("populateSavedQueriesList(); ");
dbScript.Append(" } ");
}
private void WritePopulateTableListFunction(StringBuilder dbScript)
{
dbScript.Append("function populateTableList() { ");
dbScript.Append("if (!google.gears.factory || !db) { return; } ");
dbScript.Append("var ddTables = document.getElementById('" + ddTables.ClientID + "'); ");
dbScript.Append("var query = 'select name from sqlite_master ;'; ");
dbScript.Append("var args = []; ");
dbScript.Append("try { ");
dbScript.Append("var rs = db.run(query, args); ");
dbScript.Append("populateDropdownList(rs, ddTables, 0,0); ");
dbScript.Append("addOption(ddTables,\"sqlite_master\", \"sqlite_master\"); ");
dbScript.Append("} catch (ex) { ");
dbScript.Append("setError(spanStatus, executeSQLFailureMessage + ex.message); } ");
dbScript.Append(" } ");
}
private void WritePopulateSavedQueriesListFunction(StringBuilder dbScript)
{
dbScript.Append("function populateSavedQueriesList() { ");
dbScript.Append("if (!google.gears.factory || !db) { return; } ");
dbScript.Append("var ddQueries = document.getElementById('" + ddSavedQueries.ClientID + "'); ");
dbScript.Append("var query = 'select id, name from savedqueries ;'; ");
dbScript.Append("var args = []; ");
dbScript.Append("try { ");
dbScript.Append("var rs = db.run(query, args); ");
dbScript.Append("populateDropdownList(rs, ddQueries, 0,1); ");
dbScript.Append("} catch (ex) { ");
dbScript.Append("setError(spanStatus, executeSQLFailureMessage + ex.message); } ");
dbScript.Append(" } ");
}
private void WriteSetQueryFunction(StringBuilder dbScript)
{
dbScript.Append("function setQuery() { ");
dbScript.Append("if (!google.gears.factory || !db) { return; } ");
dbScript.Append("var ddQueries = document.getElementById('" + ddSavedQueries.ClientID + "'); ");
dbScript.Append("var queryTextArea = document.getElementById('" + txtQuery.ClientID + "'); ");
dbScript.Append("var selectedQueryID; ");
dbScript.Append("for (i = 0; i < ddQueries.length; i++) { ");
dbScript.Append("if (ddQueries[i].selected) { ");
dbScript.Append("selectedQueryID = ddQueries[i].value; ");
dbScript.Append("} } ");
dbScript.Append("if(selectedQueryID){ ");
dbScript.Append("var query = 'select query from savedqueries where id = ? ;'; ");
dbScript.Append("var args = [selectedQueryID]; ");
dbScript.Append("try { ");
dbScript.Append("var rs = db.run(query, args); ");
dbScript.Append("queryTextArea.value = rs.field(0); ");
dbScript.Append("} catch (ex) { ");
dbScript.Append("setError(spanStatus, couldNotRetrieveQueryMessage + ex.message); } ");
dbScript.Append("} else { ");
dbScript.Append("alert('no query selected'); ");
dbScript.Append(" } ");
dbScript.Append(" } ");
}
private void WriteDeleteQueryFunction(StringBuilder dbScript)
{
dbScript.Append("function deleteQuery() { ");
dbScript.Append("if (!google.gears.factory || !db) { return; } ");
dbScript.Append("var ddQueries = document.getElementById('" + ddSavedQueries.ClientID + "'); ");
dbScript.Append("var queryTextArea = document.getElementById('" + txtQuery.ClientID + "'); ");
dbScript.Append("var selectedQueryID; ");
dbScript.Append("for (i = 0; i < ddQueries.length; i++) { ");
dbScript.Append("if (ddQueries[i].selected) { ");
dbScript.Append("selectedQueryID = ddQueries[i].value; ");
dbScript.Append("} } ");
dbScript.Append("if(selectedQueryID){ ");
dbScript.Append("var query = 'delete from savedqueries where id = ? ;'; ");
dbScript.Append("var args = [selectedQueryID]; ");
dbScript.Append("try { ");
dbScript.Append("var rs = db.run(query, args); ");
dbScript.Append("populateSavedQueriesList(); ");
dbScript.Append("} catch (ex) { ");
dbScript.Append("setError(spanStatus, couldNotDeleteQueryMessage + ex.message); } ");
dbScript.Append("} else { ");
dbScript.Append("alert(noQuerySelectedMessage); ");
dbScript.Append(" } ");
dbScript.Append(" } ");
}
private void WriteSaveQueryFunction(StringBuilder dbScript)
{
dbScript.Append("function saveQuery() { ");
dbScript.Append("if (!google.gears.factory || !db) { return; } ");
dbScript.Append("var txtQueryName = document.getElementById('" + txtQueryName.ClientID + "'); ");
dbScript.Append("var txtQuery = document.getElementById('" + txtQuery.ClientID + "'); ");
dbScript.Append("var queryName = txtQueryName.value; ");
dbScript.Append("var query = txtQuery.value; ");
dbScript.Append("try { ");
dbScript.Append("createSavedQuery(db,queryName,query); ");
dbScript.Append("populateSavedQueriesList(); ");
dbScript.Append("} catch (ex) { ");
dbScript.Append("setError(spanStatus, couldNotSaveQueryMessage + ex.message); } ");
dbScript.Append(" } ");
}
private void WriteSelectTableFunction(StringBuilder dbScript)
{
dbScript.Append("function selectTable() { ");
dbScript.Append("if (!google.gears.factory || !db) { return; } ");
dbScript.Append("var ddTables = document.getElementById('" + ddTables.ClientID + "'); ");
dbScript.Append("var queryTextArea = document.getElementById('" + txtQuery.ClientID + "'); ");
dbScript.Append("var selectedTable; ");
dbScript.Append("for (i = 0; i < ddTables.length; i++) { ");
dbScript.Append("if (ddTables[i].selected) { ");
dbScript.Append("selectedTable = ddTables[i].value; ");
dbScript.Append("} } ");
dbScript.Append("if(selectedTable){ ");
dbScript.Append("try { ");
dbScript.Append("queryTextArea.value = 'select * from ' + selectedTable + ';'; ");
dbScript.Append("} catch (ex) { ");
dbScript.Append("setError(spanStatus,'Could not populateSavedQueriesList: ' + ex.message); } ");
dbScript.Append("} else { ");
dbScript.Append("alert(noTableSelectedMessage); ");
dbScript.Append(" } ");
dbScript.Append(" } ");
}
private void WriteDoSetupFunction(StringBuilder dbScript)
{
dbScript.Append("function doSetup() { ");
dbScript.Append("\n spanGears = document.getElementById('" + spnGearsLink.ClientID + "'); ");
dbScript.Append("\n spanStatus = document.getElementById('" + lblStatus.ClientID + "'); ");
dbScript.Append("\n queryTextArea = document.getElementById('" + txtQuery.ClientID + "'); ");
dbScript.Append("\n outputDiv = document.getElementById('" + pnlOutput.ClientID + "'); ");
dbScript.Append("if (!window.google || !google.gears) ");
dbScript.Append("{ ");
//dbScript.Append("outputDiv.innerHTML = noGearsMessage; ");
dbScript.Append(" ");
dbScript.Append("cancelSchedule(timerId); ");
dbScript.Append("return; ");
dbScript.Append("} ");
dbScript.Append("ensureGearsDB(); ");
dbScript.Append("populateLists(); ");
dbScript.Append("cancelSchedule(timerId); ");
dbScript.Append("hideGearsLink(); ");
dbScript.Append(" } ");
}
private void WriteGetHideGearsLinkFunction(StringBuilder dbScript)
{
dbScript.Append("function hideGearsLink() { ");
dbScript.Append("\n spanGears = document.getElementById('" + spnGearsLink.ClientID + "'); ");
dbScript.Append("if (window.google && google.gears) ");
dbScript.Append("{ ");
dbScript.Append("spanGears.style.display = 'none'; ");
dbScript.Append(" ");
dbScript.Append("} ");
dbScript.Append(" } ");
}
//private void SetupInitScripts()
//{
// StringBuilder dbScript = new StringBuilder();
// dbScript.Append("<script language='javascript' type='text/javascript'>\n<!--\n");
// dbScript.Append("init(); ");
// dbScript.Append(" ");
// dbScript.Append("\n//-->\n</script>");
// Page.ClientScript.RegisterStartupScript(
// this.GetType(),
// "gears_dbAdmin_Init",
// dbScript.ToString());
//}
private void SetupInlcudeScripts()
{
this.Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"firebug", "\n<script type=\"text/javascript\" src=\""
+ ResolveUrl("~/ClientScript/firebug/firebug.js") + "\"></script>");
this.Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"gears_init", "\n<script type=\"text/javascript\" src=\""
+ ResolveUrl("~/ClientScript/google/gears/gears_init.js") + "\"></script>");
this.Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"gears_db", "\n<script type=\"text/javascript\" src=\""
+ ResolveUrl("~/ClientScript/google/gears/gears_db.js") + "\"></script>");
this.Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"gears_utils", "\n<script type=\"text/javascript\" src=\""
+ ResolveUrl("~/ClientScript/google/gears/gears_utils.js") + "\"></script>");
this.Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"saved_queries", "\n<script type=\"text/javascript\" src=\""
+ ResolveUrl("~/ClientScript/google/gears/saved_queries.js") + "\"></script>");
}
private void PopulateLabels()
{
litHeading.Text = GearsDBToolResources.QueryToolHeading;
litGearsInfo.Text = GearsDBToolResources.GearsNeededMessage;
btnSelectTable.Text = GearsDBToolResources.SelectTableButton;
btnSelectTable.Attributes.Add("OnClick", "selectTable(); return false;");
btnExecuteQuery.Text = GearsDBToolResources.ExecuteQueryButton;
btnExecuteQuery.Attributes.Add("OnClick", "executeSql(); return false;");
btnLoadSavedQuery.Text = GearsDBToolResources.LoadSavedQueryButton;
btnLoadSavedQuery.Attributes.Add("OnClick", "setQuery(); return false;");
btnSaveQuery.Text = GearsDBToolResources.SaveQueryButton;
btnSaveQuery.Attributes.Add("OnClick", "saveQuery(); return false;");
btnDeleteSavedQuery.Text = GearsDBToolResources.DeleteSavedQueryButton;
btnDeleteSavedQuery.Attributes.Add("OnClick", "deleteQuery(); return false;");
lnkSQLite.Text = GearsDBToolResources.SQLiteSyntaxLink;
lnkSQLite.ToolTip = GearsDBToolResources.SQLiteSyntaxLink;
lnkSQLite.NavigateUrl = "http://www.sqlite.org/lang.html";
}
private void LoadSettings()
{
siteUser = SiteUtils.GetCurrentSiteUser();
dbName = "db" + siteUser.UserGuid.ToString().Replace("-", string.Empty);
if (overrideDB.Length > 0) dbName = overrideDB;
//dbName = "dbTest.db";
//dbName = "PersistentStorage";
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new EventHandler(Page_Load);
}
}
}
|