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