Velocity if statement is type sensitive…

Problem

I discovered that comparisons in Velocity if statements are type-sensitive the hard way.

For example, I am trying to display the 'selected' option in a html <SELECT> object. The list of possible values is $list, each is a simple 'NameValuePair' value object { String name, String value }.

The object I am editing in this form is $obj, and the field I am testing against is $obj.typeId defined as a long.

CODE:
  1. <select name='typeId' value='$!{obj.typeId}'>
  2. #foreach ( $item in $list )
  3.   #if ( $item.value == $obj.typeId )
  4.     <option value='$item.value' selected='true'>$item.name</option>
  5.   #else
  6.     <option value='$item.value'>$item.name</option>
  7.   #end
  8. #end
  9. </select>

However, the if statement on line 3 is actually comparing a String to a long which are of course never going to be equal.

Solution

The easiest work around in this case was to convert the $obj.typeId into a String before the comparison.

CODE:
  1. <select name='typeId' value='$!{obj.typeId}'>
  2. #set ( $typeId = "$!{obj.typeId}" )
  3. #foreach ( $item in $list )
  4.   #if ( $item.value == $typeId )
  5.     <option value='$item.value' selected='true'>$item.name</option>
  6.   #else
  7.     <option value='$item.value'>$item.name</option>
  8.   #end
  9. #end
  10. </select>

Any comments / feedback welcomed ;)

Time Synchronisation With Windows 2000 or XP

If you're running Windows 2000 or XP at home, it's very unlikley that you have a 'Primary Domain Controller' to give you the time of day. ;)

Windows 2000 & XP has a built in "Windows Time" service that is also compatible with public SNTP Time Servers, all you need to do is configure it.

I chose a local SNTP server from the list: http://support.microsoft.com/kb/262680

CODE:
  1. Microsoft Windows 2000 [Version 5.00.2195]
  2. (C) Copyright 1985-2000 Microsoft Corp.
  3.  
  4. C:\>net time /querysntp
  5. This computer is not currently configured to use a specific SNTP server.
  6.  
  7. The command completed successfully.
  8.  
  9.  
  10. C:\>net time /setsntp:ntp0.uk.uu.net
  11. The command completed successfully.
  12.  
  13.  
  14. C:\>net time /querysntp
  15. The current SNTP value is: ntp0.uk.uu.net
  16.  
  17. The command completed successfully.
  18.  
  19.  
  20. C:\>net stop w32time
  21. The Windows Time service is stopping.
  22. The Windows Time service was stopped successfully.
  23.  
  24.  
  25. C:\>net start w32time
  26. The Windows Time service is starting.
  27. The Windows Time service was started successfully.
  28.  
  29.  
  30. C:\>

Done! Immediately after the last line "net start w32time", I noticed my clock jumping forward a few minutes. You can see messages in the "Event Viewer" from W32Time, for example:

CODE:
  1. Event Type: Information
  2. Event Source:   w32time
  3. Event Category: None
  4. Event ID:   593
  5. Date:      26/07/2005
  6. Time:      11:40:05
  7. User:      N/A
  8. Computer:   LOCALHOST
  9. Description: Time service corrected the clock error by 220 seconds