fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / filter / inc / pivotcachebuffer.hxx
blobb324071990636bb1aef3e46748633708fb41adf0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SC_SOURCE_FILTER_INC_PIVOTCACHEBUFFER_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_PIVOTCACHEBUFFER_HXX
23 #include <com/sun/star/table/CellAddress.hpp>
24 #include <com/sun/star/table/CellRangeAddress.hpp>
25 #include <com/sun/star/util/DateTime.hpp>
26 #include <oox/helper/containerhelper.hxx>
27 #include <oox/helper/refvector.hxx>
28 #include "workbookhelper.hxx"
30 namespace com { namespace sun { namespace star {
31 namespace sheet { class XDataPilotField; }
32 } } }
34 namespace oox { namespace core { class Relations; } }
36 namespace oox {
37 namespace xls {
39 class WorksheetHelper;
41 typedef ::std::pair< sal_Int32, OUString > IdCaptionPair;
42 typedef ::std::vector< IdCaptionPair > IdCaptionPairList;
44 class PivotCacheItem
46 public:
47 explicit PivotCacheItem();
49 /** Reads the string value from a pivot cache item. */
50 void readString( const AttributeList& rAttribs );
51 /** Reads the double value from a pivot cache item. */
52 void readNumeric( const AttributeList& rAttribs );
53 /** Reads the date/time value from a pivot cache item. */
54 void readDate( const AttributeList& rAttribs );
55 /** Reads the boolean value from a pivot cache item. */
56 void readBool( const AttributeList& rAttribs );
57 /** Reads the error code value from a pivot cache item. */
58 void readError( const AttributeList& rAttribs, const UnitConverter& rUnitConverter );
59 /** Reads the index of a shared item. */
60 void readIndex( const AttributeList& rAttribs );
62 /** Reads the string value from a pivot cache item. */
63 void readString( SequenceInputStream& rStrm );
64 /** Reads the double value from a pivot cache item. */
65 void readDouble( SequenceInputStream& rStrm );
66 /** Reads the date/time value from a pivot cache item. */
67 void readDate( SequenceInputStream& rStrm );
68 /** Reads the boolean value from a pivot cache item. */
69 void readBool( SequenceInputStream& rStrm );
70 /** Reads the error code value from a pivot cache item. */
71 void readError( SequenceInputStream& rStrm );
72 /** Reads the index of a shared item. */
73 void readIndex( SequenceInputStream& rStrm );
75 /** Reads the string value from a pivot cache item. */
76 void readString( BiffInputStream& rStrm, const WorkbookHelper& rHelper );
77 /** Reads the double value from a pivot cache item. */
78 void readDouble( BiffInputStream& rStrm );
79 /** Reads the integer value from a pivot cache item. */
80 void readInteger( BiffInputStream& rStrm );
81 /** Reads the date/time value from a pivot cache item. */
82 void readDate( BiffInputStream& rStrm );
83 /** Reads the boolean value from a pivot cache item. */
84 void readBool( BiffInputStream& rStrm );
85 /** Reads the error code value from a pivot cache item. */
86 void readError( BiffInputStream& rStrm );
88 /** Returns the type of the item. */
89 inline sal_Int32 getType() const { return mnType; }
90 /** Returns the value of the item. */
91 inline const ::com::sun::star::uno::Any& getValue() const { return maValue; }
92 /** Returns the string representation of the item. */
93 OUString getName() const;
94 /** Returns true if the item is unused. */
95 inline bool isUnused() const { return mbUnused; }
97 private:
98 friend class PivotCacheItemList;
99 // #FIXME hack Sets the value of this item to the given string ( and overwrites type if necessary
100 void setStringValue( const OUString& sName );
101 ::com::sun::star::uno::Any maValue; /// Value of the item.
102 sal_Int32 mnType; /// Value type (OOXML token identifier).
103 bool mbUnused;
106 class PivotCacheItemList : public WorkbookHelper
108 public:
109 explicit PivotCacheItemList( const WorkbookHelper& rHelper );
111 /** Imports the item from the passed attribute list. */
112 void importItem( sal_Int32 nElement, const AttributeList& rAttribs );
113 /** Imports the item from the passed stream and record. */
114 void importItem( sal_Int32 nRecId, SequenceInputStream& rStrm );
115 /** Imports a complete item list from the passed stream. */
116 void importItemList( BiffInputStream& rStrm, sal_uInt16 nCount );
118 /** Returns true, if this item list is empty. */
119 inline bool empty() const { return maItems.empty(); }
120 /** Returns the size of the item list. */
121 inline size_t size() const { return maItems.size(); }
123 /** Returns the specified item. */
124 const PivotCacheItem* getCacheItem( sal_Int32 nItemIdx ) const;
125 /** Returns the names of all items. */
126 void getCacheItemNames( ::std::vector< OUString >& orItemNames ) const;
127 void applyItemCaptions( const IdCaptionPairList& vCaptions );
129 private:
130 /** Creates and returns a new item at the end of the items list. */
131 PivotCacheItem& createItem();
132 /** Imports an array of items from the PCITEM_ARRAY record */
133 void importArray( SequenceInputStream& rStrm );
135 private:
136 typedef ::std::vector< PivotCacheItem > CacheItemVector;
137 CacheItemVector maItems; /// All items of this list.
140 struct PCFieldModel
142 OUString maName; /// Fixed name of the cache field.
143 OUString maCaption; /// Caption of the ccahe field.
144 OUString maPropertyName; /// OLAP property name.
145 OUString maFormula; /// Formula of a calculated field.
146 sal_Int32 mnNumFmtId; /// Number format for all items.
147 sal_Int32 mnSqlType; /// Data type from ODBC data source.
148 sal_Int32 mnHierarchy; /// Hierarchy this field is part of.
149 sal_Int32 mnLevel; /// Hierarchy level this field is part of.
150 sal_Int32 mnMappingCount; /// Number of property mappings.
151 bool mbDatabaseField; /// True = field from source data; false = calculated field.
152 bool mbServerField; /// True = ODBC server-based page field.
153 bool mbUniqueList; /// True = list of unique ODBC items exists.
154 bool mbMemberPropField; /// True = contains OLAP member properties.
156 explicit PCFieldModel();
159 struct PCSharedItemsModel
161 bool mbHasSemiMixed; /// True = has (blank|string|bool|error) item(s), maybe other types.
162 bool mbHasNonDate; /// True = has non-date item(s), maybe date items.
163 bool mbHasDate; /// True = has date item(s), maybe other types.
164 bool mbHasString; /// True = has (string|bool|error) item(s), maybe other types.
165 bool mbHasBlank; /// True = has blank item(s), maybe other types.
166 bool mbHasMixed; /// True = has [(string|bool|error) and (number|date)] or (number and date).
167 bool mbIsNumeric; /// True = has numeric item(s), maybe other types except date.
168 bool mbIsInteger; /// True = has numeric item(s) with only integers, maybe other types except date.
169 bool mbHasLongText; /// True = contains strings with >255 charascters.
170 bool mbHasLongIndexes; /// True = indexes to shared items are 16-bit (BIFF only).
172 explicit PCSharedItemsModel();
175 struct PCFieldGroupModel
177 ::com::sun::star::util::DateTime maStartDate; /// Manual or calculated start date for range grouping.
178 ::com::sun::star::util::DateTime maEndDate; /// Manual or calculated end date for range grouping.
179 double mfStartValue; /// Manual or calculated start value for range grouping.
180 double mfEndValue; /// Manual or calculated end value for range grouping.
181 double mfInterval; /// Interval for numeric range grouping.
182 sal_Int32 mnParentField; /// Index of cache field that contains item groups based on this field.
183 sal_Int32 mnBaseField; /// Index of cache field this grouped field is based on.
184 sal_Int32 mnGroupBy; /// Type of numeric or date range grouping.
185 bool mbRangeGroup; /// True = items are grouped by numeric ranges or date ranges.
186 bool mbDateGroup; /// True = items are grouped by date ranges or by item names.
187 bool mbAutoStart; /// True = start value for range groups is calculated from source data.
188 bool mbAutoEnd; /// True = end value for range groups is calculated from source data.
190 explicit PCFieldGroupModel();
192 /** Sets the group-by value for BIFF import. */
193 void setBiffGroupBy( sal_uInt8 nGroupBy );
196 /** Helper struct for mapping original item names from/to group item names. */
197 struct PivotCacheGroupItem
199 OUString maOrigName;
200 OUString maGroupName;
202 inline explicit PivotCacheGroupItem( const OUString& rItemName ) :
203 maOrigName( rItemName ), maGroupName( rItemName ) {}
206 typedef ::std::vector< PivotCacheGroupItem > PivotCacheGroupItemVector;
208 class PivotCacheField : public WorkbookHelper
210 public:
211 explicit PivotCacheField( const WorkbookHelper& rHelper, bool bIsDatabaseField );
213 /** Imports pivot cache field settings from the cacheField element. */
214 void importCacheField( const AttributeList& rAttribs );
215 /** Imports shared items settings from the sharedItems element. */
216 void importSharedItems( const AttributeList& rAttribs );
217 /** Imports a shared item from the passed element. */
218 void importSharedItem( sal_Int32 nElement, const AttributeList& rAttribs );
219 /** Imports grouping settings from the fieldGroup element. */
220 void importFieldGroup( const AttributeList& rAttribs );
221 /** Imports numeric grouping settings from the rangePr element. */
222 void importRangePr( const AttributeList& rAttribs );
223 /** Imports an item of the mapping between group items and base items from the passed element. */
224 void importDiscretePrItem( sal_Int32 nElement, const AttributeList& rAttribs );
225 /** Imports a group item from the passed element. */
226 void importGroupItem( sal_Int32 nElement, const AttributeList& rAttribs );
228 /** Imports pivot cache field settings from the PCDFIELD record. */
229 void importPCDField( SequenceInputStream& rStrm );
230 /** Imports shared items settings from the PCDFSHAREDITEMS record. */
231 void importPCDFSharedItems( SequenceInputStream& rStrm );
232 /** Imports one or more shared items from the passed record. */
233 void importPCDFSharedItem( sal_Int32 nRecId, SequenceInputStream& rStrm );
234 /** Imports grouping settings from the PCDFIELDGROUP record. */
235 void importPCDFieldGroup( SequenceInputStream& rStrm );
236 /** Imports numeric grouping settings from the PCDFRANGEPR record. */
237 void importPCDFRangePr( SequenceInputStream& rStrm );
238 /** Imports an item of the mapping between group items and base items from the passed record. */
239 void importPCDFDiscretePrItem( sal_Int32 nRecId, SequenceInputStream& rStrm );
240 /** Imports one or more group items from the passed record. */
241 void importPCDFGroupItem( sal_Int32 nRecId, SequenceInputStream& rStrm );
243 /** Imports pivot cache field settings from the PCDFIELD record. */
244 void importPCDField( BiffInputStream& rStrm );
245 /** Imports numeric grouping settings from the PCDFRANGEPR record. */
246 void importPCDFRangePr( BiffInputStream& rStrm );
247 /** Imports the mapping between group items and base items from the PCDFDISCRETEPR record. */
248 void importPCDFDiscretePr( BiffInputStream& rStrm );
249 /** Apply user Captions to imported group data */
250 void applyItemCaptions( const IdCaptionPairList& vCaptions );
252 /** Returns true, if the field is based on source data, or false if it is grouped or calculated. */
253 inline bool isDatabaseField() const { return maFieldModel.mbDatabaseField; }
255 /** Returns true, if the field contains a list of shared items. */
256 inline bool hasSharedItems() const { return !maSharedItems.empty(); }
257 /** Returns true, if the field contains a list of grouping items. */
258 inline bool hasGroupItems() const { return !maGroupItems.empty(); }
259 /** Returns true, if the field has inplace numeric grouping settings. */
260 inline bool hasNumericGrouping() const { return maFieldGroupModel.mbRangeGroup && !maFieldGroupModel.mbDateGroup; }
261 /** Returns true, if the field has inplace date grouping settings. */
262 inline bool hasDateGrouping() const { return maFieldGroupModel.mbRangeGroup && maFieldGroupModel.mbDateGroup; }
263 /** Returns true, if the field has a parent group field that groups the items of this field. */
264 inline bool hasParentGrouping() const { return maFieldGroupModel.mnParentField >= 0; }
266 /** Returns the name of the cache field. */
267 inline const OUString& getName() const { return maFieldModel.maName; }
268 /** Returns the index of the parent group field that groups the items of this field. */
269 inline sal_Int32 getParentGroupField() const { return maFieldGroupModel.mnParentField; }
270 /** Returns the index of the base field grouping is based on. */
271 inline sal_Int32 getGroupBaseField() const { return maFieldGroupModel.mnBaseField; }
273 /** Returns the shared or group item with the specified index. */
274 const PivotCacheItem* getCacheItem( sal_Int32 nItemIdx ) const;
275 /** Returns the names of all shared or group items. */
276 void getCacheItemNames( ::std::vector< OUString >& orItemNames ) const;
277 /** Returns shared or group items. */
278 const PivotCacheItemList& getCacheItems() const;
280 /** Creates inplace numeric grouping settings. */
281 void convertNumericGrouping(
282 const ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDataPilotField >& rxDPField ) const;
283 /** Creates inplace date grouping settings or a new date group field. */
284 OUString createDateGroupField(
285 const ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDataPilotField >& rxBaseDPField ) const;
286 /** Creates a new grouped DataPilot field and returns its name. */
287 OUString createParentGroupField(
288 const ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDataPilotField >& rxBaseDPField,
289 const PivotCacheField& rBaseCacheField,
290 PivotCacheGroupItemVector& orItemNames ) const;
292 /** Writes the title of the field into the passed sheet at the passed address. */
293 void writeSourceHeaderCell( WorksheetHelper& rSheetHelper,
294 sal_Int32 nCol, sal_Int32 nRow ) const;
295 /** Writes a source field item value into the passed sheet. */
296 void writeSourceDataCell( WorksheetHelper& rSheetHelper,
297 sal_Int32 nCol, sal_Int32 nRow,
298 const PivotCacheItem& rItem ) const;
300 /** Reads an item from the PCRECORD record and writes it to the passed sheet. */
301 void importPCRecordItem( SequenceInputStream& rStrm,
302 WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow ) const;
303 /** Reads an item index from the PCITEM_INDEXLIST record and writes the item to the passed sheet. */
304 void importPCItemIndex( BiffInputStream& rStrm,
305 WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow ) const;
307 private:
308 /** Tries to write the passed value to the passed sheet position. */
309 static void writeItemToSourceDataCell( WorksheetHelper& rSheetHelper,
310 sal_Int32 nCol, sal_Int32 nRow, const PivotCacheItem& rItem );
311 /** Tries to write the value of a shared item to the passed sheet position. */
312 void writeSharedItemToSourceDataCell( WorksheetHelper& rSheetHelper,
313 sal_Int32 nCol, sal_Int32 nRow, sal_Int32 nItemIdx ) const;
315 private:
316 typedef ::std::vector< sal_Int32 > IndexVector;
318 PivotCacheItemList maSharedItems; /// All shared items of this field.
319 PivotCacheItemList maGroupItems; /// All group items of this field.
320 IndexVector maDiscreteItems; /// Mapping between group and base items.
321 PCFieldModel maFieldModel; /// Settings for this cache field.
322 PCSharedItemsModel maSharedItemsModel; /// Settings for shared items.
323 PCFieldGroupModel maFieldGroupModel; /// Settings for item grouping.
326 struct PCDefinitionModel
328 OUString maRelId; /// Relation identifier for cache records fragment.
329 OUString maRefreshedBy; /// Name of user who last refreshed the cache.
330 double mfRefreshedDate; /// Date/time of last refresh.
331 sal_Int32 mnRecords; /// Number of data records in the cache.
332 sal_Int32 mnMissItemsLimit; /// Limit for discarding unused items.
333 sal_uInt16 mnDatabaseFields; /// Number of database (source data) fields (BIFF only).
334 bool mbInvalid; /// True = cache needs refresh.
335 bool mbSaveData; /// True = cached item values are present.
336 bool mbRefreshOnLoad; /// True = try to refresh cache on load.
337 bool mbOptimizeMemory; /// True = application may optimize memory usage.
338 bool mbEnableRefresh; /// True = refreshing cache is enabled in UI.
339 bool mbBackgroundQuery; /// True = application queries data asynchonously.
340 bool mbUpgradeOnRefresh; /// True = application may upgrade cache version.
341 bool mbTupleCache; /// True = cache stores OLAP functions.
342 bool mbSupportSubquery; /// True = data source supports subqueries.
343 bool mbSupportDrill; /// True = data source supports drilldown.
345 explicit PCDefinitionModel();
348 struct PCSourceModel
350 sal_Int32 mnSourceType; /// Type of the source data (sheet, consolidation, scenario, external).
351 sal_Int32 mnConnectionId; /// Connection identifier for external data source.
353 explicit PCSourceModel();
356 struct PCWorksheetSourceModel
358 OUString maRelId; /// Relation identifier for an external document URL.
359 OUString maSheet; /// Sheet name for cell range or sheet-local defined names.
360 OUString maDefName; /// Defined name containing a cell range if present.
361 ::com::sun::star::table::CellRangeAddress
362 maRange; /// Source cell range of the data.
364 explicit PCWorksheetSourceModel();
367 class PivotCache : public WorkbookHelper
369 public:
370 explicit PivotCache( const WorkbookHelper& rHelper );
372 /** Reads pivot cache global settings from the pivotCacheDefinition element. */
373 void importPivotCacheDefinition( const AttributeList& rAttribs );
374 /** Reads cache source settings from the cacheSource element. */
375 void importCacheSource( const AttributeList& rAttribs );
376 /** Reads sheet source settings from the worksheetSource element. */
377 void importWorksheetSource( const AttributeList& rAttribs, const ::oox::core::Relations& rRelations );
379 /** Reads pivot cache global settings from the PCDEFINITION record. */
380 void importPCDefinition( SequenceInputStream& rStrm );
381 /** Reads cache source settings from the PCDSOURCE record. */
382 void importPCDSource( SequenceInputStream& rStrm );
383 /** Reads sheet source settings from the PCDSHEETSOURCE record. */
384 void importPCDSheetSource( SequenceInputStream& rStrm, const ::oox::core::Relations& rRelations );
386 /** Reads pivot cache global settings from the PCDEFINITION record. */
387 void importPCDefinition( BiffInputStream& rStrm );
389 /** Creates and returns a new pivot cache field. */
390 PivotCacheField& createCacheField( bool bInitDatabaseField = false );
391 /** Checks validity of source data and creates a dummy data sheet for external sheet sources. */
392 void finalizeImport();
394 /** Returns true, if the pivot cache is based on a valid data source, so
395 that pivot tables can be created based on this pivot cache. */
396 inline bool isValidDataSource() const { return mbValidSource; }
397 /** Returns true, if the pivot cache is based on a dummy sheet created in finalizeImport. */
398 inline bool isBasedOnDummySheet() const { return mbDummySheet; }
399 /** Returns the internal cell range the cache is based on. */
400 inline const ::com::sun::star::table::CellRangeAddress&
401 getSourceRange() const { return maSheetSrcModel.maRange; }
402 /** Returns the relation identifier of the pivot cache records fragment. */
403 inline const OUString& getRecordsRelId() const { return maDefModel.maRelId; }
405 /** Returns the number of pivot cache fields. */
406 sal_Int32 getCacheFieldCount() const;
407 /** Returns the cache field with the specified index. */
408 const PivotCacheField* getCacheField( sal_Int32 nFieldIdx ) const;
409 /** Returns the source column index of the field with the passed index. */
410 sal_Int32 getCacheDatabaseIndex( sal_Int32 nFieldIdx ) const;
412 /** Writes the titles of all source fields into the passed sheet. */
413 void writeSourceHeaderCells( WorksheetHelper& rSheetHelper ) const;
414 /** Writes a source field item value into the passed sheet. */
415 void writeSourceDataCell( WorksheetHelper& rSheetHelper,
416 sal_Int32 nColIdx, sal_Int32 nRowIdx,
417 const PivotCacheItem& rItem ) const;
419 /** Reads a PCRECORD record and writes all item values to the passed sheet. */
420 void importPCRecord( SequenceInputStream& rStrm,
421 WorksheetHelper& rSheetHelper, sal_Int32 nRowIdx ) const;
422 /** Reads a PCITEM_INDEXLIST record and writes all item values to the passed sheet. */
423 void importPCItemIndexList( BiffInputStream& rStrm,
424 WorksheetHelper& rSheetHelper, sal_Int32 nRowIdx ) const;
426 private:
428 /** Finalizes the pivot cache if it is based on internal sheet data. */
429 void finalizeInternalSheetSource();
430 /** Finalizes the pivot cache if it is based on sheet data of an external spreadsheet document. */
431 void finalizeExternalSheetSource();
432 /** Creates a dummy sheet that will be filled with the pivot cache data. */
433 void prepareSourceDataSheet();
434 /** Checks, if the row index has changed since last call, and initializes the sheet data buffer. */
435 void updateSourceDataRow( WorksheetHelper& rSheetHelper, sal_Int32 nRow ) const;
437 private:
438 typedef RefVector< PivotCacheField > PivotCacheFieldVector;
439 typedef ::std::vector< sal_Int32 > IndexVector;
441 PivotCacheFieldVector maFields; /// All pivot cache fields.
442 PivotCacheFieldVector maDatabaseFields; /// All cache fields that are based on source data.
443 IndexVector maDatabaseIndexes; /// Database field index for all fields.
444 PCDefinitionModel maDefModel; /// Global pivot cache settings.
445 PCSourceModel maSourceModel; /// Pivot cache source settings.
446 PCWorksheetSourceModel maSheetSrcModel; /// Sheet source data if cache type is sheet.
447 ValueRangeSet maColSpans; /// Column spans used by SheetDataBuffer for optimized cell import.
448 OUString maTargetUrl; /// URL of an external source document.
449 mutable sal_Int32 mnCurrRow; /// Current row index in dummy sheet.
450 bool mbValidSource; /// True = pivot cache is based on supported data source.
451 bool mbDummySheet; /// True = pivot cache is based on a dummy sheet.
454 class PivotCacheBuffer : public WorkbookHelper
456 public:
457 explicit PivotCacheBuffer( const WorkbookHelper& rHelper );
459 /** Registers a pivot cache definition fragment. The fragment will be loaded on demand (OOXML/BIFF12 only). */
460 void registerPivotCacheFragment( sal_Int32 nCacheId, const OUString& rFragmentPath );
462 /** Imports and stores a pivot cache definition fragment on first call,
463 returns the imported cache on subsequent calls with the same identifier. */
464 PivotCache* importPivotCacheFragment( sal_Int32 nCacheId );
466 private:
467 /** Creates and returns a new pivot cache object with the passed identifier. */
468 PivotCache& createPivotCache( sal_Int32 nCacheId );
470 private:
471 typedef ::std::map< sal_Int32, OUString > FragmentPathMap;
472 typedef RefMap< sal_Int32, PivotCache > PivotCacheMap;
473 typedef ::std::vector< sal_Int32 > PivotCacheIdVector;
475 FragmentPathMap maFragmentPaths;
476 PivotCacheMap maCaches;
477 PivotCacheIdVector maCacheIds;
480 } // namespace xls
481 } // namespace oox
483 #endif
485 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */