View Javadoc

1   /*
2    * JaspertReports JSF Plugin Copyright (C) 2011 A. Alonso Dominguez
3    *
4    * This library is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as published by
6    * the Free Software Foundation; either version 2.1 of the License, or (at
7    * your option) any later version. This library is distributed in the hope
8    * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9    * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   *
11   * See the GNU Lesser General Public License for more details. You should have
12   * received a copy of the GNU Lesser General Public License along with this
13   * library; if not, write to the Free Software Foundation, Inc., 59 Temple
14   * Place, Suite 330, Boston, MA 02111-1307 USA A.
15   *
16   * Alonso Dominguez
17   * alonsoft@users.sf.net
18   */
19  package net.sf.jasperreports.jsf.engine.converters;
20  
21  import javax.faces.context.FacesContext;
22  import net.sf.jasperreports.engine.JasperReport;
23  import net.sf.jasperreports.jsf.component.UIReport;
24  import net.sf.jasperreports.jsf.context.JRFacesContext;
25  import net.sf.jasperreports.jsf.convert.ConverterException;
26  import net.sf.jasperreports.jsf.convert.ReportConverter;
27  import net.sf.jasperreports.jsf.resource.Resource;
28  
29  /**
30   *
31   * @author aalonsodominguez
32   */
33  public class ReportConverterBase implements ReportConverter {
34  
35      public boolean accepts(Object value) {
36          if (value == null) return true;
37          return (true);
38      }
39      
40      public final JasperReport convertFromValue(FacesContext context,
41              UIReport component, Object value)
42      throws ConverterException {
43          if (context == null) {
44              throw new IllegalArgumentException("context can't be null");
45          }
46          if (component == null) {
47              throw new IllegalArgumentException("component can't be null");
48          }
49  
50          Object aValue = component.getValue();
51          if (aValue == null) {
52              return null;
53          }
54  
55          JasperReport aReport;
56          if (aValue instanceof JasperReport) {
57              aReport = (JasperReport) aValue;
58          } else {
59              String valueStr;
60              if (aValue instanceof String) {
61                  valueStr = (String) aValue;
62              } else {
63                  valueStr = aValue.toString();
64              }
65  
66              Resource resource = getJRFacesContext().createResource(
67                      context, component, valueStr);
68  
69              aReport = loadFromResource(context, component, resource);
70          }
71          return aReport;
72      }
73  
74      protected JasperReport loadFromResource(FacesContext context,
75              UIReport component, Resource resource)
76      throws ConverterException {
77          return null;
78      }
79  
80      protected final FacesContext getFacesContext() {
81          FacesContext context = FacesContext.getCurrentInstance();
82          if (context == null) {
83              throw new IllegalStateException("No faces context");
84          }
85          return context;
86      }
87  
88      protected final JRFacesContext getJRFacesContext() {
89          return JRFacesContext.getInstance(getFacesContext());
90      }
91  
92  }