summaryrefslogtreecommitdiff
path: root/odk/examples/java/ToDo/ToDo.java
blob: fa457156978ae655b20746533b2cd3e38448f965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  the BSD license.
 *
 *  Copyright 2000, 2010 Oracle and/or its affiliates.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *************************************************************************/

import com.sun.star.lib.uno.helper.Factory;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XSingleComponentFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.lib.uno.helper.WeakBase;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
// addintional interfaces used by the implementation
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.sheet.XCellRangeMovement;
import com.sun.star.sheet.XFunctionAccess;
import com.sun.star.container.XIndexAccess;
import com.sun.star.table.XCellRange;
import com.sun.star.table.XCell;
import com.sun.star.table.CellAddress;
import com.sun.star.table.CellRangeAddress;
import com.sun.star.table.XColumnRowRange;
import com.sun.star.table.XTableRows;
import com.sun.star.beans.XPropertySet;
import com.sun.star.text.XTextRange;
import com.sun.star.text.XSimpleText;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XText;
import com.sun.star.text.XTextField;

import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Vector;
import java.util.Arrays;

/** This class capsulates the class, that implements the minimal component, a
 * factory for creating the service (<CODE>__getServiceFactory</CODE>) and a
 * method, that writes the information into the given registry key
 * (<CODE>__writeRegistryServiceInfo</CODE>).
 */
public class ToDo {

    /** This class implements the component. At least the interfaces
     * XInterface, XTypeProvider, and XWeak implemented by the helper class
     * WeakBase and XServiceInfo should be provided by the service.
     */
    public static class ToDoImpl extends WeakBase implements XServiceInfo, XToDo {

        /** The service name, that must be used to get an instance of this service.
         */
        private static final String __serviceName = "org.openoffice.ToDo";

        /** The initial component contextr, that gives access to
         * the service manager, supported singletons, ...
         * It's often later used
         */
        private XComponentContext m_cmpCtx;

        /** The service manager, that gives access to all registered services.
         * It's often later used
         */
        private XMultiComponentFactory m_xMCF;

        // Implementation helper variables
        static private final int INT_COLUMN_FEATURE = 0;
        static private final int INT_COLUMN_COMMENT = 1;
        static private final int INT_COLUMN_NEEDEDDAYS = 2;
        static private final int INT_COLUMN_STARTDATE = 3;
        static private final int INT_COLUMN_START_DAY_OF_WEEK = 4;
        static private final int INT_COLUMN_ENDDATE = 5;
        static private final int INT_COLUMN_END_DAY_OF_WEEK = 6;
        static private final int INT_COLUMN_DUEDATE = 7;
        static private final int INT_COLUMN_STATUS = 8;

        static private final int INT_ROW_FROM = 14; // 8

        static private final int INT_ROW_HOLIDAYS_START = 4;
        static private final int INT_COLUMN_HOLIDAYS_START = 7; // 10

        static private final String STRING_SEPARATOR = "/";


        /** The constructor of the inner class has a XComponenContext parameter.
         * @param xCompContext the initial component context
         */
        public ToDoImpl(XComponentContext xCompContext) {
            try {
                m_cmpCtx = xCompContext;
                m_xMCF = m_cmpCtx.getServiceManager();
            }
            catch( Exception e ) {
                e.printStackTrace(System.err);
            }
        }

        /** This method returns an array of all supported service names.
         * @return Array of supported service names.
         */
        public String[] getSupportedServiceNames() {
            return getServiceNames();
        }

        public static String[] getServiceNames() {
            String[] sSupportedServiceNames = { __serviceName };
            return sSupportedServiceNames;
        }

        /** This method returns true, if the given service will be
         * supported by the component.
         * @param sService Service name.
         * @return True, if the given service name will be supported.
         */
        public boolean supportsService(String sServiceName) {
            return sServiceName.equals( __serviceName );
        }

        /** Return the class name of the component.
         * @return Class name of the component.
         */
        public String getImplementationName() {
            return ToDoImpl.class.getName();
        }

