Work Involved
Part 1 – Original Webnote
Before starting to create the same site, I needed to learn how it worked. I started by printing out all the code on paper and commenting what it did. Since I was not familiar with Python, this took a lot more work than expected. Eventually, I stopped commenting on paper and started commenting in the python files themselves. I felt that many of the python syntax were not self explanatory such as:
os.environ['SCRIPT_NAME'][:14] == '/webnote-devel':
I do not believe some of the python functionality are even available in other languages. For example, the following line of code assigns values to three variables at the same time.
(wsid, nextnotenum, time) = self.getInfo()
In order to understand some of the functionality, I decided to install the website so I can view the whole process to see how it worked. It turned out easy at first. I installed python, mysql, and placed all the files in a directory. The site appeared to look as if it worked but when I started clicking on buttons, everything fell apart.
Notes were creating fine because it was handled by javascript but the page wouldn't save. This was handled by the backend save.py file. I narrowed down the error to a specific spot in the javascript file that listens to the response of the save.py file. I had trouble understanding it the so I emailed the author. He gave me suggestions and I was able to finally get the response from save.py to work.
The problem was in the indexing of a substring. Which I corrected from:
if ("\nok\n" == xmlhttp.responseText.substring(0,4))
to:
if ("ok" == xmlhttp.responseText.substring(2,4))
This was extremely odd since the files were untouched and seemed like it should work as is. After the error finally disappeared, I tested the saving functionality again. There were no error, but if I refresh the page, the notes disappeared. It was still not saving. It was frustrating since the functionality worked behind the scenes and I didn't know what it was doing. That was when I learned about the logging feature. The author told me there is a log method that you can insert anywhere to output any string to a text file. I logged everything! Every sql command, every xml, every variable. This helped tremendously because now I was able to see what the pages were trying to do against files or database. I copied the sql commands from the logs and ran them against the database which worked fine. I had a test python page run the command against the database and print out its values. No luck. I believe the problem was in the mysql database driver (MySQLdb). After a few more emails to the author, I gave up. The pages just couldn't communicate with the database at all and I wasting a lot of time in just getting the site to work. With at least some understanding, I decided to start recoding.
Part 2 – Conversion to .Net/C#
Using the techniques I learned in software dev, I tackled the most important functionality or “use cases” first. Since I had so much trouble getting the database to work with the original site, I started with it. I immediately ran into problems. Prosise book says we can use either a SqlClient or a OleDbClient. Neither worked and knowing Microsoft, I wasn't sure it would provide anything to support a competitor's database. After a lot of searching on Google, I found MyODBC which is a MySQL ODBC Driver. Also, I would have to use the ODBCClient which was not mentioned in the book. Since I've had so much trouble with databases before I decided to create a document with sample code, resources, and headings so I can use them for future reference. After installing the driver and some tweaks, it worked like a charm.
The next thing I tackled was logging. It helped me tremendously with the original site and I knew it would help me while I recreate the new one. Searching through the I/O section of the Prosise book, I found what I needed. It didn't at first. I had a lot of trouble with permissions and security. After some research, the solution was to turn off Simple File Sharing and add the user “ASP.NET Machine Account”. This was a little confusing cause now I'm not sure what the user “ASP.NET Web Admin” is. I tested the database again but this time had all queries logged to the text file.
Now I need to “design” my site. The reason I actually need to re-design it is because from what I saw, python was not a true web programming language. It prints out html character by character. Therefore, it would be foolish of me if I just copied the functions directly and not take advantage of the .Net capabilities. The python files used a very object-orient approach which is great from a programmer perspective I found it to be difficult to replicate in .Net. The “note” class was a class that contained all data of a specific note. Therefore, a workspace would have many note objects. So now I had to figure out how my workspace page would instantiate note objects. If I was programming in Java, this would be a piece of cake: Note n = new Note(). The note class just had to be a class and nothing more. I couldn't create it as a normal .Net page, note.aspx, because I would not be able to instantiate it. Again, I turned to the Prosise book and found user controls. Conceptually, it was exactly what I needed. To be able to instantiate a new object whenever I liked. I implemented the class with user controls but ran into a HUGE problem; user controls are instantiated in the aspx page. I needed to instantiate it in C# so I can manipulate it using its methods and properties.
Now I tried custom controls which was a little better since the class is in a .cs file but I ran into the same issues, the control is instantiated in some aspx page. After a lot of searching and with no results, I tried something. My main problem was this: the custom control class was exactly what I need but I wanted to instantiate it in the C# code. So I did just that, I instantiated the class, note.cs, and it worked. I was able to call its functions and everything. Perhaps it was just some simple thing that I was not aware of. But in any case, it worked.
To integrate the note class functionality with the database, I copied over fake data from the original database to the new. I was able to load a workspace, then retrieve all the notes from the database for that workspace, and insert each data into their own note object. I was able to obtain the same object oriented approach of the original site.
The original workspace file had a method called createHTML(). This method creates the html page by printing out the code character by character. This I knew asp.net would be able to handle easily, instead of print each character one by one, I just had to have the code in the aspx part of the page so when the page loads, it already has an html page where as python has to manually create one. That is where I came to the conclusion that python is not meant to display html.
Now I ran into something interesting. I realized what the site was doing was this: load a workspace, query notes from the database, create a note object for each note and stores its data, then have each note print out javascript onto the workspace to create the div layers. From an object-oriented approach, this was great but it was also redundant. If we're going to store the data into the note object and then have it print it all out again then why bother. The relationship between a workspace and its notes are very clearly defined in the database using primary keys. For each note in the database, it is assigned one workspace and one workspace only. Therefore, I really didn't need to use the note class to separate out each note. So I scraped it. Although it meant I lost all the work I put into the note class, it also meant my code is now cleaner and easy to read. The site would now work like this: load a workspace, query notes from the database, and for each note (or row) just print the values out into javascript. It was the same functionality with less code and less confusion. The user would not be able to tell the difference nor did I have to make any changes to the database.
Next, I tacked saving. Ideally, it should be simple; each note object should print out all its information and then we store it to the database. Note: in the saving case, the note object is not the same as the note object I explained earlier. The note object I'm referring to is in the frontend, the javascript. The javascript has two major classes; workspace and note. As you can see, it uses the same names as the backend and the functionality of the classes are about the same. Workspaces hold notes and notes hold each note data (except now I removed the note class from the backend).
The save functionality works as follows, when you click on the save button on the workspace, it triggers a method in the javascript. This method calls a toString method that prints out the whole workspace including its notes as a xml string. It then opens a XMLHTTP connection to a save file that accepts the input. My job was to create that save file. The first step was perhaps the hardest of the whole project; reading the input. Asp.net has built in classes that can handle XMLHTTP request easily! But after trying to implement it, I realized there was huge problem, the server (in this case, my save file) needed to be called by a specific method. That means that the client (in this case, the javascript file) needed to also be asp.net and required to make specific calls. I scrapped it immediately, the situation I was looking for is a javascript file making XMLHTTP request to a asp.net file. I had no luck with google. I kept receiving the same results: .net's version of handling callbacks. What I need now was a very manual way of reading the input stream. In python, it was written as follows:
size = os.environ['CONTENT_LENGTH']
data = sys.stdin.read(int(size)).replace('\0', '')
I tried to search for a c# equivalent of the code above but failed. I couldn't even find a site that explained what the code above actually did. I just knew because after I logged the variable ‘data' from above, it turned out to be a long xml string. After about 4 days of failure, I stumbled across the answer. The article wasn't even written about the subject, it was something entirely different but the syntax of the code sounded like what I was looking for. The code was:
HttpContext.Current.Request.InputStream
It worked perfectly. It was exactly what I was looking for; the input stream of httpcontext. I'm not sure what ‘current' and ‘request' are and after looking it up on msdn, I still don't. I still couldn't find an article that clearly explains the details (msdn didn't help much).
After I was able to read the xml string, I need to parse it. Once again, Prosise book. At first, I ran into a lot trouble reading the xml because I was using XmlNode, but once I found XmlElement, it solved all my problems. XmlElement includes MUCH more functionality that XmlNode could even imagine. For example, the method GetAttribute() is able to give you any value of the node and you only have to know the name.
root.GetAttribute( "name" );
If I was to work with XmlNode which I did at first, I would have to deal with XmlAttributeCollection and have to read one attribute of a node at a time. This confusing because now the variable looks like an array and I would not know what attribute I'm looking at. For example, if name is the first attribute then attributeCollection[0] would return the name. Whereas GetAttribute(“name”) is much more clear.
At this point, I contructed a large insert query using all the parsed xml information. I ran it against the database and verified that the loading process would be able to retrieve it properly.
After implementing the main functionality, I wanted to improve it. The two biggest feature that I wanted was that I wanted it to be private and I wanted to allow a person to have more than one workspace and make it easily accessible.
In order to make it private, I needed to add a login. I tried to use the login control from visual studio but it didn't work the way I wanted. It created an access database to stores all the users it was important for me to access these users so I can assign workspaces to them. Therefore, I made my own. I decided not to use passwords at first because if I can get just the login to work first, then adding a password would be simple. I added to two tables to the database, one to store the user name and give him an id and the second to store the relationship between a user and his workspaces. This would allow two users to have the same exact workspace name without any confusion or mixing of notes across workspaces. Everytime a user creates a new workspace, the table just gets updated with that specific user and that new workspace he created.
With this feature implemented, I realized a user cannot even see what he created. If a user created ten workspaces, he would have to remember the name of every single workspace. That was where the ‘welcome' page and workspace menu came from. After a user logs in, he presented with a welcome page that allows him to create and select a workspace that is currently his. The code just looks up all the workspaces with his userid and then prints out each name and creates a link. The workspace menu was added because it I wanted a user to be able to access his other workspaces without signing out to the welcome page everytime. At the same time, I didn't want the menu to take up precious real estate. My goal was to make it dynamic, where if you click on some image icon, all the workspaces would magically appear and if you click it again, it would disappear. So I went out to look for one. After some searching, I find this site: http://www.4thorder.us/Scripts/Scripts/JS.DHTMLMenuExpander/Version2/menu_examples.htm
It was what I was looking for but I couldn't get it to work like in the examples. The menu was not dynamically moving so I just removed the javascript files from the page and kept the styles sheet for visual purposes. I changed the code inside the table to output the users workspaces and added a ‘sign out' button so he can leave the site.
At this point, I needed to clean up the code. The same methods was sitting across multiple files such as the log(). Regular Asp has the ability to use server side includes which would allow me to place code in a separate file and then have all the other files point to it and use it. The first method I found was using user controls but like the same problem I ran into above, it gets instantiated in the aspx file and not in the aspx.cs. A few days ago I found an article on inheritance and perhaps it would allow me to achieve the results I needed but I was not able to find time to try it out.
For presentation purposes, I know I needed to somewhat address the front-end, the javascript. The original javascript file was extremely difficult to read containing about 1,800 lines of code. My goal was to break it down into simple steps such as dragging, double-clicking, etc. Unfortunately, as soon as I started, I found it to be just as difficult as the backend if not more. I knew I wanted to at least do dragging since it was already mentioned in class and it would have been of interest to everybody. So I went and searched for the most simple exercise on dragging that I could and came across two tutorials below. I found it to be not simple enough and removed an excess functions or variables and quickly became easy to present. The second tutorial, I did not have time to use and should probably be deleted or placed into some list of resources.
http://4umi.com/web/javascript/draggable.htm
http://www.codelifter.com/main/javascript/dragablelayer.html