Archive for the ‘OSS’ Category
NHibernate + Mono + MySQL Fly by
I thought about it and I want to see it works for me and there it goes ;)
- Ubuntu Karmic
- Mono 2.4.3
- NHibernate (trunk)
- MySql connector-net (trunk)

rebuild the stack for new home ;)
Sweet :) next rediscover Castle stack on Mono that should be fun let see how much I stress MonoDevelop 2.2
Gdo, Gazetteer Database Online
There are 3/4 variants of the Cambodia Gazetteer after UNTAC created the list; now it time to put this mess to rest one and for all.
Ministry of Interior, Department of Local Administration currently setup a task force to reconsolidates the master list and make it an only office Gazetteer.
Now my colleague expected to write up a forward document about this project; which I’m not going to discuss here.
What you should know in the near future there is only one set of Cambodia Gazetteer and you can access it online absolutely free, so stay tune …
And you don’t have to guess how I built or when it goes live since I had 3 projects developed & maintenance in parallel no doubt I no longer had time to enjoy playing JXII :(
Feature Highlight:
- Online browsing Province, District, Commune, Village meta data
- Province, District, Commune Map (produced by external consultant)
- Download Gazetteer data in Excel format for offline use.
I love this job ;)
PHP date diff
I call out for an excuse for not practicing PHP enough recent year; with just a simple date_diff could knock me out :) not to open my complain of having different version of PHP to check before using a function(s) I got a request from a friend to help workout how many days from two given date.
Now if I have two date string (it is what we get from form post right?)
// fixed format as I don't want to deal with complicate of different world format :(
$day1 = "26/03/2009";
$day2 = "28/03/2009";
// now I need to know how many days does it take from day1 to day2
// PHP 4.x; ignore my out of date PHP skill this is how I did it
$break_day1_apart = explode('/', $day1);
$break_day2_apart = explode('/', $day2);
$day1_number_form = $break_day1_apart[2].$break_day1_apart[1].$break_day1_apart[0];
$day2_number_form = $break_day2_apart[2].$break_day2_apart[1].$break_day2_apart[0];
$day_diff = abs((int)$day2_number_form - (int)$day1_number_form);
// and it should work by theory
echo $day_diff;
Suck ! what would you do instead?
Raising the Dark Army
I never regret to step my feet into programming world back in 1997. My first experience using notepad to script out (HTML) website. I don’t remember how many times I skew up my brother’s computer just to figure out a thing no matter how hard he try to avoid me.
Rewind for the last 10 years I keep pushing myself up step by step no matter how hard, stressful I got I never give up. 10 years? Yeah I know my brain deserved a major upgrade perhap replacement of RAM, CPU Squad Core or something :) But see it took me so long to get to this stage where I legendary programmers abroad take much shorter time to advance their skills.
You may compare my learning experience to a single pig raising in a big farm, the lack of physical interaction, challenge and guiding is the main source preventing it from fast growth. But that are all history today I am so happy, because I am no fall into those mind set and I am not alone, ladies & gentlemen the dark army is rising.
This is the 5th session that they all come together and learn .NET programming C# and I glad to become their instructor as much as I enjoy walking them through the learning curve. Although it is a programming course we don’t talk much about syntax sugar or debate about how it was designed because it doesn’t give much value in real world once it’s time to get down to business so what they need to learn are concepts and tool set for rapid application development.
I want to see some new face as well but it doesn’t seem to work out just yet :) but again we please to invite you to join our journey anytime just keep you eyes on our public calendar.
Let the spirit of sharing growth among Cambodian, let us all create an opportunity for our young generation to extend what we had discovered.
Castle Routing Module
In previous post I was talking about the beauty of MR Controller which I also mention about Routing feature of Castle MonoRail Framework. Today I am going to walk you through basic steps to configure environement, define and register MR Routing.
Step 1: Start with web.config by adding the following line into system.web\httpModules section
<add name="routing" type="Castle.MonoRail.Framework.Routing.RoutingModuleEx, Castle.MonoRail.Framework" />
Step 2: Define routing rules, base on Hammett suggestion I am going to create a static class for handling this task
// filename: RoutingRules.cs
using Castle.MonoRail.Framework.Routing;
public static class RoutingRules
{
public static void Register(IRoutingRuleContainer rules)
{
rules.Add(new PatternRoute("<controller>/[action]")
.DefaultForArea().IsEmpty
.DefaultForAction().Is("index"));
// you can add as many rules as you want
}
}
Step 3: Register rules, this have to be done once and the best place for that is in Global.asax using its’ Application_Startup event
public class Global : HttpApplication
{
public override void Application_Start(object sender, System.EventArgs e)
{
base.Application_Start(sender, e);
RoutingRules.Register(RoutingModuleEx.Engine);
}
}
And that about it. Oh by the way if you are like me. I am a big fan of UrlHelper class and I’m addicted to this macro:
// generate url /product/list<.extension>
${Url.For({@controller: 'product', @action: 'list'})}
And if you do not want to gernerate url with extension /product/list then you have to take another step by adding the following line to monorail configuration section in the web.config
Step 4: Remove extension from generated url
<monorail defaultUrlExtension="">
<!-- other configuration removed -->
<url useExtensions="false"/>
<!-- other configuration removed -->
</monorail>
Now do you have any specific routing rules wanted to discuss? It’s time to dig in ;)
Ken Egozi also had an interesting post related to this topic.
jQuery ajaxform generate on the fly
What scripting language would come to your mind first when you begin to talk about client-side scripting (web)? Don’t tell me it is VBScript or I’ll LOL, and so your answer would be? “JavaScript” alright +1 from me :). I believe most of the programmer who is reading this post knowing/aware of jQuery.
I can extend the talk about jQuery into a series of posts but I am not gonna do that because google would give you millions of result about jQuery and it usage.
Today what I am interested to talk about is using jQuery ajax functionality in conjunction with Castle.MonoRail to generate HTML Form on the fly and the form will also has ajax functionality. What does it mean? well let us all get into application context of adding new customer. In general we are going to have a customer page /customers which list all existing customers in a table format. Additional customer can be added by clicking on Add New Customer link wired up with a javascript function.
<a href='#' onclick='addNewCustomer()'>Add New Customer</a>
and the addNewCustomer() function looks like this:
<script language="javascript">
function addNewCustomer(){
$.ajax({
dataType: 'script',
url: '/customers/addNewCustomer'
});
}
</script>
From the above script the web browser going to send off a request to /customers/addNewCustomer which is going to handle by an action of CustomersController. If you care to see the action code it look like this:
[Layout("default"), Rescue("generalerror")]
public class CustomerController: SmartDispatcherController
{
.... code removed for clarity
public void AddNewCustomer(){
PropertyBag["c"] = new Customer();
}
.... code removed for clarity
}
and the rest of the work will be left over to the view engine to find the view template file and process it. Since I am a big fan of Brail View Engine the template file would be call AddNewCustomer.brail and the code look like this:
<form id='form1' method='post' action='/customer/create'>
Name: ${Form.TextField("c.Name")}
Phone: ${Form.TextField("c.Phone")}
<input type='Submit' value='Create New' />
</form>
<script type='text/javascript'>
$(document).ready(function(){
$('#form1').ajaxForm( { dataType: 'script' } );
});
</script>
Again it just simple HTML markup mixed with a few Brail’s tagging macro but take a closer look at the javascript code at bottom, what does it do? Well the idea is once the template is processed it going to generate a pure HTML form and send it back to the client side, however the javascript that send along will ensure that the new generated HTML form will perform a post back via jQuery ajax functionality. Hmm, sound cool. But didn’t I just waste a bytes of data with this approach? I tell you what, this application is going to run on a LAN so bandwidth is not a problem.
Up to this point everything works perfect, except for one final click on the Submit button to save new customer and oop! look there is a post without ajax It just work as normal as any other HTML form submission. Hmm, what I had discovered was by generating form id=’something’ or whatever as long as it is not id=’form1′. The submission of the form will route through ajax weir isn’t it? I haven’t investigate into jQuery code just yet jQuery might use form1 for something else internally, do you know what it is?
UPDATE 2008-11-25:
I believe my first assumption about form id attribute which cause ajax not to fire was wrong. What I recently found was if I am not calling $(document).ready(…); in the original page the generated dom pass in even if it contain $(document).ready(…) script block that block just do nothing.
StackOverFlow Gentlements, & Ladies
I hang around in StackOverFlow.com recently and I wanted to wait long enough so that I can describe how the experience like G do I ever told you I hate sleeping? but it seem like Jeff Atwood also doesn’t sleep & blogging; so why don’t we just read what he got to say about his toy shall we?
REST, Routing & the beauty of MR Controller
What is REST?
REST stands for Representational State Trasfer an important concept in REST is the existence of resources (sources of specific information), each of which is referenced with a global identifier (e.g., a URI in HTTP).
How does it work?
REST take advantage of HTTP methods such as POST, GET, PUT and DELETE and translate the request as CREATE, READ, UPDATE, DELETE (CRUD) operation associated with database technologies.
| HTTP | CRUD |
|---|---|
| POST | Create, Update, Delete |
| GET | Read |
| PUT | Create, Overwrite/Replace |
| DELETE | Delete |
REST application might define resources URI such as:
http://example.com/albums
http://example.com/albums/{id}
http://example.com/albums/{id}/slideshow
and client code would look something like this:
Resource userResource = new Resource('http://example.com/users/001');
userResource.delete();
It is simple enough, right?
RESTful Castle
REST needed URI to begin with; now if you are assigned to design a server side code for that what are you gonna do? Well, you simple need an URL Rewrite tools to save day, don’t you? So what does URL Rewrite tools do? Simply it would intercept incoming HTTP request and route to relevant destination (.php, .cgi, .aspx, .asmx scripts etc). It is pretty trivial to do it on apache web server:
- Make sure mod_urlrewrite is enabled
- define a few rules in .htaccess (hmm, regex not so funny)
[ControllerDetails(Area="admin")]
public class UsersController : Controller
{
public void Index()
{
}
}
Notice the use of ControllerDetails attribute which usually allow you to tell MonoRail how a controller can be found. The important part of ControllerDetails above is Area = “admin” which is very flexible and that mean you can burried a controller down as deep as you want it to be.
Is that mean I can have a controller defined and it will generate URI like this?
http://example.com/albums
http://example.com/albums/{id}
http://example.com/albums/{id}/slideshow
With controller alone? you basically can’t do, because controller generated URI in the form /area/controller/action.rails?id={id} to do a nice RESTful like URI you need another tool similar to URL Rewrite lucky enough MR got a build-in tool for that it located here Castle.MonoRail.Framework.Routing … going to talk more in the future.
Limitation & Workaround
* Public Access
http://example.com/albums
http://example.com/albums/{id}
http://example.com/albums/{id}/slideshow
* Authorization Access
http://example.com/users/albums
http://example.com/users/albums/{id}
http://example.com/users/albums/{id}/slideshow
Look at the screenshot bellow:

