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