1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.report.pentaho;
24 
25 import com.sun.star.report.DataSourceException;
26 import com.sun.star.report.DataSourceFactory;
27 
28 import java.util.HashMap;
29 
30 import org.jfree.report.DataSet;
31 import org.jfree.report.ReportData;
32 import org.jfree.report.ReportDataFactory;
33 import org.jfree.report.ReportDataFactoryException;
34 
35 
36 public class StarReportDataFactory implements ReportDataFactory, Cloneable
37 {
38 
39     private final DataSourceFactory backend;
40 
StarReportDataFactory(DataSourceFactory backend)41     public StarReportDataFactory(DataSourceFactory backend)
42     {
43         this.backend = backend;
44     }
45 
46     /**
47      * Queries a datasource. The string 'query' defines the name of the query. The
48      * Parameterset given here may contain more data than actually needed.
49      * <p/>
50      * The dataset may change between two calls, do not assume anything!
51      *
52      * @param query
53      * @param parameters
54      * @return
55      */
queryData(final String query, final DataSet parameters)56     public ReportData queryData(final String query, final DataSet parameters)
57             throws ReportDataFactoryException
58     {
59         try
60         {
61             final HashMap map = new HashMap();
62             final int count = parameters.getColumnCount();
63             for (int i = 0; i < count; i++)
64             {
65                 final Object o = parameters.get(i);
66                 map.put(parameters.getColumnName(i), o);
67             }
68             return new StarReportData(backend.queryData(query, map));
69         }
70         catch (DataSourceException dse)
71         {
72             String message = dse.getMessage();
73             if (message.length() == 0)
74             {
75                 message = "Failed to create report data wrapper";
76             }
77             throw new ReportDataFactoryException(message, dse);
78         }
79         catch (org.jfree.report.DataSourceException e)
80         {
81             String message = e.getMessage();
82             if (message.length() == 0)
83             {
84                 message = "Failed to query data";
85             }
86             throw new ReportDataFactoryException(message);
87         }
88     }
89 
open()90     public void open()
91     {
92     }
93 
close()94     public void close()
95     {
96     }
97 
98     /**
99      * Derives a freshly initialized report data factory, which is independend of
100      * the original data factory. Opening or Closing one data factory must not
101      * affect the other factories.
102      *
103      * @return
104      */
derive()105     public ReportDataFactory derive()
106     {
107         try
108         {
109             return (ReportDataFactory) clone();
110         }
111         catch (CloneNotSupportedException e)
112         {
113             throw new IllegalStateException("Clone failed?");
114         }
115     }
116 
clone()117     public Object clone() throws CloneNotSupportedException
118     {
119         return super.clone();
120     }
121 }
122