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