1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.update.schema.program.jasperreport;
22  
23  import java.io.UnsupportedEncodingException;
24  import java.net.URL;
25  import java.util.Map;
26  import java.util.UUID;
27  
28  import javax.xml.parsers.ParserConfigurationException;
29  
30  import net.sf.jasperreports.engine.DefaultJasperReportsContext;
31  import net.sf.jasperreports.engine.JRDataset;
32  import net.sf.jasperreports.engine.JRException;
33  import net.sf.jasperreports.engine.JRValueParameter;
34  import net.sf.jasperreports.engine.JasperReportsContext;
35  import net.sf.jasperreports.engine.design.JasperDesign;
36  import net.sf.jasperreports.engine.query.JRQueryExecuter;
37  import net.sf.jasperreports.engine.query.QueryExecuterFactory;
38  import net.sf.jasperreports.engine.xml.JRXmlDigesterFactory;
39  import net.sf.jasperreports.engine.xml.JRXmlLoader;
40  
41  import org.efaps.ci.CIAdminProgram;
42  import org.efaps.update.schema.program.AbstractSourceImporter;
43  import org.efaps.update.util.InstallationException;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  import org.xml.sax.SAXException;
47  
48  /**
49   * Class is used to import a JasperReport into the eFaps DataBase.
50   *
51   * @author The eFaps Team
52   * @version $Id$
53   */
54  public class JasperReportImporter
55      extends AbstractSourceImporter
56  {
57      /**
58       * Logging instance used to give logging information of this class.
59       */
60      private static final Logger LOG = LoggerFactory.getLogger(JasperReportImporter.class);
61  
62      /**
63       * Design of the current report.
64       */
65      private JasperDesign jasperDesign;
66  
67      /**
68       * @param _url  url to the file to be imported
69       * @throws InstallationException on error
70       */
71      public JasperReportImporter(final URL _url)
72          throws InstallationException
73      {
74          super(CIAdminProgram.JasperReport, _url);
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      protected void readCode()
82          throws InstallationException
83      {
84          super.readCode();
85          try {
86              DefaultJasperReportsContext.getInstance().setProperty("net.sf.jasperreports.query.executer.factory.eFaps",
87                              FakeQueryExecuterFactory.class.getName());
88  
89              this.jasperDesign = new JRXmlLoader(DefaultJasperReportsContext.getInstance(),
90                              JRXmlDigesterFactory.createDigester()).loadXML(newCodeInputStream());
91          } catch (final ParserConfigurationException e) {
92              throw new InstallationException("source code for " + getUrl() + "could not be parsed", e);
93          } catch (final SAXException e) {
94              throw new InstallationException("source code for " + getUrl() + "could not parsed", e);
95          } catch (final JRException e) {
96              // the error is very useful for the user so print it to the log
97              LOG.error("The file {} cannot be read due to an JRException {}", getUrl(), e);
98              throw new InstallationException("source code for " + getUrl() + "throws JRException", e);
99          } catch (final UnsupportedEncodingException e) {
100             throw new InstallationException("source code for " + getUrl() + "could not encoded", e);
101         }
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     protected String evalProgramName()
109     {
110         return this.jasperDesign.getName();
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     protected String evalRevision()
118     {
119         return "1";
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     protected UUID evalUUID()
127     {
128         return this.jasperDesign.getUUID();
129     }
130 
131     /**
132      * Internal FakeExecuterFactory to be able to set them from esjp.
133      */
134     public static class FakeQueryExecuterFactory
135         implements QueryExecuterFactory
136     {
137 
138         @Override
139         public JRQueryExecuter createQueryExecuter(final JRDataset _dataset,
140                                                    final Map<String, ? extends JRValueParameter> _parameters)
141             throws JRException
142         {
143             return null;
144         }
145 
146         @Override
147         public Object[] getBuiltinParameters()
148         {
149             return null;
150         }
151 
152         @Override
153         public JRQueryExecuter createQueryExecuter(final JasperReportsContext _jasperReportsContext,
154                                                    final JRDataset _dataset,
155                                                    final Map<String, ? extends JRValueParameter> _parameters)
156             throws JRException
157         {
158             return null;
159         }
160 
161         @Override
162         public boolean supportsQueryParameterType(final String _className)
163         {
164             return false;
165         }
166     }
167 }