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