ldbkutty
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?
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?
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(Pla nungLocalH ome.JNDI_N AME);
PlanungLocal planungLocal = null;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int day = calendar.get(Calendar.DATE );
int month = calendar.get(Calendar.MONT H);
int year = calendar.get(Calendar.YEAR );
switch (typeOfTimeView) {
case TypeView.TYPE_VIEW_YEAR:
try {
collection = planungLocalHome.findPlanV alueForCus tomersByDe partmentAn dYear(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.findPlanV alueForCus tomersByDe partmentAn dMonth(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.findPlanV alueForCus tomersByDe partmentAn dDay(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 findPlanValueForCustomersB yDepartmen tAndYear(j ava.lang.I nteger 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 findPlanValueForCustomersB yDepartmen tAndMonth( 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 findPlanValueForCustomersB yDepartmen tAndDay(ja va.lang.In teger 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?
public PlanungView getSumCapacity(int typeOfView, int departmentId, Date date) {
Collection collection = null;
try {
Context context = new InitialContext();
PlanungLocalHome planungLocalHome = (PlanungLocalHome)context.
PlanungLocal planungLocal = null;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int day = calendar.get(Calendar.DATE
int month = calendar.get(Calendar.MONT
int year = calendar.get(Calendar.YEAR
switch (typeOfTimeView) {
case TypeView.TYPE_VIEW_YEAR:
try {
collection = planungLocalHome.findPlanV
} catch (FinderException e1) {
System.out.println("No plan value for the given abteilungen in YEARS");
}
break;
case TypeView.TYPE_VIEW_MONTH:
try {
collection = planungLocalHome.findPlanV
} catch (FinderException e1) {
System.out.println("No plan value for the given abteilungen in MONTH");
}
break;
case TypeView.TYPE_VIEW_DAY:
try {
collection = planungLocalHome.findPlanV
} 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 findPlanValueForCustomersB
* 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 findPlanValueForCustomersB
* 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 findPlanValueForCustomersB
* 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.findPlanV alueForCus tomersByDe partmentAn dDay(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
collection = planungLocalHome.findPlanV
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
ASKER
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.Unk nownPathEx ception: Unknown terminal field: at line 1, column 22. Encountered: "planvalue" after: "c.")
?
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
?
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
also remove the brackets "(" from the where clause
ASKER
Ok, i solced that problem and made the return type with "PlanungView".
now in my action class, i have:
PlanungView[] planungViews = planungsSession.getSumCapa city(level OfTimeView ,Integer.p arseInt(fi lterReport Form.getDe partmentId ()), filterReportForm.getFromDa te() );
float sumCapacity = 0;
for (int j = 0; j < planungViews.length; j++) {
sumCapacity = sumCapacity + planungViews[j].getPlanVal ue().float Value();
}
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 ?
now in my action class, i have:
PlanungView[] planungViews = planungsSession.getSumCapa
float sumCapacity = 0;
for (int j = 0; j < planungViews.length; j++) {
sumCapacity = sumCapacity + planungViews[j].getPlanVal
}
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 ?
ASKER
so, how can i change my code for my requirement?
Thanks for help till now. :)
Thanks for help till now. :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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("resu
and then forward to ur JSP view that will do:
<%
Collection coll = (Collection) request.getAttribute("resu
Iterator iterator = coll.iterator();
while(iterator.hasNext())
{
Object toDisplay = iterator.next();
out.println(toDisplay);
}
%>