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 <svl/zforlist.hxx>
21 :
22 : #include "dpshttab.hxx"
23 : #include "dptabres.hxx"
24 : #include "document.hxx"
25 : #include "formulacell.hxx"
26 : #include "dpfilteredcache.hxx"
27 : #include "dpobject.hxx"
28 : #include "globstr.hrc"
29 : #include "rangenam.hxx"
30 : #include "queryentry.hxx"
31 :
32 : #include <com/sun/star/sheet/DataPilotFieldFilter.hpp>
33 :
34 : #include <vector>
35 : #include <set>
36 :
37 : using namespace ::com::sun::star;
38 : using ::com::sun::star::uno::Any;
39 : using ::com::sun::star::uno::Sequence;
40 : using ::std::vector;
41 :
42 81 : ScSheetDPData::ScSheetDPData(ScDocument* pD, const ScSheetSourceDesc& rDesc, const ScDPCache& rCache) :
43 : ScDPTableData(pD),
44 81 : aQuery ( rDesc.GetQueryParam() ),
45 : bIgnoreEmptyRows( false ),
46 : bRepeatIfEmpty(false),
47 162 : aCacheTable(rCache)
48 : {
49 81 : SCSIZE nEntryCount( aQuery.GetEntryCount());
50 729 : for (SCSIZE j = 0; j < nEntryCount; ++j)
51 : {
52 648 : ScQueryEntry& rEntry = aQuery.GetEntry(j);
53 648 : if (rEntry.bDoQuery)
54 : {
55 11 : ScQueryEntry::Item& rItem = rEntry.GetQueryItem();
56 11 : if (rItem.meType == ScQueryEntry::ByString)
57 : {
58 0 : sal_uInt32 nIndex = 0;
59 : bool bNumber = pD->GetFormatTable()->IsNumberFormat(
60 0 : rItem.maString.getString(), nIndex, rItem.mfVal);
61 0 : rItem.meType = bNumber ? ScQueryEntry::ByValue : ScQueryEntry::ByString;
62 : }
63 : }
64 : }
65 81 : }
66 :
67 162 : ScSheetDPData::~ScSheetDPData()
68 : {
69 162 : }
70 :
71 9 : void ScSheetDPData::DisposeData()
72 : {
73 9 : aCacheTable.clear();
74 9 : }
75 :
76 22209 : long ScSheetDPData::GetColumnCount()
77 : {
78 22209 : CreateCacheTable();
79 22209 : return aCacheTable.getColSize();
80 : }
81 :
82 27704 : OUString ScSheetDPData::getDimensionName(long nColumn)
83 : {
84 27704 : CreateCacheTable();
85 27704 : if (getIsDataLayoutDimension(nColumn))
86 : {
87 : //! different internal and display names?
88 : //return "Data";
89 2059 : return ScGlobal::GetRscString(STR_PIVOT_DATA);
90 : }
91 25645 : else if (nColumn >= aCacheTable.getColSize())
92 : {
93 : OSL_FAIL("getDimensionName: invalid dimension");
94 0 : return OUString();
95 : }
96 : else
97 : {
98 25645 : return aCacheTable.getFieldName(static_cast<SCCOL>(nColumn));
99 : }
100 : }
101 :
102 1903 : bool ScSheetDPData::IsDateDimension(long nDim)
103 : {
104 1903 : CreateCacheTable();
105 1903 : long nColCount = aCacheTable.getColSize();
106 1903 : if (getIsDataLayoutDimension(nDim))
107 : {
108 282 : return false;
109 : }
110 1621 : else if (nDim >= nColCount)
111 : {
112 : OSL_FAIL("IsDateDimension: invalid dimension");
113 0 : return false;
114 : }
115 : else
116 : {
117 1621 : return GetCacheTable().getCache()->IsDateDimension( nDim);
118 : }
119 : }
120 :
121 570 : sal_uLong ScSheetDPData::GetNumberFormat(long nDim)
122 : {
123 570 : CreateCacheTable();
124 570 : if (getIsDataLayoutDimension(nDim))
125 : {
126 83 : return 0;
127 : }
128 487 : else if (nDim >= GetCacheTable().getColSize())
129 : {
130 : OSL_FAIL("GetNumberFormat: invalid dimension");
131 0 : return 0;
132 : }
133 : else
134 : {
135 487 : return GetCacheTable().getCache()->GetNumberFormat( nDim );
136 : }
137 : }
138 6 : sal_uInt32 ScDPTableData::GetNumberFormatByIdx( NfIndexTableOffset eIdx )
139 : {
140 6 : if( !mpDoc )
141 0 : return 0;
142 :
143 6 : if ( SvNumberFormatter* pFormatter = mpDoc->GetFormatTable() )
144 6 : return pFormatter->GetFormatIndex( eIdx, LANGUAGE_SYSTEM );
145 :
146 0 : return 0;
147 : }
148 :
149 35238 : bool ScSheetDPData::getIsDataLayoutDimension(long nColumn)
150 : {
151 35238 : CreateCacheTable();
152 35238 : return (nColumn ==(long)( aCacheTable.getColSize()));
153 : }
154 :
155 491 : void ScSheetDPData::SetEmptyFlags( bool bIgnoreEmptyRowsP, bool bRepeatIfEmptyP )
156 : {
157 491 : bIgnoreEmptyRows = bIgnoreEmptyRowsP;
158 491 : bRepeatIfEmpty = bRepeatIfEmptyP;
159 491 : }
160 :
161 924 : bool ScSheetDPData::IsRepeatIfEmpty()
162 : {
163 924 : return bRepeatIfEmpty;
164 : }
165 :
166 88002 : void ScSheetDPData::CreateCacheTable()
167 : {
168 : // Scan and store the data from the source range.
169 88002 : if (!aCacheTable.empty())
170 : // already cached.
171 175698 : return;
172 :
173 306 : aCacheTable.fillTable(aQuery, bIgnoreEmptyRows, bRepeatIfEmpty);
174 : }
175 :
176 4 : void ScSheetDPData::FilterCacheTable(const vector<ScDPFilteredCache::Criterion>& rCriteria, const boost::unordered_set<sal_Int32>& rCatDims)
177 : {
178 4 : CreateCacheTable();
179 : aCacheTable.filterByPageDimension(
180 4 : rCriteria, (IsRepeatIfEmpty() ? rCatDims : boost::unordered_set<sal_Int32>()));
181 4 : }
182 :
183 133 : void ScSheetDPData::GetDrillDownData(const vector<ScDPFilteredCache::Criterion>& rCriteria, const boost::unordered_set<sal_Int32>& rCatDims, Sequence< Sequence<Any> >& rData)
184 : {
185 133 : CreateCacheTable();
186 133 : sal_Int32 nRowSize = aCacheTable.getRowSize();
187 133 : if (!nRowSize)
188 133 : return;
189 :
190 : aCacheTable.filterTable(
191 133 : rCriteria, rData, IsRepeatIfEmpty() ? rCatDims : boost::unordered_set<sal_Int32>());
192 : }
193 :
194 76 : void ScSheetDPData::CalcResults(CalcInfo& rInfo, bool bAutoShow)
195 : {
196 76 : CreateCacheTable();
197 76 : CalcResultsFromCacheTable(aCacheTable, rInfo, bAutoShow);
198 76 : }
199 :
200 21844 : const ScDPFilteredCache& ScSheetDPData::GetCacheTable() const
201 : {
202 21844 : return aCacheTable;
203 : }
204 :
205 154 : void ScSheetDPData::ReloadCacheTable()
206 : {
207 154 : aCacheTable.clear();
208 154 : CreateCacheTable();
209 154 : }
210 :
211 64 : ScSheetSourceDesc::ScSheetSourceDesc(ScDocument* pDoc) :
212 64 : mpDoc(pDoc) {}
213 :
214 109 : void ScSheetSourceDesc::SetSourceRange(const ScRange& rRange)
215 : {
216 109 : maSourceRange = rRange;
217 109 : maRangeName = OUString(); // overwrite existing range name if any.
218 109 : }
219 :
220 525 : const ScRange& ScSheetSourceDesc::GetSourceRange() const
221 : {
222 525 : if (!maRangeName.isEmpty())
223 : {
224 : // Obtain the source range from the range name first.
225 7 : maSourceRange = ScRange();
226 7 : ScRangeName* pRangeName = mpDoc->GetRangeName();
227 : do
228 : {
229 7 : if (!pRangeName)
230 0 : break;
231 :
232 7 : OUString aUpper = ScGlobal::pCharClass->uppercase(maRangeName);
233 7 : const ScRangeData* pData = pRangeName->findByUpperName(aUpper);
234 7 : if (!pData)
235 0 : break;
236 :
237 : // range name found. Fow now, we only use the first token and
238 : // ignore the rest.
239 7 : ScRange aRange;
240 7 : if (!pData->IsReference(aRange))
241 0 : break;
242 :
243 7 : maSourceRange = aRange;
244 : }
245 : while (false);
246 : }
247 525 : return maSourceRange;
248 : }
249 :
250 2 : void ScSheetSourceDesc::SetRangeName(const OUString& rName)
251 : {
252 2 : maRangeName = rName;
253 2 : }
254 :
255 70 : const OUString& ScSheetSourceDesc::GetRangeName() const
256 : {
257 70 : return maRangeName;
258 : }
259 :
260 105 : bool ScSheetSourceDesc::HasRangeName() const
261 : {
262 105 : return !maRangeName.isEmpty();
263 : }
264 :
265 139 : void ScSheetSourceDesc::SetQueryParam(const ScQueryParam& rParam)
266 : {
267 139 : maQueryParam = rParam;
268 139 : }
269 :
270 213 : const ScQueryParam& ScSheetSourceDesc::GetQueryParam() const
271 : {
272 213 : return maQueryParam;
273 : }
274 :
275 21 : bool ScSheetSourceDesc::operator== (const ScSheetSourceDesc& rOther) const
276 : {
277 23 : return maSourceRange == rOther.maSourceRange &&
278 23 : maRangeName == rOther.maRangeName &&
279 23 : maQueryParam == rOther.maQueryParam;
280 : }
281 :
282 81 : const ScDPCache* ScSheetSourceDesc::CreateCache(const ScDPDimensionSaveData* pDimData) const
283 : {
284 81 : if (!mpDoc)
285 0 : return NULL;
286 :
287 81 : sal_uLong nErrId = CheckSourceRange();
288 81 : if (nErrId)
289 : {
290 : OSL_FAIL( "Error Create Cache\n" );
291 0 : return NULL;
292 : }
293 :
294 : // All cache instances are managed centrally by ScDPCollection.
295 81 : ScDPCollection* pDPs = mpDoc->GetDPCollection();
296 81 : if (HasRangeName())
297 : {
298 : // Name-based data source.
299 2 : ScDPCollection::NameCaches& rCaches = pDPs->GetNameCaches();
300 2 : return rCaches.getCache(GetRangeName(), GetSourceRange(), pDimData);
301 : }
302 :
303 79 : ScDPCollection::SheetCaches& rCaches = pDPs->GetSheetCaches();
304 79 : return rCaches.getCache(GetSourceRange(), pDimData);
305 : }
306 :
307 86 : sal_uLong ScSheetSourceDesc::CheckSourceRange() const
308 : {
309 86 : if (!mpDoc)
310 0 : return STR_ERR_DATAPILOTSOURCE;
311 :
312 : // Make sure the range is valid and sane.
313 86 : const ScRange& rSrcRange = GetSourceRange();
314 86 : if (!rSrcRange.IsValid())
315 0 : return STR_ERR_DATAPILOTSOURCE;
316 :
317 86 : if (rSrcRange.aStart.Col() > rSrcRange.aEnd.Col() || rSrcRange.aStart.Row() > rSrcRange.aEnd.Row())
318 0 : return STR_ERR_DATAPILOTSOURCE;
319 :
320 86 : return 0;
321 102 : }
322 :
323 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|