and you say; hell YEAH that neat right; now compile and run the test site. Navigate to /album/index.rails work unfortunately navigate to /users/albums/index.rails will cause MR to throw an exception say it can’t find Area & Controller specified.
What the hell ??? the truth is MR register a controller using class name as a key I believe it run from the top namespace first which mean Basic.Web.Controllers.AlbumController got register first so when it walk into Basic.Web.Controllers.Users.AlbumController it just skip that controller.
To work around this problem against thank to ControllerDetailsAttribute which allow us to name a controller free from Controller class. How are you going to do that? well rename /users/AlbumController.cs to /users/WhatTheF_CKAlbumController.cs and boom it done.
[ControllerDetails("album", Area="users")]
public class WhatTheF_CKAlbumController{
... same code old code
}
Get lost? probably :) but don’t worry I am going to give a slow version in my C# lecturing session and if you are interested to walkin and listen up just subscribe to my google calendar.
Until then have a good weekend.
Credit where credit is due
It is the great honor to read a story behind the man behind Cambodian Open Source Software movement. I really appreciate all the hard work that the people at NiDA especially Mr. Noy Shoung had done to put Cambodian inline with the rest of the world.
Noy is the Deputy Secretary General (In Charge of Human Capacity Building and Free/Open Source Software) at National ICT Development Authority (NiDA). Cool title to have. And, one that is hard earned. Noy’s built up a team inside NiDA to localize open source desktop apps into Khmer (a language too small to be interesting to Microsoft), build up open source development skills amongst young people (still early days on this one) and train end users on Linux, Open Office and Firefox (20,000 people and counting). He’s also the major champion behind Khmer OS, a localized OpenSuse distribution.
full article here don’t forget my dear friends cheer him up with your comments.




