LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / reportbuilder / java / org / libreoffice / report / SDBCReportData.java
bloba07006bfeb5f962e345ccd396a44612721ddea55
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package org.libreoffice.report;
20 import com.sun.star.beans.UnknownPropertyException;
21 import com.sun.star.beans.XPropertySet;
22 import com.sun.star.container.XIndexAccess;
23 import com.sun.star.container.XNameAccess;
24 import com.sun.star.lang.IndexOutOfBoundsException;
25 import com.sun.star.lang.WrappedTargetException;
26 import com.sun.star.sdb.XParametersSupplier;
27 import com.sun.star.sdbc.DataType;
28 import com.sun.star.sdbc.SQLException;
29 import com.sun.star.sdbc.XResultSetMetaData;
30 import com.sun.star.sdbc.XResultSetMetaDataSupplier;
31 import com.sun.star.sdbc.XRow;
32 import com.sun.star.sdbc.XRowSet;
33 import com.sun.star.sdbcx.XColumnsSupplier;
34 import com.sun.star.uno.Any;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.util.DateTime;
37 import com.sun.star.util.Time;
39 import java.sql.Timestamp;
41 public class SDBCReportData implements DataSource
44 private final XRowSet rowSet;
45 private final XRow row;
46 private int rowCount;
47 private XIndexAccess parameters;
48 private int firstParameterIndex = -1;
49 private int columnCount;
50 private final String[] columnNames;
51 private final int[] columnTypes;
53 public SDBCReportData(final XRowSet rowSet) throws SQLException
55 row = UnoRuntime.queryInterface(XRow.class, rowSet);
56 this.rowSet = rowSet;
58 if (rowSet == null)
60 rowCount = 0;
61 columnCount = 0;
62 columnTypes = new int[1];
63 columnNames = new String[1];
65 else
67 final XParametersSupplier xSuppParams = UnoRuntime.queryInterface(
68 XParametersSupplier.class, rowSet);
69 if (xSuppParams != null)
71 parameters = xSuppParams.getParameters();
74 final XColumnsSupplier columnsSup = UnoRuntime.queryInterface(XColumnsSupplier.class, rowSet);
75 final XNameAccess columns = columnsSup.getColumns();
76 final String[] columnNamesList = columns.getElementNames();
77 final XResultSetMetaDataSupplier sup = UnoRuntime.queryInterface(XResultSetMetaDataSupplier.class, rowSet);
78 final XResultSetMetaData resultSetMetaData = sup.getMetaData();
80 columnCount = resultSetMetaData.getColumnCount();
81 firstParameterIndex = columnCount + 1;
82 if (parameters != null)
84 columnCount += parameters.getCount();
87 columnTypes = new int[columnCount];
88 columnNames = new String[columnCount];
90 for (int i = 1; i <= columnCount; ++i)
92 if (i < firstParameterIndex)
94 columnNames[i - 1] = columnNamesList[i - 1];// resultSetMetaData.getColumnName(i);
95 columnTypes[i - 1] = resultSetMetaData.getColumnType(i);
97 else
99 try
101 final XPropertySet paramColumn = UnoRuntime.queryInterface(
102 XPropertySet.class, parameters.getByIndex(i - firstParameterIndex));
103 columnNames[i - 1] = (String) paramColumn.getPropertyValue("Name");
104 columnTypes[i - 1] = (Integer) paramColumn.getPropertyValue("Type");
106 catch (Exception e)
108 columnNames[i - 1] = "Error";
109 columnTypes[i - 1] = DataType.CHAR;
114 if (rowSet.last())
116 rowCount = rowSet.getRow();
117 rowSet.beforeFirst();
119 else
121 rowCount = 0;
126 public int getColumnCount() throws DataSourceException
128 return columnCount;
131 public int getRowCount()
133 return rowCount;
136 public String getColumnName(final int column) throws DataSourceException
138 return columnNames[column - 1];
141 public boolean absolute(final int row) throws DataSourceException
143 if (rowSet == null)
145 return false;
149 if (row == 0)
151 rowSet.beforeFirst();
152 return true;
154 return rowSet.absolute(row);
156 catch (SQLException e)
158 throw new DataSourceException(e.getMessage(), e);
162 public boolean next() throws DataSourceException
164 if (rowSet == null)
166 return false;
170 return rowSet.next();
172 catch (SQLException e)
174 throw new DataSourceException(e.getMessage(), e);
178 public void close() throws DataSourceException
182 private static java.sql.Date getDate(final Object obj)
184 final java.sql.Date date;
185 if (obj instanceof com.sun.star.util.Date)
187 final com.sun.star.util.Date unodate = (com.sun.star.util.Date) obj;
188 date = java.sql.Date.valueOf(getDateString(unodate.Year, unodate.Month, unodate.Day).toString());
190 else
192 date = null;
194 return date;
197 private static StringBuffer getTimeString(final int hours, final int minutes, final int seconds)
199 final StringBuffer timeString = new StringBuffer();
200 if (hours < 10)
202 timeString.append('0');
204 timeString.append(hours);
205 timeString.append(':');
206 if (minutes < 10)
208 timeString.append('0');
210 timeString.append(minutes);
211 timeString.append(':');
212 if (seconds < 10)
214 timeString.append('0');
216 timeString.append(seconds);
217 return timeString;
220 private static StringBuffer getDateString(final int years, final int months, final int days)
222 final StringBuffer str = new StringBuffer();
223 str.append(years);
224 final StringBuffer str2 = new StringBuffer("0000");
225 str2.delete(0, str.length());
226 str.insert(0, str2);
227 str.append('-');
228 if (months < 10)
230 str.append('0');
232 str.append(months);
233 str.append('-');
234 if (days < 10)
236 str.append('0');
238 str.append(days);
239 return str;
242 private static java.sql.Time getTime(final Object obj)
244 final java.sql.Time time;
245 if (obj instanceof Time)
247 final Time unoTime = (Time) obj;
248 time = java.sql.Time.valueOf(getTimeString(unoTime.Hours, unoTime.Minutes, unoTime.Seconds).toString());
250 else
252 time = null;
254 return time;
257 private static Timestamp getTimestamp(final Object obj)
259 final Timestamp ts;
260 if (obj instanceof DateTime)
262 final DateTime unoTs = (DateTime) obj;
263 final StringBuffer str = getDateString(unoTs.Year, unoTs.Month, unoTs.Day);
264 str.append(' ');
265 str.append(getTimeString(unoTs.Hours, unoTs.Minutes, unoTs.Seconds));
266 str.append('.');
267 str.append(String.format("%09d", unoTs.NanoSeconds));
268 ts = java.sql.Timestamp.valueOf(str.toString());
270 else
272 ts = null;
274 return ts;
277 public Object getObject(final int column) throws DataSourceException
279 if (rowSet == null)
281 return null;
285 final boolean isParameterValue = (parameters != null) && (column >= firstParameterIndex);
286 Object obj;
287 final boolean wasNull;
288 if (isParameterValue)
290 final XPropertySet paramCol = UnoRuntime.queryInterface(
291 XPropertySet.class, parameters.getByIndex(column - firstParameterIndex));
292 obj = paramCol.getPropertyValue("Value");
293 wasNull = obj == null;
295 else
297 obj = row.getObject(column, null);
298 wasNull = row.wasNull();
301 if (wasNull)
303 obj = null;
305 else
307 obj = convertObject(columnTypes[column - 1], obj);
309 return obj;
311 catch (SQLException ex)
313 throw new DataSourceException(ex.getMessage(), ex);
315 catch (UnknownPropertyException ex)
317 throw new DataSourceException(ex.getMessage(), ex);
319 catch (IndexOutOfBoundsException ex)
321 throw new DataSourceException(ex.getMessage(), ex);
323 catch (WrappedTargetException ex)
325 throw new DataSourceException(ex.getMessage(), ex);
329 private Object convertObject(final int type, final Object obj)
331 Object ret;
332 switch (type)
334 case DataType.DATE:
335 ret = getDate(obj);
336 break;
337 case DataType.TIME:
338 ret = getTime(obj);
339 break;
340 case DataType.TIMESTAMP:
341 ret = getTimestamp(obj);
342 break;
343 case DataType.DECIMAL:
344 case DataType.NUMERIC:
345 if (!(obj instanceof Any))
349 ret = new java.math.BigDecimal(String.valueOf(obj));
351 catch (NumberFormatException ex)
353 ret = obj;
356 else
358 ret = obj;
360 break;
361 default:
362 ret = obj;
363 break;
365 return ret;