Put your message here! Contact me for more information
 
 







 

It’s been a long time since my previous post. There was a lot that has happened (and not happened). I switched my job from being a consultant at BridgePoint Technologies, a small consulting firm in Oak Brook, to being a lead developer at Designkitchen.com. I love the team at BridgePoint, I learned and grew a lot during my time there, and of course I always remembered to have a good time wherever I go to. I had a blast, and I still keep regular contacts with friends and colleagues there. The new position at Designkitchen is also awesome in the sense that it offers new opportunities, new challenges, and a new environment. And I find life is funny in its own way. BridgePoint’s office is right across the street from the McDonald’s HQ. I used to walked across the street to lunch with friends to the yellow McDonalds restaurant near by, which impressively has a timer to tell how fast each customer is served. The average was about less than 40 seconds. Joining us were also McDonalds corporate employees grabbing a quick bite. Was it one more reason to get back to work faster to climb the corporate ladder? Anyhow, I’m currently the lead developer for ClubBK.com, a Burger King’s effort to get their presence on the so-cool cyberspace for kids. Never did I feel more like having burgers for lunch everyday. Nonetheless, I’m having a blast working side by side with the different teams at Designkitchen. Everyday is a learning experience.

I picked up a Canon Digital Rebel XSI with a 50mm f/1.4 prime lense. I knew nothing about photography, and now is always a good time to start picking up a new hobby. My grandfather is really good photographer, and I don’t want to see him disappointing that his grand kid is being ignorant of the art. I’m not. I just never had the budget ready for such a plunge. But I’m glad I did it. The XSI is an excellent camera — light, compact, good build, and cheap. Coupled with the fast f/1.4 lense, I’ve had some good (or lucky) shots of people at really low light conditions. Everybody was impressed with the photos, without knowing about my newbie status.

I also got a new Mac Book Air with 64GB SSD for rails and iPhone apps development. Designkitchen is a heavy Mac environment. Almost 100% of the creative team is on Mac (clubBK.com is 100% mac, that’s for sure), and most Rails guys I know are also on Mac. Not that I’m being influenced by peer-pressure, but definitely that counts for something. I’ve been using the Air for a straight 2 days, leaving my Dell D830 in the backpack. Heck, I don’t need a backpack for the Air! Performance wise, the machine is zippy and Rails 2.3.0 boots in merely seconds. I managed to consume more than half the 64GB with the iPhone SDK and OS X updates, but I still have plenty of room left. I have all my media on the main desktop, and for music, Last.FM and Pandora provide some awesome head-banging time when I’m not home (try some Disarmonia Mundi and you’ll know what I mean). In other words, I’m getting macimized while staying anti-vista.

I’m saving the best for last, but I’m finally free. 2.10, the day to remember. I’m so ready to have my life back. Time to make some changes and live life a bit, as I’ve been in the shadow for way too long.

view comments
 

I don’t like to get political on my blog, but a friend of mine sent me a link to a political quiz at ABCNews.

http://abcnews.go.com/Politics/MatchoMatic/fullpage?id=5542139

Here are my answers:


mccain-obama

I don’t disagree with some of the things that Obama is saying, but I strongly agree with McCain’s principles and leadership. And as a foreign immigrant to this country, I’m strongly offended by Obama’s way of dealing with the issue:

Obama: “We should require them to pay a fine, learn English, and go to the back of the line for citizenship behind those who came here legally. But we cannot — and should not — deport 12 million people.” (number #8)

Who the hell is Obama to say “let’s fine these illegal immigrants because they just sneaked in the country?” JERK, JERK, and JERK. How about babies that were brought to the States by their parents, granted “illegally” according to US’s immigration laws. Do these babies do anything wrong to get fined? They speak English, pay taxes, go to school, and contribute to society as much as anyone else — while getting treated as a 2nd-class citizen.

McCain has a much clearer policy and it is towards an legalizing the integration of the immigrants (currently there’s no such thing!)

“The program will … ensure that all undocumented aliens either leave of follow the path to legal residence. American cannot permit a permanent category of individuals that do not have recognized status — a permanent second class”

McCain, I wish you the best of luck!

view comments
 

For a Rails/SQLServer application I’m working on, I had to deal with pagination with custom queries because of the different joins. The mislav-will_paginate plugin works great for MySQL, but for SQL Server, the paginated query generated by the current SQL Server Adapter (I’m using activerecord-sqlserver-adapter-1.0.0.9250) does not work very well. The current implementation is targetted really for SQL Server 2000 and older versions since these versions do not have support for ROW_NUMBER() method. It is a major pain in the butt to do pagination with these databases. With the newer SQL Sever 2005, the job is a bit easier. Microsoft implemented the ROW_NUMBER() method with a convoluted syntax to have better support for pagination, but it is still a drag because of the weird syntax.

Semergence wrote in his blog about patching the SQLServerAdapter to support pagination. Based on his post, I improved ActiveRecord::ConnectionAdapters::SQLServerAdapter::add_limit_offset! to make the query work in a more general way with free-form queries, e.g. queries ran with the paginate_by_sql() method provided by mislav-will_paginate

Include this script in your environment.rb file, or an external file and “require” the file within environment.rb.

  # monkey-patching SQLServerAdapter to support SQL Server 2005-style pagination
  module ActiveRecord
    module ConnectionAdapters
      class SQLServerAdapter
        def add_limit_offset!(sql, options)
          puts sql
          options[:offset] ||= 0
          options_limit = options[:limit] ? "TOP #{options[:limit]}" : ""
          options[:order] ||= if order_by = sql.match(/ORDER BY(.*$)/i)
                                order_by[1]
                              else
                                sql.match('FROM (.+?)\b')[1] + '.id'
                              end
          sql.sub!(/ORDER BY.*$/i, '')
          sql.sub!(/SELECT/i, "SELECT #{options_limit} * FROM ( SELECT ROW_NUMBER() OVER( ORDER BY #{options[:order] } ) AS row_num, ")
          sql << ") AS t WHERE row_num > #{options[:offset]}”
          puts sql
          sql
        end
      end
    end
  end

The method above monkey-patches the SQLServerAdapter by overwriting the add_limit_offset! method.

Here’s a custom query that I used and the transformed result:

Resource.paginate_by_sql([
      %!SELECT  resources.*
        	,skills_count.skill_count
        FROM resources
        	,(
        		SELECT resource_id
        			, COUNT(*) AS skill_count
        		FROM resource_skills
            WHERE meta_skill_id IN (1,2,3,4,5,6,7,8,9,10)
        		GROUP BY resource_id
        	) AS skills_count
        WHERE resources.is_active = ?
          AND resources.id = skills_count.resource_id
        ORDER BY skill_count DESC
      !, true ], :page => page, :per_page => per_page

With :page = 1, :per_page = 2, the resulted SQL is:

SELECT TOP 2 * FROM ( SELECT ROW_NUMBER() OVER( ORDER BY skill_count DESC ) AS row_num, resources.*
 	,skills_count.skill_count
 FROM resources
 	,(
 		SELECT resource_id
 			, COUNT(*) AS skill_count
 		FROM resource_skills
 WHERE meta_skill_id IN (1,2,3,4,5,6,7,8,9,10)
 		GROUP BY resource_id
 	) AS skills_count
 WHERE resources.is_active = 1
 AND resources.id = skills_count.resource_id

 ) AS t WHERE row_num > 0

The will_pagination’s COUNT query is

SELECT COUNT(*) FROM (
 SELECT resources.*
 	,skills_count.skill_count
 FROM resources
 	,(
 		SELECT resource_id
 			, COUNT(*) AS skill_count
 		FROM resource_skills
 WHERE meta_skill_id IN (21,22)
 		GROUP BY resource_id
 	) AS skills_count
 WHERE resources.is_active = 1
 AND resources.id = skills_count.resource_id
 ) AS count_table

The ORDER BY part is automatically removed from the main query (which becomes a sub-select) by the plugin to speed up the query. This in turns sanatizes the sql so that SQL Server doesn’t not complain about nested “ORDER BY” within a sub-select. Neat!

The only catch with the current add_limit_offset! is that it does not support ALIAS-ing, because the aliasing confuses the reqex to parse out the ORDER BY condition in the OVER() part of the query.

For regular find() queries, here’s a sample result

Resource.find(:first)
# original query:  SELECT * FROM resources
# transformed:   SELECT TOP 1 * FROM ( SELECT ROW_NUMBER() OVER( ORDER BY resources.id ) AS row_number, * FROM resources ) AS t WHERE row_num > 0

Hope this helps and cheers!

view comments
 

Here is a quick guide to configure log4net for general asp.net 2.0 and asp.net 3.5 web projects. While you can find general guides for setting up log4net, more advanced configuration will require further digging through Log4Net documentations. My intention is to provide a quick, 5 minutes overview of configuring log4net for a web project with a more complicated, nested logging configuration.

My goal is to have 2 independent loggers working in parallel. One logs application errors (such as exceptions or serious errors that need to be reviewed later) to the Event log of Windows, and the other will log all general messages to a text file. The reason why 2 loggers are needed is because the Windows server is configured to send out email notifications on “Error” events to notify whoever in charge of maintaining the application. The general logging file containing all logging messages will provide more application-level debugging info.

Installing and Setting up Log4Net
Log4Net is a drop-in DLL file that you can download from the Apache project site. The binary is compatible with ASP.NET 3.5 and ASP.NET 2.0, as well as the Mono platform. After adding the reference to the DLL to your project, create the Global.asax file and add one line to the Application_Start method to initialize log4net at application start up.

void Application_Start(object sender, EventArgs e)
{
  log4net.Config.XmlConfigurator.Configure();
}

In the web.config file, add the log4net section handler to the configSections block



  
    

Basically we have 2 “appender” loggers and their above configurations are quite self-explanatory. The first appender, EventLogAppender, has a level range filter, which will filter out only FATAL or ERROR log entry. Then it has 2 mappings to map the different log levels to the correct Window’s Event message type. Finally, the layout section defines how the log text should look like.

For the 2nd logger, we use a RollingFile appender, which will automatically rotate the files based on the log file size. We define where to log the files to, what the maximum size is, and how many files we would like to keep. We don’t need to configure a filter for this logger since we are collecting all entries for debugging purposes, but you can add a range filter or a pattern filter without any problem.

Finally, we define the root logger. This root-level logger is the one logger receiving our log message and it will distribute these messages to all of the configured referenced appenders.

To use log4net in the program, all you need to do is add “using log4net;” to the top of the file, and initilize a static logger as follow:

public class PageBase : System.Web.UI.Page
{
  public static readonly ILog log = LogManager.GetLogger("Logger");
  public PageBase()
  {
     log.Debug("This is a debug message");
     log.Info("This is an info message");
     log.Error("This is an error message");
  }
}

Notes

  • Log4net is meant to be un-obtrusive and it won’t throw any error whatsoever, even in the case of invalid configuration. This means that you won’t know why it fails if log4net happens to fail. As the manual says, log4net is not meant to be a reliable logging mechanism. So watch out there
  • For the file appender, if you log into a sub-folder within the web project, you should take extra precaution steps to protect the one folder from public eyes. For example, denying all anonymous users from accessing the “log” folder by adding the following section into the web.config

  
    
      
        
      
    
  

References

view comments
 

I randomly ran into Steven Levithan’s blog while searching to get an idea of how JavaScript handles Unicode. Steven is a JavaScript - Unicode and Regular Expression expert. He has a cool section called “Code Challenge” with some good food-for-thoughts challenges. It’s really JavaScript being pushed to the max, in terms of brevity, creativity, and obscurity. Check out Stephen’s “Roman Numeral Convert” challenge for example.

Reading through the comments, I picked out a nugget explaining a JavaScript behavior which actually caused me some unexpected issues with TubeCaption’s Captionizer. Steven explained best in his original comment

… you might have already realized this, but the unary + operator and parseInt are not equivalent. + can convert strings to numbers, and returns NaN if the element cannot be converted. parseInt (which takes an optional second argument for the radix) does the same thing, but also extracts leading numbers from strings. E.g., parseInt(”12x”) returns 12, while +”12x” returns NaN. Additionally, parseInt and + make different assumptions about the radix when there’s a leading zero. +”012″ returns 12, but parseInt(”012″) returns 10. The leading zero causes parseInt to treat it as an octal number in probably all browsers, despite octals being summarily deprecated in ES3. Of course, you can use parseInt(”012″,10) to get around that.

Here is a quick demo of how parseInt() behaves.

For the SRT import feature of TubeCaption’s Captionizer, I heavily relied on parseInt() to get the different time values. I was caught by surprise when a user notified me that his SRT file could not be imported into the timeline. After some debugging, it turned out that some values had padding values and the parseInt() returned incorrect results in octal instead of decimal. I wish I had known about the “+” trick and the subtlety of JavaScript at the time.

view comments