LibreOffice Module sc (master)  1
dpshttab.cxx
Go to the documentation of this file.
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/numformat.hxx>
21 #include <svl/zforlist.hxx>
22 #include <unotools/charclass.hxx>
23 
24 #include <dpcache.hxx>
25 #include <dpshttab.hxx>
26 #include <document.hxx>
27 #include <dpfilteredcache.hxx>
28 #include <dpobject.hxx>
29 #include <globstr.hrc>
30 #include <scresid.hxx>
31 #include <rangenam.hxx>
32 #include <queryentry.hxx>
33 
34 #include <osl/diagnose.h>
35 
36 #include <vector>
37 
38 using namespace ::com::sun::star;
39 using ::com::sun::star::uno::Any;
40 using ::com::sun::star::uno::Sequence;
41 using ::std::vector;
42 
43 ScSheetDPData::ScSheetDPData(const ScDocument* pD, const ScSheetSourceDesc& rDesc, const ScDPCache& rCache) :
44  ScDPTableData(pD),
45  aQuery ( rDesc.GetQueryParam() ),
46  bIgnoreEmptyRows( false ),
47  bRepeatIfEmpty(false),
48  aCacheTable(rCache)
49 {
50  SCSIZE nEntryCount( aQuery.GetEntryCount());
51  for (SCSIZE j = 0; j < nEntryCount; ++j)
52  {
53  ScQueryEntry& rEntry = aQuery.GetEntry(j);
54  if (rEntry.bDoQuery)
55  {
56  ScQueryEntry::Item& rItem = rEntry.GetQueryItem();
57  if (rItem.meType == ScQueryEntry::ByString)
58  {
59  sal_uInt32 nIndex = 0;
60  bool bNumber = pD->GetFormatTable()->IsNumberFormat(
61  rItem.maString.getString(), nIndex, rItem.mfVal);
63  }
64  }
65  }
66 }
67 
69 {
70 }
71 
73 {
75 }
76 
78 {
80  return aCacheTable.getColSize();
81 }
82 
83 OUString ScSheetDPData::getDimensionName(sal_Int32 nColumn)
84 {
86  if (getIsDataLayoutDimension(nColumn))
87  {
88  //TODO: different internal and display names?
89  //return "Data";
90  return ScResId(STR_PIVOT_DATA);
91  }
92  else if (nColumn >= aCacheTable.getColSize())
93  {
94  OSL_FAIL("getDimensionName: invalid dimension");
95  return OUString();
96  }
97  else
98  {
99  return aCacheTable.getFieldName(static_cast<SCCOL>(nColumn));
100  }
101 }
102 
103 bool ScSheetDPData::IsDateDimension(sal_Int32 nDim)
104 {
106  tools::Long nColCount = aCacheTable.getColSize();
107  if (getIsDataLayoutDimension(nDim))
108  {
109  return false;
110  }
111  else if (nDim >= nColCount)
112  {
113  OSL_FAIL("IsDateDimension: invalid dimension");
114  return false;
115  }
116  else
117  {
118  return GetCacheTable().getCache().IsDateDimension( nDim);
119  }
120 }
121 
122 sal_uInt32 ScSheetDPData::GetNumberFormat(sal_Int32 nDim)
123 {
125  if (getIsDataLayoutDimension(nDim))
126  {
127  return 0;
128  }
129  else if (nDim >= GetCacheTable().getColSize())
130  {
131  OSL_FAIL("GetNumberFormat: invalid dimension");
132  return 0;
133  }
134  else
135  {
136  return GetCacheTable().getCache().GetNumberFormat( nDim );
137  }
138 }
140 {
141  if( !mpDoc )
142  return 0;
143 
144  if ( SvNumberFormatter* pFormatter = mpDoc->GetFormatTable() )
145  return pFormatter->GetFormatIndex( eIdx, LANGUAGE_SYSTEM );
146 
147  return 0;
148 }
149 
151 {
153  return (nColumn ==static_cast<tools::Long>( aCacheTable.getColSize()));
154 }
155 
156 void ScSheetDPData::SetEmptyFlags( bool bIgnoreEmptyRowsP, bool bRepeatIfEmptyP )
157 {
158  bIgnoreEmptyRows = bIgnoreEmptyRowsP;
159  bRepeatIfEmpty = bRepeatIfEmptyP;
160 }
161 
163 {
164  return bRepeatIfEmpty;
165 }
166 
168 {
169  // Scan and store the data from the source range.
170  if (!aCacheTable.empty())
171  // already cached.
172  return;
173 
175 }
176 
177 void ScSheetDPData::FilterCacheTable(const vector<ScDPFilteredCache::Criterion>& rCriteria, const std::unordered_set<sal_Int32>& rCatDims)
178 {
181  rCriteria, (IsRepeatIfEmpty() ? rCatDims : std::unordered_set<sal_Int32>()));
182 }
183 
184 void ScSheetDPData::GetDrillDownData(const vector<ScDPFilteredCache::Criterion>& rCriteria, const std::unordered_set<sal_Int32>& rCatDims, Sequence< Sequence<Any> >& rData)
185 {
187  sal_Int32 nRowSize = aCacheTable.getRowSize();
188  if (!nRowSize)
189  return;
190 
192  rCriteria, rData, IsRepeatIfEmpty() ? rCatDims : std::unordered_set<sal_Int32>());
193 }
194 
195 void ScSheetDPData::CalcResults(CalcInfo& rInfo, bool bAutoShow)
196 {
198  CalcResultsFromCacheTable(aCacheTable, rInfo, bAutoShow);
199 }
200 
202 {
203  return aCacheTable;
204 }
205 
207 {
208  aCacheTable.clear();
210 }
211 
212 #if DUMP_PIVOT_TABLE
213 
214 void ScSheetDPData::Dump() const
215 {
216  // TODO : Implement this.
217 }
218 
219 #endif
220 
222  mpDoc(pDoc) {}
223 
225 {
226  maSourceRange = rRange;
227  maRangeName.clear(); // overwrite existing range name if any.
228 }
229 
231 {
232  if (!maRangeName.isEmpty())
233  {
234  // Obtain the source range from the range name first.
236  ScRangeName* pRangeName = mpDoc->GetRangeName();
237  do
238  {
239  if (!pRangeName)
240  break;
241 
242  OUString aUpper = ScGlobal::getCharClass().uppercase(maRangeName);
243  const ScRangeData* pData = pRangeName->findByUpperName(aUpper);
244  if (!pData)
245  break;
246 
247  // range name found. Fow now, we only use the first token and
248  // ignore the rest.
249  ScRange aRange;
250  if (!pData->IsReference(aRange))
251  break;
252 
253  maSourceRange = aRange;
254  }
255  while (false);
256  }
257  return maSourceRange;
258 }
259 
260 void ScSheetSourceDesc::SetRangeName(const OUString& rName)
261 {
262  maRangeName = rName;
263 }
264 
266 {
267  return !maRangeName.isEmpty();
268 }
269 
271 {
272  maQueryParam = rParam;
273 }
274 
276 {
277  return maSourceRange == rOther.maSourceRange &&
278  maRangeName == rOther.maRangeName &&
279  maQueryParam == rOther.maQueryParam;
280 }
281 
283 {
284  if (!mpDoc)
285  return nullptr;
286 
287  TranslateId pErrId = CheckSourceRange();
288  if (pErrId)
289  {
290  OSL_FAIL( "Error Create Cache" );
291  return nullptr;
292  }
293 
294  // All cache instances are managed centrally by ScDPCollection.
296  if (HasRangeName())
297  {
298  // Name-based data source.
299  ScDPCollection::NameCaches& rCaches = pDPs->GetNameCaches();
300  return rCaches.getCache(GetRangeName(), GetSourceRange(), pDimData);
301  }
302 
303  ScDPCollection::SheetCaches& rCaches = pDPs->GetSheetCaches();
304  return rCaches.getCache(GetSourceRange(), pDimData);
305 }
306 
308 {
309  if (!mpDoc)
310  return STR_ERR_DATAPILOTSOURCE;
311 
312  // Make sure the range is valid and sane.
313  const ScRange& rSrcRange = GetSourceRange();
314  if (!rSrcRange.IsValid())
315  return STR_ERR_DATAPILOTSOURCE;
316 
317  if (rSrcRange.aStart.Col() > rSrcRange.aEnd.Col() || rSrcRange.aStart.Row() > rSrcRange.aEnd.Row())
318  return STR_ERR_DATAPILOTSOURCE;
319 
320  return {};
321 }
322 
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString getFieldName(SCCOL nIndex) const
sal_Int32 nIndex
OUString getString() const
ScAddress aStart
Definition: address.hxx:499
This class has to do with handling exclusively grouped dimensions? TODO: Find out what this class doe...
Definition: dpdimsave.hxx:163
virtual void CreateCacheTable() override
Definition: dpshttab.cxx:167
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:89
SCROW Row() const
Definition: address.hxx:261
SC_DLLPUBLIC SheetCaches & GetSheetCaches()
Definition: dpobject.cxx:3752
sal_Int32 getRowSize() const
const Item & GetQueryItem() const
Definition: queryentry.hxx:84
virtual void GetDrillDownData(const std::vector< ScDPFilteredCache::Criterion > &rCriteria, const std::unordered_set< sal_Int32 > &rCatDims, css::uno::Sequence< css::uno::Sequence< css::uno::Any > > &rData) override
Definition: dpshttab.cxx:184
std::unique_ptr< ContentProperties > pData
void filterTable(const std::vector< Criterion > &rCriteria, css::uno::Sequence< css::uno::Sequence< css::uno::Any >> &rTabData, const std::unordered_set< sal_Int32 > &rRepeatIfEmptyDims)
Filter the table based on the specified criteria, and copy the result to rTabData.
void fillTable(const ScQueryParam &rQuery, bool bIgnoreEmptyRows, bool bRepeatIfEmpty)
virtual sal_Int32 GetColumnCount() override
use (new) typed collection instead of ScStrCollection or separate Str and ValueCollection ...
Definition: dpshttab.cxx:77
long Long
virtual void ReloadCacheTable() override
Definition: dpshttab.cxx:206
SC_DLLPUBLIC ScRangeName * GetRangeName(SCTAB nTab) const
Definition: documen3.cxx:168
SC_DLLPUBLIC ScDPCollection * GetDPCollection()
Definition: documen3.cxx:365
TranslateId CheckSourceRange() const
Check the sanity of the data source range.
Definition: dpshttab.cxx:307
const ContentProperties & rData
ScAddress aEnd
Definition: address.hxx:500
Data caches for range name based source data.
Definition: dpobject.hxx:317
This class represents the cached data part of the datapilot cache table implementation.
Definition: dpcache.hxx:47
ScDocument * mpDoc
Definition: dpshttab.hxx:80
virtual void FilterCacheTable(const std::vector< ScDPFilteredCache::Criterion > &rCriteria, const std::unordered_set< sal_Int32 > &rCatDims) override
Definition: dpshttab.cxx:177
void SetQueryParam(const ScQueryParam &rParam)
Definition: dpshttab.cxx:270
virtual OUString getDimensionName(sal_Int32 nColumn) override
Definition: dpshttab.cxx:83
ScQueryParam maQueryParam
Definition: dpshttab.hxx:79
SC_DLLPUBLIC void SetSourceRange(const ScRange &rRange)
Definition: dpshttab.cxx:224
size_t SCSIZE
size_t typedef to be able to find places where code was changed from USHORT to size_t and is used to ...
Definition: address.hxx:44
SC_DLLPUBLIC SCSIZE GetEntryCount() const
Definition: queryparam.cxx:120
ScRange maSourceRange
Definition: dpshttab.hxx:77
virtual const ScDPFilteredCache & GetCacheTable() const override
Definition: dpshttab.cxx:201
SC_DLLPUBLIC const ScQueryEntry & GetEntry(SCSIZE n) const
Definition: queryparam.cxx:125
NfIndexTableOffset
ScSheetSourceDesc()=delete
SC_DLLPUBLIC const ScRange & GetSourceRange() const
Get the range that contains the source data.
Definition: dpshttab.cxx:230
virtual void Dump() const override
const ScDocument * mpDoc
Definition: dptabdat.hxx:63
void CalcResultsFromCacheTable(const ScDPFilteredCache &rCacheTable, CalcInfo &rInfo, bool bAutoShow)
Definition: dptabdat.cxx:199
virtual ~ScSheetDPData() override
Definition: dpshttab.cxx:68
SC_DLLPUBLIC ScRangeData * findByUpperName(const OUString &rName)
Definition: rangenam.cxx:681
SC_DLLPUBLIC SvNumberFormatter * GetFormatTable() const
Definition: documen2.cxx:441
const ScDPCache * getCache(const OUString &rName, const ScRange &rRange, const ScDPDimensionSaveData *pDimData)
Definition: dpobject.cxx:3097
ScQueryParam aQuery
Definition: dpshttab.hxx:89
This structure stores dimension information used when calculating results.
Definition: dptabdat.hxx:69
Stores and manages all caches from internal sheets.
Definition: dpobject.hxx:288
const ScDPCache * getCache(const ScRange &rRange, const ScDPDimensionSaveData *pDimData)
Definition: dpobject.cxx:2916
#define LANGUAGE_SYSTEM
virtual bool IsDateDimension(sal_Int32 nDim) override
Definition: dpshttab.cxx:103
static SC_DLLPUBLIC const CharClass & getCharClass()
Definition: global.cxx:1016
sal_uInt32 GetNumberFormatByIdx(NfIndexTableOffset)
Definition: dpshttab.cxx:139
OUString uppercase(const OUString &rStr, sal_Int32 nPos, sal_Int32 nCount) const
bool IsNumberFormat(const OUString &sString, sal_uInt32 &F_Index, double &fOutNumber, SvNumInputOptions eInputOptions=SvNumInputOptions::NONE)
bool IsValid() const
Definition: address.hxx:546
bool operator==(const ScSheetSourceDesc &rOther) const
Definition: dpshttab.cxx:275
svl::SharedString maString
Definition: queryentry.hxx:49
SC_DLLPUBLIC const ScDPCache * CreateCache(const ScDPDimensionSaveData *pDimData) const
Definition: dpshttab.cxx:282
SCCOL Col() const
Definition: address.hxx:266
bool HasRangeName() const
Definition: dpshttab.cxx:265
Base class that abstracts different data source types of a datapilot table.
Definition: dptabdat.hxx:56
sal_Int32 getColSize() const
bool bRepeatIfEmpty
Definition: dpshttab.hxx:91
virtual bool IsRepeatIfEmpty() override
Definition: dpshttab.cxx:162
virtual sal_uInt32 GetNumberFormat(sal_Int32 nDim) override
Definition: dpshttab.cxx:122
virtual void SetEmptyFlags(bool bIgnoreEmptyRows, bool bRepeatIfEmpty) override
Definition: dpshttab.cxx:156
bool IsDateDimension(tools::Long nDim) const
Definition: dpcache.cxx:1063
sal_uInt32 GetNumberFormat(tools::Long nDim) const
Definition: dpcache.cxx:1053
const OUString & GetRangeName() const
Definition: dpshttab.hxx:60
virtual void DisposeData() override
Definition: dpshttab.cxx:72
virtual void CalcResults(CalcInfo &rInfo, bool bAutoShow) override
Definition: dpshttab.cxx:195
SC_DLLPUBLIC bool IsReference(ScRange &rRef) const
Definition: rangenam.cxx:368
bool bIgnoreEmptyRows
Definition: dpshttab.hxx:90
virtual bool getIsDataLayoutDimension(sal_Int32 nColumn) override
Definition: dpshttab.cxx:150
OUString maRangeName
Definition: dpshttab.hxx:78
SC_DLLPUBLIC void SetRangeName(const OUString &rName)
Definition: dpshttab.cxx:260
This class is only a wrapper to the actual cache, to provide filtering on the raw data based on the q...
ScSheetDPData(const ScDocument *pD, const ScSheetSourceDesc &rDesc, const ScDPCache &rCache)
Definition: dpshttab.cxx:43
NameCaches & GetNameCaches()
Definition: dpobject.cxx:3762
void filterByPageDimension(const std::vector< Criterion > &rCriteria, const std::unordered_set< sal_Int32 > &rRepeatIfEmptyDims)
Set filter on/off flag to each row to control visibility.
This class contains authoritative information on the internal reference used as the data source for d...
Definition: dpshttab.hxx:39
Each instance of this struct represents a single filtering criteria.
Definition: queryentry.hxx:33
const ScDPCache & getCache() const
ScDPFilteredCache aCacheTable
Definition: dpshttab.hxx:93