Archive for March, 2006

MySQL 5.0 Stored Procedures

MySQL 5.0 comes with some really cool additions like stored procedures, functions and views.

I was experimenting with writing a procedure at work and, this is what i came up with.

DELIMITER $$;
 
 DROP PROCEDURE IF EXISTS `vms_database`.`FixSafety`$$
 
 CREATE PROCEDURE `FixSafety`()
 BEGIN
     DECLARE id, b int ;   
     DECLARE serial_no1 varchar(255) ;
     DECLARE track_id varchar(255) ;   
     DECLARE cur_1 CURSOR FOR SELECT MAX(SAFETY_VALVE_REPAIR_ID) AS id , SERIAL_NO , SAFETY_VALVE_EQUIPMENT_TRACK_ID AS track_id  FROM safety_valve_repair WHERE SERIAL_NO IS NOT NULL AND trim(SERIAL_NO)!=‘’ GROUP BY SERIAL_NO;
   
     DECLARE CONTINUE HANDLER FOR NOT FOUND
     SET b = 1 ;   
     OPEN cur_1 ;   
     REPEAT
         FETCH cur_1 INTO id, serial_no1, track_id ;       
         UPDATE safety_valve_repair SET SAFETY_VALVE_EQUIPMENT_TRACK_ID=id,MOST_RECENT_FLAG=‘N’ WHERE SERIAL_NO = serial_no1 AND SAFETY_VALVE_REPAIR_ID!=id ;
         UPDATE safety_valve_repair SET MOST_RECENT_FLAG=‘Y’ WHERE SAFETY_VALVE_REPAIR_ID = id ;
         UNTIL b = 1
     END REPEAT ;
     CLOSE cur_1 ;
 END$$
 
 DELIMITER ;$$

The procedure that i wrote was 2-3 times faster, than a piece of java code that, i wrote to perform the same operation.
Eventhough i still don’t like much of how procedures are written in MySQL 5.0, its still a good start.

JBoss Seam (EJB3+JSF+WORKFLOW)

Ok you wanted a sneak peak into EJB3? find a way to integrate JSF with EJB3?

Well here comes JBoss Seam it has everything u need to get started with EJB3 and also to find out how cool JSF can be when integrated with EJB3. I came across some nice features about Seam when i visited the Linux Asia 2006 conference held in New Delhi, India. I met Subramaniam Satyamoorthy (Director of Operations and Services (Asia Pacific)) , quite a funny guy :) . He showed us some of the workings of Seam, that got me hooked into it. And i am still experimenting with a lot of interesting stuff in it, not to mention JBoss jBPM which i had no idea about untill i knew about JBoss Seam. EJB3 uses Hibernate for persistance so you got best of both worlds :p. Maybe after a while i might  just post some code for download regrading Seam. Eventhough Seam is still in beta its still a handfull.  So check it out at http://www.jboss.com/products/seam 

and oh btw, you need to either select "ejb3 cluster" or "ejb3" profile while installing Jboss Application Server .

Integrating Codesnippet and Editormonkey plugin for Wordpress 2.x

