Open Source Journey

Thursday, February 25, 2010

C# 3.0 Implicit Type Local Var

If you have been reading the other blogs, you probably pause when you read the title of this blog. C#?! Why would an Open Source blog write and discuss anything to do with the at one time the biggest "opponent" of Open Source after all. These days, the official press release and the quite hefty check that Microsoft gave Sun, indicate that Microsoft has, whether this is true or not, come to term with the fact that Open Source is here to stay. In fact, Microsoft decides to become the primary sponsor of one of the plethora of Open Source conferences. Sadly, I forget which one though.

For many, especially the clients and organizations in this 21st century, this is indeed a welcoming sign. While each software vendor always tries to stake as much in every contract and purchase, the reality for many including my case in the recent project, we have to deal with technologies that come from both camps anyway. On site, I have to use the Microsoft SharePoint (MOSS 2003) for example as the key for the Change Management process. Yes, with $53-Million at stakes, we need a better way to collaborate amongst ourselves and with key stakeholders beyond email exchanges.

We choose the Oracle/PeopleSoft to manage the Student Life Cycle with Oracle 10g backend, BEA WebLogic App Server and Apache Web Server. As the Security Architect, I am gladly using the Java-binding and through the PeopleSoft Component Interface, I do have the options of creating bulk User Profiles/Accounts.

But, for the SharePoint, User Profiles and other information are tightly integrated to our LDAP infrastructure. And when it was time for me to "bulk" create the User Profiles, I welcome the option of using the C# and the XML Web Services. I remembered that I did not have a lot of time, only 1 day I believed, as I was about to fly overseas and there were requests to add hundreds more of new accounts.

After digging through some articles on the MSDN website, I came up with a nifty way to use C# and WSDL, to "talk" to the SharePoint "User Profile" Web services and create each Profile along with the appropriate Role in a matter of minutes. By 5:00 PM that day, I was done with that work, and more than ever ready for the flight to see my family and relatives 12000 miles away in Bali.

The bottom line is that very often in our life, we do not have the luxury to pick sides. I am a firm believer in Open Source. But as the story illustrated above could tell, in many cases, I have to deal with the other ecosystems as well, Oracle, PeopleSoft or Microsoft. Failures to do so would have unpleasant consequences especially comes the performance review.

After working with Java since 1999, I find the transition to C#, well, rather pleasant. The learning curve is not as steep as I remembered making the Perl journey in 2002.

For today, I would like to go over one of the "new" feature that is available in C# 3.0, which is the Implicit Type Local Var.

Here is one code example with the Bali flair illustrating the Implicit Type Local Var:
using System;
using System.Collections.Generic;

namespace Balisoft.App.CSharp30
{
class Program {
static void Main(string[] args)
{
List subdistrics = new List();
subdistrics.Add("Baturti");
subdistrics.Add("Penebel");
subdistrics.Add("Pupuan");
subdistrics.Add("Marga");
subdistrics.Add("Selemadeg");
subdistrics.Add("Kerambitan");
subdistrics.Add("Tabanan");
subdistrics.Add("Selemadeg Barat");
subdistrics.Add("Selemadeg Timur");

foreach (var a in subdistrics) {
Console.WriteLine(a);
}
}
}
}

Here is the nice formatted version of the program file:



Notice in particular the following code segment:

foreach (var a in subdistrics) {
Console.WriteLine(a);
}


That is it for now....I would discuss the other features of the C# 3.0 in the future.






Wednesday, February 24, 2010

Maven 2, Hibernate & Oracle 10g

For those who follow the official Hibernate tutorial and wish to change the Database from the HSQLDB to Oracle 10g, then looks further no-more.

Here is a snapshot of my local directory:



Install the Oracle JDBC driver to your local Maven 2 repository.... with the following command

mvn install:install-file -Dfile=ojdbc14.jar -DgroupId=com.oracle.jdbc -DartifactId=ojdbc14 -Dversion=10.2.0.1.0 -Dpackaging=jar -DgeneratePom=true


Note that you need to have the ojdbc14.jar in the current directory where you issue the mvn install:install-file command or you can specify the path.

The next step would be to include the new Oracle JDBC in your pom.xml....by adding the following




And the last part, will be to update the Hibernate properties/configuration file, in this case, it is the hibernate.cfg.xml......





And then run a couple of DB updates, then you can see the results for yourself....





Maven 2


After using Ant as the primary build tools for several years, I decided to give Maven a try. Ant has been a great tool. As a software lead, I had used it in a Rail Risk Assessment project with a sizable federal grants. The winning point for Ant was at the time, I could incorporate automatic Unit and Integration tests into the build process....something that was rather alien to the group that hired me.

Whenever I have questions on Ant, I seek the vast Google resources or perhaps even the personal connection. I also happen to know the author of the authoritative Oreilly book on Ant, Erik H. His children and mine happen to go the same private elementary school here in town.

Now that I am in Maven land for several years, I could not believe that I have not jumped into the bandwagon earlier. I remembered my maiden Java journey in 1999 taking the class and compiling all the source codes through the command-line. Boy, they were painful. Managing the classpath to all the required JAR(s) is not for the faint of heart.

And then came, the JBuilder, and then IBM, the heavyweight, chips in with Eclipse. And voila, suddenly, IDE makes those pains go away and Java development could be fun again.

Of course, with every story, there is always the ups and downs. While Eclipse has been the IDE of choice, it comes with its own problems. Go to the official website, then you are presented with a plethora of choices. What frameworks and languages do you want to work with?! J2SE, J2EE?! C++?! or perhaps even PHP or Ruby?!

After that, you need to make sure that you have the right plug-ins so that you can move on with your development goal. For the J2EE, you most likely need the plug-ins to Tomcat, Jetty web-server, JBoss AS and various DB connectors.

Now, at the beginning of the Oracle/PeopleSoft project in 2005, I begin to use the UNIX utilities quite extensively from the BASH script to the Perl and Ruby. And, I realize that command-line is not something to shy away from. With that newfound rigor, I started to wonder whether there is something new in the Java-world that alleviate those pains that I remember earlier in my Java journey.

One thing led to the next, I come across Maven 2 and never even look back. Yes, as with any tools, there is a learning curve. There are certain conventions that you need to be familiar with and to follow to use Maven to its potential. And yes, you would make mistakes. But, trust me, those are the mistakes worth learning from.


Tuesday, February 16, 2010

ExtJS and JSON in concert


I have to rely on Javascript to assist me in my work as Security and Portal Architect for quite a while now. Back then before I came across frameworks such as WATIR/Ruby, Javascript has been the tool of choice along with Perl to deliver the critical Security deliverables end-to-end.

So, it is no surprise that nowadays I stay hectic by working with and catching up with what the leading Javascript frameworks are all about. For today, I am going to focus on ExtJS Grid Panel, Column Model and how with JSON, it enables the interactive display of Server-side data.

Requirements

1) Web Server--I am using XAMPP for my development server and I am going to rely on PHP to supply the data that the ExtJS datastore needs
2) ExtJS --download it from the official www.extjs.com website, in this case I am using the 3.0 version
3) text editor - Eclipse IDE but Emacs or Vim would be just fine

Data
The data would be coming in the JSON format from the Server-side. If you are new to JSON, you can do a bit exploration with what JSON is all about. But, if you are familiar with XML, then you should have no problems with the following example:

in authors.php create the following function ==>

function GetAuthors() {
$authors = "{'authors':[";
$authors .= "{'id':'1', 'title':'Walden','author':'Thoreau','descr':'Walden is the garden of Eden from the author perspective','type':'book'},";
$authors .= "{'id':'2', 'title':'Ramayana','author':'Rsi Walmiki','descr':'The greatest story about divine faith and resolute devotion','type':'book'},";
$authors .= "{'id':'3', 'title':'Mahabarata', 'author':'Rsi Vyasa','descr':'one of the two major Sanskrit epic of India','type':'book'}";
$authors .= "]}";
return $authors;
}

echo GetAuthors();
?>




Notice that each "Author", or perhaps more suitably each "Book" entry, has the following attributes: id, title, author, descr, and type.

Those attributes are the ones that you have to work with and specify in the ExtJS JSONStore.

Web Page

The Javascript section of the Web page will contain the following code snippets:






Pay attention to the section where the JSON Datastore points to the authors.php, the Server-side script that we talked about earlier

var store = new Ext.data.JsonStore({
url: 'authors.php',
root: 'authors',
fields: ['id', 'title','author','descr','type']
});


And now for the result in action ==>





Thursday, February 4, 2010

Ruby on Rails

I stumbled upon Ruby especially WATIR, Web Application Testing in Ruby, in my current job. Along with Perl, Ruby has been a God send. As the Principal Security resource, I am responsible for developing, maintaining, and managing Application Security for 40,000+ end users and 1000+ back-office staffs with 10 online presences.

The Security requirements have been captured and defined in the MS Excel Security matrix, and it is my job to "execute" those requirements correctly so that Security is correct and Federal laws such as HIPAA and FERPAA are also complied....that means specifically that if one has "Viewer" only access, then he or she can only see the last 4-digits SSN for example.

I will go through that experience in more details in my next blogs but for now...I want to take you to ride the wonderful journey with Rails, the popular Web frameworks, and in the spirit of Superbowl 2010, develop a Web-based NFL team app using Ruby on Rails.

