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