1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/util/DateTime.hpp>
24 #include <oox/helper/containerhelper.hxx>
25 #include <oox/helper/refvector.hxx>
26 #include "workbookhelper.hxx"
28 namespace oox
{ class AttributeList
; }
29 namespace oox
{ class SequenceInputStream
; }
31 namespace com
{ namespace sun
{ namespace star
{
32 namespace sheet
{ class XDataPilotField
; }
35 namespace oox
{ namespace core
{ class Relations
; } }
37 class ScDPSaveDimension
;
44 class WorksheetHelper
;
46 typedef ::std::pair
< sal_Int32
, OUString
> IdCaptionPair
;
47 typedef ::std::vector
< IdCaptionPair
> IdCaptionPairList
;
52 explicit PivotCacheItem();
54 /** Reads the string value from a pivot cache item. */
55 void readString( const AttributeList
& rAttribs
);
56 /** Reads the double value from a pivot cache item. */
57 void readNumeric( const AttributeList
& rAttribs
);
58 /** Reads the date/time value from a pivot cache item. */
59 void readDate( const AttributeList
& rAttribs
);
60 /** Reads the boolean value from a pivot cache item. */
61 void readBool( const AttributeList
& rAttribs
);
62 /** Reads the error code value from a pivot cache item. */
63 void readError( const AttributeList
& rAttribs
);
64 /** Reads the index of a shared item. */
65 void readIndex( const AttributeList
& rAttribs
);
67 /** Reads the string value from a pivot cache item. */
68 void readString( SequenceInputStream
& rStrm
);
69 /** Reads the double value from a pivot cache item. */
70 void readDouble( SequenceInputStream
& rStrm
);
71 /** Reads the date/time value from a pivot cache item. */
72 void readDate( SequenceInputStream
& rStrm
);
73 /** Reads the boolean value from a pivot cache item. */
74 void readBool( SequenceInputStream
& rStrm
);
75 /** Reads the error code value from a pivot cache item. */
76 void readError( SequenceInputStream
& rStrm
);
77 /** Reads the index of a shared item. */
78 void readIndex( SequenceInputStream
& rStrm
);
80 /** Returns the type of the item. */
81 sal_Int32
getType() const { return mnType
; }
82 /** Returns the value of the item. */
83 const css::uno::Any
& getValue() const { return maValue
; }
84 /** Returns the string representation of the item. */
85 OUString
getName() const;
87 /** Returns the string representation of the item, using the actual formatting. */
88 OUString
getFormattedName(const ScDPSaveDimension
& rSaveDim
, ScDPObject
* pObj
, const DateTime
& rNullDate
) const;
89 /** Returns true if the item is unused. */
90 bool isUnused() const { return mbUnused
; }
93 friend class PivotCacheItemList
;
94 // #FIXME hack Sets the value of this item to the given string ( and overwrites type if necessary
95 void setStringValue( const OUString
& sName
);
96 css::uno::Any maValue
; /// Value of the item.
97 sal_Int32 mnType
; /// Value type (OOXML token identifier).
101 class PivotCacheItemList
: public WorkbookHelper
104 explicit PivotCacheItemList( const WorkbookHelper
& rHelper
);
106 /** Imports the item from the passed attribute list. */
107 void importItem( sal_Int32 nElement
, const AttributeList
& rAttribs
);
108 /** Imports the item from the passed stream and record. */
109 void importItem( sal_Int32 nRecId
, SequenceInputStream
& rStrm
);
111 /** Returns true, if this item list is empty. */
112 bool empty() const { return maItems
.empty(); }
113 /** Returns the size of the item list. */
114 size_t size() const { return maItems
.size(); }
116 /** Returns the specified item. */
117 const PivotCacheItem
* getCacheItem( sal_Int32 nItemIdx
) const;
118 /** Returns the names of all items. */
119 void getCacheItemNames( ::std::vector
< OUString
>& orItemNames
) const;
120 void applyItemCaptions( const IdCaptionPairList
& vCaptions
);
123 /** Creates and returns a new item at the end of the items list. */
124 PivotCacheItem
& createItem();
125 /** Imports an array of items from the PCITEM_ARRAY record */
126 void importArray( SequenceInputStream
& rStrm
);
129 typedef ::std::vector
< PivotCacheItem
> CacheItemVector
;
130 CacheItemVector maItems
; /// All items of this list.
135 OUString maName
; /// Fixed name of the cache field.
136 OUString maCaption
; /// Caption of the cache field.
137 OUString maPropertyName
; /// OLAP property name.
138 OUString maFormula
; /// Formula of a calculated field.
139 sal_Int32 mnNumFmtId
; /// Number format for all items.
140 sal_Int32 mnSqlType
; /// Data type from ODBC data source.
141 sal_Int32 mnHierarchy
; /// Hierarchy this field is part of.
142 sal_Int32 mnLevel
; /// Hierarchy level this field is part of.
143 sal_Int32 mnMappingCount
; /// Number of property mappings.
144 bool mbDatabaseField
; /// True = field from source data; false = calculated field.
145 bool mbServerField
; /// True = ODBC server-based page field.
146 bool mbUniqueList
; /// True = list of unique ODBC items exists.
147 bool mbMemberPropField
; /// True = contains OLAP member properties.
149 explicit PCFieldModel();
152 struct PCSharedItemsModel
154 bool mbHasSemiMixed
; /// True = has (blank|string|bool|error) item(s), maybe other types.
155 bool mbHasNonDate
; /// True = has non-date item(s), maybe date items.
156 bool mbHasDate
; /// True = has date item(s), maybe other types.
157 bool mbHasString
; /// True = has (string|bool|error) item(s), maybe other types.
158 bool mbHasBlank
; /// True = has blank item(s), maybe other types.
159 bool mbHasMixed
; /// True = has [(string|bool|error) and (number|date)] or (number and date).
160 bool mbIsNumeric
; /// True = has numeric item(s), maybe other types except date.
161 bool mbIsInteger
; /// True = has numeric item(s) with only integers, maybe other types except date.
162 bool mbHasLongText
; /// True = contains strings with >255 characters.
164 explicit PCSharedItemsModel();
167 struct PCFieldGroupModel
169 css::util::DateTime maStartDate
; /// Manual or calculated start date for range grouping.
170 css::util::DateTime maEndDate
; /// Manual or calculated end date for range grouping.
171 double mfStartValue
; /// Manual or calculated start value for range grouping.
172 double mfEndValue
; /// Manual or calculated end value for range grouping.
173 double mfInterval
; /// Interval for numeric range grouping.
174 sal_Int32 mnParentField
; /// Index of cache field that contains item groups based on this field.
175 sal_Int32 mnBaseField
; /// Index of cache field this grouped field is based on.
176 sal_Int32 mnGroupBy
; /// Type of numeric or date range grouping.
177 bool mbRangeGroup
; /// True = items are grouped by numeric ranges or date ranges.
178 bool mbDateGroup
; /// True = items are grouped by date ranges or by item names.
179 bool mbAutoStart
; /// True = start value for range groups is calculated from source data.
180 bool mbAutoEnd
; /// True = end value for range groups is calculated from source data.
181 OUString msFinalGroupName
; /// Finalized group name of this field used in internal pivot table collection.
184 explicit PCFieldGroupModel();
186 /** Sets the group-by value for BIFF import. */
187 void setBiffGroupBy( sal_uInt8 nGroupBy
);
190 /** Helper struct for mapping original item names from/to group item names. */
191 struct PivotCacheGroupItem
193 OUString
const maOrigName
;
194 OUString maGroupName
;
196 explicit PivotCacheGroupItem( const OUString
& rItemName
) :
197 maOrigName( rItemName
), maGroupName( rItemName
) {}
200 typedef ::std::vector
< PivotCacheGroupItem
> PivotCacheGroupItemVector
;
202 class PivotCacheField
: public WorkbookHelper
205 explicit PivotCacheField( const WorkbookHelper
& rHelper
, bool bIsDatabaseField
);
207 /** Imports pivot cache field settings from the cacheField element. */
208 void importCacheField( const AttributeList
& rAttribs
);
209 /** Imports shared items settings from the sharedItems element. */
210 void importSharedItems( const AttributeList
& rAttribs
);
211 /** Imports a shared item from the passed element. */
212 void importSharedItem( sal_Int32 nElement
, const AttributeList
& rAttribs
);
213 /** Imports grouping settings from the fieldGroup element. */
214 void importFieldGroup( const AttributeList
& rAttribs
);
215 /** Imports numeric grouping settings from the rangePr element. */
216 void importRangePr( const AttributeList
& rAttribs
);
217 /** Imports an item of the mapping between group items and base items from the passed element. */
218 void importDiscretePrItem( sal_Int32 nElement
, const AttributeList
& rAttribs
);
219 /** Imports a group item from the passed element. */
220 void importGroupItem( sal_Int32 nElement
, const AttributeList
& rAttribs
);
222 /** Imports pivot cache field settings from the PCDFIELD record. */
223 void importPCDField( SequenceInputStream
& rStrm
);
224 /** Imports shared items settings from the PCDFSHAREDITEMS record. */
225 void importPCDFSharedItems( SequenceInputStream
& rStrm
);
226 /** Imports one or more shared items from the passed record. */
227 void importPCDFSharedItem( sal_Int32 nRecId
, SequenceInputStream
& rStrm
);
228 /** Imports grouping settings from the PCDFIELDGROUP record. */
229 void importPCDFieldGroup( SequenceInputStream
& rStrm
);
230 /** Imports numeric grouping settings from the PCDFRANGEPR record. */
231 void importPCDFRangePr( SequenceInputStream
& rStrm
);
232 /** Imports an item of the mapping between group items and base items from the passed record. */
233 void importPCDFDiscretePrItem( sal_Int32 nRecId
, SequenceInputStream
& rStrm
);
234 /** Imports one or more group items from the passed record. */
235 void importPCDFGroupItem( sal_Int32 nRecId
, SequenceInputStream
& rStrm
);
237 /** Apply user Captions to imported group data */
238 void applyItemCaptions( const IdCaptionPairList
& vCaptions
);
240 /** Returns true, if the field is based on source data, or false if it is grouped or calculated. */
241 bool isDatabaseField() const { return maFieldModel
.mbDatabaseField
; }
243 /** Returns true, if the field contains a list of shared items. */
244 bool hasSharedItems() const { return !maSharedItems
.empty(); }
245 /** Returns true, if the field contains a list of grouping items. */
246 bool hasGroupItems() const { return !maGroupItems
.empty(); }
247 /** Returns true, if the field has inplace numeric grouping settings. */
248 bool hasNumericGrouping() const { return maFieldGroupModel
.mbRangeGroup
&& !maFieldGroupModel
.mbDateGroup
; }
249 /** Returns true, if the field has inplace date grouping settings. */
250 bool hasDateGrouping() const { return maFieldGroupModel
.mbRangeGroup
&& maFieldGroupModel
.mbDateGroup
; }
251 /** Returns true, if the field has a parent group field that groups the items of this field. */
252 bool hasParentGrouping() const { return maFieldGroupModel
.mnParentField
>= 0; }
254 /** Returns the name of the cache field. */
255 const OUString
& getName() const { return maFieldModel
.maName
; }
256 /** Returns the index of the parent group field that groups the items of this field. */
257 sal_Int32
getParentGroupField() const { return maFieldGroupModel
.mnParentField
; }
258 /** Returns the index of the base field grouping is based on. */
259 sal_Int32
getGroupBaseField() const { return maFieldGroupModel
.mnBaseField
; }
260 /** Returns the finalized group name of this field. */
261 const OUString
& getFinalGroupName() const { return maFieldGroupModel
.msFinalGroupName
; }
262 /** Set the finalized group name of this field. */
263 void setFinalGroupName(const OUString
& rFinalGroupName
) { maFieldGroupModel
.msFinalGroupName
= rFinalGroupName
; }
265 /** Returns the shared or group item with the specified index. */
266 const PivotCacheItem
* getCacheItem( sal_Int32 nItemIdx
) const;
267 /** Returns the names of all shared or group items. */
268 void getCacheItemNames( ::std::vector
< OUString
>& orItemNames
) const;
269 /** Returns shared or group items. */
270 const PivotCacheItemList
& getCacheItems() const;
272 /** Creates inplace numeric grouping settings. */
273 void convertNumericGrouping(
274 const css::uno::Reference
< css::sheet::XDataPilotField
>& rxDPField
) const;
275 /** Creates inplace date grouping settings or a new date group field. */
276 OUString
createDateGroupField(
277 const css::uno::Reference
< css::sheet::XDataPilotField
>& rxBaseDPField
) const;
278 /** Creates a new grouped DataPilot field and returns its name. */
279 OUString
createParentGroupField(
280 const css::uno::Reference
< css::sheet::XDataPilotField
>& rxBaseDPField
,
281 const PivotCacheField
& rBaseCacheField
,
282 PivotCacheGroupItemVector
& orItemNames
) const;
284 /** Writes the title of the field into the passed sheet at the passed address. */
285 void writeSourceHeaderCell( const WorksheetHelper
& rSheetHelper
,
286 sal_Int32 nCol
, sal_Int32 nRow
) const;
287 /** Writes a source field item value into the passed sheet. */
288 void writeSourceDataCell( const WorksheetHelper
& rSheetHelper
,
289 sal_Int32 nCol
, sal_Int32 nRow
,
290 const PivotCacheItem
& rItem
) const;
292 /** Reads an item from the PCRECORD record and writes it to the passed sheet. */
293 void importPCRecordItem( SequenceInputStream
& rStrm
,
294 const WorksheetHelper
& rSheetHelper
, sal_Int32 nCol
, sal_Int32 nRow
) const;
297 /** Tries to write the passed value to the passed sheet position. */
298 static void writeItemToSourceDataCell( const WorksheetHelper
& rSheetHelper
,
299 sal_Int32 nCol
, sal_Int32 nRow
, const PivotCacheItem
& rItem
);
300 /** Tries to write the value of a shared item to the passed sheet position. */
301 void writeSharedItemToSourceDataCell( const WorksheetHelper
& rSheetHelper
,
302 sal_Int32 nCol
, sal_Int32 nRow
, sal_Int32 nItemIdx
) const;
305 typedef ::std::vector
< sal_Int32
> IndexVector
;
307 PivotCacheItemList maSharedItems
; /// All shared items of this field.
308 PivotCacheItemList maGroupItems
; /// All group items of this field.
309 IndexVector maDiscreteItems
; /// Mapping between group and base items.
310 PCFieldModel maFieldModel
; /// Settings for this cache field.
311 PCSharedItemsModel maSharedItemsModel
; /// Settings for shared items.
312 PCFieldGroupModel maFieldGroupModel
; /// Settings for item grouping.
315 struct PCDefinitionModel
317 OUString maRelId
; /// Relation identifier for cache records fragment.
318 OUString maRefreshedBy
; /// Name of user who last refreshed the cache.
319 double mfRefreshedDate
; /// Date/time of last refresh.
320 sal_Int32 mnRecords
; /// Number of data records in the cache.
321 sal_Int32 mnMissItemsLimit
; /// Limit for discarding unused items.
322 bool mbInvalid
; /// True = cache needs refresh.
323 bool mbSaveData
; /// True = cached item values are present.
324 bool mbRefreshOnLoad
; /// True = try to refresh cache on load.
325 bool mbOptimizeMemory
; /// True = application may optimize memory usage.
326 bool mbEnableRefresh
; /// True = refreshing cache is enabled in UI.
327 bool mbBackgroundQuery
; /// True = application queries data asynchronously.
328 bool mbUpgradeOnRefresh
; /// True = application may upgrade cache version.
329 bool mbTupleCache
; /// True = cache stores OLAP functions.
330 bool mbSupportSubquery
; /// True = data source supports subqueries.
331 bool mbSupportDrill
; /// True = data source supports drilldown.
333 explicit PCDefinitionModel();
338 sal_Int32 mnSourceType
; /// Type of the source data (sheet, consolidation, scenario, external).
339 sal_Int32 mnConnectionId
; /// Connection identifier for external data source.
341 explicit PCSourceModel();
344 struct PCWorksheetSourceModel
346 OUString maRelId
; /// Relation identifier for an external document URL.
347 OUString maSheet
; /// Sheet name for cell range or sheet-local defined names.
348 OUString maDefName
; /// Defined name containing a cell range if present.
349 ScRange maRange
; /// Source cell range of the data.
351 explicit PCWorksheetSourceModel();
354 class PivotCache
: public WorkbookHelper
357 explicit PivotCache( const WorkbookHelper
& rHelper
);
359 /** Reads pivot cache global settings from the pivotCacheDefinition element. */
360 void importPivotCacheDefinition( const AttributeList
& rAttribs
);
361 /** Reads cache source settings from the cacheSource element. */
362 void importCacheSource( const AttributeList
& rAttribs
);
363 /** Reads sheet source settings from the worksheetSource element. */
364 void importWorksheetSource( const AttributeList
& rAttribs
, const ::oox::core::Relations
& rRelations
);
366 /** Reads pivot cache global settings from the PCDEFINITION record. */
367 void importPCDefinition( SequenceInputStream
& rStrm
);
368 /** Reads cache source settings from the PCDSOURCE record. */
369 void importPCDSource( SequenceInputStream
& rStrm
);
370 /** Reads sheet source settings from the PCDSHEETSOURCE record. */
371 void importPCDSheetSource( SequenceInputStream
& rStrm
, const ::oox::core::Relations
& rRelations
);
373 /** Creates and returns a new pivot cache field. */
374 PivotCacheField
& createCacheField();
375 /** Checks validity of source data and creates a dummy data sheet for external sheet sources. */
376 void finalizeImport();
378 /** Returns true, if the pivot cache is based on a valid data source, so
379 that pivot tables can be created based on this pivot cache. */
380 bool isValidDataSource() const { return mbValidSource
; }
381 /** Returns true, if the pivot cache is based on a dummy sheet created in finalizeImport. */
382 bool isBasedOnDummySheet() const { return mbDummySheet
; }
383 /** Returns the internal cell range the cache is based on. */
385 getSourceRange() const { return maSheetSrcModel
.maRange
; }
386 /** Returns the relation identifier of the pivot cache records fragment. */
387 const OUString
& getRecordsRelId() const { return maDefModel
.maRelId
; }
389 /** Returns the cache field with the specified index. */
390 PivotCacheField
* getCacheField( sal_Int32 nFieldIdx
);
391 const PivotCacheField
* getCacheField( sal_Int32 nFieldIdx
) const;
392 /** Returns the source column index of the field with the passed index. */
393 sal_Int32
getCacheDatabaseIndex( sal_Int32 nFieldIdx
) const;
395 /** Writes the titles of all source fields into the passed sheet. */
396 void writeSourceHeaderCells( const WorksheetHelper
& rSheetHelper
) const;
397 /** Writes a source field item value into the passed sheet. */
398 void writeSourceDataCell( const WorksheetHelper
& rSheetHelper
,
399 sal_Int32 nColIdx
, sal_Int32 nRowIdx
,
400 const PivotCacheItem
& rItem
) const;
402 /** Reads a PCRECORD record and writes all item values to the passed sheet. */
403 void importPCRecord( SequenceInputStream
& rStrm
,
404 const WorksheetHelper
& rSheetHelper
, sal_Int32 nRowIdx
) const;
408 /** Finalizes the pivot cache if it is based on internal sheet data. */
409 void finalizeInternalSheetSource();
410 /** Finalizes the pivot cache if it is based on sheet data of an external spreadsheet document. */
411 void finalizeExternalSheetSource();
412 /** Creates a dummy sheet that will be filled with the pivot cache data. */
413 void prepareSourceDataSheet();
414 /** Checks, if the row index has changed since last call, and initializes the sheet data buffer. */
415 void updateSourceDataRow( const WorksheetHelper
& rSheetHelper
, sal_Int32 nRow
) const;
418 typedef RefVector
< PivotCacheField
> PivotCacheFieldVector
;
419 typedef ::std::vector
< sal_Int32
> IndexVector
;
421 PivotCacheFieldVector maFields
; /// All pivot cache fields.
422 PivotCacheFieldVector maDatabaseFields
; /// All cache fields that are based on source data.
423 IndexVector maDatabaseIndexes
; /// Database field index for all fields.
424 PCDefinitionModel maDefModel
; /// Global pivot cache settings.
425 PCSourceModel maSourceModel
; /// Pivot cache source settings.
426 PCWorksheetSourceModel maSheetSrcModel
; /// Sheet source data if cache type is sheet.
427 ValueRangeSet maColSpans
; /// Column spans used by SheetDataBuffer for optimized cell import.
428 OUString maTargetUrl
; /// URL of an external source document.
429 mutable sal_Int32 mnCurrRow
; /// Current row index in dummy sheet.
430 bool mbValidSource
; /// True = pivot cache is based on supported data source.
431 bool mbDummySheet
; /// True = pivot cache is based on a dummy sheet.
434 class PivotCacheBuffer
: public WorkbookHelper
437 explicit PivotCacheBuffer( const WorkbookHelper
& rHelper
);
439 /** Registers a pivot cache definition fragment. The fragment will be loaded on demand (OOXML/BIFF12 only). */
440 void registerPivotCacheFragment( sal_Int32 nCacheId
, const OUString
& rFragmentPath
);
442 /** Imports and stores a pivot cache definition fragment on first call,
443 returns the imported cache on subsequent calls with the same identifier. */
444 PivotCache
* importPivotCacheFragment( sal_Int32 nCacheId
);
447 /** Creates and returns a new pivot cache object with the passed identifier. */
448 PivotCache
& createPivotCache( sal_Int32 nCacheId
);
451 typedef ::std::map
< sal_Int32
, OUString
> FragmentPathMap
;
452 typedef RefMap
< sal_Int32
, PivotCache
> PivotCacheMap
;
453 typedef ::std::vector
< sal_Int32
> PivotCacheIdVector
;
455 FragmentPathMap maFragmentPaths
;
456 PivotCacheMap maCaches
;
457 PivotCacheIdVector maCacheIds
;
465 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */