1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <dpgroup.hxx>
#include <dpcache.hxx>
#include <document.hxx>
#include <dpfilteredcache.hxx>
#include <dputil.hxx>
#include <osl/diagnose.h>
#include <rtl/math.hxx>
#include <svl/numformat.hxx>
#include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <algorithm>
using namespace ::com::sun::star;
using ::com::sun::star::uno::Any;
using ::com::sun::star::uno::Sequence;
using ::std::vector;
using ::std::shared_ptr;
const sal_uInt16 SC_DP_LEAPYEAR = 1648; // arbitrary leap year for date calculations
namespace {
class ScDPGroupNumFilter : public ScDPFilteredCache::FilterBase
{
public:
ScDPGroupNumFilter(std::vector<ScDPItemData>&& rValues, const ScDPNumGroupInfo& rInfo);
virtual bool match(const ScDPItemData &rCellData) const override;
virtual std::vector<ScDPItemData> getMatchValues() const override;
private:
std::vector<ScDPItemData> maValues;
ScDPNumGroupInfo maNumInfo;
};
}
ScDPGroupNumFilter::ScDPGroupNumFilter( std::vector<ScDPItemData>&& rValues, const ScDPNumGroupInfo& rInfo) :
maValues(std::move(rValues)), maNumInfo(rInfo) {}
bool ScDPGroupNumFilter::match(const ScDPItemData& rCellData) const
{
if (rCellData.GetType() != ScDPItemData::Value)
return false;
for (const auto& rValue : maValues)
{
double fVal = rValue.GetValue();
if (std::isinf(fVal))
{
if (std::signbit(fVal))
{
// Less than the min value.
if (rCellData.GetValue() < maNumInfo.mfStart)
return true;
}
// Greater than the max value.
if (maNumInfo.mfEnd < rCellData.GetValue())
return true;
continue;
}
double low = fVal;
double high = low + maNumInfo.mfStep;
if (maNumInfo.mbIntegerOnly)
high += 1.0;
if (low <= rCellData.GetValue() && rCellData.GetValue() < high)
return true;
}
return false;
}
std::vector<ScDPItemData> ScDPGroupNumFilter::getMatchValues() const
{
return std::vector<ScDPItemData>();
}
namespace {
class ScDPGroupDateFilter : public ScDPFilteredCache::FilterBase
{
public:
ScDPGroupDateFilter(
std::vector<ScDPItemData>&& rValues, const Date& rNullDate, const ScDPNumGroupInfo& rNumInfo);
virtual bool match(const ScDPItemData & rCellData) const override;
virtual std::vector<ScDPItemData> getMatchValues() const override;
private:
std::vector<ScDPItemData> maValues;
Date maNullDate;
ScDPNumGroupInfo maNumInfo;
};
}
ScDPGroupDateFilter::ScDPGroupDateFilter(
std::vector<ScDPItemData>&& rValues, const Date& rNullDate, const ScDPNumGroupInfo& rNumInfo) :
maValues(std::move(rValues)),
maNullDate(rNullDate),
maNumInfo(rNumInfo)
{
}
bool ScDPGroupDateFilter::match( const ScDPItemData & rCellData ) const
{
using namespace ::com::sun::star::sheet;
using ::rtl::math::approxFloor;
using ::rtl::math::approxEqual;
if ( !rCellData.IsValue() )
return false;
for (const ScDPItemData& rValue : maValues)
{
if (rValue.GetType() != ScDPItemData::GroupValue)
continue;
sal_Int32 nGroupType = rValue.GetGroupValue().mnGroupType;
sal_Int32 nValue = rValue.GetGroupValue().mnValue;
// Start and end dates are inclusive. (An end date without a time value
// is included, while an end date with a time value is not.)
if (rCellData.GetValue() < maNumInfo.mfStart && !approxEqual(rCellData.GetValue(), maNumInfo.mfStart))
{
if (nValue == ScDPItemData::DateFirst)
return true;
continue;
}
if (rCellData.GetValue() > maNumInfo.mfEnd && !approxEqual(rCellData.GetValue(), maNumInfo.mfEnd))
{
if (nValue == ScDPItemData::DateLast)
return true;
continue;
}
if (nGroupType == DataPilotFieldGroupBy::HOURS || nGroupType == DataPilotFieldGroupBy::MINUTES ||
nGroupType == DataPilotFieldGroupBy::SECONDS)
{
// handle time
// (do as in the cell functions, ScInterpreter::ScGetHour() etc.)
sal_uInt16 nHour, nMinute, nSecond;
double fFractionOfSecond;
tools::Time::GetClock( rCellData.GetValue(), nHour, nMinute, nSecond, fFractionOfSecond, 0);
switch (nGroupType)
{
case DataPilotFieldGroupBy::HOURS:
{
if (nHour == nValue)
return true;
}
break;
case DataPilotFieldGroupBy::MINUTES:
{
if (nMinute == nValue)
return true;
}
break;
case DataPilotFieldGroupBy::SECONDS:
{
if (nSecond == nValue)
return true;
}
break;
default:
OSL_FAIL("invalid time part");
}
continue;
}
Date date = maNullDate + static_cast<sal_Int32>(approxFloor(rCellData.GetValue()));
switch (nGroupType)
{
case DataPilotFieldGroupBy::YEARS:
{
sal_Int32 year = static_cast<sal_Int32>(date.GetYear());
if (year == nValue)
return true;
}
break;
case DataPilotFieldGroupBy::QUARTERS:
{
sal_Int32 qtr = 1 + (static_cast<sal_Int32>(date.GetMonth()) - 1) / 3;
if (qtr == nValue)
return true;
}
break;
case DataPilotFieldGroupBy::MONTHS:
{
sal_Int32 month = static_cast<sal_Int32>(date.GetMonth());
if (month == nValue)
return true;
}
break;
case DataPilotFieldGroupBy::DAYS:
{
Date yearStart(1, 1, date.GetYear());
sal_Int32 days = (date - yearStart) + 1; // Jan 01 has value 1
if (days >= 60 && !date.IsLeapYear())
{
// This is not a leap year. Adjust the value accordingly.
++days;
}
if (days == nValue)
return true;
}
break;
default:
OSL_FAIL("invalid date part");
}
}
return false;
}
std::vector<ScDPItemData> ScDPGroupDateFilter::getMatchValues() const
{
return std::vector<ScDPItemData>();
}
namespace {
bool isDateInGroup(const ScDPItemData& rGroupItem, const ScDPItemData& rChildItem)
{
if (rGroupItem.GetType() != ScDPItemData::GroupValue || rChildItem.GetType() != ScDPItemData::GroupValue)
return false;
sal_Int32 nGroupPart = rGroupItem.GetGroupValue().mnGroupType;
sal_Int32 nGroupValue = rGroupItem.GetGroupValue().mnValue;
sal_Int32 nChildPart = rChildItem.GetGroupValue().mnGroupType;
sal_Int32 nChildValue = rChildItem.GetGroupValue().mnValue;
if (nGroupValue == ScDPItemData::DateFirst || nGroupValue == ScDPItemData::DateLast ||
nChildValue == ScDPItemData::DateFirst || nChildValue == ScDPItemData::DateLast)
{
// first/last entry matches only itself
return nGroupValue == nChildValue;
}
switch (nChildPart) // inner part
{
case css::sheet::DataPilotFieldGroupBy::MONTHS:
// a month is only contained in its quarter
if (nGroupPart == css::sheet::DataPilotFieldGroupBy::QUARTERS)
// months and quarters are both 1-based
return (nGroupValue - 1 == (nChildValue - 1) / 3);
break;
case css::sheet::DataPilotFieldGroupBy::DAYS:
// a day is only contained in its quarter or month
if (nGroupPart == css::sheet::DataPilotFieldGroupBy::MONTHS ||
nGroupPart == css::sheet::DataPilotFieldGroupBy::QUARTERS)
{
Date aDate(1, 1, SC_DP_LEAPYEAR);
aDate.AddDays(nChildValue - 1); // days are 1-based
sal_Int32 nCompare = aDate.GetMonth();
if (nGroupPart == css::sheet::DataPilotFieldGroupBy::QUARTERS)
nCompare = ( ( nCompare - 1 ) / 3 ) + 1; // get quarter from date
return nGroupValue == nCompare;
}
break;
default:
;
}
return true;
}
}
ScDPGroupItem::ScDPGroupItem( const ScDPItemData& rName ) :
aGroupName( rName )
{
}
void ScDPGroupItem::AddElement( const ScDPItemData& rName )
{
aElements.push_back( rName );
}
bool ScDPGroupItem::HasElement( const ScDPItemData& rData ) const
{
return std::any_of(aElements.begin(), aElements.end(),
[&rData](const ScDPItemData& rElement) { return rElement.IsCaseInsEqual(rData); });
}
bool ScDPGroupItem::HasCommonElement( const ScDPGroupItem& rOther ) const
{
return std::any_of(aElements.begin(), aElements.end(),
[&rOther](const ScDPItemData& rElement) { return rOther.HasElement(rElement); });
}
void ScDPGroupItem::FillGroupFilter( ScDPFilteredCache::GroupFilter& rFilter ) const
{
for (const auto& rElement : aElements)
rFilter.addMatchItem(rElement);
}
ScDPGroupDimension::ScDPGroupDimension( tools::Long nSource, const OUString& rNewName ) :
nSourceDim( nSource ),
nGroupDim( -1 ),
aGroupName( rNewName ),
mbDateDimension(false)
{
}
ScDPGroupDimension::~ScDPGroupDimension()
{
maMemberEntries.clear();
}
ScDPGroupDimension::ScDPGroupDimension( const ScDPGroupDimension& rOther ) :
nSourceDim( rOther.nSourceDim ),
nGroupDim( rOther.nGroupDim ),
aGroupName( rOther.aGroupName ),
aItems( rOther.aItems ),
mbDateDimension(rOther.mbDateDimension)
{
}
ScDPGroupDimension& ScDPGroupDimension::operator=( const ScDPGroupDimension& rOther )
{
nSourceDim = rOther.nSourceDim;
nGroupDim = rOther.nGroupDim;
aGroupName = rOther.aGroupName;
aItems = rOther.aItems;
mbDateDimension = rOther.mbDateDimension;
return *this;
}
void ScDPGroupDimension::AddItem( const ScDPGroupItem& rItem )
{
aItems.push_back( rItem );
}
void ScDPGroupDimension::SetGroupDim( tools::Long nDim )
{
nGroupDim = nDim;
}
const std::vector<SCROW>& ScDPGroupDimension::GetColumnEntries(
const ScDPFilteredCache& rCacheTable) const
{
if (!maMemberEntries.empty())
return maMemberEntries;
rCacheTable.getCache().GetGroupDimMemberIds(nGroupDim, maMemberEntries);
return maMemberEntries;
}
const ScDPGroupItem* ScDPGroupDimension::GetGroupForData( const ScDPItemData& rData ) const
{
auto aIter = std::find_if(aItems.begin(), aItems.end(),
[&rData](const ScDPGroupItem& rItem) { return rItem.HasElement(rData); });
if (aIter != aItems.end())
return &*aIter;
return nullptr;
}
const ScDPGroupItem* ScDPGroupDimension::GetGroupForName( const ScDPItemData& rName ) const
{
auto aIter = std::find_if(aItems.begin(), aItems.end(),
[&rName](const ScDPGroupItem& rItem) { return rItem.GetName().IsCaseInsEqual(rName); });
if (aIter != aItems.end())
return &*aIter;
return nullptr;
}
const ScDPGroupItem* ScDPGroupDimension::GetGroupByIndex( size_t nIndex ) const
{
if (nIndex >= aItems.size())
return nullptr;
return &aItems[nIndex];
}
void ScDPGroupDimension::DisposeData()
{
maMemberEntries.clear();
}
void ScDPGroupDimension::SetDateDimension()
{
mbDateDimension = true;
}
ScDPNumGroupDimension::ScDPNumGroupDimension() : mbDateDimension(false) {}
ScDPNumGroupDimension::ScDPNumGroupDimension( const ScDPNumGroupInfo& rInfo ) :
aGroupInfo(rInfo), mbDateDimension(false) {}
ScDPNumGroupDimension::ScDPNumGroupDimension( const ScDPNumGroupDimension& rOther ) :
aGroupInfo(rOther.aGroupInfo), mbDateDimension(rOther.mbDateDimension) {}
ScDPNumGroupDimension& ScDPNumGroupDimension::operator=( const ScDPNumGroupDimension& rOther )
{
aGroupInfo = rOther.aGroupInfo;
mbDateDimension = rOther.mbDateDimension;
return *this;
}
void ScDPNumGroupDimension::DisposeData()
{
aGroupInfo = ScDPNumGroupInfo();
maMemberEntries.clear();
}
ScDPNumGroupDimension::~ScDPNumGroupDimension()
{
}
void ScDPNumGroupDimension::SetDateDimension()
{
aGroupInfo.mbEnable = true; //TODO: or query both?
mbDateDimension = true;
}
const std::vector<SCROW>& ScDPNumGroupDimension::GetNumEntries(
SCCOL nSourceDim, const ScDPCache* pCache) const
{
if (!maMemberEntries.empty())
return maMemberEntries;
pCache->GetGroupDimMemberIds(nSourceDim, maMemberEntries);
return maMemberEntries;
}
ScDPGroupTableData::ScDPGroupTableData( const shared_ptr<ScDPTableData>& pSource, ScDocument* pDocument ) :
ScDPTableData(pDocument),
pSourceData( pSource ),
pDoc( pDocument )
{
OSL_ENSURE( pSource, "ScDPGroupTableData: pSource can't be NULL" );
CreateCacheTable();
nSourceCount = pSource->GetColumnCount(); // real columns, excluding data layout
pNumGroups.reset( new ScDPNumGroupDimension[nSourceCount] );
}
ScDPGroupTableData::~ScDPGroupTableData()
{
}
void ScDPGroupTableData::AddGroupDimension( const ScDPGroupDimension& rGroup )
{
ScDPGroupDimension aNewGroup( rGroup );
aNewGroup.SetGroupDim( GetColumnCount() ); // new dimension will be at the end
aGroups.push_back( aNewGroup );
}
void ScDPGroupTableData::SetNumGroupDimension( sal_Int32 nIndex, const ScDPNumGroupDimension& rGroup )
{
if ( nIndex < nSourceCount )
{
pNumGroups[nIndex] = rGroup;
// automatic minimum / maximum is handled in GetNumEntries
}
}
sal_Int32 ScDPGroupTableData::GetDimensionIndex( std::u16string_view rName )
{
for (tools::Long i = 0; i < nSourceCount; ++i) // nSourceCount excludes data layout
if (pSourceData->getDimensionName(i) == rName) //TODO: ignore case?
return i;
return -1; // none
}
sal_Int32 ScDPGroupTableData::GetColumnCount()
{
return nSourceCount + aGroups.size();
}
bool ScDPGroupTableData::IsNumGroupDimension( tools::Long nDimension ) const
{
return ( nDimension < nSourceCount && pNumGroups[nDimension].GetInfo().mbEnable );
}
void ScDPGroupTableData::GetNumGroupInfo(tools::Long nDimension, ScDPNumGroupInfo& rInfo)
{
if ( nDimension < nSourceCount )
rInfo = pNumGroups[nDimension].GetInfo();
}
sal_Int32 ScDPGroupTableData::GetMembersCount( sal_Int32 nDim )
{
const std::vector< SCROW >& members = GetColumnEntries( nDim );
return members.size();
}
const std::vector< SCROW >& ScDPGroupTableData::GetColumnEntries( sal_Int32 nColumn )
{
if ( nColumn >= nSourceCount )
{
if ( getIsDataLayoutDimension( nColumn) ) // data layout dimension?
nColumn = nSourceCount; // index of data layout in source data
else
{
const ScDPGroupDimension& rGroupDim = aGroups[nColumn - nSourceCount];
return rGroupDim.GetColumnEntries( GetCacheTable() );
}
}
if ( IsNumGroupDimension( nColumn ) )
{
// dimension number is unchanged for numerical groups
return pNumGroups[nColumn].GetNumEntries(
static_cast<SCCOL>(nColumn), &GetCacheTable().getCache());
}
return pSourceData->GetColumnEntries( nColumn );
}
const ScDPItemData* ScDPGroupTableData::GetMemberById( sal_Int32 nDim, sal_Int32 nId )
{
return pSourceData->GetMemberById( nDim, nId );
}
OUString ScDPGroupTableData::getDimensionName(sal_Int32 nColumn)
{
if ( nColumn >= nSourceCount )
{
if ( nColumn == sal::static_int_cast<tools::Long>( nSourceCount + aGroups.size() ) ) // data layout dimension?
nColumn = nSourceCount; // index of data layout in source data
else
return aGroups[nColumn - nSourceCount].GetName();
}
return pSourceData->getDimensionName( nColumn );
}
bool ScDPGroupTableData::getIsDataLayoutDimension(sal_Int32 nColumn)
{
// position of data layout dimension is moved from source data
return ( nColumn == sal::static_int_cast<tools::Long>( nSourceCount + aGroups.size() ) ); // data layout dimension?
}
bool ScDPGroupTableData::IsDateDimension(sal_Int32 nDim)
{
if ( nDim >= nSourceCount )
{
if ( nDim == sal::static_int_cast<tools::Long>( nSourceCount + aGroups.size() ) ) // data layout dimension?
nDim = nSourceCount; // index of data layout in source data
else
nDim = aGroups[nDim - nSourceCount].GetSourceDim(); // look at original dimension
}
return pSourceData->IsDateDimension( nDim );
}
sal_uInt32 ScDPGroupTableData::GetNumberFormat(sal_Int32 nDim)
{
if ( nDim >= nSourceCount )
{
if ( nDim == sal::static_int_cast<tools::Long>( nSourceCount + aGroups.size() ) ) // data layout dimension?
nDim = nSourceCount; // index of data layout in source data
else
nDim = aGroups[nDim - nSourceCount].GetSourceDim(); // look at original dimension
}
return pSourceData->GetNumberFormat( nDim );
}
void ScDPGroupTableData::DisposeData()
{
for ( auto& rGroup : aGroups )
rGroup.DisposeData();
for ( tools::Long i=0; i<nSourceCount; i++ )
pNumGroups[i].DisposeData();
pSourceData->DisposeData();
}
void ScDPGroupTableData::SetEmptyFlags( bool bIgnoreEmptyRows, bool bRepeatIfEmpty )
{
pSourceData->SetEmptyFlags( bIgnoreEmptyRows, bRepeatIfEmpty );
}
bool ScDPGroupTableData::IsRepeatIfEmpty()
{
return pSourceData->IsRepeatIfEmpty();
}
void ScDPGroupTableData::CreateCacheTable()
{
pSourceData->CreateCacheTable();
}
namespace {
class FindCaseInsensitive
{
ScDPItemData maValue;
public:
explicit FindCaseInsensitive(const ScDPItemData& rVal) : maValue(rVal) {}
bool operator() (const ScDPItemData& rItem) const
{
return maValue.IsCaseInsEqual(rItem);
}
};
}
void ScDPGroupTableData::ModifyFilterCriteria(vector<ScDPFilteredCache::Criterion>& rCriteria)
{
// Build dimension ID to object map for group dimensions.
typedef std::unordered_map<tools::Long, const ScDPGroupDimension*> GroupFieldMapType;
GroupFieldMapType aGroupFieldIds;
for (const auto& rGroup : aGroups)
{
aGroupFieldIds.emplace(rGroup.GetGroupDim(), &rGroup);
}
vector<ScDPFilteredCache::Criterion> aNewCriteria;
aNewCriteria.reserve(rCriteria.size() + aGroups.size());
// Go through all the filtered field names and process them appropriately.
const ScDPCache& rCache = GetCacheTable().getCache();
GroupFieldMapType::const_iterator itrGrpEnd = aGroupFieldIds.end();
for (const auto& rCriterion : rCriteria)
{
std::vector<ScDPItemData> aMatchValues = rCriterion.mpFilter->getMatchValues();
GroupFieldMapType::const_iterator itrGrp = aGroupFieldIds.find(rCriterion.mnFieldIndex);
if (itrGrp == itrGrpEnd)
{
if (IsNumGroupDimension(rCriterion.mnFieldIndex))
{
// internal number group field
const ScDPNumGroupInfo* pNumInfo = rCache.GetNumGroupInfo(rCriterion.mnFieldIndex);
if (!pNumInfo)
// Number group dimension without num info? Something is wrong...
continue;
ScDPFilteredCache::Criterion aCri;
aCri.mnFieldIndex = rCriterion.mnFieldIndex;
const ScDPNumGroupDimension& rNumGrpDim = pNumGroups[rCriterion.mnFieldIndex];
if (rNumGrpDim.IsDateDimension())
{
// grouped by dates.
aCri.mpFilter =
std::make_shared<ScDPGroupDateFilter>(
std::move(aMatchValues), pDoc->GetFormatTable()->GetNullDate(), *pNumInfo);
}
else
{
// This dimension is grouped by numeric ranges.
aCri.mpFilter =
std::make_shared<ScDPGroupNumFilter>(std::move(aMatchValues), *pNumInfo);
}
aNewCriteria.push_back(aCri);
}
else
{
// This is a regular source field.
aNewCriteria.push_back(rCriterion);
}
}
else
{
// This is an ordinary group field or external number group field.
const ScDPGroupDimension* pGrpDim = itrGrp->second;
tools::Long nSrcDim = pGrpDim->GetSourceDim();
tools::Long nGrpDim = pGrpDim->GetGroupDim();
const ScDPNumGroupInfo* pNumInfo = rCache.GetNumGroupInfo(nGrpDim);
if (pGrpDim->IsDateDimension() && pNumInfo)
{
// external number group
ScDPFilteredCache::Criterion aCri;
aCri.mnFieldIndex = nSrcDim; // use the source dimension, not the group dimension.
aCri.mpFilter =
std::make_shared<ScDPGroupDateFilter>(
std::move(aMatchValues), pDoc->GetFormatTable()->GetNullDate(), *pNumInfo);
aNewCriteria.push_back(aCri);
}
else
{
// normal group
ScDPFilteredCache::Criterion aCri;
aCri.mnFieldIndex = nSrcDim;
aCri.mpFilter = std::make_shared<ScDPFilteredCache::GroupFilter>();
ScDPFilteredCache::GroupFilter* pGrpFilter =
static_cast<ScDPFilteredCache::GroupFilter*>(aCri.mpFilter.get());
size_t nGroupItemCount = pGrpDim->GetItemCount();
for (size_t i = 0; i < nGroupItemCount; ++i)
{
const ScDPGroupItem* pGrpItem = pGrpDim->GetGroupByIndex(i);
if (!pGrpItem)
continue;
// Make sure this group name equals one of the match values.
if (std::none_of(aMatchValues.begin(), aMatchValues.end(), FindCaseInsensitive(pGrpItem->GetName())))
continue;
pGrpItem->FillGroupFilter(*pGrpFilter);
}
aNewCriteria.push_back(aCri);
}
}
}
rCriteria.swap(aNewCriteria);
}
void ScDPGroupTableData::FilterCacheTable(std::vector<ScDPFilteredCache::Criterion>&& rCriteria, std::unordered_set<sal_Int32>&& rCatDims)
{
ModifyFilterCriteria(rCriteria);
pSourceData->FilterCacheTable(std::move(rCriteria), std::move(rCatDims));
}
void ScDPGroupTableData::GetDrillDownData(std::vector<ScDPFilteredCache::Criterion>&& rCriteria, std::unordered_set<sal_Int32>&&rCatDims, Sequence< Sequence<Any> >& rData)
{
ModifyFilterCriteria(rCriteria);
pSourceData->GetDrillDownData(std::move(rCriteria), std::move(rCatDims), rData);
}
void ScDPGroupTableData::CalcResults(CalcInfo& rInfo, bool bAutoShow)
{
// #i111435# Inside FillRowDataFromCacheTable/GetItemData, virtual methods
// getIsDataLayoutDimension and GetSourceDim are used, so it has to be called
// with original rInfo, containing dimension indexes of the grouped data.
const ScDPFilteredCache& rCacheTable = pSourceData->GetCacheTable();
sal_Int32 nRowSize = rCacheTable.getRowSize();
for (sal_Int32 nRow = 0; nRow < nRowSize; ++nRow)
{
sal_Int32 nLastRow;
if (!rCacheTable.isRowActive(nRow, &nLastRow))
{
nRow = nLastRow;
continue;
}
CalcRowData aData;
FillRowDataFromCacheTable(nRow, rCacheTable, rInfo, aData);
if ( !rInfo.aColLevelDims.empty() )
FillGroupValues(aData.aColData, rInfo.aColLevelDims);
if ( !rInfo.aRowLevelDims.empty() )
FillGroupValues(aData.aRowData, rInfo.aRowLevelDims);
if ( !rInfo.aPageDims.empty() )
FillGroupValues(aData.aPageData, rInfo.aPageDims);
ProcessRowData(rInfo, aData, bAutoShow);
}
}
const ScDPFilteredCache& ScDPGroupTableData::GetCacheTable() const
{
return pSourceData->GetCacheTable();
}
void ScDPGroupTableData::ReloadCacheTable()
{
pSourceData->ReloadCacheTable();
}
void ScDPGroupTableData::FillGroupValues(vector<SCROW>& rItems, const vector<sal_Int32>& rDims)
{
sal_Int32 nGroupedColumns = aGroups.size();
const ScDPCache& rCache = GetCacheTable().getCache();
size_t i = 0;
for (tools::Long nColumn : rDims)
{
bool bDateDim = false;
sal_Int32 nSourceDim = nColumn;
if ( nColumn >= nSourceCount && nColumn < nSourceCount + nGroupedColumns )
{
const ScDPGroupDimension& rGroupDim = aGroups[nColumn - nSourceCount];
nSourceDim= rGroupDim.GetSourceDim();
bDateDim = rGroupDim.IsDateDimension();
if (!bDateDim) // date is handled below
{
const ScDPItemData& rItem = *GetMemberById(nSourceDim, rItems[i]);
const ScDPGroupItem* pGroupItem = rGroupDim.GetGroupForData(rItem);
if (pGroupItem)
{
rItems[i] =
rCache.GetIdByItemData(nColumn, pGroupItem->GetName());
}
else
rItems[i] = rCache.GetIdByItemData(nColumn, rItem);
}
}
else if ( IsNumGroupDimension( nColumn ) )
{
bDateDim = pNumGroups[nColumn].IsDateDimension();
if (!bDateDim) // date is handled below
{
const ScDPItemData* pData = rCache.GetItemDataById(nSourceDim, rItems[i]);
if (pData->GetType() == ScDPItemData::Value)
{
ScDPNumGroupInfo aNumInfo;
GetNumGroupInfo(nColumn, aNumInfo);
double fGroupValue = ScDPUtil::getNumGroupStartValue(pData->GetValue(), aNumInfo);
ScDPItemData aItemData;
aItemData.SetRangeStart(fGroupValue);
rItems[i] = rCache.GetIdByItemData(nSourceDim, aItemData);
}
// else (textual) keep original value
}
}
const ScDPNumGroupInfo* pNumInfo = rCache.GetNumGroupInfo(nColumn);
if (bDateDim && pNumInfo)
{
// This is a date group dimension.
sal_Int32 nDatePart = rCache.GetGroupType(nColumn);
const ScDPItemData* pData = rCache.GetItemDataById(nSourceDim, rItems[i]);
if (pData->GetType() == ScDPItemData::Value)
{
SvNumberFormatter* pFormatter = pDoc->GetFormatTable();
sal_Int32 nPartValue = ScDPUtil::getDatePartValue(
pData->GetValue(), pNumInfo, nDatePart, pFormatter);
ScDPItemData aItem(nDatePart, nPartValue);
rItems[i] = rCache.GetIdByItemData(nColumn, aItem);
}
}
++i;
}
}
bool ScDPGroupTableData::IsBaseForGroup(sal_Int32 nDim) const
{
return std::any_of(aGroups.begin(), aGroups.end(),
[&nDim](const ScDPGroupDimension& rDim) { return rDim.GetSourceDim() == nDim; });
}
sal_Int32 ScDPGroupTableData::GetGroupBase(sal_Int32 nGroupDim) const
{
auto aIter = std::find_if(aGroups.begin(), aGroups.end(),
[&nGroupDim](const ScDPGroupDimension& rDim) { return rDim.GetGroupDim() == nGroupDim; });
if (aIter != aGroups.end())
return aIter->GetSourceDim();
return -1; // none
}
bool ScDPGroupTableData::IsNumOrDateGroup(sal_Int32 nDimension) const
{
// Virtual method from ScDPTableData, used in result data to force text labels.
if ( nDimension < nSourceCount )
{
return pNumGroups[nDimension].GetInfo().mbEnable ||
pNumGroups[nDimension].IsDateDimension();
}
auto aIter = std::find_if(aGroups.begin(), aGroups.end(),
[&nDimension](const ScDPGroupDimension& rDim) { return rDim.GetGroupDim() == nDimension; });
if (aIter != aGroups.end())
return aIter->IsDateDimension();
return false;
}
bool ScDPGroupTableData::IsInGroup( const ScDPItemData& rGroupData, sal_Int32 nGroupIndex,
const ScDPItemData& rBaseData, sal_Int32 nBaseIndex ) const
{
auto aIter = std::find_if(aGroups.begin(), aGroups.end(),
[&nGroupIndex, &nBaseIndex](const ScDPGroupDimension& rDim) {
return rDim.GetGroupDim() == nGroupIndex && rDim.GetSourceDim() == nBaseIndex; });
if (aIter != aGroups.end())
{
const ScDPGroupDimension& rDim = *aIter;
if (rDim.IsDateDimension())
{
return isDateInGroup(rGroupData, rBaseData);
}
else
{
// If the item is in a group, only that group is valid.
// If the item is not in any group, its own name is valid.
const ScDPGroupItem* pGroup = rDim.GetGroupForData( rBaseData );
return pGroup ? pGroup->GetName().IsCaseInsEqual( rGroupData ) :
rGroupData.IsCaseInsEqual( rBaseData );
}
}
OSL_FAIL("IsInGroup: no group dimension found");
return true;
}
bool ScDPGroupTableData::HasCommonElement( const ScDPItemData& rFirstData, sal_Int32 nFirstIndex,
const ScDPItemData& rSecondData, sal_Int32 nSecondIndex ) const
{
const ScDPGroupDimension* pFirstDim = nullptr;
const ScDPGroupDimension* pSecondDim = nullptr;
for ( const auto& rDim : aGroups )
{
const ScDPGroupDimension* pDim = &rDim;
if ( pDim->GetGroupDim() == nFirstIndex )
pFirstDim = pDim;
else if ( pDim->GetGroupDim() == nSecondIndex )
pSecondDim = pDim;
}
if ( pFirstDim && pSecondDim )
{
bool bFirstDate = pFirstDim->IsDateDimension();
bool bSecondDate = pSecondDim->IsDateDimension();
if (bFirstDate || bSecondDate)
{
// If one is a date group dimension, the other one must be, too.
if (!bFirstDate || !bSecondDate)
{
OSL_FAIL( "mix of date and non-date groups" );
return true;
}
return isDateInGroup(rFirstData, rSecondData);
}
const ScDPGroupItem* pFirstItem = pFirstDim->GetGroupForName( rFirstData );
const ScDPGroupItem* pSecondItem = pSecondDim->GetGroupForName( rSecondData );
if ( pFirstItem && pSecondItem )
{
// two existing groups -> sal_True if they have a common element
return pFirstItem->HasCommonElement( *pSecondItem );
}
else if ( pFirstItem )
{
// "automatic" group contains only its own name
return pFirstItem->HasElement( rSecondData );
}
else if ( pSecondItem )
{
// "automatic" group contains only its own name
return pSecondItem->HasElement( rFirstData );
}
else
{
// no groups -> sal_True if equal
return rFirstData.IsCaseInsEqual( rSecondData );
}
}
OSL_FAIL("HasCommonElement: no group dimension found");
return true;
}
sal_Int32 ScDPGroupTableData::GetSourceDim( sal_Int32 nDim )
{
if ( getIsDataLayoutDimension( nDim ) )
return nSourceCount;
if ( nDim >= nSourceCount && nDim < nSourceCount +static_cast<tools::Long>(aGroups.size()) )
{
const ScDPGroupDimension& rGroupDim = aGroups[nDim - nSourceCount];
return rGroupDim.GetSourceDim();
}
return nDim;
}
sal_Int32 ScDPGroupTableData::Compare(sal_Int32 nDim, sal_Int32 nDataId1, sal_Int32 nDataId2)
{
if ( getIsDataLayoutDimension(nDim) )
return 0;
const ScDPItemData* rItem1 = GetMemberById(nDim, nDataId1);
const ScDPItemData* rItem2 = GetMemberById(nDim, nDataId2);
if (rItem1 == nullptr || rItem2 == nullptr)
return 0;
return ScDPItemData::Compare( *rItem1,*rItem2);
}
#if DUMP_PIVOT_TABLE
void ScDPGroupTableData::Dump() const
{
cout << "--- ScDPGroupTableData" << endl;
for (tools::Long i = 0; i < nSourceCount; ++i)
{
cout << "* dimension: " << i << endl;
const ScDPNumGroupDimension& rGrp = pNumGroups[i];
rGrp.GetInfo().Dump();
}
cout << "---" << endl;
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|