With backgrounds also heavy in Java, I count my blessings and am very excited with the JRuby on Rails.
With JRuby, you have the ability to incorporate the existing Java libraries and code-base that you have, but losing the native (C-compiled) Ruby libraries, also called Ruby gems.

So without further ado,....

Install JRuby

wget http://dist.codehaus.org/jruby/1.2.0/jruby-bin-1.2.0.tar.gz
tar xzvf jruby-bin*
mv jruby-1.2.0 /cygdrive/c/jruby
echo "export PATH="/cygdrive/c/jruby/bin:$PATH"" >> ~/.bashrc
export PATH="/cygdrive/c/jruby/bin:$PATH"
Install Rails
jruby -S gem install rails

Now let us create our application by firing up rails....




Now, since we are working with JRuby, we need to modify the database backend by working with JDBC

So go and edit the config/database.yml file and change the adapter from sqlite3 to jdbcsqlite3 as follows:


In our application, each NFL team would have many players, whereas a player belongs to a team.


Next, use the scaffold facility that is available for both the team and the player model/entity.





Notice that for each singular entity, (team or player), the scaffold creates the plurals version.
team => table teams, teams_controller
player => table players, players_controller

You can verify this DRY, Do Not Repeat, principle by looking at the db/schema.rb


establishing the Associations between the Team and the Player entity is done as follows
in the model itself








create the DB tables by issuing the command

jruby -S rake db:migrate



What I often have to do is to populate those tables in the DB...You can fire up the Webrick or Mongrel Web server that Rails comes with and enter the data manually, after all the CRUD facility is there through scaffolding, or another option is to use the test fixtures





players.yml

allred:
number: 56
name: Allred, Colin
position: LB
team_id: 1

amano:
number: 54
name: Amano, Eugene
position: G/C
team_id: 1

amato:
number: 58
name: Amato, Ken
position: LB/LS
team_id: 1

bakhtiari:
number: 99
name: Bakhtiari, Eric
position: DE
team_id: 1

ball:
number: 98
name: Ball, David
position: DE
team_id: 1

bironas:
number: 2
name: Bironas, Rob
position: K
team_id: 1

britt:
number: 18
name: Britt, Kenny
position: WR
team_id: 1

brown:
number: 79
name: Brown, Kareem
position: DL
team_id: 1

brown:
number: 97
name: Brown, Tony
position: DT
team_id: 1

bulluck:
number: 53
name: Bulluck, Keith
position: OLB
team_id: 1

collins:
number: 5
name: Collins, Kerry
position: QB
team_id: 1

cook:
number: 89
name: Cook, Jared
position: TE
team_id: 1

teams.yml

titans:
id: 1
name: Titans
city: Nashville
sport: football

raiders:
id: 2
name: Raiders
city: Oakland
sport: football

Load the fixtures into the DB through the following command

jruby -S rake db:fixtures:load

Fires up rails and starts riding the Rails experience ................







Catalyst-MVC

This is a continuation of the previous blog on the Perl DBIx
class....in essence this is to document my journey particularly
on the Object Relational Mapping (ORM) subject.

I am actually going further than simply exploring a
simple model for a bookshop ala Amazon or BarnesAndNoble.com,
but would take it further on how to use the Model in the
spirit of the Web development particularly how we can
incorporate the model with the venerable Perl Catalyst MVC (Model
View Controller) framework.

Catalyst MVC
One thing that draws me to the world of Perl for the past 7+ years is
the premise that "There is more than one way to do it".
I find that there are both beauty and dangers in that premise.

For industry that replete with the term of "best practices", that premise
would run contrary to the mantra. As for me, best practices is a journey
and never cast in stone. As for any journey, one has to begin with a single
step and would make lots of mistakes. For some mistakes are something to frown upon.

For me personally, mistakes are part of life. They are the springboard to get better,
to be more concise, more elegant, and more productive.
I find that beauty in Perl, Python and quite recently Ruby (for the past 3+ years anyway).

But I digress....if you want to conquer anything, software development or otherwise,
you need the right tools. For Web and Web 2.0 development, you would most likely
encounter the issues of CRUD (Create, Read, Update, and Delete).

and if you are thinking about leveraging Perl for your Web development needs, then looks no
further than Catalyst MVC.


Step 1

Fire up catalyst .....I name my app as mybookshop






let us start by defining and working with the Model straight from SQL to the DBIx Class Schema


The Model


Pretty simple and straightforward. Now we are going to use SQLite3
as the backend DB.

if you save the above DDL as books.sql then use the following command
to generate the SQLite3 DB:

cat books.sql | sqlite3 books.db

and use the DBIx Class Loader make_schema_at utilities to come up with
schema for the Books, Authors, and Categories:






