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 
29 using namespace ::com::sun::star;
30 using ::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 
51 OUString ScDPTableData::GetFormattedString(tools::Long 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 
113 {
114  return false; // always false
115 }
116 
118 {
119  return -1; // always none
120 }
121 
123 {
124  return false; // always false
125 }
126 
128  const ScDPItemData&, tools::Long ) const
129 {
130  OSL_FAIL("IsInGroup shouldn't be called for non-group data");
131  return false;
132 }
133 
135  const ScDPItemData&, tools::Long ) const
136 {
137  OSL_FAIL("HasCommonElement shouldn't be called for non-group data");
138  return false;
139 }
140 void 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 
168 void 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) ) &&
185  ( !rInfo.pRowRoot->GetChildDimension() || rInfo.pRowRoot->GetChildDimension()->IsValidEntry(rData.aRowData) ) )
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 
194  rInfo.pRowRoot->ProcessData(rData.aRowData, rInfo.pColRoot->GetChildDimension(),
195  rData.aColData, rData.aValues);
196  }
197 }
198 
199 void 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 
217 void ScDPTableData::GetItemData(const ScDPFilteredCache& rCacheTable, sal_Int32 nRow,
218  const vector<long>& 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  tools::Long 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 
242 {
243  if ( nDim > MAXCOL )
244  return 0;
245  return GetCacheTable().getFieldEntries( nDim ).size();
246 }
247 
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 
259 {
260  return GetCacheTable().getCache().GetItemDataById(nDim, static_cast<SCROW>(nId));
261 }
262 
263 const std::vector< SCROW >& ScDPTableData::GetColumnEntries( tools::Long nColumn )
264 {
265  return GetCacheTable().getFieldEntries( nColumn );
266 }
267 
269 {
270  return nDim;
271 
272 }
273 
275 {
276  if ( getIsDataLayoutDimension(nDim) )
277  return 0;
278 
279  if ( nDataId1 > nDataId2 )
280  return 1;
281  else if ( nDataId1 == nDataId2 )
282  return 0;
283  else
284  return -1;
285 }
286 
287 #if DUMP_PIVOT_TABLE
288 void ScDPTableData::Dump() const
289 {
290 }
291 #endif
292 
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetInitAllChildren(bool b)
Definition: dptabres.hxx:259
const ScDPItemData * GetMemberByIndex(tools::Long nDim, tools::Long nIndex)
Definition: dptabdat.cxx:248
virtual bool IsRepeatIfEmpty()
Definition: dptabdat.cxx:102
OUString GetFormattedString(tools::Long nDim, const ScDPItemData &rItem, bool bLocaleIndependent) const
Definition: dpcache.cxx:1210
DayOfWeek GetDayOfWeek() const
virtual sal_uInt32 GetNumberFormat(tools::Long nDim)
Definition: dptabdat.cxx:107
::std::vector< long > aColLevelDims
Definition: dptabdat.hxx:72
const char aData[]
sal_Int32 getRowSize() const
virtual tools::Long GetMembersCount(tools::Long nDim)
Definition: dptabdat.cxx:241
void SetInitChild(bool b)
Definition: dptabres.hxx:258
virtual bool getIsDataLayoutDimension(tools::Long nColumn)=0
ScDPTableData(const ScDPTableData &)=delete
long Long
sal_Int64 n
::std::vector< ScDPValue > aValues
Definition: dptabdat.hxx:148
sal_Int16 nId
virtual tools::Long GetSourceDim(tools::Long nDim)
Definition: dptabdat.cxx:268
void ProcessData(const ::std::vector< SCROW > &aChildMembers, const ScDPResultDimension *pDataDim, const ::std::vector< SCROW > &aDataMembers, const ::std::vector< ScDPValue > &aValues)
Definition: dptabres.cxx:1256
tools::Long nLastRet
Definition: dptabdat.hxx:63
sal_uInt16 GetWeekOfYear(DayOfWeek eStartDay=MONDAY, sal_Int16 nMinimumNumberOfDaysInWeek=4) const
This class represents the cached data part of the datapilot cache table implementation.
Definition: dpcache.hxx:48
::std::vector< ScDPDimension * > aColDims
Definition: dptabdat.hxx:73
SCROW GetItemDataId(sal_uInt16 nDim, SCROW nRow, bool bRepeatIfEmpty) const
Definition: dpcache.cxx:960
tools::Long nLastLevel
Definition: dptabdat.hxx:62
virtual tools::Long GetGroupBase(tools::Long nGroupDim) const
Definition: dptabdat.cxx:117
sal_uInt16 GetMonth() const
const ScDPItemData * GetItemDataById(tools::Long nDim, SCROW nId) const
Definition: dpcache.cxx:985
void getValue(ScDPValue &rVal, SCCOL nCol, SCROW nRow) const
tools::Long nLastHier
Definition: dptabdat.hxx:61
bool isRowActive(sal_Int32 nRow, sal_Int32 *pLastRow=nullptr) const
Check whether a specified row is active or not.
virtual bool IsInGroup(const ScDPItemData &rGroupData, tools::Long nGroupIndex, const ScDPItemData &rBaseData, tools::Long nBaseIndex) const
Definition: dptabdat.cxx:127
::std::vector< ScDPLevel * > aColLevels
Definition: dptabdat.hxx:74
const ScDocument * mpDoc
Definition: dptabdat.hxx:64
sal_Int16 GetYear() const
::std::vector< long > aRowLevelDims
Definition: dptabdat.hxx:75
void CalcResultsFromCacheTable(const ScDPFilteredCache &rCacheTable, CalcInfo &rInfo, bool bAutoShow)
Definition: dptabdat.cxx:199
::std::vector< ScDPDimension * > aRowDims
Definition: dptabdat.hxx:76
virtual void Dump() const
When assigning a string value, you can also assign an interned string whose life-cycle is managed by ...
Definition: dpitemdata.hxx:29
int i
virtual bool IsNumOrDateGroup(tools::Long nDim) const
Definition: dptabdat.cxx:122
This structure stores dimension information used when calculating results.
Definition: dptabdat.hxx:70
#define SC_DAPI_HIERARCHY_WEEK
Definition: dptabdat.hxx:34
::std::vector< SCROW > aRowData
Definition: dptabdat.hxx:146
const SCCOL MAXCOL
Definition: address.hxx:70
tools::Long nLastDateVal
Definition: dptabdat.hxx:60
virtual const std::vector< SCROW > & GetColumnEntries(tools::Long nColumn)
Definition: dptabdat.cxx:263
tools::Long GetColumnCount() const
Definition: dpcache.cxx:890
static void ProcessRowData(CalcInfo &rInfo, const CalcRowData &rData, bool bAutoShow)
Definition: dptabdat.cxx:168
sal_uInt16 GetDay() const
void AddDays(sal_Int32 nAddDays)
ScDPResultMember * pColRoot
Definition: dptabdat.hxx:82
::std::vector< SCROW > aColData
Definition: dptabdat.hxx:145
void FillRowDataFromCacheTable(sal_Int32 nRow, const ScDPFilteredCache &rCacheTable, const CalcInfo &rInfo, CalcRowData &rData)
Definition: dptabdat.cxx:140
virtual bool HasCommonElement(const ScDPItemData &rFirstData, tools::Long nFirstIndex, const ScDPItemData &rSecondData, tools::Long nSecondIndex) const
Definition: dptabdat.cxx:134
::std::vector< long > aPageDims
Definition: dptabdat.hxx:78
sal_Int32 SCROW
Definition: types.hxx:18
void ProcessData(const ::std::vector< SCROW > &aMembers, const ScDPResultDimension *pDataDim, const ::std::vector< SCROW > &aDataMembers, const ::std::vector< ScDPValue > &aValues) const
Definition: dptabres.cxx:3009
virtual tools::Long Compare(tools::Long nDim, tools::Long nDataId1, tools::Long nDataId2)
Definition: dptabdat.cxx:274
This structure stores vector arrays that hold intermediate data for each row during cache table itera...
Definition: dptabdat.hxx:143
ScDPInitState * pInitState
Definition: dptabdat.hxx:81
::std::vector< SCROW > aPageData
Definition: dptabdat.hxx:147
const ScDPResultDimension * GetChildDimension() const
this will be removed!
Definition: dptabres.hxx:411
virtual bool IsBaseForGroup(tools::Long nDim) const
Definition: dptabdat.cxx:112
virtual const ScDPItemData * GetMemberById(tools::Long nDim, tools::Long nId)
Definition: dptabdat.cxx:258
const ::std::vector< SCROW > & getFieldEntries(sal_Int32 nColumn) const
Get the unique entries for a field specified by index.
::std::vector< ScDPLevel * > aRowLevels
Definition: dptabdat.hxx:77
bool IsValidEntry(const ::std::vector< SCROW > &aMembers) const
Definition: dptabres.cxx:2995
virtual const ScDPFilteredCache & GetCacheTable() const =0
void GetItemData(const ScDPFilteredCache &rCacheTable, sal_Int32 nRow, const ::std::vector< long > &rDims,::std::vector< SCROW > &rItemData)
Definition: dptabdat.cxx:217
virtual ~ScDPTableData()
Definition: dptabdat.cxx:47
::std::vector< long > aDataSrcCols
Definition: dptabdat.hxx:79
ScDPResultMember * pRowRoot
Definition: dptabdat.hxx:83
This class is only a wrapper to the actual cache, to provide filtering on the raw data based on the q...
#define SC_DAPI_HIERARCHY_QUARTER
Definition: dptabdat.hxx:33
void LateInitFrom(LateInitParams &rParams, const ::std::vector< SCROW > &pItemData, size_t nPos, ScDPInitState &rInitState)
Definition: dptabres.cxx:1082
const ScDPCache & getCache() const
OUString GetFormattedString(tools::Long nDim, const ScDPItemData &rItem, bool bLocaleIndependent) const
Definition: dptabdat.cxx:51
tools::Long GetDatePart(tools::Long nDateVal, tools::Long nHierarchy, tools::Long nLevel)
Definition: dptabdat.cxx:57