Making an email link in a table with Java Server Faces

August 27, 2008

Ok, here’s how to do it.

1. Edit the table with the email in it - and set the type of the email field to a HYPERLINK.

2. Create a STATIC TEXT field on the form somewhere, change its ID to txtJAVASCRIPT, turn off the escape property and add a binding attribute to it. We’ll use this to run a bit of javascript to call the email.

3. Add a binding attribute to tablerowgroup1 on your table

4. In the action code for the Hyperlink insert the following code, this will make the javascript basically tell the browser to open the users email programme with the email address in the subject. Obviously you’ll need to change tblcustomersDataProvider1 to your data provider and email to the field in the database corresponding to the persons email address.

public String hyperlink2_action() {
        // Process email
        tblcustomersDataProvider1.setCursorRow(
                tableRowGroup1.getRowKey());
        String sEmail=tblcustomersDataProvider1.getValue("email")+"";
        if (!sEmail.equals("")) {
           
            txtJAVASCRIPT.setText("<script language='javascript'>\nwindow.location=\"mailto:"+sEmail+"\";\n</script>");
           
        } else {
           
            txtJAVASCRIPT.setText("");
        }
        return null;
    }

5. To cancel the email request we add the following into the public void preprocess() code for the jsf page

    txtJAVASCRIPT.setText(""); 

Using this technique you can pretty much make any javascript happen on a page based on an event.


Beautiful Coding - The way to life.

August 27, 2008

I often have to fix other people’s code. You can tell coders who have been coding for years simply by the feel of their code.

One of the problems with java is the simple fact that you can end up with a spaghetti-junction of objects all calling, calling back, implementing, sub classing, overriding, running ejb’s etc etc and it can be a bit of a nightmare to find your way through.

The key here (although you can reverse engineer into a UML Model with Netbeans) is really in the way that the code is written.

Things I do to make code more readable

- Initials of variables

All my strings for instance start with s - sName, sUsername
integers I , iCount, iGold
doubles d

and so on for different types of variables or objects.

Components on forms I use a 3 letter start up.

txtTEXTBOX
lblLABEL
stcSTATICTEXT
cmbCOMBOBOX

Also in the code using the java doc function, within Netbeans if you type /** enter it will auto document the next function and you just “fill in the blanks”, javaDoc is fantastic and well worth looking into. If you have “java doced” your code, not only does Netbeans pick up the documentation when you are typing or using your own functions but also you can generate a website that documents your classes!!! How Call

ALT-SHIFT-F - auto format your code, very useful.

Comment long bracketed loops. I always put // from  next to a bracketed loop  that is too long to see in one screen. e.g.

for (int iCount=0;iCount<10;++iCount) {         
system.out.println("Hello World "+iCount); } // From count to 10 icount

Also comment all calls to EJB’s to say what they do -

// Get this job’s History
JobManagerBean.getHistory(sJobNumber);

Beautiful Code!

Lisp programmers write beautiuful code, why? Because you have to, it becomes unreadable otherwise. In a way I wish all programmers would learn Lisp - because it makes you think in a different way and see software development from an almost etherial height. When I get some further research time I want to work on a lisp implementation for netbeans.

Remember that you will not be around forever, don’t punish the next poor programmer who has to work on your code. I like my code to be really easy to read and understand.

Code Rewriting

You know that sometimes I will rewrite a whole subroutine if I’m not happy with it. Programmers are artists, the difference is that the result of our art is a system for people to use or some form of output. Well written, neat, fast, efficient code means happy users and that is the caling of a programmer, to make the lives of the users easier.

If your software makes it harder. It needs rewriting, or maybe replacing with a pen and paper.

Be passionate about your coding and your users will be passionate about your software.


Jasper Reports — those empty reports.

August 26, 2008

Jasper reports is a wonderful product .. and it does integrate really nicely with netbeans. But when it goes wrong you can get blank reports with no clue to what the problem is.

Here are some short observations when coding it up with java and JSF that might help you.

1. Watch your field names - I noticed that when you generated reports Jasper Reports writes a pretty odd query especially with SQL server 2000 at the back end (yes I know, some people still use it, although this one WILL be upgraded to MYSQL):-

SELECT
     tblZones.”ZoneDescription” AS tblZones_ZoneDescription
FROM
     “dbo”.”tblZones” tblZones
ORDER BY
  tblZones.”ZoneDescription

Now this is fine - so then the fields in the report are mapped to tblZones_ZoneDescription intead of just leaving it as is - so if you then have a custom query for the report make sure that you are using the right field names or you could get that “blank page thing”

2. Main/Subforms or header/details (you know, Purchase Order Header and lines) - getting sub forms to work is hard graft! - I will post a solution when we do it - but until then it’s easier to have a SINGLE query with a join have the lines in the detail and group by the header id.

3. When it all goes wrong - sometimes I’ve had it refusing to run the jasper reports report I created earlier in good Blue Peter fashion. Normally a restart of NetBeans does fix this - but sometimes I’ve found a CHKDSK.

4. BACK IT UP - EVERYONE who develops should use a repository system such as subversion or CVS it’s saved my life countless times.

Have fun!


Solaris Development + Virtual Box cannot ping itself

August 7, 2008

If you have a Virtual Box WIndows server and you want to connect to it from the Solaris server it is running on..try this

1. Set the Virtual Box to DHCP and you will probably find you can ping the Solaris server and back again
2. Get up a command prompt and do IPCONFIG
3. Set up your static IP address and make sure you set up the WINS SERVER and TURN ON NETBIOS

TADA!