ICEfaces
  1. ICEfaces
  2. ICE-3686

outputResource: lazy access on Resource

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.8RC1, 1.8
    • Component/s: ICE-Components
    • Labels:
      None
    • Environment:
      outputResource + lazy loading

      Description

      The outputResource component should be able to lazily download the resource .

        Activity

        Hide
        Adnan Durrani added a comment -

        The Resource.open() method get invoked only when link/button was clicked. So by writing the custom handler this problem can be resolved.

        The outputResource demo has been modified under component-showcase (revision 18154). Please see the following snippet of the custom handler.

        class MyResource implements Resource, Serializable{
        private String resourceName;
        private InputStream inputStream;
        private final Date lastModified;
        public MyResource(String resourceName)

        { this.resourceName = resourceName; this.lastModified = new Date(); }

        public InputStream open() throws IOException {
        if (inputStream == null)

        { inputStream = new ByteArrayInputStream( OutputResourceBean.toByteArray( Thread.currentThread().getContextClassLoader().getResourceAsStream(OutputResourceBean.RESOURCE_PATH + resourceName))); }

        return inputStream;
        }
        public String calculateDigest()

        { return resourceName; }

        public Date lastModified()

        { return lastModified; }

        public void withOptions(Options arg0) throws IOException {
        }
        }

        Show
        Adnan Durrani added a comment - The Resource.open() method get invoked only when link/button was clicked. So by writing the custom handler this problem can be resolved. The outputResource demo has been modified under component-showcase (revision 18154). Please see the following snippet of the custom handler. class MyResource implements Resource, Serializable{ private String resourceName; private InputStream inputStream; private final Date lastModified; public MyResource(String resourceName) { this.resourceName = resourceName; this.lastModified = new Date(); } public InputStream open() throws IOException { if (inputStream == null) { inputStream = new ByteArrayInputStream( OutputResourceBean.toByteArray( Thread.currentThread().getContextClassLoader().getResourceAsStream(OutputResourceBean.RESOURCE_PATH + resourceName))); } return inputStream; } public String calculateDigest() { return resourceName; } public Date lastModified() { return lastModified; } public void withOptions(Options arg0) throws IOException { } }
        Hide
        Adnan Durrani added a comment -

        The component-showcase demo changed. revision 18154

        Show
        Adnan Durrani added a comment - The component-showcase demo changed. revision 18154
        Hide
        Yogaraj Khanal added a comment -

        I was wondering if I use this approach in 1.7.2 will it work or do I need to upgrade to 1.8 for solving this problem

        Show
        Yogaraj Khanal added a comment - I was wondering if I use this approach in 1.7.2 will it work or do I need to upgrade to 1.8 for solving this problem
        Hide
        Yogaraj Khanal added a comment -

        I solved this problem using code below i am not sure if this is efficient one

        /*

        • To change this template, choose Tools | Templates
        • and open the template in the editor.
          */
          package gov.fda.oim.ccm.ui;

        import gov.fda.oim.ccm.common.backing.BaseBackingBean;
        import gov.fda.oim.ccm.domain.Communication;
        import java.io.InputStream;
        import java.io.FileNotFoundException;
        import java.io.IOException;
        import java.util.ArrayList;
        import java.util.HashMap;
        import java.util.List;
        import java.util.logging.Level;
        import java.util.logging.Logger;
        import net.sf.jasperreports.engine.JRException;
        import javax.faces.context.FacesContext;
        import net.sf.jasperreports.engine.JREmptyDataSource;
        import net.sf.jasperreports.engine.JRExporterParameter;
        import net.sf.jasperreports.engine.JasperFillManager;
        import net.sf.jasperreports.engine.JasperPrint;
        import org.apache.commons.logging.Log;
        import org.apache.commons.logging.LogFactory;
        import com.icesoft.faces.context.ByteArrayResource;
        import com.icesoft.faces.context.Resource;
        import gov.fda.oim.ccm.domain.Amendment;
        import gov.fda.oim.ccm.domain.Category;
        import gov.fda.oim.ccm.domain.Participant;
        import gov.fda.oim.ccm.domain.Telecon;
        import java.io.ByteArrayInputStream;
        import java.io.ByteArrayOutputStream;
        import java.io.FileInputStream;
        import java.io.Serializable;
        import java.text.Format;
        import java.text.SimpleDateFormat;
        import java.util.Date;
        import javax.faces.context.ExternalContext;
        import net.sf.jasperreports.engine.export.JRTextExporter;
        import net.sf.jasperreports.engine.export.JRTextExporterParameter;

        /**
        *

        • @author xuey
          */
          public class IRATemplateBackingBean extends BaseBackingBean {

        private final Log logger = LogFactory.getLog(this.getClass());
        public static final String RESOURCE_PATH = "/WEB-INF/report/";
        private Resource wordResourceForIra;

        public Resource getWordResourceForIra()

        { return wordResourceForIra; }

        public IRATemplateBackingBean() {

        try

        { FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext ec = fc.getExternalContext(); wordResourceForIra = new CcmIraTemplateResource(ec, "ira.doc"); }

        catch (Exception e)

        { e.printStackTrace(); }

        }

        public static byte[] toByteArray(InputStream input) throws IOException {

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int len = 0;
        while ((len = input.read(buf)) > -1)

        { output.write(buf, 0, len); }

        return output.toByteArray();
        }
        }

        class CcmIraTemplateResource extends BaseBackingBean implements Resource, Serializable {

        private final Log logger = LogFactory.getLog(this.getClass());
        private String resourceName;
        private InputStream inputStream;
        private final Date lastModified;
        private ExternalContext extContext;
        private IRACommunicationBackingBean iraCommunicationBackingBean;

        public IRACommunicationBackingBean getIraCommunicationBackingBean()

        { return iraCommunicationBackingBean; }

        public void setIraCommunicationBackingBean(IRACommunicationBackingBean iraCommunicationBackingBean)

        { this.iraCommunicationBackingBean = iraCommunicationBackingBean; }

        public CcmIraTemplateResource(ExternalContext ec, String resourceName)

        { this.extContext = ec; this.lastModified = new Date(); this.resourceName = resourceName; }

        public InputStream open() throws IOException {
        logger.debug("$$IRATemplateBackingBean->open->Before Generating File$$");
        System.out.println("$$IRATemplateBackingBean->Before Generating open$$");
        InputStream reportStream = extContext.getResourceAsStream(IRATemplateBackingBean.RESOURCE_PATH + "CCM_IRA_TEMP.jasper");
        String outputReportFile = "C:\\mySoftware\\CCM\\trunk\\WebContent\\WEB-INF\\report
        ira.doc";
        HashMap parameters = new HashMap();
        parameters.put("CCM_IRA_TEMP", "RECORD OF TELEPHONE CONVERSATION");
        parameters.put("submissionType", "submissionType");
        parameters.put("submissionID", "submissionID");
        parameters.put("reviewOffice", "reviewOffice");
        parameters.put("iraTitle", "iraTitle");
        parameters.put("sponsorName", "sponsorName");
        parameters.put("teleconDateTime", "formatedDate");
        parameters.put("fdaInitiated", "fdaInitiated");
        parameters.put("telNo", "telNo");
        parameters.put("commCat", "categoryComm");
        parameters.put("author", "author");
        parameters.put("teleconSummary", "teleconSummary");
        parameters.put("fdaParticipants", "fdapart");
        parameters.put("nonFda", "nonfdapart");
        parameters.put("refAmend", "amendmentRef");
        JasperPrint jasperPrint;
        try

        { jasperPrint = JasperFillManager.fillReport(reportStream, parameters, new JREmptyDataSource()); JRTextExporter exporter = new JRTextExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputReportFile); exporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, new Integer(55)); exporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, new Integer(80)); exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, new Integer(8)); exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, new Integer(19)); exporter.exportReport(); }

        catch (JRException ex)

        { Logger.getLogger(CcmIraTemplateResource.class.getName()).log(Level.SEVERE, null, ex); }

        logger.debug("$$IRATemplateBackingBean->open->After Generating File$$");
        System.out.println("$$IRATemplateBackingBean->open->After Generating File$$");
        if (inputStream == null)

        { /* */ InputStream stream = extContext.getResourceAsStream(IRATemplateBackingBean.RESOURCE_PATH + resourceName); byte[] byteArray = IRATemplateBackingBean.toByteArray(stream); inputStream = new ByteArrayInputStream(byteArray); }

        return inputStream;
        }

        public String calculateDigest()

        { return resourceName; }

        public Date lastModified()

        { return lastModified; }

        public void withOptions(Options arg0) throws IOException

        { throw new UnsupportedOperationException("Not supported yet."); }

        }

        Show
        Yogaraj Khanal added a comment - I solved this problem using code below i am not sure if this is efficient one /* To change this template, choose Tools | Templates and open the template in the editor. */ package gov.fda.oim.ccm.ui; import gov.fda.oim.ccm.common.backing.BaseBackingBean; import gov.fda.oim.ccm.domain.Communication; import java.io.InputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import net.sf.jasperreports.engine.JRException; import javax.faces.context.FacesContext; import net.sf.jasperreports.engine.JREmptyDataSource; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.icesoft.faces.context.ByteArrayResource; import com.icesoft.faces.context.Resource; import gov.fda.oim.ccm.domain.Amendment; import gov.fda.oim.ccm.domain.Category; import gov.fda.oim.ccm.domain.Participant; import gov.fda.oim.ccm.domain.Telecon; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.Serializable; import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import javax.faces.context.ExternalContext; import net.sf.jasperreports.engine.export.JRTextExporter; import net.sf.jasperreports.engine.export.JRTextExporterParameter; /** * @author xuey */ public class IRATemplateBackingBean extends BaseBackingBean { private final Log logger = LogFactory.getLog(this.getClass()); public static final String RESOURCE_PATH = "/WEB-INF/report/"; private Resource wordResourceForIra; public Resource getWordResourceForIra() { return wordResourceForIra; } public IRATemplateBackingBean() { try { FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext ec = fc.getExternalContext(); wordResourceForIra = new CcmIraTemplateResource(ec, "ira.doc"); } catch (Exception e) { e.printStackTrace(); } } public static byte[] toByteArray(InputStream input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buf = new byte [4096] ; int len = 0; while ((len = input.read(buf)) > -1) { output.write(buf, 0, len); } return output.toByteArray(); } } class CcmIraTemplateResource extends BaseBackingBean implements Resource, Serializable { private final Log logger = LogFactory.getLog(this.getClass()); private String resourceName; private InputStream inputStream; private final Date lastModified; private ExternalContext extContext; private IRACommunicationBackingBean iraCommunicationBackingBean; public IRACommunicationBackingBean getIraCommunicationBackingBean() { return iraCommunicationBackingBean; } public void setIraCommunicationBackingBean(IRACommunicationBackingBean iraCommunicationBackingBean) { this.iraCommunicationBackingBean = iraCommunicationBackingBean; } public CcmIraTemplateResource(ExternalContext ec, String resourceName) { this.extContext = ec; this.lastModified = new Date(); this.resourceName = resourceName; } public InputStream open() throws IOException { logger.debug("$$IRATemplateBackingBean->open->Before Generating File$$"); System.out.println("$$IRATemplateBackingBean->Before Generating open$$"); InputStream reportStream = extContext.getResourceAsStream(IRATemplateBackingBean.RESOURCE_PATH + "CCM_IRA_TEMP.jasper"); String outputReportFile = "C:\\mySoftware\\CCM\\trunk\\WebContent\\WEB-INF\\report ira.doc"; HashMap parameters = new HashMap(); parameters.put("CCM_IRA_TEMP", "RECORD OF TELEPHONE CONVERSATION"); parameters.put("submissionType", "submissionType"); parameters.put("submissionID", "submissionID"); parameters.put("reviewOffice", "reviewOffice"); parameters.put("iraTitle", "iraTitle"); parameters.put("sponsorName", "sponsorName"); parameters.put("teleconDateTime", "formatedDate"); parameters.put("fdaInitiated", "fdaInitiated"); parameters.put("telNo", "telNo"); parameters.put("commCat", "categoryComm"); parameters.put("author", "author"); parameters.put("teleconSummary", "teleconSummary"); parameters.put("fdaParticipants", "fdapart"); parameters.put("nonFda", "nonfdapart"); parameters.put("refAmend", "amendmentRef"); JasperPrint jasperPrint; try { jasperPrint = JasperFillManager.fillReport(reportStream, parameters, new JREmptyDataSource()); JRTextExporter exporter = new JRTextExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputReportFile); exporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, new Integer(55)); exporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, new Integer(80)); exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, new Integer(8)); exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, new Integer(19)); exporter.exportReport(); } catch (JRException ex) { Logger.getLogger(CcmIraTemplateResource.class.getName()).log(Level.SEVERE, null, ex); } logger.debug("$$IRATemplateBackingBean->open->After Generating File$$"); System.out.println("$$IRATemplateBackingBean->open->After Generating File$$"); if (inputStream == null) { /* */ InputStream stream = extContext.getResourceAsStream(IRATemplateBackingBean.RESOURCE_PATH + resourceName); byte[] byteArray = IRATemplateBackingBean.toByteArray(stream); inputStream = new ByteArrayInputStream(byteArray); } return inputStream; } public String calculateDigest() { return resourceName; } public Date lastModified() { return lastModified; } public void withOptions(Options arg0) throws IOException { throw new UnsupportedOperationException("Not supported yet."); } }

          People

          • Assignee:
            Unassigned
            Reporter:
            Adnan Durrani
          • Votes:
            16 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: