Link to home
Start Free TrialLog in
Avatar of ldbkutty
ldbkuttyFlag for India

asked on

scenario regarding ejb, action class and struts

My ejb query returns a SUM value. and i am calling the query in a loop (say 5 times). Assume the first time it reutrns 5, second time 10, third time 15, fourth time 20 and fifth time 25.

I want to store this every value in collection (or view object or whatever) and display that "every" value in my struts-JSP.

What is the best scenario? collection or viewobject or what type to return? How i can display them in my JSP?
                
Avatar of petmagdy
petmagdy
Flag of Canada image

If u want just to display the results once after the Queries finish then:
Create an Action that will call the EJB method which will run the Queries and return the result in a Collection (like Vector Object for example), after that the Action will put the collection into the request like this:

request.setAttribute("results", theCollection);

and then forward to ur JSP view that will do:

<%
Collection coll = (Collection) request.getAttribute("results");
Iterator iterator = coll.iterator();
while(iterator.hasNext())
{
 Object toDisplay = iterator.next();
  out.println(toDisplay);
}
%>
Avatar of ldbkutty

ASKER

I have this method in my Session-EJB :

public PlanungView getSumCapacity(int typeOfView, int departmentId, Date date) {
          
       Collection collection = null;
        try {
            Context context = new InitialContext();
            PlanungLocalHome planungLocalHome = (PlanungLocalHome)context.lookup(PlanungLocalHome.JNDI_NAME);
            PlanungLocal planungLocal = null;
           
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
           
            int day = calendar.get(Calendar.DATE);
            int month = calendar.get(Calendar.MONTH);
            int year = calendar.get(Calendar.YEAR);
           
            switch (typeOfTimeView) {
                  case TypeView.TYPE_VIEW_YEAR:
                      try {
                                collection = planungLocalHome.findPlanValueForCustomersByDepartmentAndYear(new Integer(departmentId), new Integer(year));
                          } catch (FinderException e1) {
                              System.out.println("No plan value for the given abteilungen in YEARS");
                          }
                      break;
                  case TypeView.TYPE_VIEW_MONTH:
                      try {
                                collection = planungLocalHome.findPlanValueForCustomersByDepartmentAndMonth(new Integer(departmentId), new Integer(month));
                          } catch (FinderException e1) {
                              System.out.println("No plan value for the given abteilungen in MONTH");
                          }
                      break;
                  case TypeView.TYPE_VIEW_DAY:
                      try {
                                collection = planungLocalHome.findPlanValueForCustomersByDepartmentAndDay(new Integer(departmentId), new Integer(day));
                          } catch (FinderException e1) {
                              System.out.println("No plan value for the given abteilungen in DAY");
                          }
                      break;
                  default:
                      break;
                  }
           
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          
          return collection;  
}


Now, i have planned to call this method in my Action Class within a loop of dates. So, every date returns a collection, right? I want that every value to be displayed in my Struts-JSP.

My EJB Queries are:

 * @ejb.finder
 *       signature = "java.util.Collection findPlanValueForCustomersByDepartmentAndYear(java.lang.Integer departmentId, java.lang.Integer year)"
 *       query = "select SUM(c.planvalue) from Planung as c where c.currentEntry = true and (c.departmentId= ?1) AND (c.year = ?2)"
 *
 * @ejb.finder
 *       signature = "java.util.Collection findPlanValueForCustomersByDepartmentAndMonth(java.lang.Integer departmentId, java.lang.Integer month)"
 *       query = "select SUM(c.planvalue) from Planung as c where c.currentEntry = true and (c.departmentId= ?1) AND (c.month = ?2)"
 *
 * @ejb.finder
 *       signature = "java.util.Collection findPlanValueForCustomersByDepartmentAndDay(java.lang.Integer abteilungenId, java.lang.Integer date)"
 *       query = "select SUM(c.planvalue) from Planung as c where c.currentEntry = true and (c.departmentId= ?1) AND (c.date = ?2)"


1. Is the above method and finders ok? or should i make the return type of my session method as PlanungView and use it?
do u want just to return a Collection (Vector or List) of Long values of the SUMs? if so in order to help u just run the following and get me the result into ur session method:

    collection = planungLocalHome.findPlanValueForCustomersByDepartmentAndDay(new Integer(departmentId), new Integer(day));
    Iterator itr = collection.iterator();
    while(itr.hasNext())
    {
        Object obj = itr.next();
        System.out.println("Class is of type: " + obj.getClass().getName() );
   }

repeat this for the other finders
any idea why my finder throws exception:

Error compiling EJB-QL statement 'select SUM(c.planvalue) from Planung as c where c.currentEntry = true and (c.departmentId= ?1) AND (c.month = ?2)'; - nested throwable: (org.jboss.ejb.plugins.cmp.ejbql.UnknownPathException: Unknown terminal field: at line 1, column 22.  Encountered: "planvalue" after: "c.")

?
I can't help u with this unless u post the whole entity descripter, but probably a CMP field name is wrong or type mismatch, note that the CMP field names are case senstive,
also remove the brackets "(" from the where clause
Ok, i solced that problem and made the return type with "PlanungView".

now in my action class, i have:

PlanungView[] planungViews = planungsSession.getSumCapacity(levelOfTimeView,Integer.parseInt(filterReportForm.getDepartmentId()), filterReportForm.getFromDate() );
float sumCapacity = 0;
for (int j = 0; j < planungViews.length; j++) {
     sumCapacity = sumCapacity + planungViews[j].getPlanValue().floatValue();
}
                
and i get the sum of PlanValues for my Query. :-)

but now the problem is, i am passing the FromDate() as a static arugument. !

I have two date fields in myJSP: fromDate and toDate.

consider, i enter for example, fromDate as "1.1.2004" and toDate as "1.1.2005" and i select "years" (levelOfTimeView property)

so while calling the function "getSumCapacity(...)", i need to pass the "date" two times (one for 2004 and 2005).
So, if i get the result of "2004" as "500.00" and the result of "2005" as "600.00", i need to show both of them in my JSP.

Is that clear ?
so, how can i change my code for my requirement?

Thanks for help till now. :)
ASKER CERTIFIED SOLUTION
Avatar of petmagdy
petmagdy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial