134dd1e25SAndrew Rist /************************************************************** 2*62edf1a8Smseidel * 334dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 434dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file 534dd1e25SAndrew Rist * distributed with this work for additional information 634dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file 734dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the 834dd1e25SAndrew Rist * "License"); you may not use this file except in compliance 934dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at 10*62edf1a8Smseidel * 1134dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*62edf1a8Smseidel * 1334dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing, 1434dd1e25SAndrew Rist * software distributed under the License is distributed on an 1534dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1634dd1e25SAndrew Rist * KIND, either express or implied. See the License for the 1734dd1e25SAndrew Rist * specific language governing permissions and limitations 1834dd1e25SAndrew Rist * under the License. 19*62edf1a8Smseidel * 2034dd1e25SAndrew Rist *************************************************************/ 2134dd1e25SAndrew Rist 2234dd1e25SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir import com.sun.star.lib.uno.helper.Factory; 25cdf0e10cSrcweir import com.sun.star.lang.XInitialization; 26cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 27cdf0e10cSrcweir import com.sun.star.lang.XSingleComponentFactory; 28cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 29cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 30cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider; 31cdf0e10cSrcweir import com.sun.star.lib.uno.helper.WeakBase; 32cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey; 33cdf0e10cSrcweir import com.sun.star.uno.Type; 34cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 35cdf0e10cSrcweir import com.sun.star.uno.XInterface; 36cdf0e10cSrcweir import com.sun.star.uno.XWeak; 37cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 38cdf0e10cSrcweir import org.openoffice.*; 39cdf0e10cSrcweir 40*62edf1a8Smseidel // additional interfaces used by the implementation 41cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument; 42cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet; 43cdf0e10cSrcweir import com.sun.star.sheet.XCellRangeMovement; 44cdf0e10cSrcweir import com.sun.star.sheet.XFunctionAccess; 45cdf0e10cSrcweir import com.sun.star.container.XIndexAccess; 46cdf0e10cSrcweir import com.sun.star.table.XCellRange; 47cdf0e10cSrcweir import com.sun.star.table.XCell; 48cdf0e10cSrcweir import com.sun.star.table.CellAddress; 49cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress; 50cdf0e10cSrcweir import com.sun.star.table.XColumnRowRange; 51cdf0e10cSrcweir import com.sun.star.table.XTableRows; 52cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 53cdf0e10cSrcweir import com.sun.star.text.XTextRange; 54cdf0e10cSrcweir import com.sun.star.text.XSimpleText; 55cdf0e10cSrcweir import com.sun.star.text.XTextCursor; 56cdf0e10cSrcweir import com.sun.star.text.XText; 57cdf0e10cSrcweir import com.sun.star.text.XTextField; 58cdf0e10cSrcweir 59cdf0e10cSrcweir import java.util.GregorianCalendar; 60cdf0e10cSrcweir import java.util.Calendar; 61cdf0e10cSrcweir import java.util.Vector; 62cdf0e10cSrcweir import java.util.Arrays; 63cdf0e10cSrcweir 64cdf0e10cSrcweir /** This class capsulates the class, that implements the minimal component, a 65cdf0e10cSrcweir * factory for creating the service (<CODE>__getServiceFactory</CODE>) and a 66cdf0e10cSrcweir * method, that writes the information into the given registry key 67cdf0e10cSrcweir * (<CODE>__writeRegistryServiceInfo</CODE>). 68cdf0e10cSrcweir */ 69cdf0e10cSrcweir public class ToDo { 70*62edf1a8Smseidel 71cdf0e10cSrcweir /** This class implements the component. At least the interfaces 72cdf0e10cSrcweir * XInterface, XTypeProvider, and XWeak implemented by the helper class 73cdf0e10cSrcweir * WeakBase and XServiceInfo should be provided by the service. 74cdf0e10cSrcweir */ 75cdf0e10cSrcweir public static class ToDoImpl extends WeakBase implements XServiceInfo, XToDo { 76*62edf1a8Smseidel 77cdf0e10cSrcweir /** The service name, that must be used to get an instance of this service. 78cdf0e10cSrcweir */ 79cdf0e10cSrcweir private static final String __serviceName = "org.openoffice.ToDo"; 80*62edf1a8Smseidel 81cdf0e10cSrcweir /** The initial component contextr, that gives access to 82cdf0e10cSrcweir * the service manager, supported singletons, ... 83cdf0e10cSrcweir * It's often later used 84cdf0e10cSrcweir */ 85cdf0e10cSrcweir private XComponentContext m_cmpCtx; 86cdf0e10cSrcweir 87cdf0e10cSrcweir /** The service manager, that gives access to all registered services. 88cdf0e10cSrcweir * It's often later used 89cdf0e10cSrcweir */ 90cdf0e10cSrcweir private XMultiComponentFactory m_xMCF; 91cdf0e10cSrcweir 92cdf0e10cSrcweir // Implementation helper variables 93cdf0e10cSrcweir static private final int INT_COLUMN_FEATURE = 0; 94cdf0e10cSrcweir static private final int INT_COLUMN_COMMENT = 1; 95cdf0e10cSrcweir static private final int INT_COLUMN_NEEDEDDAYS = 2; 96cdf0e10cSrcweir static private final int INT_COLUMN_STARTDATE = 3; 97cdf0e10cSrcweir static private final int INT_COLUMN_START_DAY_OF_WEEK = 4; 98cdf0e10cSrcweir static private final int INT_COLUMN_ENDDATE = 5; 99cdf0e10cSrcweir static private final int INT_COLUMN_END_DAY_OF_WEEK = 6; 100cdf0e10cSrcweir static private final int INT_COLUMN_DUEDATE = 7; 101cdf0e10cSrcweir static private final int INT_COLUMN_STATUS = 8; 102*62edf1a8Smseidel 103cdf0e10cSrcweir static private final int INT_ROW_FROM = 14; // 8 104*62edf1a8Smseidel 105cdf0e10cSrcweir static private final int INT_ROW_HOLIDAYS_START = 4; 106cdf0e10cSrcweir static private final int INT_COLUMN_HOLIDAYS_START = 7; // 10 107*62edf1a8Smseidel 108cdf0e10cSrcweir static private final String STRING_SEPARATOR = "/"; 109cdf0e10cSrcweir 110*62edf1a8Smseidel 111cdf0e10cSrcweir /** The constructor of the inner class has a XComponenContext parameter. 112cdf0e10cSrcweir * @param xCompContext the initial component context 113cdf0e10cSrcweir */ ToDoImpl(XComponentContext xCompContext)114cdf0e10cSrcweir public ToDoImpl(XComponentContext xCompContext) { 115cdf0e10cSrcweir try { 116cdf0e10cSrcweir m_cmpCtx = xCompContext; 117*62edf1a8Smseidel m_xMCF = m_cmpCtx.getServiceManager(); 118cdf0e10cSrcweir } 119cdf0e10cSrcweir catch( Exception e ) { 120cdf0e10cSrcweir e.printStackTrace(System.err); 121cdf0e10cSrcweir } 122cdf0e10cSrcweir } 123*62edf1a8Smseidel 124cdf0e10cSrcweir /** This method returns an array of all supported service names. 125cdf0e10cSrcweir * @return Array of supported service names. 126cdf0e10cSrcweir */ getSupportedServiceNames()127cdf0e10cSrcweir public String[] getSupportedServiceNames() { 128cdf0e10cSrcweir return getServiceNames(); 129cdf0e10cSrcweir } 130cdf0e10cSrcweir getServiceNames()131cdf0e10cSrcweir public static String[] getServiceNames() { 132cdf0e10cSrcweir String[] sSupportedServiceNames = { __serviceName }; 133cdf0e10cSrcweir return sSupportedServiceNames; 134*62edf1a8Smseidel } 135*62edf1a8Smseidel 136cdf0e10cSrcweir /** This method returns true, if the given service will be 137cdf0e10cSrcweir * supported by the component. 138cdf0e10cSrcweir * @param sService Service name. 139cdf0e10cSrcweir * @return True, if the given service name will be supported. 140cdf0e10cSrcweir */ supportsService(String sServiceName)141cdf0e10cSrcweir public boolean supportsService(String sServiceName) { 142cdf0e10cSrcweir return sServiceName.equals( __serviceName ); 143cdf0e10cSrcweir } 144*62edf1a8Smseidel 145cdf0e10cSrcweir /** Return the class name of the component. 146cdf0e10cSrcweir * @return Class name of the component. 147cdf0e10cSrcweir */ getImplementationName()148cdf0e10cSrcweir public String getImplementationName() { 149cdf0e10cSrcweir return ToDoImpl.class.getName(); 150cdf0e10cSrcweir } 151*62edf1a8Smseidel 152cdf0e10cSrcweir /** For every bug/feature listed in a spreadsheet document this method 153cdf0e10cSrcweir * calculates the start date, day of week of the start date, the end date 154cdf0e10cSrcweir * and the day of week of the end date. All calculations are dependent 155cdf0e10cSrcweir * on the values of "Needed Days", "Due Date" and "Status". The columns 156cdf0e10cSrcweir * "Needed Days" and "Status" are mandatory. The first feature/bug should 157cdf0e10cSrcweir * be placed in row nine. The date to start the calculation should be 158cdf0e10cSrcweir * placed in cell C6. The private holidays should be placed in cell K4/K5 159cdf0e10cSrcweir * and below. All rows will be calculated up to the first empty cell in 160cdf0e10cSrcweir * the first column. If a cell in the column "Due Date" will be colored 161cdf0e10cSrcweir * red, you should take a look at your entries. 162cdf0e10cSrcweir * @param aInstance Spreadsheet document. 163cdf0e10cSrcweir * @throws com.sun.star.uno.RuntimeException This exception could occur 164cdf0e10cSrcweir * at every interface method. 165cdf0e10cSrcweir */ recalc( java.lang.Object aInstance )166cdf0e10cSrcweir public void recalc( java.lang.Object aInstance ) 167cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException { 168cdf0e10cSrcweir try { 169cdf0e10cSrcweir // Querying for the interface XSpreadsheetDocument 170cdf0e10cSrcweir XSpreadsheetDocument xspreadsheetdocument = 171cdf0e10cSrcweir ( XSpreadsheetDocument ) UnoRuntime.queryInterface( 172cdf0e10cSrcweir XSpreadsheetDocument.class, aInstance ); 173*62edf1a8Smseidel 174cdf0e10cSrcweir // Querying for the interface XIndexAccess 175cdf0e10cSrcweir XIndexAccess xindexaccess = ( XIndexAccess ) 176cdf0e10cSrcweir UnoRuntime.queryInterface( XIndexAccess.class, 177cdf0e10cSrcweir xspreadsheetdocument.getSheets() ); 178*62edf1a8Smseidel 179cdf0e10cSrcweir // Getting the first XSpreadsheet 180cdf0e10cSrcweir XSpreadsheet xspreadsheet = (XSpreadsheet)UnoRuntime.queryInterface( 181cdf0e10cSrcweir XSpreadsheet.class, xindexaccess.getByIndex( 0 )); 182*62edf1a8Smseidel 183cdf0e10cSrcweir // Querying for the interface XCellRange on the XSpeadsheet 184cdf0e10cSrcweir XCellRange xcellrange = ( XCellRange ) 185cdf0e10cSrcweir UnoRuntime.queryInterface( XCellRange.class, xspreadsheet ); 186*62edf1a8Smseidel 187*62edf1a8Smseidel /* Getting the Gregorian calendar with the date on which to start 188cdf0e10cSrcweir the calculation */ 189cdf0e10cSrcweir GregorianCalendar gregCalAbsoluteStartDate = 190cdf0e10cSrcweir this.getGregorianCalendarFromString(this.getStringFromCell( 191cdf0e10cSrcweir xcellrange, 5, 2 ) ); 192cdf0e10cSrcweir gregCalAbsoluteStartDate.add( Calendar.DATE, -1 ); 193*62edf1a8Smseidel 194cdf0e10cSrcweir // Set the start date with the absolute start date 195cdf0e10cSrcweir GregorianCalendar gregCalStartDate = 196cdf0e10cSrcweir (GregorianCalendar) gregCalAbsoluteStartDate.clone(); 197*62edf1a8Smseidel 198cdf0e10cSrcweir /* Creating the service FunctionAccess, which allows generic 199cdf0e10cSrcweir access to all spreadsheet functions */ 200cdf0e10cSrcweir Object objectFunctionAccess = 201cdf0e10cSrcweir m_xMCF.createInstanceWithContext( 202cdf0e10cSrcweir "com.sun.star.sheet.FunctionAccess", m_cmpCtx ); 203*62edf1a8Smseidel 204cdf0e10cSrcweir // Querying for the interface XFunctionAccess on service 205cdf0e10cSrcweir // FunctionAccess 206cdf0e10cSrcweir XFunctionAccess xfunctionaccess = (XFunctionAccess) 207cdf0e10cSrcweir UnoRuntime.queryInterface(XFunctionAccess.class, 208cdf0e10cSrcweir objectFunctionAccess ); 209*62edf1a8Smseidel 210cdf0e10cSrcweir // Creating vector for holidays 211cdf0e10cSrcweir Vector vectorHolidays = new Vector(); 212*62edf1a8Smseidel 213cdf0e10cSrcweir // Get the Official Holidays 214cdf0e10cSrcweir this.getOfficialHolidays( vectorHolidays, xcellrange, 215cdf0e10cSrcweir xfunctionaccess, 216cdf0e10cSrcweir gregCalStartDate.get( 217cdf0e10cSrcweir Calendar.YEAR ) ); 218*62edf1a8Smseidel 219cdf0e10cSrcweir // Get the private holidays 220cdf0e10cSrcweir this.getPrivateHolidays(vectorHolidays, xcellrange, 221cdf0e10cSrcweir xfunctionaccess); 222*62edf1a8Smseidel 223cdf0e10cSrcweir // Getting the object array of holidays 224cdf0e10cSrcweir Object[] objectSortedHolidays = vectorHolidays.toArray(); 225*62edf1a8Smseidel 226cdf0e10cSrcweir // Sorting the holidays 227cdf0e10cSrcweir Arrays.sort( objectSortedHolidays ); 228*62edf1a8Smseidel 229cdf0e10cSrcweir // Collect the Official Holidays and the private holidays 230cdf0e10cSrcweir Object [][]objectHolidays = 231cdf0e10cSrcweir new Object[][] { objectSortedHolidays }; 232*62edf1a8Smseidel 233cdf0e10cSrcweir // Row index 234cdf0e10cSrcweir int intRowTo = this.INT_ROW_FROM - 1; 235*62edf1a8Smseidel 236cdf0e10cSrcweir // Getting the feature of the first cell 237cdf0e10cSrcweir String sFeature = this.getStringFromCell(xcellrange, 238cdf0e10cSrcweir intRowTo + 1, 239cdf0e10cSrcweir this.INT_COLUMN_FEATURE); 240*62edf1a8Smseidel 241cdf0e10cSrcweir // Determine the last row with an entry in the first column 242cdf0e10cSrcweir while ( ( sFeature != null ) && 243cdf0e10cSrcweir ( !sFeature.equals( "" ) ) ) { 244cdf0e10cSrcweir intRowTo++; 245cdf0e10cSrcweir sFeature = this.getStringFromCell( xcellrange, 246cdf0e10cSrcweir intRowTo + 1, this.INT_COLUMN_FEATURE ); 247cdf0e10cSrcweir } 248*62edf1a8Smseidel 249cdf0e10cSrcweir // Setting the last row to be calculated 250cdf0e10cSrcweir final int INT_ROW_TO = intRowTo + 1; 251*62edf1a8Smseidel 252cdf0e10cSrcweir // Deleting cells which will be recalculated 253cdf0e10cSrcweir for ( int intRow = this.INT_ROW_FROM; intRow < INT_ROW_TO + 5; 254cdf0e10cSrcweir intRow++ ) { 255cdf0e10cSrcweir for ( int intColumn = this.INT_COLUMN_STARTDATE; 256cdf0e10cSrcweir intColumn <= this.INT_COLUMN_END_DAY_OF_WEEK; 257cdf0e10cSrcweir intColumn++ ) { 258cdf0e10cSrcweir this.setStringToCell(xcellrange, intRow, intColumn, ""); 259cdf0e10cSrcweir } 260cdf0e10cSrcweir } 261*62edf1a8Smseidel 262cdf0e10cSrcweir /* Clearing the background color of the due date cells and setting 263cdf0e10cSrcweir the hyperlink to the bugtracker */ 264cdf0e10cSrcweir for (int intRow = this.INT_ROW_FROM; intRow < INT_ROW_TO; intRow++) 265*62edf1a8Smseidel { 266cdf0e10cSrcweir // Querying for the interface XPropertySet for the cell 267cdf0e10cSrcweir // providing the due date 268cdf0e10cSrcweir XPropertySet xpropertyset = ( XPropertySet ) 269cdf0e10cSrcweir UnoRuntime.queryInterface(XPropertySet.class, 270cdf0e10cSrcweir xcellrange.getCellByPosition( 271cdf0e10cSrcweir this.INT_COLUMN_DUEDATE, 272cdf0e10cSrcweir intRow )); 273*62edf1a8Smseidel 274cdf0e10cSrcweir // Changing the background color of the cell to white 275cdf0e10cSrcweir xpropertyset.setPropertyValue( "CellBackColor", 276cdf0e10cSrcweir new Integer( 16777215 ) ); 277*62edf1a8Smseidel 278cdf0e10cSrcweir // Getting the cell of the bug id 279cdf0e10cSrcweir XCell xcell = xcellrange.getCellByPosition( 280cdf0e10cSrcweir this.INT_COLUMN_FEATURE, intRow ); 281*62edf1a8Smseidel 282cdf0e10cSrcweir // Querying for the interface XSimpleText 283cdf0e10cSrcweir XSimpleText xsimpletext = ( XSimpleText ) 284cdf0e10cSrcweir UnoRuntime.queryInterface( XSimpleText.class, xcell ); 285*62edf1a8Smseidel 286cdf0e10cSrcweir // Getting the text cursor 287cdf0e10cSrcweir XTextCursor xtextcursor = xsimpletext.createTextCursor(); 288*62edf1a8Smseidel 289cdf0e10cSrcweir // Querying for the interface XTextRange 290cdf0e10cSrcweir XTextRange xtextrange = ( XTextRange ) 291cdf0e10cSrcweir UnoRuntime.queryInterface( XTextRange.class, xtextcursor ); 292*62edf1a8Smseidel 293cdf0e10cSrcweir // Getting the bug ID from the cell 294*62edf1a8Smseidel String sBugID = xtextrange.getString(); 295cdf0e10cSrcweir if ( !sBugID.startsWith( 296cdf0e10cSrcweir "http://www.openoffice.org/issues/show_bug.cgi?id=") ) { 297cdf0e10cSrcweir String sBugIDLink = 298cdf0e10cSrcweir "http://www.openoffice.org/issues/show_bug.cgi?id=" + sBugID; 299*62edf1a8Smseidel 300cdf0e10cSrcweir // Querying for the interface XMultiServiceFactory 301cdf0e10cSrcweir XMultiServiceFactory xMSFTextField = 302cdf0e10cSrcweir (XMultiServiceFactory)UnoRuntime.queryInterface( 303cdf0e10cSrcweir XMultiServiceFactory.class, aInstance ); 304*62edf1a8Smseidel 305cdf0e10cSrcweir // Creating an instance of the text field URL 306cdf0e10cSrcweir Object objectTextField = 307cdf0e10cSrcweir xMSFTextField.createInstance( 308cdf0e10cSrcweir "com.sun.star.text.TextField.URL" ); 309*62edf1a8Smseidel 310cdf0e10cSrcweir // Querying for the interface XTextField 311cdf0e10cSrcweir XTextField xtextfield = ( XTextField ) 312cdf0e10cSrcweir UnoRuntime.queryInterface( XTextField.class, 313cdf0e10cSrcweir objectTextField ); 314*62edf1a8Smseidel 315cdf0e10cSrcweir // Querying for the interface XPropertySet 316cdf0e10cSrcweir XPropertySet xpropertysetTextField = ( XPropertySet ) 317cdf0e10cSrcweir UnoRuntime.queryInterface( XPropertySet.class, 318cdf0e10cSrcweir xtextfield ); 319*62edf1a8Smseidel 320cdf0e10cSrcweir // Setting the URL 321cdf0e10cSrcweir xpropertysetTextField.setPropertyValue( "URL", 322cdf0e10cSrcweir sBugIDLink ); 323*62edf1a8Smseidel 324cdf0e10cSrcweir // Setting the representation of the URL 325cdf0e10cSrcweir xpropertysetTextField.setPropertyValue( "Representation", 326cdf0e10cSrcweir sBugID ); 327*62edf1a8Smseidel 328cdf0e10cSrcweir // Querying for the interface XText 329cdf0e10cSrcweir XText xtext = ( XText )UnoRuntime.queryInterface( 330cdf0e10cSrcweir XText.class, xcell ); 331*62edf1a8Smseidel 332cdf0e10cSrcweir // Delete cell content 333cdf0e10cSrcweir xtextrange.setString( "" ); 334*62edf1a8Smseidel 335cdf0e10cSrcweir // Inserting the text field URL to the cell 336cdf0e10cSrcweir xtext.insertTextContent( xtextrange, xtextfield, false ); 337cdf0e10cSrcweir } 338cdf0e10cSrcweir } 339*62edf1a8Smseidel 340cdf0e10cSrcweir // Processing all features/bugs in the table 341cdf0e10cSrcweir for (int intRow = this.INT_ROW_FROM; intRow < INT_ROW_TO; intRow++) 342cdf0e10cSrcweir { 343cdf0e10cSrcweir // Getting the cell of the column "Needed Days" in the 344cdf0e10cSrcweir // current row 345cdf0e10cSrcweir XCell xcell = xcellrange.getCellByPosition( 346cdf0e10cSrcweir INT_COLUMN_NEEDEDDAYS, intRow ); 347cdf0e10cSrcweir 348cdf0e10cSrcweir // Getting the number of needed days to perform the feature 349cdf0e10cSrcweir int intNeededDays = (int) Math.round( xcell.getValue() ); 350*62edf1a8Smseidel 351cdf0e10cSrcweir // Getting the content of a specified cell 352cdf0e10cSrcweir String sStatus = this.getStringFromCell( xcellrange, 353cdf0e10cSrcweir intRow, this.INT_COLUMN_STATUS ); 354*62edf1a8Smseidel 355cdf0e10cSrcweir /* Testing if the number of needed days is greater than 356cdf0e10cSrcweir zero and if 357cdf0e10cSrcweir the status is not "done" */ 358cdf0e10cSrcweir if ( ( intNeededDays > 0 ) 359cdf0e10cSrcweir && !( sStatus.toLowerCase().trim().equals("done")) ) { 360cdf0e10cSrcweir // Getting the start date after a specified number of 361cdf0e10cSrcweir // workdays 362cdf0e10cSrcweir gregCalStartDate = this.getWorkday( 363cdf0e10cSrcweir gregCalStartDate, 1, objectHolidays, 364cdf0e10cSrcweir xfunctionaccess ); 365*62edf1a8Smseidel 366cdf0e10cSrcweir // Getting a string with the date format jjjj-mm-dd from 367*62edf1a8Smseidel // the Gregorian calendar 368cdf0e10cSrcweir String sDate = this.getStringFromGregorianCalendar( 369cdf0e10cSrcweir gregCalStartDate ); 370*62edf1a8Smseidel 371cdf0e10cSrcweir // Set the start date in the specified cell of the table 372cdf0e10cSrcweir this.setStringToCell(xcellrange, intRow, 373cdf0e10cSrcweir this.INT_COLUMN_STARTDATE, sDate); 374*62edf1a8Smseidel 375cdf0e10cSrcweir // For the start day set the day of week in the specified 376cdf0e10cSrcweir // cell of the table 377cdf0e10cSrcweir this.setDayOfWeek( gregCalStartDate, 378cdf0e10cSrcweir xcellrange, intRow, 379cdf0e10cSrcweir this.INT_COLUMN_START_DAY_OF_WEEK ); 380*62edf1a8Smseidel 381cdf0e10cSrcweir // Getting the end date after a specified number of workdays 382cdf0e10cSrcweir GregorianCalendar gregCalEndDate = 383cdf0e10cSrcweir this.getWorkday( gregCalStartDate, 384cdf0e10cSrcweir intNeededDays - 1, 385cdf0e10cSrcweir objectHolidays, xfunctionaccess ); 386*62edf1a8Smseidel 387cdf0e10cSrcweir // Creating a string with the date format jjjj-mm-dd 388cdf0e10cSrcweir sDate = this.getStringFromGregorianCalendar( 389cdf0e10cSrcweir gregCalEndDate ); 390*62edf1a8Smseidel 391cdf0e10cSrcweir // Set the end date in the specified cell of the table 392cdf0e10cSrcweir this.setStringToCell( xcellrange, intRow, 393cdf0e10cSrcweir this.INT_COLUMN_ENDDATE, sDate ); 394*62edf1a8Smseidel 395cdf0e10cSrcweir // For the end day set the day of week in the specified 396cdf0e10cSrcweir // cell of the table 397cdf0e10cSrcweir this.setDayOfWeek(gregCalEndDate, xcellrange, 398cdf0e10cSrcweir intRow, this.INT_COLUMN_END_DAY_OF_WEEK); 399*62edf1a8Smseidel 400cdf0e10cSrcweir // Set the initial date for the next loop 401cdf0e10cSrcweir gregCalStartDate = ( GregorianCalendar ) 402cdf0e10cSrcweir gregCalEndDate.clone(); 403*62edf1a8Smseidel 404cdf0e10cSrcweir // Get the due date from the table 405cdf0e10cSrcweir String sDueDate = this.getStringFromCell( 406cdf0e10cSrcweir xcellrange, intRow, this.INT_COLUMN_DUEDATE ); 407*62edf1a8Smseidel 408cdf0e10cSrcweir // Testing if the due date is not empty 409cdf0e10cSrcweir if ( !sDueDate.equals( "" ) ) { 410cdf0e10cSrcweir GregorianCalendar gregCalDueDate = 411cdf0e10cSrcweir this.getGregorianCalendarFromString(sDueDate); 412*62edf1a8Smseidel 413cdf0e10cSrcweir // Testing if the due date is before the calculated 414cdf0e10cSrcweir // end date 415cdf0e10cSrcweir if ( gregCalDueDate.before( 416cdf0e10cSrcweir gregCalEndDate ) ) { 417cdf0e10cSrcweir /* Getting the date when the processing of the 418cdf0e10cSrcweir feature/bug should 419cdf0e10cSrcweir be started at the latest */ 420cdf0e10cSrcweir GregorianCalendar gregCalLatestDateToStart = 421cdf0e10cSrcweir this.getWorkday(gregCalDueDate, 422cdf0e10cSrcweir -( intNeededDays - 1 ), 423cdf0e10cSrcweir objectHolidays, 424cdf0e10cSrcweir xfunctionaccess); 425*62edf1a8Smseidel 426cdf0e10cSrcweir // Begin with the current row 427cdf0e10cSrcweir int intRowToInsert = intRow; 428*62edf1a8Smseidel 429cdf0e10cSrcweir // Get the start date for the feature/bug in the 430cdf0e10cSrcweir // current row 431cdf0e10cSrcweir GregorianCalendar gregCalPreviousStartDate = 432cdf0e10cSrcweir this.getGregorianCalendarFromString( 433cdf0e10cSrcweir this.getStringFromCell( 434cdf0e10cSrcweir xcellrange, intRowToInsert, 435cdf0e10cSrcweir this.INT_COLUMN_STARTDATE ) ); 436*62edf1a8Smseidel 437cdf0e10cSrcweir // Testing if we have to search for an earlier date 438cdf0e10cSrcweir // to begin 439cdf0e10cSrcweir while ((gregCalLatestDateToStart.before( 440cdf0e10cSrcweir gregCalPreviousStartDate)) && 441cdf0e10cSrcweir (INT_ROW_FROM != intRowToInsert)) { 442cdf0e10cSrcweir // Decrease the row 443cdf0e10cSrcweir intRowToInsert--; 444*62edf1a8Smseidel 445cdf0e10cSrcweir // Get the start date for the feature/bug in 446cdf0e10cSrcweir // the current row 447cdf0e10cSrcweir String sStartDate = this.getStringFromCell( 448cdf0e10cSrcweir xcellrange, intRowToInsert, 449cdf0e10cSrcweir this.INT_COLUMN_STARTDATE ); 450*62edf1a8Smseidel 451cdf0e10cSrcweir // Search until a valid start date is found 452cdf0e10cSrcweir while ( sStartDate.equals( "" ) ) { 453cdf0e10cSrcweir // Decrease the row 454cdf0e10cSrcweir intRowToInsert--; 455*62edf1a8Smseidel 456cdf0e10cSrcweir // Get the start date for the feature/bug 457cdf0e10cSrcweir // in the current row 458cdf0e10cSrcweir sStartDate = this.getStringFromCell( 459cdf0e10cSrcweir xcellrange, intRowToInsert, 460cdf0e10cSrcweir this.INT_COLUMN_STARTDATE ); 461cdf0e10cSrcweir } 462*62edf1a8Smseidel 463cdf0e10cSrcweir // Get the GregorianCalender format for the 464cdf0e10cSrcweir // start date 465cdf0e10cSrcweir gregCalPreviousStartDate = 466cdf0e10cSrcweir this.getGregorianCalendarFromString( 467cdf0e10cSrcweir sStartDate ); 468cdf0e10cSrcweir } 469*62edf1a8Smseidel 470cdf0e10cSrcweir // Getting the cell of the column "Needed Days" 471cdf0e10cSrcweir // in the row where to insert 472cdf0e10cSrcweir XCell xcellNeededDaysWhereToInsert = 473cdf0e10cSrcweir xcellrange.getCellByPosition( 474cdf0e10cSrcweir INT_COLUMN_NEEDEDDAYS, intRowToInsert ); 475cdf0e10cSrcweir // Getting the number of needed days to perform 476cdf0e10cSrcweir // the feature 477cdf0e10cSrcweir int intNeededDaysWhereToInsert = (int) 478cdf0e10cSrcweir Math.round( 479cdf0e10cSrcweir xcellNeededDaysWhereToInsert.getValue()); 480*62edf1a8Smseidel 481cdf0e10cSrcweir GregorianCalendar gregCalPreviousNewEndDate = 482cdf0e10cSrcweir this.getWorkday(gregCalPreviousStartDate, 483cdf0e10cSrcweir intNeededDays - 1 + 484cdf0e10cSrcweir intNeededDaysWhereToInsert, 485cdf0e10cSrcweir objectHolidays, 486*62edf1a8Smseidel xfunctionaccess); 487cdf0e10cSrcweir String sPreviousDueDate = this.getStringFromCell( 488cdf0e10cSrcweir xcellrange, intRowToInsert, 489cdf0e10cSrcweir this.INT_COLUMN_DUEDATE ); 490*62edf1a8Smseidel 491cdf0e10cSrcweir GregorianCalendar gregCalPreviousDueDate = null; 492*62edf1a8Smseidel 493cdf0e10cSrcweir if ( !sPreviousDueDate.equals( "" ) ) { 494cdf0e10cSrcweir gregCalPreviousDueDate = 495cdf0e10cSrcweir this.getGregorianCalendarFromString( 496cdf0e10cSrcweir sPreviousDueDate ); 497cdf0e10cSrcweir } 498*62edf1a8Smseidel 499cdf0e10cSrcweir if ( ( intRowToInsert == intRow ) || 500cdf0e10cSrcweir ( gregCalPreviousNewEndDate.after( 501cdf0e10cSrcweir gregCalPreviousDueDate ) ) ) { 502cdf0e10cSrcweir // Querying for the interface XPropertySet for 503cdf0e10cSrcweir // the cell providing the due date 504cdf0e10cSrcweir XPropertySet xpropertyset = ( XPropertySet ) 505cdf0e10cSrcweir UnoRuntime.queryInterface( 506cdf0e10cSrcweir XPropertySet.class, 507cdf0e10cSrcweir xcellrange.getCellByPosition( 508cdf0e10cSrcweir this.INT_COLUMN_DUEDATE, 509cdf0e10cSrcweir intRow ) ); 510*62edf1a8Smseidel 511cdf0e10cSrcweir // Changing the background color of the cell 512cdf0e10cSrcweir // to red 513cdf0e10cSrcweir xpropertyset.setPropertyValue( 514cdf0e10cSrcweir "CellBackColor", new Integer( 16711680 ) ); 515cdf0e10cSrcweir } else { 516cdf0e10cSrcweir // Querying for the interface XColumnRowRange 517cdf0e10cSrcweir // on the XCellRange 518cdf0e10cSrcweir XColumnRowRange xcolumnrowrange = 519cdf0e10cSrcweir ( XColumnRowRange)UnoRuntime.queryInterface( 520cdf0e10cSrcweir XColumnRowRange.class, xcellrange ); 521cdf0e10cSrcweir // Inserting one row to the table 522cdf0e10cSrcweir XTableRows xTableRows = 523cdf0e10cSrcweir xcolumnrowrange.getRows(); 524cdf0e10cSrcweir xTableRows.insertByIndex( intRowToInsert, 1 ); 525*62edf1a8Smseidel 526cdf0e10cSrcweir // Querying for the interface 527cdf0e10cSrcweir // XCellRangeMovement on XCellRange 528cdf0e10cSrcweir XCellRangeMovement xcellrangemovement = 529cdf0e10cSrcweir (XCellRangeMovement)UnoRuntime.queryInterface( 530cdf0e10cSrcweir XCellRangeMovement.class, xcellrange ); 531*62edf1a8Smseidel 532cdf0e10cSrcweir // Creating the cell address of the destination 533cdf0e10cSrcweir CellAddress celladdress = new CellAddress(); 534cdf0e10cSrcweir celladdress.Sheet = 0; 535cdf0e10cSrcweir celladdress.Column = 0; 536cdf0e10cSrcweir celladdress.Row = intRowToInsert; 537*62edf1a8Smseidel 538cdf0e10cSrcweir // Creating the cell range of the source 539cdf0e10cSrcweir CellRangeAddress cellrangeaddress = 540cdf0e10cSrcweir new CellRangeAddress(); 541cdf0e10cSrcweir cellrangeaddress.Sheet = 0; 542cdf0e10cSrcweir cellrangeaddress.StartColumn = 0; 543cdf0e10cSrcweir cellrangeaddress.StartRow = intRow + 1; 544cdf0e10cSrcweir cellrangeaddress.EndColumn = 8; 545cdf0e10cSrcweir cellrangeaddress.EndRow = intRow + 1; 546*62edf1a8Smseidel 547cdf0e10cSrcweir // Moves the cell range to another position in 548cdf0e10cSrcweir // the document 549cdf0e10cSrcweir xcellrangemovement.moveRange(celladdress, 550cdf0e10cSrcweir cellrangeaddress); 551*62edf1a8Smseidel 552cdf0e10cSrcweir // Removing the row not needed anymore 553cdf0e10cSrcweir xcolumnrowrange.getRows().removeByIndex(intRow 554cdf0e10cSrcweir + 1, 1); 555*62edf1a8Smseidel 556cdf0e10cSrcweir // Set the current row, because we want to 557cdf0e10cSrcweir // recalculate all rows below 558cdf0e10cSrcweir intRow = intRowToInsert - 1; 559*62edf1a8Smseidel 560cdf0e10cSrcweir // Tests at which line we want to insert 561cdf0e10cSrcweir if ( intRow >= this.INT_ROW_FROM ) { 562cdf0e10cSrcweir // Get the start date 563cdf0e10cSrcweir gregCalStartDate = 564cdf0e10cSrcweir this.getGregorianCalendarFromString( 565cdf0e10cSrcweir this.getStringFromCell( xcellrange, 566cdf0e10cSrcweir intRow,this.INT_COLUMN_ENDDATE)); 567cdf0e10cSrcweir } 568cdf0e10cSrcweir else { 569cdf0e10cSrcweir // Set the start date with the absolute s 570cdf0e10cSrcweir // tart date 571cdf0e10cSrcweir gregCalStartDate = (GregorianCalendar) 572cdf0e10cSrcweir gregCalAbsoluteStartDate.clone(); 573cdf0e10cSrcweir } 574cdf0e10cSrcweir } 575cdf0e10cSrcweir } 576cdf0e10cSrcweir } 577cdf0e10cSrcweir } 578cdf0e10cSrcweir } 579cdf0e10cSrcweir } 580cdf0e10cSrcweir catch( Exception exception ) { 581cdf0e10cSrcweir showExceptionMessage( exception ); 582cdf0e10cSrcweir } 583*62edf1a8Smseidel } 584*62edf1a8Smseidel 585*62edf1a8Smseidel /** Getting a string from a Gregorian calendar. 586cdf0e10cSrcweir * @param gregCal Date to be converted. 587*62edf1a8Smseidel * @return string (converted Gregorian calendar). 588cdf0e10cSrcweir */ getStringFromGregorianCalendar( GregorianCalendar gregCal )589cdf0e10cSrcweir public String getStringFromGregorianCalendar( GregorianCalendar gregCal ) { 590cdf0e10cSrcweir String sDate = ( gregCal.get( Calendar.MONTH ) + 1 ) 591cdf0e10cSrcweir + STRING_SEPARATOR + gregCal.get( Calendar.DATE ) 592cdf0e10cSrcweir // + STRING_SEPARATOR + ( gregCal.get( Calendar.MONTH ) + 1 ) 593cdf0e10cSrcweir + STRING_SEPARATOR + gregCal.get( Calendar.YEAR ); 594*62edf1a8Smseidel 595*62edf1a8Smseidel return sDate; 596cdf0e10cSrcweir } 597*62edf1a8Smseidel 598cdf0e10cSrcweir /** Getting a GregorianCalendar from a string. 599cdf0e10cSrcweir * @param sDate String to be converted. 600cdf0e10cSrcweir * @return The result of the converting of the string. 601cdf0e10cSrcweir */ getGregorianCalendarFromString( String sDate )602cdf0e10cSrcweir public GregorianCalendar getGregorianCalendarFromString( String sDate ) { 603cdf0e10cSrcweir int []intDateValue = this.getDateValuesFromString( sDate ); 604*62edf1a8Smseidel 605cdf0e10cSrcweir return( new GregorianCalendar( intDateValue[ 2 ], intDateValue[ 0 ], 606cdf0e10cSrcweir intDateValue[ 1 ] ) ); 607cdf0e10cSrcweir } 608*62edf1a8Smseidel 609cdf0e10cSrcweir /** Getting the day, month and year from a string. 610cdf0e10cSrcweir * @param sDate String to be parsed. 611cdf0e10cSrcweir * @return Returns an array of integer variables. 612cdf0e10cSrcweir */ getDateValuesFromString( String sDate)613cdf0e10cSrcweir public int[] getDateValuesFromString( String sDate) { 614cdf0e10cSrcweir int[] intDateValues = new int[ 3 ]; 615*62edf1a8Smseidel 616cdf0e10cSrcweir int intPositionFirstTag = sDate.indexOf( STRING_SEPARATOR ); 617cdf0e10cSrcweir int intPositionSecondTag = sDate.indexOf(STRING_SEPARATOR, 618cdf0e10cSrcweir intPositionFirstTag + 1); 619*62edf1a8Smseidel 620cdf0e10cSrcweir // Getting the value of the month 621cdf0e10cSrcweir intDateValues[ 0 ] = Integer.parseInt( 622cdf0e10cSrcweir sDate.substring(0, intPositionFirstTag)) - 1; 623cdf0e10cSrcweir // Getting the value of the day 624cdf0e10cSrcweir intDateValues[ 1 ] = Integer.parseInt( 625*62edf1a8Smseidel sDate.substring(intPositionFirstTag + 1, intPositionSecondTag)); 626cdf0e10cSrcweir // Getting the value of the year 627cdf0e10cSrcweir intDateValues[ 2 ] = Integer.parseInt( 628cdf0e10cSrcweir sDate.substring(intPositionSecondTag + 1, sDate.length())); 629*62edf1a8Smseidel 630cdf0e10cSrcweir return intDateValues; 631cdf0e10cSrcweir } 632*62edf1a8Smseidel 633cdf0e10cSrcweir /** Getting a content from a specified cell. 634cdf0e10cSrcweir * @param xcellrange Providing access to cells. 635cdf0e10cSrcweir * @param intRow Number of row. 636cdf0e10cSrcweir * @param intColumn Number of column. 637cdf0e10cSrcweir * @return String from the specified cell. 638cdf0e10cSrcweir */ getStringFromCell( XCellRange xcellrange, int intRow, int intColumn )639cdf0e10cSrcweir public String getStringFromCell( XCellRange xcellrange, int intRow, 640cdf0e10cSrcweir int intColumn ) { 641cdf0e10cSrcweir XTextRange xtextrangeStartDate = null; 642*62edf1a8Smseidel 643cdf0e10cSrcweir try { 644cdf0e10cSrcweir // Getting the cell holding the information about the start date 645cdf0e10cSrcweir XCell xcellStartDate = xcellrange.getCellByPosition(intColumn, 646cdf0e10cSrcweir intRow); 647cdf0e10cSrcweir // Querying for the interface XTextRange on the XCell 648cdf0e10cSrcweir xtextrangeStartDate = (XTextRange) 649cdf0e10cSrcweir UnoRuntime.queryInterface(XTextRange.class, xcellStartDate); 650cdf0e10cSrcweir } 651cdf0e10cSrcweir catch( Exception exception ) { 652cdf0e10cSrcweir this.showExceptionMessage( exception ); 653cdf0e10cSrcweir } 654*62edf1a8Smseidel 655cdf0e10cSrcweir // Getting the start date 656*62edf1a8Smseidel return xtextrangeStartDate.getString().trim(); 657cdf0e10cSrcweir } 658*62edf1a8Smseidel 659cdf0e10cSrcweir /** Writing a specified string to a specified cell. 660cdf0e10cSrcweir * @param xcellrange Providing access to the cells. 661cdf0e10cSrcweir * @param intRow Number of row. 662cdf0e10cSrcweir * @param intColumn Number of column. 663cdf0e10cSrcweir * @param sDate Date to write to the cell. 664cdf0e10cSrcweir */ setStringToCell( XCellRange xcellrange, int intRow, int intColumn, String sDate )665cdf0e10cSrcweir public void setStringToCell( XCellRange xcellrange, int intRow, 666cdf0e10cSrcweir int intColumn, String sDate ) { 667cdf0e10cSrcweir try { 668cdf0e10cSrcweir // Getting the cell holding the information on the day to start 669cdf0e10cSrcweir XCell xcellStartDate = xcellrange.getCellByPosition(intColumn, 670cdf0e10cSrcweir intRow); 671cdf0e10cSrcweir // Querying for the interface XTextRange on the XCell 672cdf0e10cSrcweir XTextRange xtextrange = (XTextRange) 673cdf0e10cSrcweir UnoRuntime.queryInterface(XTextRange.class, xcellStartDate); 674cdf0e10cSrcweir // Setting the new start date 675cdf0e10cSrcweir xtextrange.setString( sDate ); 676cdf0e10cSrcweir } 677cdf0e10cSrcweir catch( Exception exception ) { 678cdf0e10cSrcweir this.showExceptionMessage( exception ); 679cdf0e10cSrcweir } 680cdf0e10cSrcweir } 681*62edf1a8Smseidel 682cdf0e10cSrcweir /** Calculates the week of day and calls the method "setStringToCell". 683cdf0e10cSrcweir * @param gregCal Day to be written to the cell. 684cdf0e10cSrcweir * @param xcellrange Providing access to the cells. 685cdf0e10cSrcweir * @param intRow Number of row. 686cdf0e10cSrcweir * @param intColumn Number of column. 687cdf0e10cSrcweir */ setDayOfWeek( GregorianCalendar gregCal, XCellRange xcellrange, int intRow, int intColumn)688cdf0e10cSrcweir public void setDayOfWeek( GregorianCalendar gregCal, 689cdf0e10cSrcweir XCellRange xcellrange, int intRow, 690cdf0e10cSrcweir int intColumn) { 691cdf0e10cSrcweir int intDayOfWeek = gregCal.get( Calendar.DAY_OF_WEEK ); 692cdf0e10cSrcweir String sDayOfWeek = ""; 693cdf0e10cSrcweir if ( intDayOfWeek == Calendar.MONDAY ) { 694cdf0e10cSrcweir sDayOfWeek = "MON"; 695cdf0e10cSrcweir } else if ( intDayOfWeek == Calendar.TUESDAY ) { 696cdf0e10cSrcweir sDayOfWeek = "TUE"; 697cdf0e10cSrcweir } else if ( intDayOfWeek == Calendar.WEDNESDAY ) { 698cdf0e10cSrcweir sDayOfWeek = "WED"; 699cdf0e10cSrcweir } else if ( intDayOfWeek == Calendar.THURSDAY ) { 700cdf0e10cSrcweir sDayOfWeek = "THU"; 701cdf0e10cSrcweir } else if ( intDayOfWeek == Calendar.FRIDAY ) { 702cdf0e10cSrcweir sDayOfWeek = "FRI"; 703cdf0e10cSrcweir } 704*62edf1a8Smseidel 705cdf0e10cSrcweir this.setStringToCell( xcellrange, intRow, intColumn, 706cdf0e10cSrcweir sDayOfWeek ); 707cdf0e10cSrcweir } 708*62edf1a8Smseidel 709cdf0e10cSrcweir /** Calculates the dates of the official holidays with help of Calc 710cdf0e10cSrcweir * functions. 711cdf0e10cSrcweir * @param vectorHolidays Holding all holidays. 712cdf0e10cSrcweir * @param xcellrange Providing the cells. 713cdf0e10cSrcweir * @param xfunctionaccess Provides access to functions of the Calc. 714cdf0e10cSrcweir * @param intYear Year to calculate the official holidays. 715cdf0e10cSrcweir */ getOfficialHolidays( Vector vectorHolidays, XCellRange xcellrange, XFunctionAccess xfunctionaccess, int intYear )716cdf0e10cSrcweir public void getOfficialHolidays( 717cdf0e10cSrcweir Vector vectorHolidays, 718cdf0e10cSrcweir XCellRange xcellrange, 719cdf0e10cSrcweir XFunctionAccess xfunctionaccess, 720cdf0e10cSrcweir int intYear ) { 721cdf0e10cSrcweir try { 722cdf0e10cSrcweir // Official Holidays for how many years? 723cdf0e10cSrcweir final int intHowManyYears = 2; 724cdf0e10cSrcweir 725cdf0e10cSrcweir // Get the Official Holiday for two years 726cdf0e10cSrcweir for ( int intNumberOfYear = 0; 727cdf0e10cSrcweir intNumberOfYear <= ( intHowManyYears - 1 ); 728cdf0e10cSrcweir intNumberOfYear++ ) { 729cdf0e10cSrcweir intYear += intNumberOfYear; 730*62edf1a8Smseidel 731*62edf1a8Smseidel // Getting the Easter Sunday 732cdf0e10cSrcweir Double dEasterSunday = ( Double ) 733cdf0e10cSrcweir xfunctionaccess.callFunction( 734cdf0e10cSrcweir "EASTERSUNDAY", new Object[] { new Integer(intYear) }); 735*62edf1a8Smseidel 736cdf0e10cSrcweir int intEasterSunday = (int)Math.round( 737cdf0e10cSrcweir dEasterSunday.doubleValue()); 738*62edf1a8Smseidel 739cdf0e10cSrcweir // New-year 740cdf0e10cSrcweir vectorHolidays.addElement( xfunctionaccess.callFunction( 741cdf0e10cSrcweir "DATE", 742cdf0e10cSrcweir new Object[] { 743cdf0e10cSrcweir new Integer( intYear ), 744cdf0e10cSrcweir new Integer( 1 ), 745cdf0e10cSrcweir new Integer( 1 ) } )); 746*62edf1a8Smseidel 747cdf0e10cSrcweir // Good Friday 748cdf0e10cSrcweir vectorHolidays.addElement( 749cdf0e10cSrcweir new Double( intEasterSunday - 2 ) ); 750*62edf1a8Smseidel 751*62edf1a8Smseidel // Easter Monday 752cdf0e10cSrcweir vectorHolidays.addElement( 753cdf0e10cSrcweir new Double( intEasterSunday + 1 ) ); 754*62edf1a8Smseidel 755cdf0e10cSrcweir // Labour Day 756cdf0e10cSrcweir vectorHolidays.addElement( xfunctionaccess.callFunction( 757cdf0e10cSrcweir "DATE", 758cdf0e10cSrcweir new Object[] { 759cdf0e10cSrcweir new Integer( intYear ), 760cdf0e10cSrcweir new Integer( 5 ), 761cdf0e10cSrcweir new Integer( 1 ) } )); 762*62edf1a8Smseidel 763cdf0e10cSrcweir // Ascension Day 764cdf0e10cSrcweir vectorHolidays.addElement(new Double(intEasterSunday + 39 )); 765*62edf1a8Smseidel 766*62edf1a8Smseidel // Pentecost Monday 767cdf0e10cSrcweir vectorHolidays.addElement(new Double(intEasterSunday + 50 )); 768*62edf1a8Smseidel 769cdf0e10cSrcweir // German Unification 770cdf0e10cSrcweir vectorHolidays.addElement( xfunctionaccess.callFunction( 771cdf0e10cSrcweir "DATE", 772cdf0e10cSrcweir new Object[] { 773cdf0e10cSrcweir new Integer( intYear ), 774cdf0e10cSrcweir new Integer( 10 ), 775cdf0e10cSrcweir new Integer( 3 ) } )); 776*62edf1a8Smseidel 777cdf0e10cSrcweir // Christmas Day First 778cdf0e10cSrcweir vectorHolidays.addElement( xfunctionaccess.callFunction( 779cdf0e10cSrcweir "DATE", 780cdf0e10cSrcweir new Object[] { 781cdf0e10cSrcweir new Integer( intYear ), 782cdf0e10cSrcweir new Integer( 12 ), 783cdf0e10cSrcweir new Integer( 25 ) } )); 784*62edf1a8Smseidel 785cdf0e10cSrcweir // Christmas Day Second 786cdf0e10cSrcweir vectorHolidays.addElement( xfunctionaccess.callFunction( 787cdf0e10cSrcweir "DATE", 788cdf0e10cSrcweir new Object[] { 789cdf0e10cSrcweir new Integer( intYear ), 790cdf0e10cSrcweir new Integer( 12 ), 791cdf0e10cSrcweir new Integer( 26 ) } )); 792cdf0e10cSrcweir } 793cdf0e10cSrcweir } 794cdf0e10cSrcweir catch( Exception exception ) { 795cdf0e10cSrcweir this.showExceptionMessage( exception ); 796cdf0e10cSrcweir } 797cdf0e10cSrcweir } 798*62edf1a8Smseidel 799cdf0e10cSrcweir /** Returns the serial number of the date before or after a specified 800cdf0e10cSrcweir * number of workdays. 801cdf0e10cSrcweir * @param gregCalStartDate Date to start with the calculation. 802cdf0e10cSrcweir * @param intDays Number of workdays (e.g. 5 or -3). 803cdf0e10cSrcweir * @param objectHolidays Private and public holidays to take into account. 804cdf0e10cSrcweir * @param xfunctionaccess Allows to call functions from the Calc. 805cdf0e10cSrcweir * @return The gregorian date before or after a specified number of 806cdf0e10cSrcweir * workdays. 807cdf0e10cSrcweir */ getWorkday( GregorianCalendar gregCalStartDate, int intDays, Object[][] objectHolidays, XFunctionAccess xfunctionaccess )808cdf0e10cSrcweir public GregorianCalendar getWorkday( 809cdf0e10cSrcweir GregorianCalendar gregCalStartDate, 810cdf0e10cSrcweir int intDays, Object[][] objectHolidays, 811cdf0e10cSrcweir XFunctionAccess xfunctionaccess ) { 812cdf0e10cSrcweir GregorianCalendar gregCalWorkday = null; 813*62edf1a8Smseidel 814cdf0e10cSrcweir try { 815cdf0e10cSrcweir // Getting the value of the start date 816cdf0e10cSrcweir Double dDate = ( Double ) xfunctionaccess.callFunction( 817cdf0e10cSrcweir "DATE", 818cdf0e10cSrcweir new Object[] { 819cdf0e10cSrcweir new Integer( gregCalStartDate.get( Calendar.YEAR ) ), 820cdf0e10cSrcweir new Integer( gregCalStartDate.get( Calendar.MONTH ) + 1 ), 821cdf0e10cSrcweir new Integer( gregCalStartDate.get( Calendar.DATE ) ) 822cdf0e10cSrcweir } ); 823*62edf1a8Smseidel 824cdf0e10cSrcweir Double dWorkday = ( Double ) xfunctionaccess.callFunction( 825cdf0e10cSrcweir "com.sun.star.sheet.addin.Analysis.getWorkday", 826cdf0e10cSrcweir new Object[] { dDate, new Integer( intDays ), objectHolidays } ); 827*62edf1a8Smseidel 828cdf0e10cSrcweir Double dYear = ( Double ) xfunctionaccess.callFunction( 829cdf0e10cSrcweir "YEAR", new Object[] { dWorkday } ); 830cdf0e10cSrcweir Double dMonth = ( Double ) xfunctionaccess.callFunction( 831cdf0e10cSrcweir "MONTH", new Object[] { dWorkday } ); 832cdf0e10cSrcweir Double dDay = ( Double ) xfunctionaccess.callFunction( 833cdf0e10cSrcweir "DAY", new Object[] { dWorkday } ); 834*62edf1a8Smseidel 835cdf0e10cSrcweir gregCalWorkday = new GregorianCalendar( 836cdf0e10cSrcweir dYear.intValue(), 837cdf0e10cSrcweir dMonth.intValue() - 1, 838cdf0e10cSrcweir dDay.intValue() ); 839cdf0e10cSrcweir } 840cdf0e10cSrcweir catch( Exception exception ) { 841cdf0e10cSrcweir this.showExceptionMessage( exception ); 842cdf0e10cSrcweir } 843*62edf1a8Smseidel 844cdf0e10cSrcweir return gregCalWorkday; 845cdf0e10cSrcweir } 846*62edf1a8Smseidel 847cdf0e10cSrcweir /** Getting the holidays from the spreadsheet. 848cdf0e10cSrcweir * @param vectorHolidays Holding all holidays. 849cdf0e10cSrcweir * @param xcellrange Providing the cells. 850cdf0e10cSrcweir * @param xfunctionaccess Provides the access to functions of the Calc. 851cdf0e10cSrcweir */ getPrivateHolidays( Vector vectorHolidays, XCellRange xcellrange, XFunctionAccess xfunctionaccess )852cdf0e10cSrcweir public void getPrivateHolidays( Vector vectorHolidays, 853cdf0e10cSrcweir XCellRange xcellrange, 854cdf0e10cSrcweir XFunctionAccess xfunctionaccess ) { 855cdf0e10cSrcweir try { 856cdf0e10cSrcweir int intRow = this.INT_ROW_HOLIDAYS_START; 857cdf0e10cSrcweir int intColumn = this.INT_COLUMN_HOLIDAYS_START; 858*62edf1a8Smseidel 859cdf0e10cSrcweir double dHolidayStart = xcellrange.getCellByPosition( 860cdf0e10cSrcweir intColumn, intRow ).getValue(); 861*62edf1a8Smseidel 862cdf0e10cSrcweir double dHolidayEnd = xcellrange.getCellByPosition( 863cdf0e10cSrcweir intColumn + 1, intRow ).getValue(); 864*62edf1a8Smseidel 865cdf0e10cSrcweir while ( dHolidayStart != 0 ) { 866cdf0e10cSrcweir if ( dHolidayEnd == 0 ) { 867cdf0e10cSrcweir vectorHolidays.addElement( 868cdf0e10cSrcweir new Integer( (int) Math.round( 869cdf0e10cSrcweir dHolidayStart ) ) ); 870cdf0e10cSrcweir } 871cdf0e10cSrcweir else { 872cdf0e10cSrcweir for ( int intHoliday = (int) Math.round( 873cdf0e10cSrcweir dHolidayStart ); 874cdf0e10cSrcweir intHoliday <= (int) Math.round( dHolidayEnd ); 875cdf0e10cSrcweir intHoliday++ ) { 876cdf0e10cSrcweir vectorHolidays.addElement( new Double( intHoliday ) ); 877cdf0e10cSrcweir } 878cdf0e10cSrcweir } 879*62edf1a8Smseidel 880cdf0e10cSrcweir intRow++; 881cdf0e10cSrcweir dHolidayStart = xcellrange.getCellByPosition( 882cdf0e10cSrcweir intColumn, intRow).getValue(); 883cdf0e10cSrcweir dHolidayEnd = xcellrange.getCellByPosition( 884cdf0e10cSrcweir intColumn + 1, intRow).getValue(); 885cdf0e10cSrcweir } 886cdf0e10cSrcweir } 887cdf0e10cSrcweir catch( Exception exception ) { 888cdf0e10cSrcweir this.showExceptionMessage( exception ); 889cdf0e10cSrcweir } 890cdf0e10cSrcweir } 891*62edf1a8Smseidel 892cdf0e10cSrcweir /** Showing the stack trace in a JOptionPane. 893cdf0e10cSrcweir * @param sMessage The message to show. 894cdf0e10cSrcweir */ showMessage( String sMessage )895cdf0e10cSrcweir public void showMessage( String sMessage ) { 896cdf0e10cSrcweir javax.swing.JFrame jframe = new javax.swing.JFrame(); 897cdf0e10cSrcweir jframe.setLocation(100, 100); 898cdf0e10cSrcweir jframe.setSize(300, 200); 899cdf0e10cSrcweir jframe.setVisible(true); 900cdf0e10cSrcweir javax.swing.JOptionPane.showMessageDialog( 901cdf0e10cSrcweir jframe, sMessage, "Debugging information", 902cdf0e10cSrcweir javax.swing.JOptionPane.INFORMATION_MESSAGE); 903cdf0e10cSrcweir jframe.dispose(); 904cdf0e10cSrcweir } 905*62edf1a8Smseidel 906cdf0e10cSrcweir /** Writing the stack trace from an exception to a string and calling 907cdf0e10cSrcweir * the method showMessage() with this string. 908a893be29SPedro Giffuni * @param exception The occurred exception. 909cdf0e10cSrcweir * @see showMessage 910cdf0e10cSrcweir */ showExceptionMessage( Exception exception )911cdf0e10cSrcweir public void showExceptionMessage( Exception exception ) { 912cdf0e10cSrcweir java.io.StringWriter swriter = new java.io.StringWriter(); 913cdf0e10cSrcweir java.io.PrintWriter printwriter = 914cdf0e10cSrcweir new java.io.PrintWriter( swriter ); 915cdf0e10cSrcweir exception.printStackTrace( printwriter); 916cdf0e10cSrcweir System.err.println( exception ); 917cdf0e10cSrcweir this.showMessage( swriter.getBuffer().substring(0) ); 918*62edf1a8Smseidel } 919cdf0e10cSrcweir } 920*62edf1a8Smseidel 921cdf0e10cSrcweir /** 922cdf0e10cSrcweir * Gives a factory for creating the service. 923cdf0e10cSrcweir * This method is called by the <code>JavaLoader</code> 924cdf0e10cSrcweir * <p> 925cdf0e10cSrcweir * @return returns a <code>XSingleComponentFactory</code> for creating 926cdf0e10cSrcweir * the component 927cdf0e10cSrcweir * @param sImplName the name of the implementation for which a 928cdf0e10cSrcweir * service is desired 929cdf0e10cSrcweir * @see com.sun.star.comp.loader.JavaLoader 930cdf0e10cSrcweir */ __getComponentFactory(String sImplName)931cdf0e10cSrcweir public static XSingleComponentFactory __getComponentFactory(String sImplName) { 932cdf0e10cSrcweir XSingleComponentFactory xFactory = null; 933*62edf1a8Smseidel 934cdf0e10cSrcweir if ( sImplName.equals( ToDoImpl.class.getName() ) ) 935cdf0e10cSrcweir xFactory = Factory.createComponentFactory(ToDoImpl.class, 936cdf0e10cSrcweir ToDoImpl.getServiceNames()); 937*62edf1a8Smseidel 938cdf0e10cSrcweir return xFactory; 939cdf0e10cSrcweir } 940cdf0e10cSrcweir 941cdf0e10cSrcweir /** 942cdf0e10cSrcweir * Writes the service information into the given registry key. 943cdf0e10cSrcweir * This method is called by the <code>JavaLoader</code> 944cdf0e10cSrcweir * <p> 945cdf0e10cSrcweir * @return returns true if the operation succeeded 946cdf0e10cSrcweir * @param regKey the registryKey 947cdf0e10cSrcweir * @see com.sun.star.comp.loader.JavaLoader 948cdf0e10cSrcweir */ 949cdf0e10cSrcweir // This method not longer necessary since OOo 3.4 where the component registration 950cdf0e10cSrcweir // was changed to passive component registration. For more details see 951*62edf1a8Smseidel // https://wiki.openoffice.org/wiki/Passive_Component_Registration 952cdf0e10cSrcweir 953cdf0e10cSrcweir // public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) { 954cdf0e10cSrcweir // return Factory.writeRegistryServiceInfo(ToDoImpl.class.getName(), 955cdf0e10cSrcweir // ToDoImpl.getServiceNames(), regKey); 956cdf0e10cSrcweir // } 957cdf0e10cSrcweir } 958cdf0e10cSrcweir 959