        /** For every bug/feature listed in a spreadsheet document this method
         * calculates the start date, day of week of the start date, the end date
         * and the day of week of the end date. All calculations are dependent
         * on the values of "Needed Days", "Due Date" and "Status". The columns
         * "Needed Days" and "Status" are mandatory. The first feature/bug should
         * be placed in row nine. The date to start the calculation should be
         * placed in cell C6. The private holidays should be placed in cell K4/K5
         * and below. All rows will be calculated up to the first empty cell in
         * the first column. If a cell in the column "Due Date" will be colored
         * red, you should take a look at your entries.
         * @param aInstance Spreadsheet document.
         * @throws com.sun.star.uno.RuntimeException This exception could occur
         *                                           at every interface method.
         */
        public void recalc( java.lang.Object aInstance )
            throws com.sun.star.uno.RuntimeException {
            try {
                // Querying for the interface XSpreadsheetDocument
                XSpreadsheetDocument xspreadsheetdocument =
                    UnoRuntime.queryInterface(
                    XSpreadsheetDocument.class, aInstance );

                // Querying for the interface XIndexAccess
                XIndexAccess xindexaccess = UnoRuntime.queryInterface( XIndexAccess.class,
                                             xspreadsheetdocument.getSheets() );

                // Getting the first XSpreadsheet
                XSpreadsheet xspreadsheet = UnoRuntime.queryInterface(
                    XSpreadsheet.class, xindexaccess.getByIndex( 0 ));

                // Querying for the interface XCellRange on the XSpeadsheet
                XCellRange xcellrange = UnoRuntime.queryInterface( XCellRange.class, xspreadsheet );

                /* Getting the gregorian calendar with the date on which to start
                   the calculation */
                GregorianCalendar gregCalAbsoluteStartDate =
                    this.getGregorianCalendarFromString(this.getStringFromCell(
                                                            xcellrange, 5, 2 ) );
                gregCalAbsoluteStartDate.add( Calendar.DATE, -1 );

                // Set the start date with the absolute start date
                GregorianCalendar gregCalStartDate =
                (GregorianCalendar) gregCalAbsoluteStartDate.clone();

                /* Creating the service FunctionAccess, which allows generic
                   access to all spreadsheet functions */
                Object objectFunctionAccess =
                    m_xMCF.createInstanceWithContext(
                        "com.sun.star.sheet.FunctionAccess", m_cmpCtx );

                // Querying for the interface XFunctionAccess on service
                // FunctionAccess
                XFunctionAccess xfunctionaccess = UnoRuntime.queryInterface(XFunctionAccess.class,
                                                  objectFunctionAccess );

                // Creating vector for holidays
                Vector<Object> vectorHolidays = new Vector<Object>();

                // Get the Official Holidays
                this.getOfficialHolidays( vectorHolidays, xcellrange,
                                          xfunctionaccess,
                                          gregCalStartDate.get(
                                              Calendar.YEAR ) );

                // Get the private holidays
                this.getPrivateHolidays(vectorHolidays, xcellrange,
                                        xfunctionaccess);

                // Getting the object array of holidays
                Object[] objectSortedHolidays = vectorHolidays.toArray();

                // Sorting the holidays
                Arrays.sort( objectSortedHolidays );

                // Collect the Official Holidays and the private holidays
                Object [][]objectHolidays =
                new Object[][] { objectSortedHolidays };

                // Row index
                int intRowTo = ToDoImpl.INT_ROW_FROM - 1;

                // Getting the feature of the first cell
                String sFeature = this.getStringFromCell(xcellrange,
                                                         intRowTo + 1,
                                                         ToDoImpl.INT_COLUMN_FEATURE);

                // Determine the last row with an entry in the first column
                while ( ( sFeature != null ) &&
                        ( !sFeature.equals( "" ) ) ) {
                    intRowTo++;
                    sFeature = this.getStringFromCell( xcellrange,
                    intRowTo + 1, ToDoImpl.INT_COLUMN_FEATURE );
                }

                // Setting the last row to be calculated
                final int INT_ROW_TO = intRowTo + 1;

                // Deleting cells which will be recalculated
                for ( int intRow = ToDoImpl.INT_ROW_FROM; intRow < INT_ROW_TO + 5;
                      intRow++ ) {
                    for ( int intColumn = ToDoImpl.INT_COLUMN_STARTDATE;
                          intColumn <= ToDoImpl.INT_COLUMN_END_DAY_OF_WEEK;
                          intColumn++ ) {
                        this.setStringToCell(xcellrange, intRow, intColumn, "");
                    }
                }

                /* Clearing the background color of the due date cells and setting
                   the hyperlink to the bugtracker */
                for (int intRow = ToDoImpl.INT_ROW_FROM; intRow < INT_ROW_TO; intRow++)
                {
                    // Querying for the interface XPropertySet for the cell
                    // providing the due date
                    XPropertySet xpropertyset = UnoRuntime.queryInterface(XPropertySet.class,
                                                  xcellrange.getCellByPosition(
                                                  ToDoImpl.INT_COLUMN_DUEDATE,
                                                  intRow ));

                    // Changing the background color of the cell to white
                    xpropertyset.setPropertyValue( "CellBackColor",
                    new Integer( 16777215 ) );

                    // Getting the cell of the bug id
                    XCell xcell = xcellrange.getCellByPosition(
                    ToDoImpl.INT_COLUMN_FEATURE, intRow );

                    // Querying for the interface XSimpleText
                    XSimpleText xsimpletext = UnoRuntime.queryInterface( XSimpleText.class, xcell );

                    // Getting the text cursor
                    XTextCursor xtextcursor = xsimpletext.createTextCursor();

                    // Querying for the interface XTextRange
                    XTextRange xtextrange = UnoRuntime.queryInterface( XTextRange.class, xtextcursor );

                    // Getting the bug ID from the cell
                    String sBugID = xtextrange.getString();
                    if ( !sBugID.startsWith(
                             "http://www.openoffice.org/issues/show_bug.cgi?id=") ) {
                        String sBugIDLink =
                            "http://www.openoffice.org/issues/show_bug.cgi?id=" + sBugID;

                        // Querying for the interface XMultiServiceFactory
                        XMultiServiceFactory xMSFTextField =
                            UnoRuntime.queryInterface(
                            XMultiServiceFactory.class, aInstance );

                        // Creating an instance of the text field URL
                        Object objectTextField =
                            xMSFTextField.createInstance(
                                "com.sun.star.text.TextField.URL" );

                        // Querying for the interface XTextField
                        XTextField xtextfield = UnoRuntime.queryInterface( XTextField.class,
                                                   objectTextField );

                        // Querying for the interface XPropertySet
                        XPropertySet xpropertysetTextField = UnoRuntime.queryInterface( XPropertySet.class,
                                                   xtextfield );

                        // Setting the URL
                        xpropertysetTextField.setPropertyValue( "URL",
                                                                sBugIDLink );

                        // Setting the representation of the URL
                        xpropertysetTextField.setPropertyValue( "Representation",
                                                                sBugID );

                        // Querying for the interface XText
                        XText xtext = UnoRuntime.queryInterface(
                            XText.class, xcell );

                        // Delete cell content
                        xtextrange.setString( "" );

                        // Inserting the text field URL to the cell
                        xtext.insertTextContent( xtextrange, xtextfield, false );
                    }
                }

                // Processing all features/bugs in the table
                for (int intRow = ToDoImpl.INT_ROW_FROM; intRow < INT_ROW_TO; intRow++)
                {
                    // Getting the cell of the column "Needed Days" in the
                    // current row
                    XCell xcell = xcellrange.getCellByPosition(
                        INT_COLUMN_NEEDEDDAYS, intRow );

                    // Getting the number of needed days to perform the feature
                    int intNeededDays = (int) Math.round( xcell.getValue() );

                    // Getting the content of a specified cell
                    String sStatus = this.getStringFromCell( xcellrange,
                    intRow, ToDoImpl.INT_COLUMN_STATUS );

                    /* Testing if the number of needed days is greater than
                       zero and if
                       the status is not "done" */
                    if ( ( intNeededDays > 0 )
                         && !( sStatus.toLowerCase().trim().equals("done")) ) {
                        // Getting the start date after a specified number of
                        // workdays
                        gregCalStartDate = this.getWorkday(
                            gregCalStartDate, 1, objectHolidays,
                            xfunctionaccess );

                        // Getting a string with the date format jjjj-mm-dd from
                        // the gregorian calendar
                        String sDate = this.getStringFromGregorianCalendar(
                            gregCalStartDate );

                        // Set the start date in the specified cell of the table
                        this.setStringToCell(xcellrange, intRow,
                                             ToDoImpl.INT_COLUMN_STARTDATE, sDate);

                        // For the start day set the day of week in the specified
                        // cell of the table
                        this.setDayOfWeek( gregCalStartDate,
                                           xcellrange, intRow,
                                           ToDoImpl.INT_COLUMN_START_DAY_OF_WEEK );

                        // Getting the end date after a specified number of workdays
                        GregorianCalendar gregCalEndDate =
                            this.getWorkday( gregCalStartDate,
                                             intNeededDays - 1,
                                             objectHolidays, xfunctionaccess );

                        // Creating a string with the date format jjjj-mm-dd
                        sDate = this.getStringFromGregorianCalendar(
                            gregCalEndDate );

                        // Set the end date in the specified cell of the table
                        this.setStringToCell( xcellrange, intRow,
                                              ToDoImpl.INT_COLUMN_ENDDATE, sDate );

                        // For the end day set the day of week in the specified
                        // cell of the table
                        this.setDayOfWeek(gregCalEndDate, xcellrange,
                                          intRow, ToDoImpl.INT_COLUMN_END_DAY_OF_WEEK);

                        // Set the initial date for the next loop
                        gregCalStartDate = ( GregorianCalendar )
                            gregCalEndDate.clone();

                        // Get the due date from the table
                        String sDueDate = this.getStringFromCell(
                            xcellrange, intRow, ToDoImpl.INT_COLUMN_DUEDATE );

                        // Testing if the due date is not empty
                        if ( !sDueDate.equals( "" ) ) {
                            GregorianCalendar gregCalDueDate =
                                this.getGregorianCalendarFromString(sDueDate);

                            // Testing if the due date is before the calculated
                            // end date
                            if ( gregCalDueDate.before(
                                     gregCalEndDate ) ) {
                                /* Getting the date when the processing of the
                                   feature/bug should
                                   be started at the latest */
                                GregorianCalendar gregCalLatestDateToStart =
                                    this.getWorkday(gregCalDueDate,
                                                    -( intNeededDays - 1 ),
                                                    objectHolidays,
                                                    xfunctionaccess);

                                // Begin with the current row
                                int intRowToInsert = intRow;

                                // Get the start date for the feature/bug in the
                                // current row
                                GregorianCalendar gregCalPreviousStartDate =
                                    this.getGregorianCalendarFromString(
                                        this.getStringFromCell(
                                            xcellrange, intRowToInsert,
                                            ToDoImpl.INT_COLUMN_STARTDATE ) );

                                // Testing if we have to search for an earlier date
                                // to begin
                                while ((gregCalLatestDateToStart.before(
                                            gregCalPreviousStartDate)) &&
                                        (INT_ROW_FROM != intRowToInsert)) {
                                    // Decrease the row
                                    intRowToInsert--;

                                    // Get the start date for the feature/bug in
                                    // the current row
                                    String sStartDate = this.getStringFromCell(
                                        xcellrange, intRowToInsert,
                                        ToDoImpl.INT_COLUMN_STARTDATE );

                                    // Search until a valid start date is found
                                    while ( sStartDate.equals( "" ) ) {
                                        // Decrease the row
                                        intRowToInsert--;

                                        // Get the start date for the feature/bug
                                        // in the current row
                                        sStartDate = this.getStringFromCell(
                                            xcellrange, intRowToInsert,
                                            ToDoImpl.INT_COLUMN_STARTDATE );
                                    }

                                    // Get the GregorianCalender format for the
                                    // start date
                                    gregCalPreviousStartDate =
                                        this.getGregorianCalendarFromString(
                                            sStartDate );
                                }

                                // Getting the cell of the column "Needed Days"
                                // in the row where to insert
                                XCell xcellNeededDaysWhereToInsert =
                                    xcellrange.getCellByPosition(
                                        INT_COLUMN_NEEDEDDAYS, intRowToInsert );
                                // Getting the number of needed days to perform
                                // the feature
                                int intNeededDaysWhereToInsert = (int)
                                    Math.round(
                                        xcellNeededDaysWhereToInsert.getValue());

                                GregorianCalendar gregCalPreviousNewEndDate =
                                    this.getWorkday(gregCalPreviousStartDate,
                                                    intNeededDays - 1 +
                                                    intNeededDaysWhereToInsert,
                                                    objectHolidays,
                                                    xfunctionaccess);
                                String sPreviousDueDate = this.getStringFromCell(
                                    xcellrange, intRowToInsert,
                                    ToDoImpl.INT_COLUMN_DUEDATE );

                                GregorianCalendar gregCalPreviousDueDate = null;

                                if ( !sPreviousDueDate.equals( "" ) ) {
                                    gregCalPreviousDueDate =
                                        this.getGregorianCalendarFromString(
                                            sPreviousDueDate );
                                }

                                if ( ( intRowToInsert == intRow ) ||
                                     ( gregCalPreviousNewEndDate.after(
                                         gregCalPreviousDueDate ) ) ) {
                                    // Querying for the interface XPropertySet for
                                    // the cell providing the due date
                                    XPropertySet xpropertyset = UnoRuntime.queryInterface(
                                            XPropertySet.class,
                                            xcellrange.getCellByPosition(
                                            ToDoImpl.INT_COLUMN_DUEDATE,
                                            intRow ) );

                                    // Changing the background color of the cell
                                    // to red
                                    xpropertyset.setPropertyValue(
                                        "CellBackColor", new Integer( 16711680 ) );
                                } else {
                                    // Querying for the interface XColumnRowRange
                                    // on the XCellRange
                                    XColumnRowRange xcolumnrowrange =
                                        UnoRuntime.queryInterface(
                                        XColumnRowRange.class, xcellrange );
                                    // Inserting one row to the table
                                    XTableRows xTableRows =
                                        xcolumnrowrange.getRows();
                                    xTableRows.insertByIndex( intRowToInsert, 1 );

                                    // Querying for the interface
                                    // XCellRangeMovement on XCellRange
                                    XCellRangeMovement xcellrangemovement =
                                      UnoRuntime.queryInterface(
                                          XCellRangeMovement.class, xcellrange );

                                    // Creating the cell address of the destination
                                    CellAddress celladdress = new CellAddress();
                                    celladdress.Sheet = 0;
                                    celladdress.Column = 0;
                                    celladdress.Row = intRowToInsert;

                                    // Creating the cell range of the source
                                    CellRangeAddress cellrangeaddress =
                                    new CellRangeAddress();
                                    cellrangeaddress.Sheet = 0;
                                    cellrangeaddress.StartColumn = 0;
                                    cellrangeaddress.StartRow = intRow + 1;
                                    cellrangeaddress.EndColumn = 8;
                                    cellrangeaddress.EndRow = intRow + 1;

                                    // Moves the cell range to another position in
                                    // the document
                                    xcellrangemovement.moveRange(celladdress,
                                                                 cellrangeaddress);

                                    // Removing the row not needed anymore
                                    xcolumnrowrange.getRows().removeByIndex(intRow
                                                                            + 1, 1);

                                    // Set the current row, because we want to
                                    // recalculate all rows below
                                    intRow = intRowToInsert - 1;

                                    // Tests at which line we want to insert
                                    if ( intRow >= ToDoImpl.INT_ROW_FROM ) {
                                        // Get the start date
                                        gregCalStartDate =
                                            this.getGregorianCalendarFromString(
                                                this.getStringFromCell( xcellrange,
                                                   intRow,ToDoImpl.INT_COLUMN_ENDDATE));
                                    }
                                    else {
                                        // Set the start date with the absolute s
                                        // tart date
                                        gregCalStartDate = (GregorianCalendar)
                                            gregCalAbsoluteStartDate.clone();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch( Exception exception ) {
                showExceptionMessage( exception );
            }
        }

        /** Getting a string from a gregorian calendar.
         * @param gregCal Date to be converted.
         * @return string (converted gregorian calendar).
         */
        public String getStringFromGregorianCalendar( GregorianCalendar gregCal ) {
            String sDate = ( gregCal.get( Calendar.MONTH ) + 1 )
                + STRING_SEPARATOR + gregCal.get( Calendar.DATE )
//                + STRING_SEPARATOR + ( gregCal.get( Calendar.MONTH ) + 1 )
                + STRING_SEPARATOR + gregCal.get( Calendar.YEAR );

            return  sDate;
        }

        /** Getting a GregorianCalendar from a string.
         * @param sDate String to be converted.
         * @return The result of the converting of the string.
         */
        public GregorianCalendar getGregorianCalendarFromString( String sDate ) {
            int []intDateValue = this.getDateValuesFromString( sDate );

            return( new GregorianCalendar( intDateValue[ 2 ], intDateValue[ 0 ],
                                           intDateValue[ 1 ] ) );
        }

        /** Getting the day, month and year from a string.
         * @param sDate String to be parsed.
         * @return Returns an array of integer variables.
         */
        public int[] getDateValuesFromString( String sDate) {
            int[] intDateValues = new int[ 3 ];

            int intPositionFirstTag = sDate.indexOf( STRING_SEPARATOR );
            int intPositionSecondTag = sDate.indexOf(STRING_SEPARATOR,
                                                     intPositionFirstTag + 1);

            // Getting the value of the month
            intDateValues[ 0 ] = Integer.parseInt(
                sDate.substring(0, intPositionFirstTag)) - 1;
            // Getting the value of the day
            intDateValues[ 1 ] = Integer.parseInt(
                sDate.substring(intPositionFirstTag + 1, intPositionSecondTag));
            // Getting the value of the year
            intDateValues[ 2 ] = Integer.parseInt(
                sDate.substring(intPositionSecondTag + 1, sDate.length()));

            return intDateValues;
        }

        /** Getting a content from a specified cell.
         * @param xcellrange Providing access to cells.
         * @param intRow Number of row.
         * @param intColumn Number of column.
         * @return String from the specified cell.
         */
        public String getStringFromCell( XCellRange xcellrange, int intRow,
                                         int intColumn ) {
            XTextRange xtextrangeStartDate = null;

            try {
                // Getting the cell holding the information about the start date
                XCell xcellStartDate = xcellrange.getCellByPosition(intColumn,
                                                                    intRow);
                // Querying for the interface XTextRange on the XCell
                xtextrangeStartDate = UnoRuntime.queryInterface(XTextRange.class, xcellStartDate);
            }
            catch( Exception exception ) {
                this.showExceptionMessage( exception );
            }

            // Getting the start date
            return  xtextrangeStartDate.getString().trim();
        }

        /** Writing a specified string to a specified cell.
         * @param xcellrange Providing access to the cells.
         * @param intRow Number of row.
         * @param intColumn Number of column.
         * @param sDate Date to write to the cell.
         */
        public void setStringToCell( XCellRange xcellrange, int intRow,
                                     int intColumn, String sDate ) {
            try {
                // Getting the cell holding the information on the day to start
                XCell xcellStartDate = xcellrange.getCellByPosition(intColumn,
                                                                    intRow);
                // Querying for the interface XTextRange on the XCell
                XTextRange xtextrange = UnoRuntime.queryInterface(XTextRange.class, xcellStartDate);
                // Setting the new start date
                xtextrange.setString( sDate );
            }
            catch( Exception exception ) {
                this.showExceptionMessage( exception );
            }
        }

        /** Calculates the week of day and calls the method "setStringToCell".
         * @param gregCal Day to be written to the cell.
         * @param xcellrange Providing access to the cells.
         * @param intRow Number of row.
         * @param intColumn Number of column.
         */
        public void setDayOfWeek( GregorianCalendar gregCal,
                                  XCellRange xcellrange, int intRow,
                                  int intColumn) {
            int intDayOfWeek = gregCal.get( Calendar.DAY_OF_WEEK );
            String sDayOfWeek = "";
            if ( intDayOfWeek == Calendar.MONDAY ) {
                sDayOfWeek = "MON";
            } else if ( intDayOfWeek == Calendar.TUESDAY ) {
                sDayOfWeek = "TUE";
            } else if ( intDayOfWeek == Calendar.WEDNESDAY ) {
                sDayOfWeek = "WED";
            } else if ( intDayOfWeek == Calendar.THURSDAY ) {
                sDayOfWeek = "THU";
            } else if ( intDayOfWeek == Calendar.FRIDAY ) {
                sDayOfWeek = "FRI";
            }

            this.setStringToCell( xcellrange, intRow, intColumn,
            sDayOfWeek );
        }

        /** Calculates the dates of the official holidays with help of Calc
         * functions.
         * @param vectorHolidays Holding all holidays.
         * @param xcellrange Providing the cells.
         * @param xfunctionaccess Provides access to functions of the Calc.
         * @param intYear Year to calculate the official holidays.
         */
        public void getOfficialHolidays(
        Vector<Object> vectorHolidays,
        XCellRange xcellrange,
        XFunctionAccess xfunctionaccess,
        int intYear ) {
            try {
                // Official Holidays for how many years?
                final int intHowManyYears = 2;

                // Get the Official Holiday for two years
                for ( int intNumberOfYear = 0;
                      intNumberOfYear <= ( intHowManyYears - 1 );
                      intNumberOfYear++ ) {
                    intYear += intNumberOfYear;

                    // Getting the Easter sunday
                    Double dEasterSunday = ( Double )
                        xfunctionaccess.callFunction(
                            "EASTERSUNDAY", new Object[] { new Integer(intYear) });

                    int intEasterSunday = (int)Math.round(
                        dEasterSunday.doubleValue());

                    // New-year
                    vectorHolidays.addElement( xfunctionaccess.callFunction(
                                                   "DATE",
                                                   new Object[] {
                                                       new Integer( intYear ),
                                                       new Integer( 1 ),
                                                       new Integer( 1 ) } ));

                    // Good Friday
                    vectorHolidays.addElement(
                    new Double( intEasterSunday - 2 ) );

                    // Easter monday
                    vectorHolidays.addElement(
                    new Double( intEasterSunday + 1 ) );

                    // Labour Day
                    vectorHolidays.addElement( xfunctionaccess.callFunction(
                                                   "DATE",
                                                   new Object[] {
                                                       new Integer( intYear ),
                                                       new Integer( 5 ),
                                                       new Integer( 1 ) } ));

                    // Ascension Day
                    vectorHolidays.addElement(new Double(intEasterSunday + 39 ));

                    // Pentecost monday
                    vectorHolidays.addElement(new Double(intEasterSunday + 50 ));

                    // German Unification
                    vectorHolidays.addElement( xfunctionaccess.callFunction(
                                                   "DATE",
                                                   new Object[] {
                                                       new Integer( intYear ),
                                                       new Integer( 10 ),
                                                       new Integer( 3 ) } ));

                    // Christmas Day First
                    vectorHolidays.addElement( xfunctionaccess.callFunction(
                                                   "DATE",
                                                   new Object[] {
                                                       new Integer( intYear ),
                                                       new Integer( 12 ),
                                                       new Integer( 25 ) } ));

                    // Christmas Day Second
                    vectorHolidays.addElement( xfunctionaccess.callFunction(
                                                   "DATE",
                                                   new Object[] {
                                                       new Integer( intYear ),
                                                       new Integer( 12 ),
                                                       new Integer( 26 ) } ));
                }
            }
            catch( Exception exception ) {
                this.showExceptionMessage( exception );
            }
        }

        /** Returns the serial number of the date before or after a specified
         * number of workdays.
         * @param gregCalStartDate Date to start with the calculation.
         * @param intDays Number of workdays (e.g. 5 or -3).
         * @param objectHolidays Private and public holidays to take into account.
         * @param xfunctionaccess Allows to call functions from the Calc.
         * @return The gregorian date before or after a specified number of
         *         workdays.
         */
        public GregorianCalendar getWorkday(
            GregorianCalendar gregCalStartDate,
            int intDays, Object[][] objectHolidays,
            XFunctionAccess xfunctionaccess ) {
            GregorianCalendar gregCalWorkday = null;

            try {
                // Getting the value of the start date
                Double dDate = ( Double ) xfunctionaccess.callFunction(
                    "DATE",
                    new Object[] {
                        new Integer( gregCalStartDate.get( Calendar.YEAR ) ),
                        new Integer( gregCalStartDate.get( Calendar.MONTH ) + 1 ),
                        new Integer( gregCalStartDate.get( Calendar.DATE ) )
                    } );

                Double dWorkday = ( Double ) xfunctionaccess.callFunction(
                "com.sun.star.sheet.addin.Analysis.getWorkday",
                new Object[] { dDate, new Integer( intDays ), objectHolidays } );

                Double dYear = ( Double ) xfunctionaccess.callFunction(
                    "YEAR", new Object[] { dWorkday } );
                Double dMonth = ( Double ) xfunctionaccess.callFunction(
                    "MONTH", new Object[] { dWorkday } );
                Double dDay = ( Double ) xfunctionaccess.callFunction(
                    "DAY", new Object[] { dWorkday } );

                gregCalWorkday = new GregorianCalendar(
                dYear.intValue(),
                dMonth.intValue() - 1,
                dDay.intValue() );
            }
            catch( Exception exception ) {
                this.showExceptionMessage( exception );
            }

            return gregCalWorkday;
        }

        /** Getting the holidays from the spreadsheet.
         * @param vectorHolidays Holding all holidays.
         * @param xcellrange Providing the cells.
         * @param xfunctionaccess Provides the access to functions of the Calc.
         */
        public void getPrivateHolidays( Vector<Object> vectorHolidays,
                                        XCellRange xcellrange,
                                        XFunctionAccess xfunctionaccess ) {
            try {
                int intRow = ToDoImpl.INT_ROW_HOLIDAYS_START;
                int intColumn = ToDoImpl.INT_COLUMN_HOLIDAYS_START;

                double dHolidayStart = xcellrange.getCellByPosition(
                    intColumn, intRow ).getValue();

                double dHolidayEnd = xcellrange.getCellByPosition(
                    intColumn + 1, intRow ).getValue();

                while ( dHolidayStart != 0 ) {
                    if ( dHolidayEnd == 0 ) {
                        vectorHolidays.addElement(
                            new Integer( (int) Math.round(
                                             dHolidayStart ) ) );
                    }
                    else {
                        for ( int intHoliday = (int) Math.round(
                                  dHolidayStart );
                              intHoliday <= (int) Math.round( dHolidayEnd );
                              intHoliday++ ) {
                            vectorHolidays.addElement( new Double( intHoliday ) );
                        }
                    }

                    intRow++;
                    dHolidayStart = xcellrange.getCellByPosition(
                        intColumn, intRow).getValue();
                    dHolidayEnd = xcellrange.getCellByPosition(
                        intColumn + 1, intRow).getValue();
                }
            }
            catch( Exception exception ) {
                this.showExceptionMessage( exception );
            }
        }

        /** Showing the stack trace in a JOptionPane.
         * @param sMessage The message to show.
         */
        public void showMessage( String sMessage ) {
            javax.swing.JFrame jframe = new javax.swing.JFrame();
            jframe.setLocation(100, 100);
            jframe.setSize(300, 200);
            jframe.setVisible(true);
            javax.swing.JOptionPane.showMessageDialog(
                jframe, sMessage, "Debugging information",
                javax.swing.JOptionPane.INFORMATION_MESSAGE);
            jframe.dispose();
        }

        /** Writing the stack trace from an exception to a string and calling
         * the method showMessage() with this string.
         * @param exception The occurred exception.
         * @see showMessage
         */
        public void showExceptionMessage( Exception exception ) {
            java.io.StringWriter swriter = new java.io.StringWriter();
            java.io.PrintWriter printwriter =
                new java.io.PrintWriter( swriter );
            exception.printStackTrace( printwriter);
            System.err.println( exception );
            this.showMessage( swriter.getBuffer().substring(0) );
        }
    }

    /**
     * Gives a factory for creating the service.
     * This method is called by the <code>JavaLoader</code>
     * <p>
     * @return  returns a <code>XSingleComponentFactory</code> for creating
     *          the component
     * @param   sImplName the name of the implementation for which a
     *          service is desired
     * @see     com.sun.star.comp.loader.JavaLoader
     */
    public static XSingleComponentFactory __getComponentFactory(String sImplName) {
        XSingleComponentFactory xFactory = null;

        if ( sImplName.equals( ToDoImpl.class.getName() ) )
            xFactory = Factory.createComponentFactory(ToDoImpl.class,
                                                      ToDoImpl.getServiceNames());

        return xFactory;
    }

    /**
     * Writes the service information into the given registry key.
     * This method is called by the <code>JavaLoader</code>
     * <p>
     * @return  returns true if the operation succeeded
     * @param   regKey the registryKey
     * @see     com.sun.star.comp.loader.JavaLoader
     */
    // This method not longer necessary since OOo 3.4 where the component registration
    // was changed to passive component registration. For more details see
    // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration

//     public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
//         return Factory.writeRegistryServiceInfo(ToDoImpl.class.getName(),
//                                                 ToDoImpl.getServiceNames(), regKey);
//     }
}