Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / C#

Genesis Hybrid Smart Client Framework part III

Rate me:
Please Sign up or sign in to vote.
4.29/5 (10 votes)
19 Jun 2009Ms-PL10 min read 33.9K   7  
This is part III of a VII part series. This article covers the back-end of the Genesis Smart Client Framework including the database design.
#region Copyright Clause
/*
 * Blue Marble Genesis Smart Client Framework
 * 
 * Copyright � 2009 Blue Marble. All rights reserved.
 * 
 * This code is protected from any unauthorized duplication or viewing under
 * international copyright laws. Unauthorized access to this code is prosecutable
 * under the full extent of the law.
 */
#endregion

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
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 System.Collections.Generic;

public partial class Update : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["GenesisConnectionString"].ConnectionString);

            SqlCommand command = new SqlCommand();

            command.Connection = connection;
            command.CommandText = " SELECT * FROM Module_File ";

            connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            List<string> updateStrings = new List<string>();
            List<string> commandStrings = new List<string>();

            Response.Write("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");

            int recordCounter = 0;

            string tempPath = ConfigurationManager.AppSettings["TempPath"];
            string webPath = ConfigurationManager.AppSettings["WebPath"];
            string localPath = ConfigurationManager.AppSettings["LocalPath"];
            string developmentPath = ConfigurationManager.AppSettings["DevelopmentPath"];

            tempPath = tempPath + Guid.NewGuid().ToString() + "\\";

            while (reader.Read())
            {
                string backgroundColor = "White";

                if ((recordCounter % 2) == 0)
                {
                    backgroundColor = "Gainsboro";
                }

                Response.Write(string.Format("<tr bgcolor=\"{0}\"><td width=\"80%\">", backgroundColor));
                
                string filePath = (string)reader["FilePath"];
                string fileName = (string)reader["FileName"];
                string downloadPath = (string)reader["FileUrl"];

                int fileType = (int)reader["FileTypeID"];

                string folderTree = downloadPath.ToLower().Replace(webPath.ToLower(), "");
                folderTree = folderTree.ToLower().Replace(fileName.ToLower(), "");
                folderTree = folderTree.Replace("/", "\\");

                if (fileType == 1)
                {
                    folderTree = folderTree.Remove(folderTree.Length - 1);
                }

                Response.Write(filePath + fileName);

                bool failed = false;

                try
                {
                    System.IO.Directory.CreateDirectory(localPath + folderTree);
                    System.IO.Directory.CreateDirectory(tempPath + folderTree);

                    if (fileType == 1)
                    {
                        System.IO.File.Copy(developmentPath + filePath + fileName, localPath + folderTree + fileName, true);
                        System.IO.File.Copy(developmentPath + filePath + fileName, tempPath + folderTree + fileName, true);

                        System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(tempPath + folderTree + fileName);

                        System.Reflection.AssemblyFileVersionAttribute fileVersionAttribute = (System.Reflection.AssemblyFileVersionAttribute)assembly.GetCustomAttributes(typeof(System.Reflection.AssemblyFileVersionAttribute), true)[0];

                        string versionMajor = fileVersionAttribute.Version.Split('.')[0];
                        string versionMinor = fileVersionAttribute.Version.Split('.')[1];
                        string versionBuild = fileVersionAttribute.Version.Split('.')[2];
                        string versionRevision = fileVersionAttribute.Version.Split('.')[3];

                        string updateString = string.Format(" UPDATE Module_File SET FileVersionMajor = {0}, FileVersionMinor = {1}, FileVersionBuild = {2}, FileVersionRevision = {3} WHERE FileGuid = '{4}' ",
                            versionMajor,
                            versionMinor,
                            versionBuild,
                            versionRevision,
                            reader["FileGuid"]);

                        updateStrings.Add(updateString);

                        Type[] types = assembly.GetExportedTypes();

                        foreach (Type type in types)
                        {
                            Type commandInterface = type.GetInterface("Genesis.Client.Common.Interfaces.ICommand");

                            if (commandInterface != null)
                            {
                                Genesis.Common.Attributes.CommandCodeAttribute commandCodeAttribute = (Genesis.Common.Attributes.CommandCodeAttribute)type.GetCustomAttributes(typeof(Genesis.Common.Attributes.CommandCodeAttribute), true)[0];
                                Genesis.Common.Attributes.CommandDescriptionAttribute commandDescriptionAttribute = (Genesis.Common.Attributes.CommandDescriptionAttribute)type.GetCustomAttributes(typeof(Genesis.Common.Attributes.CommandDescriptionAttribute), true)[0];
                                Genesis.Common.Attributes.CommandNameAttribute commandNameAttribute = (Genesis.Common.Attributes.CommandNameAttribute)type.GetCustomAttributes(typeof(Genesis.Common.Attributes.CommandNameAttribute), true)[0];

                                string commandString = string.Format(" IF NOT EXISTS(SELECT CommandGuid FROM Module_Command WHERE CommandType = '{0}') " +
                                    " BEGIN " +
                                    " INSERT INTO Module_Command (FileGuid, CommandName, CommandCode, CommandDescription, CommandType) VALUES ('{1}', '{2}', '{3}', '{4}', '{5}') " +
                                    " END " +
                                    " ELSE BEGIN " +
                                    " UPDATE Module_Command SET CommandName = '{2}', CommandCode = '{3}', CommandDescription = '{4}' WHERE CommandType = '{0}'" +
                                    " END ", type.FullName, reader["FileGuid"], commandNameAttribute.CommandName, commandCodeAttribute.CommandCode, commandDescriptionAttribute.CommandDescription, type.FullName);

                                commandStrings.Add(commandString);

                                Response.Write("<br/><font size=\"-2\" color=\"seagreen\">Processed " + type.FullName + "</font>");
                            }
                        }

                        Response.Write(string.Format("<br/><font color=\"steelblue\"><strong>[{0}.{1}.{2}.{3}]</strong></font>", versionMajor, versionMinor, versionBuild, versionRevision));
                    }
                    else
                    {
                        System.IO.File.Copy(developmentPath + filePath + fileName, localPath + folderTree + fileName, true);
                        System.IO.File.Copy(developmentPath + filePath + fileName, tempPath + folderTree + fileName, true);
                    }
                }
                catch (System.Exception exception)
                {
                    Response.Write(string.Format("<br/><font color=\"red\">Error: {0}</font>", exception.Message));
                    failed = true;
                }

                Response.Write("</td><td align=\"right\" valign=\"top\">");

                if (!failed)
                {
                    Response.Write("<font color=\"green\">Ok</font>");
                }
                else
                {
                    Response.Write("<font color=\"red\">Failed</font>");
                }

                Response.Write("</td></tr>");

                Response.Flush();

                recordCounter++;
            }

            Response.Write("</table>");

            reader.Close();

            foreach (string updateString in updateStrings)
            {
                command = new SqlCommand(updateString, connection);

                command.ExecuteNonQuery();
            }

            foreach (string commandString in commandStrings)
            {
                command = new SqlCommand(commandString, connection);

                command.ExecuteNonQuery();
            }

            connection.Close();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
We Fix Code
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions