missing your own call

Layer SuperType

Posted in Design Patterns by Asif Shahzad on July 14th, 2008

Layer SuperType Design Pattern eliminates code redundancy. and provide better to access to common functionality.

Some time we produce too redundant code. For example implementing Data Access Objects and writing the basic functionality code in each DAO (when using different DAO for each entity). By using Layer SuperType we can eliminate such problem.

For example in one of my (Hibernate and Struts based) project, I am using the Layer SuperType. I have written a class named AbstractDAO (which is basically super layer of Layer SuperType) as follow:

public class AbstractDao {

protected static Session session;
protected static Transaction tx;

 public AbstractDao() {
     HibernateSessionFactory.buildIfNeeded();
}

protected void save(Object obj) {
 try {

     startOperation();
     session.save(obj);
     tx.commit();

     } catch (HibernateException e) {
       handleException(e);

     } finally {
       HibernateSessionFactory.closeSession();
     }
} 

public void saveOrUpdate(Object obj) {
 try {

     startOperation();
     session.saveOrUpdate(obj);
     tx.commit();

     } catch (HibernateException e) {
       handleException(e);

     } finally {
       HibernateSessionFactory.closeSession();
     }
}

and other methods that need to be come under Super Layer.

Now code of DAOs for each entity is extrememly minimized, and you dont need to make explicit type casts while calling DAO’s methods. And the most important thing is, you don’t need to write the exception handing logic in DAOs which is very time/space taking activity.

Now I show you one DAO which uses the Layer SuperType.

public class WidgetDAO  extends AbstractDao{

public WidgetDAO() {
     super(); 
}

public Widget getWidget(Integer id){

 Widget widget = new Widget();
 widget = (Widget)super.objQuery("from Widget w where w.id=" + id);
 return widget;

}

public void addorUpdateWidget(Widget widget){

  super.saveOrUpdate(widget);
}
}

Now i can get a widget using:

Widget wgt = new WidgetDAO().getWidget(1); // here 1 is Widget ID

You see how Layer SuperTypes makes the application code simple, clean, and minimized.

Capitalist Conspiracy - Inside International Banking

Posted in Economy by Asif Shahzad on July 6th, 2008

After watching such videos and knowing other related facts. I have come to know, the world is not being controlled by US or West but by the Capitalism (even an extremely advance version of it, THE WORLD OWNERSHIP DESIRE ). Yes it is true, US is administering Capitalism.

PART-1

PART-2

PART 3 - 5 ( view at youtube, by clicking YOUTUBE on the above video )

But this is something of such importance that one cant ignore it. The whole humanity is at risk just because of a few minds who are governing the world by using different states apparatus.

But the real question is what one can do? and do you find yourself responsible for doing something? does you care about the hummanity future? or you have decided that its going to be happen and it is as it is … and nothing can stop or change this?

Share your views and understandings …

Tagged with: , ,

Allam Iqbal - Iblees ki Majlis-e-Shura (English)

Posted in Belief by Asif Shahzad on June 25th, 2008

I find this poem, one of the greatest of Allama Iqbal’s Work. See how it encompass the whole message of his poetry and wisdom.

A great tribute and thanks to guy who not only translated into English but also explained it.

How a scientist becomes a muslim!

Posted in Belief by Asif Shahzad on June 23rd, 2008

I always pursue to understand Islam intellectually. Recently i come to know the islam-guide.com and found some wonderful stuff.

Watch the video and see how the non Muslims are coming to Islam. And let us analyze ourselves where we are leading towards. And where we were supposed to be.

http://www.islam-guide.com/video/tejasen-1.ram

Professor Tejatat Tejasen is the Chairman of the Department of Anatomy at Chiang Mai University, Chiang Mai, Thailand.  Previously, he was the Dean of the Faculty of Medicine at the same university.  During the Eighth Saudi Medical Conference in Riyadh, Saudi Arabia, Professor Tejasen stood up and said:

“During the last three years, I became interested in the Quran . . . . From my study and what I have learned from this conference, I believe that everything that has been recorded in the Quran fourteen hundred years ago must be the truth, that can be proved by the scientific means.  Since the Prophet Muhammad could neither read nor write, Muhammad must be a messenger who relayed this truth, which was revealed to him as an enlightenment by the one who is eligible [as the] creator.  This creator must be God.  Therefore, I think this is the time to say La ilaha illa Allah, there is no god to worship except Allah (God), Muhammadur rasoolu Allah, Muhammad is Messenger (Prophet) of Allah (God).  Lastly, I must congratulate for the excellent and highly successful arrangement for this conference . . . . I have gained not only from the scientific point of view and religious point of view but also the great chance of meeting many well-known scientists and making many new friends among the participants.  The most precious thing of all that I have gained by coming to this place is La ilaha illa Allah, Muhammadur rasoolu Allah, and to have become a Muslim.”

Reference: http://www.islam-guide.com/frm-ch1-1.htm.
Thanks to islam-guide.com team, Jezakellah!

Struts : Using Arrays in ActionForm

Posted in Struts by Asif Shahzad on June 6th, 2008

just recently i was assigned a task i.e. Populate a form using ActionForm that have multiple values for same property name [i.e. Array]. Although it is very straight forward, but i spent some more time, this is how i proceed. (i am putting it here, thinking it might help someone).

Scenerio/Case: A web application allow users to add/remove widgets on their homepage and set widgets display order. User selected widgets are presented on the home page in specified order when he signed-in.
We want to make a form that display the list of all widgets with indication what widgets user is currently using and in which order they are being displayed. This form allows user to:

1. Remove his current home page widgets
2. Change order
3. Add new widgets from the list of available widgets

Let us dig into code directly. This is how the ActionForm look like:

public class EditPageWidgetsForm extends ActionForm{
private Integer[] id = new Integer[100]; // the widget Id
private String[] title = new String[100]; //widget title
private Integer[] order    = new Integer[100]; //widget order
private Boolean[] status = new Boolean[100]; // widet status; enable or disable

public EditPageWidgetsForm(){ }

Generate/Wirte default getters and setters for each property. For example for property “id” it would look like:

public Integer getId(int index) {
return this.id[index];
}

public void setId(Integer[] id) {
this.id = id;
}

Generate getters and setters for other properties as well.

Now write another getter and setter for each property as follow:

public Integer getId(int index){
return this.id[index];
}

public void setId ( int index , Integer value ) {
this.id [ index ] = value ;
}

Write getter and setters as above for other properties as well, in the same way as above.
__________________________________________________________

Now come to Action that populates this ActionForm. I am using Dispatch Action, so i first populate the action form in view() method of Dispatch Action and then i submit the for to update() method, so that user current widgets preferences can be saved.

The related code of view() method looks like:

public class EditHomePageWidgetsAction extends DispatchAction {
public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {

UserWidgetDAO userWidgetDAO = new UserWidgetDAO();
WidgetDAO widgetDAO = new WidgetDAO();
EditPageWidgetsForm ewForm = (EditPageWidgetsForm)form; // ewForm: Edit Widget Form

UsersList user = (UsersList)request.getSession().getAttribute(”userlist”);
// UserList is basically User, and it represent only one user, yea! a wrong name

ArrayList userWidgetsList = (ArrayList) userWidgetDAO.getUserWidgets(user.getId(), new Integer(1));
// Integer 1 is page Id

ArrayList widgetsList = (ArrayList) widgetDAO.getAllWidgets();

Iterator userWidgetsItr = userWidgetsList.iterator(); // These are user widgets
Iterator widgetsItr = widgetsList.iterator(); // these are all widgets in application

Integer[] idArr = new Integer[widgetsList.size()];
String[] titleArr = new String [widgetsList.size()];
Integer[] orderArr = new Integer[widgetsList.size()];
Boolean[] statusArr = new Boolean[widgetsList.size()];

int index = 0;

while(widgetsItr.hasNext()){
Widget wgt = (Widget)widgetsItr.next();
idArr[index] = wgt.getId();
titleArr[index] = wgt.getTitle();
orderArr[index] = null;
statusArr[index] = new Boolean(false);

userWidgetsItr = userWidgetsList.iterator();
while(userWidgetsItr.hasNext()){
UserWidget uw = (UserWidget)userWidgetsItr.next();
if(wgt.getId().equals((Integer)uw.getWidget().getId())){
orderArr[index] = uw.getOrder();
statusArr[index] = new Boolean(true);
break;
}
}
index++;
}

ewForm.setId(idArr);
ewForm.setTitle(titleArr);
ewForm.setOrder(orderArr);
ewForm.setStatus(statusArr);

request.getSession().setAttribute(”userWidgetsList”,userWidgetsList);
return mapping.findForward(ApplicationConstants.SUCCESS);
}

You see, it basically populates the ActionForm for each user specifically. Very easy to understand, let me know if you need explanation.
___________________________________________________________________________________

So now come to the View ( i.e. a JSP page ). The JSP page looks like:

<html:form action=”EditHomePageWidgets.do?method=update” method=”post”>
<div id=”table”><logic:iterate name=”editPageWidgetsForm” id=”widget” indexId=”ctr”>
<div id=”divrow”>

<div id=”divcell”> <html:text size=”2″ readonly=”true” property=’<%=”id["+ ctr +"]” %>’/></div>
<div id=”divcell”> <html:text size=”35″ readonly=”true” property=’<%=”title["+ ctr +"]“%>’ /> </div>
<div id=”divcell”> <html:text property=’<%=”order["+ ctr +"]“%>’ /> </div>
<div id=”divcell”> <html:checkbox property=’<%=”status["+ ctr +"]“%>’ /> </div>

</div>
</logic:iterate>

</div> <!–  end div table –>
<html:submit>SAVE</html:submit> </html:form>

_______________________________________________________________

The update() method which is called at submit, is very easy to write. You will receive the ActionForm, and do your processing as appropriate for your application. So i am not putting it here. Let me know if you need more detail.

Let me have your feed back. Thanks!

SilentIntellect and Missing Your Own Calls

Posted in Uncategorized by Asif Shahzad on March 24th, 2008

Its natural to first describe the meaning of the title. I have observed with the passage of time, when i see something wrong or someone in trouble, i receive a call from my inside. The call asks me to think about the issue. Now there are two options that i can adopt:

1. Ignore the call (i call it “missing your own call”) ; by telling myself that i am not the concerned person, i haven’t enough power, its not my matter, or its not a problem at all, or its his own problem so let him face alone.

2. Pick the Call; in this case i really feel about the issue and even think to solve it. These are really pleasurable moments when i solve it successfully. But some problems are such gigantic that it makes me sad and deep down in thinking… and i am returned back by an idea i.e. “miss it”. But then a thought pulls me by saying, you are “missing your own call”.

I was lucky enough when no such calls were made to me by me. But now when i receive these calls, i say to myself, you can’t put your intellect silent, you have to attend it!