1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package mod._sc;
28 
29 import com.sun.star.beans.XPropertySet;
30 import com.sun.star.container.XIndexAccess;
31 import com.sun.star.container.XNamed;
32 import com.sun.star.lang.XComponent;
33 import com.sun.star.lang.XMultiServiceFactory;
34 import com.sun.star.sheet.DataPilotFieldGroupInfo;
35 import com.sun.star.sheet.XDataPilotDescriptor;
36 import com.sun.star.sheet.XDataPilotFieldGrouping;
37 import com.sun.star.sheet.XDataPilotTables;
38 import com.sun.star.sheet.XDataPilotTablesSupplier;
39 import com.sun.star.sheet.XSpreadsheet;
40 import com.sun.star.sheet.XSpreadsheetDocument;
41 import com.sun.star.sheet.XSpreadsheets;
42 import com.sun.star.table.CellAddress;
43 import com.sun.star.table.CellRangeAddress;
44 import com.sun.star.uno.AnyConverter;
45 import com.sun.star.uno.Type;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.uno.XInterface;
48 
49 import java.io.PrintWriter;
50 
51 import lib.StatusException;
52 import lib.TestCase;
53 import lib.TestEnvironment;
54 import lib.TestParameters;
55 
56 import util.SOfficeFactory;
57 
58 
59 
60 public class ScDataPilotFieldGroupObj extends TestCase
61 {
62     static XSpreadsheetDocument xSheetDoc = null;
63 
64     /**
65      * A field is filled some values. This integer determines the size of the
66      * field in x and y direction.
67      */
68     private int mMaxFieldIndex = 6;
69 
70     /**
71      * Creates Spreadsheet document.
72      */
73     protected void initialize (TestParameters tParam, PrintWriter log)
74     {
75         SOfficeFactory SOF = SOfficeFactory.getFactory (
76             (XMultiServiceFactory) tParam.getMSF ());
77 
78         try
79         {
80             log.println ("creating a Spreadsheet document");
81             xSheetDoc = SOF.createCalcDoc (null);
82         }
83         catch (com.sun.star.uno.Exception e)
84         {
85             // Some exception occures.FAILED
86             e.printStackTrace (log);
87             throw new StatusException ("Couldn't create document", e);
88         }
89     }
90 
91     /**
92      * Disposes Spreadsheet document.
93      */
94     protected void cleanup (TestParameters tParam, PrintWriter log)
95     {
96         log.println ("    disposing xSheetDoc ");
97 
98         XComponent oComp = (XComponent) UnoRuntime.queryInterface (
99             XComponent.class, xSheetDoc);
100         util.DesktopTools.closeDoc (oComp);
101     }
102 
103     protected synchronized TestEnvironment createTestEnvironment (TestParameters Param,
104         PrintWriter log)
105     {
106         XInterface oObj = null;
107         XInterface datapilotfield = null;
108         XInterface groups = null;
109 
110         // creation of testobject here
111         // first we write what we are intend to do to log file
112         log.println ("Creating a test environment");
113 
114         // the cell range
115         CellRangeAddress sCellRangeAdress = new CellRangeAddress ();
116         sCellRangeAdress.Sheet = 0;
117         sCellRangeAdress.StartColumn = 1;
118         sCellRangeAdress.StartRow = 0;
119         sCellRangeAdress.EndColumn = mMaxFieldIndex - 1;
120         sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
121 
122         // position of the data pilot table
123         CellAddress sCellAdress = new CellAddress ();
124         sCellAdress.Sheet = 0;
125         sCellAdress.Column = 7;
126         sCellAdress.Row = 8;
127 
128         log.println ("Getting a sheet");
129 
130         XSpreadsheets xSpreadsheets = (XSpreadsheets) xSheetDoc.getSheets ();
131         XSpreadsheet oSheet = null;
132         XSpreadsheet oSheet2 = null;
133         XIndexAccess oIndexAccess = (XIndexAccess) UnoRuntime.queryInterface (
134             XIndexAccess.class, xSpreadsheets);
135 
136         try
137         {
138             oSheet = (XSpreadsheet) AnyConverter.toObject (
139                 new Type (XSpreadsheet.class),
140                 oIndexAccess.getByIndex (0));
141             oSheet2 = (XSpreadsheet) AnyConverter.toObject (
142                 new Type (XSpreadsheet.class),
143                 oIndexAccess.getByIndex (1));
144         }
145         catch (com.sun.star.lang.WrappedTargetException e)
146         {
147             e.printStackTrace ();
148             throw new StatusException ("Couldn't get a spreadsheet", e);
149         }
150         catch (com.sun.star.lang.IndexOutOfBoundsException e)
151         {
152             e.printStackTrace ();
153             throw new StatusException ("Couldn't get a spreadsheet", e);
154         }
155         catch (com.sun.star.lang.IllegalArgumentException e)
156         {
157             e.printStackTrace ();
158             throw new StatusException ("Couldn't get a spreadsheet", e);
159         }
160 
161         try
162         {
163             log.println ("Filling a table");
164 
165             for (int i = 1; i < mMaxFieldIndex; i++)
166             {
167                 oSheet.getCellByPosition (i, 0).setFormula ("Col" + i);
168                 oSheet.getCellByPosition (0, i).setFormula ("Row" + i);
169                 oSheet2.getCellByPosition (i, 0).setFormula ("Col" + i);
170                 oSheet2.getCellByPosition (0, i).setFormula ("Row" + i);
171             }
172 
173             for (int i = 1; i < mMaxFieldIndex; i++)
174             {
175                 for (int j = 1; j < mMaxFieldIndex; j++)
176                 {
177                     oSheet.getCellByPosition (i, j).setValue (i * (j + 1));
178                     oSheet2.getCellByPosition (i, j).setValue (i * (j + 2));
179                 }
180             }
181 
182             oSheet.getCellByPosition (1, 1).setFormula ("aName");
183             oSheet.getCellByPosition (1, 2).setFormula ("otherName");
184             oSheet.getCellByPosition (1, 3).setFormula ("una");
185             oSheet.getCellByPosition (1, 4).setFormula ("otherName");
186             oSheet.getCellByPosition (1, 5).setFormula ("somethingelse");
187 
188         }
189         catch (com.sun.star.lang.IndexOutOfBoundsException e)
190         {
191             e.printStackTrace ();
192             throw new StatusException ("Couldn't fill some cells", e);
193         }
194 
195         // change a value of a cell and check the change in the data pilot
196         // (for the XDataPilotTable.refresh() test)
197         Object oChangeCell = null;
198         Object oCheckCell = null;
199         Integer aChangeValue = null;
200 
201         try
202         {
203             // cell of data
204             oChangeCell = oSheet.getCellByPosition (1, 5);
205 
206             int x = sCellAdress.Column;
207             int y = sCellAdress.Row + 3;
208 
209 
210             // cell of the data pilot output
211             oCheckCell = oSheet.getCellByPosition (x, y);
212             aChangeValue = new Integer (27);
213         }
214         catch (com.sun.star.lang.IndexOutOfBoundsException e)
215         {
216             e.printStackTrace ();
217             throw new StatusException ("Couldn't get cells for changeing.", e);
218         }
219 
220 
221         // create the test objects
222         log.println ("Getting test objects");
223 
224         XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier) UnoRuntime.queryInterface (
225             XDataPilotTablesSupplier.class,
226             oSheet);
227         XDataPilotTables DPT = DPTS.getDataPilotTables ();
228         XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor ();
229         DPDsc.setSourceRange (sCellRangeAdress);
230 
231         XPropertySet fieldPropSet = null;
232 
233         try
234         {
235             Object oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (0);
236             fieldPropSet = (XPropertySet) UnoRuntime.queryInterface (
237                 XPropertySet.class, oDataPilotField);
238             fieldPropSet.setPropertyValue ("Orientation",
239                 com.sun.star.sheet.DataPilotFieldOrientation.ROW);
240             oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (1);
241             fieldPropSet = (XPropertySet) UnoRuntime.queryInterface (
242                 XPropertySet.class, oDataPilotField);
243             fieldPropSet.setPropertyValue ("Function",
244                 com.sun.star.sheet.GeneralFunction.SUM);
245             fieldPropSet.setPropertyValue ("Orientation",
246                 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
247             oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (2);
248             fieldPropSet = (XPropertySet) UnoRuntime.queryInterface (
249                 XPropertySet.class, oDataPilotField);
250             fieldPropSet.setPropertyValue ("Orientation",
251                 com.sun.star.sheet.DataPilotFieldOrientation.COLUMN);
252         }
253         catch (com.sun.star.lang.WrappedTargetException e)
254         {
255             e.printStackTrace ();
256             throw new StatusException ("Couldn't create a test environment", e);
257         }
258         catch (com.sun.star.lang.IllegalArgumentException e)
259         {
260             e.printStackTrace ();
261             throw new StatusException ("Couldn't create a test environment", e);
262         }
263         catch (com.sun.star.beans.PropertyVetoException e)
264         {
265             e.printStackTrace ();
266             throw new StatusException ("Couldn't create a test environment", e);
267         }
268         catch (com.sun.star.beans.UnknownPropertyException e)
269         {
270             e.printStackTrace ();
271             throw new StatusException ("Couldn't create a test environment", e);
272         }
273         catch (com.sun.star.lang.IndexOutOfBoundsException e)
274         {
275             e.printStackTrace ();
276             throw new StatusException ("Couldn't create a test environment", e);
277         }
278 
279         log.println ("Insert the DataPilotTable");
280 
281         if (DPT.hasByName ("DataPilotTable"))
282         {
283             DPT.removeByName ("DataPilotTable");
284         }
285 
286         DPT.insertNewByName ("DataPilotTable", sCellAdress, DPDsc);
287         XIndexAccess xIA = (XIndexAccess) UnoRuntime.queryInterface (XIndexAccess.class,DPTS.getDataPilotTables ());
288         XIndexAccess IA = null;
289         try
290         {
291             XDataPilotDescriptor xDPT = (XDataPilotDescriptor) UnoRuntime.queryInterface (XDataPilotDescriptor.class,xIA.getByIndex (0));
292             IA = xDPT.getRowFields ();
293             //getSRange(IA);
294             System.out.println ("COUNT: "+IA.getCount ());
295             datapilotfield = (XInterface) AnyConverter.toObject (
296                 new Type (XInterface.class), IA.getByIndex (0));
297         }
298         catch (com.sun.star.lang.WrappedTargetException e)
299         {
300             e.printStackTrace ();
301             throw new StatusException ("Couldn't get data pilot field", e);
302         }
303         catch (com.sun.star.lang.IndexOutOfBoundsException e)
304         {
305             e.printStackTrace ();
306             throw new StatusException ("Couldn't get data pilot field", e);
307         }
308         catch (com.sun.star.lang.IllegalArgumentException e)
309         {
310             e.printStackTrace ();
311             throw new StatusException ("Couldn't get data pilot field", e);
312         }
313 
314         try
315         {
316             XDataPilotFieldGrouping  dpfg = (XDataPilotFieldGrouping) UnoRuntime.queryInterface (XDataPilotFieldGrouping.class, datapilotfield);
317             String[] elements = new String[]{"aName","otherName"};
318             dpfg.createNameGroup (elements);
319             DataPilotFieldGroupInfo dpgi=null;
320             xIA = (XIndexAccess) UnoRuntime.queryInterface (XIndexAccess.class,DPTS.getDataPilotTables ());
321         try
322         {
323             XDataPilotDescriptor xDPT = (XDataPilotDescriptor) UnoRuntime.queryInterface (XDataPilotDescriptor.class,xIA.getByIndex (0));
324             IA = xDPT.getRowFields ();
325         }
326         catch (com.sun.star.lang.WrappedTargetException e)
327         {
328             e.printStackTrace ();
329             throw new StatusException ("Couldn't get data pilot field", e);
330         }
331         catch (com.sun.star.lang.IndexOutOfBoundsException e)
332         {
333             e.printStackTrace ();
334             throw new StatusException ("Couldn't get data pilot field", e);
335         }
336             for (int i=0;i<IA.getCount ();i++)
337             {
338                 datapilotfield = (XInterface) AnyConverter.toObject (
339                     new Type (XInterface.class), IA.getByIndex (i));
340                 XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface (XPropertySet.class, IA.getByIndex (i));
341                 if (((Boolean)xPropertySet.getPropertyValue ("IsGroupField")).booleanValue ())
342                 {
343                     xPropertySet = (XPropertySet) UnoRuntime.queryInterface (XPropertySet.class, datapilotfield);
344                     XNamed xNamed = (XNamed) UnoRuntime.queryInterface (XNamed.class, IA.getByIndex (i));
345                     System.out.println ("name: "+xNamed.getName ());
346                     dpgi = (DataPilotFieldGroupInfo) xPropertySet.getPropertyValue ("GroupInfo");
347                 }
348             }
349             groups = dpgi.Groups;
350             XIndexAccess groupAccess = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, groups);
351             oObj = (XInterface) UnoRuntime.queryInterface(XInterface.class, groupAccess.getByIndex(0));
352         }
353         catch (Exception e)
354         {
355             e.printStackTrace ();
356         }
357 
358         log.println ("Creating object - " +
359             ((oObj == null) ? "FAILED" : "OK"));
360 
361         TestEnvironment tEnv = new TestEnvironment (oObj);
362 
363         log.println ("Implementationname: " + util.utils.getImplName (oObj));
364 
365         // Other parameters required for interface tests
366         return tEnv;
367     }
368 
369 }
370