summaryrefslogtreecommitdiff
path: root/reportbuilder/java/org/libreoffice/report/pentaho/SOReportJobFactory.java
blob: 6c70a3425d905ac8b6761e00d0f03824c4f6be1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
package org.libreoffice.report.pentaho;

import com.sun.star.beans.NamedValue;
import com.sun.star.beans.PropertyVetoException;
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertyChangeListener;
import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.XVetoableChangeListener;
import com.sun.star.container.XChild;
import com.sun.star.embed.XStorage;
import com.sun.star.frame.XModel;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.lang.XInitialization;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.lang.XSingleComponentFactory;
import com.sun.star.lib.uno.helper.Factory;
import com.sun.star.lib.uno.helper.PropertySetMixin;
import com.sun.star.lib.uno.helper.WeakBase;
import com.sun.star.registry.InvalidRegistryException;
import com.sun.star.registry.InvalidValueException;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.registry.XSimpleRegistry;
import org.libreoffice.report.DataSourceFactory;
import org.libreoffice.report.JobProperties;
import org.libreoffice.report.ReportEngineParameterNames;
import org.libreoffice.report.ReportExecutionException;
import org.libreoffice.report.ReportJob;
import org.libreoffice.report.ReportJobDefinition;
import org.libreoffice.report.ReportJobFactory;
import org.libreoffice.report.SDBCReportDataFactory;
import org.libreoffice.report.SOImageService;
import org.libreoffice.report.StorageRepository;
import com.sun.star.report.XReportDefinition;
import com.sun.star.sdb.XDocumentDataSource;
import com.sun.star.sdbc.XConnection;
import com.sun.star.sdbc.XRowSet;
import com.sun.star.task.XJob;
import com.sun.star.uno.Exception;
import com.sun.star.uno.Type;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

import java.util.logging.Level;
import java.util.logging.Logger;

import java.io.Writer;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This class capsulates the class, that implements the minimal component, a factory for creating the service
 * (<CODE>__getComponentFactory</CODE>) and a method, that writes the information into the given registry key
 * (<CODE>__writeRegistryServiceInfo</CODE>).
 */
public class SOReportJobFactory
{

    private SOReportJobFactory()
    {
    }

    public static class _SOReportJobFactory extends WeakBase implements XInitialization, XServiceInfo, XJob, XPropertySet, ReportJobFactory
    {

        private static final Log LOGGER = LogFactory.getLog(_SOReportJobFactory.class);
        /**
         * The service name, that must be used to get an instance of this service.
         */
        private static final String __serviceName =
                "org.libreoffice.report.pentaho.SOReportJobFactory";
        private final PropertySetMixin m_prophlp;
        /**
         * The initial component contextr, that gives access to the service manager, supported singletons, ... It's
         * often later used
         */
        private final XComponentContext m_cmpCtx;
        private XConnection activeConnection;
        private XReportDefinition report;

        public _SOReportJobFactory(final XComponentContext xCompContext)
        {
            m_cmpCtx = xCompContext;
            m_prophlp = new PropertySetMixin(m_cmpCtx, this,
                    new Type(XJob.class),
                    null); // no optionals
        }

        /**
         * This method is a member of the interface for initializing an object directly after its creation.
         *
         * @param object This array of arbitrary objects will be passed to the component after its creation.
         * @throws Exception Every exception will not be handled, but will be passed to the caller.
         */
        public void initialize(final Object[] object)
                throws com.sun.star.uno.Exception
        {
            /* The component describes what arguments its expected and in which
             * order!At this point you can read the objects and can initialize
             * your component using these objects.
             */
        }

        /**
         * This method returns an array of all supported service names.
         *
         * @return Array of supported service names.
         */
        public String[] getSupportedServiceNames()
        {
            return getServiceNames();
        }

        /**
         * This method is a simple helper function to used in the static component initialisation functions as well as
         * in getSupportedServiceNames.
         */
        private static String[] getServiceNames()
        {
            return new String[]
                    {
                        __serviceName
                    };
        }

        /**
         * This method returns true, if the given service will be supported by the component.
         *
         * @param sServiceName Service name.
         * @return True, if the given service name will be supported.
         */
        public boolean supportsService(final String sServiceName)
        {
            return sServiceName.equals(__serviceName);
        }

        /**
         * Return the class name of the component.
         *
         * @return Class name of the component.
         */
        public String getImplementationName()
        {
            return SOReportJobFactory.class.getName();
        }

