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.DataSource;
26 
27 import org.jfree.report.DataSourceException;
28 import org.jfree.report.ReportData;
29 
30 public class StarReportData implements ReportData
31 {
32 
33     private final DataSource dataSource;
34     private int currentRow;
35     private final int rowCount;
36 
StarReportData(final DataSource dataSource)37     public StarReportData(final DataSource dataSource)
38             throws com.sun.star.report.DataSourceException
39     {
40         if (dataSource == null)
41         {
42             throw new NullPointerException();
43         }
44         this.dataSource = dataSource;
45         this.currentRow = 0;
46         this.rowCount = dataSource.getRowCount();
47     }
48 
setCursorPosition(final int row)49     public boolean setCursorPosition(final int row) throws DataSourceException
50     {
51         try
52         {
53             final boolean ret = dataSource.absolute(row);
54             if (ret)
55             {
56                 currentRow = row;
57             }
58             return ret;
59         }
60         catch (com.sun.star.report.DataSourceException e)
61         {
62             throw new DataSourceException("Failed to move cursor", e);
63         }
64     }
65 
close()66     public void close()
67             throws DataSourceException
68     {
69         try
70         {
71             dataSource.close();
72         }
73         catch (com.sun.star.report.DataSourceException e)
74         {
75             throw new DataSourceException("Failed to close datasource", e);
76         }
77     }
78 
getCursorPosition()79     public int getCursorPosition()
80             throws DataSourceException
81     {
82         return currentRow;
83     }
84 
85     /**
86      * This operation checks, whether a call to next will be likely to succeed. If
87      * there is a next data row, this should return true.
88      *
89      * @return
90      * @throws org.jfree.report.DataSourceException
91      *
92      */
isAdvanceable()93     public boolean isAdvanceable() throws DataSourceException
94     {
95         return currentRow < rowCount;
96     }
97 
next()98     public boolean next()
99             throws DataSourceException
100     {
101         try
102         {
103             if (dataSource.next())
104             {
105                 currentRow += 1;
106                 return true;
107             }
108             return false;
109         }
110         catch (com.sun.star.report.DataSourceException e)
111         {
112             throw new DataSourceException("Failed to move cursor", e);
113         }
114     }
115 
get(final int column)116     public Object get(final int column)
117             throws DataSourceException
118     {
119         if (!isReadable())
120         {
121             throw new DataSourceException("Failed to query column.");
122         }
123 
124         try
125         {
126             return dataSource.getObject(column + 1);
127         }
128         catch (com.sun.star.report.DataSourceException e)
129         {
130             throw new DataSourceException("Failed to query column.", e);
131         }
132     }
133 
getColumnCount()134     public int getColumnCount()
135             throws DataSourceException
136     {
137         try
138         {
139             return dataSource.getColumnCount();
140         }
141         catch (com.sun.star.report.DataSourceException e)
142         {
143             throw new DataSourceException("Failed to query column count.", e);
144         }
145     }
146 
getColumnName(final int column)147     public String getColumnName(final int column)
148             throws DataSourceException
149     {
150         try
151         {
152             return dataSource.getColumnName(column + 1);
153         }
154         catch (com.sun.star.report.DataSourceException e)
155         {
156             throw new DataSourceException("Failed to query column name.", e);
157         }
158     }
159 
isReadable()160     public boolean isReadable() throws DataSourceException
161     {
162         return currentRow > 0 && rowCount > 0;
163     }
164 }
165