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