Tuesday, May 20, 2014

I Spy Bad Code – Change Screen.Cursor

Recently, I have seen following code appeared again and again in one of application written in Delphi:

begin

      …
  Screen.Cursor := crHourGlass;
  try
   …

  except
    Screen.Cursor := crDefault;
    …
  end; {try}
  Screen.Cursor := crDefault;
end;

This code works in most of the time, but it does have one issue, it makes an assumption that the cursor was crDefault thus it change the cursor back to default before the end. What if the cursor is not default before entering this code block? We can improve the code as following thus it will restore the cursor to what it is before this code block:

begin

       …

      LSavedCursor  := Screen.Cursor;
  Screen.Cursor := crHourGlass;
  try
    …

  except   
    Screen.Cursor := LSavedCursor;
    …
  end; {try}
  Screen.Cursor := LSavedCursor;
end;

Or more proper way (in my opinion) is to use a try … finally block to protect the code block and it will guarantee the cursor will be restored:

begin

      …

      LSavedCursor := Screen.Cursor;
  Screen.Cursor := crHourGlass;
  try
    try
      …  //doing busy business code insert here

    except
      …
    end; {try}

  finally
    Screen.Cursor := LSavedCursor;

  end;
end;

Can it be improved further? The answer is YES and NO. If you are still using Delphi earlier than Delphi 2009, then the answer is NO, because we are going to use Anonymous Method which was introduced by Delphi 2009 and later version of Delphi.

With above code, you actually need to define the local variable LSaveCursor in each method in which it has above code, so it is a bit trivial, and you cannot refactoring above code to a method because the actually doing busy business code is vary from method to method, refactoring each doing busy business code to a method and then pass the method’s point will be a over kill approach.  But with anonymous method, we don’t need to refactoring doing busy business code to a method, instead, we can pass the code as an anonymous method.

First we need to create a procedure to take care the logic for changing the cursor to hour glass and restore it when it completes. Below is the procedure:

procedure BusyDoing (AProc:TProc);
var
  LSavedCursor : TCursor;
begin
  LSavedCursor := Screen.Cursor;
  Screen.Cursor := crHourGlass;
  try
    AProc;
  finally
    Screen.Cursor := LSavedCursor;
  end;
end;

This procedure has one parameter AProc which is a pre-defined anonymous method which is defined in unit System.SysUtils as

TProc = reference to procedure;

To use this procedure, all we need to do is enclose the doing busy business code as an anonymous method like this:

BusyDoing(
    procedure
      begin
           … //doing busy business code insert here

      end
);

That is it, very neat, isn’t it?


 

Sunday, March 16, 2014

Paint.Net 4.0 Beta is available

Just in case you didn't know, Paint.Net is a free image and photo editing software for PCs that run Windows. It written in .Net and has features similar to those big guys such as Photo Shop, Paint Shop and The GIMP.
Now its version 4.0 beta is available for download. There is a feature in 4.0 which I like the most is “Copy Merged”. What it does is copy from all layers as one image, which is equivalent to what you have to achieve
by many steps in v3.5:
  1. Flatten the layers
  2. Copy all
  3. Undo the flattern

Thursday, March 6, 2014

My First Orchard CMS Website

http://www.orientart.com.au is my first Orchard website. It is built on Orchard 1.7.2.
I have inherited TheThemeMachine as the theme. I have restyled the menu bar so it
is not so techy. I have created a slider module (based on bxslider) for showing the
featured products. I use taxonomies for product category, Amba.ImagePowerTools
for product picture. I do not use Map module for showing Google Map, instead I use
HTML widget.

Wednesday, January 29, 2014

NHibernate Journey (1) – “show_sql”

I am following Your first NHibernate based application to start my NHibernate journey.

In the article, it uses NUnit for test, but I have to use MSTest as I am using VS Express 2013 in which NUnit GUI cannot be installed.

I have set “show_sql” to true in the hibernate.cfg.xml when first time test Add method of product repository and there was not

SQL statement outputted. Until I was running “session.Get<Product>” then there was SELECT statement appearing in the output of the

test. According to the article, there suppose to be INSERT statement appearing in the output as well when adding new product to the

database. What is wrong?

Tuesday, October 29, 2013

Kentico: Running API Examples from within CMS Desk

If API Examples is installed, the examples can be run from Support –> API examples.

Many of the examples have been hard coded with culture “en-us”, they will be most likely failed if the site is using a culture different from “en-us”.

The example code is like below, it fails on the red line:

