Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef SC_DPOBJECT_HXX
30 : : #define SC_DPOBJECT_HXX
31 : :
32 : : #include "scdllapi.h"
33 : : #include "global.hxx"
34 : : #include "address.hxx"
35 : : #include "dpoutput.hxx"
36 : : #include "dptypes.hxx"
37 : : #include "pivot.hxx"
38 : : #include "dpmacros.hxx"
39 : :
40 : : #include <com/sun/star/sheet/XDimensionsSupplier.hpp>
41 : :
42 : : #include <set>
43 : : #include <vector>
44 : :
45 : : #include <boost/ptr_container/ptr_list.hpp>
46 : : #include <boost/ptr_container/ptr_vector.hpp>
47 : : #include <boost/ptr_container/ptr_map.hpp>
48 : : #include <boost/shared_ptr.hpp>
49 : :
50 : : namespace com { namespace sun { namespace star {
51 : :
52 : : namespace container {
53 : : class XIndexAccess;
54 : : }
55 : :
56 : : namespace sheet {
57 : : struct DataPilotTablePositionData;
58 : : struct DataPilotTableHeaderData;
59 : : struct DataPilotFieldFilter;
60 : : }
61 : : }}}
62 : :
63 : : class Rectangle;
64 : : class ScDPSaveData;
65 : : class ScDPOutput;
66 : : struct ScPivotParam;
67 : : struct ScImportSourceDesc;
68 : : class ScSheetSourceDesc;
69 : : struct PivotField;
70 : : class ScDPTableData;
71 : : class ScDPDimensionSaveData;
72 : :
73 : 0 : struct ScDPServiceDesc
74 : : {
75 : : ::rtl::OUString aServiceName;
76 : : ::rtl::OUString aParSource;
77 : : ::rtl::OUString aParName;
78 : : ::rtl::OUString aParUser;
79 : : ::rtl::OUString aParPass;
80 : :
81 : : ScDPServiceDesc( const ::rtl::OUString& rServ, const ::rtl::OUString& rSrc, const ::rtl::OUString& rNam,
82 : : const ::rtl::OUString& rUser, const ::rtl::OUString& rPass );
83 : :
84 : : bool operator== ( const ScDPServiceDesc& rOther ) const;
85 : : };
86 : :
87 : :
88 : : class SC_DLLPUBLIC ScDPObject
89 : : {
90 : : private:
91 : : ScDocument* pDoc;
92 : : // settings
93 : : ScDPSaveData* pSaveData;
94 : : ::rtl::OUString aTableName;
95 : : ::rtl::OUString aTableTag;
96 : : ScRange aOutRange;
97 : : ScSheetSourceDesc* pSheetDesc; // for sheet data
98 : : ScImportSourceDesc* pImpDesc; // for database data
99 : : ScDPServiceDesc* pServDesc; // for external service
100 : : ::boost::shared_ptr<ScDPTableData> mpTableData;
101 : : // cached data
102 : : com::sun::star::uno::Reference<com::sun::star::sheet::XDimensionsSupplier> xSource;
103 : : ScDPOutput* pOutput;
104 : : sal_uInt16 mnAutoFormatIndex;
105 : : long nHeaderRows; // page fields plus filter button
106 : : bool mbHeaderLayout:1; // true : grid, false : standard
107 : : bool bAllowMove:1;
108 : : bool bAlive:1; // false if only used to hold settings
109 : : bool bSettingsChanged:1;
110 : : bool mbEnableGetPivotData:1;
111 : :
112 : : SC_DLLPRIVATE ScDPTableData* GetTableData();
113 : : SC_DLLPRIVATE void CreateObjects();
114 : : SC_DLLPRIVATE void CreateOutput();
115 : : SC_DLLPRIVATE void ClearSource();
116 : : SC_DLLPRIVATE bool FillLabelDataForDimension(
117 : : const com::sun::star::uno::Reference<
118 : : com::sun::star::container::XIndexAccess>& xDims,
119 : : sal_Int32 nDim, ScDPLabelData& rLabelData);
120 : :
121 : : public:
122 : : ScDPObject(ScDocument* pD);
123 : : ScDPObject(const ScDPObject& r);
124 : : ~ScDPObject();
125 : :
126 : : void EnableGetPivotData(bool b);
127 : :
128 : : /**
129 : : * When a DP object is "alive", it has table output on a sheet. This flag
130 : : * doesn't really change the behavior of the object, but is used only for
131 : : * testing purposes.
132 : : */
133 : : void SetAlive(bool bSet);
134 : : void SetAllowMove(bool bSet);
135 : :
136 : : void InvalidateData();
137 : : void ClearTableData();
138 : : void ReloadGroupTableData();
139 : :
140 : : void Output( const ScAddress& rPos );
141 : : ScRange GetNewOutputRange( bool& rOverflow );
142 : : const ScRange GetOutputRangeByType( sal_Int32 nType );
143 : :
144 : : void SetSaveData(const ScDPSaveData& rData);
145 : 518 : ScDPSaveData* GetSaveData() const { return pSaveData; }
146 : :
147 : : void SetOutRange(const ScRange& rRange);
148 : 2183 : const ScRange& GetOutRange() const { return aOutRange; }
149 : :
150 : : void SetHeaderLayout(bool bUseGrid);
151 : : bool GetHeaderLayout() const;
152 : :
153 : : void SetSheetDesc(const ScSheetSourceDesc& rDesc);
154 : : void SetImportDesc(const ScImportSourceDesc& rDesc);
155 : : void SetServiceData(const ScDPServiceDesc& rDesc);
156 : :
157 : : void WriteSourceDataTo( ScDPObject& rDest ) const;
158 : : void WriteTempDataTo( ScDPObject& rDest ) const;
159 : :
160 : 149 : const ScSheetSourceDesc* GetSheetDesc() const { return pSheetDesc; }
161 : 0 : const ScImportSourceDesc* GetImportSourceDesc() const { return pImpDesc; }
162 : 0 : const ScDPServiceDesc* GetDPServiceDesc() const { return pServDesc; }
163 : :
164 : : com::sun::star::uno::Reference<com::sun::star::sheet::XDimensionsSupplier> GetSource();
165 : :
166 : : bool IsSheetData() const;
167 : 0 : bool IsImportData() const { return(pImpDesc != NULL); }
168 : 0 : bool IsServiceData() const { return(pServDesc != NULL); }
169 : :
170 : : void SetName(const ::rtl::OUString& rNew);
171 : 1901 : const ::rtl::OUString& GetName() const { return aTableName; }
172 : : void SetTag(const ::rtl::OUString& rNew);
173 : 23 : const ::rtl::OUString& GetTag() const { return aTableTag; }
174 : :
175 : : /**
176 : : * Data description cell displays the description of a data dimension if
177 : : * and only if there is only one data dimension. It's usually located at
178 : : * the upper-left corner of the table output.
179 : : */
180 : : bool IsDataDescriptionCell(const ScAddress& rPos);
181 : :
182 : : bool IsDimNameInUse(const ::rtl::OUString& rName) const;
183 : : ::rtl::OUString GetDimName( long nDim, bool& rIsDataLayout, sal_Int32* pFlags = NULL );
184 : : bool IsDuplicated( long nDim );
185 : : long GetDimCount();
186 : : void GetHeaderPositionData(const ScAddress& rPos, ::com::sun::star::sheet::DataPilotTableHeaderData& rData);
187 : : long GetHeaderDim( const ScAddress& rPos, sal_uInt16& rOrient );
188 : : bool GetHeaderDrag( const ScAddress& rPos, bool bMouseLeft, bool bMouseTop,
189 : : long nDragDim,
190 : : Rectangle& rPosRect, sal_uInt16& rOrient, long& rDimPos );
191 : : bool IsFilterButton( const ScAddress& rPos );
192 : :
193 : : bool GetPivotData( ScDPGetPivotDataField& rTarget, /* returns result */
194 : : const std::vector< ScDPGetPivotDataField >& rFilters );
195 : : bool ParseFilters( ScDPGetPivotDataField& rTarget,
196 : : std::vector< ScDPGetPivotDataField >& rFilters,
197 : : const ::rtl::OUString& rFilterList );
198 : :
199 : : void GetMemberResultNames(ScDPUniqueStringSet& rNames, long nDimension);
200 : :
201 : : void FillPageList( std::vector<rtl::OUString>& rStrings, long nField );
202 : :
203 : : void ToggleDetails(const ::com::sun::star::sheet::DataPilotTableHeaderData& rElemDesc, ScDPObject* pDestObj);
204 : :
205 : : bool FillOldParam(ScPivotParam& rParam) const;
206 : : bool FillLabelData(sal_Int32 nDim, ScDPLabelData& Labels);
207 : : bool FillLabelData(ScPivotParam& rParam);
208 : :
209 : : bool GetHierarchiesNA( sal_Int32 nDim, com::sun::star::uno::Reference< com::sun::star::container::XNameAccess >& xHiers );
210 : : bool GetHierarchies( sal_Int32 nDim, com::sun::star::uno::Sequence< rtl::OUString >& rHiers );
211 : :
212 : : sal_Int32 GetUsedHierarchy( sal_Int32 nDim );
213 : :
214 : : bool GetMembersNA( sal_Int32 nDim, com::sun::star::uno::Reference< com::sun::star::container::XNameAccess >& xMembers );
215 : : bool GetMembersNA( sal_Int32 nDim, sal_Int32 nHier, com::sun::star::uno::Reference< com::sun::star::container::XNameAccess >& xMembers );
216 : :
217 : : bool GetMemberNames( sal_Int32 nDim, ::com::sun::star::uno::Sequence< ::rtl::OUString >& rNames );
218 : : bool GetMembers( sal_Int32 nDim, sal_Int32 nHier, ::std::vector<ScDPLabelData::Member>& rMembers );
219 : :
220 : : void UpdateReference( UpdateRefMode eUpdateRefMode,
221 : : const ScRange& r, SCsCOL nDx, SCsROW nDy, SCsTAB nDz );
222 : : bool RefsEqual( const ScDPObject& r ) const;
223 : : void WriteRefsTo( ScDPObject& r ) const;
224 : :
225 : : void GetPositionData(const ScAddress& rPos, ::com::sun::star::sheet::DataPilotTablePositionData& rPosData);
226 : :
227 : : bool GetDataFieldPositionData(const ScAddress& rPos,
228 : : ::com::sun::star::uno::Sequence<
229 : : ::com::sun::star::sheet::DataPilotFieldFilter >& rFilters);
230 : :
231 : : void GetDrillDownData(const ScAddress& rPos,
232 : : ::com::sun::star::uno::Sequence<
233 : : ::com::sun::star::uno::Sequence<
234 : : ::com::sun::star::uno::Any > >& rTableData);
235 : :
236 : : // apply drop-down attribute, initialize nHeaderRows, without accessing the source
237 : : // (button attribute must be present)
238 : : void RefreshAfterLoad();
239 : :
240 : : void BuildAllDimensionMembers();
241 : :
242 : : /**
243 : : * Remove in the save data entries for members that don't exist anymore.
244 : : * This is called during pivot table refresh.
245 : : */
246 : : void SyncAllDimensionMembers();
247 : :
248 : : static bool HasRegisteredSources();
249 : : static com::sun::star::uno::Sequence<rtl::OUString> GetRegisteredSources();
250 : : static com::sun::star::uno::Reference<com::sun::star::sheet::XDimensionsSupplier>
251 : : CreateSource( const ScDPServiceDesc& rDesc );
252 : :
253 : : static void ConvertOrientation(
254 : : ScDPSaveData& rSaveData,
255 : : const ::std::vector<PivotField>& rFields, sal_uInt16 nOrient,
256 : : const com::sun::star::uno::Reference<
257 : : com::sun::star::sheet::XDimensionsSupplier>& xSource,
258 : : const ScDPLabelDataVec& rLabels,
259 : : std::vector<PivotField>* pRefColFields = NULL,
260 : : std::vector<PivotField>* pRefRowFields = NULL,
261 : : std::vector<PivotField>* pRefPageFields = NULL );
262 : :
263 : : static bool IsOrientationAllowed( sal_uInt16 nOrient, sal_Int32 nDimFlags );
264 : :
265 : : #if DEBUG_PIVOT_TABLE
266 : : void DumpCache() const;
267 : : #endif
268 : : };
269 : :
270 : :
271 : : class ScDPCollection
272 : : {
273 : : friend class ScDPCache;
274 : : public:
275 : :
276 : : /**
277 : : * Stores and manages all caches from internal sheets.
278 : : */
279 : 142 : class SheetCaches
280 : : {
281 : : friend class ScDPCollection;
282 : : typedef boost::ptr_map<size_t, ScDPCache> CachesType;
283 : : typedef std::vector<ScRange> RangeIndexType;
284 : : CachesType maCaches;
285 : : RangeIndexType maRanges;
286 : : ScDocument* mpDoc;
287 : : public:
288 : : SheetCaches(ScDocument* pDoc);
289 : : bool hasCache(const ScRange& rRange) const;
290 : : const ScDPCache* getCache(const ScRange& rRange, const ScDPDimensionSaveData* pDimData);
291 : : size_t size() const;
292 : :
293 : : void updateReference(
294 : : UpdateRefMode eMode, const ScRange& r, SCsCOL nDx, SCsROW nDy, SCsTAB nDz);
295 : :
296 : : private:
297 : : ScDPCache* getExistingCache(const ScRange& rRange);
298 : :
299 : : void updateCache(const ScRange& rRange, const ScDPDimensionSaveData* pDimData, std::set<ScDPObject*>& rRefs);
300 : : bool remove(const ScDPCache* p);
301 : : };
302 : :
303 : : /**
304 : : * Data caches for range name based source data.
305 : : */
306 : 142 : class NameCaches
307 : : {
308 : : friend class ScDPCollection;
309 : : typedef ::boost::ptr_map<rtl::OUString, ScDPCache> CachesType;
310 : : CachesType maCaches;
311 : : ScDocument* mpDoc;
312 : : public:
313 : : NameCaches(ScDocument* pDoc);
314 : : bool hasCache(const rtl::OUString& rName) const;
315 : : const ScDPCache* getCache(
316 : : const ::rtl::OUString& rName, const ScRange& rRange, const ScDPDimensionSaveData* pDimData);
317 : : size_t size() const;
318 : : private:
319 : : ScDPCache* getExistingCache(const rtl::OUString& rName);
320 : :
321 : : void updateCache(
322 : : const rtl::OUString& rName, const ScRange& rRange,
323 : : const ScDPDimensionSaveData* pDimData, std::set<ScDPObject*>& rRefs);
324 : : bool remove(const ScDPCache* p);
325 : : };
326 : :
327 : : /**
328 : : * Defines connection type to external data source. Used as a key to look
329 : : * up database cache.
330 : : */
331 : 0 : struct DBType
332 : : {
333 : : sal_Int32 mnSdbType;
334 : : ::rtl::OUString maDBName;
335 : : ::rtl::OUString maCommand;
336 : : DBType(sal_Int32 nSdbType, const ::rtl::OUString& rDBName, const ::rtl::OUString& rCommand);
337 : :
338 : : struct less : public ::std::binary_function<DBType, DBType, bool>
339 : : {
340 : : bool operator() (const DBType& left, const DBType& right) const;
341 : : };
342 : : };
343 : :
344 : : /**
345 : : * Data caches for external database sources.
346 : : */
347 : 142 : class DBCaches
348 : : {
349 : : friend class ScDPCollection;
350 : : typedef ::boost::ptr_map<DBType, ScDPCache, DBType::less> CachesType;
351 : : CachesType maCaches;
352 : : ScDocument* mpDoc;
353 : : public:
354 : : DBCaches(ScDocument* pDoc);
355 : : bool hasCache(sal_Int32 nSdbType, const rtl::OUString& rDBName, const rtl::OUString& rCommand) const;
356 : : const ScDPCache* getCache(
357 : : sal_Int32 nSdbType, const ::rtl::OUString& rDBName, const ::rtl::OUString& rCommand,
358 : : const ScDPDimensionSaveData* pDimData);
359 : :
360 : : private:
361 : : ScDPCache* getExistingCache(
362 : : sal_Int32 nSdbType, const ::rtl::OUString& rDBName, const ::rtl::OUString& rCommand);
363 : :
364 : : com::sun::star::uno::Reference<com::sun::star::sdbc::XRowSet> createRowSet(
365 : : sal_Int32 nSdbType, const ::rtl::OUString& rDBName, const ::rtl::OUString& rCommand);
366 : :
367 : : void updateCache(sal_Int32 nSdbType, const ::rtl::OUString& rDBName, const ::rtl::OUString& rCommand,
368 : : const ScDPDimensionSaveData* pDimData, std::set<ScDPObject*>& rRefs);
369 : : bool remove(const ScDPCache* p);
370 : : };
371 : :
372 : : ScDPCollection(ScDocument* pDocument);
373 : : ScDPCollection(const ScDPCollection& r);
374 : : ~ScDPCollection();
375 : :
376 : : sal_uLong ReloadCache(ScDPObject* pDPObj, std::set<ScDPObject*>& rRefs);
377 : : bool ReloadGroupsInCache(ScDPObject* pDPObj, std::set<ScDPObject*>& rRefs);
378 : :
379 : : SC_DLLPUBLIC size_t GetCount() const;
380 : : SC_DLLPUBLIC ScDPObject* operator[](size_t nIndex);
381 : : SC_DLLPUBLIC const ScDPObject* operator[](size_t nIndex) const;
382 : :
383 : : const ScDPObject* GetByName(const ::rtl::OUString& rName) const;
384 : :
385 : : void DeleteOnTab( SCTAB nTab );
386 : : void UpdateReference( UpdateRefMode eUpdateRefMode,
387 : : const ScRange& r, SCsCOL nDx, SCsROW nDy, SCsTAB nDz );
388 : : void CopyToTab( SCTAB nOld, SCTAB nNew );
389 : : bool RefsEqual( const ScDPCollection& r ) const;
390 : : void WriteRefsTo( ScDPCollection& r ) const;
391 : :
392 : : /**
393 : : * Create a new name that's not yet used by any existing data pilot
394 : : * objects. All data pilot names are 'DataPilot' + <num>, and the nMin
395 : : * specifies the minimum number allowed.
396 : : *
397 : : * @param nMin minimum number allowed.
398 : : *
399 : : * @return new name for data pilot object.
400 : : */
401 : : ::rtl::OUString CreateNewName( sal_uInt16 nMin = 1 ) const;
402 : :
403 : : void FreeTable(ScDPObject* pDPObj);
404 : : SC_DLLPUBLIC bool InsertNewTable(ScDPObject* pDPObj);
405 : :
406 : : bool HasDPTable(SCCOL nCol, SCROW nRow, SCTAB nTab) const;
407 : :
408 : : SheetCaches& GetSheetCaches();
409 : : NameCaches& GetNameCaches();
410 : : DBCaches& GetDBCaches();
411 : :
412 : : private:
413 : : /** Only to be called from ScDPCache::RemoveReference(). */
414 : : void RemoveCache(const ScDPCache* pCache);
415 : :
416 : : void GetAllTables(const ScRange& rSrcRange, std::set<ScDPObject*>& rRefs) const;
417 : : void GetAllTables(const rtl::OUString& rSrcName, std::set<ScDPObject*>& rRefs) const;
418 : : void GetAllTables(
419 : : sal_Int32 nSdbType, const ::rtl::OUString& rDBName, const ::rtl::OUString& rCommand,
420 : : std::set<ScDPObject*>& rRefs) const;
421 : :
422 : : private:
423 : : typedef ::boost::ptr_vector<ScDPObject> TablesType;
424 : :
425 : : ScDocument* mpDoc;
426 : : TablesType maTables;
427 : : SheetCaches maSheetCaches;
428 : : NameCaches maNameCaches;
429 : : DBCaches maDBCaches;
430 : : };
431 : :
432 : : bool operator<(const ScDPCollection::DBType& left, const ScDPCollection::DBType& right);
433 : :
434 : : #endif
435 : :
436 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|