User: kz Date: 2008-05-05 13:32:50+0000 Modified: dba/reportdesign/java/com/sun/star/report/SDBCReportDataFactory.java
Log: INTEGRATION: CWS dba30beta (1.4.6); FILE MERGED 2008/04/22 10:30:24 oj 1.4.6.1: #i88503# merge changes from rptchart02 File Changes: Directory: /dba/reportdesign/java/com/sun/star/report/ ====================================================== File [changed]: SDBCReportDataFactory.java Url: http://dba.openoffice.org/source/browse/dba/reportdesign/java/com/sun/star/report/SDBCReportDataFactory.java?r1=1.4&r2=1.5 Delta lines: +178 -142 ----------------------- --- SDBCReportDataFactory.java 2008-04-10 17:16:52+0000 1.4 +++ SDBCReportDataFactory.java 2008-05-05 13:32:48+0000 1.5 @@ -29,8 +29,12 @@ ************************************************************************/ package com.sun.star.report; +import com.sun.star.beans.PropertyVetoException; +import com.sun.star.beans.UnknownPropertyException; import com.sun.star.beans.XPropertySet; import com.sun.star.container.XIndexAccess; +import com.sun.star.lang.IllegalArgumentException; +import com.sun.star.lang.WrappedTargetException; import com.sun.star.sdbc.XConnection; import com.sun.star.container.XNameAccess; import com.sun.star.lang.XComponent; @@ -48,8 +52,10 @@ import com.sun.star.task.XInteractionHandler; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; +import java.util.ArrayList; import java.util.Iterator; -import java.util.Vector; +import java.util.List; +import org.jfree.util.Log; /** * Very primitive implementation, just to show how this could be used ... @@ -58,9 +64,15 @@ public class SDBCReportDataFactory implements DataSourceFactory { - private XRowSet rowSet; - private XConnection connection; - private XComponentContext m_cmpCtx; + public static final String COMMAND_TYPE = "command-type"; + public static final String GROUP_EXPRESSIONS = "group-expressions"; + public static final String MASTER_VALUES = "master-values"; + public static final String DETAIL_COLUMNS = "detail-columns"; + private static final String APPLY_FILTER = "ApplyFilter"; + private static final String UNO_COMMAND = "Command"; + private static final String UNO_COMMAND_TYPE = "CommandType"; + private final XConnection connection; + private final XComponentContext m_cmpCtx; public SDBCReportDataFactory(final XComponentContext cmpCtx, final XConnection connection) { @@ -72,115 +84,35 @@ { try { - rowSet = (XRowSet) UnoRuntime.queryInterface(XRowSet.class, m_cmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.sdb.RowSet", m_cmpCtx)); - final XPropertySet rowSetProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, rowSet); - int commandType = CommandType.TABLE; - final String commandTypeValue = (String) parameters.get("command-type"); + int commandType = CommandType.COMMAND; + final String commandTypeValue = (String) parameters.get(COMMAND_TYPE); + if (commandTypeValue != null) + { if (commandTypeValue.equals("query")) { commandType = CommandType.QUERY; } - else if (commandTypeValue.equals("command")) + else if (commandTypeValue.equals("table")) { - commandType = CommandType.COMMAND; - } - - - rowSetProp.setPropertyValue("ActiveConnection", connection); - rowSetProp.setPropertyValue("CommandType", new Integer(commandType)); - rowSetProp.setPropertyValue("Command", command); - - final String filter = (String) parameters.get("filter"); - if (filter != null) - { - rowSetProp.setPropertyValue("Filter", filter); - rowSetProp.setPropertyValue("ApplyFilter", new Boolean(filter.length() != 0)); + commandType = CommandType.TABLE; } else { - rowSetProp.setPropertyValue("ApplyFilter", new Boolean(false)); - } - final XConnectionTools tools = (XConnectionTools) UnoRuntime.queryInterface(XConnectionTools.class, connection); - String order = getOrderStatement(commandType, command, (Vector) parameters.get("group-expressions")); - if (order.length() > 0) - { - if (commandType != CommandType.TABLE) - { - - XSingleSelectQueryComposer composer = tools.getComposer(commandType, command); - String sCurrentSQL = composer.getQuery(); - // Magic here, read the nice documentation out of the IDL. - composer.setQuery(sCurrentSQL); - String sOldOrder = composer.getOrder(); - if (sOldOrder.length() > 0) - { - order += ","; - order += sOldOrder; - composer.setOrder(""); - final String sQuery = composer.getQuery(); - rowSetProp.setPropertyValue("Command", sQuery); - rowSetProp.setPropertyValue("CommandType", new Integer(CommandType.COMMAND)); - } - } - } - rowSetProp.setPropertyValue("Order", order); - - if (command.length() != 0) - { - Vector masterValues = (Vector) parameters.get("master-values"); - if (masterValues != null && masterValues.size() > 0) - { - // Vector masterColumns = (Vector) parameters.get("master-columns"); - Vector detailColumns = (Vector) parameters.get("detail-columns"); - XSingleSelectQueryComposer composer = tools.getComposer(commandType, command); - StringBuffer oldFilter = new StringBuffer(); - oldFilter.append(composer.getFilter()); - if (oldFilter.length() != 0) - { - oldFilter.append(" AND "); - } - //Iterator masterIt = masterColumns.iterator(); - int newParamterCounter = 1; - for (Iterator it = detailColumns.iterator(); it.hasNext(); ++newParamterCounter) - { - String detail = (String) it.next(); - //String master = (String) masterIt.next(); - oldFilter.append(detail); - oldFilter.append(" = :_"); - oldFilter.append(newParamterCounter); - if (it.hasNext()) - { - oldFilter.append(" AND "); + commandType = CommandType.COMMAND; } } + final XRowSet rowSet = createRowSet(command, commandType, parameters); + final XPropertySet rowSetProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, rowSet); - composer.setFilter(oldFilter.toString()); - - final String sQuery = composer.getQuery(); - rowSetProp.setPropertyValue("Command", sQuery); - rowSetProp.setPropertyValue("CommandType", new Integer(CommandType.COMMAND)); - - int oldParameterCount = 0; - final XParametersSupplier paraSup = (XParametersSupplier) UnoRuntime.queryInterface(XParametersSupplier.class, composer); - if (paraSup != null) - { - final XIndexAccess params = paraSup.getParameters(); - if (params != null) - { - oldParameterCount = params.getCount(); - } - } + final XConnectionTools tools = (XConnectionTools) UnoRuntime.queryInterface(XConnectionTools.class, connection); + fillOrderStatement(command, commandType, parameters, tools, rowSetProp); - final XParameters para = (XParameters) UnoRuntime.queryInterface(XParameters.class, rowSet); - for (int i = 0; i < masterValues.size(); i++) + if (command.length() != 0) { - final Object object = masterValues.elementAt(i); - para.setObject(oldParameterCount + i, object); - } - } + final int oldParameterCount = fillParameter(parameters, tools, command, commandType, rowSet); - XCompletedExecution execute = (XCompletedExecution) UnoRuntime.queryInterface(XCompletedExecution.class, rowSet); - if (execute != null) + final XCompletedExecution execute = (XCompletedExecution) UnoRuntime.queryInterface(XCompletedExecution.class, rowSet); + if (execute != null && oldParameterCount > 0) { final XInteractionHandler handler = (XInteractionHandler) UnoRuntime.queryInterface(XInteractionHandler.class, m_cmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.sdb.InteractionHandler", m_cmpCtx)); execute.executeWithCompletion(handler); @@ -198,25 +130,22 @@ } } - private String getOrderStatement(final int commandType, final String command, final Vector groupExpressions) + private String getOrderStatement(final int commandType, final String command, final List groupExpressions) { - StringBuffer order = new StringBuffer(); + final StringBuffer order = new StringBuffer(); final int count = groupExpressions.size(); - if (count == 0) + if (count != 0) { - return order.toString(); - } try { final String quote = connection.getMetaData().getIdentifierQuoteString(); final XConnectionTools tools = (XConnectionTools) UnoRuntime.queryInterface(XConnectionTools.class, connection); - XComponent[] hold = new XComponent[2]; - XNameAccess columns = tools.getFieldsByCommandDescriptor(commandType, command, hold); - + final XComponent[] hold = new XComponent[2]; + final XNameAccess columns = tools.getFieldsByCommandDescriptor(commandType, command, hold); for (int i = 0; i < count; i++) { - Object[] pair = (Object[]) groupExpressions.elementAt(i); + final Object[] pair = (Object[]) groupExpressions.get(i); String expression = (String) pair[0]; if (columns.hasByName(expression)) @@ -229,30 +158,137 @@ order.append(expression); if (order.length() > 0) { - order.append(" "); + order.append(' '); } - String sorting = (String) pair[1]; - if (sorting == null || sorting.equals("false")) + final String sorting = (String) pair[1]; + if (sorting == null || sorting.equals(OfficeToken.FALSE)) { order.append("DESC"); } if ((i + 1) < count) { - order.append(","); + order.append(' '); } } } - } catch (IndexOutOfBoundsException ex) { - ex.printStackTrace(); + Log.error("ReportProcessing failed", ex); } - catch (SQLException sQLException) + catch (SQLException ex) { - sQLException.printStackTrace(); + Log.error("ReportProcessing failed", ex); + } } - return order.toString(); } + + int fillParameter(final Map parameters, final XConnectionTools tools, final String command, final int commandType, final XRowSet rowSet) throws SQLException, UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException + { + int oldParameterCount = 0; + final ArrayList masterValues = (ArrayList) parameters.get(MASTER_VALUES); + if (masterValues != null && !masterValues.isEmpty()) + { + // Vector masterColumns = (Vector) parameters.get("master-columns"); + final ArrayList detailColumns = (ArrayList) parameters.get(DETAIL_COLUMNS); + final XSingleSelectQueryComposer composer = tools.getComposer(commandType, command); + + // get old parameter count + final XParametersSupplier paraSup = (XParametersSupplier) UnoRuntime.queryInterface(XParametersSupplier.class, composer); + if (paraSup != null) + { + final XIndexAccess params = paraSup.getParameters(); + if (params != null) + { + oldParameterCount = params.getCount(); + } + } + // create the new filter + final String quote = connection.getMetaData().getIdentifierQuoteString(); + final StringBuffer oldFilter = new StringBuffer(); + oldFilter.append(composer.getFilter()); + if (oldFilter.length() != 0) + { + oldFilter.append(" AND "); + } + + int newParamterCounter = 1; + + for (final Iterator it = detailColumns.iterator(); it.hasNext(); ++newParamterCounter) + { + final String detail = (String) it.next(); + //String master = (String) masterIt.next(); + oldFilter.append(quote); + oldFilter.append(detail); + oldFilter.append(quote); + oldFilter.append(" = :link_"); + oldFilter.append(newParamterCounter); + if (it.hasNext()) + { + oldFilter.append(" AND "); + } + } + + composer.setFilter(oldFilter.toString()); + + final String sQuery = composer.getQuery(); + final XPropertySet rowSetProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, rowSet); + rowSetProp.setPropertyValue(UNO_COMMAND, sQuery); + rowSetProp.setPropertyValue(UNO_COMMAND_TYPE, new Integer(CommandType.COMMAND)); + + final XParameters para = (XParameters) UnoRuntime.queryInterface(XParameters.class, rowSet); + for (int i = 0; i < masterValues.size(); i++) + { + final Object object = masterValues.get(i); + para.setObject(oldParameterCount + i + 1, object); + } + } + return oldParameterCount; + } + + final XRowSet createRowSet(final String command, final int commandType, final Map parameters) throws Exception + { + final XRowSet rowSet = (XRowSet) UnoRuntime.queryInterface(XRowSet.class, m_cmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.sdb.RowSet", m_cmpCtx)); + final XPropertySet rowSetProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, rowSet); + + rowSetProp.setPropertyValue("ActiveConnection", connection); + rowSetProp.setPropertyValue(UNO_COMMAND_TYPE, new Integer(commandType)); + rowSetProp.setPropertyValue(UNO_COMMAND, command); + + final String filter = (String) parameters.get("filter"); + if (filter != null) + { + rowSetProp.setPropertyValue("Filter", filter); + rowSetProp.setPropertyValue(APPLY_FILTER, Boolean.valueOf(filter.length() != 0)); + } + else + { + rowSetProp.setPropertyValue(APPLY_FILTER, Boolean.FALSE); + } + return rowSet; + } + + void fillOrderStatement(final String command, final int commandType, final Map parameters, final XConnectionTools tools, final XPropertySet rowSetProp) throws SQLException, UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException + { + final StringBuffer order = new StringBuffer(getOrderStatement(commandType, command, (ArrayList) parameters.get(GROUP_EXPRESSIONS))); + if (order.length() > 0 && commandType != CommandType.TABLE) + { + final XSingleSelectQueryComposer composer = tools.getComposer(commandType, command); + final String sCurrentSQL = composer.getQuery(); + // Magic here, read the nice documentation out of the IDL. + composer.setQuery(sCurrentSQL); + final String sOldOrder = composer.getOrder(); + if (sOldOrder.length() > 0) + { + order.append(','); + order.append(sOldOrder); + composer.setOrder(""); + final String sQuery = composer.getQuery(); + rowSetProp.setPropertyValue(UNO_COMMAND, sQuery); + rowSetProp.setPropertyValue(UNO_COMMAND_TYPE, new Integer(CommandType.COMMAND)); + } + } + rowSetProp.setPropertyValue("Order", order.toString()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