I just tried out one of the best wordpress plugins out there the Codesnippet and Editormonkey , since for people like me who like to paste code everywhere they can :p and also since wordpress is not so kind to us :( , i had to go search for a plugin that would make my life a bit easier. Codesnippet is a great plugin to get syntax highlighting done for your code. Similarly Editormonkey replaces the default WYSIWYG editor in Wordpress 2.x with a much more advanced WYSIWYG editor with choices to either use TinyMCE or FCKEditor. Well i tried them both and was very impressed but when i started using the both together, came across some gliches Codesnippet stopped working since Editormonkey would change much of my code into HTML with "<" and ">" etc converted to  &lt; and &gt; and i could no longer use "code" blocks with "lang" attributes. So i tried hacking into it and came up with a few fixes for Codesnippet to work with Editormonkey. So if you are one of those who likes to use both you can download the modified "Codesnippet.php" from here.

You might also want to change the css for "Codesnippet" under Options->Code Highligting to something like this

border:1px solid #ccc; background:#eee; padding: 5px;margin:10px; font-size: xx-small; overflow:auto

 

looks much nicer in firefox.

Don’t forget to visit Codesnippet’s page at http://blog.enargi.com/codesnippet/   and Editormonkey at http://www.rajprasad.net/plugins/editormonkey
really great plugins….

Update: looks like the codesnippet site is down but u can download codesnippet from here

This is not the exact release, its just a tar of the files in my plugins directory . I have put it up just coz there is no download available elsewhere. 

Cheers!

Single Sign-On with CAS (Central Authentication Service)

First things first,

What is Single Sign-On?
: Wikipdedia has this definition for it.

CAS (Central Authentication System): Its a project started by Yale university to provide a trusted way for an application to authenticate a user (ref: The JA-SIG CAS Project)

I came across CAS when i was searching for a Single Sing-On framework for a project, i did find another similar project Java Open Single Sign-On Project (www.josso.org). And the reason why i choose CAS is its Proven track record. CAS has been successfully implemented in a lot of projects not to mention Yale University has been using it for a long time now (Ok you could have guessed that :p).

When i downloaded the source and deployed the web application that came with it, i thought the java cas-client would work just out of the box. Well it would have, if i had a valid server certificate for my tomcat server, compared to my self-signed certifcate to make tomcat work with SSL. Wasn’t too long until i realized what was causing the problem the class edu.yale.its.tp.cas.util.SecureURL (inside the Java Cas Client) that was downloaded from http://www.ja-sig.org/products/cas/client/javaclient/index.html which didn’t accept my self-signed certificate from the server. So i just went back and changed it :) Everything after that went super smooth, not to mention trying to use CAS everywhere i could :p

I played around with it a bit and installed a simple LDAP Authentication handler for the CAS Server application, this is an extract from the file WEB-INF/deployerConfigContext.xml

<bean id=“ldapAuth” class=“org.tecnova.cas.authentication.handler.support.LDAPAuthenticationHandler”>
     <property name=“providerUrl”>
         <value>ldap://localhost:389</value>
     </property>
 
     <property name=“credentialQueryString”>
         <value>cn=#username#,ou=Employees,dc=tecnova,dc=com</value>
     </property>
 
 </bean>
 …
 …
 …
 
 <property name=“authenticationHandlers”>
     <list>
         <bean class = “org.jasig.cas.authentication.handler.support.
 HttpBasedServiceCredentialsAuthenticationHandler”
  />

         …
         …
         <ref local=“ldapAuth” />
     </list>
 </property>

and to enable CAS authentication on the client application all we need are the following entries in the WEB-INF/web.xml of the Client web application.

<web-app>
 …
 …
     <filter>
         <filter-name>CAS Filter</filter-name>
         <filter-class>edu.yale.its.tp.cas.client.filter.CASFilter</filter-class>
       
         <init-param>
             <param-name>edu.yale.its.tp.cas.client.filter.loginUrl</param-name>
             <param-value>https://localhost/cas/login</param-value>
         </init-param>
 
         <init-param>
             <param-name>edu.yale.its.tp.cas.client.filter.validateUrl</param-name>
             <param-value>https://localhost/cas/proxyValidate</param-value>
         </init-param>
 
         <init-param>
             <param-name>edu.yale.its.tp.cas.client.filter.serverName</param-name>
             <param-value>localhost</param-value>
         </init-param>
 
     </filter>
 
     <filter-mapping>
         <filter-name>CAS Filter</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
 …
 …
 </web-app>

Remember my tomcat is set to listen on port 80 and has SSL enabled, CAS won’t work without SSL enabled on your app server.

For rest of the details on what I did I have attached a sample Server and Client web application. Which can be downloaded here.

Using Apache Axis to create Web Services

Apache axis, is one of the best way to deploy Web Services without needing to buy those expensive Application Servers out there. Axis is deployed as a simple web application, with web services deployed as either simple .jws files or more complex compiled classes. Web Services can either be coded as simple java classes and later renamed to .jws rather than the usual .java, when invoked via axis using a url like http://localhost:8080/axis/services/MyService.jws, Axis compiles the code and exposes it as a SOAP Web Service. The other way of deploying a web service is by compiling the code and writing something called as the "Web Service Deployment Descriptor (WSDD)" WSDD files describe a Web Service,ex: the actual class and the methods that have been exposed in the Web Service, the scope of the Web Service etc. A sample axis Web Application which contains both a service (Calc) and a Client for the Web Service (Calc.jsp) can be found here.

Using Jasper Reports with Hibernate and Teamwork

Jasper Reports is the perfect open source reporting tool for the masses, heres a code snippet to generate the "Operator by Worklog" sample report using Jasper Report in Teamwork 3

 

CompanyCalendar cc = new CompanyCalendar();
 
 cc.set(CompanyCalendar.YEAR, cc.get(CompanyCalendar.YEAR) - 1);
 cc.set(CompanyCalendar.MONTH, CompanyCalendar.JANUARY);
 cc.set(CompanyCalendar.DAY_OF_MONTH, 1);
 
 OqlQuery oql = new OqlQuery(“select new map(r.id as id, r.personName as personName , r.personSurname  as personSurname, sum(w.duration) as worklog_total) from com.twproject.resource.Resource r, com.twproject.worklog.Worklog w where w.assig = r.myself and w.inserted > :paramInserted and r.myself is not null group by r.id”);
 
 oql.setParameter(“paramInserted”,cc.getTime());
 List persLog = oql.list();
 
 Map parameters = new HashMap();
 
 InputStream reportStream = new FileInputStream(
 request.getRealPath(“applications/teamwork/reports/jrxml/new_worklog.jrxml”));
 JasperDesign jasperDesign = JasperManager.loadXmlDesign(reportStream);
 JasperReport jasperReport = JasperManager.compileReport(jasperDesign);
 
 JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(persLog);
 JasperPrint jasperPrint = JasperManager.fillReport(jasperReport, parameters, ds);
 
 //—- Export PDF —-
 JasperManager.printReportToPdfFile(jasperPrint, request.getRealPath(“”) + “/applications/teamwork/reports/output/report.pdf”);
 
 //—- Export HTML —-
 JasperExportManager.exportReportToHtmlFile(jasperPrint, request.getRealPath(“”) + “/applications/teamwork/reports/output/report.html”);

 

Sample worklog.jrxml for Teamwork 3

Welcome

Hi, welcome to my little place on the net.
Your can find most of my work here including articles, notes, tutorials, links, downloads … hmm…what did i miss…..

oh yeah, BLOGS!! :)

don’t forget to visit Khokhar’s Hut