package sn.unitech.stock.bean;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import sn.unitech.common.dao.ReadProperties;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
public class ToolsBean {
private ReadProperties readProperties=ReadProperties.getReadProperties();
public static ToolsBean toolsBean= new ToolsBean();
public final static String idParamUser="ParamUser";
public final static String idTypAutresUnites="AUTRES";
public final SimpleDateFormat sdfDate=new SimpleDateFormat("dd/MM/yyyy");
public final SimpleDateFormat sdfHeure=new SimpleDateFormat("HH:mm");
public final static String lblDetail="Detail";
public final static String lblFermer="Reduire";
public final static String panelFournisseur="PANELFOURNISSEUR";
public final static String panelClient="PANELCLIENT";
public final static String panelRangmnt="PANELRANGEMENT";
public final static String TIERS_FOURNISSEUR="FOURNISSEUR";
public final static String TIERS_RANGEMENT="RANGEMENT";
public final static String TIERS_CLIENT="CLIENT";
public final static String FROM_TABLE_DETAILEVENT="DETAILEVENT";
public final static String FROM_TABLE_STOCK="STOCK";
public final static String FROM_TABLE_PRODUIT="PRODUIT";
public final static String FROM_TABLE_DETAILCOMMANDE="DETAILCOMMANDE";
public final static String MODAL_PANEL="javascript:Richfaces.showModalPanel('idmodalListeProduits')";
public final static String EST_DE_NATURE_FRAIS="FRAIS";
public final static String EST_DE_NATURE_REMISE="REMISE";
public final static String EST_DE_NATURE_TAXE="TAXE";
public static final String CURRENT_COMMANDE = "commande";
public final static String BAR_TYPE="barchart";
public final static String LINE_TYPE="linechart";
private ToolsBean() {
// TODO Auto-generated constructor stub
}
public static ToolsBean getToolsBean() {
return toolsBean;
}
public Object getAttribute(String idAttribut){
return((HttpSession ) FacesContext.getCurrentInstance().getExternalContext()
.getSession(false)).getAttribute(idAttribut);
}
public void setAttribute(String idAttribut,Object obj){
((HttpSession ) FacesContext.getCurrentInstance().getExternalContext()
.getSession(false)).setAttribute(idAttribut,obj);
}
public List dataModelToList(DataModel dataModel ){
List list=new ArrayList();
Iterator it = dataModel.iterator();
while(it.hasNext()){
list.add(it.next());
}
return list;
}
Map defaultParametter(){
// ParamsUser paramsUser=getParamsUser();
ParamsUser paramsUser=(ParamsUser)this.getToolsBean().getAttribute(ToolsBean.idParamUser);
paramsUser.parametters.clear();
paramsUser.parametters.put(0,paramsUser.adherent.getIdAdherent());
return paramsUser.parametters;
}
public SimpleDateFormat getSdfHeure() {
return sdfHeure;
}
public String getBundleMessage(String key){
ResourceBundle bundle = ResourceBundle.getBundle(FacesContext.getCurrentInstance().getApplication().getMessageBundle());
return bundle.getString(key);
}
/********************************** Avis Operation *****************************************/
public String imprimerRapport(String format, String modeleRapport,String nomRapport,Object rs,Map parametters) {
try{
byte[] bytes ;JasperPrint jasperPrint=null;
JasperDesign jasperDesign = JRXmlLoader.load(readProperties.readChemin("Etats.local")+modeleRapport);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
if(rs instanceof List) {
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource((List) rs);
jasperPrint = JasperFillManager.fillReport(jasperReport, parametters,ds);
}
if(rs instanceof ResultSet) {
JRResultSetDataSource ds=new JRResultSetDataSource((ResultSet)rs);
jasperPrint = JasperFillManager.fillReport(jasperReport, parametters,ds);
}
System.out.println(" taille jasper "+jasperPrint.toString());
if(jasperPrint != null){
if(format.equalsIgnoreCase("IMP")){
JasperPrintManager.printReport(jasperPrint,true);
}
if(format.equalsIgnoreCase("HTML")){
System.out.println(" Ok Html ");
ByteArrayOutputStream htmlReport = new ByteArrayOutputStream();
OutputStream out=null;
JasperExportManager.exportReportToHtmlFile(jasperPrint,"etats.html");
JRHtmlExporter htmlExporter = new JRHtmlExporter();
htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
htmlExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,htmlReport);
htmlExporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
htmlExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "ISO8859_1");
try {
htmlExporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
System.out.println("erreur print");
}
bytes =htmlReport.toByteArray();
System.out.println(" taille byte "+bytes.length);
// response.sendRedirect("http://localhost:8080/PoF/operation/testimp.meteo");
response.setContentType("text/html");
response.setHeader("Content-disposition","attachment;filename="+nomRapport+".html");
response.getOutputStream().write(bytes);
response.setContentLength(bytes.length);
context.responseComplete();
System.out.println(" Fin Ok Html ");
} // fin HTML
if(format.equalsIgnoreCase("PDF")){
System.out.println(" Ok PDF print");
bytes = JasperExportManager.exportReportToPdf(jasperPrint);
response.setHeader("Content-disposition","attachment;filename="+nomRapport +".pdf");
response.getContentType();
response.setContentLength(bytes.length);
response.getOutputStream().write(bytes);
response.setContentType("application/pdf");
context.responseComplete();
} // fin pdf
}// fin test si jasperprint not null
return "ok print";
}catch(Exception ex){ex.printStackTrace();return ex.getMessage();}
}
}