#region "API examples - Creating documents"

   /// <summary>
   /// Creates the initial document strucutre used for the example. Called when the "Create document structure" button is pressed.
   /// </summary>
   private bool CreateDocumentStructure()
   {
       // Add a new culture to the current site
       CultureInfo culture = CultureInfoProvider.GetCultureInfo("de-de");
       CultureSiteInfoProvider.AddCultureToSite(culture.CultureID, CMSContext.CurrentSiteID);

       // Create new instance of the Tree provider
       TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

       // Get parent node
      TreeNode parentNode = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/", "en-us");

       if (parentNode != null)
       {
           // Create the API Example folder
           TreeNode newNode = TreeNode.New("CMS.Folder", tree);

           newNode.DocumentName = "API Example";
           newNode.DocumentCulture = "en-us";

           newNode.Insert(parentNode);

           parentNode = newNode;

           // Create the Source folder - the document to be moved will be stored here
           newNode = TreeNode.New("CMS.Folder", tree);

           newNode.DocumentName = "Source";
           newNode.DocumentCulture = "en-us";

           newNode.Insert(parentNode);

           // Create the Target folder - a document will be moved here
           newNode = TreeNode.New("CMS.Folder", tree);

           newNode.DocumentName = "Target";
           newNode.DocumentCulture = "en-us";

           newNode.Insert(parentNode);

           return true;
       }

       return false;
   }

You can modify the culture to your site’s culture and the example should be able to run on your site.

Tuesday, September 3, 2013

Tips for upgrading Kentico CMS site from v6 to v7

Recently I upgraded one web site written in Kentico CMS v6 to v7 and would like to share my experience.

Preparation

Due to the some API changes in v7, you might need to change your code a lot if your web site is mainly using ASPX template.
To track your changes, it is better if the changes can be recorded down and can be revert any time if it is needed. This is where
source control comes in place. If you have not apply source control to your web site yet, this is the good opportunity to start
using source control.

Here are the suggestions for using SVN as the source control for this upgrading purpose:

  1. create a new repository
  2. create trunk and branches in the repository
  3. check out the trunk to a local folder
  4. copy all files and folders from your web site folder into trunk folder
  5. add all files and folders and commit to SVN repository
  6. create a branch into branch folder based on the trunk

The idea behind this is to record all changes to the branch during the upgrading. Depends on the complexity of the web site,
the changes you will make to the source code might take few days/weeks, so that I do not recommend that upgrade the
production directly, instead, you should make a copy to your development machine and upgrade this copy first. All the changes
made to the local instance will be committed into the new created branch. When the upgrade is done and has been test,
production then can be upgraded. The upgrade to the production will be as simple as following steps:

  1. copy files from the production server into trunk.
  2. commit all the modifications if there is any.
  3. merge changes from the branch
  4. copy file back to production from the trunk

Upgrade

The database upgrade script might contains changes to the views. But I have found that in Micorsoft SQL 2008 R2, after execution
of the upgrade script, the views might not be synchronized properly. In my case the error was some data conversion failures.
The root cause seemed to be the changes on base views had not propagated properly to other views which were base on the base
views.

If you need to perform the upgrade manually, it requires to delete a lot files and folders before updating files to new version’s.
The instruction has listed the files and folders, but it is very fussy if you go through the list and delete file/folder one by one.
A easier way is to copy the content of the list to a text file, then add “del “ to the beginning of the full name of the file and add
”del /s/q “ to the beginning of the full name of the path, save the text file as a .BAT file, this batch file can delete files and folders
in one go and save you a lot of time.

There is an undocumented change you need to take care if your page uses CMSForm for editing a document in the same way as
the Form tab in the CMS Desk content page, you need to assign DocumentManager.LocalMessagesPlaceHolder to the the CMSForm
element’s MessagesPlaceHolderto property, otherwise, there will be an exception when the form is submitted.

Tuesday, August 20, 2013

My Weapons (Tool set)

  1. 7-Zip.
  2. Adobe Reader
  3. EditPad and Notepad++: Advanced text editor.
  4. Fiddler Web Debugger: http/https traffic inspector.
  5. firstobject XML Editor: Formatting XML and browsing nodes in a tree view.
  6. Google Chrome: HTML/Javascript/CSS inspector/debugger.
  7. IDE:
    1. Delphi:
      components:
      1. CnPack IDE Wizards
      2. madshi’s madCollection
    2. Microsoft Visual Studio
  8. Kentico CMS.
  9. Link Shell Extension: Managing file’s hard link.
  10. Microsoft Network Monitor.
  11. Nullsoft Install System
  12. Paint.Net: Graphic tool.
  13. Pdf995.
  14. soapUI.
  15. Synergy: one set of mouse and keyboard for multiple PC.
  16. TeamViewer
  17. VisualSVN Server, TortoiseSVN and WinMerge
  18. WinDirStat: shows disk usage in colour blocks
  19. Windows Live Writer: Writing blogs.
    Plugins:
    1. Live Writer Code Prettify Plugin