[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

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?
                
0
ldbkutty
Asked:
ldbkutty
  • 4
  • 4
1 Solution
 
petmagdyCommented:
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);
}
%>
0
 
ldbkuttyAuthor Commented:
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?
0
 
petmagdyCommented:
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ldbkuttyAuthor Commented:
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.")

?
0
 
petmagdyCommented:
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
0
 
ldbkuttyAuthor Commented:
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 ?
0
 
ldbkuttyAuthor Commented:
so, how can i change my code for my requirement?

Thanks for help till now. :)
0
 
petmagdyCommented:
ok, as far I under Stand u need to do a new Session Method like this:

like the following psudo code:

import java.util.*;

public Vector getSumCapacityForDates(int typeOfView, int departmentId, Date[] years)
{
 // Loop on the Date Array using the original function, collect the results in the new vector
  Vector result = new Vector();
  for(int i=0; i < years.length; i++)
  {
   Vector thisResult = ( Vector) Arrays.asList( getSumCapacity(typeOfView,departmentId,   years[i] ) );
   result.addAll ( thisResult );
  }
return result;
}

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now