bump product version to 4.1.6.2
[LibreOffice.git] / qadevOOo / runner / graphical / OpenOfficeDatabaseReportExtractor.java
blob217a4cb327a73fc063cf84e547a7833a0aa840ae
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 .
19 package graphical;
21 import com.sun.star.beans.PropertyValue;
22 import com.sun.star.beans.XPropertySet;
23 import com.sun.star.container.XNameAccess;
24 import com.sun.star.frame.FrameSearchFlag;
25 import com.sun.star.frame.XComponentLoader;
26 import com.sun.star.frame.XDesktop;
27 import com.sun.star.frame.XModel;
28 import com.sun.star.frame.XStorable;
29 import com.sun.star.lang.XComponent;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.lang.XServiceInfo;
32 import com.sun.star.sdb.XOfficeDatabaseDocument;
33 import com.sun.star.sdb.XReportDocumentsSupplier;
34 import com.sun.star.sdb.application.XDatabaseDocumentUI;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XInterface;
37 import com.sun.star.util.XCloseable;
38 import complexlib.Assurance;
39 import helper.PropertyHelper;
40 import helper.URLHelper;
41 import java.io.File;
42 import java.util.ArrayList;
44 class PropertySetHelper
46 XPropertySet m_xPropertySet;
47 public PropertySetHelper(Object _aObj)
49 m_xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, _aObj);
52 /**
53 get a property and don't convert it
54 @param _sName the string name of the property
55 @return the object value of the property without any conversion
57 public Object getPropertyValueAsObject(String _sName)
59 Object aObject = null;
61 if (m_xPropertySet != null)
63 try
65 aObject = m_xPropertySet.getPropertyValue(_sName);
67 catch (com.sun.star.beans.UnknownPropertyException e)
69 System.out.println("ERROR: UnknownPropertyException caught. '" + _sName + "'");
70 System.out.println("Message: " + e.getMessage());
72 catch (com.sun.star.lang.WrappedTargetException e)
74 System.out.println("ERROR: WrappedTargetException caught.");
75 System.out.println("Message: " + e.getMessage());
78 return aObject;
82 public class OpenOfficeDatabaseReportExtractor extends Assurance
84 private ParameterHelper m_aParameterHelper;
86 public OpenOfficeDatabaseReportExtractor(ParameterHelper _aParameter)
88 m_aParameterHelper = _aParameter;
91 private XDesktop m_xDesktop = null;
92 private XDesktop getXDesktop()
95 if (m_xDesktop == null)
97 try
99 XInterface xInterface = (XInterface) getMultiServiceFactory().createInstance( "com.sun.star.frame.Desktop" );
100 m_xDesktop = UnoRuntime.queryInterface(XDesktop.class, xInterface);
102 catch (com.sun.star.uno.Exception e)
104 GlobalLogWriter.println("ERROR: uno.Exception caught");
105 GlobalLogWriter.println("Message: " + e.getMessage());
108 return m_xDesktop;
111 private void showElements(XNameAccess _xNameAccess)
113 if (_xNameAccess != null)
115 String[] sElementNames = _xNameAccess.getElementNames();
116 for(int i=0;i<sElementNames.length; i++)
118 System.out.println("Value: [" + i + "] := " + sElementNames[i]);
121 else
123 System.out.println("Warning: Given object is null.");
128 private XMultiServiceFactory m_xMultiServiceFactory = null;
129 private XMultiServiceFactory getMultiServiceFactory()
131 if (m_xMultiServiceFactory == null)
133 m_xMultiServiceFactory = m_aParameterHelper.getMultiServiceFactory();
135 return m_xMultiServiceFactory;
139 * This is the main test Function of current ReportDesignerTest
140 * @param _sDocument
141 * @return
144 public ArrayList<String> load(String _sDocument /*, int _nType*/)
146 // We need to copy the database file to a place where we have write access, NEVER use the docpool for this
147 String sOutputPath = m_aParameterHelper.getOutputPath();
148 File aOutputPath = new File(sOutputPath);
149 aOutputPath.mkdirs();
151 String sFilename = FileHelper.getBasename(_sDocument);
152 String sDestinationFile = FileHelper.appendPath(sOutputPath, sFilename);
153 FileHelper.copy(_sDocument, sDestinationFile);
155 // now the fix reference of the AbsoluteReferenceFile should exist.
156 assure("There exists no file: " + sDestinationFile, FileHelper.exists(sDestinationFile));
158 String sFileURL = URLHelper.getFileURLFromSystemPath(sDestinationFile);
159 GlobalLogWriter.println("File URL: " + sFileURL);
161 ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>();
163 XComponent xDocComponent = loadComponent(sFileURL, getXDesktop(), aPropertyList);
165 GlobalLogWriter.println("Load done");
167 ArrayList<String> aList = null;
170 XOfficeDatabaseDocument xOfficeDBDoc = UnoRuntime.queryInterface(XOfficeDatabaseDocument.class, xDocComponent);
172 assure("can't access DatabaseDocument", xOfficeDBDoc != null);
174 XModel xDBSource = UnoRuntime.queryInterface(XModel.class, xOfficeDBDoc);
175 Object aController = xDBSource.getCurrentController();
176 assure("Controller of xOfficeDatabaseDocument is empty!", aController != null);
178 XDatabaseDocumentUI aDBDocUI = UnoRuntime.queryInterface(XDatabaseDocumentUI.class, aController);
179 aDBDocUI.connect();
180 boolean isConnect = aDBDocUI.isConnected();
181 if (isConnect)
183 GlobalLogWriter.println("Connection is true");
185 else
187 GlobalLogWriter.println("Connection is false");
190 XReportDocumentsSupplier xSupplier = UnoRuntime.queryInterface(XReportDocumentsSupplier.class, xOfficeDBDoc);
191 XNameAccess xNameAccess = xSupplier.getReportDocuments();
192 assure("xOfficeDatabaseDocument returns no Report Document", xNameAccess != null);
194 showElements(xNameAccess);
196 Object aActiveConnectionObj = aDBDocUI.getActiveConnection();
197 assure("ActiveConnection is empty", aActiveConnectionObj != null);
199 ArrayList<PropertyValue> aPropertyList2 = new ArrayList<PropertyValue>();
201 PropertyValue aActiveConnection = new PropertyValue();
202 aActiveConnection.Name = "ActiveConnection";
203 aActiveConnection.Value = aActiveConnectionObj;
204 aPropertyList2.add(aActiveConnection);
206 aList = loadAndStoreReports(xNameAccess, aPropertyList2);
207 createDBEntry();
209 catch(Exception e)
211 GlobalLogWriter.println("ERROR: Exception caught");
212 GlobalLogWriter.println("Message: " + e.getMessage());
215 closeComponent(xDocComponent);
216 return aList;
219 private String getDocumentPoolName()
221 return "AutogenReportDesignTest";
224 private void createDBEntry()
226 // try to connect the database
227 String sDBConnection = (String)m_aParameterHelper.getTestParameters().get( convwatch.PropertyName.DB_CONNECTION_STRING );
228 if (sDBConnection != null && sDBConnection.length() > 0)
230 GlobalLogWriter.println("DBConnection: " + sDBConnection);
232 getOutputPath();
233 getDocumentPoolName();
234 TimeHelper.waitInSeconds(1, "wait for DB.");
238 private ArrayList<String> loadAndStoreReports(XNameAccess _xNameAccess, ArrayList<PropertyValue> _aPropertyList)
240 ArrayList<String> aList = new ArrayList<String>();
241 if (_xNameAccess != null)
243 String[] sElementNames = _xNameAccess.getElementNames();
244 for(int i=0;i<sElementNames.length; i++)
246 String sReportName = sElementNames[i];
247 XComponent xDoc = loadComponent(sReportName, _xNameAccess, _aPropertyList);
248 if (xDoc != null)
250 String sDocumentPathName = storeComponent(sReportName, xDoc);
251 aList.add(sDocumentPathName);
252 closeComponent(xDoc);
254 else
256 System.out.println("Leave out maybe due to errors.");
258 // sBackPath contains the path where to find the extracted ODB Document
261 return aList;
264 private String getFormatExtension(Object _xComponent)
266 String sExtension;
267 XServiceInfo xServiceInfo = UnoRuntime.queryInterface( XServiceInfo.class, _xComponent );
268 if ( xServiceInfo.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
270 // calc
271 sExtension = ".ods";
273 else if (xServiceInfo.supportsService("com.sun.star.text.TextDocument"))
275 //writer
276 sExtension = ".odt";
278 else
280 sExtension = ".UNKNOWN";
282 return sExtension;
285 private String m_sOutputPath = null;
287 private String getOutputPath()
289 if (m_sOutputPath == null)
291 String sOutputPath = (String)m_aParameterHelper.getTestParameters().get( convwatch.PropertyName.DOC_COMPARATOR_OUTPUT_PATH );
292 sOutputPath = helper.StringHelper.removeQuoteIfExists(sOutputPath);
294 sOutputPath = FileHelper.appendPath(sOutputPath, DateHelper.getDateTimeForFilename());
296 File aOutputFile = new File(sOutputPath); // create the directory of the given output path
297 aOutputFile.mkdirs();
298 m_sOutputPath = sOutputPath;
300 return m_sOutputPath;
304 store given _xComponent under the given Name in DOC_COMPARATOR_INPUTPATH
306 private String storeComponent(String _sName, Object _xComponent)
308 String sOutputPath = getOutputPath();
310 String sName = _sName + getFormatExtension(_xComponent);
311 sOutputPath = FileHelper.appendPath(sOutputPath, sName);
313 // we need the name and path
314 String sBackPathName = sOutputPath;
316 String sOutputURL = URLHelper.getFileURLFromSystemPath(sOutputPath);
318 ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>(); // set some properties for storeAsURL
320 PropertyValue aOverwrite = new PropertyValue(); // always overwrite already exist files
321 aOverwrite.Name = "Overwrite";
322 aOverwrite.Value = Boolean.TRUE;
323 aPropertyList.add(aOverwrite);
325 // store the document in an other directory
326 XStorable aStorable = UnoRuntime.queryInterface( XStorable.class, _xComponent);
327 if (aStorable != null)
329 GlobalLogWriter.println("store document as URL: '" + sOutputURL + "'");
332 aStorable.storeAsURL(sOutputURL, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList));
334 catch (com.sun.star.io.IOException e)
336 GlobalLogWriter.println("ERROR: Exception caught");
337 GlobalLogWriter.println("Can't write document URL: '" + sOutputURL + "'");
338 GlobalLogWriter.println("Message: " + e.getMessage());
341 return sBackPathName;
344 private XComponent loadComponent(String _sName, Object _xComponent, ArrayList<PropertyValue> _aPropertyList)
346 XComponent xDocComponent = null;
347 XComponentLoader xComponentLoader = UnoRuntime.queryInterface( XComponentLoader.class, _xComponent );
351 PropertyValue[] aLoadProperties = PropertyHelper.createPropertyValueArrayFormArrayList(_aPropertyList);
352 GlobalLogWriter.println("Load component: '" + _sName + "'");
353 xDocComponent = xComponentLoader.loadComponentFromURL(_sName, "_blank", FrameSearchFlag.ALL, aLoadProperties);
354 GlobalLogWriter.println("Load component: '" + _sName + "' done");
356 catch (com.sun.star.io.IOException e)
358 GlobalLogWriter.println("ERROR: Exception caught");
359 GlobalLogWriter.println("Can't load document '" + _sName + "'");
360 GlobalLogWriter.println("Message: " + e.getMessage());
362 catch (com.sun.star.lang.IllegalArgumentException e)
364 GlobalLogWriter.println("ERROR: Exception caught");
365 GlobalLogWriter.println("Illegal Arguments given to loadComponentFromURL.");
366 GlobalLogWriter.println("Message: " + e.getMessage());
368 return xDocComponent;
371 private void closeComponent(XComponent _xDoc)
373 // Close the document
374 XCloseable xCloseable = UnoRuntime.queryInterface(XCloseable.class, _xDoc);
377 xCloseable.close(true);
379 catch (com.sun.star.util.CloseVetoException e)
381 GlobalLogWriter.println("ERROR: CloseVetoException caught");
382 GlobalLogWriter.println("CloseVetoException occurred Can't close document.");
383 GlobalLogWriter.println("Message: " + e.getMessage());