here is the script that I have used in the command line to come up with
those schemas above

perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir=. -e 'make_schema_at("MyBookShop::Schema", {debug => 1}, ["dbi:SQLite:dbname:books.db","",""])'

If you would like further information, you can consult the CPAN page directly


The DBIC Schema would look like as follows:




The Controller

Let us focus our attentions with the Books entity right now. Let us say that
we are going to display the books selection through the URL like this:

http://localhost:3000/books
http://localhost:3000/books/list

In order to do that, we need to create a Books controller through the command line:

perl script/mybookshop_create.pl controller Books
with the results of that the Books.pm (Perl module) is added under the Controller
directory.




The Books.pm, the Perl Module file, is where you are going to define and all the actions relating to the Books model..

Going back the list of URL(s) specified above, we want to use the "localhost/books/list" to display our books selections. In Catalyst parlance, we have to define a "list" method in order to accomplish the objective....and one example will be detailed as such:



Let us spend some time going over the code................

sub list : Local {
# Retrieve the usual Perl OO '$self' for this object. $c is the Catalyst
# 'Context' that's used to 'glue together' the various components
# that make up the application
my ($self, $c) = @_;
# Retrieve all of the book records as book model objects and store in the
# stash where they can be accessed by the TT template
my $page = $c->req->param('page') || 1;
my $rows = $c->req->param('rows') || 3;
my $where = { };
my $attr = { page => $page, rows => $rows };
my $rs = $c->model('DBIC::Books')->search($where, $attr);
my $pager = $rs->pager();

$c->stash->{pager} = $pager;
$c->stash->{books} = [$rs->all];
# But, for now, use this code until we create the model later
#$c->stash->{books} = '';
# Set the TT template to use. You will almost always want to do this
# in your action methods (action methods respond to user input in
# your controllers).
$c->stash->{template} = 'books/list.tt2';
}
The var. $c is for the Catalyst Controller, whereas the $c->req is the "HTTP" Request object.
We are going to incorporate pagination in our example, thus...we assume unless defined that
we are starting with page 1 and each page would only retrieve 3 items/results.

my $page = $c->req->param('page') || 1;
my $rows = $c->req->param('rows') || 3;
my $where = { };
my $attr = { page => $page, rows => $rows };
my $rs = $c->model('DBIC::Books')->search($where, $attr);
my $pager = $rs->pager();
We have to make the results available for the View, which is the next topic inline, by "saving" our resultsets in through stash

$c->stash->{pager} = $pager;
$c->stash->{books} = [$rs->all];


The View

Following along from the last section, the View comes into play in the following code section:

# Set the TT template to use. You will almost always want to do this
# in your action methods (action methods respond to user input in
# your controllers).
$c->stash->{template} = 'books/list.tt2';

The TT in this case stands for the Template Toolkit, which is one of the "View" frameworks that are popular within the Catalyst MVC.



Remember our "books" model that we save in the Controller module, as in the following code:

$c->stash->{books} = [$rs->all];

The "books" resultsets are manipulated in the View by the following code in the list.tt2 module

[% # Display each book in a table row %]
[% FOREACH book IN books -%]
[% book.name %]
[% book.rating %]
[% book.fk_author_id.name %]
[% END -%]
In case you are wondering where you specify the Template Toolkit files...they are usually under app/root/src directory...in my case it is specifically under mybookshop/root/src



Once everything is in place, you can explore more by starting the Catalyst server








So...play around...experiment...make mistakes...and get better :)...Remember...there is more than one way to do it :)

Let me know if you have any comments or questions....I will be more than happy to help in anyway I could.

Monday, January 25, 2010

Mason on XAMPP

I have seen some questions online on how to integrate Mason with the XAMPP package

It took a while to figure things out but finally I found out the missing pieces from several online sources notably the modperlbook...

As you know, the XAMPP package comes with a very basic Perl packages installed....

so to add Mason into the XAMPP Perl installation follow this

1) I have both installed ActiveState Perl & Cygwin Perl installed on my Win XP machine

most of the time Cygwin CPAN upgrade packages is not working well with me so I have to resort to using the ActiveState Perl


so from the MS DOS prompt fire up ActiveState perl and from the Modperl book
you need to update the PERLINC environment var:




2) fire up the ActiveState Perl package manager PPM




3) add trouchelle Perl repositories http://trouchelle.com/perl/ppmrepview.pl

4) then add the Mason package

5) modify the XAMPP Perl configuration file as follow:




6) the above configuration indicates that I want Mason to work
on any html files served from the masontest directory under htdocs

so without further ado...fire upi XAMPP and start watching out for those perky
errors in the logs



7) once you get rid of any errors..then navigate to the masontest URL and see
Mason in action











Followers