LibreOffice Module sc (master) 1
dptabdat.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <dptabdat.hxx>
21#include <dpcache.hxx>
22#include <dpfilteredcache.hxx>
23#include <dptabres.hxx>
24
25#include <osl/diagnose.h>
26#include <tools/date.hxx>
27
28
29using namespace ::com::sun::star;
30using ::std::vector;
31
33 pInitState( nullptr ),
34 pColRoot( nullptr ),
35 pRowRoot( nullptr )
36{
37}
38
40 mpDoc(pDoc)
41{
42 nLastDateVal = nLastHier = nLastLevel = nLastRet = -1; // invalid
43
44 //TODO: reset before new calculation (in case the base date is changed)
45}
46
48{
49}
50
51OUString ScDPTableData::GetFormattedString(sal_Int32 nDim, const ScDPItemData& rItem, bool bLocaleIndependent) const
52{
53 const ScDPCache& rCache = GetCacheTable().getCache();
54 return rCache.GetFormattedString(nDim, rItem, bLocaleIndependent);
55}
56
58{
59 if ( nDateVal == nLastDateVal && nHierarchy == nLastHier && nLevel == nLastLevel )
60 return nLastRet;
61
62 Date aDate( 30,12,1899 ); //TODO: get from source data (and cache here)
63 aDate.AddDays( nDateVal);
64
65 tools::Long nRet = 0;
66 switch (nHierarchy)
67 {
69 switch (nLevel)
70 {
71 case 0: nRet = aDate.GetYear(); break;
72 case 1: nRet = (aDate.GetMonth()-1) / 3 + 1; break;
73 case 2: nRet = aDate.GetMonth(); break;
74 case 3: nRet = aDate.GetDay(); break;
75 default:
76 OSL_FAIL("GetDatePart: wrong level");
77 }
78 break;
80 switch (nLevel)
81 {
82 //TODO: use settings for different definitions
83 case 0: nRet = aDate.GetYear(); break;
84 case 1: nRet = aDate.GetWeekOfYear(); break;
85 case 2: nRet = static_cast<tools::Long>(aDate.GetDayOfWeek()); break;
86 default:
87 OSL_FAIL("GetDatePart: wrong level");
88 }
89 break;
90 default:
91 OSL_FAIL("GetDatePart: wrong hierarchy");
92 }
93
94 nLastDateVal = nDateVal;
95 nLastHier = nHierarchy;
96 nLastLevel = nLevel;
97 nLastRet = nRet;
98
99 return nRet;
100}
101
103{
104 return false;
105}
106
108{
109 return 0; // default format
110}
111
112bool ScDPTableData::IsBaseForGroup(sal_Int32) const
113{
114 return false; // always false
115}
116
117sal_Int32 ScDPTableData::GetGroupBase(sal_Int32) const
118{
119 return -1; // always none
120}
121
123{
124 return false; // always false
125}
126
128 const ScDPItemData&, sal_Int32 ) const
129{
130 OSL_FAIL("IsInGroup shouldn't be called for non-group data");
131 return false;
132}
133
135 const ScDPItemData&, sal_Int32 ) const
136{
137 OSL_FAIL("HasCommonElement shouldn't be called for non-group data");
138 return false;
139}
140void ScDPTableData::FillRowDataFromCacheTable(sal_Int32 nRow, const ScDPFilteredCache& rCacheTable,
141 const CalcInfo& rInfo, CalcRowData& rData)
142{
143 // column dimensions
144 GetItemData(rCacheTable, nRow, rInfo.aColLevelDims, rData.aColData);
145
146 // row dimensions
147 GetItemData(rCacheTable, nRow, rInfo.aRowLevelDims, rData.aRowData);
148
149 // page dimensions
150 GetItemData(rCacheTable, nRow, rInfo.aPageDims, rData.aPageData);
151
152 tools::Long nCacheColumnCount = rCacheTable.getCache().GetColumnCount();
153 sal_Int32 n = rInfo.aDataSrcCols.size();
154 for (sal_Int32 i = 0; i < n; ++i)
155 {
156 tools::Long nDim = rInfo.aDataSrcCols[i];
157 rData.aValues.emplace_back( );
158 // #i111435# GetItemData needs dimension indexes including groups,
159 // so the index must be checked here (groups aren't useful as data fields).
160 if ( nDim < nCacheColumnCount )
161 {
162 ScDPValue& rVal = rData.aValues.back();
163 rCacheTable.getValue( rVal, static_cast<SCCOL>(nDim), static_cast<SCROW>(nRow));
164 }
165 }
166}
167
168void ScDPTableData::ProcessRowData(CalcInfo& rInfo, const CalcRowData& rData, bool bAutoShow)
169{
170 if (!bAutoShow)
171 {
172 LateInitParams aColParams(rInfo.aColDims, rInfo.aColLevels, false);
173 LateInitParams aRowParams(rInfo.aRowDims, rInfo.aRowLevels, true);
174 // root always init child
175 aColParams.SetInitChild(true);
176 aColParams.SetInitAllChildren( false);
177 aRowParams.SetInitChild(true);
178 aRowParams.SetInitAllChildren( false);
179
180 rInfo.pColRoot->LateInitFrom(aColParams, rData.aColData, 0, *rInfo.pInitState);
181 rInfo.pRowRoot->LateInitFrom(aRowParams, rData.aRowData, 0, *rInfo.pInitState);
182 }
183
184 if ( ( !rInfo.pColRoot->GetChildDimension() || rInfo.pColRoot->GetChildDimension()->IsValidEntry(rData.aColData) ) &&
186 {
187 //TODO: single process method with ColMembers, RowMembers and data !!!
188 if (rInfo.pColRoot->GetChildDimension())
189 {
190 vector<SCROW> aEmptyData;
191 rInfo.pColRoot->GetChildDimension()->ProcessData(rData.aColData, nullptr, aEmptyData, rData.aValues);
192 }
193
195 rData.aColData, rData.aValues);
196 }
197}
198
199void ScDPTableData::CalcResultsFromCacheTable(const ScDPFilteredCache& rCacheTable, CalcInfo& rInfo, bool bAutoShow)
200{
201 sal_Int32 nRowSize = rCacheTable.getRowSize();
202 for (sal_Int32 nRow = 0; nRow < nRowSize; ++nRow)
203 {
204 sal_Int32 nLastRow;
205 if (!rCacheTable.isRowActive(nRow, &nLastRow))
206 {
207 nRow = nLastRow;
208 continue;
209 }
210
212 FillRowDataFromCacheTable(nRow, rCacheTable, rInfo, aData);
213 ProcessRowData(rInfo, aData, bAutoShow);
214 }
215}
216
217void ScDPTableData::GetItemData(const ScDPFilteredCache& rCacheTable, sal_Int32 nRow,
218 const vector<sal_Int32>& rDims, vector<SCROW>& rItemData)
219{
220 sal_Int32 nDimSize = rDims.size();
221 rItemData.reserve(rItemData.size() + nDimSize);
222 for (sal_Int32 i = 0; i < nDimSize; ++i)
223 {
224 sal_Int32 nDim = rDims[i];
225
226 if (getIsDataLayoutDimension(nDim))
227 {
228 rItemData.push_back( -1 );
229 continue;
230 }
231
232 nDim = GetSourceDim( nDim );
233 if ( nDim >= rCacheTable.getCache().GetColumnCount() )
234 continue;
235
236 SCROW nId= rCacheTable.getCache().GetItemDataId( static_cast<SCCOL>(nDim), static_cast<SCROW>(nRow), IsRepeatIfEmpty());
237 rItemData.push_back( nId );
238 }
239}
240
241sal_Int32 ScDPTableData::GetMembersCount( sal_Int32 nDim )
242{
243 if ( nDim > MAXCOL )
244 return 0;
245 return GetCacheTable().getFieldEntries( nDim ).size();
246}
247
248const ScDPItemData* ScDPTableData::GetMemberByIndex( sal_Int32 nDim, sal_Int32 nIndex )
249{
250 if ( nIndex >= GetMembersCount( nDim ) )
251 return nullptr;
252
253 const ::std::vector<SCROW>& nMembers = GetCacheTable().getFieldEntries( nDim );
254
255 return GetCacheTable().getCache().GetItemDataById( static_cast<SCCOL>(nDim), static_cast<SCROW>(nMembers[nIndex]) );
256}
257
258const ScDPItemData* ScDPTableData::GetMemberById( sal_Int32 nDim, sal_Int32 nId)
259{
260 return GetCacheTable().getCache().GetItemDataById(nDim, static_cast<SCROW>(nId));
261}
262
263const std::vector< SCROW >& ScDPTableData::GetColumnEntries( sal_Int32 nColumn )
264{
265 return GetCacheTable().getFieldEntries( nColumn );
266}
267
268sal_Int32 ScDPTableData::GetSourceDim( sal_Int32 nDim )
269{
270 return nDim;
271}
272
273sal_Int32 ScDPTableData::Compare( sal_Int32 nDim, sal_Int32 nDataId1, sal_Int32 nDataId2)
274{
275 if ( getIsDataLayoutDimension(nDim) )
276 return 0;
277
278 if ( nDataId1 > nDataId2 )
279 return 1;
280 else if ( nDataId1 == nDataId2 )
281 return 0;
282 else
283 return -1;
284}
285
286#if DUMP_PIVOT_TABLE
287void ScDPTableData::Dump() const
288{
289}
290#endif
291
292/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SCCOL MAXCOL
Definition: address.hxx:69
sal_uInt16 GetWeekOfYear(DayOfWeek eStartDay=MONDAY, sal_Int16 nMinimumNumberOfDaysInWeek=4) const
void AddDays(sal_Int32 nAddDays)
sal_Int16 GetYear() const
sal_uInt16 GetDay() const
DayOfWeek GetDayOfWeek() const
sal_uInt16 GetMonth() const
void SetInitChild(bool b)
Definition: dptabres.hxx:256
void SetInitAllChildren(bool b)
Definition: dptabres.hxx:257
This class represents the cached data part of the datapilot cache table implementation.
Definition: dpcache.hxx:48
const ScDPItemData * GetItemDataById(tools::Long nDim, SCROW nId) const
Definition: dpcache.cxx:983
OUString GetFormattedString(tools::Long nDim, const ScDPItemData &rItem, bool bLocaleIndependent) const
Definition: dpcache.cxx:1205
SCROW GetItemDataId(sal_uInt16 nDim, SCROW nRow, bool bRepeatIfEmpty) const
Definition: dpcache.cxx:958
tools::Long GetColumnCount() const
Definition: dpcache.cxx:888
This class is only a wrapper to the actual cache, to provide filtering on the raw data based on the q...
sal_Int32 getRowSize() const
const ::std::vector< SCROW > & getFieldEntries(sal_Int32 nColumn) const
Get the unique entries for a field specified by index.
void getValue(ScDPValue &rVal, SCCOL nCol, SCROW nRow) const
bool isRowActive(sal_Int32 nRow, sal_Int32 *pLastRow=nullptr) const
Check whether a specified row is active or not.
const ScDPCache & getCache() const
When assigning a string value, you can also assign an interned string whose life-cycle is managed by ...
Definition: dpitemdata.hxx:29
bool IsValidEntry(const ::std::vector< SCROW > &aMembers) const
Definition: dptabres.cxx:3007
void ProcessData(const ::std::vector< SCROW > &aMembers, const ScDPResultDimension *pDataDim, const ::std::vector< SCROW > &aDataMembers, const ::std::vector< ScDPValue > &aValues) const
Definition: dptabres.cxx:3021
void ProcessData(const ::std::vector< SCROW > &aChildMembers, const ScDPResultDimension *pDataDim, const ::std::vector< SCROW > &aDataMembers, const ::std::vector< ScDPValue > &aValues)
Definition: dptabres.cxx:1257
void LateInitFrom(LateInitParams &rParams, const ::std::vector< SCROW > &pItemData, size_t nPos, ScDPInitState &rInitState)
Definition: dptabres.cxx:1083
const ScDPResultDimension * GetChildDimension() const
this will be removed!
Definition: dptabres.hxx:409
tools::Long nLastLevel
Definition: dptabdat.hxx:61
virtual sal_Int32 GetGroupBase(sal_Int32 nGroupDim) const
Definition: dptabdat.cxx:117
void CalcResultsFromCacheTable(const ScDPFilteredCache &rCacheTable, CalcInfo &rInfo, bool bAutoShow)
Definition: dptabdat.cxx:199
virtual sal_Int32 GetMembersCount(sal_Int32 nDim)
Definition: dptabdat.cxx:241
virtual bool IsNumOrDateGroup(sal_Int32 nDim) const
Definition: dptabdat.cxx:122
static void ProcessRowData(CalcInfo &rInfo, const CalcRowData &rData, bool bAutoShow)
Definition: dptabdat.cxx:168
virtual bool HasCommonElement(const ScDPItemData &rFirstData, sal_Int32 nFirstIndex, const ScDPItemData &rSecondData, sal_Int32 nSecondIndex) const
Definition: dptabdat.cxx:134
OUString GetFormattedString(sal_Int32 nDim, const ScDPItemData &rItem, bool bLocaleIndependent) const
Definition: dptabdat.cxx:51
ScDPTableData(const ScDPTableData &)=delete
virtual const ScDPItemData * GetMemberById(sal_Int32 nDim, sal_Int32 nId)
Definition: dptabdat.cxx:258
virtual sal_Int32 Compare(sal_Int32 nDim, sal_Int32 nDataId1, sal_Int32 nDataId2)
Definition: dptabdat.cxx:273
const ScDocument * mpDoc
Definition: dptabdat.hxx:63
virtual void Dump() const
void FillRowDataFromCacheTable(sal_Int32 nRow, const ScDPFilteredCache &rCacheTable, const CalcInfo &rInfo, CalcRowData &rData)
Definition: dptabdat.cxx:140
virtual bool getIsDataLayoutDimension(sal_Int32 nColumn)=0
tools::Long GetDatePart(tools::Long nDateVal, tools::Long nHierarchy, tools::Long nLevel)
Definition: dptabdat.cxx:57
const ScDPItemData * GetMemberByIndex(sal_Int32 nDim, sal_Int32 nIndex)
Definition: dptabdat.cxx:248
virtual bool IsInGroup(const ScDPItemData &rGroupData, sal_Int32 nGroupIndex, const ScDPItemData &rBaseData, sal_Int32 nBaseIndex) const
Definition: dptabdat.cxx:127
virtual ~ScDPTableData()
Definition: dptabdat.cxx:47
tools::Long nLastDateVal
Definition: dptabdat.hxx:59
virtual const ScDPFilteredCache & GetCacheTable() const =0
tools::Long nLastHier
Definition: dptabdat.hxx:60
virtual bool IsBaseForGroup(sal_Int32 nDim) const
Definition: dptabdat.cxx:112
virtual bool IsRepeatIfEmpty()
Definition: dptabdat.cxx:102
virtual const std::vector< SCROW > & GetColumnEntries(sal_Int32 nColumn)
Definition: dptabdat.cxx:263
void GetItemData(const ScDPFilteredCache &rCacheTable, sal_Int32 nRow, const ::std::vector< sal_Int32 > &rDims, ::std::vector< SCROW > &rItemData)
Definition: dptabdat.cxx:217
tools::Long nLastRet
Definition: dptabdat.hxx:62
virtual sal_Int32 GetSourceDim(sal_Int32 nDim)
Definition: dptabdat.cxx:268
virtual sal_uInt32 GetNumberFormat(sal_Int32 nDim)
Definition: dptabdat.cxx:107
#define SC_DAPI_HIERARCHY_QUARTER
Definition: dptabdat.hxx:32
#define SC_DAPI_HIERARCHY_WEEK
Definition: dptabdat.hxx:33
sal_Int32 nIndex
sal_Int64 n
constexpr OUStringLiteral aData
int i
long Long
sal_Int16 nId
This structure stores dimension information used when calculating results.
Definition: dptabdat.hxx:70
::std::vector< sal_Int32 > aPageDims
Definition: dptabdat.hxx:77
::std::vector< ScDPLevel * > aColLevels
Definition: dptabdat.hxx:73
::std::vector< sal_Int32 > aRowLevelDims
Definition: dptabdat.hxx:74
::std::vector< sal_Int32 > aDataSrcCols
Definition: dptabdat.hxx:78
::std::vector< ScDPDimension * > aColDims
Definition: dptabdat.hxx:72
ScDPResultMember * pRowRoot
Definition: dptabdat.hxx:82
::std::vector< sal_Int32 > aColLevelDims
Definition: dptabdat.hxx:71
ScDPResultMember * pColRoot
Definition: dptabdat.hxx:81
::std::vector< ScDPDimension * > aRowDims
Definition: dptabdat.hxx:75
::std::vector< ScDPLevel * > aRowLevels
Definition: dptabdat.hxx:76
ScDPInitState * pInitState
Definition: dptabdat.hxx:80
This structure stores vector arrays that hold intermediate data for each row during cache table itera...
Definition: dptabdat.hxx:143
::std::vector< SCROW > aPageData
Definition: dptabdat.hxx:146
::std::vector< ScDPValue > aValues
Definition: dptabdat.hxx:147
::std::vector< SCROW > aRowData
Definition: dptabdat.hxx:145
::std::vector< SCROW > aColData
Definition: dptabdat.hxx:144
sal_Int16 SCCOL
Definition: types.hxx:21
sal_Int32 SCROW
Definition: types.hxx:17