Wednesday, 7 July 2010

The Magic of Get/Post Parameter


While doing code re-factoring today on a major function. I discovered a super funny and interesting problem
To start with, take a look in the following code
@RequestMapping(value="saveProductBarcode.htm")
public ModelAndView saveProductBarcode(Model model,
@ModelAttribute("productDetailForm") ProductDetailForm productDetailForm,
@ModelAttribute("productDetail_action") String productDetail_action,
HttpServletRequest request) throws Exception{

String parameter = request.getParameter("parameter");

if (parameter != null && parameter.equals("save")){
ProductBarcode productBarcode = new ProductBarcode();
productBarcode.setBarcode(productDetailForm.getBarcode());
productBarcode.setSysStatus(ProductBarcode.ACTIVE);
productBarcode.setProduct(productDetailForm.getProduct());
productDetailService.saveProductBarcode(productBarcode);

...
model.addAttribute("productDetailForm", productDetailForm);
return new ModelAndView("/sve/product/reference/productdetail/index");
}

return new ModelAndView("/sve/product/reference/productdetail/index");
}


It is very funny looking at the highlighted segment. If your page is targeted to saveProductBarcode.htm, then the function saveProductBarcode is invoked. For this reason, why you need to pass a parameter with a value “save” to invoke the logics?
The fact is, it is not the only function inside the controller writing in this approach. Nearly every function is like that......

Consider another piece of code segment,
@RequestMapping(value="removeProductBarcode.htm")
public ModelAndView removeProductBarcode(Model model,
@ModelAttribute("productDetailForm") ProductDetailForm productDetailForm,
@ModelAttribute("productDetail_action") String productDetail_action,
HttpServletRequest request) throws Exception{

String parameter = request.getParameter("parameter");

ProductBarcode removeProductBarcode = null;
for (ProductBarcode productBarcode
                    :productDetailForm.getProduct().getProductBarcodes()){
int i = ((List)productDetailForm
                        .getProduct().getProductBarcodes()).indexOf(productBarcode);

// Remove Product Barcode
if (parameter.equals("delete_product_barcode_" + i)){
removeProductBarcode = productBarcode;
break;
}
}
if (removeProductBarcode != null){
productDetailForm.getProduct().
                         getProductBarcodes().remove(removeProductBarcode);
productDetailService.deleteProductBarcode(removeProductBarcode);
return new ModelAndView("/sve/product/reference/productdetail/index");
}

return new ModelAndView("/sve/product/reference/productdetail/index");
}

This is web programming 101. When you wanna pass parameter to the server, you can use either GET/POST parameter to bring information back to the controller from the web user interface. The fact is, you are NOT LIMITED to use ONLY ONE parameter. You can use multiple of them. This is really the first time for me to see someone wrote (parameter.equals("delete_product_barcode_" + i).
When i dig a little deeper into it, the parameter is actually like “delete_product_barcode_0” or “delete_product_barcode_1” and so on..... What the developer has to do then, is to parse the string value of the parameter and get the ith value. Come on, you can actually use http://xxx/xxx/do_something.jsp?action=delete_product_barcode&item=i

I guess this is not something very difficult, isn’t it ?
Last but not least, you are returning to “sve/product/reference/productdetail/index” anyway, you can omit the one inside the if-condition when “removeProductBarcode != null

 

Thursday, 10 June 2010

When Fred is Gone (Day 1)

Day 1
There are lots of tasks, FICS, DSAT, and LLPS, well, i guess LLPS is none of my business.
Ronald is busying making last minute updates on the FICS project. At the same time, I'm keeping the look-n-feel looks great. But it is a super tough mission too.....
I'm busying answering Kevin's email and making powerpoint for DSAT's presentation. Hope that I don't have to go and demo to them =P

It is such a tough day without Fredfred.

Kenny =(

Thursday, 6 May 2010

8 hours of work everyday

We spent 8 or more hours a day at office, doing what we are good at or familiar with - Programming and Development.
After spending the 8 hours everyday of work, try to ask yourself a silly question, what have you done today?
Did you learn anything today?

It turns out that, I started feel meaningless for what I have been doing these coming to be 3 years of work.
I thought, the company is very organized. I thought the company is very structured. I thought the company follows a methodological approach to make software and web systems works.
But to the end, I guess I trusted the faked truth. 

8 hours a day sitting at the office is not that productivity than I worked 2 hours for my self-initiated system at home after work.
What does that implies?
It could mean that the company doesn't worth your time to spend on anymore, or it just simply means that you already lost interested in this area of work.

I guess I couldn't stand for long...

A desperate programmer

Wednesday, 24 March 2010

a fire-fighting request - episode 2

continue on the previous post,
as we are waiting for task assignments we go on to dig deeper into the codes,
just to discover even more code smells.

- hardcoded SQL statement and query logic in JSP
- string comparisons using "==", which leads to unreachable blocks of code
- though JDBC connection factories(see previous post) are in place, there are still direct inline creations of JDBC connection.
- direct dependency from domain object to JDBC API
- and....what the hell is this?

Tuesday, 23 March 2010

a fire-fighting request

i was transferred to another project this morning on an urgent request to meet the schedule.
the project was in charged by the in-house development team of my client,
it is a straight forward web app with member subscription screen flow.

it was after i checked out all the codes to only realize that this project is such a disaster.
firstly with the requirements given i dont really expect a sophisticated architecture,
but a plain servlet + jsp web project started in 2010 indeed surprised me.

as i glanced through the code there are lots of code smells,

- extremely unorganized packages, looks like every one of them was doing his/her own project.
- JDBC connection factory classes (yes, there are more than one, checked in by different developers, apparently they are not from the same university so the coding style are different) with hardcoded connection properties.
- there are even business methods defined in one of the factory classes above.
- SQL statements by string concatenations (you think SQL-injections are so 90's huh?)

just to name a few.

Monday, 22 March 2010

why you should not deliver a service client...

every time you provide a service to external party,
chances are that they will request a client for your service,
for whatever reason.

apart from wisdom by authorities,
i hate delivering a service client when one request it with lazy reason.

the following is a conversation in IM between me and a developer from my client.

{ background: part of my project involves providing a RESTful web service in JSON for several lists of reference data }

client: hi i received ur email about the jason web services
me: (ok, jason is a male's name, and JSON is the format.)
client: but a got a 404 when i tried http://xxx.xxx.xxx.xxx:8080/JSONXxxService/getXxxxList
me: it is /YYYY_SERVER/JSONXxxService/getXxxxList, if you have read my mail carefully.
client: actually, i tried that too, got a 405 instead.
me: well, did you tried that in your browser?
client: yes, haha
me: as i have mentioned in the mail, it only supports POST.
client: oooooo, i just figure it out.

(a while later...)

client: would you help to get the full set of those lists since it needs some time to develop the client.
me: what lists?
client: all of them.

I would be willing to help, indeed, if I have your salary too.