/* * 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 convwatch; import java.io.File; import java.util.ArrayList; import com.sun.star.beans.PropertyValue; import com.sun.star.beans.XPropertySet; import com.sun.star.container.XNameAccess; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XDesktop; import com.sun.star.frame.XModel; import com.sun.star.frame.XStorable; import com.sun.star.lang.XComponent; import com.sun.star.lang.XMultiServiceFactory; import com.sun.star.sdb.XDocumentDataSource; import com.sun.star.sdb.XOfficeDatabaseDocument; import com.sun.star.sdb.XReportDocumentsSupplier; import com.sun.star.sdb.application.XDatabaseDocumentUI; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XInterface; import com.sun.star.util.XCloseable; import com.sun.star.lang.XServiceInfo; import complexlib.ComplexTestCase; import helper.OfficeProvider; import helper.URLHelper; import helper.OfficeWatcher; class PropertySetHelper { XPropertySet m_xPropertySet; public PropertySetHelper(Object _aObj) { m_xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, _aObj); } /** get a property and don't convert it @param _sName the string name of the property @return the object value of the property without any conversion */ public Object getPropertyValueAsObject(String _sName) { Object aObject = null; if (m_xPropertySet != null) { try { aObject = m_xPropertySet.getPropertyValue(_sName); } catch (com.sun.star.beans.UnknownPropertyException e) { System.out.println("ERROR: UnknownPropertyException caught. '" + _sName + "'"); System.out.println("Message: " + e.getMessage()); } catch (com.sun.star.lang.WrappedTargetException e) { System.out.println("ERROR: WrappedTargetException caught."); System.out.println("Message: " + e.getMessage()); } } return aObject; } } class PropertyHelper { /** Create a PropertyValue[] from a ArrayList @return a PropertyValue[] */ public static PropertyValue[] createPropertyValueArrayFormArrayList(ArrayList _aPropertyList) { // copy the whole PropertyValue List to an PropertyValue Array PropertyValue[] aSaveProperties = null; if (_aPropertyList == null) { aSaveProperties = new PropertyValue[0]; } else { if (_aPropertyList.size() > 0) { aSaveProperties = new PropertyValue[_aPropertyList.size()]; for (int i = 0;i<_aPropertyList.size(); i++) { aSaveProperties[i] = _aPropertyList.get(i); } } else { aSaveProperties = new PropertyValue[0]; } } return aSaveProperties; } } public class ReportDesignerTest extends ComplexTestCase { String mTestDocumentPath; /** * This method returns a list of Strings, each string must be a function name in this class. */ @Override public String[] getTestMethodNames() { return new String[] {"ReportDesignTest"}; // MAIN } private void checkIfOfficeExists(String _sOfficePathWithTrash) { String sOfficePath = ""; int nIndex = _sOfficePathWithTrash.indexOf("soffice.exe"); if (nIndex > 0) { sOfficePath = _sOfficePathWithTrash.substring(0, nIndex + 11); } else { nIndex = _sOfficePathWithTrash.indexOf("soffice"); if (nIndex > 0) { sOfficePath = _sOfficePathWithTrash.substring(0, nIndex + 7); } } sOfficePath = helper.StringHelper.removeQuoteIfExists(sOfficePath); log.println(sOfficePath); File sOffice = new File(sOfficePath); if (! sOffice.exists()) { log.println("ERROR: There exists no office installation at given path: '" + sOfficePath + "'"); System.exit(0); } } private static XDesktop m_xDesktop = null; public static XDesktop getXDesktop() { if (m_xDesktop == null) { try { XInterface xInterface = (XInterface) m_xXMultiServiceFactory.createInstance( "com.sun.star.frame.Desktop" ); m_xDesktop = UnoRuntime.queryInterface(XDesktop.class, xInterface); } catch (com.sun.star.uno.Exception e) { log.println("ERROR: uno.Exception caught"); log.println("Message: " + e.getMessage()); } } return m_xDesktop; } private void showElements(XNameAccess _xNameAccess) { if (_xNameAccess != null) { String[] sElementNames = _xNameAccess.getElementNames(); for(int i=0;i aPropertyList = new ArrayList(); PropertyValue aActiveConnection = new PropertyValue(); aActiveConnection.Name = "ActiveConnection"; aActiveConnection.Value = aActiveConnectionObj; aPropertyList.add(aActiveConnection); loadAndStoreReports(xNameAccess, aPropertyList /*, _nType*/ ); createDBEntry(/*_nType*/); } catch(com.sun.star.uno.Exception e) { log.println("ERROR: Exception caught"); log.println("Message: " + e.getMessage()); } // Close the document closeComponent(xDocComponent); } private String getDocumentPoolName(/*int _nType*/) { return "AutogenReportDesignTest"; } private void createDBEntry(/*int _nType*/) { // try to connect the database String sDBConnection = (String)param.get( convwatch.PropertyName.DB_CONNECTION_STRING ); log.println("DBConnection: " + sDBConnection); DB.init(sDBConnection); String sSourceVersion = m_sSourceVersion; String sSourceName = m_sSourceName; String sSourceCreatorType = ""; String sDestinationName = m_sDestinationName; String sDestinationCreatorType = ""; String sDocumentPoolDir = getOutputPath(/*_nType*/); String sDocumentPoolName = getDocumentPoolName(/*_nType*/); String sSpecial = ""; DB.insertinto_documentcompare(sSourceVersion, sSourceName, sSourceCreatorType, m_sDestinationVersion, sDestinationName, sDestinationCreatorType, sDocumentPoolDir, sDocumentPoolName, m_sMailAddress, sSpecial, m_sParentDistinct); TimeHelper.waitInSeconds(1, "wait for DB."); } private void loadAndStoreReports(XNameAccess _xNameAccess, ArrayList _aPropertyList /*, int _nType*/ ) { if (_xNameAccess != null) { String[] sElementNames = _xNameAccess.getElementNames(); for(int i=0;i aPropertyList = new ArrayList(); // set some properties for storeAsURL PropertyValue aOverwrite = new PropertyValue(); // always overwrite already exist files aOverwrite.Name = "Overwrite"; aOverwrite.Value = Boolean.TRUE; aPropertyList.add(aOverwrite); // store the document in an other directory XStorable aStorable = UnoRuntime.queryInterface( XStorable.class, _xComponent); if (aStorable != null) { log.println("store document as URL: '" + sOutputURL + "'"); try { aStorable.storeAsURL(sOutputURL, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList)); } catch (com.sun.star.io.IOException e) { log.println("ERROR: Exception caught"); log.println("Can't write document URL: '" + sOutputURL + "'"); log.println("Message: " + e.getMessage()); } } } private XComponent loadComponent(String _sName, Object _xComponent, ArrayList _aPropertyList) { XComponent xDocComponent = null; XComponentLoader xComponentLoader = UnoRuntime.queryInterface( XComponentLoader.class, _xComponent ); try { PropertyValue[] aLoadProperties = PropertyHelper.createPropertyValueArrayFormArrayList(_aPropertyList); log.println("Load component: '" + _sName + "'"); xDocComponent = xComponentLoader.loadComponentFromURL(_sName, "_blank", 0, aLoadProperties); log.println("Load component: '" + _sName + "' done"); } catch (com.sun.star.io.IOException e) { log.println("ERROR: Exception caught"); log.println("Can't load document '" + _sName + "'"); log.println("Message: " + e.getMessage()); } catch (com.sun.star.lang.IllegalArgumentException e) { log.println("ERROR: Exception caught"); log.println("Illegal Arguments given to loadComponentFromURL."); log.println("Message: " + e.getMessage()); } return xDocComponent; } private void closeComponent(XComponent _xDoc) { // Close the document XCloseable xCloseable = UnoRuntime.queryInterface(XCloseable.class, _xDoc); try { xCloseable.close(true); } catch (com.sun.star.util.CloseVetoException e) { log.println("ERROR: CloseVetoException caught"); log.println("CloseVetoException occurred Can't close document."); log.println("Message: " + e.getMessage()); } } }