1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package graphical; 25 26 import com.sun.star.beans.PropertyValue; 27 import com.sun.star.beans.XPropertySet; 28 import com.sun.star.container.XNameAccess; 29 import com.sun.star.frame.FrameSearchFlag; 30 import com.sun.star.frame.XComponentLoader; 31 import com.sun.star.frame.XDesktop; 32 import com.sun.star.frame.XModel; 33 import com.sun.star.frame.XStorable; 34 import com.sun.star.lang.XComponent; 35 import com.sun.star.lang.XMultiServiceFactory; 36 import com.sun.star.lang.XServiceInfo; 37 import com.sun.star.sdb.XOfficeDatabaseDocument; 38 import com.sun.star.sdb.XReportDocumentsSupplier; 39 import com.sun.star.sdb.application.XDatabaseDocumentUI; 40 import com.sun.star.uno.UnoRuntime; 41 import com.sun.star.uno.XInterface; 42 import com.sun.star.util.XCloseable; 43 import complexlib.Assurance; 44 // import complexlib.ComplexTestCase.AssureException; 45 import helper.OfficeProvider; 46 import helper.PropertyHelper; 47 import helper.URLHelper; 48 import java.io.File; 49 import java.util.ArrayList; 50 51 /** 52 * 53 * @author ll93751 54 */ 55 56 class PropertySetHelper 57 { 58 XPropertySet m_xPropertySet; PropertySetHelper(Object _aObj)59 public PropertySetHelper(Object _aObj) 60 { 61 m_xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, _aObj); 62 } 63 64 /** 65 get a property and don't convert it 66 @param _sName the string name of the property 67 @return the object value of the property without any conversion 68 */ getPropertyValueAsObject(String _sName)69 public Object getPropertyValueAsObject(String _sName) 70 { 71 Object aObject = null; 72 73 if (m_xPropertySet != null) 74 { 75 try 76 { 77 aObject = m_xPropertySet.getPropertyValue(_sName); 78 } 79 catch (com.sun.star.beans.UnknownPropertyException e) 80 { 81 System.out.println("ERROR: UnknownPropertyException caught. '" + _sName + "'"); 82 System.out.println("Message: " + e.getMessage()); 83 } 84 catch (com.sun.star.lang.WrappedTargetException e) 85 { 86 System.out.println("ERROR: WrappedTargetException caught."); 87 System.out.println("Message: " + e.getMessage()); 88 } 89 } 90 return aObject; 91 } 92 } 93 94 public class OpenOfficeDatabaseReportExtractor extends Assurance 95 { 96 private ParameterHelper m_aParameterHelper; 97 OpenOfficeDatabaseReportExtractor(ParameterHelper _aParameter)98 public OpenOfficeDatabaseReportExtractor(ParameterHelper _aParameter) 99 { 100 m_aParameterHelper = _aParameter; 101 } 102 103 private XDesktop m_xDesktop = null; getXDesktop()104 private XDesktop getXDesktop() 105 { 106 107 if (m_xDesktop == null) 108 { 109 try 110 { 111 XInterface xInterface = (XInterface) getMultiServiceFactory().createInstance( "com.sun.star.frame.Desktop" ); 112 m_xDesktop = UnoRuntime.queryInterface(XDesktop.class, xInterface); 113 } 114 catch (com.sun.star.uno.Exception e) 115 { 116 GlobalLogWriter.println("ERROR: uno.Exception caught"); 117 GlobalLogWriter.println("Message: " + e.getMessage()); 118 } 119 } 120 return m_xDesktop; 121 } 122 showElements(XNameAccess _xNameAccess)123 private void showElements(XNameAccess _xNameAccess) 124 { 125 if (_xNameAccess != null) 126 { 127 String[] sElementNames = _xNameAccess.getElementNames(); 128 for(int i=0;i<sElementNames.length; i++) 129 { 130 System.out.println("Value: [" + i + "] := " + sElementNames[i]); 131 } 132 } 133 else 134 { 135 System.out.println("Warning: Given object is null."); 136 } 137 } 138 139 140 private OfficeProvider m_aProvider = null; 141 private XMultiServiceFactory m_xMultiServiceFactory = null; getMultiServiceFactory()142 private XMultiServiceFactory getMultiServiceFactory() 143 { 144 if (m_xMultiServiceFactory == null) 145 { 146 m_xMultiServiceFactory = m_aParameterHelper.getMultiServiceFactory(); 147 } 148 return m_xMultiServiceFactory; 149 } 150 // private void startOffice() 151 // { 152 // // int tempTime = m_aParameterHelper.getTestParameters().getInt("SingleTimeOut"); 153 // param.put("TimeOut", new Integer(300000)); 154 // System.out.println("TimeOut: " + param.getInt("TimeOut")); 155 // System.out.println("ThreadTimeOut: " + param.getInt("ThreadTimeOut")); 156 // 157 // // OfficeProvider aProvider = null; 158 // m_aProvider = new OfficeProvider(); 159 // m_xXMultiServiceFactory = (XMultiServiceFactory) m_aProvider.getManager(param); 160 // param.put("ServiceFactory", m_xXMultiServiceFactory); 161 // } 162 // 163 // private void stopOffice() 164 // { 165 // if (m_aProvider != null) 166 // { 167 // m_aProvider.closeExistingOffice(param, true); 168 // m_aProvider = null; 169 // } 170 // TimeHelper.waitInSeconds(2, "Give close Office some time."); 171 // } 172 173 private String m_sMailAddress = null; 174 private String m_sParentDistinct = null; 175 176 // private String m_sUPDMinor; 177 // private String m_sCWS_WORK_STAMP; 178 179 private static String m_sSourceVersion; 180 private static String m_sDestinationVersion; 181 private static String m_sSourceName; 182 private static String m_sDestinationName; 183 184 private static final int WRITER = 1; 185 private static final int CALC = 2; 186 187 /** 188 * This is the main test Function of current ReportDesignerTest 189 * @param _sDocument 190 * @return 191 */ 192 load(String _sDocument )193 public ArrayList<String> load(String _sDocument /*, int _nType*/) 194 { 195 // We need to copy the database file to a place where we have write access, NEVER use the docpool for this 196 String sOutputPath = m_aParameterHelper.getOutputPath(); 197 File aOutputPath = new File(sOutputPath); 198 aOutputPath.mkdirs(); 199 200 String sFilename = FileHelper.getBasename(_sDocument); 201 String sDestinationFile = FileHelper.appendPath(sOutputPath, sFilename); 202 FileHelper.copy(_sDocument, sDestinationFile); 203 204 // now the fix reference of the AbsoluteReferenceFile should exist. 205 assure("There exists no file: " + sDestinationFile, FileHelper.exists(sDestinationFile)); 206 207 String sFileURL = URLHelper.getFileURLFromSystemPath(sDestinationFile); 208 GlobalLogWriter.println("File URL: " + sFileURL); 209 210 ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>(); 211 212 // FYI: it is not allowed to open the document read only 213 // PropertyValue aReadOnly = new PropertyValue(); // always overwrite already exist files 214 // aReadOnly.Name = "ReadOnly"; 215 // aReadOnly.Value = Boolean.TRUE; 216 // aPropertyList.add(aReadOnly); 217 218 XComponent xDocComponent = loadComponent(sFileURL, getXDesktop(), aPropertyList); 219 220 GlobalLogWriter.println("Load done"); 221 // context = createUnoService("com.sun.star.sdb.DatabaseContext") 222 // oDataBase = context.getByName("hh") 223 // oDBDoc = oDataBase.DatabaseDocument 224 // 225 // dim args(1) as new com.sun.star.beans.PropertyValue 226 // args(0).Name = "ActiveConnection" 227 // args(0).Value = oDBDoc.getCurrentController().getPropertyValue("ActiveConnection") 228 // reportContainer = oDBDoc.getReportDocuments() 229 // report = reportContainer.loadComponentFromURL("Report40","",0,args) 230 231 ArrayList<String> aList = null; 232 try 233 { 234 // XInterface x = (XInterface)getMultiServiceFactory().createInstance("com.sun.star.sdb.DatabaseContext"); 235 // assure("can't create instance of com.sun.star.sdb.DatabaseContext", x != null); 236 // GlobalLogWriter.println("createInstance com.sun.star.sdb.DatabaseContext done"); 237 238 // XNameAccess xNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, x); 239 // showElements(xNameAccess); 240 // Object aObj = xNameAccess.getByName(sFileURL); 241 // GlobalLogWriter.println("1"); 242 243 // PropertySetHelper aHelper = new PropertySetHelper(aObj); 244 // XDocumentDataSource xDataSource = (XDocumentDataSource)UnoRuntime.queryInterface(XDocumentDataSource.class, aObj); 245 // Object aDatabaseDocument = aHelper.getPropertyValueAsObject("DatabaseDocument"); 246 // XOfficeDatabaseDocument xOfficeDBDoc = xDataSource.getDatabaseDocument(); 247 248 XOfficeDatabaseDocument xOfficeDBDoc = UnoRuntime.queryInterface(XOfficeDatabaseDocument.class, xDocComponent); 249 250 // XOfficeDatabaseDocument xOfficeDBDoc = (XOfficeDatabaseDocument)UnoRuntime.queryInterface(XOfficeDatabaseDocument.class, xDataSource); 251 assure("can't access DatabaseDocument", xOfficeDBDoc != null); 252 // GlobalLogWriter.println("2"); 253 254 XModel xDBSource = UnoRuntime.queryInterface(XModel.class, xOfficeDBDoc); 255 Object aController = xDBSource.getCurrentController(); 256 assure("Controller of xOfficeDatabaseDocument is empty!", aController != null); 257 // GlobalLogWriter.println("3"); 258 259 XDatabaseDocumentUI aDBDocUI = UnoRuntime.queryInterface(XDatabaseDocumentUI.class, aController); 260 aDBDocUI.connect(); 261 boolean isConnect = aDBDocUI.isConnected(); 262 if (isConnect) 263 { 264 GlobalLogWriter.println("Connection is true"); 265 } 266 else 267 { 268 GlobalLogWriter.println("Connection is false"); 269 } 270 271 // aHelper = new PropertySetHelper(aController); 272 273 XReportDocumentsSupplier xSupplier = UnoRuntime.queryInterface(XReportDocumentsSupplier.class, xOfficeDBDoc); 274 XNameAccess xNameAccess = xSupplier.getReportDocuments(); 275 assure("xOfficeDatabaseDocument returns no Report Document", xNameAccess != null); 276 // GlobalLogWriter.println("5"); 277 278 showElements(xNameAccess); 279 280 // Object aActiveConnectionObj = aHelper.getPropertyValueAsObject("ActiveConnection"); 281 Object aActiveConnectionObj = aDBDocUI.getActiveConnection(); 282 assure("ActiveConnection is empty", aActiveConnectionObj != null); 283 // GlobalLogWriter.println("5"); 284 285 ArrayList<PropertyValue> aPropertyList2 = new ArrayList<PropertyValue>(); 286 287 PropertyValue aActiveConnection = new PropertyValue(); 288 aActiveConnection.Name = "ActiveConnection"; 289 aActiveConnection.Value = aActiveConnectionObj; 290 aPropertyList2.add(aActiveConnection); 291 292 aList = loadAndStoreReports(xNameAccess, aPropertyList2 /*, _nType*/ ); 293 createDBEntry(/*_nType*/); 294 } 295 catch(Exception/*com.sun.star.uno.Exception*/ e) 296 { 297 GlobalLogWriter.println("ERROR: Exception caught"); 298 GlobalLogWriter.println("Message: " + e.getMessage()); 299 } 300 301 // String mTestDocumentPath = (String) param.get("TestDocumentPath"); 302 // System.out.println("mTestDocumentPath: '" + mTestDocumentPath + "'"); 303 // // workaround for issue using deprecated "DOCPTH" prop 304 // System.setProperty("DOCPTH", mTestDocumentPath); 305 306 // Close the document 307 // util.utils.shortWait(2000); 308 309 closeComponent(xDocComponent); 310 return aList; 311 } 312 getDocumentPoolName( )313 private String getDocumentPoolName(/*int _nType*/) 314 { 315 return "AutogenReportDesignTest"; 316 // return getFileFormat(_nType); 317 } 318 319 // ----------------------------------------------------------------------------- createDBEntry( )320 private void createDBEntry(/*int _nType*/) 321 { 322 // try to connect the database 323 String sDBConnection = (String)m_aParameterHelper.getTestParameters().get( convwatch.PropertyName.DB_CONNECTION_STRING ); 324 if (sDBConnection != null && sDBConnection.length() > 0) 325 { 326 GlobalLogWriter.println("DBConnection: " + sDBConnection); 327 // TODO: DB 328 // DB.init(sDBConnection); 329 330 // String sFixRefSubDirectory = "ReportDesign_qa_complex_" + getFileFormat(_nType); 331 String sFixRefSubDirectory = "ReportDesignFixRef"; 332 333 String sSourceVersion = m_sSourceVersion; 334 // String sSourceVersion = sFixRefSubDirectory; 335 String sSourceName = m_sSourceName; 336 // String sSourceCreatorType = "fixref"; 337 String sSourceCreatorType = ""; 338 String sDestinationVersion = m_sDestinationVersion; 339 // if (sDestinationVersion.length() == 0) 340 // { 341 // sDestinationVersion = m_sUPDMinor; 342 // } 343 String sDestinationName = m_sDestinationName; 344 String sDestinationCreatorType = ""; 345 String sDocumentPoolDir = getOutputPath(/*_nType*/); 346 String sDocumentPoolName = getDocumentPoolName(/*_nType*/); 347 String sSpecial = ""; 348 349 // TODO: DB 350 // DB.insertinto_documentcompare(sSourceVersion, sSourceName, sSourceCreatorType, 351 // m_sDestinationVersion, sDestinationName, sDestinationCreatorType, 352 // sDocumentPoolDir, sDocumentPoolName, m_sMailAddress, 353 // sSpecial, m_sParentDistinct); 354 TimeHelper.waitInSeconds(1, "wait for DB."); 355 // DB.test(); 356 // System.exit(1); 357 } 358 } 359 loadAndStoreReports(XNameAccess _xNameAccess, ArrayList<PropertyValue> _aPropertyList )360 private ArrayList<String> loadAndStoreReports(XNameAccess _xNameAccess, ArrayList<PropertyValue> _aPropertyList /*, int _nType*/ ) 361 { 362 ArrayList<String> aList = new ArrayList<String>(); 363 if (_xNameAccess != null) 364 { 365 String[] sElementNames = _xNameAccess.getElementNames(); 366 for(int i=0;i<sElementNames.length; i++) 367 { 368 String sReportName = sElementNames[i]; 369 XComponent xDoc = loadComponent(sReportName, _xNameAccess, _aPropertyList); 370 if (xDoc != null) 371 { 372 // util.utils.shortWait(1000); 373 // print? or store? 374 String sDocumentPathName = storeComponent(sReportName, xDoc /*, _nType*/); 375 aList.add(sDocumentPathName); 376 // util.utils.shortWait(1000); 377 closeComponent(xDoc); 378 // util.utils.shortWait(1000); 379 } 380 else 381 { 382 System.out.println("Leave out maybe due to errors."); 383 } 384 // sBackPath contains the path where to find the extracted ODB Document 385 } 386 } 387 return aList; 388 } 389 getFormatExtension(Object _xComponent )390 private String getFormatExtension(Object _xComponent /* int _nType*/ ) 391 { 392 String sExtension; 393 XServiceInfo xServiceInfo = UnoRuntime.queryInterface( XServiceInfo.class, _xComponent ); 394 if ( xServiceInfo.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) ) 395 { 396 // calc 397 sExtension = ".ods"; 398 } 399 else if (xServiceInfo.supportsService("com.sun.star.text.TextDocument")) 400 { 401 //writer 402 sExtension = ".odt"; 403 } 404 else 405 { 406 sExtension = ".UNKNOWN"; 407 } 408 return sExtension; 409 } 410 411 // switch(_nType) 412 // { 413 // case WRITER: 414 // sExtension = ".odt"; 415 // break; 416 // case CALC: 417 // sExtension = ".ods"; 418 // break; 419 // default: 420 // sExtension = ".UNKNOWN"; 421 // } 422 // return sExtension; 423 // } 424 // private String getFileFormat(int _nType) 425 // { 426 // String sFileType; 427 // switch(_nType) 428 // { 429 // case WRITER: 430 // sFileType = "writer8"; 431 // break; 432 // case CALC: 433 // sFileType = "calc8"; 434 // break; 435 // default: 436 // sFileType = "UNKNOWN"; 437 // } 438 // return sFileType; 439 // } 440 441 private String m_sOutputPath = null; 442 getOutputPath( )443 private String getOutputPath(/*int _nType*/) 444 { 445 if (m_sOutputPath == null) 446 { 447 String sOutputPath = (String)m_aParameterHelper.getTestParameters().get( convwatch.PropertyName.DOC_COMPARATOR_OUTPUT_PATH ); 448 sOutputPath = helper.StringHelper.removeQuoteIfExists(sOutputPath); 449 450 // sOutputPath += "tmp_123"; 451 sOutputPath = FileHelper.appendPath(sOutputPath, DateHelper.getDateTimeForFilename()); 452 // sOutputPath += System.getProperty("file.separator"); 453 454 // sOutputPath += getFileFormat(_nType); 455 // sOutputPath += System.getProperty("file.separator"); 456 457 File aOutputFile = new File(sOutputPath); // create the directory of the given output path 458 aOutputFile.mkdirs(); 459 m_sOutputPath = sOutputPath; 460 } 461 return m_sOutputPath; 462 } 463 464 /* 465 store given _xComponent under the given Name in DOC_COMPARATOR_INPUTPATH 466 */ storeComponent(String _sName, Object _xComponent )467 private String storeComponent(String _sName, Object _xComponent /*, int _nType*/ ) 468 { 469 String sOutputPath = getOutputPath(/*_nType*/); 470 471 // // add DocumentPoolName 472 // sOutputPath = FileHelper.appendPath(sOutputPath, getDocumentPoolName(/*_nType*/)); 473 // // sOutputPath += System.getProperty("file.separator"); 474 // 475 // File aOutputFile = new File(sOutputPath); // create the directory of the given output path 476 // aOutputFile.mkdirs(); 477 478 479 String sName = _sName + getFormatExtension(_xComponent /*_nType*/); 480 sOutputPath = FileHelper.appendPath(sOutputPath, sName); 481 482 // we need the name and path 483 String sBackPathName = sOutputPath; 484 485 String sOutputURL = URLHelper.getFileURLFromSystemPath(sOutputPath); 486 487 ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>(); // set some properties for storeAsURL 488 489 // PropertyValue aFileFormat = new PropertyValue(); 490 // aFileFormat.Name = "FilterName"; 491 // aFileFormat.Value = getFileFormat(_nType); 492 // aPropertyList.add(aFileFormat); 493 494 PropertyValue aOverwrite = new PropertyValue(); // always overwrite already exist files 495 aOverwrite.Name = "Overwrite"; 496 aOverwrite.Value = Boolean.TRUE; 497 aPropertyList.add(aOverwrite); 498 499 // store the document in an other directory 500 XStorable aStorable = UnoRuntime.queryInterface( XStorable.class, _xComponent); 501 if (aStorable != null) 502 { 503 GlobalLogWriter.println("store document as URL: '" + sOutputURL + "'"); 504 try 505 { 506 aStorable.storeAsURL(sOutputURL, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList)); 507 } 508 catch (com.sun.star.io.IOException e) 509 { 510 GlobalLogWriter.println("ERROR: Exception caught"); 511 GlobalLogWriter.println("Can't write document URL: '" + sOutputURL + "'"); 512 GlobalLogWriter.println("Message: " + e.getMessage()); 513 } 514 } 515 return sBackPathName; 516 } 517 loadComponent(String _sName, Object _xComponent, ArrayList _aPropertyList)518 private XComponent loadComponent(String _sName, Object _xComponent, ArrayList _aPropertyList) 519 { 520 XComponent xDocComponent = null; 521 XComponentLoader xComponentLoader = UnoRuntime.queryInterface( XComponentLoader.class, _xComponent ); 522 523 try 524 { 525 PropertyValue[] aLoadProperties = PropertyHelper.createPropertyValueArrayFormArrayList(_aPropertyList); 526 GlobalLogWriter.println("Load component: '" + _sName + "'"); 527 xDocComponent = xComponentLoader.loadComponentFromURL(_sName, "_blank", FrameSearchFlag.ALL, aLoadProperties); 528 GlobalLogWriter.println("Load component: '" + _sName + "' done"); 529 } 530 catch (com.sun.star.io.IOException e) 531 { 532 GlobalLogWriter.println("ERROR: Exception caught"); 533 GlobalLogWriter.println("Can't load document '" + _sName + "'"); 534 GlobalLogWriter.println("Message: " + e.getMessage()); 535 } 536 catch (com.sun.star.lang.IllegalArgumentException e) 537 { 538 GlobalLogWriter.println("ERROR: Exception caught"); 539 GlobalLogWriter.println("Illegal Arguments given to loadComponentFromURL."); 540 GlobalLogWriter.println("Message: " + e.getMessage()); 541 } 542 return xDocComponent; 543 } 544 closeComponent(XComponent _xDoc)545 private void closeComponent(XComponent _xDoc) 546 { 547 // Close the document 548 XCloseable xCloseable = UnoRuntime.queryInterface(XCloseable.class, _xDoc); 549 try 550 { 551 xCloseable.close(true); 552 } 553 catch (com.sun.star.util.CloseVetoException e) 554 { 555 GlobalLogWriter.println("ERROR: CloseVetoException caught"); 556 GlobalLogWriter.println("CloseVetoException occured Can't close document."); 557 GlobalLogWriter.println("Message: " + e.getMessage()); 558 } 559 } 560 561 } 562