Friday, March 26, 2010

Tutorials

I have started a Java tutorial for starting up with Java. It can be found at my Google-site. The tutorial is basic Java.

http://sites.google.com/site/ironicprogrammer/tutorials/ironic-programmer-java-tutorial

I will continuosly add more stuff to the site. Even if the first lessons may be a bit text to read. I will try to do programming by examples.

Sunday, March 7, 2010

Scary, but maybe true

A week ago I read the blogpost "The Non-Programming Programmer" by Jeff Atwood at Coding horror about that many applicants for programming work can not program at all.

During my 10 years in the business I experienced a few myself. I wish I then had the examples to give and exclude some that can not program.

Some examples:

What you can see as a teacher

At my first job as a teacher at university, part of my job was to lecture Pascal and C. Included was laborations in the language learned. Here I could see some serious laziness or lack of logic. One of the first tasks used to be to print out a matrix of the numbers 1..100 in a table of size 10x10. Of course this task is to learn some basic structures in the language learned like iterations as for-loop and conditionals like if. This should be a fairly simple task, but one guy actually just did a
and so on.
Another example is the laziness of students. Some did not want to put in some work to learn the language, they just wanted to pass the class. There is different ways to do this. One is to try to get a group to add your name to the report, another way is to copy someone elses work. Now the stupid part of this, a teacher notice more than you may think. Someone that is not able to manage simple task and then all of a sudden turn up with a solution that looks pretty similar to the brightest student or that of someone that the teacher helped and put in some pattern of text that was put there as an example but is failry recognizable since it was a value you do not just put in.

About applying programmers

Here we have programmers(? or not) that actually have a bigger mouth then ability to program. They do good at an interview and get the job. Later it turn out.

  • that they do not at all know anything about Java, and the excuse is that they hoped that they soon shuould catch up and learn.
  • the one I heard from the boss that applied (this was early 2000 when the market was hot). That they heard that the company was looking and maybe this person could be a candidate because he did som Basic 10 years ago and was thinking maybe he could do some programming.
  • I have noticed, at an interview, think really carefull when people start to talk about project they have been in and saying: "In this project WE used this and that language/database." or "The application I worked is build in this and that technique and this database". The problem with this is that some uses the knowledge of others in their CV, just because they were in a project that had a very nice arhitechture does not mean they did anything of it.

Programmers at work

  • One consultant I met at a task where we came in to look at why the clients web build in asp was so slow. Well the consultant they had there that have been working on the web did not know any SQL so in 4 different frames in a framset did a full lookup on the resultset of a table looped until the key was found and then used anothere key to do the same on another table. 
  • A programmer I worked with got the task to do some of the work when we were dividing an existing Session EJB into 3-4 smaller ones and at the same time change the medthods to return Collection of objects instead of a ResultSet. He actually did that, that is moved the code from the old one to the new one. What he missed was that the work is actually not finishe just because you move code, there is actually placed were you use the code, like a client that ALSO needs to be changed if you change the contract and move the methods it need. WHOOPS!
  • Another man I worked with said he knew Java EE after he went throug a book, downloading the examples and compiled and run throug them once. But, when he did some actual coding, he did not reuse the methods I already had done, he made new ones with other names.
  • Some programers seam to have the philosophy. "If it compiles and run it must be right". NO it is not. Making the compiler happy is easy, making the application work is not the same as keeping the compiler happy, it is logic not syntax.
So that post was very welcomed and will be used in the future.

While we are at it, try to solve this one with recursion:
You have a class called Org to represent a organization. The root organization is the starting point and every instance of the class holds its own suborganizations and a reference to its parent.


Org.java
Your task is to print the organization tree starting from the root. 

Please share your own stories in the comments.

Friday, March 5, 2010

File upload in JSF2 using base class

Surfing the net for JSF2 resources can be a tedious task. Actually for file upload I actually found a couple of blogs describing how to do it the firs I found actually only (FileUpload with JSF 2 and Servlet 3.0) made a taglib based on the second one. And it includes maven which I do not use.

I had to remake the example a bit since I got question to provide a sample application. When extracting the code I noticed it did not work anymore. The reason is probably the one mentioned at this blog: http://mcatr.blogspot.com/2010/01/multipart-requests-and-jsf.html.To make it work again, I did a solution that I am not happy with but it works. It takes the idea from this blog, joined with some from the other blogs mentioned. The former blog states that a multipart request to JSF 2 do not work, because it can not extract parameters from that kind of request.

The problem is that for what I actually wanted it was not right and one was a little bit overkill. So as usual I took another approach. I got some helf from Uploading files with JSF 2.0 and Servlet 3.0 but that solution includes making a custom component and renderer, and is dependant on classes found in Uploading files in Servlet 3.0. I actually copied all the code and tried it...but I got a failure when loading the page. So I wanted something with less code. and simpler to use, and I have a case when I am going to store vCards in a database so a solution to save the file on disk means I have to modify code anyway.

I took the following approach:

  • Use standard html <input type="file" />
  • Use @ManagedBean
  • I first had a base class to handle the file and extended the Managed bean with this one, but since this stopped working I had to add:
    • A Wrapper to wrap multipart requests
    • A Filter to catch multipart request and transform them to the Wrapper type.
  • I also have a FacesUtil class to simplify getting request and parameters
Here is the code:

In the view (vCardForm.xhtml)

Class to represent the uploaded file (UploadFile.java)

I created a class to store some information about the file being upload, I can later choose if I want to store to disk or in a database, for example sending this to an EJB and the EJB descides what to do.

The managed bean (VCardBean.java)

The interesting thing going on here is that i initialize the base class with the request for use of the request. Probably I could have done this in the constructor of the baseclass, but something you have to have to refactor.

MultipartRequestWrapper


MultipartRequestFilter



Session bean for handling the storage.

For the example this only stores it in the session.

Finally the class to inherit to make it all work

This is now obsolete, but I keep it in case JSF will handle the mutlipart form in the future. But even then it may not be needed.

The only thing needed in the Managed bean is to call findFile("vcardFile") on the multipart request to get the file that is being uploaded and take action on that.

A sample project (for netbeans) can be found here: http://sites.google.com/site/ironicprogrammer/home/jsf-file-upload-example