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