        private String getLocaleFromRegistry(final XSimpleRegistry simpleReg, final String path, final String value)
        {
            String currentLocale = null;
            try
            {
                simpleReg.open(path, true, false);
                final XRegistryKey xRegistryRootKey = simpleReg.getRootKey();
                // read locale
                final XRegistryKey locale = xRegistryRootKey.openKey(value);
                if (locale != null)
                {
                    final String newLocale = locale.getStringValue();
                    if (newLocale != null)
                    {
                        currentLocale = newLocale.replace('-', '_');
                    }
                }
            }
            catch (InvalidValueException ex)
            {
                Logger.getLogger(SOReportJobFactory.class.getName()).log(Level.SEVERE, null, ex);
            }
            catch (InvalidRegistryException ex)
            {
                Logger.getLogger(SOReportJobFactory.class.getName()).log(Level.SEVERE, null, ex);
            }

            return currentLocale;
        }

        public Object execute(final NamedValue[] namedValue)
                throws com.sun.star.lang.IllegalArgumentException, com.sun.star.uno.Exception
        {
            final ClassLoader cl = java.lang.Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
            try
            {
                final XSimpleRegistry simpleReg = UnoRuntime.queryInterface(XSimpleRegistry.class,
                        m_cmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.configuration.ConfigurationRegistry", m_cmpCtx));

                String currentLocale = getLocaleFromRegistry(simpleReg, "org.openoffice.Setup", "L10N/ooSetupSystemLocale");
                if (currentLocale == null || "".equals(currentLocale))
                {
                    currentLocale = getLocaleFromRegistry(simpleReg, "org.openoffice.Office.Linguistic", "General/DefaultLocale");
                }
                if (currentLocale != null && !"".equals(currentLocale))
                {
                    System.setProperty("org.pentaho.reporting.libraries.formula.locale", currentLocale);
                }
                final ReportJob job = createReportJob(namedValue);
                job.execute();

            }
            catch (java.lang.Exception e)
            {
                LOGGER.error("ReportProcessing failed", e);
                Writer result = new StringWriter();
                PrintWriter printWriter = new PrintWriter(result);
                e.printStackTrace(printWriter);
                throw new com.sun.star.lang.WrappedTargetException(e, e.toString() + '\n' + result.toString(), this, null);
            }
            catch (java.lang.IncompatibleClassChangeError e)
            {
                LOGGER.error("Detected an IncompatibleClassChangeError");
                Writer result = new StringWriter();
                PrintWriter printWriter = new PrintWriter(result);
                e.printStackTrace(printWriter);
                throw new com.sun.star.lang.WrappedTargetException(e, e.toString() + '\n' + result.toString(), this, null);
            }
            Thread.currentThread().setContextClassLoader(cl);

            return null;
        }

        public ReportJob createReportJob(final NamedValue[] namedValue) throws IllegalArgumentException, ReportExecutionException, Exception
        {
            XStorage input = null;
            XStorage output = null;
            XRowSet rowSet = null;
            String mimetype = null;
            String author = null;
            String title = null;
            Integer maxRows = null;

            for (int i = 0; i < namedValue.length; ++i)
            {
                final NamedValue aProps = namedValue[i];
                if ("ActiveConnection".equalsIgnoreCase(aProps.Name))
                {
                    activeConnection = UnoRuntime.queryInterface(XConnection.class, aProps.Value);
                }
                else if ("ReportDefinition".equalsIgnoreCase(aProps.Name))
                {
                    report = UnoRuntime.queryInterface(XReportDefinition.class, aProps.Value);
                }
                else if ("InputStorage".equalsIgnoreCase(aProps.Name))
                {
                    input = UnoRuntime.queryInterface(XStorage.class, aProps.Value);
                }
                else if ("OutputStorage".equalsIgnoreCase(aProps.Name))
                {
                    output = UnoRuntime.queryInterface(XStorage.class, aProps.Value);
                }
                else if ("RowSet".equalsIgnoreCase(aProps.Name))
                {
                    rowSet = UnoRuntime.queryInterface(XRowSet.class, aProps.Value);
                }
                else if ("mimetype".equalsIgnoreCase(aProps.Name))
                {
                    mimetype = (String) aProps.Value;
                }
                else if ("MaxRows".equalsIgnoreCase(aProps.Name))
                {
                    maxRows = (Integer) aProps.Value;
                }
                else if (ReportEngineParameterNames.AUTHOR.equalsIgnoreCase(aProps.Name))
                {
                    author = (String) aProps.Value;
                }
                else if (ReportEngineParameterNames.TITLE.equalsIgnoreCase(aProps.Name))
                {
                    title = (String) aProps.Value;
                }
            }

            if (input == null || output == null)
            {
                throw new com.sun.star.lang.IllegalArgumentException();
            }

            if (rowSet == null)
            {
                if (report == null || activeConnection == null)
                {
                    throw new com.sun.star.lang.IllegalArgumentException();
                }
                mimetype = report.getMimeType();
            }
            else
            {
                final XPropertySet set = UnoRuntime.queryInterface(XPropertySet.class, rowSet);
                if (set == null)
                {
                    throw new com.sun.star.lang.IllegalArgumentException();
                }
                activeConnection = UnoRuntime.queryInterface(XConnection.class, set.getPropertyValue("ActiveConnection"));
            }
            if (mimetype == null)
            {
                mimetype = PentahoReportEngineMetaData.OPENDOCUMENT_TEXT;
            }

            final XChild child = UnoRuntime.queryInterface(XChild.class, activeConnection);
            final XDocumentDataSource docSource = UnoRuntime.queryInterface(XDocumentDataSource.class, child.getParent());
            final XModel model = UnoRuntime.queryInterface(XModel.class, docSource.getDatabaseDocument());
            final DataSourceFactory dataFactory = new SDBCReportDataFactory(m_cmpCtx, activeConnection);
            final StorageRepository storageRepository = new StorageRepository(input, output, model.getURL());

            final String inputName = "content.xml";
            final String outputName = "content.xml";

            final PentahoReportEngine engine = new PentahoReportEngine();
            final ReportJobDefinition definition = engine.createJobDefinition();
            final JobProperties procParms = definition.getProcessingParameters();
            procParms.setProperty(ReportEngineParameterNames.INPUT_REPOSITORY, storageRepository);
            procParms.setProperty(ReportEngineParameterNames.OUTPUT_REPOSITORY, storageRepository);
            procParms.setProperty(ReportEngineParameterNames.INPUT_NAME, inputName);
            procParms.setProperty(ReportEngineParameterNames.OUTPUT_NAME, outputName);
            procParms.setProperty(ReportEngineParameterNames.CONTENT_TYPE, mimetype);
            procParms.setProperty(ReportEngineParameterNames.INPUT_DATASOURCE_FACTORY, dataFactory);
            procParms.setProperty(ReportEngineParameterNames.IMAGE_SERVICE, new SOImageService(m_cmpCtx));
            procParms.setProperty(ReportEngineParameterNames.INPUT_REPORTJOB_FACTORY, this);
            procParms.setProperty(ReportEngineParameterNames.MAXROWS, maxRows);
            if (author != null)
            {
                procParms.setProperty(ReportEngineParameterNames.AUTHOR, author);
            }
            if (title != null)
            {
                procParms.setProperty(ReportEngineParameterNames.TITLE, title);
            }

            return engine.createJob(definition);
        }

        // com.sun.star.beans.XPropertySet:
        public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()
        {
            return m_prophlp.getPropertySetInfo();
        }

        public void setPropertyValue(final String aPropertyName, final Object aValue)
                throws UnknownPropertyException, PropertyVetoException, com.sun.star.lang.IllegalArgumentException,
                WrappedTargetException
        {
            m_prophlp.setPropertyValue(aPropertyName, aValue);
        }

        public Object getPropertyValue(final String aPropertyName)
                throws UnknownPropertyException, WrappedTargetException
        {
            return m_prophlp.getPropertyValue(aPropertyName);
        }

        public void addPropertyChangeListener(final String aPropertyName, final XPropertyChangeListener xListener)
                throws UnknownPropertyException, WrappedTargetException
        {
            m_prophlp.addPropertyChangeListener(aPropertyName, xListener);
        }

        public void removePropertyChangeListener(final String aPropertyName, final XPropertyChangeListener xListener)
                throws UnknownPropertyException, WrappedTargetException
        {
            m_prophlp.removePropertyChangeListener(aPropertyName, xListener);
        }

        public void addVetoableChangeListener(final String aPropertyName, final XVetoableChangeListener xListener)
                throws UnknownPropertyException, WrappedTargetException
        {
            m_prophlp.addVetoableChangeListener(aPropertyName, xListener);
        }

        public void removeVetoableChangeListener(final String aPropertyName, final XVetoableChangeListener xListener)
                throws UnknownPropertyException, WrappedTargetException
        {
            m_prophlp.removeVetoableChangeListener(aPropertyName, xListener);
        }
    }

    /**
     * Gives a factory for creating the service. This method is called by the <code>JavaLoader</code>.
     *
     * @param sImplName the name of the implementation for which a service is desired
     * @return returns a <code>XSingleComponentFactory</code> for creating the component
     * @see com.sun.star.comp.loader.JavaLoader
     */
    public static XSingleComponentFactory __getComponentFactory(final String sImplName)
    {
        XSingleComponentFactory xFactory = null;

        try
        {
            if (sImplName.equals(_SOReportJobFactory.class.getName()))
            {
                xFactory = Factory.createComponentFactory(_SOReportJobFactory.class, _SOReportJobFactory.getServiceNames());
            }
            else if (sImplName.equals(SOFunctionManager.class.getName()))
            {
                xFactory = Factory.createComponentFactory(SOFunctionManager.class, SOFunctionManager.getServiceNames());
            }
            else if (sImplName.equals(SOFormulaParser.class.getName()))
            {
                xFactory = Factory.createComponentFactory(SOFormulaParser.class, SOFormulaParser.getServiceNames());
            }
        }
        catch (java.lang.IncompatibleClassChangeError e2)
        {
        }

        return xFactory;
    }

}