1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26
27
28
29 // INCLUDE --------------------------------------------------------------
30
31 #include <tools/debug.hxx>
32 #include <svl/zforlist.hxx>
33
34 #include "dpshttab.hxx"
35 #include "dptabres.hxx"
36 #include "document.hxx"
37 #include "collect.hxx"
38 #include "cell.hxx"
39 #include "dpcachetable.hxx"
40 #include "dpobject.hxx"
41 #include "globstr.hrc"
42 // Wang Xu Ming -- 2009-8-17
43 // DataPilot Migration - Cache&&Performance
44 #include "dpglobal.hxx"
45 // End Comments
46 #include <com/sun/star/sheet/DataPilotFieldFilter.hpp>
47
48 #include <vector>
49 #include <set>
50
51 using namespace ::com::sun::star;
52 using ::com::sun::star::uno::Any;
53 using ::com::sun::star::uno::Sequence;
54 using ::std::vector;
55 using ::std::hash_map;
56 using ::std::hash_set;
57
58 // -----------------------------------------------------------------------
59
ScSheetDPData(ScDocument * pD,const ScSheetSourceDesc & rDesc,long nCacheId)60 ScSheetDPData::ScSheetDPData( ScDocument* pD, const ScSheetSourceDesc& rDesc , long nCacheId) :
61 ScDPTableData(pD, rDesc.GetCacheId( pD, nCacheId) ), // DataPilot Migration - Cache&&Performance
62 aQuery ( rDesc.aQueryParam ),
63 pSpecial(NULL),
64 bIgnoreEmptyRows( sal_False ),
65 bRepeatIfEmpty(sal_False),
66 aCacheTable( pD, GetCacheId() ) // base class ID is initialized with the GetCacheId call above
67 {
68 SCSIZE nEntryCount( aQuery.GetEntryCount());
69 pSpecial = new sal_Bool[nEntryCount];
70 for (SCSIZE j = 0; j < nEntryCount; ++j )
71 {
72 ScQueryEntry& rEntry = aQuery.GetEntry(j);
73 if (rEntry.bDoQuery)
74 {
75 pSpecial[j] = false;
76 if (!rEntry.bQueryByString)
77 {
78 if (*rEntry.pStr == EMPTY_STRING &&
79 ((rEntry.nVal == SC_EMPTYFIELDS) || (rEntry.nVal == SC_NONEMPTYFIELDS)))
80 pSpecial[j] = true;
81 }
82 else
83 {
84 sal_uInt32 nIndex = 0;
85 rEntry.bQueryByString =
86 !(pD->GetFormatTable()->
87 IsNumberFormat(*rEntry.pStr, nIndex, rEntry.nVal));
88 }
89 }
90 }
91 }
92
~ScSheetDPData()93 ScSheetDPData::~ScSheetDPData()
94 {
95 delete[] pSpecial;
96 }
97
DisposeData()98 void ScSheetDPData::DisposeData()
99 {
100 aCacheTable.clear();
101 }
102
GetColumnCount()103 long ScSheetDPData::GetColumnCount()
104 {
105 CreateCacheTable();
106 return aCacheTable.getColSize();
107 }
108
getDimensionName(long nColumn)109 String ScSheetDPData::getDimensionName(long nColumn)
110 {
111 CreateCacheTable();
112 if (getIsDataLayoutDimension(nColumn))
113 {
114 //! different internal and display names?
115 //return "Data";
116 return ScGlobal::GetRscString(STR_PIVOT_DATA);
117 }
118 else if (nColumn >= aCacheTable.getColSize())
119 {
120 DBG_ERROR("getDimensionName: invalid dimension");
121 return String();
122 }
123 else
124 {
125 return aCacheTable.getFieldName((SCCOL)nColumn);
126 }
127 }
128
IsDateDimension(long nDim)129 sal_Bool ScSheetDPData::IsDateDimension(long nDim)
130 {
131 CreateCacheTable();
132 long nColCount = aCacheTable.getColSize();
133 if (getIsDataLayoutDimension(nDim))
134 {
135 return sal_False;
136 }
137 else if (nDim >= nColCount)
138 {
139 DBG_ERROR("IsDateDimension: invalid dimension");
140 return sal_False;
141 }
142 else
143 {
144 return aCacheTable.GetCache()->IsDateDimension( nDim);
145 }
146 }
147
GetNumberFormat(long nDim)148 sal_uLong ScSheetDPData::GetNumberFormat(long nDim)
149 {
150 CreateCacheTable();
151 if (getIsDataLayoutDimension(nDim))
152 {
153 return 0;
154 }
155 else if (nDim >= GetCacheTable().getColSize())
156 {
157 DBG_ERROR("GetNumberFormat: invalid dimension");
158 return 0;
159 }
160 else
161 {
162 return GetCacheTable().GetCache()->GetNumberFormat( nDim );
163 }
164 }
GetNumberFormatByIdx(NfIndexTableOffset eIdx)165 sal_uInt32 ScDPTableData::GetNumberFormatByIdx( NfIndexTableOffset eIdx )
166 {
167 if( !mpDoc )
168 return 0;
169
170 if ( SvNumberFormatter* pFormatter = mpDoc->GetFormatTable() )
171 return pFormatter->GetFormatIndex( eIdx, LANGUAGE_SYSTEM );
172
173 return 0;
174 }
175
getIsDataLayoutDimension(long nColumn)176 sal_Bool ScSheetDPData::getIsDataLayoutDimension(long nColumn)
177 {
178 CreateCacheTable();
179 return (nColumn ==(long)( aCacheTable.getColSize()));
180 }
181
SetEmptyFlags(sal_Bool bIgnoreEmptyRowsP,sal_Bool bRepeatIfEmptyP)182 void ScSheetDPData::SetEmptyFlags( sal_Bool bIgnoreEmptyRowsP, sal_Bool bRepeatIfEmptyP )
183 {
184 bIgnoreEmptyRows = bIgnoreEmptyRowsP;
185 bRepeatIfEmpty = bRepeatIfEmptyP;
186 }
187
IsRepeatIfEmpty()188 bool ScSheetDPData::IsRepeatIfEmpty()
189 {
190 return bRepeatIfEmpty;
191 }
192
CreateCacheTable()193 void ScSheetDPData::CreateCacheTable()
194 {
195 // Scan and store the data from the source range.
196 if (!aCacheTable.empty())
197 // already cached.
198 return;
199
200 aCacheTable.fillTable( aQuery, pSpecial,
201 bIgnoreEmptyRows, bRepeatIfEmpty );
202 }
203
FilterCacheTable(const vector<ScDPCacheTable::Criterion> & rCriteria,const hash_set<sal_Int32> & rCatDims)204 void ScSheetDPData::FilterCacheTable(const vector<ScDPCacheTable::Criterion>& rCriteria, const hash_set<sal_Int32>& rCatDims)
205 {
206 CreateCacheTable();
207 aCacheTable.filterByPageDimension(
208 rCriteria, (IsRepeatIfEmpty() ? rCatDims : hash_set<sal_Int32>()));
209 }
210
GetDrillDownData(const vector<ScDPCacheTable::Criterion> & rCriteria,const hash_set<sal_Int32> & rCatDims,Sequence<Sequence<Any>> & rData)211 void ScSheetDPData::GetDrillDownData(const vector<ScDPCacheTable::Criterion>& rCriteria, const hash_set<sal_Int32>& rCatDims, Sequence< Sequence<Any> >& rData)
212 {
213 CreateCacheTable();
214 sal_Int32 nRowSize = aCacheTable.getRowSize();
215 if (!nRowSize)
216 return;
217
218 aCacheTable.filterTable(
219 rCriteria, rData, IsRepeatIfEmpty() ? rCatDims : hash_set<sal_Int32>());
220 }
221
CalcResults(CalcInfo & rInfo,bool bAutoShow)222 void ScSheetDPData::CalcResults(CalcInfo& rInfo, bool bAutoShow)
223 {
224 CreateCacheTable();
225 CalcResultsFromCacheTable(aCacheTable, rInfo, bAutoShow);
226 }
227
GetCacheTable() const228 const ScDPCacheTable& ScSheetDPData::GetCacheTable() const
229 {
230 return aCacheTable;
231 }
232
233
234 // Wang Xu Ming -- 2009-8-5
235 // DataPilot Migration - Cache&&Performance
CreateCache(ScDocument * pDoc,long nID) const236 ScDPTableDataCache* ScSheetSourceDesc::CreateCache( ScDocument* pDoc , long nID ) const
237 {
238 if ( pDoc )
239 {
240 ScDPTableDataCache* pCache = GetExistDPObjectCache( pDoc );
241 if ( pCache && ( nID < 0 || nID == pCache->GetId() ) )
242 return pCache;
243
244 sal_uLong nErrId = CheckValidate( pDoc );
245 if ( !nErrId )
246 {
247 pCache = new ScDPTableDataCache( pDoc );
248
249 pCache->InitFromDoc( pDoc, aSourceRange );
250 pCache->SetId( nID );
251 pDoc->AddDPObjectCache( pCache );
252
253 DBG_TRACE1("Create a cache id = %d \n", pCache->GetId() );
254 }
255 else
256 DBG_ERROR( "\n Error Create Cache" );
257 return pCache;
258 }
259 return NULL;
260 }
261
GetExistDPObjectCache(ScDocument * pDoc) const262 ScDPTableDataCache* ScSheetSourceDesc::GetExistDPObjectCache ( ScDocument* pDoc ) const
263 {
264 return pDoc->GetUsedDPObjectCache( aSourceRange );
265 }
GetCache(ScDocument * pDoc,long nID) const266 ScDPTableDataCache* ScSheetSourceDesc::GetCache( ScDocument* pDoc, long nID ) const
267 {
268 ScDPTableDataCache* pCache = pDoc->GetDPObjectCache( nID );
269 if ( NULL == pCache && pDoc )
270 pCache = GetExistDPObjectCache( pDoc );
271 if ( NULL == pCache )
272 pCache = CreateCache( pDoc );
273 return pCache;
274 }
275
GetCacheId(ScDocument * pDoc,long nID) const276 long ScSheetSourceDesc:: GetCacheId( ScDocument* pDoc, long nID ) const
277 {
278 ScDPTableDataCache* pCache = GetCache( pDoc, nID);
279 if ( NULL == pCache )
280 return -1;
281 else
282 return pCache->GetId();
283 }
284
CheckValidate(ScDocument * pDoc) const285 sal_uLong ScSheetSourceDesc::CheckValidate( ScDocument* pDoc ) const
286 {
287 ScRange aSrcRange( aSourceRange);
288 if ( !pDoc )
289 return STR_ERR_DATAPILOTSOURCE;
290
291 // #i116457# Empty column titles were allowed before 3.3, and might be useful for hidden columns with annotations.
292 // Be compatible with 3.2: Allow empty titles, create columns with empty names, hide them in the dialogs.
293
294 if( pDoc->IsBlockEmpty( aSrcRange.aStart.Tab(), aSrcRange.aStart.Col(), aSrcRange.aStart.Row()+1, aSrcRange.aEnd.Col(), aSrcRange.aEnd.Row() ) )
295 {
296 return STR_PIVOT_ONLYONEROWERR;
297 }
298 return 0;
299 }
300 // End Comments
301
302 // -----------------------------------------------------------------------
303
304
305
306
307
308
309
310