Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "pivotcachebuffer.hxx"
21 :
22 : #include <set>
23 : #include <com/sun/star/container/XIndexAccess.hpp>
24 : #include <com/sun/star/container/XNameAccess.hpp>
25 : #include <com/sun/star/container/XNamed.hpp>
26 : #include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp>
27 : #include <com/sun/star/sheet/DataPilotFieldGroupInfo.hpp>
28 : #include <com/sun/star/sheet/XDataPilotFieldGrouping.hpp>
29 : #include <rtl/ustrbuf.hxx>
30 : #include "oox/core/filterbase.hxx"
31 : #include "oox/helper/attributelist.hxx"
32 : #include "oox/helper/containerhelper.hxx"
33 : #include "oox/helper/propertyset.hxx"
34 : #include "oox/token/properties.hxx"
35 : #include "biffinputstream.hxx"
36 : #include "defnamesbuffer.hxx"
37 : #include "excelhandlers.hxx"
38 : #include "pivotcachefragment.hxx"
39 : #include "sheetdatabuffer.hxx"
40 : #include "tablebuffer.hxx"
41 : #include "unitconverter.hxx"
42 : #include "worksheetbuffer.hxx"
43 :
44 : namespace oox {
45 : namespace xls {
46 :
47 : // ============================================================================
48 :
49 : using namespace ::com::sun::star::container;
50 : using namespace ::com::sun::star::sheet;
51 : using namespace ::com::sun::star::table;
52 : using namespace ::com::sun::star::uno;
53 : using namespace ::com::sun::star::util;
54 :
55 : using ::oox::core::Relations;
56 :
57 : // ============================================================================
58 :
59 : namespace {
60 :
61 : const sal_uInt16 BIFF12_PCDFIELD_SERVERFIELD = 0x0001;
62 : const sal_uInt16 BIFF12_PCDFIELD_NOUNIQUEITEMS = 0x0002;
63 : const sal_uInt16 BIFF12_PCDFIELD_DATABASEFIELD = 0x0004;
64 : const sal_uInt16 BIFF12_PCDFIELD_HASCAPTION = 0x0008;
65 : const sal_uInt16 BIFF12_PCDFIELD_MEMBERPROPFIELD = 0x0010;
66 : const sal_uInt16 BIFF12_PCDFIELD_HASFORMULA = 0x0100;
67 : const sal_uInt16 BIFF12_PCDFIELD_HASPROPERTYNAME = 0x0200;
68 :
69 : const sal_uInt16 BIFF12_PCDFSITEMS_HASSEMIMIXED = 0x0001;
70 : const sal_uInt16 BIFF12_PCDFSITEMS_HASNONDATE = 0x0002;
71 : const sal_uInt16 BIFF12_PCDFSITEMS_HASDATE = 0x0004;
72 : const sal_uInt16 BIFF12_PCDFSITEMS_HASSTRING = 0x0008;
73 : const sal_uInt16 BIFF12_PCDFSITEMS_HASBLANK = 0x0010;
74 : const sal_uInt16 BIFF12_PCDFSITEMS_HASMIXED = 0x0020;
75 : const sal_uInt16 BIFF12_PCDFSITEMS_ISNUMERIC = 0x0040;
76 : const sal_uInt16 BIFF12_PCDFSITEMS_ISINTEGER = 0x0080;
77 : const sal_uInt16 BIFF12_PCDFSITEMS_HASMINMAX = 0x0100;
78 : const sal_uInt16 BIFF12_PCDFSITEMS_HASLONGTEXT = 0x0200;
79 :
80 : const sal_uInt16 BIFF12_PCITEM_ARRAY_DOUBLE = 0x0001;
81 : const sal_uInt16 BIFF12_PCITEM_ARRAY_STRING = 0x0002;
82 : const sal_uInt16 BIFF12_PCITEM_ARRAY_ERROR = 0x0010;
83 : const sal_uInt16 BIFF12_PCITEM_ARRAY_DATE = 0x0020;
84 :
85 : const sal_uInt8 BIFF12_PCDFRANGEPR_AUTOSTART = 0x01;
86 : const sal_uInt8 BIFF12_PCDFRANGEPR_AUTOEND = 0x02;
87 : const sal_uInt8 BIFF12_PCDFRANGEPR_DATEGROUP = 0x04;
88 :
89 : const sal_uInt8 BIFF12_PCDEFINITION_SAVEDATA = 0x01;
90 : const sal_uInt8 BIFF12_PCDEFINITION_INVALID = 0x02;
91 : const sal_uInt8 BIFF12_PCDEFINITION_REFRESHONLOAD = 0x04;
92 : const sal_uInt8 BIFF12_PCDEFINITION_OPTIMIZEMEMORY = 0x08;
93 : const sal_uInt8 BIFF12_PCDEFINITION_ENABLEREFRESH = 0x10;
94 : const sal_uInt8 BIFF12_PCDEFINITION_BACKGROUNDQUERY = 0x20;
95 : const sal_uInt8 BIFF12_PCDEFINITION_UPGRADEONREFR = 0x40;
96 : const sal_uInt8 BIFF12_PCDEFINITION_TUPELCACHE = 0x80;
97 :
98 : const sal_uInt8 BIFF12_PCDEFINITION_HASUSERNAME = 0x01;
99 : const sal_uInt8 BIFF12_PCDEFINITION_HASRELID = 0x02;
100 : const sal_uInt8 BIFF12_PCDEFINITION_SUPPORTSUBQUERY = 0x04;
101 : const sal_uInt8 BIFF12_PCDEFINITION_SUPPORTDRILL = 0x08;
102 :
103 : const sal_uInt8 BIFF12_PCDWBSOURCE_HASRELID = 0x01;
104 : const sal_uInt8 BIFF12_PCDWBSOURCE_HASSHEET = 0x02;
105 :
106 : // ----------------------------------------------------------------------------
107 :
108 : const sal_uInt16 BIFF_PCDSOURCE_WORKSHEET = 0x0001;
109 : const sal_uInt16 BIFF_PCDSOURCE_EXTERNAL = 0x0002;
110 : const sal_uInt16 BIFF_PCDSOURCE_CONSOLIDATION = 0x0004;
111 : const sal_uInt16 BIFF_PCDSOURCE_SCENARIO = 0x0010;
112 :
113 : const sal_uInt16 BIFF_PC_NOSTRING = 0xFFFF;
114 :
115 : const sal_uInt16 BIFF_PCDFIELD_HASITEMS = 0x0001;
116 : const sal_uInt16 BIFF_PCDFIELD_HASUNSHAREDITEMS = 0x0002;
117 : const sal_uInt16 BIFF_PCDFIELD_CALCULATED = 0x0004;
118 : const sal_uInt16 BIFF_PCDFIELD_HASPARENT = 0x0008;
119 : const sal_uInt16 BIFF_PCDFIELD_RANGEGROUP = 0x0010;
120 : const sal_uInt16 BIFF_PCDFIELD_ISNUMERIC = 0x0020;
121 : const sal_uInt16 BIFF_PCDFIELD_HASSEMIMIXED = 0x0080;
122 : const sal_uInt16 BIFF_PCDFIELD_HASMINMAX = 0x0100;
123 : const sal_uInt16 BIFF_PCDFIELD_HASLONGINDEX = 0x0200;
124 : const sal_uInt16 BIFF_PCDFIELD_HASNONDATE = 0x0400;
125 : const sal_uInt16 BIFF_PCDFIELD_HASDATE = 0x0800;
126 : const sal_uInt16 BIFF_PCDFIELD_SERVERFIELD = 0x2000;
127 : const sal_uInt16 BIFF_PCDFIELD_NOUNIQUEITEMS = 0x4000;
128 :
129 : const sal_uInt16 BIFF_PCDFRANGEPR_AUTOSTART = 0x0001;
130 : const sal_uInt16 BIFF_PCDFRANGEPR_AUTOEND = 0x0002;
131 :
132 : const sal_uInt16 BIFF_PCDEFINITION_SAVEDATA = 0x0001;
133 : const sal_uInt16 BIFF_PCDEFINITION_INVALID = 0x0002;
134 : const sal_uInt16 BIFF_PCDEFINITION_REFRESHONLOAD = 0x0004;
135 : const sal_uInt16 BIFF_PCDEFINITION_OPTIMIZEMEMORY = 0x0008;
136 : const sal_uInt16 BIFF_PCDEFINITION_BACKGROUNDQUERY = 0x0010;
137 : const sal_uInt16 BIFF_PCDEFINITION_ENABLEREFRESH = 0x0020;
138 :
139 : // ----------------------------------------------------------------------------
140 :
141 : /** Adjusts the weird date format read from binary streams.
142 :
143 : Dates before 1900-Mar-01 are stored including the non-existing leap day
144 : 1900-02-29. Time values (without date) are stored as times of day
145 : 1900-Jan-00. Nothing has to be done when the workbook is stored in 1904
146 : date mode (dates before 1904-Jan-01 will not occur in this case).
147 : */
148 0 : void lclAdjustBinDateTime( DateTime& orDateTime )
149 : {
150 0 : if( (orDateTime.Year == 1900) && (orDateTime.Month <= 2) )
151 : {
152 : OSL_ENSURE( (orDateTime.Month == 1) || ((orDateTime.Month == 2) && (orDateTime.Day > 0)), "lclAdjustBinDateTime - invalid date" );
153 0 : switch( orDateTime.Month )
154 : {
155 0 : case 2: if( orDateTime.Day > 1 ) --orDateTime.Day; else { orDateTime.Day += 30; --orDateTime.Month; } break;
156 0 : case 1: if( orDateTime.Day > 1 ) --orDateTime.Day; else { orDateTime.Day += 30; orDateTime.Month = 12; --orDateTime.Year; } break;
157 : }
158 : }
159 0 : }
160 :
161 : } // namespace
162 :
163 : // ============================================================================
164 :
165 0 : PivotCacheItem::PivotCacheItem() :
166 0 : mnType( XML_m ), mbUnused( false )
167 : {
168 0 : }
169 :
170 0 : void PivotCacheItem::readString( const AttributeList& rAttribs )
171 : {
172 0 : maValue <<= rAttribs.getXString( XML_v, OUString() );
173 0 : mnType = XML_s;
174 0 : }
175 :
176 0 : void PivotCacheItem::readNumeric( const AttributeList& rAttribs )
177 : {
178 0 : maValue <<= rAttribs.getDouble( XML_v, 0.0 );
179 0 : mnType = XML_n;
180 0 : mbUnused = rAttribs.getBool( XML_u, false );
181 0 : }
182 :
183 0 : void PivotCacheItem::readDate( const AttributeList& rAttribs )
184 : {
185 0 : maValue <<= rAttribs.getDateTime( XML_v, DateTime() );
186 0 : mnType = XML_d;
187 0 : }
188 :
189 0 : void PivotCacheItem::readBool( const AttributeList& rAttribs )
190 : {
191 0 : maValue <<= rAttribs.getBool( XML_v, false );
192 0 : mnType = XML_b;
193 0 : }
194 :
195 0 : void PivotCacheItem::readError( const AttributeList& rAttribs, const UnitConverter& rUnitConverter )
196 : {
197 0 : maValue <<= static_cast< sal_Int32 >( rUnitConverter.calcBiffErrorCode( rAttribs.getXString( XML_v, OUString() ) ) );
198 0 : mnType = XML_e;
199 0 : }
200 :
201 0 : void PivotCacheItem::readIndex( const AttributeList& rAttribs )
202 : {
203 0 : maValue <<= rAttribs.getInteger( XML_v, -1 );
204 0 : mnType = XML_x;
205 0 : }
206 :
207 0 : void PivotCacheItem::readString( SequenceInputStream& rStrm )
208 : {
209 0 : maValue <<= BiffHelper::readString( rStrm );
210 0 : mnType = XML_s;
211 0 : }
212 :
213 0 : void PivotCacheItem::readDouble( SequenceInputStream& rStrm )
214 : {
215 0 : maValue <<= rStrm.readDouble();
216 0 : mnType = XML_n;
217 0 : }
218 :
219 0 : void PivotCacheItem::readDate( SequenceInputStream& rStrm )
220 : {
221 0 : DateTime aDateTime;
222 0 : aDateTime.Year = rStrm.readuInt16();
223 0 : aDateTime.Month = rStrm.readuInt16();
224 0 : aDateTime.Day = rStrm.readuInt8();
225 0 : aDateTime.Hours = rStrm.readuInt8();
226 0 : aDateTime.Minutes = rStrm.readuInt8();
227 0 : aDateTime.Seconds = rStrm.readuInt8();
228 0 : lclAdjustBinDateTime( aDateTime );
229 0 : maValue <<= aDateTime;
230 0 : mnType = XML_d;
231 0 : }
232 :
233 0 : void PivotCacheItem::readBool( SequenceInputStream& rStrm )
234 : {
235 0 : maValue <<= (rStrm.readuInt8() != 0);
236 0 : mnType = XML_b;
237 0 : }
238 :
239 0 : void PivotCacheItem::readError( SequenceInputStream& rStrm )
240 : {
241 0 : maValue <<= static_cast< sal_Int32 >( rStrm.readuInt8() );
242 0 : mnType = XML_e;
243 0 : }
244 :
245 0 : void PivotCacheItem::readIndex( SequenceInputStream& rStrm )
246 : {
247 0 : maValue <<= rStrm.readInt32();
248 0 : mnType = XML_x;
249 0 : }
250 :
251 0 : void PivotCacheItem::readString( BiffInputStream& rStrm, const WorkbookHelper& rHelper )
252 : {
253 0 : maValue <<= (rHelper.getBiff() == BIFF8) ? rStrm.readUniString() : rStrm.readByteStringUC( true, rHelper.getTextEncoding() );
254 0 : mnType = XML_s;
255 0 : }
256 :
257 0 : void PivotCacheItem::readDouble( BiffInputStream& rStrm )
258 : {
259 0 : maValue <<= rStrm.readDouble();
260 0 : mnType = XML_n;
261 0 : }
262 :
263 0 : void PivotCacheItem::readInteger( BiffInputStream& rStrm )
264 : {
265 0 : maValue <<= rStrm.readInt16();
266 0 : mnType = XML_i; // fake, used for BIFF only
267 0 : }
268 :
269 0 : void PivotCacheItem::readDate( BiffInputStream& rStrm )
270 : {
271 0 : DateTime aDateTime;
272 0 : aDateTime.Year = rStrm.readuInt16();
273 0 : aDateTime.Month = rStrm.readuInt16();
274 0 : aDateTime.Day = rStrm.readuInt8();
275 0 : aDateTime.Hours = rStrm.readuInt8();
276 0 : aDateTime.Minutes = rStrm.readuInt8();
277 0 : aDateTime.Seconds = rStrm.readuInt8();
278 0 : lclAdjustBinDateTime( aDateTime );
279 0 : maValue <<= aDateTime;
280 0 : mnType = XML_d;
281 0 : }
282 :
283 0 : void PivotCacheItem::readBool( BiffInputStream& rStrm )
284 : {
285 0 : maValue <<= (rStrm.readuInt8() != 0);
286 0 : mnType = XML_b;
287 0 : }
288 :
289 0 : void PivotCacheItem::readError( BiffInputStream& rStrm )
290 : {
291 0 : maValue <<= static_cast< sal_Int32 >( rStrm.readuInt8() );
292 0 : mnType = XML_e;
293 0 : }
294 :
295 0 : void PivotCacheItem::setStringValue( const OUString& sString )
296 : {
297 0 : mnType = XML_s;
298 0 : maValue <<= sString;
299 0 : }
300 :
301 0 : OUString PivotCacheItem::getName() const
302 : {
303 0 : switch( mnType )
304 : {
305 0 : case XML_m: return OUString();
306 0 : case XML_s: return maValue.get< OUString >();
307 0 : case XML_n: return OUString::valueOf( maValue.get< double >() ); // !TODO
308 0 : case XML_i: return OUString::valueOf( maValue.get< sal_Int32 >() );
309 0 : case XML_d: return OUString(); // !TODO
310 0 : case XML_b: return OUString::valueOf( static_cast< sal_Bool >( maValue.get< bool >() ) ); // !TODO
311 0 : case XML_e: return OUString(); // !TODO
312 : }
313 : OSL_FAIL( "PivotCacheItem::getName - invalid data type" );
314 0 : return OUString();
315 : }
316 :
317 : // ----------------------------------------------------------------------------
318 :
319 0 : PivotCacheItemList::PivotCacheItemList( const WorkbookHelper& rHelper ) :
320 0 : WorkbookHelper( rHelper )
321 : {
322 0 : }
323 :
324 0 : void PivotCacheItemList::importItem( sal_Int32 nElement, const AttributeList& rAttribs )
325 : {
326 0 : PivotCacheItem& rItem = createItem();
327 0 : switch( nElement )
328 : {
329 0 : case XLS_TOKEN( m ): break;
330 0 : case XLS_TOKEN( s ): rItem.readString( rAttribs ); break;
331 0 : case XLS_TOKEN( n ): rItem.readNumeric( rAttribs ); break;
332 0 : case XLS_TOKEN( d ): rItem.readDate( rAttribs ); break;
333 0 : case XLS_TOKEN( b ): rItem.readBool( rAttribs ); break;
334 0 : case XLS_TOKEN( e ): rItem.readError( rAttribs, getUnitConverter() ); break;
335 : default: OSL_FAIL( "PivotCacheItemList::importItem - unknown element type" );
336 : }
337 0 : }
338 :
339 0 : void PivotCacheItemList::importItem( sal_Int32 nRecId, SequenceInputStream& rStrm )
340 : {
341 0 : if( nRecId == BIFF12_ID_PCITEM_ARRAY )
342 : {
343 0 : importArray( rStrm );
344 0 : return;
345 : }
346 :
347 0 : PivotCacheItem& rItem = createItem();
348 0 : switch( nRecId )
349 : {
350 : case BIFF12_ID_PCITEM_MISSING:
351 0 : case BIFF12_ID_PCITEMA_MISSING: break;
352 : case BIFF12_ID_PCITEM_STRING:
353 0 : case BIFF12_ID_PCITEMA_STRING: rItem.readString( rStrm ); break;
354 : case BIFF12_ID_PCITEM_DOUBLE:
355 0 : case BIFF12_ID_PCITEMA_DOUBLE: rItem.readDouble( rStrm ); break;
356 : case BIFF12_ID_PCITEM_DATE:
357 0 : case BIFF12_ID_PCITEMA_DATE: rItem.readDate( rStrm ); break;
358 : case BIFF12_ID_PCITEM_BOOL:
359 0 : case BIFF12_ID_PCITEMA_BOOL: rItem.readBool( rStrm ); break;
360 : case BIFF12_ID_PCITEM_ERROR:
361 0 : case BIFF12_ID_PCITEMA_ERROR: rItem.readError( rStrm ); break;
362 : default: OSL_FAIL( "PivotCacheItemList::importItem - unknown record type" );
363 : }
364 : }
365 :
366 0 : void PivotCacheItemList::importItemList( BiffInputStream& rStrm, sal_uInt16 nCount )
367 : {
368 0 : bool bLoop = true;
369 0 : for( sal_uInt16 nItemIdx = 0; bLoop && (nItemIdx < nCount); ++nItemIdx )
370 : {
371 0 : bLoop = rStrm.startNextRecord();
372 0 : if( bLoop ) switch( rStrm.getRecId() )
373 : {
374 0 : case BIFF_ID_PCITEM_MISSING: createItem(); break;
375 0 : case BIFF_ID_PCITEM_STRING: createItem().readString( rStrm, *this ); break;
376 0 : case BIFF_ID_PCITEM_DOUBLE: createItem().readDouble( rStrm ); break;
377 0 : case BIFF_ID_PCITEM_INTEGER: createItem().readInteger( rStrm ); break;
378 0 : case BIFF_ID_PCITEM_DATE: createItem().readDate( rStrm ); break;
379 0 : case BIFF_ID_PCITEM_BOOL: createItem().readBool( rStrm ); break;
380 0 : case BIFF_ID_PCITEM_ERROR: createItem().readError( rStrm ); break;
381 0 : default: rStrm.rewindRecord(); bLoop = false;
382 : }
383 : }
384 : OSL_ENSURE( bLoop, "PivotCacheItemList::importItemList - could not read all cache item records" );
385 0 : }
386 :
387 0 : const PivotCacheItem* PivotCacheItemList::getCacheItem( sal_Int32 nItemIdx ) const
388 : {
389 0 : return ContainerHelper::getVectorElement( maItems, nItemIdx );
390 : }
391 :
392 0 : void PivotCacheItemList::applyItemCaptions( const IdCaptionPairList& vCaptions )
393 : {
394 0 : for( IdCaptionPairList::const_iterator aIt = vCaptions.begin(), aEnd = vCaptions.end(); aIt != aEnd; ++aIt )
395 : {
396 0 : if ( static_cast<sal_uInt32>( aIt->first ) < maItems.size() )
397 0 : maItems[ aIt->first ].setStringValue( aIt->second );
398 : }
399 0 : }
400 :
401 0 : void PivotCacheItemList::getCacheItemNames( ::std::vector< OUString >& orItemNames ) const
402 : {
403 0 : orItemNames.clear();
404 0 : orItemNames.reserve( maItems.size() );
405 0 : for( CacheItemVector::const_iterator aIt = maItems.begin(), aEnd = maItems.end(); aIt != aEnd; ++aIt )
406 0 : orItemNames.push_back( aIt->getName() );
407 0 : }
408 :
409 : // private --------------------------------------------------------------------
410 :
411 0 : PivotCacheItem& PivotCacheItemList::createItem()
412 : {
413 0 : maItems.resize( maItems.size() + 1 );
414 0 : return maItems.back();
415 : }
416 :
417 0 : void PivotCacheItemList::importArray( SequenceInputStream& rStrm )
418 : {
419 0 : sal_uInt16 nType = rStrm.readuInt16();
420 0 : sal_Int32 nCount = rStrm.readInt32();
421 0 : for( sal_Int32 nIdx = 0; !rStrm.isEof() && (nIdx < nCount); ++nIdx )
422 : {
423 0 : switch( nType )
424 : {
425 0 : case BIFF12_PCITEM_ARRAY_DOUBLE: createItem().readDouble( rStrm ); break;
426 0 : case BIFF12_PCITEM_ARRAY_STRING: createItem().readString( rStrm ); break;
427 0 : case BIFF12_PCITEM_ARRAY_ERROR: createItem().readError( rStrm ); break;
428 0 : case BIFF12_PCITEM_ARRAY_DATE: createItem().readDate( rStrm ); break;
429 : default:
430 : OSL_FAIL( "PivotCacheItemList::importArray - unknown data type" );
431 0 : nIdx = nCount;
432 : }
433 : }
434 0 : }
435 :
436 : // ============================================================================
437 :
438 0 : PCFieldModel::PCFieldModel() :
439 : mnNumFmtId( 0 ),
440 : mnSqlType( 0 ),
441 : mnHierarchy( 0 ),
442 : mnLevel( 0 ),
443 : mnMappingCount( 0 ),
444 : mbDatabaseField( true ),
445 : mbServerField( false ),
446 : mbUniqueList( true ),
447 0 : mbMemberPropField( false )
448 : {
449 0 : }
450 :
451 : // ----------------------------------------------------------------------------
452 :
453 0 : PCSharedItemsModel::PCSharedItemsModel() :
454 : mbHasSemiMixed( true ),
455 : mbHasNonDate( true ),
456 : mbHasDate( false ),
457 : mbHasString( true ),
458 : mbHasBlank( false ),
459 : mbHasMixed( false ),
460 : mbIsNumeric( false ),
461 : mbIsInteger( false ),
462 : mbHasLongText( false ),
463 0 : mbHasLongIndexes( false )
464 : {
465 0 : }
466 :
467 : // ----------------------------------------------------------------------------
468 :
469 0 : PCFieldGroupModel::PCFieldGroupModel() :
470 : mfStartValue( 0.0 ),
471 : mfEndValue( 0.0 ),
472 : mfInterval( 1.0 ),
473 : mnParentField( -1 ),
474 : mnBaseField( -1 ),
475 : mnGroupBy( XML_range ),
476 : mbRangeGroup( false ),
477 : mbDateGroup( false ),
478 : mbAutoStart( true ),
479 0 : mbAutoEnd( true )
480 : {
481 0 : }
482 :
483 0 : void PCFieldGroupModel::setBiffGroupBy( sal_uInt8 nGroupBy )
484 : {
485 : static const sal_Int32 spnGroupBy[] = { XML_range,
486 : XML_seconds, XML_minutes, XML_hours, XML_days, XML_months, XML_quarters, XML_years };
487 0 : mnGroupBy = STATIC_ARRAY_SELECT( spnGroupBy, nGroupBy, XML_range );
488 0 : }
489 :
490 : // ----------------------------------------------------------------------------
491 :
492 0 : PivotCacheField::PivotCacheField( const WorkbookHelper& rHelper, bool bIsDatabaseField ) :
493 : WorkbookHelper( rHelper ),
494 : maSharedItems( rHelper ),
495 0 : maGroupItems( rHelper )
496 : {
497 0 : maFieldModel.mbDatabaseField = bIsDatabaseField;
498 0 : }
499 :
500 0 : void PivotCacheField::importCacheField( const AttributeList& rAttribs )
501 : {
502 0 : maFieldModel.maName = rAttribs.getXString( XML_name, OUString() );
503 0 : maFieldModel.maCaption = rAttribs.getXString( XML_caption, OUString() );
504 0 : maFieldModel.maPropertyName = rAttribs.getXString( XML_propertyName, OUString() );
505 0 : maFieldModel.maFormula = rAttribs.getXString( XML_formula, OUString() );
506 0 : maFieldModel.mnNumFmtId = rAttribs.getInteger( XML_numFmtId, 0 );
507 0 : maFieldModel.mnSqlType = rAttribs.getInteger( XML_sqlType, 0 );
508 0 : maFieldModel.mnHierarchy = rAttribs.getInteger( XML_hierarchy, 0 );
509 0 : maFieldModel.mnLevel = rAttribs.getInteger( XML_level, 0 );
510 0 : maFieldModel.mnMappingCount = rAttribs.getInteger( XML_mappingCount, 0 );
511 0 : maFieldModel.mbDatabaseField = rAttribs.getBool( XML_databaseField, true );
512 0 : maFieldModel.mbServerField = rAttribs.getBool( XML_serverField, false );
513 0 : maFieldModel.mbUniqueList = rAttribs.getBool( XML_uniqueList, true );
514 0 : maFieldModel.mbMemberPropField = rAttribs.getBool( XML_memberPropertyField, false );
515 0 : }
516 :
517 0 : void PivotCacheField::importSharedItems( const AttributeList& rAttribs )
518 : {
519 : OSL_ENSURE( maSharedItems.empty(), "PivotCacheField::importSharedItems - multiple shared items elements" );
520 0 : maSharedItemsModel.mbHasSemiMixed = rAttribs.getBool( XML_containsSemiMixedTypes, true );
521 0 : maSharedItemsModel.mbHasNonDate = rAttribs.getBool( XML_containsNonDate, true );
522 0 : maSharedItemsModel.mbHasDate = rAttribs.getBool( XML_containsDate, false );
523 0 : maSharedItemsModel.mbHasString = rAttribs.getBool( XML_containsString, true );
524 0 : maSharedItemsModel.mbHasBlank = rAttribs.getBool( XML_containsBlank, false );
525 0 : maSharedItemsModel.mbHasMixed = rAttribs.getBool( XML_containsMixedTypes, false );
526 0 : maSharedItemsModel.mbIsNumeric = rAttribs.getBool( XML_containsNumber, false );
527 0 : maSharedItemsModel.mbIsInteger = rAttribs.getBool( XML_containsInteger, false );
528 0 : maSharedItemsModel.mbHasLongText = rAttribs.getBool( XML_longText, false );
529 0 : }
530 :
531 0 : void PivotCacheField::importSharedItem( sal_Int32 nElement, const AttributeList& rAttribs )
532 : {
533 0 : maSharedItems.importItem( nElement, rAttribs );
534 0 : }
535 :
536 0 : void PivotCacheField::importFieldGroup( const AttributeList& rAttribs )
537 : {
538 0 : maFieldGroupModel.mnParentField = rAttribs.getInteger( XML_par, -1 );
539 0 : maFieldGroupModel.mnBaseField = rAttribs.getInteger( XML_base, -1 );
540 0 : }
541 :
542 0 : void PivotCacheField::importRangePr( const AttributeList& rAttribs )
543 : {
544 0 : maFieldGroupModel.maStartDate = rAttribs.getDateTime( XML_startDate, DateTime() );
545 0 : maFieldGroupModel.maEndDate = rAttribs.getDateTime( XML_endDate, DateTime() );
546 0 : maFieldGroupModel.mfStartValue = rAttribs.getDouble( XML_startNum, 0.0 );
547 0 : maFieldGroupModel.mfEndValue = rAttribs.getDouble( XML_endNum, 0.0 );
548 0 : maFieldGroupModel.mfInterval = rAttribs.getDouble( XML_groupInterval, 1.0 );
549 0 : maFieldGroupModel.mnGroupBy = rAttribs.getToken( XML_groupBy, XML_range );
550 0 : maFieldGroupModel.mbRangeGroup = true;
551 0 : maFieldGroupModel.mbDateGroup = maFieldGroupModel.mnGroupBy != XML_range;
552 0 : maFieldGroupModel.mbAutoStart = rAttribs.getBool( XML_autoStart, true );
553 0 : maFieldGroupModel.mbAutoEnd = rAttribs.getBool( XML_autoEnd, true );
554 0 : }
555 :
556 0 : void PivotCacheField::importDiscretePrItem( sal_Int32 nElement, const AttributeList& rAttribs )
557 : {
558 : OSL_ENSURE( nElement == XLS_TOKEN( x ), "PivotCacheField::importDiscretePrItem - unexpected element" );
559 0 : if( nElement == XLS_TOKEN( x ) )
560 0 : maDiscreteItems.push_back( rAttribs.getInteger( XML_v, -1 ) );
561 0 : }
562 :
563 0 : void PivotCacheField::importGroupItem( sal_Int32 nElement, const AttributeList& rAttribs )
564 : {
565 0 : maGroupItems.importItem( nElement, rAttribs );
566 0 : }
567 :
568 0 : void PivotCacheField::importPCDField( SequenceInputStream& rStrm )
569 : {
570 : sal_uInt16 nFlags;
571 0 : rStrm >> nFlags >> maFieldModel.mnNumFmtId;
572 0 : maFieldModel.mnSqlType = rStrm.readInt16();
573 0 : rStrm >> maFieldModel.mnHierarchy >> maFieldModel.mnLevel >> maFieldModel.mnMappingCount >> maFieldModel.maName;
574 0 : if( getFlag( nFlags, BIFF12_PCDFIELD_HASCAPTION ) )
575 0 : rStrm >> maFieldModel.maCaption;
576 0 : if( getFlag( nFlags, BIFF12_PCDFIELD_HASFORMULA ) )
577 0 : rStrm.skip( ::std::max< sal_Int32 >( rStrm.readInt32(), 0 ) );
578 0 : if( maFieldModel.mnMappingCount > 0 )
579 0 : rStrm.skip( ::std::max< sal_Int32 >( rStrm.readInt32(), 0 ) );
580 0 : if( getFlag( nFlags, BIFF12_PCDFIELD_HASPROPERTYNAME ) )
581 0 : rStrm >> maFieldModel.maPropertyName;
582 :
583 0 : maFieldModel.mbDatabaseField = getFlag( nFlags, BIFF12_PCDFIELD_DATABASEFIELD );
584 0 : maFieldModel.mbServerField = getFlag( nFlags, BIFF12_PCDFIELD_SERVERFIELD );
585 0 : maFieldModel.mbUniqueList = !getFlag( nFlags, BIFF12_PCDFIELD_NOUNIQUEITEMS );
586 0 : maFieldModel.mbMemberPropField = getFlag( nFlags, BIFF12_PCDFIELD_MEMBERPROPFIELD );
587 0 : }
588 :
589 0 : void PivotCacheField::importPCDFSharedItems( SequenceInputStream& rStrm )
590 : {
591 : sal_uInt16 nFlags;
592 0 : rStrm >> nFlags;
593 0 : maSharedItemsModel.mbHasSemiMixed = getFlag( nFlags, BIFF12_PCDFSITEMS_HASSEMIMIXED );
594 0 : maSharedItemsModel.mbHasNonDate = getFlag( nFlags, BIFF12_PCDFSITEMS_HASNONDATE );
595 0 : maSharedItemsModel.mbHasDate = getFlag( nFlags, BIFF12_PCDFSITEMS_HASDATE );
596 0 : maSharedItemsModel.mbHasString = getFlag( nFlags, BIFF12_PCDFSITEMS_HASSTRING );
597 0 : maSharedItemsModel.mbHasBlank = getFlag( nFlags, BIFF12_PCDFSITEMS_HASBLANK );
598 0 : maSharedItemsModel.mbHasMixed = getFlag( nFlags, BIFF12_PCDFSITEMS_HASMIXED );
599 0 : maSharedItemsModel.mbIsNumeric = getFlag( nFlags, BIFF12_PCDFSITEMS_ISNUMERIC );
600 0 : maSharedItemsModel.mbIsInteger = getFlag( nFlags, BIFF12_PCDFSITEMS_ISINTEGER );
601 0 : maSharedItemsModel.mbHasLongText = getFlag( nFlags, BIFF12_PCDFSITEMS_HASLONGTEXT );
602 0 : }
603 :
604 0 : void PivotCacheField::importPCDFSharedItem( sal_Int32 nRecId, SequenceInputStream& rStrm )
605 : {
606 0 : maSharedItems.importItem( nRecId, rStrm );
607 0 : }
608 :
609 0 : void PivotCacheField::importPCDFieldGroup( SequenceInputStream& rStrm )
610 : {
611 0 : rStrm >> maFieldGroupModel.mnParentField >> maFieldGroupModel.mnBaseField;
612 0 : }
613 :
614 0 : void PivotCacheField::importPCDFRangePr( SequenceInputStream& rStrm )
615 : {
616 : sal_uInt8 nGroupBy, nFlags;
617 0 : rStrm >> nGroupBy >> nFlags >> maFieldGroupModel.mfStartValue >> maFieldGroupModel.mfEndValue >> maFieldGroupModel.mfInterval;
618 :
619 0 : maFieldGroupModel.setBiffGroupBy( nGroupBy );
620 0 : maFieldGroupModel.mbRangeGroup = true;
621 0 : maFieldGroupModel.mbDateGroup = getFlag( nFlags, BIFF12_PCDFRANGEPR_DATEGROUP );
622 0 : maFieldGroupModel.mbAutoStart = getFlag( nFlags, BIFF12_PCDFRANGEPR_AUTOSTART );
623 0 : maFieldGroupModel.mbAutoEnd = getFlag( nFlags, BIFF12_PCDFRANGEPR_AUTOEND );
624 :
625 : OSL_ENSURE( maFieldGroupModel.mbDateGroup == (maFieldGroupModel.mnGroupBy != XML_range), "PivotCacheField::importPCDFRangePr - wrong date flag" );
626 0 : if( maFieldGroupModel.mbDateGroup )
627 : {
628 0 : maFieldGroupModel.maStartDate = getUnitConverter().calcDateTimeFromSerial( maFieldGroupModel.mfStartValue );
629 0 : maFieldGroupModel.maEndDate = getUnitConverter().calcDateTimeFromSerial( maFieldGroupModel.mfEndValue );
630 : }
631 0 : }
632 :
633 0 : void PivotCacheField::importPCDFDiscretePrItem( sal_Int32 nRecId, SequenceInputStream& rStrm )
634 : {
635 : OSL_ENSURE( nRecId == BIFF12_ID_PCITEM_INDEX, "PivotCacheField::importPCDFDiscretePrItem - unexpected record" );
636 0 : if( nRecId == BIFF12_ID_PCITEM_INDEX )
637 0 : maDiscreteItems.push_back( rStrm.readInt32() );
638 0 : }
639 :
640 0 : void PivotCacheField::importPCDFGroupItem( sal_Int32 nRecId, SequenceInputStream& rStrm )
641 : {
642 0 : maGroupItems.importItem( nRecId, rStrm );
643 0 : }
644 :
645 0 : void PivotCacheField::importPCDField( BiffInputStream& rStrm )
646 : {
647 : sal_uInt16 nFlags, nGroupItems, nBaseItems, nSharedItems;
648 0 : rStrm >> nFlags;
649 0 : maFieldGroupModel.mnParentField = rStrm.readuInt16();
650 0 : maFieldGroupModel.mnBaseField = rStrm.readuInt16();
651 0 : rStrm.skip( 2 ); // number of unique items (either shared or group)
652 0 : rStrm >> nGroupItems >> nBaseItems >> nSharedItems;
653 0 : maFieldModel.maName = (getBiff() == BIFF8) ? rStrm.readUniString() : rStrm.readByteStringUC( true, getTextEncoding() );
654 :
655 0 : maFieldModel.mbServerField = getFlag( nFlags, BIFF_PCDFIELD_SERVERFIELD );
656 0 : maFieldModel.mbUniqueList = !getFlag( nFlags, BIFF_PCDFIELD_NOUNIQUEITEMS );
657 0 : maSharedItemsModel.mbHasSemiMixed = getFlag( nFlags, BIFF_PCDFIELD_HASSEMIMIXED );
658 0 : maSharedItemsModel.mbHasNonDate = getFlag( nFlags, BIFF_PCDFIELD_HASNONDATE );
659 0 : maSharedItemsModel.mbHasDate = getFlag( nFlags, BIFF_PCDFIELD_HASDATE );
660 0 : maSharedItemsModel.mbIsNumeric = getFlag( nFlags, BIFF_PCDFIELD_ISNUMERIC );
661 0 : maSharedItemsModel.mbHasLongIndexes = getFlag( nFlags, BIFF_PCDFIELD_HASLONGINDEX );
662 0 : maFieldGroupModel.mbRangeGroup = getFlag( nFlags, BIFF_PCDFIELD_RANGEGROUP );
663 :
664 : // in BIFF, presence of parent group field is denoted by a flag
665 0 : if( !getFlag( nFlags, BIFF_PCDFIELD_HASPARENT ) )
666 0 : maFieldGroupModel.mnParentField = -1;
667 :
668 : // following PCDFSQLTYPE record contains SQL type
669 0 : if( (rStrm.getNextRecId() == BIFF_ID_PCDFSQLTYPE) && rStrm.startNextRecord() )
670 0 : maFieldModel.mnSqlType = rStrm.readInt16();
671 :
672 : // read group items, if any
673 0 : if( nGroupItems > 0 )
674 : {
675 : OSL_ENSURE( getFlag( nFlags, BIFF_PCDFIELD_HASITEMS ), "PivotCacheField::importPCDField - missing items flag" );
676 0 : maGroupItems.importItemList( rStrm, nGroupItems );
677 :
678 0 : sal_uInt16 nNextRecId = rStrm.getNextRecId();
679 0 : bool bHasRangePr = nNextRecId == BIFF_ID_PCDFRANGEPR;
680 0 : bool bHasDiscretePr = nNextRecId == BIFF_ID_PCDFDISCRETEPR;
681 :
682 : OSL_ENSURE( bHasRangePr || bHasDiscretePr, "PivotCacheField::importPCDField - missing group properties record" );
683 : OSL_ENSURE( bHasRangePr == maFieldGroupModel.mbRangeGroup, "PivotCacheField::importPCDField - invalid range grouping flag" );
684 0 : if( bHasRangePr && rStrm.startNextRecord() )
685 0 : importPCDFRangePr( rStrm );
686 0 : else if( bHasDiscretePr && rStrm.startNextRecord() )
687 0 : importPCDFDiscretePr( rStrm );
688 : }
689 :
690 : // read the shared items, if any
691 0 : if( nSharedItems > 0 )
692 : {
693 : OSL_ENSURE( getFlag( nFlags, BIFF_PCDFIELD_HASITEMS ), "PivotCacheField::importPCDField - missing items flag" );
694 0 : maSharedItems.importItemList( rStrm, nSharedItems );
695 : }
696 0 : }
697 :
698 0 : void PivotCacheField::importPCDFRangePr( BiffInputStream& rStrm )
699 : {
700 : sal_uInt16 nFlags;
701 0 : rStrm >> nFlags;
702 0 : maFieldGroupModel.setBiffGroupBy( extractValue< sal_uInt8 >( nFlags, 2, 3 ) );
703 0 : maFieldGroupModel.mbRangeGroup = true;
704 0 : maFieldGroupModel.mbDateGroup = maFieldGroupModel.mnGroupBy != XML_range;
705 0 : maFieldGroupModel.mbAutoStart = getFlag( nFlags, BIFF_PCDFRANGEPR_AUTOSTART );
706 0 : maFieldGroupModel.mbAutoEnd = getFlag( nFlags, BIFF_PCDFRANGEPR_AUTOEND );
707 :
708 : /* Start, end, and interval are stored in 3 separate item records. Type of
709 : the items is dependent on numeric/date mode. Numeric groups expect
710 : three PCITEM_DOUBLE records, date groups expect two PCITEM_DATE records
711 : and one PCITEM_INT record. */
712 0 : PivotCacheItemList aLimits( *this );
713 0 : aLimits.importItemList( rStrm, 3 );
714 : OSL_ENSURE( aLimits.size() == 3, "PivotCacheField::importPCDFRangePr - missing grouping records" );
715 0 : const PivotCacheItem* pStartValue = aLimits.getCacheItem( 0 );
716 0 : const PivotCacheItem* pEndValue = aLimits.getCacheItem( 1 );
717 0 : const PivotCacheItem* pInterval = aLimits.getCacheItem( 2 );
718 0 : if( pStartValue && pEndValue && pInterval )
719 : {
720 0 : if( maFieldGroupModel.mbDateGroup )
721 : {
722 0 : bool bHasTypes = (pStartValue->getType() == XML_d) && (pEndValue->getType() == XML_d) && (pInterval->getType() == XML_i);
723 : OSL_ENSURE( bHasTypes, "PivotCacheField::importPCDFRangePr - wrong data types in grouping items" );
724 0 : if( bHasTypes )
725 : {
726 0 : maFieldGroupModel.maStartDate = pStartValue->getValue().get< DateTime >();
727 0 : maFieldGroupModel.maEndDate = pEndValue->getValue().get< DateTime >();
728 0 : maFieldGroupModel.mfInterval = pInterval->getValue().get< sal_Int16 >();
729 : }
730 : }
731 : else
732 : {
733 0 : bool bHasTypes = (pStartValue->getType() == XML_n) && (pEndValue->getType() == XML_n) && (pInterval->getType() == XML_n);
734 : OSL_ENSURE( bHasTypes, "PivotCacheField::importPCDFRangePr - wrong data types in grouping items" );
735 0 : if( bHasTypes )
736 : {
737 0 : maFieldGroupModel.mfStartValue = pStartValue->getValue().get< double >();
738 0 : maFieldGroupModel.mfEndValue = pEndValue->getValue().get< double >();
739 0 : maFieldGroupModel.mfInterval = pInterval->getValue().get< double >();
740 : }
741 : }
742 0 : }
743 0 : }
744 :
745 0 : void PivotCacheField::importPCDFDiscretePr( BiffInputStream& rStrm )
746 : {
747 0 : sal_Int32 nCount = static_cast< sal_Int32 >( rStrm.size() / 2 );
748 0 : for( sal_Int32 nIndex = 0; !rStrm.isEof() && (nIndex < nCount); ++nIndex )
749 0 : maDiscreteItems.push_back( rStrm.readuInt16() );
750 0 : }
751 :
752 0 : const PivotCacheItem* PivotCacheField::getCacheItem( sal_Int32 nItemIdx ) const
753 : {
754 0 : if( hasGroupItems() )
755 0 : return maGroupItems.getCacheItem( nItemIdx );
756 0 : if( hasSharedItems() )
757 0 : return maSharedItems.getCacheItem( nItemIdx );
758 0 : return 0;
759 : }
760 :
761 0 : void PivotCacheField::applyItemCaptions( const IdCaptionPairList& vCaptions )
762 : {
763 0 : if( hasGroupItems() )
764 0 : maGroupItems.applyItemCaptions( vCaptions );
765 0 : if( hasSharedItems() )
766 0 : maSharedItems.applyItemCaptions( vCaptions );
767 0 : }
768 :
769 0 : void PivotCacheField::getCacheItemNames( ::std::vector< OUString >& orItemNames ) const
770 : {
771 0 : if( hasGroupItems() )
772 0 : maGroupItems.getCacheItemNames( orItemNames );
773 0 : else if( hasSharedItems() )
774 0 : maSharedItems.getCacheItemNames( orItemNames );
775 0 : }
776 :
777 0 : PivotCacheItemList PivotCacheField::getCacheItems() const
778 : {
779 0 : if( hasGroupItems() )
780 0 : return maGroupItems;
781 0 : return maSharedItems;
782 : }
783 :
784 0 : void PivotCacheField::convertNumericGrouping( const Reference< XDataPilotField >& rxDPField ) const
785 : {
786 : OSL_ENSURE( hasGroupItems() && hasNumericGrouping(), "PivotCacheField::convertNumericGrouping - not a numeric group field" );
787 0 : PropertySet aPropSet( rxDPField );
788 0 : if( hasGroupItems() && hasNumericGrouping() && aPropSet.is() )
789 : {
790 0 : DataPilotFieldGroupInfo aGroupInfo;
791 0 : aGroupInfo.HasAutoStart = maFieldGroupModel.mbAutoStart;
792 0 : aGroupInfo.HasAutoEnd = maFieldGroupModel.mbAutoEnd;
793 0 : aGroupInfo.HasDateValues = sal_False;
794 0 : aGroupInfo.Start = maFieldGroupModel.mfStartValue;
795 0 : aGroupInfo.End = maFieldGroupModel.mfEndValue;
796 0 : aGroupInfo.Step = maFieldGroupModel.mfInterval;
797 0 : aGroupInfo.GroupBy = 0;
798 0 : aPropSet.setProperty( PROP_GroupInfo, aGroupInfo );
799 0 : }
800 0 : }
801 :
802 0 : OUString PivotCacheField::createDateGroupField( const Reference< XDataPilotField >& rxBaseDPField ) const
803 : {
804 : OSL_ENSURE( hasGroupItems() && hasDateGrouping(), "PivotCacheField::createDateGroupField - not a numeric group field" );
805 0 : Reference< XDataPilotField > xDPGroupField;
806 0 : PropertySet aPropSet( rxBaseDPField );
807 0 : if( hasGroupItems() && hasDateGrouping() && aPropSet.is() )
808 : {
809 0 : bool bDayRanges = (maFieldGroupModel.mnGroupBy == XML_days) && (maFieldGroupModel.mfInterval >= 2.0);
810 :
811 0 : DataPilotFieldGroupInfo aGroupInfo;
812 0 : aGroupInfo.HasAutoStart = maFieldGroupModel.mbAutoStart;
813 0 : aGroupInfo.HasAutoEnd = maFieldGroupModel.mbAutoEnd;
814 0 : aGroupInfo.HasDateValues = sal_True;
815 0 : aGroupInfo.Start = getUnitConverter().calcSerialFromDateTime( maFieldGroupModel.maStartDate );
816 0 : aGroupInfo.End = getUnitConverter().calcSerialFromDateTime( maFieldGroupModel.maEndDate );
817 0 : aGroupInfo.Step = bDayRanges ? maFieldGroupModel.mfInterval : 0.0;
818 :
819 : using namespace ::com::sun::star::sheet::DataPilotFieldGroupBy;
820 0 : switch( maFieldGroupModel.mnGroupBy )
821 : {
822 0 : case XML_years: aGroupInfo.GroupBy = YEARS; break;
823 0 : case XML_quarters: aGroupInfo.GroupBy = QUARTERS; break;
824 0 : case XML_months: aGroupInfo.GroupBy = MONTHS; break;
825 0 : case XML_days: aGroupInfo.GroupBy = DAYS; break;
826 0 : case XML_hours: aGroupInfo.GroupBy = HOURS; break;
827 0 : case XML_minutes: aGroupInfo.GroupBy = MINUTES; break;
828 0 : case XML_seconds: aGroupInfo.GroupBy = SECONDS; break;
829 : default: OSL_FAIL( "PivotCacheField::convertRangeGrouping - unknown date/time interval" );
830 : }
831 :
832 : try
833 : {
834 0 : Reference< XDataPilotFieldGrouping > xDPGrouping( rxBaseDPField, UNO_QUERY_THROW );
835 0 : xDPGroupField = xDPGrouping->createDateGroup( aGroupInfo );
836 : }
837 0 : catch( Exception& )
838 : {
839 0 : }
840 : }
841 :
842 0 : Reference< XNamed > xFieldName( xDPGroupField, UNO_QUERY );
843 0 : return xFieldName.is() ? xFieldName->getName() : OUString();
844 : }
845 :
846 0 : OUString PivotCacheField::createParentGroupField( const Reference< XDataPilotField >& rxBaseDPField, const PivotCacheField& rBaseCacheField, PivotCacheGroupItemVector& orItemNames ) const
847 : {
848 : OSL_ENSURE( hasGroupItems() && !maDiscreteItems.empty(), "PivotCacheField::createParentGroupField - not a group field" );
849 : OSL_ENSURE( maDiscreteItems.size() == orItemNames.size(), "PivotCacheField::createParentGroupField - number of item names does not match grouping info" );
850 0 : Reference< XDataPilotFieldGrouping > xDPGrouping( rxBaseDPField, UNO_QUERY );
851 0 : if( !xDPGrouping.is() ) return OUString();
852 :
853 : // map the group item indexes from maGroupItems to all item indexes from maDiscreteItems
854 : typedef ::std::vector< sal_Int32 > GroupItemList;
855 : typedef ::std::vector< GroupItemList > GroupItemMap;
856 0 : GroupItemMap aItemMap( maGroupItems.size() );
857 0 : for( IndexVector::const_iterator aBeg = maDiscreteItems.begin(), aIt = aBeg, aEnd = maDiscreteItems.end(); aIt != aEnd; ++aIt )
858 : {
859 0 : if( GroupItemList* pItems = ContainerHelper::getVectorElementAccess( aItemMap, *aIt ) )
860 : {
861 0 : if ( const PivotCacheItem* pItem = rBaseCacheField.getCacheItems().getCacheItem( aIt - aBeg ) )
862 : {
863 : // Skip unspecified or ununsed entries or errors
864 0 : if ( pItem->isUnused() || ( pItem->getType() == XML_m ) || ( pItem->getType() == XML_e ) )
865 0 : continue;
866 : }
867 0 : pItems->push_back( static_cast< sal_Int32 >( aIt - aBeg ) );
868 : }
869 : }
870 :
871 : // process all groups
872 0 : Reference< XDataPilotField > xDPGroupField;
873 0 : for( GroupItemMap::iterator aBeg = aItemMap.begin(), aIt = aBeg, aEnd = aItemMap.end(); aIt != aEnd; ++aIt )
874 : {
875 : OSL_ENSURE( !aIt->empty(), "PivotCacheField::createParentGroupField - item/group should not be empty" );
876 0 : if( !aIt->empty() )
877 : {
878 : /* Insert the names of the items that are part of this group. Calc
879 : expects the names of the members of the field whose members are
880 : grouped (which may be the names of groups too). Excel provides
881 : the names of the base field items instead (no group names
882 : involved). Therefore, the passed collection of current item
883 : names as they are already grouped is used here to resolve the
884 : item names. */
885 0 : ::std::vector< OUString > aMembers;
886 0 : for( GroupItemList::iterator aBeg2 = aIt->begin(), aIt2 = aBeg2, aEnd2 = aIt->end(); aIt2 != aEnd2; ++aIt2 )
887 0 : if( const PivotCacheGroupItem* pName = ContainerHelper::getVectorElement( orItemNames, *aIt2 ) )
888 0 : if( ::std::find( aMembers.begin(), aMembers.end(), pName->maGroupName ) == aMembers.end() )
889 0 : aMembers.push_back( pName->maGroupName );
890 :
891 : /* Check again, that this is not just a group that is not grouped
892 : further with other items. */
893 0 : if( !aMembers.empty() ) try
894 : {
895 : // only the first call of createNameGroup() returns the new field
896 0 : Reference< XDataPilotField > xDPNewField = xDPGrouping->createNameGroup( ContainerHelper::vectorToSequence( aMembers ) );
897 : OSL_ENSURE( xDPGroupField.is() != xDPNewField.is(), "PivotCacheField::createParentGroupField - missing group field" );
898 0 : if( !xDPGroupField.is() )
899 0 : xDPGroupField = xDPNewField;
900 :
901 : // get current grouping info
902 0 : DataPilotFieldGroupInfo aGroupInfo;
903 0 : PropertySet aPropSet( xDPGroupField );
904 0 : aPropSet.getProperty( aGroupInfo, PROP_GroupInfo );
905 :
906 : /* Find the group object and the auto-generated group name.
907 : The returned field contains all groups derived from the
908 : previous field if that is grouped too. To find the correct
909 : group, the first item used to create the group is serached.
910 : Calc provides the original item names of the base field
911 : when the group is querried for its members. Its does not
912 : provide the names of members that are already groups in the
913 : field used to create the new groups. (Is this a bug?)
914 : Therefore, a name from the passed list of original item
915 : names is used to find the correct group. */
916 0 : OUString aFirstItem;
917 0 : if( const PivotCacheGroupItem* pName = ContainerHelper::getVectorElement( orItemNames, aIt->front() ) )
918 0 : aFirstItem = pName->maOrigName;
919 0 : Reference< XNamed > xGroupName;
920 0 : OUString aAutoName;
921 0 : Reference< XIndexAccess > xGroupsIA( aGroupInfo.Groups, UNO_QUERY_THROW );
922 0 : for( sal_Int32 nIdx = 0, nCount = xGroupsIA->getCount(); (nIdx < nCount) && (aAutoName.isEmpty()); ++nIdx ) try
923 : {
924 0 : Reference< XNameAccess > xItemsNA( xGroupsIA->getByIndex( nIdx ), UNO_QUERY_THROW );
925 0 : if( xItemsNA->hasByName( aFirstItem ) )
926 : {
927 0 : xGroupName.set( xGroupsIA->getByIndex( nIdx ), UNO_QUERY_THROW );
928 0 : aAutoName = xGroupName->getName();
929 0 : }
930 : }
931 0 : catch( Exception& )
932 : {
933 : }
934 : OSL_ENSURE( !aAutoName.isEmpty(), "PivotCacheField::createParentGroupField - cannot find auto-generated group name" );
935 :
936 : // get the real group name from the list of group items
937 0 : OUString aGroupName;
938 0 : if( const PivotCacheItem* pGroupItem = maGroupItems.getCacheItem( static_cast< sal_Int32 >( aIt - aBeg ) ) )
939 0 : aGroupName = pGroupItem->getName();
940 : OSL_ENSURE( !aGroupName.isEmpty(), "PivotCacheField::createParentGroupField - cannot find group name" );
941 0 : if( aGroupName.isEmpty() )
942 0 : aGroupName = aAutoName;
943 :
944 0 : if( xGroupName.is() && !aGroupName.isEmpty() )
945 : {
946 : // replace the auto-generated group name with the real name
947 0 : if( aAutoName != aGroupName )
948 : {
949 0 : xGroupName->setName( aGroupName );
950 0 : aPropSet.setProperty( PROP_GroupInfo, aGroupInfo );
951 : }
952 : // replace original item names in passed vector with group name
953 0 : for( GroupItemList::iterator aIt2 = aIt->begin(), aEnd2 = aIt->end(); aIt2 != aEnd2; ++aIt2 )
954 0 : if( PivotCacheGroupItem* pName = ContainerHelper::getVectorElementAccess( orItemNames, *aIt2 ) )
955 0 : pName->maGroupName = aGroupName;
956 0 : }
957 : }
958 0 : catch( Exception& )
959 : {
960 0 : }
961 : }
962 : }
963 :
964 0 : Reference< XNamed > xFieldName( xDPGroupField, UNO_QUERY );
965 0 : return xFieldName.is() ? xFieldName->getName() : OUString();
966 : }
967 :
968 0 : void PivotCacheField::writeSourceHeaderCell( WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow ) const
969 : {
970 0 : CellModel aModel;
971 0 : aModel.maCellAddr = CellAddress( rSheetHelper.getSheetIndex(), nCol, nRow );
972 0 : rSheetHelper.getSheetData().setStringCell( aModel, maFieldModel.maName );
973 0 : }
974 :
975 0 : void PivotCacheField::writeSourceDataCell( WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow, const PivotCacheItem& rItem ) const
976 : {
977 0 : bool bHasIndex = rItem.getType() == XML_x;
978 : OSL_ENSURE( bHasIndex != maSharedItems.empty(), "PivotCacheField::writeSourceDataCell - shared items missing or not expected" );
979 0 : if( bHasIndex )
980 0 : writeSharedItemToSourceDataCell( rSheetHelper, nCol, nRow, rItem.getValue().get< sal_Int32 >() );
981 : else
982 0 : writeItemToSourceDataCell( rSheetHelper, nCol, nRow, rItem );
983 0 : }
984 :
985 0 : void PivotCacheField::importPCRecordItem( SequenceInputStream& rStrm, WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow ) const
986 : {
987 0 : if( hasSharedItems() )
988 : {
989 0 : writeSharedItemToSourceDataCell( rSheetHelper, nCol, nRow, rStrm.readInt32() );
990 : }
991 : else
992 : {
993 0 : PivotCacheItem aItem;
994 0 : if( maSharedItemsModel.mbIsNumeric )
995 0 : aItem.readDouble( rStrm );
996 0 : else if( maSharedItemsModel.mbHasDate && !maSharedItemsModel.mbHasString )
997 0 : aItem.readDate( rStrm );
998 : else
999 0 : aItem.readString( rStrm );
1000 0 : writeItemToSourceDataCell( rSheetHelper, nCol, nRow, aItem );
1001 : }
1002 0 : }
1003 :
1004 0 : void PivotCacheField::importPCItemIndex( BiffInputStream& rStrm, WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow ) const
1005 : {
1006 : OSL_ENSURE( hasSharedItems(), "PivotCacheField::importPCItemIndex - invalid call, no shared items found" );
1007 0 : sal_Int32 nIndex = maSharedItemsModel.mbHasLongIndexes ? rStrm.readuInt16() : rStrm.readuInt8();
1008 0 : writeSharedItemToSourceDataCell( rSheetHelper, nCol, nRow, nIndex );
1009 0 : }
1010 :
1011 : // private --------------------------------------------------------------------
1012 :
1013 0 : void PivotCacheField::writeItemToSourceDataCell( WorksheetHelper& rSheetHelper,
1014 : sal_Int32 nCol, sal_Int32 nRow, const PivotCacheItem& rItem ) const
1015 : {
1016 0 : if( rItem.getType() != XML_m )
1017 : {
1018 0 : CellModel aModel;
1019 0 : aModel.maCellAddr = CellAddress( rSheetHelper.getSheetIndex(), nCol, nRow );
1020 0 : SheetDataBuffer& rSheetData = rSheetHelper.getSheetData();
1021 0 : switch( rItem.getType() )
1022 : {
1023 0 : case XML_s: rSheetData.setStringCell( aModel, rItem.getValue().get< OUString >() ); break;
1024 0 : case XML_n: rSheetData.setValueCell( aModel, rItem.getValue().get< double >() ); break;
1025 0 : case XML_i: rSheetData.setValueCell( aModel, rItem.getValue().get< sal_Int16 >() ); break;
1026 0 : case XML_d: rSheetData.setDateTimeCell( aModel, rItem.getValue().get< DateTime >() ); break;
1027 0 : case XML_b: rSheetData.setBooleanCell( aModel, rItem.getValue().get< bool >() ); break;
1028 0 : case XML_e: rSheetData.setErrorCell( aModel, static_cast< sal_uInt8 >( rItem.getValue().get< sal_Int32 >() ) ); break;
1029 : default: OSL_FAIL( "PivotCacheField::writeItemToSourceDataCell - unexpected item data type" );
1030 : }
1031 : }
1032 0 : }
1033 :
1034 0 : void PivotCacheField::writeSharedItemToSourceDataCell(
1035 : WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow, sal_Int32 nItemIdx ) const
1036 : {
1037 0 : if( const PivotCacheItem* pCacheItem = maSharedItems.getCacheItem( nItemIdx ) )
1038 0 : writeItemToSourceDataCell( rSheetHelper, nCol, nRow, *pCacheItem );
1039 0 : }
1040 :
1041 : // ============================================================================
1042 :
1043 0 : PCDefinitionModel::PCDefinitionModel() :
1044 : mfRefreshedDate( 0.0 ),
1045 : mnRecords( 0 ),
1046 : mnMissItemsLimit( 0 ),
1047 : mnDatabaseFields( 0 ),
1048 : mbInvalid( false ),
1049 : mbSaveData( true ),
1050 : mbRefreshOnLoad( false ),
1051 : mbOptimizeMemory( false ),
1052 : mbEnableRefresh( true ),
1053 : mbBackgroundQuery( false ),
1054 : mbUpgradeOnRefresh( false ),
1055 : mbTupleCache( false ),
1056 : mbSupportSubquery( false ),
1057 0 : mbSupportDrill( false )
1058 : {
1059 0 : }
1060 :
1061 : // ----------------------------------------------------------------------------
1062 :
1063 0 : PCSourceModel::PCSourceModel() :
1064 : mnSourceType( XML_TOKEN_INVALID ),
1065 0 : mnConnectionId( 0 )
1066 : {
1067 0 : }
1068 :
1069 : // ----------------------------------------------------------------------------
1070 :
1071 0 : PCWorksheetSourceModel::PCWorksheetSourceModel()
1072 : {
1073 0 : maRange.StartColumn = maRange.StartRow = maRange.EndColumn = maRange.EndRow = -1;
1074 0 : }
1075 :
1076 : // ----------------------------------------------------------------------------
1077 :
1078 0 : PivotCache::PivotCache( const WorkbookHelper& rHelper ) :
1079 : WorkbookHelper( rHelper ),
1080 : mnCurrRow( -1 ),
1081 : mbValidSource( false ),
1082 0 : mbDummySheet( false )
1083 : {
1084 0 : }
1085 :
1086 0 : void PivotCache::importPivotCacheDefinition( const AttributeList& rAttribs )
1087 : {
1088 0 : maDefModel.maRelId = rAttribs.getString( R_TOKEN( id ), OUString() );
1089 0 : maDefModel.maRefreshedBy = rAttribs.getXString( XML_refreshedBy, OUString() );
1090 0 : maDefModel.mfRefreshedDate = rAttribs.getDouble( XML_refreshedDate, 0.0 );
1091 0 : maDefModel.mnRecords = rAttribs.getInteger( XML_recordCount, 0 );
1092 0 : maDefModel.mnMissItemsLimit = rAttribs.getInteger( XML_missingItemsLimit, 0 );
1093 0 : maDefModel.mbInvalid = rAttribs.getBool( XML_invalid, false );
1094 0 : maDefModel.mbSaveData = rAttribs.getBool( XML_saveData, true );
1095 0 : maDefModel.mbRefreshOnLoad = rAttribs.getBool( XML_refreshOnLoad, false );
1096 0 : maDefModel.mbOptimizeMemory = rAttribs.getBool( XML_optimizeMemory, false );
1097 0 : maDefModel.mbEnableRefresh = rAttribs.getBool( XML_enableRefresh, true );
1098 0 : maDefModel.mbBackgroundQuery = rAttribs.getBool( XML_backgroundQuery, false );
1099 0 : maDefModel.mbUpgradeOnRefresh = rAttribs.getBool( XML_upgradeOnRefresh, false );
1100 0 : maDefModel.mbTupleCache = rAttribs.getBool( XML_tupleCache, false );
1101 0 : maDefModel.mbSupportSubquery = rAttribs.getBool( XML_supportSubquery, false );
1102 0 : maDefModel.mbSupportDrill = rAttribs.getBool( XML_supportAdvancedDrill, false );
1103 0 : }
1104 :
1105 0 : void PivotCache::importCacheSource( const AttributeList& rAttribs )
1106 : {
1107 0 : maSourceModel.mnSourceType = rAttribs.getToken( XML_type, XML_TOKEN_INVALID );
1108 0 : maSourceModel.mnConnectionId = rAttribs.getInteger( XML_connectionId, 0 );
1109 0 : }
1110 :
1111 0 : void PivotCache::importWorksheetSource( const AttributeList& rAttribs, const Relations& rRelations )
1112 : {
1113 0 : maSheetSrcModel.maRelId = rAttribs.getString( R_TOKEN( id ), OUString() );
1114 0 : maSheetSrcModel.maSheet = rAttribs.getXString( XML_sheet, OUString() );
1115 0 : maSheetSrcModel.maDefName = rAttribs.getXString( XML_name, OUString() );
1116 :
1117 : // resolve URL of external document
1118 0 : maTargetUrl = rRelations.getExternalTargetFromRelId( maSheetSrcModel.maRelId );
1119 : // store range address unchecked with sheet index 0, will be resolved/checked later
1120 0 : getAddressConverter().convertToCellRangeUnchecked( maSheetSrcModel.maRange, rAttribs.getString( XML_ref, OUString() ), 0 );
1121 0 : }
1122 :
1123 0 : void PivotCache::importPCDefinition( SequenceInputStream& rStrm )
1124 : {
1125 : sal_uInt8 nFlags1, nFlags2;
1126 0 : rStrm.skip( 3 ); // create/refresh version id's
1127 0 : rStrm >> nFlags1 >> maDefModel.mnMissItemsLimit >> maDefModel.mfRefreshedDate >> nFlags2 >> maDefModel.mnRecords;
1128 0 : if( getFlag( nFlags2, BIFF12_PCDEFINITION_HASUSERNAME ) )
1129 0 : rStrm >> maDefModel.maRefreshedBy;
1130 0 : if( getFlag( nFlags2, BIFF12_PCDEFINITION_HASRELID ) )
1131 0 : rStrm >> maDefModel.maRelId;
1132 :
1133 0 : maDefModel.mbInvalid = getFlag( nFlags1, BIFF12_PCDEFINITION_INVALID );
1134 0 : maDefModel.mbSaveData = getFlag( nFlags1, BIFF12_PCDEFINITION_SAVEDATA );
1135 0 : maDefModel.mbRefreshOnLoad = getFlag( nFlags1, BIFF12_PCDEFINITION_REFRESHONLOAD );
1136 0 : maDefModel.mbOptimizeMemory = getFlag( nFlags1, BIFF12_PCDEFINITION_OPTIMIZEMEMORY );
1137 0 : maDefModel.mbEnableRefresh = getFlag( nFlags1, BIFF12_PCDEFINITION_ENABLEREFRESH );
1138 0 : maDefModel.mbBackgroundQuery = getFlag( nFlags1, BIFF12_PCDEFINITION_BACKGROUNDQUERY );
1139 0 : maDefModel.mbUpgradeOnRefresh = getFlag( nFlags1, BIFF12_PCDEFINITION_UPGRADEONREFR );
1140 0 : maDefModel.mbTupleCache = getFlag( nFlags1, BIFF12_PCDEFINITION_TUPELCACHE );
1141 0 : maDefModel.mbSupportSubquery = getFlag( nFlags2, BIFF12_PCDEFINITION_SUPPORTSUBQUERY );
1142 0 : maDefModel.mbSupportDrill = getFlag( nFlags2, BIFF12_PCDEFINITION_SUPPORTDRILL );
1143 0 : }
1144 :
1145 0 : void PivotCache::importPCDSource( SequenceInputStream& rStrm )
1146 : {
1147 : sal_Int32 nSourceType;
1148 0 : rStrm >> nSourceType >> maSourceModel.mnConnectionId;
1149 : static const sal_Int32 spnSourceTypes[] = { XML_worksheet, XML_external, XML_consolidation, XML_scenario };
1150 0 : maSourceModel.mnSourceType = STATIC_ARRAY_SELECT( spnSourceTypes, nSourceType, XML_TOKEN_INVALID );
1151 0 : }
1152 :
1153 0 : void PivotCache::importPCDSheetSource( SequenceInputStream& rStrm, const Relations& rRelations )
1154 : {
1155 : sal_uInt8 nIsDefName, nIsBuiltinName, nFlags;
1156 0 : rStrm >> nIsDefName >> nIsBuiltinName >> nFlags;
1157 0 : if( getFlag( nFlags, BIFF12_PCDWBSOURCE_HASSHEET ) )
1158 0 : rStrm >> maSheetSrcModel.maSheet;
1159 0 : if( getFlag( nFlags, BIFF12_PCDWBSOURCE_HASRELID ) )
1160 0 : rStrm >> maSheetSrcModel.maRelId;
1161 :
1162 : // read cell range or defined name
1163 0 : if( nIsDefName == 0 )
1164 : {
1165 0 : BinRange aBinRange;
1166 0 : rStrm >> aBinRange;
1167 : // store range address unchecked with sheet index 0, will be resolved/checked later
1168 0 : getAddressConverter().convertToCellRangeUnchecked( maSheetSrcModel.maRange, aBinRange, 0 );
1169 : }
1170 : else
1171 : {
1172 0 : rStrm >> maSheetSrcModel.maDefName;
1173 0 : if( nIsBuiltinName != 0 )
1174 0 : maSheetSrcModel.maDefName = "_xlnm." + maSheetSrcModel.maDefName;
1175 : }
1176 :
1177 : // resolve URL of external document
1178 0 : maTargetUrl = rRelations.getExternalTargetFromRelId( maSheetSrcModel.maRelId );
1179 0 : }
1180 :
1181 0 : void PivotCache::importPCDefinition( BiffInputStream& rStrm )
1182 : {
1183 : sal_uInt16 nFlags, nUserNameLen;
1184 0 : rStrm >> maDefModel.mnRecords;
1185 0 : rStrm.skip( 2 ); // repeated cache ID
1186 0 : rStrm >> nFlags;
1187 0 : rStrm.skip( 2 ); // unused
1188 0 : rStrm >> maDefModel.mnDatabaseFields;
1189 0 : rStrm.skip( 6 ); // total field count, report record count, (repeated) cache type
1190 0 : rStrm >> nUserNameLen;
1191 0 : if( nUserNameLen != BIFF_PC_NOSTRING )
1192 0 : maDefModel.maRefreshedBy = (getBiff() == BIFF8) ?
1193 : rStrm.readUniString( nUserNameLen ) :
1194 0 : rStrm.readCharArrayUC( nUserNameLen, getTextEncoding() );
1195 :
1196 0 : maDefModel.mbInvalid = getFlag( nFlags, BIFF_PCDEFINITION_INVALID );
1197 0 : maDefModel.mbSaveData = getFlag( nFlags, BIFF_PCDEFINITION_SAVEDATA );
1198 0 : maDefModel.mbRefreshOnLoad = getFlag( nFlags, BIFF_PCDEFINITION_REFRESHONLOAD );
1199 0 : maDefModel.mbOptimizeMemory = getFlag( nFlags, BIFF_PCDEFINITION_OPTIMIZEMEMORY );
1200 0 : maDefModel.mbEnableRefresh = getFlag( nFlags, BIFF_PCDEFINITION_ENABLEREFRESH );
1201 0 : maDefModel.mbBackgroundQuery = getFlag( nFlags, BIFF_PCDEFINITION_BACKGROUNDQUERY );
1202 :
1203 0 : if( (rStrm.getNextRecId() == BIFF_ID_PCDEFINITION2) && rStrm.startNextRecord() )
1204 0 : rStrm >> maDefModel.mfRefreshedDate;
1205 0 : }
1206 :
1207 0 : PivotCacheField& PivotCache::createCacheField( bool bInitDatabaseField )
1208 : {
1209 0 : bool bIsDatabaseField = !bInitDatabaseField || (maFields.size() < maDefModel.mnDatabaseFields);
1210 0 : PivotCacheFieldVector::value_type xCacheField( new PivotCacheField( *this, bIsDatabaseField ) );
1211 0 : maFields.push_back( xCacheField );
1212 0 : return *xCacheField;
1213 : }
1214 :
1215 0 : void PivotCache::finalizeImport()
1216 : {
1217 : // collect all fields that are based on source data (needed to finalize source data below)
1218 : OSL_ENSURE( !maFields.empty(), "PivotCache::finalizeImport - no pivot cache fields found" );
1219 0 : for( PivotCacheFieldVector::const_iterator aIt = maFields.begin(), aEnd = maFields.end(); aIt != aEnd; ++aIt )
1220 : {
1221 0 : if( (*aIt)->isDatabaseField() )
1222 : {
1223 : OSL_ENSURE( (aIt == maFields.begin()) || (*(aIt - 1))->isDatabaseField(),
1224 : "PivotCache::finalizeImport - database field follows a calculated field" );
1225 0 : maDatabaseIndexes.push_back( static_cast< sal_Int32 >( maDatabaseFields.size() ) );
1226 0 : maDatabaseFields.push_back( *aIt );
1227 : }
1228 : else
1229 : {
1230 0 : maDatabaseIndexes.push_back( -1 );
1231 : }
1232 : }
1233 : OSL_ENSURE( !maDatabaseFields.empty(), "PivotCache::finalizeImport - no pivot cache source fields found" );
1234 :
1235 : // finalize source data depending on source type
1236 0 : switch( maSourceModel.mnSourceType )
1237 : {
1238 : case XML_worksheet:
1239 : {
1240 : // decide whether an external document is used
1241 0 : bool bInternal = maTargetUrl.isEmpty() && maSheetSrcModel.maRelId.isEmpty();
1242 0 : bool bExternal = !maTargetUrl.isEmpty(); // relation ID may be empty, e.g. BIFF import
1243 : OSL_ENSURE( bInternal || bExternal, "PivotCache::finalizeImport - invalid external document URL" );
1244 0 : if( bInternal )
1245 0 : finalizeInternalSheetSource();
1246 0 : else if( bExternal )
1247 0 : finalizeExternalSheetSource();
1248 : }
1249 0 : break;
1250 :
1251 : // currently, we only support worksheet data sources
1252 : case XML_external:
1253 0 : break;
1254 : case XML_consolidation:
1255 0 : break;
1256 : case XML_scenario:
1257 0 : break;
1258 : }
1259 0 : }
1260 :
1261 0 : sal_Int32 PivotCache::getCacheFieldCount() const
1262 : {
1263 0 : return static_cast< sal_Int32 >( maFields.size() );
1264 : }
1265 :
1266 0 : const PivotCacheField* PivotCache::getCacheField( sal_Int32 nFieldIdx ) const
1267 : {
1268 0 : return maFields.get( nFieldIdx ).get();
1269 : }
1270 :
1271 0 : sal_Int32 PivotCache::getCacheDatabaseIndex( sal_Int32 nFieldIdx ) const
1272 : {
1273 0 : return ContainerHelper::getVectorElement( maDatabaseIndexes, nFieldIdx, -1 );
1274 : }
1275 :
1276 0 : void PivotCache::writeSourceHeaderCells( WorksheetHelper& rSheetHelper ) const
1277 : {
1278 : OSL_ENSURE( static_cast< size_t >( maSheetSrcModel.maRange.EndColumn - maSheetSrcModel.maRange.StartColumn + 1 ) == maDatabaseFields.size(),
1279 : "PivotCache::writeSourceHeaderCells - source cell range width does not match number of source fields" );
1280 0 : sal_Int32 nCol = maSheetSrcModel.maRange.StartColumn;
1281 0 : sal_Int32 nMaxCol = getAddressConverter().getMaxApiAddress().Column;
1282 0 : sal_Int32 nRow = maSheetSrcModel.maRange.StartRow;
1283 0 : mnCurrRow = -1;
1284 0 : updateSourceDataRow( rSheetHelper, nRow );
1285 0 : for( PivotCacheFieldVector::const_iterator aIt = maDatabaseFields.begin(), aEnd = maDatabaseFields.end(); (aIt != aEnd) && (nCol <= nMaxCol); ++aIt, ++nCol )
1286 0 : (*aIt)->writeSourceHeaderCell( rSheetHelper, nCol, nRow );
1287 0 : }
1288 :
1289 0 : void PivotCache::writeSourceDataCell( WorksheetHelper& rSheetHelper, sal_Int32 nColIdx, sal_Int32 nRowIdx, const PivotCacheItem& rItem ) const
1290 : {
1291 0 : sal_Int32 nCol = maSheetSrcModel.maRange.StartColumn + nColIdx;
1292 : OSL_ENSURE( (maSheetSrcModel.maRange.StartColumn <= nCol) && (nCol <= maSheetSrcModel.maRange.EndColumn), "PivotCache::writeSourceDataCell - invalid column index" );
1293 0 : sal_Int32 nRow = maSheetSrcModel.maRange.StartRow + nRowIdx;
1294 : OSL_ENSURE( (maSheetSrcModel.maRange.StartRow < nRow) && (nRow <= maSheetSrcModel.maRange.EndRow), "PivotCache::writeSourceDataCell - invalid row index" );
1295 0 : updateSourceDataRow( rSheetHelper, nRow );
1296 0 : if( const PivotCacheField* pCacheField = maDatabaseFields.get( nColIdx ).get() )
1297 0 : pCacheField->writeSourceDataCell( rSheetHelper, nCol, nRow, rItem );
1298 0 : }
1299 :
1300 0 : void PivotCache::importPCRecord( SequenceInputStream& rStrm, WorksheetHelper& rSheetHelper, sal_Int32 nRowIdx ) const
1301 : {
1302 0 : sal_Int32 nRow = maSheetSrcModel.maRange.StartRow + nRowIdx;
1303 : OSL_ENSURE( (maSheetSrcModel.maRange.StartRow < nRow) && (nRow <= maSheetSrcModel.maRange.EndRow), "PivotCache::importPCRecord - invalid row index" );
1304 0 : sal_Int32 nCol = maSheetSrcModel.maRange.StartColumn;
1305 0 : sal_Int32 nMaxCol = getAddressConverter().getMaxApiAddress().Column;
1306 0 : for( PivotCacheFieldVector::const_iterator aIt = maDatabaseFields.begin(), aEnd = maDatabaseFields.end(); !rStrm.isEof() && (aIt != aEnd) && (nCol <= nMaxCol); ++aIt, ++nCol )
1307 0 : (*aIt)->importPCRecordItem( rStrm, rSheetHelper, nCol, nRow );
1308 0 : }
1309 :
1310 0 : void PivotCache::importPCItemIndexList( BiffInputStream& rStrm, WorksheetHelper& rSheetHelper, sal_Int32 nRowIdx ) const
1311 : {
1312 0 : sal_Int32 nRow = maSheetSrcModel.maRange.StartRow + nRowIdx;
1313 : OSL_ENSURE( (maSheetSrcModel.maRange.StartRow < nRow) && (nRow <= maSheetSrcModel.maRange.EndRow), "PivotCache::importPCItemIndexList - invalid row index" );
1314 0 : sal_Int32 nCol = maSheetSrcModel.maRange.StartColumn;
1315 0 : sal_Int32 nMaxCol = getAddressConverter().getMaxApiAddress().Column;
1316 0 : for( PivotCacheFieldVector::const_iterator aIt = maDatabaseFields.begin(), aEnd = maDatabaseFields.end(); !rStrm.isEof() && (aIt != aEnd) && (nCol <= nMaxCol); ++aIt, ++nCol )
1317 0 : if( (*aIt)->hasSharedItems() )
1318 0 : (*aIt)->importPCItemIndex( rStrm, rSheetHelper, nCol, nRow );
1319 0 : }
1320 :
1321 : // private --------------------------------------------------------------------
1322 :
1323 0 : void PivotCache::finalizeInternalSheetSource()
1324 : {
1325 : // resolve sheet name to sheet index
1326 0 : sal_Int16 nSheet = getWorksheets().getCalcSheetIndex( maSheetSrcModel.maSheet );
1327 :
1328 : // if cache is based on a defined name or table, try to resolve to cell range
1329 0 : if( !maSheetSrcModel.maDefName.isEmpty() )
1330 : {
1331 : // local or global defined name
1332 0 : if( const DefinedName* pDefName = getDefinedNames().getByModelName( maSheetSrcModel.maDefName, nSheet ).get() )
1333 : {
1334 0 : mbValidSource = pDefName->getAbsoluteRange( maSheetSrcModel.maRange );
1335 : }
1336 : // table
1337 0 : else if( const Table* pTable = getTables().getTable( maSheetSrcModel.maDefName ).get() )
1338 : {
1339 : // get original range from table, but exclude the totals row(s)
1340 0 : maSheetSrcModel.maRange = pTable->getOriginalRange();
1341 0 : mbValidSource = (pTable->getHeight() - pTable->getTotalsRows()) > 1;
1342 0 : if( mbValidSource )
1343 0 : maSheetSrcModel.maRange.EndRow -= pTable->getTotalsRows();
1344 : }
1345 : }
1346 : // else try the cell range (if the sheet exists)
1347 0 : else if( nSheet >= 0 )
1348 : {
1349 : // insert sheet index into the range, range address will be checked below
1350 0 : maSheetSrcModel.maRange.Sheet = nSheet;
1351 0 : mbValidSource = true;
1352 : }
1353 : // else sheet has been deleted, generate the source data from cache
1354 0 : else if( !maSheetSrcModel.maSheet.isEmpty() )
1355 : {
1356 0 : prepareSourceDataSheet();
1357 : // return here to skip the source range check below
1358 0 : return;
1359 : }
1360 :
1361 : // check range location, do not allow ranges that overflow the sheet partly
1362 0 : mbValidSource = mbValidSource &&
1363 0 : getAddressConverter().checkCellRange( maSheetSrcModel.maRange, false, true ) &&
1364 0 : (maSheetSrcModel.maRange.StartRow < maSheetSrcModel.maRange.EndRow);
1365 : }
1366 :
1367 0 : void PivotCache::finalizeExternalSheetSource()
1368 : {
1369 : /* If pivot cache is based on external sheet data, try to restore sheet
1370 : data from cache records. No support for external defined names or tables,
1371 : sheet name and path to cache records fragment (OOXML only) are required. */
1372 0 : bool bHasRelation = (getFilterType() == FILTER_BIFF) || !maDefModel.maRelId.isEmpty();
1373 0 : if( bHasRelation && maSheetSrcModel.maDefName.isEmpty() && !maSheetSrcModel.maSheet.isEmpty() )
1374 0 : prepareSourceDataSheet();
1375 0 : }
1376 :
1377 0 : void PivotCache::prepareSourceDataSheet()
1378 : {
1379 0 : CellRangeAddress& rRange = maSheetSrcModel.maRange;
1380 : // data will be inserted in top-left cell, sheet index is still set to 0 (will be set below)
1381 0 : rRange.EndColumn -= rRange.StartColumn;
1382 0 : rRange.StartColumn = 0;
1383 0 : rRange.EndRow -= rRange.StartRow;
1384 0 : rRange.StartRow = 0;
1385 : // check range location, do not allow ranges that overflow the sheet partly
1386 0 : if( getAddressConverter().checkCellRange( rRange, false, true ) )
1387 : {
1388 0 : maColSpans.insert( ValueRange( rRange.StartColumn, rRange.EndColumn ) );
1389 0 : OUString aSheetName = "DPCache_" + maSheetSrcModel.maSheet;
1390 0 : rRange.Sheet = getWorksheets().insertEmptySheet( aSheetName, false );
1391 0 : mbValidSource = mbDummySheet = rRange.Sheet >= 0;
1392 : }
1393 0 : }
1394 :
1395 0 : void PivotCache::updateSourceDataRow( WorksheetHelper& rSheetHelper, sal_Int32 nRow ) const
1396 : {
1397 0 : if( mnCurrRow != nRow )
1398 : {
1399 0 : rSheetHelper.getSheetData().setColSpans( nRow, maColSpans );
1400 0 : mnCurrRow = nRow;
1401 : }
1402 0 : }
1403 :
1404 : // ============================================================================
1405 :
1406 21 : PivotCacheBuffer::PivotCacheBuffer( const WorkbookHelper& rHelper ) :
1407 21 : WorkbookHelper( rHelper )
1408 : {
1409 21 : }
1410 :
1411 0 : void PivotCacheBuffer::registerPivotCacheFragment( sal_Int32 nCacheId, const OUString& rFragmentPath )
1412 : {
1413 : OSL_ENSURE( nCacheId >= 0, "PivotCacheBuffer::registerPivotCacheFragment - invalid pivot cache identifier" );
1414 : OSL_ENSURE( maFragmentPaths.count( nCacheId ) == 0, "PivotCacheBuffer::registerPivotCacheFragment - fragment path exists already" );
1415 0 : if( (nCacheId >= 0) && !rFragmentPath.isEmpty() )
1416 0 : maFragmentPaths[ nCacheId ] = rFragmentPath;
1417 0 : }
1418 :
1419 0 : PivotCache* PivotCacheBuffer::importPivotCacheFragment( sal_Int32 nCacheId )
1420 : {
1421 0 : switch( getFilterType() )
1422 : {
1423 : /* OOXML/BIFF12 filter: On first call for the cache ID, the pivot
1424 : cache object is created and inserted into maCaches. Then, the cache
1425 : definition fragment is read and the cache is returned. On
1426 : subsequent calls, the created cache will be found in maCaches and
1427 : returned immediately. */
1428 : case FILTER_OOXML:
1429 : {
1430 : // try to find an imported pivot cache
1431 0 : if( PivotCache* pCache = maCaches.get( nCacheId ).get() )
1432 0 : return pCache;
1433 :
1434 : // check if a fragment path exists for the passed cache identifier
1435 0 : FragmentPathMap::iterator aIt = maFragmentPaths.find( nCacheId );
1436 0 : if( aIt == maFragmentPaths.end() )
1437 0 : return 0;
1438 :
1439 : /* Import the cache fragment. This may create a dummy data sheet
1440 : for external sheet sources. */
1441 0 : PivotCache& rCache = createPivotCache( nCacheId );
1442 0 : importOoxFragment( new PivotCacheDefinitionFragment( *this, aIt->second, rCache ) );
1443 0 : return &rCache;
1444 : }
1445 :
1446 : /* BIFF filter: Pivot table provides 0-based index into list of pivot
1447 : cache source links (PIVOTCACHE/PCDSOURCE/... record blocks in
1448 : workbook stream). First, this index has to be resolved to the cache
1449 : identifier that is used to manage the cache stream names (the
1450 : maFragmentPaths member). The cache object itself exists already
1451 : before the first call for the cache source index, because source data
1452 : link is part of workbook data, not of the cache stream. To detect
1453 : subsequent calls with an already initialized cache, the entry in
1454 : maFragmentPaths will be removed after reading the cache stream. */
1455 : case FILTER_BIFF:
1456 : {
1457 : /* Resolve cache index to cache identifier and try to find pivot
1458 : cache. Cache must exist already for a valid cache index. */
1459 0 : nCacheId = ContainerHelper::getVectorElement( maCacheIds, nCacheId, -1 );
1460 0 : PivotCache* pCache = maCaches.get( nCacheId ).get();
1461 0 : if( !pCache )
1462 0 : return 0;
1463 :
1464 : /* Try to find fragment path entry (stream name). If missing, the
1465 : stream has been read already, and the cache can be returned. */
1466 0 : FragmentPathMap::iterator aIt = maFragmentPaths.find( nCacheId );
1467 0 : if( aIt != maFragmentPaths.end() )
1468 : {
1469 : /* Import the cache stream. This may create a dummy data sheet
1470 : for external sheet sources. */
1471 0 : BiffPivotCacheFragment( *this, aIt->second, *pCache ).importFragment();
1472 : // remove the fragment entry to mark that the cache is initialized
1473 0 : maFragmentPaths.erase( aIt );
1474 : }
1475 0 : return pCache;
1476 : }
1477 :
1478 : case FILTER_UNKNOWN:
1479 : OSL_FAIL( "PivotCacheBuffer::importPivotCacheFragment - unknown filter type" );
1480 : }
1481 0 : return 0;
1482 : }
1483 :
1484 0 : PivotCache& PivotCacheBuffer::createPivotCache( sal_Int32 nCacheId )
1485 : {
1486 0 : maCacheIds.push_back( nCacheId );
1487 0 : PivotCacheMap::mapped_type& rxCache = maCaches[ nCacheId ];
1488 0 : rxCache.reset( new PivotCache( *this ) );
1489 0 : return *rxCache;
1490 : }
1491 :
1492 : // ============================================================================
1493 :
1494 : } // namespace xls
1495 15 : } // namespace oox
1496 :
1497 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|