java - unable to pass values from session variable for making pdf using iText and Struts2 -
here trying have made lists fetching values , storing in particular lists , since want access values in method creating session variable , passing values in session variable setting key , accessing in method using get(key) successful so.but want want make pdf in method using values in lists. unable so.below code , please me pass values making pdf.
package com.ca.actions; import java.io.fileoutputstream; import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.util.arraylist; import java.util.list; import java.util.map; import org.apache.struts2.dispatcher.sessionmap; import org.apache.struts2.interceptor.sessionaware; import com.ca.database.database; import com.ca.pojo.event; import com.itextpdf.text.document; import com.itextpdf.text.element; import com.itextpdf.text.pagesize; import com.itextpdf.text.phrase; import com.itextpdf.text.pdf.pdfpcell; import com.itextpdf.text.pdf.pdfptable; import com.itextpdf.text.pdf.pdfwriter; import com.opensymphony.xwork2.actionsupport; import com.opensymphony.xwork2.preparable; public class dataforgeneralreportsaction extends actionsupport implements preparable, sessionaware { private list<string> eventsgeneral = new arraylist<string>(); private list<string> companiesgeneral = new arraylist<string>(); private sessionmap<string, object> sessionmapgeneral; list<string> eventidlist = new arraylist<string>(); list<string> eventnamelist = new arraylist<string>(); list<string> eventvenuelist = new arraylist<string>(); list<string> eventtimelist = new arraylist<string>(); list<string> companynamelist = new arraylist<string>(); list<string> totalamountlist = new arraylist<string>(); list<string> receivedamountlist = new arraylist<string>(); list<string> balanceamountlist = new arraylist<string>(); list<string> eventtdslist = new arraylist<string>(); list<string> paymentdatelist = new arraylist<string>(); list<string> chequeddlist = new arraylist<string>(); private string eventgeneral = null; private string companygeneral = null; list<event> dataforgeneralreports; public list<string> geteventidlist() { return eventidlist; } public void seteventidlist(list<string> eventidlist) { this.eventidlist = eventidlist; } public list<string> geteventnamelist() { return eventnamelist; } public void seteventnamelist(list<string> eventnamelist) { this.eventnamelist = eventnamelist; } public list<string> geteventvenuelist() { return eventvenuelist; } public void seteventvenuelist(list<string> eventvenuelist) { this.eventvenuelist = eventvenuelist; } public list<string> geteventtimelist() { return eventtimelist; } public void seteventtimelist(list<string> eventtimelist) { this.eventtimelist = eventtimelist; } public list<string> getcompanynamelist() { return companynamelist; } public void setcompanynamelist(list<string> companynamelist) { this.companynamelist = companynamelist; } public list<string> gettotalamountlist() { return totalamountlist; } public void settotalamountlist(list<string> totalamountlist) { this.totalamountlist = totalamountlist; } public list<string> getreceivedamountlist() { return receivedamountlist; } public void setreceivedamountlist(list<string> receivedamountlist) { this.receivedamountlist = receivedamountlist; } public list<string> getbalanceamountlist() { return balanceamountlist; } public void setbalanceamountlist(list<string> balanceamountlist) { this.balanceamountlist = balanceamountlist; } public list<string> geteventtdslist() { return eventtdslist; } public void seteventtdslist(list<string> eventtdslist) { this.eventtdslist = eventtdslist; } public list<string> getpaymentdatelist() { return paymentdatelist; } public void setpaymentdatelist(list<string> paymentdatelist) { this.paymentdatelist = paymentdatelist; } public list<string> getchequeddlist() { return chequeddlist; } public void setchequeddlist(list<string> chequeddlist) { this.chequeddlist = chequeddlist; } public sessionmap<string, object> getsessionmapgeneral() { return sessionmapgeneral; } public void setsessionmapgeneral( sessionmap<string, object> sessionmapgeneral) { this.sessionmapgeneral = sessionmapgeneral; } public string geteventgeneral() { return eventgeneral; } public void seteventgeneral(string eventgeneral) { this.eventgeneral = eventgeneral; } public string getcompanygeneral() { return companygeneral; } public void setcompanygeneral(string companygeneral) { this.companygeneral = companygeneral; } public list<event> getdataforgeneralreports() { return dataforgeneralreports; } public void setdataforgeneralreports(list<event> dataforgeneralreports) { this.dataforgeneralreports = dataforgeneralreports; } public list<string> geteventsgeneral() { return eventsgeneral; } public void seteventsgeneral(list<string> eventsgeneral) { this.eventsgeneral = eventsgeneral; } public list<string> getcompaniesgeneral() { return companiesgeneral; } public void setcompaniesgeneral(list<string> companiesgeneral) { this.companiesgeneral = companiesgeneral; } public dataforgeneralreportsaction() { // todo auto-generated constructor stub } @override public void prepare() throws exception { // todo auto-generated method stub connection con = null; try { con = new database().get_connection(); // load companies preparedstatement ps = con .preparestatement("select distinct company_name event"); resultset rs = ps.executequery(); while (rs.next()) { companiesgeneral.add(rs.getstring("company_name")); } // load events ps = con.preparestatement("select distinct event_name event"); rs = ps.executequery(); while (rs.next()) { eventsgeneral.add(rs.getstring("event_name")); } } catch (exception e) { e.printstacktrace(); } { con.close(); } } @override public string execute() throws exception { connection con = null; try { con = new database().get_connection(); // load table. first time table loaded string sql = "select event_id, event_name, company_name,event_venue,total_amount,received_amount,event_tds,balance_amount,cheque_dd_no," + "date_format(payment_date,'%d/%m/%y') dateaspayment,event_time " + "from event"; string = ""; // if instead action has been called jsp page, // result filtered on event , company: if (eventgeneral != null && companygeneral != null) { = " event_name = ? , company_name = ?"; } // load companies preparedstatement ps = con.preparestatement(sql + where); if (where.length() > 0) { ps.setstring(1, eventgeneral); ps.setstring(2, companygeneral); } dataforgeneralreports = new arraylist<event>(); resultset rs = ps.executequery(); int i, j = 0; while (rs.next()) { dataforgeneralreports.add(new event(rs.getstring("event_id"), rs.getstring("event_name"), rs .getstring("company_name"), rs .getstring("event_venue"), rs .getstring("event_time"), rs .getstring("total_amount"), rs .getstring("received_amount"), rs .getstring("cheque_dd_no"), rs .getstring("dateaspayment"), rs .getstring("balance_amount"), rs .getstring("event_tds"))); eventidlist.add(rs.getstring("event_id")); eventnamelist.add(rs.getstring("event_name")); companynamelist.add(rs.getstring("company_name")); eventvenuelist.add(rs.getstring("event_venue")); eventtimelist.add(rs.getstring("event_time")); totalamountlist.add(rs.getstring("total_amount")); receivedamountlist.add(rs.getstring("received_amount")); chequeddlist.add(rs.getstring("cheque_dd_no")); paymentdatelist.add(rs.getstring("dateaspayment")); eventtdslist.add(rs.getstring("event_tds")); balanceamountlist.add(rs.getstring("balance_amount")); } sessionmapgeneral.put("eventidpdf", eventidlist); sessionmapgeneral.put("eventnamepdf", eventnamelist); sessionmapgeneral.put("companynamepdf", companynamelist); sessionmapgeneral.put("eventvenuepdf", eventvenuelist); sessionmapgeneral.put("eventtimepdf", eventtimelist); sessionmapgeneral.put("totalamountpdf", totalamountlist); sessionmapgeneral.put("receivedamountpdf",receivedamountlist); sessionmapgeneral.put("chequeddpdf", chequeddlist); sessionmapgeneral.put("paymentdatepdf",paymentdatelist); sessionmapgeneral.put("eventtdspdf", eventtdslist); sessionmapgeneral.put("balanceamountpdf", balanceamountlist); } catch (exception e) { e.printstacktrace(); } { con.close(); } return success; } public string generatepdfgeneral() throws exception { system.out.println(sessionmapgeneral.get("eventidpdf")); document document = new document(pagesize.a4, 50, 50, 50, 50); pdfwriter writer = pdfwriter.getinstance(document, new fileoutputstream("d:\\generalreports.pdf")); pdfptable table = new pdfptable(11); table.setspacingbefore(25); table.setwidthpercentage(100); table.setspacingafter(25); pdfpcell c1 = new pdfpcell(new phrase("event id ")); c1.sethorizontalalignment(element.align_center); table.addcell(c1); c1 = new pdfpcell(new phrase("event name ")); c1.sethorizontalalignment(element.align_center); table.addcell(c1); c1 = new pdfpcell(new phrase("event time")); c1.sethorizontalalignment(element.align_center); table.addcell(c1); c1 = new pdfpcell(new phrase("event venue")); c1.sethorizontalalignment(element.align_center); table.addcell(c1); c1 = new pdfpcell(new phrase("company name")); c1.sethorizontalalignment(element.align_center); table.addcell(c1); c1 = new pdfpcell(new phrase("total amount")); c1.sethorizontalalignment(element.align_center); table.addcell(c1); c1 = new pdfpcell(new phrase("received amount")); c1.sethorizontalalignment(element.align_center); table.addcell(c1); c1 = new pdfpcell(new phrase("cheque/dd number")); c1.sethorizontalalignment(element.align_center); table.addcell(c1); c1 = new pdfpcell(new phrase("payment date")); c1.sethorizontalalignment(element.align_center); table.addcell(c1); c1 = new pdfpcell(new phrase("event tds")); c1.sethorizontalalignment(element.align_center); table.addcell(c1); c1 = new pdfpcell(new phrase("balance amount")); c1.sethorizontalalignment(element.align_center); table.addcell(c1); table.setheaderrows(1); table.addcell( (pdfpcell) sessionmapgeneral.get("eventidpdf")); table.addcell("1"); table.addcell("net amount"); table.addcell("1"); table.addcell("service tax number: "); table.addcell("2"); table.addcell("service tax @ 0.14 %"); table.addcell("12jh"); table.addcell("123"); table.addcell("safs"); table.addcell("sbc tax @0.50%"); document.open(); document.add(table); document.close(); return "success"; } @override public void setsession(map<string, object> map) { // todo auto-generated method stub sessionmapgeneral = (sessionmap) map; } }
and getting error:
java.lang.classcastexception: java.util.arraylist cannot cast com.itextpdf.text.pdf.pdfpcell com.ca.actions.dataforgeneralreportsaction.generatepdfgeneral(dataforgeneralreportsaction.java:346) sun.reflect.nativemethodaccessorimpl.invoke0(native method) sun.reflect.nativemethodaccessorimpl.invoke(unknown source) sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) java.lang.reflect.method.invoke(unknown source) ognl.ognlruntime.invokemethod(ognlruntime.java:870) ognl.ognlruntime.callappropriatemethod(ognlruntime.java:1293) ognl.objectmethodaccessor.callmethod(objectmethodaccessor.java:68) com.opensymphony.xwork2.ognl.accessor.xworkmethodaccessor.callmethodwithdebuginfo(xworkmethodaccessor.java:117) com.opensymphony.xwork2.ognl.accessor.xworkmethodaccessor.callmethod(xworkmethodaccessor.java:108) ognl.ognlruntime.callmethod(ognlruntime.java:1369) ognl.astmethod.getvaluebody(astmethod.java:90) ognl.simplenode.evaluategetvaluebody(simplenode.java:212) ognl.simplenode.getvalue(simplenode.java:258) ognl.ognl.getvalue(ognl.java:494) ognl.ognl.getvalue(ognl.java:458)
this wrong:
table.addcell( (pdfpcell) sessionmapgeneral.get("eventidpdf"));
you have sessionmap
string
values key , object
values values:
private sessionmap<string, object> sessionmapgeneral;
you add entry key "eventidpdf"
:
sessionmapgeneral.put("eventidpdf", eventidlist);
the eventidlist
arraylist
:
list<string> eventidlist = new arraylist<string>();
you can't cast arraylist
pdfpcell
you're trying here:
table.addcell( (pdfpcell) sessionmapgeneral.get("eventidpdf"));
you have like:
pdfpcell cell = new pdfpcell(); list<string> list = (list<string>) sessionmapgeneral.get("eventidpdf"); (string item : list) { cell.addelement(new paragraph(item)); } table.addcell(cell);
Comments
Post a Comment