merge the formfield patch from ooo-build
[ooovba.git] / oox / source / xls / pivotcachebuffer.cxx
blob7b503142a89ea0051ff2943d7bef719971a5aac5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: pivottablebuffer.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "oox/xls/pivotcachebuffer.hxx"
32 #include <set>
33 #include <rtl/ustrbuf.hxx>
34 #include <com/sun/star/container/XIndexAccess.hpp>
35 #include <com/sun/star/container/XNameAccess.hpp>
36 #include <com/sun/star/container/XNamed.hpp>
37 #include <com/sun/star/table/XCell.hpp>
38 #include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp>
39 #include <com/sun/star/sheet/DataPilotFieldGroupInfo.hpp>
40 #include <com/sun/star/sheet/XDataPilotFieldGrouping.hpp>
41 #include "properties.hxx"
42 #include "oox/helper/attributelist.hxx"
43 #include "oox/helper/propertyset.hxx"
44 #include "oox/helper/recordinputstream.hxx"
45 #include "oox/core/filterbase.hxx"
46 #include "oox/xls/biffinputstream.hxx"
47 #include "oox/xls/defnamesbuffer.hxx"
48 #include "oox/xls/excelhandlers.hxx"
49 #include "oox/xls/pivotcachefragment.hxx"
50 #include "oox/xls/tablebuffer.hxx"
51 #include "oox/xls/unitconverter.hxx"
52 #include "oox/xls/worksheetbuffer.hxx"
54 using ::rtl::OUString;
55 using ::rtl::OUStringBuffer;
56 using ::com::sun::star::uno::Any;
57 using ::com::sun::star::uno::Exception;
58 using ::com::sun::star::uno::Reference;
59 using ::com::sun::star::uno::UNO_QUERY;
60 using ::com::sun::star::uno::UNO_QUERY_THROW;
61 using ::com::sun::star::container::XIndexAccess;
62 using ::com::sun::star::container::XNameAccess;
63 using ::com::sun::star::container::XNamed;
64 using ::com::sun::star::util::DateTime;
65 using ::com::sun::star::table::CellAddress;
66 using ::com::sun::star::sheet::DataPilotFieldGroupInfo;
67 using ::com::sun::star::table::XCell;
68 using ::com::sun::star::sheet::XDataPilotField;
69 using ::com::sun::star::sheet::XDataPilotFieldGrouping;
70 using ::oox::core::Relations;
72 namespace oox {
73 namespace xls {
75 // ============================================================================
77 namespace {
79 const sal_uInt16 OOBIN_PCDFIELD_SERVERFIELD = 0x0001;
80 const sal_uInt16 OOBIN_PCDFIELD_NOUNIQUEITEMS = 0x0002;
81 const sal_uInt16 OOBIN_PCDFIELD_DATABASEFIELD = 0x0004;
82 const sal_uInt16 OOBIN_PCDFIELD_HASCAPTION = 0x0008;
83 const sal_uInt16 OOBIN_PCDFIELD_MEMBERPROPFIELD = 0x0010;
84 const sal_uInt16 OOBIN_PCDFIELD_HASFORMULA = 0x0100;
85 const sal_uInt16 OOBIN_PCDFIELD_HASPROPERTYNAME = 0x0200;
87 const sal_uInt16 OOBIN_PCDFSITEMS_HASSEMIMIXED = 0x0001;
88 const sal_uInt16 OOBIN_PCDFSITEMS_HASNONDATE = 0x0002;
89 const sal_uInt16 OOBIN_PCDFSITEMS_HASDATE = 0x0004;
90 const sal_uInt16 OOBIN_PCDFSITEMS_HASSTRING = 0x0008;
91 const sal_uInt16 OOBIN_PCDFSITEMS_HASBLANK = 0x0010;
92 const sal_uInt16 OOBIN_PCDFSITEMS_HASMIXED = 0x0020;
93 const sal_uInt16 OOBIN_PCDFSITEMS_ISNUMERIC = 0x0040;
94 const sal_uInt16 OOBIN_PCDFSITEMS_ISINTEGER = 0x0080;
95 const sal_uInt16 OOBIN_PCDFSITEMS_HASMINMAX = 0x0100;
96 const sal_uInt16 OOBIN_PCDFSITEMS_HASLONGTEXT = 0x0200;
98 const sal_uInt16 OOBIN_PCITEM_ARRAY_DOUBLE = 0x0001;
99 const sal_uInt16 OOBIN_PCITEM_ARRAY_STRING = 0x0002;
100 const sal_uInt16 OOBIN_PCITEM_ARRAY_ERROR = 0x0010;
101 const sal_uInt16 OOBIN_PCITEM_ARRAY_DATE = 0x0020;
103 const sal_uInt8 OOBIN_PCDFRANGEPR_AUTOSTART = 0x01;
104 const sal_uInt8 OOBIN_PCDFRANGEPR_AUTOEND = 0x02;
105 const sal_uInt8 OOBIN_PCDFRANGEPR_DATEGROUP = 0x04;
107 const sal_uInt8 OOBIN_PCDEFINITION_SAVEDATA = 0x01;
108 const sal_uInt8 OOBIN_PCDEFINITION_INVALID = 0x02;
109 const sal_uInt8 OOBIN_PCDEFINITION_REFRESHONLOAD = 0x04;
110 const sal_uInt8 OOBIN_PCDEFINITION_OPTIMIZEMEMORY = 0x08;
111 const sal_uInt8 OOBIN_PCDEFINITION_ENABLEREFRESH = 0x10;
112 const sal_uInt8 OOBIN_PCDEFINITION_BACKGROUNDQUERY = 0x20;
113 const sal_uInt8 OOBIN_PCDEFINITION_UPGRADEONREFR = 0x40;
114 const sal_uInt8 OOBIN_PCDEFINITION_TUPELCACHE = 0x80;
116 const sal_uInt8 OOBIN_PCDEFINITION_HASUSERNAME = 0x01;
117 const sal_uInt8 OOBIN_PCDEFINITION_HASRELID = 0x02;
118 const sal_uInt8 OOBIN_PCDEFINITION_SUPPORTSUBQUERY = 0x04;
119 const sal_uInt8 OOBIN_PCDEFINITION_SUPPORTDRILL = 0x08;
121 const sal_uInt8 OOBIN_PCDWBSOURCE_HASRELID = 0x01;
122 const sal_uInt8 OOBIN_PCDWBSOURCE_HASSHEET = 0x02;
124 const sal_uInt16 BIFF_PCDSOURCE_WORKSHEET = 0x0001;
125 const sal_uInt16 BIFF_PCDSOURCE_EXTERNAL = 0x0002;
126 const sal_uInt16 BIFF_PCDSOURCE_CONSOLIDATION = 0x0004;
127 const sal_uInt16 BIFF_PCDSOURCE_SCENARIO = 0x0010;
129 const sal_uInt16 BIFF_PC_NOSTRING = 0xFFFF;
131 const sal_uInt16 BIFF_PCDFIELD_HASITEMS = 0x0001;
132 const sal_uInt16 BIFF_PCDFIELD_HASUNSHAREDITEMS = 0x0002;
133 const sal_uInt16 BIFF_PCDFIELD_CALCULATED = 0x0004;
134 const sal_uInt16 BIFF_PCDFIELD_HASPARENT = 0x0008;
135 const sal_uInt16 BIFF_PCDFIELD_RANGEGROUP = 0x0010;
136 const sal_uInt16 BIFF_PCDFIELD_ISNUMERIC = 0x0020;
137 const sal_uInt16 BIFF_PCDFIELD_HASSEMIMIXED = 0x0080;
138 const sal_uInt16 BIFF_PCDFIELD_HASMINMAX = 0x0100;
139 const sal_uInt16 BIFF_PCDFIELD_HASLONGINDEX = 0x0200;
140 const sal_uInt16 BIFF_PCDFIELD_HASNONDATE = 0x0400;
141 const sal_uInt16 BIFF_PCDFIELD_HASDATE = 0x0800;
142 const sal_uInt16 BIFF_PCDFIELD_SERVERFIELD = 0x2000;
143 const sal_uInt16 BIFF_PCDFIELD_NOUNIQUEITEMS = 0x4000;
145 const sal_uInt16 BIFF_PCDFRANGEPR_AUTOSTART = 0x0001;
146 const sal_uInt16 BIFF_PCDFRANGEPR_AUTOEND = 0x0002;
148 const sal_uInt16 BIFF_PCDEFINITION_SAVEDATA = 0x0001;
149 const sal_uInt16 BIFF_PCDEFINITION_INVALID = 0x0002;
150 const sal_uInt16 BIFF_PCDEFINITION_REFRESHONLOAD = 0x0004;
151 const sal_uInt16 BIFF_PCDEFINITION_OPTIMIZEMEMORY = 0x0008;
152 const sal_uInt16 BIFF_PCDEFINITION_BACKGROUNDQUERY = 0x0010;
153 const sal_uInt16 BIFF_PCDEFINITION_ENABLEREFRESH = 0x0020;
155 // ----------------------------------------------------------------------------
157 /** Adjusts the weird date format read from binary streams.
159 Dates before 1900-Mar-01 are stored including the non-existing leap day
160 1900-02-29. Time values (without date) are stored as times of day
161 1900-Jan-00. Nothing has to be done when the workbook is stored in 1904
162 date mode (dates before 1904-Jan-01 will not occur in this case).
164 void lclAdjustBinDateTime( DateTime& orDateTime )
166 if( (orDateTime.Year == 1900) && (orDateTime.Month <= 2) )
168 OSL_ENSURE( (orDateTime.Month == 1) || ((orDateTime.Month == 2) && (orDateTime.Day > 0)), "lclAdjustBinDateTime - invalid date" );
169 switch( orDateTime.Month )
171 case 2: if( orDateTime.Day > 1 ) --orDateTime.Day; else { orDateTime.Day += 30; --orDateTime.Month; } break;
172 case 1: if( orDateTime.Day > 1 ) --orDateTime.Day; else { orDateTime.Day += 30; orDateTime.Month = 12; --orDateTime.Year; } break;
177 } // namespace
179 // ============================================================================
181 PivotCacheItem::PivotCacheItem() :
182 mnType( XML_m )
186 void PivotCacheItem::readString( const AttributeList& rAttribs )
188 maValue <<= rAttribs.getXString( XML_v, OUString() );
189 mnType = XML_s;
192 void PivotCacheItem::readNumeric( const AttributeList& rAttribs )
194 maValue <<= rAttribs.getDouble( XML_v, 0.0 );
195 mnType = XML_n;
198 void PivotCacheItem::readDate( const AttributeList& rAttribs )
200 maValue <<= rAttribs.getDateTime( XML_v, DateTime() );
201 mnType = XML_d;
204 void PivotCacheItem::readBool( const AttributeList& rAttribs )
206 maValue <<= rAttribs.getBool( XML_v, false );
207 mnType = XML_b;
210 void PivotCacheItem::readError( const AttributeList& rAttribs, const UnitConverter& rUnitConverter )
212 maValue <<= static_cast< sal_Int32 >( rUnitConverter.calcBiffErrorCode( rAttribs.getXString( XML_v, OUString() ) ) );
213 mnType = XML_e;
216 void PivotCacheItem::readIndex( const AttributeList& rAttribs )
218 maValue <<= rAttribs.getInteger( XML_v, -1 );
219 mnType = XML_x;
222 void PivotCacheItem::readString( RecordInputStream& rStrm )
224 maValue <<= rStrm.readString();
225 mnType = XML_s;
228 void PivotCacheItem::readDouble( RecordInputStream& rStrm )
230 maValue <<= rStrm.readDouble();
231 mnType = XML_n;
234 void PivotCacheItem::readDate( RecordInputStream& rStrm )
236 DateTime aDateTime;
237 aDateTime.Year = rStrm.readuInt16();
238 aDateTime.Month = rStrm.readuInt16();
239 aDateTime.Day = rStrm.readuInt8();
240 aDateTime.Hours = rStrm.readuInt8();
241 aDateTime.Minutes = rStrm.readuInt8();
242 aDateTime.Seconds = rStrm.readuInt8();
243 lclAdjustBinDateTime( aDateTime );
244 maValue <<= aDateTime;
245 mnType = XML_d;
248 void PivotCacheItem::readBool( RecordInputStream& rStrm )
250 maValue <<= (rStrm.readuInt8() != 0);
251 mnType = XML_b;
254 void PivotCacheItem::readError( RecordInputStream& rStrm )
256 maValue <<= static_cast< sal_Int32 >( rStrm.readuInt8() );
257 mnType = XML_e;
260 void PivotCacheItem::readIndex( RecordInputStream& rStrm )
262 maValue <<= rStrm.readInt32();
263 mnType = XML_x;
266 void PivotCacheItem::readString( BiffInputStream& rStrm, const WorkbookHelper& rHelper )
268 maValue <<= (rHelper.getBiff() == BIFF8) ? rStrm.readUniString() : rStrm.readByteStringUC( true, rHelper.getTextEncoding() );
269 mnType = XML_s;
272 void PivotCacheItem::readDouble( BiffInputStream& rStrm )
274 maValue <<= rStrm.readDouble();
275 mnType = XML_n;
278 void PivotCacheItem::readInteger( BiffInputStream& rStrm )
280 maValue <<= rStrm.readInt16();
281 mnType = XML_i; // fake, used for BIFF only
284 void PivotCacheItem::readDate( BiffInputStream& rStrm )
286 DateTime aDateTime;
287 aDateTime.Year = rStrm.readuInt16();
288 aDateTime.Month = rStrm.readuInt16();
289 aDateTime.Day = rStrm.readuInt8();
290 aDateTime.Hours = rStrm.readuInt8();
291 aDateTime.Minutes = rStrm.readuInt8();
292 aDateTime.Seconds = rStrm.readuInt8();
293 lclAdjustBinDateTime( aDateTime );
294 maValue <<= aDateTime;
295 mnType = XML_d;
298 void PivotCacheItem::readBool( BiffInputStream& rStrm )
300 maValue <<= (rStrm.readuInt8() != 0);
301 mnType = XML_b;
304 void PivotCacheItem::readError( BiffInputStream& rStrm )
306 maValue <<= static_cast< sal_Int32 >( rStrm.readuInt8() );
307 mnType = XML_e;
310 OUString PivotCacheItem::getName() const
312 switch( mnType )
314 case XML_m: return OUString();
315 case XML_s: return maValue.get< OUString >();
316 case XML_n: return OUString::valueOf( maValue.get< double >() ); // !TODO
317 case XML_i: return OUString::valueOf( maValue.get< sal_Int32 >() );
318 case XML_d: return OUString(); // !TODO
319 case XML_b: return OUString::valueOf( static_cast< sal_Bool >( maValue.get< bool >() ) ); // !TODO
320 case XML_e: return OUString(); // !TODO
322 OSL_ENSURE( false, "PivotCacheItem::getName - invalid data type" );
323 return OUString();
326 // ----------------------------------------------------------------------------
328 PivotCacheItemList::PivotCacheItemList( const WorkbookHelper& rHelper ) :
329 WorkbookHelper( rHelper )
333 void PivotCacheItemList::importItem( sal_Int32 nElement, const AttributeList& rAttribs )
335 PivotCacheItem& rItem = createItem();
336 switch( nElement )
338 case XLS_TOKEN( m ): break;
339 case XLS_TOKEN( s ): rItem.readString( rAttribs ); break;
340 case XLS_TOKEN( n ): rItem.readNumeric( rAttribs ); break;
341 case XLS_TOKEN( d ): rItem.readDate( rAttribs ); break;
342 case XLS_TOKEN( b ): rItem.readBool( rAttribs ); break;
343 case XLS_TOKEN( e ): rItem.readError( rAttribs, getUnitConverter() ); break;
344 default: OSL_ENSURE( false, "PivotCacheItemList::importItem - unknown element type" );
348 void PivotCacheItemList::importItem( sal_Int32 nRecId, RecordInputStream& rStrm )
350 if( nRecId == OOBIN_ID_PCITEM_ARRAY )
352 importArray( rStrm );
353 return;
356 PivotCacheItem& rItem = createItem();
357 switch( nRecId )
359 case OOBIN_ID_PCITEM_MISSING:
360 case OOBIN_ID_PCITEMA_MISSING: break;
361 case OOBIN_ID_PCITEM_STRING:
362 case OOBIN_ID_PCITEMA_STRING: rItem.readString( rStrm ); break;
363 case OOBIN_ID_PCITEM_DOUBLE:
364 case OOBIN_ID_PCITEMA_DOUBLE: rItem.readDouble( rStrm ); break;
365 case OOBIN_ID_PCITEM_DATE:
366 case OOBIN_ID_PCITEMA_DATE: rItem.readDate( rStrm ); break;
367 case OOBIN_ID_PCITEM_BOOL:
368 case OOBIN_ID_PCITEMA_BOOL: rItem.readBool( rStrm ); break;
369 case OOBIN_ID_PCITEM_ERROR:
370 case OOBIN_ID_PCITEMA_ERROR: rItem.readError( rStrm ); break;
371 default: OSL_ENSURE( false, "PivotCacheItemList::importItem - unknown record type" );
375 void PivotCacheItemList::importItemList( BiffInputStream& rStrm, sal_uInt16 nCount )
377 bool bLoop = true;
378 for( sal_uInt16 nItemIdx = 0; bLoop && (nItemIdx < nCount); ++nItemIdx )
380 bLoop = rStrm.startNextRecord();
381 if( bLoop ) switch( rStrm.getRecId() )
383 case BIFF_ID_PCITEM_MISSING: createItem(); break;
384 case BIFF_ID_PCITEM_STRING: createItem().readString( rStrm, *this ); break;
385 case BIFF_ID_PCITEM_DOUBLE: createItem().readDouble( rStrm ); break;
386 case BIFF_ID_PCITEM_INTEGER: createItem().readInteger( rStrm ); break;
387 case BIFF_ID_PCITEM_DATE: createItem().readDate( rStrm ); break;
388 case BIFF_ID_PCITEM_BOOL: createItem().readBool( rStrm ); break;
389 case BIFF_ID_PCITEM_ERROR: createItem().readError( rStrm ); break;
390 default: rStrm.rewindRecord(); bLoop = false;
393 OSL_ENSURE( bLoop, "PivotCacheItemList::importItemList - could not read all cache item records" );
396 const PivotCacheItem* PivotCacheItemList::getCacheItem( sal_Int32 nItemIdx ) const
398 return ContainerHelper::getVectorElement( maItems, nItemIdx );
401 void PivotCacheItemList::getCacheItemNames( ::std::vector< OUString >& orItemNames ) const
403 orItemNames.clear();
404 orItemNames.reserve( maItems.size() );
405 for( CacheItemVector::const_iterator aIt = maItems.begin(), aEnd = maItems.end(); aIt != aEnd; ++aIt )
406 orItemNames.push_back( aIt->getName() );
409 // private --------------------------------------------------------------------
411 PivotCacheItem& PivotCacheItemList::createItem()
413 maItems.resize( maItems.size() + 1 );
414 return maItems.back();
417 void PivotCacheItemList::importArray( RecordInputStream& rStrm )
419 sal_uInt16 nType = rStrm.readuInt16();
420 sal_Int32 nCount = rStrm.readInt32();
421 for( sal_Int32 nIdx = 0; !rStrm.isEof() && (nIdx < nCount); ++nIdx )
423 switch( nType )
425 case OOBIN_PCITEM_ARRAY_DOUBLE: createItem().readDouble( rStrm ); break;
426 case OOBIN_PCITEM_ARRAY_STRING: createItem().readString( rStrm ); break;
427 case OOBIN_PCITEM_ARRAY_ERROR: createItem().readError( rStrm ); break;
428 case OOBIN_PCITEM_ARRAY_DATE: createItem().readDate( rStrm ); break;
429 default:
430 OSL_ENSURE( false, "PivotCacheItemList::importArray - unknown data type" );
431 nIdx = nCount;
436 // ============================================================================
438 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 mbMemberPropField( false )
451 // ----------------------------------------------------------------------------
453 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 mbHasLongIndexes( false )
467 // ----------------------------------------------------------------------------
469 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 mbAutoEnd( true )
483 void PCFieldGroupModel::setBinGroupBy( sal_uInt8 nGroupBy )
485 static const sal_Int32 spnGroupBy[] = { XML_range,
486 XML_seconds, XML_minutes, XML_hours, XML_days, XML_months, XML_quarters, XML_years };
487 mnGroupBy = STATIC_ARRAY_SELECT( spnGroupBy, nGroupBy, XML_range );
490 // ----------------------------------------------------------------------------
492 PivotCacheField::PivotCacheField( const WorkbookHelper& rHelper, bool bIsDatabaseField ) :
493 WorkbookHelper( rHelper ),
494 maSharedItems( rHelper ),
495 maGroupItems( rHelper )
497 maFieldModel.mbDatabaseField = bIsDatabaseField;
500 void PivotCacheField::importCacheField( const AttributeList& rAttribs )
502 maFieldModel.maName = rAttribs.getXString( XML_name, OUString() );
503 maFieldModel.maCaption = rAttribs.getXString( XML_caption, OUString() );
504 maFieldModel.maPropertyName = rAttribs.getXString( XML_propertyName, OUString() );
505 maFieldModel.maFormula = rAttribs.getXString( XML_formula, OUString() );
506 maFieldModel.mnNumFmtId = rAttribs.getInteger( XML_numFmtId, 0 );
507 maFieldModel.mnSqlType = rAttribs.getInteger( XML_sqlType, 0 );
508 maFieldModel.mnHierarchy = rAttribs.getInteger( XML_hierarchy, 0 );
509 maFieldModel.mnLevel = rAttribs.getInteger( XML_level, 0 );
510 maFieldModel.mnMappingCount = rAttribs.getInteger( XML_mappingCount, 0 );
511 maFieldModel.mbDatabaseField = rAttribs.getBool( XML_databaseField, true );
512 maFieldModel.mbServerField = rAttribs.getBool( XML_serverField, false );
513 maFieldModel.mbUniqueList = rAttribs.getBool( XML_uniqueList, true );
514 maFieldModel.mbMemberPropField = rAttribs.getBool( XML_memberPropertyField, false );
517 void PivotCacheField::importSharedItems( const AttributeList& rAttribs )
519 OSL_ENSURE( maSharedItems.empty(), "PivotCacheField::importSharedItems - multiple shared items elements" );
520 maSharedItemsModel.mbHasSemiMixed = rAttribs.getBool( XML_containsSemiMixedTypes, true );
521 maSharedItemsModel.mbHasNonDate = rAttribs.getBool( XML_containsNonDate, true );
522 maSharedItemsModel.mbHasDate = rAttribs.getBool( XML_containsDate, false );
523 maSharedItemsModel.mbHasString = rAttribs.getBool( XML_containsString, true );
524 maSharedItemsModel.mbHasBlank = rAttribs.getBool( XML_containsBlank, false );
525 maSharedItemsModel.mbHasMixed = rAttribs.getBool( XML_containsMixedTypes, false );
526 maSharedItemsModel.mbIsNumeric = rAttribs.getBool( XML_containsNumber, false );
527 maSharedItemsModel.mbIsInteger = rAttribs.getBool( XML_containsInteger, false );
528 maSharedItemsModel.mbHasLongText = rAttribs.getBool( XML_longText, false );
531 void PivotCacheField::importSharedItem( sal_Int32 nElement, const AttributeList& rAttribs )
533 maSharedItems.importItem( nElement, rAttribs );
536 void PivotCacheField::importFieldGroup( const AttributeList& rAttribs )
538 maFieldGroupModel.mnParentField = rAttribs.getInteger( XML_par, -1 );
539 maFieldGroupModel.mnBaseField = rAttribs.getInteger( XML_base, -1 );
542 void PivotCacheField::importRangePr( const AttributeList& rAttribs )
544 maFieldGroupModel.maStartDate = rAttribs.getDateTime( XML_startDate, DateTime() );
545 maFieldGroupModel.maEndDate = rAttribs.getDateTime( XML_endDate, DateTime() );
546 maFieldGroupModel.mfStartValue = rAttribs.getDouble( XML_startNum, 0.0 );
547 maFieldGroupModel.mfEndValue = rAttribs.getDouble( XML_endNum, 0.0 );
548 maFieldGroupModel.mfInterval = rAttribs.getDouble( XML_groupInterval, 1.0 );
549 maFieldGroupModel.mnGroupBy = rAttribs.getToken( XML_groupBy, XML_range );
550 maFieldGroupModel.mbRangeGroup = true;
551 maFieldGroupModel.mbDateGroup = maFieldGroupModel.mnGroupBy != XML_range;
552 maFieldGroupModel.mbAutoStart = rAttribs.getBool( XML_autoStart, true );
553 maFieldGroupModel.mbAutoEnd = rAttribs.getBool( XML_autoEnd, true );
556 void PivotCacheField::importDiscretePrItem( sal_Int32 nElement, const AttributeList& rAttribs )
558 OSL_ENSURE( nElement == XLS_TOKEN( x ), "PivotCacheField::importDiscretePrItem - unexpected element" );
559 if( nElement == XLS_TOKEN( x ) )
560 maDiscreteItems.push_back( rAttribs.getInteger( XML_v, -1 ) );
563 void PivotCacheField::importGroupItem( sal_Int32 nElement, const AttributeList& rAttribs )
565 maGroupItems.importItem( nElement, rAttribs );
568 void PivotCacheField::importPCDField( RecordInputStream& rStrm )
570 sal_uInt16 nFlags;
571 rStrm >> nFlags >> maFieldModel.mnNumFmtId;
572 maFieldModel.mnSqlType = rStrm.readInt16();
573 rStrm >> maFieldModel.mnHierarchy >> maFieldModel.mnLevel >> maFieldModel.mnMappingCount >> maFieldModel.maName;
574 if( getFlag( nFlags, OOBIN_PCDFIELD_HASCAPTION ) )
575 rStrm >> maFieldModel.maCaption;
576 if( getFlag( nFlags, OOBIN_PCDFIELD_HASFORMULA ) )
577 rStrm.skip( ::std::max< sal_Int32 >( rStrm.readInt32(), 0 ) );
578 if( maFieldModel.mnMappingCount > 0 )
579 rStrm.skip( ::std::max< sal_Int32 >( rStrm.readInt32(), 0 ) );
580 if( getFlag( nFlags, OOBIN_PCDFIELD_HASPROPERTYNAME ) )
581 rStrm >> maFieldModel.maPropertyName;
583 maFieldModel.mbDatabaseField = getFlag( nFlags, OOBIN_PCDFIELD_DATABASEFIELD );
584 maFieldModel.mbServerField = getFlag( nFlags, OOBIN_PCDFIELD_SERVERFIELD );
585 maFieldModel.mbUniqueList = !getFlag( nFlags, OOBIN_PCDFIELD_NOUNIQUEITEMS );
586 maFieldModel.mbMemberPropField = getFlag( nFlags, OOBIN_PCDFIELD_MEMBERPROPFIELD );
589 void PivotCacheField::importPCDFSharedItems( RecordInputStream& rStrm )
591 sal_uInt16 nFlags;
592 rStrm >> nFlags;
593 maSharedItemsModel.mbHasSemiMixed = getFlag( nFlags, OOBIN_PCDFSITEMS_HASSEMIMIXED );
594 maSharedItemsModel.mbHasNonDate = getFlag( nFlags, OOBIN_PCDFSITEMS_HASNONDATE );
595 maSharedItemsModel.mbHasDate = getFlag( nFlags, OOBIN_PCDFSITEMS_HASDATE );
596 maSharedItemsModel.mbHasString = getFlag( nFlags, OOBIN_PCDFSITEMS_HASSTRING );
597 maSharedItemsModel.mbHasBlank = getFlag( nFlags, OOBIN_PCDFSITEMS_HASBLANK );
598 maSharedItemsModel.mbHasMixed = getFlag( nFlags, OOBIN_PCDFSITEMS_HASMIXED );
599 maSharedItemsModel.mbIsNumeric = getFlag( nFlags, OOBIN_PCDFSITEMS_ISNUMERIC );
600 maSharedItemsModel.mbIsInteger = getFlag( nFlags, OOBIN_PCDFSITEMS_ISINTEGER );
601 maSharedItemsModel.mbHasLongText = getFlag( nFlags, OOBIN_PCDFSITEMS_HASLONGTEXT );
604 void PivotCacheField::importPCDFSharedItem( sal_Int32 nRecId, RecordInputStream& rStrm )
606 maSharedItems.importItem( nRecId, rStrm );
609 void PivotCacheField::importPCDFieldGroup( RecordInputStream& rStrm )
611 rStrm >> maFieldGroupModel.mnParentField >> maFieldGroupModel.mnBaseField;
614 void PivotCacheField::importPCDFRangePr( RecordInputStream& rStrm )
616 sal_uInt8 nGroupBy, nFlags;
617 rStrm >> nGroupBy >> nFlags >> maFieldGroupModel.mfStartValue >> maFieldGroupModel.mfEndValue >> maFieldGroupModel.mfInterval;
619 maFieldGroupModel.setBinGroupBy( nGroupBy );
620 maFieldGroupModel.mbRangeGroup = true;
621 maFieldGroupModel.mbDateGroup = getFlag( nFlags, OOBIN_PCDFRANGEPR_DATEGROUP );
622 maFieldGroupModel.mbAutoStart = getFlag( nFlags, OOBIN_PCDFRANGEPR_AUTOSTART );
623 maFieldGroupModel.mbAutoEnd = getFlag( nFlags, OOBIN_PCDFRANGEPR_AUTOEND );
625 OSL_ENSURE( maFieldGroupModel.mbDateGroup == (maFieldGroupModel.mnGroupBy != XML_range), "PivotCacheField::importPCDFRangePr - wrong date flag" );
626 if( maFieldGroupModel.mbDateGroup )
628 maFieldGroupModel.maStartDate = getUnitConverter().calcDateTimeFromSerial( maFieldGroupModel.mfStartValue );
629 maFieldGroupModel.maEndDate = getUnitConverter().calcDateTimeFromSerial( maFieldGroupModel.mfEndValue );
633 void PivotCacheField::importPCDFDiscretePrItem( sal_Int32 nRecId, RecordInputStream& rStrm )
635 OSL_ENSURE( nRecId == OOBIN_ID_PCITEM_INDEX, "PivotCacheField::importPCDFDiscretePrItem - unexpected record" );
636 if( nRecId == OOBIN_ID_PCITEM_INDEX )
637 maDiscreteItems.push_back( rStrm.readInt32() );
640 void PivotCacheField::importPCDFGroupItem( sal_Int32 nRecId, RecordInputStream& rStrm )
642 maGroupItems.importItem( nRecId, rStrm );
645 void PivotCacheField::importPCDField( BiffInputStream& rStrm )
647 sal_uInt16 nFlags, nGroupItems, nBaseItems, nSharedItems;
648 rStrm >> nFlags;
649 maFieldGroupModel.mnParentField = rStrm.readuInt16();
650 maFieldGroupModel.mnBaseField = rStrm.readuInt16();
651 rStrm.skip( 2 ); // number of unique items (either shared or group)
652 rStrm >> nGroupItems >> nBaseItems >> nSharedItems;
653 maFieldModel.maName = (getBiff() == BIFF8) ? rStrm.readUniString() : rStrm.readByteStringUC( true, getTextEncoding() );
655 maFieldModel.mbServerField = getFlag( nFlags, BIFF_PCDFIELD_SERVERFIELD );
656 maFieldModel.mbUniqueList = !getFlag( nFlags, BIFF_PCDFIELD_NOUNIQUEITEMS );
657 maSharedItemsModel.mbHasSemiMixed = getFlag( nFlags, BIFF_PCDFIELD_HASSEMIMIXED );
658 maSharedItemsModel.mbHasNonDate = getFlag( nFlags, BIFF_PCDFIELD_HASNONDATE );
659 maSharedItemsModel.mbHasDate = getFlag( nFlags, BIFF_PCDFIELD_HASDATE );
660 maSharedItemsModel.mbIsNumeric = getFlag( nFlags, BIFF_PCDFIELD_ISNUMERIC );
661 maSharedItemsModel.mbHasLongIndexes = getFlag( nFlags, BIFF_PCDFIELD_HASLONGINDEX );
662 maFieldGroupModel.mbRangeGroup = getFlag( nFlags, BIFF_PCDFIELD_RANGEGROUP );
664 // in BIFF, presence of parent group field is denoted by a flag
665 if( !getFlag( nFlags, BIFF_PCDFIELD_HASPARENT ) )
666 maFieldGroupModel.mnParentField = -1;
668 // following PCDFSQLTYPE record contains SQL type
669 if( (rStrm.getNextRecId() == BIFF_ID_PCDFSQLTYPE) && rStrm.startNextRecord() )
670 maFieldModel.mnSqlType = rStrm.readInt16();
672 // read group items, if any
673 if( nGroupItems > 0 )
675 OSL_ENSURE( getFlag( nFlags, BIFF_PCDFIELD_HASITEMS ), "PivotCacheField::importPCDField - missing items flag" );
676 maGroupItems.importItemList( rStrm, nGroupItems );
678 sal_uInt16 nNextRecId = rStrm.getNextRecId();
679 bool bHasRangePr = nNextRecId == BIFF_ID_PCDFRANGEPR;
680 bool bHasDiscretePr = nNextRecId == BIFF_ID_PCDFDISCRETEPR;
682 OSL_ENSURE( bHasRangePr || bHasDiscretePr, "PivotCacheField::importPCDField - missing group properties record" );
683 OSL_ENSURE( bHasRangePr == maFieldGroupModel.mbRangeGroup, "PivotCacheField::importPCDField - invalid range grouping flag" );
684 if( bHasRangePr && rStrm.startNextRecord() )
685 importPCDFRangePr( rStrm );
686 else if( bHasDiscretePr && rStrm.startNextRecord() )
687 importPCDFDiscretePr( rStrm );
690 // read the shared items, if any
691 if( nSharedItems > 0 )
693 OSL_ENSURE( getFlag( nFlags, BIFF_PCDFIELD_HASITEMS ), "PivotCacheField::importPCDField - missing items flag" );
694 maSharedItems.importItemList( rStrm, nSharedItems );
698 void PivotCacheField::importPCDFRangePr( BiffInputStream& rStrm )
700 sal_uInt16 nFlags;
701 rStrm >> nFlags;
702 maFieldGroupModel.setBinGroupBy( extractValue< sal_uInt8 >( nFlags, 2, 3 ) );
703 maFieldGroupModel.mbRangeGroup = true;
704 maFieldGroupModel.mbDateGroup = maFieldGroupModel.mnGroupBy != XML_range;
705 maFieldGroupModel.mbAutoStart = getFlag( nFlags, BIFF_PCDFRANGEPR_AUTOSTART );
706 maFieldGroupModel.mbAutoEnd = getFlag( nFlags, BIFF_PCDFRANGEPR_AUTOEND );
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 PivotCacheItemList aLimits( *this );
713 aLimits.importItemList( rStrm, 3 );
714 OSL_ENSURE( aLimits.size() == 3, "PivotCacheField::importPCDFRangePr - missing grouping records" );
715 const PivotCacheItem* pStartValue = aLimits.getCacheItem( 0 );
716 const PivotCacheItem* pEndValue = aLimits.getCacheItem( 1 );
717 const PivotCacheItem* pInterval = aLimits.getCacheItem( 2 );
718 if( pStartValue && pEndValue && pInterval )
720 if( maFieldGroupModel.mbDateGroup )
722 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 if( bHasTypes )
726 maFieldGroupModel.maStartDate = pStartValue->getValue().get< DateTime >();
727 maFieldGroupModel.maEndDate = pEndValue->getValue().get< DateTime >();
728 maFieldGroupModel.mfInterval = pInterval->getValue().get< sal_Int16 >();
731 else
733 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 if( bHasTypes )
737 maFieldGroupModel.mfStartValue = pStartValue->getValue().get< double >();
738 maFieldGroupModel.mfEndValue = pEndValue->getValue().get< double >();
739 maFieldGroupModel.mfInterval = pInterval->getValue().get< double >();
745 void PivotCacheField::importPCDFDiscretePr( BiffInputStream& rStrm )
747 sal_Int32 nCount = static_cast< sal_Int32 >( rStrm.getLength() / 2 );
748 for( sal_Int32 nIndex = 0; !rStrm.isEof() && (nIndex < nCount); ++nIndex )
749 maDiscreteItems.push_back( rStrm.readuInt16() );
752 const PivotCacheItem* PivotCacheField::getCacheItem( sal_Int32 nItemIdx ) const
754 if( hasGroupItems() )
755 return maGroupItems.getCacheItem( nItemIdx );
756 if( hasSharedItems() )
757 return maSharedItems.getCacheItem( nItemIdx );
758 return 0;
761 void PivotCacheField::getCacheItemNames( ::std::vector< OUString >& orItemNames ) const
763 if( hasGroupItems() )
764 maGroupItems.getCacheItemNames( orItemNames );
765 else if( hasSharedItems() )
766 maSharedItems.getCacheItemNames( orItemNames );
769 void PivotCacheField::convertNumericGrouping( const Reference< XDataPilotField >& rxDPField ) const
771 OSL_ENSURE( hasGroupItems() && hasNumericGrouping(), "PivotCacheField::convertNumericGrouping - not a numeric group field" );
772 PropertySet aPropSet( rxDPField );
773 if( hasGroupItems() && hasNumericGrouping() && aPropSet.is() )
775 DataPilotFieldGroupInfo aGroupInfo;
776 aGroupInfo.HasAutoStart = maFieldGroupModel.mbAutoStart;
777 aGroupInfo.HasAutoEnd = maFieldGroupModel.mbAutoEnd;
778 aGroupInfo.HasDateValues = sal_False;
779 aGroupInfo.Start = maFieldGroupModel.mfStartValue;
780 aGroupInfo.End = maFieldGroupModel.mfEndValue;
781 aGroupInfo.Step = maFieldGroupModel.mfInterval;
782 aGroupInfo.GroupBy = 0;
783 aPropSet.setProperty( PROP_GroupInfo, aGroupInfo );
787 OUString PivotCacheField::createDateGroupField( const Reference< XDataPilotField >& rxBaseDPField ) const
789 OSL_ENSURE( hasGroupItems() && hasDateGrouping(), "PivotCacheField::createDateGroupField - not a numeric group field" );
790 Reference< XDataPilotField > xDPGroupField;
791 PropertySet aPropSet( rxBaseDPField );
792 if( hasGroupItems() && hasDateGrouping() && aPropSet.is() )
794 bool bDayRanges = (maFieldGroupModel.mnGroupBy == XML_days) && (maFieldGroupModel.mfInterval >= 2.0);
796 DataPilotFieldGroupInfo aGroupInfo;
797 aGroupInfo.HasAutoStart = maFieldGroupModel.mbAutoStart;
798 aGroupInfo.HasAutoEnd = maFieldGroupModel.mbAutoEnd;
799 aGroupInfo.HasDateValues = sal_True;
800 aGroupInfo.Start = getUnitConverter().calcSerialFromDateTime( maFieldGroupModel.maStartDate );
801 aGroupInfo.End = getUnitConverter().calcSerialFromDateTime( maFieldGroupModel.maEndDate );
802 aGroupInfo.Step = bDayRanges ? maFieldGroupModel.mfInterval : 0.0;
804 using namespace ::com::sun::star::sheet::DataPilotFieldGroupBy;
805 switch( maFieldGroupModel.mnGroupBy )
807 case XML_years: aGroupInfo.GroupBy = YEARS; break;
808 case XML_quarters: aGroupInfo.GroupBy = QUARTERS; break;
809 case XML_months: aGroupInfo.GroupBy = MONTHS; break;
810 case XML_days: aGroupInfo.GroupBy = DAYS; break;
811 case XML_hours: aGroupInfo.GroupBy = HOURS; break;
812 case XML_minutes: aGroupInfo.GroupBy = MINUTES; break;
813 case XML_seconds: aGroupInfo.GroupBy = SECONDS; break;
814 default: OSL_ENSURE( false, "PivotCacheField::convertRangeGrouping - unknown date/time interval" );
819 Reference< XDataPilotFieldGrouping > xDPGrouping( rxBaseDPField, UNO_QUERY_THROW );
820 xDPGroupField = xDPGrouping->createDateGroup( aGroupInfo );
822 catch( Exception& )
827 Reference< XNamed > xFieldName( xDPGroupField, UNO_QUERY );
828 return xFieldName.is() ? xFieldName->getName() : OUString();
831 OUString PivotCacheField::createParentGroupField( const Reference< XDataPilotField >& rxBaseDPField, PivotCacheGroupItemVector& orItemNames ) const
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 Reference< XDataPilotFieldGrouping > xDPGrouping( rxBaseDPField, UNO_QUERY );
836 if( !xDPGrouping.is() ) return OUString();
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 GroupItemMap aItemMap( maGroupItems.size() );
842 for( IndexVector::const_iterator aBeg = maDiscreteItems.begin(), aIt = aBeg, aEnd = maDiscreteItems.end(); aIt != aEnd; ++aIt )
843 if( GroupItemList* pItems = ContainerHelper::getVectorElement( aItemMap, *aIt ) )
844 pItems->push_back( static_cast< sal_Int32 >( aIt - aBeg ) );
846 // process all groups
847 Reference< XDataPilotField > xDPGroupField;
848 for( GroupItemMap::iterator aBeg = aItemMap.begin(), aIt = aBeg, aEnd = aItemMap.end(); aIt != aEnd; ++aIt )
850 OSL_ENSURE( !aIt->empty(), "PivotCacheField::createParentGroupField - item/group should not be empty" );
851 // if the item count is greater than 1, the item is a group of items
852 if( aIt->size() > 1 )
854 /* Insert the names of the items that are part of this group. Calc
855 expects the names of the members of the field whose members are
856 grouped (which may be the names of groups too). Excel provides
857 the names of the base field items instead (no group names
858 involved). Therefore, the passed collection of current item
859 names as they are already grouped is used here to resolve the
860 item names. */
861 ::std::vector< OUString > aMembers;
862 for( GroupItemList::iterator aBeg2 = aIt->begin(), aIt2 = aBeg2, aEnd2 = aIt->end(); aIt2 != aEnd2; ++aIt2 )
863 if( const PivotCacheGroupItem* pName = ContainerHelper::getVectorElement( orItemNames, *aIt2 ) )
864 if( ::std::find( aMembers.begin(), aMembers.end(), pName->maGroupName ) == aMembers.end() )
865 aMembers.push_back( pName->maGroupName );
867 /* Check again, that this is not just a group that is not grouped
868 further with other items. */
869 if( aMembers.size() > 1 ) try
871 // only the first call of createNameGroup() returns the new field
872 Reference< XDataPilotField > xDPNewField = xDPGrouping->createNameGroup( ContainerHelper::vectorToSequence( aMembers ) );
873 OSL_ENSURE( xDPGroupField.is() != xDPNewField.is(), "PivotCacheField::createParentGroupField - missing group field" );
874 if( !xDPGroupField.is() )
875 xDPGroupField = xDPNewField;
877 // get current grouping info
878 DataPilotFieldGroupInfo aGroupInfo;
879 PropertySet aPropSet( xDPGroupField );
880 aPropSet.getProperty( aGroupInfo, PROP_GroupInfo );
882 /* Find the group object and the auto-generated group name.
883 The returned field contains all groups derived from the
884 previous field if that is grouped too. To find the correct
885 group, the first item used to create the group is serached.
886 Calc provides the original item names of the base field
887 when the group is querried for its members. Its does not
888 provide the names of members that are already groups in the
889 field used to create the new groups. (Is this a bug?)
890 Therefore, a name from the passed list of original item
891 names is used to find the correct group. */
892 OUString aFirstItem;
893 if( const PivotCacheGroupItem* pName = ContainerHelper::getVectorElement( orItemNames, aIt->front() ) )
894 aFirstItem = pName->maOrigName;
895 Reference< XNamed > xGroupName;
896 OUString aAutoName;
897 Reference< XIndexAccess > xGroupsIA( aGroupInfo.Groups, UNO_QUERY_THROW );
898 for( sal_Int32 nIdx = 0, nCount = xGroupsIA->getCount(); (nIdx < nCount) && (aAutoName.getLength() == 0); ++nIdx ) try
900 Reference< XNameAccess > xItemsNA( xGroupsIA->getByIndex( nIdx ), UNO_QUERY_THROW );
901 if( xItemsNA->hasByName( aFirstItem ) )
903 xGroupName.set( xGroupsIA->getByIndex( nIdx ), UNO_QUERY_THROW );
904 aAutoName = xGroupName->getName();
907 catch( Exception& )
910 OSL_ENSURE( aAutoName.getLength() > 0, "PivotCacheField::createParentGroupField - cannot find auto-generated group name" );
912 // get the real group name from the list of group items
913 OUString aGroupName;
914 if( const PivotCacheItem* pGroupItem = maGroupItems.getCacheItem( static_cast< sal_Int32 >( aIt - aBeg ) ) )
915 aGroupName = pGroupItem->getName();
916 OSL_ENSURE( aGroupName.getLength() > 0, "PivotCacheField::createParentGroupField - cannot find group name" );
917 if( aGroupName.getLength() == 0 )
918 aGroupName = aAutoName;
920 if( xGroupName.is() && (aGroupName.getLength() > 0) )
922 // replace the auto-generated group name with the real name
923 if( aAutoName != aGroupName )
925 xGroupName->setName( aGroupName );
926 aPropSet.setProperty( PROP_GroupInfo, aGroupInfo );
928 // replace original item names in passed vector with group name
929 for( GroupItemList::iterator aIt2 = aIt->begin(), aEnd2 = aIt->end(); aIt2 != aEnd2; ++aIt2 )
930 if( PivotCacheGroupItem* pName = ContainerHelper::getVectorElement( orItemNames, *aIt2 ) )
931 pName->maGroupName = aGroupName;
934 catch( Exception& )
940 Reference< XNamed > xFieldName( xDPGroupField, UNO_QUERY );
941 return xFieldName.is() ? xFieldName->getName() : OUString();
944 void PivotCacheField::writeSourceHeaderCell( WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow ) const
946 rSheetHelper.setStringCell( rSheetHelper.getCell( CellAddress( rSheetHelper.getSheetIndex(), nCol, nRow ) ), maFieldModel.maName );
949 void PivotCacheField::writeSourceDataCell( WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow, const PivotCacheItem& rItem ) const
951 bool bHasIndex = rItem.getType() == XML_x;
952 OSL_ENSURE( bHasIndex != maSharedItems.empty(), "PivotCacheField::writeSourceDataCell - shared items missing or not expected" );
953 if( bHasIndex )
954 writeSharedItemToSourceDataCell( rSheetHelper, nCol, nRow, rItem.getValue().get< sal_Int32 >() );
955 else
956 writeItemToSourceDataCell( rSheetHelper, nCol, nRow, rItem );
959 void PivotCacheField::importPCRecordItem( RecordInputStream& rStrm, WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow ) const
961 if( hasSharedItems() )
963 writeSharedItemToSourceDataCell( rSheetHelper, nCol, nRow, rStrm.readInt32() );
965 else
967 PivotCacheItem aItem;
968 if( maSharedItemsModel.mbIsNumeric )
969 aItem.readDouble( rStrm );
970 else if( maSharedItemsModel.mbHasDate && !maSharedItemsModel.mbHasString )
971 aItem.readDate( rStrm );
972 else
973 aItem.readString( rStrm );
974 writeItemToSourceDataCell( rSheetHelper, nCol, nRow, aItem );
978 void PivotCacheField::importPCItemIndex( BiffInputStream& rStrm, WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow ) const
980 OSL_ENSURE( hasSharedItems(), "PivotCacheField::importPCItemIndex - invalid call, no shared items found" );
981 sal_Int32 nIndex = maSharedItemsModel.mbHasLongIndexes ? rStrm.readuInt16() : rStrm.readuInt8();
982 writeSharedItemToSourceDataCell( rSheetHelper, nCol, nRow, nIndex );
985 // private --------------------------------------------------------------------
987 void PivotCacheField::writeItemToSourceDataCell( WorksheetHelper& rSheetHelper,
988 sal_Int32 nCol, sal_Int32 nRow, const PivotCacheItem& rItem ) const
990 if( rItem.getType() != XML_m )
992 Reference< XCell > xCell = rSheetHelper.getCell( CellAddress( rSheetHelper.getSheetIndex(), nCol, nRow ) );
993 if( xCell.is() ) switch( rItem.getType() )
995 case XML_s: rSheetHelper.setStringCell( xCell, rItem.getValue().get< OUString >() ); break;
996 case XML_n: xCell->setValue( rItem.getValue().get< double >() ); break;
997 case XML_i: xCell->setValue( rItem.getValue().get< sal_Int16 >() ); break;
998 case XML_d: rSheetHelper.setDateTimeCell( xCell, rItem.getValue().get< DateTime >() ); break;
999 case XML_b: rSheetHelper.setBooleanCell( xCell, rItem.getValue().get< bool >() ); break;
1000 case XML_e: rSheetHelper.setErrorCell( xCell, static_cast< sal_uInt8 >( rItem.getValue().get< sal_Int32 >() ) ); break;
1001 default: OSL_ENSURE( false, "PivotCacheField::writeItemToSourceDataCell - unexpected item data type" );
1006 void PivotCacheField::writeSharedItemToSourceDataCell(
1007 WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow, sal_Int32 nItemIdx ) const
1009 if( const PivotCacheItem* pCacheItem = maSharedItems.getCacheItem( nItemIdx ) )
1010 writeItemToSourceDataCell( rSheetHelper, nCol, nRow, *pCacheItem );
1013 // ============================================================================
1015 PCDefinitionModel::PCDefinitionModel() :
1016 mfRefreshedDate( 0.0 ),
1017 mnRecords( 0 ),
1018 mnMissItemsLimit( 0 ),
1019 mnDatabaseFields( 0 ),
1020 mbInvalid( false ),
1021 mbSaveData( true ),
1022 mbRefreshOnLoad( false ),
1023 mbOptimizeMemory( false ),
1024 mbEnableRefresh( true ),
1025 mbBackgroundQuery( false ),
1026 mbUpgradeOnRefresh( false ),
1027 mbTupleCache( false ),
1028 mbSupportSubquery( false ),
1029 mbSupportDrill( false )
1033 // ----------------------------------------------------------------------------
1035 PCSourceModel::PCSourceModel() :
1036 mnSourceType( XML_TOKEN_INVALID ),
1037 mnConnectionId( 0 )
1041 // ----------------------------------------------------------------------------
1043 PCWorksheetSourceModel::PCWorksheetSourceModel()
1045 maRange.StartColumn = maRange.StartRow = maRange.EndColumn = maRange.EndRow = -1;
1048 // ----------------------------------------------------------------------------
1050 PivotCache::PivotCache( const WorkbookHelper& rHelper ) :
1051 WorkbookHelper( rHelper ),
1052 mbValidSource( false ),
1053 mbDummySheet( false )
1057 void PivotCache::importPivotCacheDefinition( const AttributeList& rAttribs )
1059 maDefModel.maRelId = rAttribs.getString( R_TOKEN( id ), OUString() );
1060 maDefModel.maRefreshedBy = rAttribs.getXString( XML_refreshedBy, OUString() );
1061 maDefModel.mfRefreshedDate = rAttribs.getDouble( XML_refreshedDate, 0.0 );
1062 maDefModel.mnRecords = rAttribs.getInteger( XML_recordCount, 0 );
1063 maDefModel.mnMissItemsLimit = rAttribs.getInteger( XML_missingItemsLimit, 0 );
1064 maDefModel.mbInvalid = rAttribs.getBool( XML_invalid, false );
1065 maDefModel.mbSaveData = rAttribs.getBool( XML_saveData, true );
1066 maDefModel.mbRefreshOnLoad = rAttribs.getBool( XML_refreshOnLoad, false );
1067 maDefModel.mbOptimizeMemory = rAttribs.getBool( XML_optimizeMemory, false );
1068 maDefModel.mbEnableRefresh = rAttribs.getBool( XML_enableRefresh, true );
1069 maDefModel.mbBackgroundQuery = rAttribs.getBool( XML_backgroundQuery, false );
1070 maDefModel.mbUpgradeOnRefresh = rAttribs.getBool( XML_upgradeOnRefresh, false );
1071 maDefModel.mbTupleCache = rAttribs.getBool( XML_tupleCache, false );
1072 maDefModel.mbSupportSubquery = rAttribs.getBool( XML_supportSubquery, false );
1073 maDefModel.mbSupportDrill = rAttribs.getBool( XML_supportAdvancedDrill, false );
1076 void PivotCache::importCacheSource( const AttributeList& rAttribs )
1078 maSourceModel.mnSourceType = rAttribs.getToken( XML_type, XML_TOKEN_INVALID );
1079 maSourceModel.mnConnectionId = rAttribs.getInteger( XML_connectionId, 0 );
1082 void PivotCache::importWorksheetSource( const AttributeList& rAttribs, const Relations& rRelations )
1084 maSheetSrcModel.maRelId = rAttribs.getString( R_TOKEN( id ), OUString() );
1085 maSheetSrcModel.maSheet = rAttribs.getXString( XML_sheet, OUString() );
1086 maSheetSrcModel.maDefName = rAttribs.getXString( XML_name, OUString() );
1088 // resolve URL of external document
1089 maTargetUrl = rRelations.getExternalTargetFromRelId( maSheetSrcModel.maRelId );
1090 // store range address unchecked with sheet index 0, will be resolved/checked later
1091 getAddressConverter().convertToCellRangeUnchecked( maSheetSrcModel.maRange, rAttribs.getString( XML_ref, OUString() ), 0 );
1094 void PivotCache::importPCDefinition( RecordInputStream& rStrm )
1096 sal_uInt8 nFlags1, nFlags2;
1097 rStrm.skip( 3 ); // create/refresh version id's
1098 rStrm >> nFlags1 >> maDefModel.mnMissItemsLimit >> maDefModel.mfRefreshedDate >> nFlags2 >> maDefModel.mnRecords;
1099 if( getFlag( nFlags2, OOBIN_PCDEFINITION_HASUSERNAME ) )
1100 rStrm >> maDefModel.maRefreshedBy;
1101 if( getFlag( nFlags2, OOBIN_PCDEFINITION_HASRELID ) )
1102 rStrm >> maDefModel.maRelId;
1104 maDefModel.mbInvalid = getFlag( nFlags1, OOBIN_PCDEFINITION_INVALID );
1105 maDefModel.mbSaveData = getFlag( nFlags1, OOBIN_PCDEFINITION_SAVEDATA );
1106 maDefModel.mbRefreshOnLoad = getFlag( nFlags1, OOBIN_PCDEFINITION_REFRESHONLOAD );
1107 maDefModel.mbOptimizeMemory = getFlag( nFlags1, OOBIN_PCDEFINITION_OPTIMIZEMEMORY );
1108 maDefModel.mbEnableRefresh = getFlag( nFlags1, OOBIN_PCDEFINITION_ENABLEREFRESH );
1109 maDefModel.mbBackgroundQuery = getFlag( nFlags1, OOBIN_PCDEFINITION_BACKGROUNDQUERY );
1110 maDefModel.mbUpgradeOnRefresh = getFlag( nFlags1, OOBIN_PCDEFINITION_UPGRADEONREFR );
1111 maDefModel.mbTupleCache = getFlag( nFlags1, OOBIN_PCDEFINITION_TUPELCACHE );
1112 maDefModel.mbSupportSubquery = getFlag( nFlags2, OOBIN_PCDEFINITION_SUPPORTSUBQUERY );
1113 maDefModel.mbSupportDrill = getFlag( nFlags2, OOBIN_PCDEFINITION_SUPPORTDRILL );
1116 void PivotCache::importPCDSource( RecordInputStream& rStrm )
1118 sal_Int32 nSourceType;
1119 rStrm >> nSourceType >> maSourceModel.mnConnectionId;
1120 static const sal_Int32 spnSourceTypes[] = { XML_worksheet, XML_external, XML_consolidation, XML_scenario };
1121 maSourceModel.mnSourceType = STATIC_ARRAY_SELECT( spnSourceTypes, nSourceType, XML_TOKEN_INVALID );
1124 void PivotCache::importPCDSheetSource( RecordInputStream& rStrm, const Relations& rRelations )
1126 sal_uInt8 nIsDefName, nIsBuiltinName, nFlags;
1127 rStrm >> nIsDefName >> nIsBuiltinName >> nFlags;
1128 if( getFlag( nFlags, OOBIN_PCDWBSOURCE_HASSHEET ) )
1129 rStrm >> maSheetSrcModel.maSheet;
1130 if( getFlag( nFlags, OOBIN_PCDWBSOURCE_HASRELID ) )
1131 rStrm >> maSheetSrcModel.maRelId;
1133 // read cell range or defined name
1134 if( nIsDefName == 0 )
1136 BinRange aBinRange;
1137 rStrm >> aBinRange;
1138 // store range address unchecked with sheet index 0, will be resolved/checked later
1139 getAddressConverter().convertToCellRangeUnchecked( maSheetSrcModel.maRange, aBinRange, 0 );
1141 else
1143 rStrm >> maSheetSrcModel.maDefName;
1144 if( nIsBuiltinName != 0 )
1145 maSheetSrcModel.maDefName = CREATE_OUSTRING( "_xlnm." ) + maSheetSrcModel.maDefName;
1148 // resolve URL of external document
1149 maTargetUrl = rRelations.getExternalTargetFromRelId( maSheetSrcModel.maRelId );
1152 void PivotCache::importPCDSource( BiffInputStream& rStrm )
1154 switch( rStrm.readuInt16() )
1156 case BIFF_PCDSOURCE_WORKSHEET:
1158 maSourceModel.mnSourceType = XML_worksheet;
1159 sal_uInt16 nNextRecId = rStrm.getNextRecId();
1160 switch( nNextRecId )
1162 case BIFF_ID_DCONREF: if( rStrm.startNextRecord() ) importDConRef( rStrm ); break;
1163 case BIFF_ID_DCONNAME: if( rStrm.startNextRecord() ) importDConName( rStrm ); break;
1164 case BIFF_ID_DCONBINAME: if( rStrm.startNextRecord() ) importDConBIName( rStrm ); break;
1167 break;
1168 case BIFF_PCDSOURCE_EXTERNAL:
1169 maSourceModel.mnSourceType = XML_external;
1170 break;
1171 case BIFF_PCDSOURCE_CONSOLIDATION:
1172 maSourceModel.mnSourceType = XML_consolidation;
1173 break;
1174 case BIFF_PCDSOURCE_SCENARIO:
1175 maSourceModel.mnSourceType = XML_scenario;
1176 break;
1177 default:
1178 maSourceModel.mnSourceType = XML_TOKEN_INVALID;
1182 void PivotCache::importPCDefinition( BiffInputStream& rStrm )
1184 sal_uInt16 nFlags, nUserNameLen;
1185 rStrm >> maDefModel.mnRecords;
1186 rStrm.skip( 2 ); // repeated cache ID
1187 rStrm >> nFlags;
1188 rStrm.skip( 2 ); // unused
1189 rStrm >> maDefModel.mnDatabaseFields;
1190 rStrm.skip( 6 ); // total field count, report record count, (repeated) cache type
1191 rStrm >> nUserNameLen;
1192 if( nUserNameLen != BIFF_PC_NOSTRING )
1193 maDefModel.maRefreshedBy = (getBiff() == BIFF8) ?
1194 rStrm.readUniString( nUserNameLen ) :
1195 rStrm.readCharArrayUC( nUserNameLen, getTextEncoding() );
1197 maDefModel.mbInvalid = getFlag( nFlags, BIFF_PCDEFINITION_INVALID );
1198 maDefModel.mbSaveData = getFlag( nFlags, BIFF_PCDEFINITION_SAVEDATA );
1199 maDefModel.mbRefreshOnLoad = getFlag( nFlags, BIFF_PCDEFINITION_REFRESHONLOAD );
1200 maDefModel.mbOptimizeMemory = getFlag( nFlags, BIFF_PCDEFINITION_OPTIMIZEMEMORY );
1201 maDefModel.mbEnableRefresh = getFlag( nFlags, BIFF_PCDEFINITION_ENABLEREFRESH );
1202 maDefModel.mbBackgroundQuery = getFlag( nFlags, BIFF_PCDEFINITION_BACKGROUNDQUERY );
1204 if( (rStrm.getNextRecId() == BIFF_ID_PCDEFINITION2) && rStrm.startNextRecord() )
1205 rStrm >> maDefModel.mfRefreshedDate;
1208 PivotCacheField& PivotCache::createCacheField( bool bInitDatabaseField )
1210 bool bIsDatabaseField = !bInitDatabaseField || (maFields.size() < maDefModel.mnDatabaseFields);
1211 PivotCacheFieldVector::value_type xCacheField( new PivotCacheField( *this, bIsDatabaseField ) );
1212 maFields.push_back( xCacheField );
1213 return *xCacheField;
1216 void PivotCache::finalizeImport()
1218 // collect all fields that are based on source data (needed to finalize source data below)
1219 OSL_ENSURE( !maFields.empty(), "PivotCache::finalizeImport - no pivot cache fields found" );
1220 for( PivotCacheFieldVector::const_iterator aIt = maFields.begin(), aEnd = maFields.end(); aIt != aEnd; ++aIt )
1222 if( (*aIt)->isDatabaseField() )
1224 OSL_ENSURE( (aIt == maFields.begin()) || (*(aIt - 1))->isDatabaseField(),
1225 "PivotCache::finalizeImport - database field follows a calculated field" );
1226 maDatabaseIndexes.push_back( static_cast< sal_Int32 >( maDatabaseFields.size() ) );
1227 maDatabaseFields.push_back( *aIt );
1229 else
1231 maDatabaseIndexes.push_back( -1 );
1234 OSL_ENSURE( !maDatabaseFields.empty(), "PivotCache::finalizeImport - no pivot cache source fields found" );
1236 // finalize source data depending on source type
1237 switch( maSourceModel.mnSourceType )
1239 case XML_worksheet:
1241 // decide whether an external document is used
1242 bool bInternal = (maTargetUrl.getLength() == 0) && (maSheetSrcModel.maRelId.getLength() == 0);
1243 bool bExternal = maTargetUrl.getLength() > 0; // relation ID may be empty, e.g. BIFF import
1244 OSL_ENSURE( bInternal || bExternal, "PivotCache::finalizeImport - invalid external document URL" );
1245 if( bInternal )
1246 finalizeInternalSheetSource();
1247 else if( bExternal )
1248 finalizeExternalSheetSource();
1250 break;
1252 // currently, we only support worksheet data sources
1253 case XML_external:
1254 break;
1255 case XML_consolidation:
1256 break;
1257 case XML_scenario:
1258 break;
1262 sal_Int32 PivotCache::getCacheFieldCount() const
1264 return static_cast< sal_Int32 >( maFields.size() );
1267 const PivotCacheField* PivotCache::getCacheField( sal_Int32 nFieldIdx ) const
1269 return maFields.get( nFieldIdx ).get();
1272 sal_Int32 PivotCache::getCacheDatabaseIndex( sal_Int32 nFieldIdx ) const
1274 return ContainerHelper::getVectorElement< sal_Int32 >( maDatabaseIndexes, nFieldIdx, -1 );
1277 void PivotCache::writeSourceHeaderCells( WorksheetHelper& rSheetHelper ) const
1279 OSL_ENSURE( static_cast< size_t >( maSheetSrcModel.maRange.EndColumn - maSheetSrcModel.maRange.StartColumn + 1 ) == maDatabaseFields.size(),
1280 "PivotCache::writeSourceHeaderCells - source cell range width does not match number of source fields" );
1281 sal_Int32 nCol = maSheetSrcModel.maRange.StartColumn;
1282 sal_Int32 nMaxCol = getAddressConverter().getMaxApiAddress().Column;
1283 sal_Int32 nRow = maSheetSrcModel.maRange.StartRow;
1284 for( PivotCacheFieldVector::const_iterator aIt = maDatabaseFields.begin(), aEnd = maDatabaseFields.end(); (aIt != aEnd) && (nCol <= nMaxCol); ++aIt, ++nCol )
1285 (*aIt)->writeSourceHeaderCell( rSheetHelper, nCol, nRow );
1288 void PivotCache::writeSourceDataCell( WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow, const PivotCacheItem& rItem ) const
1290 OSL_ENSURE( (0 <= nCol) && (nCol <= maSheetSrcModel.maRange.EndColumn - maSheetSrcModel.maRange.StartColumn), "PivotCache::writeSourceDataCell - invalid column index" );
1291 OSL_ENSURE( (0 < nRow) && (nRow <= maSheetSrcModel.maRange.EndRow - maSheetSrcModel.maRange.StartRow), "PivotCache::writeSourceDataCell - invalid row index" );
1292 if( const PivotCacheField* pCacheField = maDatabaseFields.get( nCol ).get() )
1293 pCacheField->writeSourceDataCell( rSheetHelper, maSheetSrcModel.maRange.StartColumn + nCol, maSheetSrcModel.maRange.StartRow + nRow, rItem );
1296 void PivotCache::importPCRecord( RecordInputStream& rStrm, WorksheetHelper& rSheetHelper, sal_Int32 nRow ) const
1298 OSL_ENSURE( (0 < nRow) && (nRow <= maSheetSrcModel.maRange.EndRow - maSheetSrcModel.maRange.StartRow), "PivotCache::importPCRecord - invalid row index" );
1299 sal_Int32 nCol = maSheetSrcModel.maRange.StartColumn;
1300 sal_Int32 nMaxCol = getAddressConverter().getMaxApiAddress().Column;
1301 nRow += maSheetSrcModel.maRange.StartRow;
1302 for( PivotCacheFieldVector::const_iterator aIt = maDatabaseFields.begin(), aEnd = maDatabaseFields.end(); !rStrm.isEof() && (aIt != aEnd) && (nCol <= nMaxCol); ++aIt, ++nCol )
1303 (*aIt)->importPCRecordItem( rStrm, rSheetHelper, nCol, nRow );
1306 void PivotCache::importPCItemIndexList( BiffInputStream& rStrm, WorksheetHelper& rSheetHelper, sal_Int32 nRow ) const
1308 OSL_ENSURE( (0 < nRow) && (nRow <= maSheetSrcModel.maRange.EndRow - maSheetSrcModel.maRange.StartRow), "PivotCache::importPCItemIndexList - invalid row index" );
1309 sal_Int32 nCol = maSheetSrcModel.maRange.StartColumn;
1310 sal_Int32 nMaxCol = getAddressConverter().getMaxApiAddress().Column;
1311 nRow += maSheetSrcModel.maRange.StartRow;
1312 for( PivotCacheFieldVector::const_iterator aIt = maDatabaseFields.begin(), aEnd = maDatabaseFields.end(); !rStrm.isEof() && (aIt != aEnd) && (nCol <= nMaxCol); ++aIt, ++nCol )
1313 if( (*aIt)->hasSharedItems() )
1314 (*aIt)->importPCItemIndex( rStrm, rSheetHelper, nCol, nRow );
1317 // private --------------------------------------------------------------------
1319 void PivotCache::importDConRef( BiffInputStream& rStrm )
1321 BinRange aBinRange;
1322 aBinRange.read( rStrm, false ); // always 8-bit column indexes
1323 // store range address unchecked with sheet index 0, will be resolved/checked later
1324 getAddressConverter().convertToCellRangeUnchecked( maSheetSrcModel.maRange, aBinRange, 0 );
1326 // the URL with (required) sheet name and optional URL of an external document
1327 importDConUrl( rStrm );
1328 OSL_ENSURE( maSheetSrcModel.maSheet.getLength() > 0, "PivotCache::importDConRef - missing sheet name" );
1331 void PivotCache::importDConName( BiffInputStream& rStrm )
1333 maSheetSrcModel.maDefName = (getBiff() == BIFF8) ? rStrm.readUniString() : rStrm.readByteStringUC( false, getTextEncoding() );
1334 OSL_ENSURE( maSheetSrcModel.maDefName.getLength() > 0, "PivotCache::importDConName - missing defined name" );
1335 importDConUrl( rStrm );
1338 void PivotCache::importDConBIName( BiffInputStream& rStrm )
1340 sal_uInt8 nNameId = rStrm.readuInt8();
1341 rStrm.skip( 3 );
1342 maSheetSrcModel.maDefName = OUString( sal_Unicode( nNameId ) );
1343 importDConUrl( rStrm );
1346 void PivotCache::importDConUrl( BiffInputStream& rStrm )
1348 // the URL with sheet name and optional URL of an external document
1349 OUString aEncodedUrl;
1350 if( getBiff() == BIFF8 )
1352 // empty string does not contain a flags byte, cannot use simple readUniString() here...
1353 sal_uInt16 nChars = rStrm.readuInt16();
1354 if( nChars > 0 )
1355 aEncodedUrl = rStrm.readUniString( nChars );
1357 else
1359 aEncodedUrl = rStrm.readByteStringUC( false, getTextEncoding() );
1362 if( aEncodedUrl.getLength() > 0 )
1364 OUString aClassName;
1365 getAddressConverter().parseBiffTargetUrl( aClassName, maTargetUrl, maSheetSrcModel.maSheet, aEncodedUrl, true );
1369 void PivotCache::finalizeInternalSheetSource()
1371 // resolve sheet name to sheet index
1372 sal_Int16 nSheet = getWorksheets().getCalcSheetIndex( maSheetSrcModel.maSheet );
1374 // if cache is based on a defined name or table, try to resolve to cell range
1375 if( maSheetSrcModel.maDefName.getLength() > 0 )
1377 // local or global defined name
1378 if( const DefinedName* pDefName = getDefinedNames().getByModelName( maSheetSrcModel.maDefName, nSheet ).get() )
1380 mbValidSource = pDefName->getAbsoluteRange( maSheetSrcModel.maRange );
1382 // table
1383 else if( const Table* pTable = getTables().getTable( maSheetSrcModel.maDefName ).get() )
1385 // get original range from table, but exclude the totals row(s)
1386 maSheetSrcModel.maRange = pTable->getOriginalRange();
1387 mbValidSource = (pTable->getHeight() - pTable->getTotalsRows()) > 1;
1388 if( mbValidSource )
1389 maSheetSrcModel.maRange.EndRow -= pTable->getTotalsRows();
1392 // else try the cell range (if the sheet exists)
1393 else if( nSheet >= 0 )
1395 // insert sheet index into the range, range address will be checked below
1396 maSheetSrcModel.maRange.Sheet = nSheet;
1397 mbValidSource = true;
1399 // else sheet has been deleted, generate the source data from cache
1400 else if( maSheetSrcModel.maSheet.getLength() > 0 )
1402 prepareSourceDataSheet();
1403 // return here to skip the source range check below
1404 return;
1407 // check range location, do not allow ranges that overflow the sheet partly
1408 mbValidSource = mbValidSource &&
1409 getAddressConverter().checkCellRange( maSheetSrcModel.maRange, false, true ) &&
1410 (maSheetSrcModel.maRange.StartRow < maSheetSrcModel.maRange.EndRow);
1413 void PivotCache::finalizeExternalSheetSource()
1415 /* If pivot cache is based on external sheet data, try to restore sheet
1416 data from cache records. No support for external defined names or tables,
1417 sheet name and path to cache records fragment (OOX only) are required. */
1418 bool bHasRelation = (getFilterType() == FILTER_BIFF) || (maDefModel.maRelId.getLength() > 0);
1419 if( bHasRelation && (maSheetSrcModel.maDefName.getLength() == 0) && (maSheetSrcModel.maSheet.getLength() > 0) )
1420 prepareSourceDataSheet();
1423 void PivotCache::prepareSourceDataSheet()
1425 // data will be inserted in top-left cell, sheet index is still set to 0 (will be set below)
1426 maSheetSrcModel.maRange.EndColumn -= maSheetSrcModel.maRange.StartColumn;
1427 maSheetSrcModel.maRange.StartColumn = 0;
1428 maSheetSrcModel.maRange.EndRow -= maSheetSrcModel.maRange.StartRow;
1429 maSheetSrcModel.maRange.StartRow = 0;
1430 // check range location, do not allow ranges that overflow the sheet partly
1431 if( getAddressConverter().checkCellRange( maSheetSrcModel.maRange, false, true ) )
1433 OUString aSheetName = CREATE_OUSTRING( "DPCache_" ) + maSheetSrcModel.maSheet;
1434 maSheetSrcModel.maRange.Sheet = getWorksheets().insertEmptySheet( aSheetName, false );
1435 mbValidSource = mbDummySheet = maSheetSrcModel.maRange.Sheet >= 0;
1439 // ============================================================================
1441 PivotCacheBuffer::PivotCacheBuffer( const WorkbookHelper& rHelper ) :
1442 WorkbookHelper( rHelper )
1446 void PivotCacheBuffer::registerPivotCacheFragment( sal_Int32 nCacheId, const OUString& rFragmentPath )
1448 OSL_ENSURE( nCacheId >= 0, "PivotCacheBuffer::registerPivotCacheFragment - invalid pivot cache identifier" );
1449 OSL_ENSURE( maFragmentPaths.count( nCacheId ) == 0, "PivotCacheBuffer::registerPivotCacheFragment - fragment path exists already" );
1450 if( (nCacheId >= 0) && (rFragmentPath.getLength() > 0) )
1451 maFragmentPaths[ nCacheId ] = rFragmentPath;
1454 void PivotCacheBuffer::importPivotCacheRef( BiffInputStream& rStrm )
1456 // read the PIVOTCACHE record that contains the stream ID
1457 sal_Int32 nCacheId = rStrm.readuInt16();
1458 OSL_ENSURE( maFragmentPaths.count( nCacheId ) == 0, "PivotCacheBuffer::importPivotCacheRef - cache stream exists already" );
1459 OUStringBuffer aStrmName;
1460 static const sal_Unicode spcHexChars[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
1461 for( sal_uInt8 nBit = 0; nBit < 16; nBit += 4 )
1462 aStrmName.insert( 0, spcHexChars[ extractValue< size_t >( nCacheId, nBit, 4 ) ] );
1463 aStrmName.insert( 0, (getBiff() == BIFF8) ? CREATE_OUSTRING( "_SX_DB_CUR/" ) : CREATE_OUSTRING( "_SX_DB/" ) );
1464 maFragmentPaths[ nCacheId ] = aStrmName.makeStringAndClear();
1466 // try to read PCDSOURCE record (will read following data location records too)
1467 sal_uInt16 nNextRecId = rStrm.getNextRecId();
1468 OSL_ENSURE( nNextRecId == BIFF_ID_PCDSOURCE, "PivotCacheBuffer::importPivotCacheRef - PCDSOURCE record expected" );
1469 if( (nNextRecId == BIFF_ID_PCDSOURCE) && rStrm.startNextRecord() )
1470 createPivotCache( nCacheId ).importPCDSource( rStrm );
1473 PivotCache* PivotCacheBuffer::importPivotCacheFragment( sal_Int32 nCacheId )
1475 switch( getFilterType() )
1477 /* BIFF filter: Pivot table provides 0-based index into list of pivot
1478 cache source links (PIVOTCACHE/PCDSOURCE/... record blocks in
1479 workbook stream). First, this index has to be resolved to the cache
1480 identifier that is used to manage the cache stream names (the
1481 maFragmentPaths member). The cache object itself exists already
1482 before the first call for the cache source index (see
1483 PivotCacheBuffer::importPivotCacheRef() above), because source data
1484 link is part of workbook data, not of the cache stream. To detect
1485 subsequent calls with an already initialized cache, the entry in
1486 maFragmentPaths will be removed after reading the cache stream. */
1487 case FILTER_BIFF:
1489 /* Resolve cache index to cache identifier and try to find pivot
1490 cache. Cache must exist already for a valid cache index. */
1491 nCacheId = ContainerHelper::getVectorElement< sal_Int32 >( maCacheIds, nCacheId, -1 );
1492 PivotCache* pCache = maCaches.get( nCacheId ).get();
1493 if( !pCache )
1494 return 0;
1496 /* Try to find fragment path entry (stream name). If missing, the
1497 stream has been read already, and the cache can be returned. */
1498 FragmentPathMap::iterator aIt = maFragmentPaths.find( nCacheId );
1499 if( aIt != maFragmentPaths.end() )
1501 /* Import the cache stream. This may create a dummy data sheet
1502 for external sheet sources. */
1503 BiffPivotCacheFragment( *this, aIt->second, *pCache ).importFragment();
1504 // remove the fragment entry to mark that the cache is initialized
1505 maFragmentPaths.erase( aIt );
1507 return pCache;
1510 /* OOX/OOBIN filter: On first call for the cache ID, the pivot cache
1511 object is created and inserted into maCaches. Then, the cache
1512 definition fragment is read and the cache is returned. On
1513 subsequent calls, the created cache will be found in maCaches and
1514 returned immediately. */
1515 case FILTER_OOX:
1517 // try to find an imported pivot cache
1518 if( PivotCache* pCache = maCaches.get( nCacheId ).get() )
1519 return pCache;
1521 // check if a fragment path exists for the passed cache identifier
1522 FragmentPathMap::iterator aIt = maFragmentPaths.find( nCacheId );
1523 if( aIt == maFragmentPaths.end() )
1524 return 0;
1526 /* Import the cache fragment. This may create a dummy data sheet
1527 for external sheet sources. */
1528 PivotCache& rCache = createPivotCache( nCacheId );
1529 importOoxFragment( new OoxPivotCacheDefinitionFragment( *this, aIt->second, rCache ) );
1530 return &rCache;
1533 case FILTER_UNKNOWN:
1534 OSL_ENSURE( false, "PivotCacheBuffer::importPivotCacheFragment - unknown filter type" );
1536 return 0;
1539 PivotCache& PivotCacheBuffer::createPivotCache( sal_Int32 nCacheId )
1541 maCacheIds.push_back( nCacheId );
1542 PivotCacheMap::mapped_type& rxCache = maCaches[ nCacheId ];
1543 rxCache.reset( new PivotCache( *this ) );
1544 return *rxCache;
1547 // ============================================================================
1549 } // namespace xls
1550 } // namespace oox