Line data Source code
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 :
22 : #include "global.hxx"
23 : #include "dpfilteredcache.hxx"
24 : #include "dptabres.hxx"
25 : #include "document.hxx"
26 : #include "dpobject.hxx"
27 :
28 : #include <stdio.h>
29 : #include <rtl/math.hxx>
30 : #include <tools/date.hxx>
31 : #include <unotools/transliterationwrapper.hxx>
32 : #include <unotools/collatorwrapper.hxx>
33 :
34 : #include <com/sun/star/sheet/DataPilotFieldFilter.hpp>
35 :
36 : using namespace ::com::sun::star;
37 : using ::com::sun::star::uno::Sequence;
38 : using ::com::sun::star::uno::Any;
39 : using ::std::vector;
40 :
41 : // ---------------------------------------------------------------------------
42 :
43 78 : ScDPTableData::CalcInfo::CalcInfo() :
44 78 : bRepeatIfEmpty(false)
45 : {
46 78 : }
47 :
48 : // ---------------------------------------------------------------------------
49 :
50 83 : ScDPTableData::ScDPTableData(ScDocument* pDoc) :
51 83 : mpDoc(pDoc)
52 : {
53 83 : nLastDateVal = nLastHier = nLastLevel = nLastRet = -1; // invalid
54 :
55 : //! reset before new calculation (in case the base date is changed)
56 83 : }
57 :
58 83 : ScDPTableData::~ScDPTableData()
59 : {
60 83 : }
61 :
62 2130 : OUString ScDPTableData::GetFormattedString(long nDim, const ScDPItemData& rItem) const
63 : {
64 2130 : const ScDPCache* pCache = GetCacheTable().getCache();
65 2130 : return pCache->GetFormattedString(nDim, rItem);
66 : }
67 :
68 0 : long ScDPTableData::GetDatePart( long nDateVal, long nHierarchy, long nLevel )
69 : {
70 0 : if ( nDateVal == nLastDateVal && nHierarchy == nLastHier && nLevel == nLastLevel )
71 0 : return nLastRet;
72 :
73 0 : Date aDate( 30,12,1899 ); //! get from source data (and cache here)
74 0 : aDate += nDateVal;
75 :
76 0 : long nRet = 0;
77 0 : switch (nHierarchy)
78 : {
79 : case SC_DAPI_HIERARCHY_QUARTER:
80 0 : switch (nLevel)
81 : {
82 0 : case 0: nRet = aDate.GetYear(); break;
83 0 : case 1: nRet = (aDate.GetMonth()-1) / 3 + 1; break;
84 0 : case 2: nRet = aDate.GetMonth(); break;
85 0 : case 3: nRet = aDate.GetDay(); break;
86 : default:
87 : OSL_FAIL("GetDatePart: wrong level");
88 : }
89 0 : break;
90 : case SC_DAPI_HIERARCHY_WEEK:
91 0 : switch (nLevel)
92 : {
93 : //! use settings for different definitions
94 0 : case 0: nRet = aDate.GetYear(); break; //!...
95 0 : case 1: nRet = aDate.GetWeekOfYear(); break;
96 0 : case 2: nRet = (long)aDate.GetDayOfWeek(); break;
97 : default:
98 : OSL_FAIL("GetDatePart: wrong level");
99 : }
100 0 : break;
101 : default:
102 : OSL_FAIL("GetDatePart: wrong hierarchy");
103 : }
104 :
105 0 : nLastDateVal = nDateVal;
106 0 : nLastHier = nHierarchy;
107 0 : nLastLevel = nLevel;
108 0 : nLastRet = nRet;
109 :
110 0 : return nRet;
111 : }
112 :
113 0 : bool ScDPTableData::IsRepeatIfEmpty()
114 : {
115 0 : return false;
116 : }
117 :
118 0 : sal_uLong ScDPTableData::GetNumberFormat(long)
119 : {
120 0 : return 0; // default format
121 : }
122 :
123 124 : sal_Bool ScDPTableData::IsBaseForGroup(long) const
124 : {
125 124 : return false; // always false
126 : }
127 :
128 124 : long ScDPTableData::GetGroupBase(long) const
129 : {
130 124 : return -1; // always none
131 : }
132 :
133 307 : sal_Bool ScDPTableData::IsNumOrDateGroup(long) const
134 : {
135 307 : return false; // always false
136 : }
137 :
138 0 : sal_Bool ScDPTableData::IsInGroup( const ScDPItemData&, long,
139 : const ScDPItemData&, long ) const
140 : {
141 : OSL_FAIL("IsInGroup shouldn't be called for non-group data");
142 0 : return false;
143 : }
144 :
145 0 : sal_Bool ScDPTableData::HasCommonElement( const ScDPItemData&, long,
146 : const ScDPItemData&, long ) const
147 : {
148 : OSL_FAIL("HasCommonElement shouldn't be called for non-group data");
149 0 : return false;
150 : }
151 419 : void ScDPTableData::FillRowDataFromCacheTable(sal_Int32 nRow, const ScDPFilteredCache& rCacheTable,
152 : const CalcInfo& rInfo, CalcRowData& rData)
153 : {
154 : // column dimensions
155 419 : GetItemData(rCacheTable, nRow, rInfo.aColLevelDims, rData.aColData);
156 :
157 : // row dimensions
158 419 : GetItemData(rCacheTable, nRow, rInfo.aRowLevelDims, rData.aRowData);
159 :
160 : // page dimensions
161 419 : GetItemData(rCacheTable, nRow, rInfo.aPageDims, rData.aPageData);
162 :
163 419 : long nCacheColumnCount = rCacheTable.getCache()->GetColumnCount();
164 419 : sal_Int32 n = rInfo.aDataSrcCols.size();
165 838 : for (sal_Int32 i = 0; i < n; ++i)
166 : {
167 419 : long nDim = rInfo.aDataSrcCols[i];
168 419 : rData.aValues.push_back( ScDPValue() );
169 : // #i111435# GetItemData needs dimension indexes including groups,
170 : // so the index must be checked here (groups aren't useful as data fields).
171 419 : if ( nDim < nCacheColumnCount )
172 : {
173 419 : ScDPValue& rVal = rData.aValues.back();
174 419 : rCacheTable.getValue( rVal, static_cast<SCCOL>(nDim), static_cast<SCROW>(nRow), false);
175 : }
176 : }
177 419 : }
178 :
179 419 : void ScDPTableData::ProcessRowData(CalcInfo& rInfo, const CalcRowData& rData, bool bAutoShow)
180 : {
181 419 : if (!bAutoShow)
182 : {
183 414 : LateInitParams aColParams(rInfo.aColDims, rInfo.aColLevels, false);
184 828 : LateInitParams aRowParams(rInfo.aRowDims, rInfo.aRowLevels, true);
185 : // root always init child
186 414 : aColParams.SetInitChild(true);
187 414 : aColParams.SetInitAllChildren( false);
188 414 : aRowParams.SetInitChild(true);
189 414 : aRowParams.SetInitAllChildren( false);
190 :
191 414 : rInfo.pColRoot->LateInitFrom(aColParams, rData.aColData, 0, *rInfo.pInitState);
192 828 : rInfo.pRowRoot->LateInitFrom(aRowParams, rData.aRowData, 0, *rInfo.pInitState);
193 : }
194 :
195 1252 : if ( ( !rInfo.pColRoot->GetChildDimension() || rInfo.pColRoot->GetChildDimension()->IsValidEntry(rData.aColData) ) &&
196 784 : ( !rInfo.pRowRoot->GetChildDimension() || rInfo.pRowRoot->GetChildDimension()->IsValidEntry(rData.aRowData) ) )
197 : {
198 : //! single process method with ColMembers, RowMembers and data !!!
199 414 : if (rInfo.pColRoot->GetChildDimension())
200 : {
201 210 : vector<SCROW> aEmptyData;
202 210 : rInfo.pColRoot->GetChildDimension()->ProcessData(rData.aColData, NULL, aEmptyData, rData.aValues);
203 : }
204 :
205 414 : rInfo.pRowRoot->ProcessData(rData.aRowData, rInfo.pColRoot->GetChildDimension(),
206 828 : rData.aColData, rData.aValues);
207 : }
208 419 : }
209 :
210 70 : void ScDPTableData::CalcResultsFromCacheTable(const ScDPFilteredCache& rCacheTable, CalcInfo& rInfo, bool bAutoShow)
211 : {
212 70 : sal_Int32 nRowSize = rCacheTable.getRowSize();
213 439 : for (sal_Int32 nRow = 0; nRow < nRowSize; ++nRow)
214 : {
215 : sal_Int32 nLastRow;
216 369 : if (!rCacheTable.isRowActive(nRow, &nLastRow))
217 : {
218 18 : nRow = nLastRow;
219 18 : continue;
220 : }
221 :
222 351 : CalcRowData aData;
223 351 : FillRowDataFromCacheTable(nRow, rCacheTable, rInfo, aData);
224 351 : ProcessRowData(rInfo, aData, bAutoShow);
225 351 : }
226 70 : }
227 :
228 1257 : void ScDPTableData::GetItemData(const ScDPFilteredCache& rCacheTable, sal_Int32 nRow,
229 : const vector<long>& rDims, vector<SCROW>& rItemData)
230 : {
231 1257 : sal_Int32 nDimSize = rDims.size();
232 2045 : for (sal_Int32 i = 0; i < nDimSize; ++i)
233 : {
234 788 : long nDim = rDims[i];
235 :
236 788 : if (getIsDataLayoutDimension(nDim))
237 : {
238 38 : rItemData.push_back( -1 );
239 76 : continue;
240 : }
241 :
242 750 : nDim = GetSourceDim( nDim );
243 750 : if ( nDim >= rCacheTable.getCache()->GetColumnCount() )
244 0 : continue;
245 :
246 750 : SCROW nId= rCacheTable.getCache()->GetItemDataId( static_cast<SCCOL>(nDim), static_cast<SCROW>(nRow), IsRepeatIfEmpty());
247 750 : rItemData.push_back( nId );
248 : }
249 1257 : }
250 :
251 : // -----------------------------------------------------------------------
252 :
253 142 : long ScDPTableData::GetMembersCount( long nDim )
254 : {
255 142 : if ( nDim > MAXCOL )
256 0 : return 0;
257 142 : return GetCacheTable().getFieldEntries( nDim ).size();
258 : }
259 :
260 0 : const ScDPItemData* ScDPTableData::GetMemberByIndex( long nDim, long nIndex )
261 : {
262 0 : if ( nIndex >= GetMembersCount( nDim ) )
263 0 : return NULL;
264 :
265 0 : const ::std::vector<SCROW>& nMembers = GetCacheTable().getFieldEntries( nDim );
266 :
267 0 : return GetCacheTable().getCache()->GetItemDataById( (SCCOL) nDim, (SCROW)nMembers[nIndex] );
268 : }
269 :
270 9825 : const ScDPItemData* ScDPTableData::GetMemberById( long nDim, long nId)
271 : {
272 :
273 9825 : return GetCacheTable().getCache()->GetItemDataById(nDim, static_cast<SCROW>(nId));
274 : }
275 :
276 841 : const std::vector< SCROW >& ScDPTableData::GetColumnEntries( long nColumn )
277 : {
278 841 : return GetCacheTable().getFieldEntries( nColumn );
279 : }
280 :
281 591 : long ScDPTableData::GetSourceDim( long nDim )
282 : {
283 591 : return nDim;
284 :
285 : }
286 :
287 1212 : long ScDPTableData::Compare( long nDim, long nDataId1, long nDataId2)
288 : {
289 1212 : if ( getIsDataLayoutDimension(nDim) )
290 12 : return 0;
291 :
292 1200 : long n1 = GetCacheTable().getOrder(nDim, nDataId1);
293 1200 : long n2 = GetCacheTable().getOrder(nDim, nDataId2);
294 1200 : if ( n1 > n2 )
295 1200 : return 1;
296 0 : else if ( n1 == n2 )
297 0 : return 0;
298 : else
299 0 : return -1;
300 93 : }
301 :
302 : #if DEBUG_PIVOT_TABLE
303 : void ScDPTableData::Dump() const
304 : {
305 : }
306 : #endif
307 :
308 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|