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?
                
LVL 32
ldbkuttyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.