LCOV - code coverage report
Current view: top level - sc/source/filter/oox - pivotcachebuffer.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 167 752 22.2 %
Date: 2014-11-03 Functions: 34 96 35.4 %
Legend: Lines: hit not hit

          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 "pivotcachebuffer.hxx"
      21             : 
      22             : #include <set>
      23             : #include <com/sun/star/container/XIndexAccess.hpp>
      24             : #include <com/sun/star/container/XNameAccess.hpp>
      25             : #include <com/sun/star/container/XNamed.hpp>
      26             : #include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp>
      27             : #include <com/sun/star/sheet/DataPilotFieldGroupInfo.hpp>
      28             : #include <com/sun/star/sheet/XDataPilotFieldGrouping.hpp>
      29             : #include <rtl/ustrbuf.hxx>
      30             : #include <oox/core/filterbase.hxx>
      31             : #include <oox/helper/attributelist.hxx>
      32             : #include <oox/helper/containerhelper.hxx>
      33             : #include <oox/helper/propertyset.hxx>
      34             : #include <oox/token/properties.hxx>
      35             : #include "biffinputstream.hxx"
      36             : #include "defnamesbuffer.hxx"
      37             : #include "excelhandlers.hxx"
      38             : #include "pivotcachefragment.hxx"
      39             : #include "sheetdatabuffer.hxx"
      40             : #include "tablebuffer.hxx"
      41             : #include "unitconverter.hxx"
      42             : #include "worksheetbuffer.hxx"
      43             : 
      44             : namespace oox {
      45             : namespace xls {
      46             : 
      47             : using namespace ::com::sun::star::container;
      48             : using namespace ::com::sun::star::sheet;
      49             : using namespace ::com::sun::star::table;
      50             : using namespace ::com::sun::star::uno;
      51             : using namespace ::com::sun::star::util;
      52             : 
      53             : using ::oox::core::Relations;
      54             : 
      55             : namespace {
      56             : 
      57             : const sal_uInt16 BIFF12_PCDFIELD_SERVERFIELD        = 0x0001;
      58             : const sal_uInt16 BIFF12_PCDFIELD_NOUNIQUEITEMS      = 0x0002;
      59             : const sal_uInt16 BIFF12_PCDFIELD_DATABASEFIELD      = 0x0004;
      60             : const sal_uInt16 BIFF12_PCDFIELD_HASCAPTION         = 0x0008;
      61             : const sal_uInt16 BIFF12_PCDFIELD_MEMBERPROPFIELD    = 0x0010;
      62             : const sal_uInt16 BIFF12_PCDFIELD_HASFORMULA         = 0x0100;
      63             : const sal_uInt16 BIFF12_PCDFIELD_HASPROPERTYNAME    = 0x0200;
      64             : 
      65             : const sal_uInt16 BIFF12_PCDFSITEMS_HASSEMIMIXED     = 0x0001;
      66             : const sal_uInt16 BIFF12_PCDFSITEMS_HASNONDATE       = 0x0002;
      67             : const sal_uInt16 BIFF12_PCDFSITEMS_HASDATE          = 0x0004;
      68             : const sal_uInt16 BIFF12_PCDFSITEMS_HASSTRING        = 0x0008;
      69             : const sal_uInt16 BIFF12_PCDFSITEMS_HASBLANK         = 0x0010;
      70             : const sal_uInt16 BIFF12_PCDFSITEMS_HASMIXED         = 0x0020;
      71             : const sal_uInt16 BIFF12_PCDFSITEMS_ISNUMERIC        = 0x0040;
      72             : const sal_uInt16 BIFF12_PCDFSITEMS_ISINTEGER        = 0x0080;
      73             : const sal_uInt16 BIFF12_PCDFSITEMS_HASLONGTEXT      = 0x0200;
      74             : 
      75             : const sal_uInt16 BIFF12_PCITEM_ARRAY_DOUBLE         = 0x0001;
      76             : const sal_uInt16 BIFF12_PCITEM_ARRAY_STRING         = 0x0002;
      77             : const sal_uInt16 BIFF12_PCITEM_ARRAY_ERROR          = 0x0010;
      78             : const sal_uInt16 BIFF12_PCITEM_ARRAY_DATE           = 0x0020;
      79             : 
      80             : const sal_uInt8 BIFF12_PCDFRANGEPR_AUTOSTART        = 0x01;
      81             : const sal_uInt8 BIFF12_PCDFRANGEPR_AUTOEND          = 0x02;
      82             : const sal_uInt8 BIFF12_PCDFRANGEPR_DATEGROUP        = 0x04;
      83             : 
      84             : const sal_uInt8 BIFF12_PCDEFINITION_SAVEDATA        = 0x01;
      85             : const sal_uInt8 BIFF12_PCDEFINITION_INVALID         = 0x02;
      86             : const sal_uInt8 BIFF12_PCDEFINITION_REFRESHONLOAD   = 0x04;
      87             : const sal_uInt8 BIFF12_PCDEFINITION_OPTIMIZEMEMORY  = 0x08;
      88             : const sal_uInt8 BIFF12_PCDEFINITION_ENABLEREFRESH   = 0x10;
      89             : const sal_uInt8 BIFF12_PCDEFINITION_BACKGROUNDQUERY = 0x20;
      90             : const sal_uInt8 BIFF12_PCDEFINITION_UPGRADEONREFR   = 0x40;
      91             : const sal_uInt8 BIFF12_PCDEFINITION_TUPELCACHE      = 0x80;
      92             : 
      93             : const sal_uInt8 BIFF12_PCDEFINITION_HASUSERNAME     = 0x01;
      94             : const sal_uInt8 BIFF12_PCDEFINITION_HASRELID        = 0x02;
      95             : const sal_uInt8 BIFF12_PCDEFINITION_SUPPORTSUBQUERY = 0x04;
      96             : const sal_uInt8 BIFF12_PCDEFINITION_SUPPORTDRILL    = 0x08;
      97             : 
      98             : const sal_uInt8 BIFF12_PCDWBSOURCE_HASRELID         = 0x01;
      99             : const sal_uInt8 BIFF12_PCDWBSOURCE_HASSHEET         = 0x02;
     100             : 
     101             : const sal_uInt16 BIFF_PC_NOSTRING                   = 0xFFFF;
     102             : 
     103             : const sal_uInt16 BIFF_PCDFIELD_HASITEMS             = 0x0001;
     104             : const sal_uInt16 BIFF_PCDFIELD_HASPARENT            = 0x0008;
     105             : const sal_uInt16 BIFF_PCDFIELD_RANGEGROUP           = 0x0010;
     106             : const sal_uInt16 BIFF_PCDFIELD_ISNUMERIC            = 0x0020;
     107             : const sal_uInt16 BIFF_PCDFIELD_HASSEMIMIXED         = 0x0080;
     108             : const sal_uInt16 BIFF_PCDFIELD_HASLONGINDEX         = 0x0200;
     109             : const sal_uInt16 BIFF_PCDFIELD_HASNONDATE           = 0x0400;
     110             : const sal_uInt16 BIFF_PCDFIELD_HASDATE              = 0x0800;
     111             : const sal_uInt16 BIFF_PCDFIELD_SERVERFIELD          = 0x2000;
     112             : const sal_uInt16 BIFF_PCDFIELD_NOUNIQUEITEMS        = 0x4000;
     113             : 
     114             : const sal_uInt16 BIFF_PCDFRANGEPR_AUTOSTART         = 0x0001;
     115             : const sal_uInt16 BIFF_PCDFRANGEPR_AUTOEND           = 0x0002;
     116             : 
     117             : const sal_uInt16 BIFF_PCDEFINITION_SAVEDATA         = 0x0001;
     118             : const sal_uInt16 BIFF_PCDEFINITION_INVALID          = 0x0002;
     119             : const sal_uInt16 BIFF_PCDEFINITION_REFRESHONLOAD    = 0x0004;
     120             : const sal_uInt16 BIFF_PCDEFINITION_OPTIMIZEMEMORY   = 0x0008;
     121             : const sal_uInt16 BIFF_PCDEFINITION_BACKGROUNDQUERY  = 0x0010;
     122             : const sal_uInt16 BIFF_PCDEFINITION_ENABLEREFRESH    = 0x0020;
     123             : 
     124             : /** Adjusts the weird date format read from binary streams.
     125             : 
     126             :     Dates before 1900-Mar-01 are stored including the non-existing leap day
     127             :     1900-02-29. tools::Time values (without date) are stored as times of day
     128             :     1900-Jan-00. Nothing has to be done when the workbook is stored in 1904
     129             :     date mode (dates before 1904-Jan-01 will not occur in this case).
     130             :  */
     131           0 : void lclAdjustBinDateTime( css::util::DateTime& orDateTime )
     132             : {
     133           0 :     if( (orDateTime.Year == 1900) && (orDateTime.Month <= 2) )
     134             :     {
     135             :         OSL_ENSURE( (orDateTime.Month == 1) || ((orDateTime.Month == 2) && (orDateTime.Day > 0)), "lclAdjustBinDateTime - invalid date" );
     136           0 :         switch( orDateTime.Month )
     137             :         {
     138           0 :             case 2: if( orDateTime.Day > 1 ) --orDateTime.Day; else { orDateTime.Day += 30; --orDateTime.Month; }                       break;
     139           0 :             case 1: if( orDateTime.Day > 1 ) --orDateTime.Day; else { orDateTime.Day += 30; orDateTime.Month = 12; --orDateTime.Year; } break;
     140             :         }
     141             :     }
     142           0 : }
     143             : 
     144             : } // namespace
     145             : 
     146          98 : PivotCacheItem::PivotCacheItem() :
     147          98 :     mnType( XML_m ), mbUnused( false )
     148             : {
     149          98 : }
     150             : 
     151          72 : void PivotCacheItem::readString( const AttributeList& rAttribs )
     152             : {
     153          72 :     maValue <<= rAttribs.getXString( XML_v, OUString() );
     154          72 :     mnType = XML_s;
     155          72 : }
     156             : 
     157          26 : void PivotCacheItem::readNumeric( const AttributeList& rAttribs )
     158             : {
     159          26 :     maValue <<= rAttribs.getDouble( XML_v, 0.0 );
     160          26 :     mnType = XML_n;
     161          26 :     mbUnused = rAttribs.getBool( XML_u, false );
     162          26 : }
     163             : 
     164           0 : void PivotCacheItem::readDate( const AttributeList& rAttribs )
     165             : {
     166           0 :     maValue <<= rAttribs.getDateTime( XML_v, css::util::DateTime() );
     167           0 :     mnType = XML_d;
     168           0 : }
     169             : 
     170           0 : void PivotCacheItem::readBool( const AttributeList& rAttribs )
     171             : {
     172           0 :     maValue <<= rAttribs.getBool( XML_v, false );
     173           0 :     mnType = XML_b;
     174           0 : }
     175             : 
     176           0 : void PivotCacheItem::readError( const AttributeList& rAttribs, const UnitConverter& rUnitConverter )
     177             : {
     178           0 :     maValue <<= static_cast< sal_Int32 >( rUnitConverter.calcBiffErrorCode( rAttribs.getXString( XML_v, OUString() ) ) );
     179           0 :     mnType = XML_e;
     180           0 : }
     181             : 
     182           0 : void PivotCacheItem::readIndex( const AttributeList& rAttribs )
     183             : {
     184           0 :     maValue <<= rAttribs.getInteger( XML_v, -1 );
     185           0 :     mnType = XML_x;
     186           0 : }
     187             : 
     188           0 : void PivotCacheItem::readString( SequenceInputStream& rStrm )
     189             : {
     190           0 :     maValue <<= BiffHelper::readString( rStrm );
     191           0 :     mnType = XML_s;
     192           0 : }
     193             : 
     194           0 : void PivotCacheItem::readDouble( SequenceInputStream& rStrm )
     195             : {
     196           0 :     maValue <<= rStrm.readDouble();
     197           0 :     mnType = XML_n;
     198           0 : }
     199             : 
     200           0 : void PivotCacheItem::readDate( SequenceInputStream& rStrm )
     201             : {
     202           0 :     css::util::DateTime aDateTime;
     203           0 :     aDateTime.Year = rStrm.readuInt16();
     204           0 :     aDateTime.Month = rStrm.readuInt16();
     205           0 :     aDateTime.Day = rStrm.readuInt8();
     206           0 :     aDateTime.Hours = rStrm.readuInt8();
     207           0 :     aDateTime.Minutes = rStrm.readuInt8();
     208           0 :     aDateTime.Seconds = rStrm.readuInt8();
     209           0 :     lclAdjustBinDateTime( aDateTime );
     210           0 :     maValue <<= aDateTime;
     211           0 :     mnType = XML_d;
     212           0 : }
     213             : 
     214           0 : void PivotCacheItem::readBool( SequenceInputStream& rStrm )
     215             : {
     216           0 :     maValue <<= (rStrm.readuInt8() != 0);
     217           0 :     mnType = XML_b;
     218           0 : }
     219             : 
     220           0 : void PivotCacheItem::readError( SequenceInputStream& rStrm )
     221             : {
     222           0 :     maValue <<= static_cast< sal_Int32 >( rStrm.readuInt8() );
     223           0 :     mnType = XML_e;
     224           0 : }
     225             : 
     226           0 : void PivotCacheItem::readIndex( SequenceInputStream& rStrm )
     227             : {
     228           0 :     maValue <<= rStrm.readInt32();
     229           0 :     mnType = XML_x;
     230           0 : }
     231             : 
     232           0 : void PivotCacheItem::readString( BiffInputStream& rStrm, const WorkbookHelper& rHelper )
     233             : {
     234           0 :     maValue <<= (rHelper.getBiff() == BIFF8) ? rStrm.readUniString() : rStrm.readByteStringUC( true, rHelper.getTextEncoding() );
     235           0 :     mnType = XML_s;
     236           0 : }
     237             : 
     238           0 : void PivotCacheItem::readDouble( BiffInputStream& rStrm )
     239             : {
     240           0 :     maValue <<= rStrm.readDouble();
     241           0 :     mnType = XML_n;
     242           0 : }
     243             : 
     244           0 : void PivotCacheItem::readInteger( BiffInputStream& rStrm )
     245             : {
     246           0 :     maValue <<= rStrm.readInt16();
     247           0 :     mnType = XML_i;                 // fake, used for BIFF only
     248           0 : }
     249             : 
     250           0 : void PivotCacheItem::readDate( BiffInputStream& rStrm )
     251             : {
     252           0 :     css::util::DateTime aDateTime;
     253           0 :     aDateTime.Year = rStrm.readuInt16();
     254           0 :     aDateTime.Month = rStrm.readuInt16();
     255           0 :     aDateTime.Day = rStrm.readuInt8();
     256           0 :     aDateTime.Hours = rStrm.readuInt8();
     257           0 :     aDateTime.Minutes = rStrm.readuInt8();
     258           0 :     aDateTime.Seconds = rStrm.readuInt8();
     259           0 :     lclAdjustBinDateTime( aDateTime );
     260           0 :     maValue <<= aDateTime;
     261           0 :     mnType = XML_d;
     262           0 : }
     263             : 
     264           0 : void PivotCacheItem::readBool( BiffInputStream& rStrm )
     265             : {
     266           0 :     maValue <<= (rStrm.readuInt8() != 0);
     267           0 :     mnType = XML_b;
     268           0 : }
     269             : 
     270           0 : void PivotCacheItem::readError( BiffInputStream& rStrm )
     271             : {
     272           0 :     maValue <<= static_cast< sal_Int32 >( rStrm.readuInt8() );
     273           0 :     mnType = XML_e;
     274           0 : }
     275             : 
     276           0 : void PivotCacheItem::setStringValue( const OUString& sString )
     277             : {
     278           0 :     mnType = XML_s;
     279           0 :     maValue <<= sString;
     280           0 : }
     281             : 
     282          30 : OUString PivotCacheItem::getName() const
     283             : {
     284          30 :     switch( mnType )
     285             :     {
     286           0 :         case XML_m: return OUString();
     287          30 :         case XML_s: return maValue.get< OUString >();
     288           0 :         case XML_n: return OUString::number( maValue.get< double >() );                             // !TODO
     289           0 :         case XML_i: return OUString::number( maValue.get< sal_Int32 >() );
     290           0 :         case XML_d: return OUString();                                                              // !TODO
     291           0 :         case XML_b: return OUString::boolean( maValue.get< bool >() );                              // !TODO
     292           0 :         case XML_e: return OUString();                                                              // !TODO
     293             :     }
     294             :     OSL_FAIL( "PivotCacheItem::getName - invalid data type" );
     295           0 :     return OUString();
     296             : }
     297             : 
     298          96 : PivotCacheItemList::PivotCacheItemList( const WorkbookHelper& rHelper ) :
     299          96 :     WorkbookHelper( rHelper )
     300             : {
     301          96 : }
     302             : 
     303          98 : void PivotCacheItemList::importItem( sal_Int32 nElement, const AttributeList& rAttribs )
     304             : {
     305          98 :     PivotCacheItem& rItem = createItem();
     306          98 :     switch( nElement )
     307             :     {
     308           0 :         case XLS_TOKEN( m ):                                                        break;
     309          72 :         case XLS_TOKEN( s ):    rItem.readString( rAttribs );                       break;
     310          26 :         case XLS_TOKEN( n ):    rItem.readNumeric( rAttribs );                      break;
     311           0 :         case XLS_TOKEN( d ):    rItem.readDate( rAttribs );                         break;
     312           0 :         case XLS_TOKEN( b ):    rItem.readBool( rAttribs );                         break;
     313           0 :         case XLS_TOKEN( e ):    rItem.readError( rAttribs, getUnitConverter() );    break;
     314             :         default:    OSL_FAIL( "PivotCacheItemList::importItem - unknown element type" );
     315             :     }
     316          98 : }
     317             : 
     318           0 : void PivotCacheItemList::importItem( sal_Int32 nRecId, SequenceInputStream& rStrm )
     319             : {
     320           0 :     if( nRecId == BIFF12_ID_PCITEM_ARRAY )
     321             :     {
     322           0 :         importArray( rStrm );
     323           0 :         return;
     324             :     }
     325             : 
     326           0 :     PivotCacheItem& rItem = createItem();
     327           0 :     switch( nRecId )
     328             :     {
     329             :         case BIFF12_ID_PCITEM_MISSING:
     330           0 :         case BIFF12_ID_PCITEMA_MISSING:                             break;
     331             :         case BIFF12_ID_PCITEM_STRING:
     332           0 :         case BIFF12_ID_PCITEMA_STRING:  rItem.readString( rStrm );  break;
     333             :         case BIFF12_ID_PCITEM_DOUBLE:
     334           0 :         case BIFF12_ID_PCITEMA_DOUBLE:  rItem.readDouble( rStrm );  break;
     335             :         case BIFF12_ID_PCITEM_DATE:
     336           0 :         case BIFF12_ID_PCITEMA_DATE:    rItem.readDate( rStrm );    break;
     337             :         case BIFF12_ID_PCITEM_BOOL:
     338           0 :         case BIFF12_ID_PCITEMA_BOOL:    rItem.readBool( rStrm );    break;
     339             :         case BIFF12_ID_PCITEM_ERROR:
     340           0 :         case BIFF12_ID_PCITEMA_ERROR:   rItem.readError( rStrm );   break;
     341             :         default:    OSL_FAIL( "PivotCacheItemList::importItem - unknown record type" );
     342             :     }
     343             : }
     344             : 
     345           0 : void PivotCacheItemList::importItemList( BiffInputStream& rStrm, sal_uInt16 nCount )
     346             : {
     347           0 :     bool bLoop = true;
     348           0 :     for( sal_uInt16 nItemIdx = 0; bLoop && (nItemIdx < nCount); ++nItemIdx )
     349             :     {
     350           0 :         bLoop = rStrm.startNextRecord();
     351           0 :         if( bLoop ) switch( rStrm.getRecId() )
     352             :         {
     353           0 :             case BIFF_ID_PCITEM_MISSING:    createItem();                               break;
     354           0 :             case BIFF_ID_PCITEM_STRING:     createItem().readString( rStrm, *this );    break;
     355           0 :             case BIFF_ID_PCITEM_DOUBLE:     createItem().readDouble( rStrm );           break;
     356           0 :             case BIFF_ID_PCITEM_INTEGER:    createItem().readInteger( rStrm );          break;
     357           0 :             case BIFF_ID_PCITEM_DATE:       createItem().readDate( rStrm );             break;
     358           0 :             case BIFF_ID_PCITEM_BOOL:       createItem().readBool( rStrm );             break;
     359           0 :             case BIFF_ID_PCITEM_ERROR:      createItem().readError( rStrm );            break;
     360           0 :             default:                        rStrm.rewindRecord(); bLoop = false;
     361             :         }
     362             :     }
     363             :     OSL_ENSURE( bLoop, "PivotCacheItemList::importItemList - could not read all cache item records" );
     364           0 : }
     365             : 
     366          30 : const PivotCacheItem* PivotCacheItemList::getCacheItem( sal_Int32 nItemIdx ) const
     367             : {
     368          30 :     return ContainerHelper::getVectorElement( maItems, nItemIdx );
     369             : }
     370             : 
     371           0 : void PivotCacheItemList::applyItemCaptions( const IdCaptionPairList& vCaptions )
     372             : {
     373           0 :     for( IdCaptionPairList::const_iterator aIt = vCaptions.begin(), aEnd = vCaptions.end(); aIt != aEnd; ++aIt )
     374             :     {
     375           0 :         if ( static_cast<sal_uInt32>( aIt->first ) < maItems.size() )
     376           0 :             maItems[ aIt->first ].setStringValue( aIt->second );
     377             :     }
     378           0 : }
     379             : 
     380           0 : void PivotCacheItemList::getCacheItemNames( ::std::vector< OUString >& orItemNames ) const
     381             : {
     382           0 :     orItemNames.clear();
     383           0 :     orItemNames.reserve( maItems.size() );
     384           0 :     for( CacheItemVector::const_iterator aIt = maItems.begin(), aEnd = maItems.end(); aIt != aEnd; ++aIt )
     385           0 :         orItemNames.push_back( aIt->getName() );
     386           0 : }
     387             : 
     388             : // private --------------------------------------------------------------------
     389             : 
     390          98 : PivotCacheItem& PivotCacheItemList::createItem()
     391             : {
     392          98 :     maItems.resize( maItems.size() + 1 );
     393          98 :     return maItems.back();
     394             : }
     395             : 
     396           0 : void PivotCacheItemList::importArray( SequenceInputStream& rStrm )
     397             : {
     398           0 :     sal_uInt16 nType = rStrm.readuInt16();
     399           0 :     sal_Int32 nCount = rStrm.readInt32();
     400           0 :     for( sal_Int32 nIdx = 0; !rStrm.isEof() && (nIdx < nCount); ++nIdx )
     401             :     {
     402           0 :         switch( nType )
     403             :         {
     404           0 :             case BIFF12_PCITEM_ARRAY_DOUBLE: createItem().readDouble( rStrm );   break;
     405           0 :             case BIFF12_PCITEM_ARRAY_STRING: createItem().readString( rStrm );   break;
     406           0 :             case BIFF12_PCITEM_ARRAY_ERROR:  createItem().readError( rStrm );    break;
     407           0 :             case BIFF12_PCITEM_ARRAY_DATE:   createItem().readDate( rStrm );     break;
     408             :             default:
     409             :                 OSL_FAIL( "PivotCacheItemList::importArray - unknown data type" );
     410           0 :                 nIdx = nCount;
     411             :         }
     412             :     }
     413           0 : }
     414             : 
     415          48 : PCFieldModel::PCFieldModel() :
     416             :     mnNumFmtId( 0 ),
     417             :     mnSqlType( 0 ),
     418             :     mnHierarchy( 0 ),
     419             :     mnLevel( 0 ),
     420             :     mnMappingCount( 0 ),
     421             :     mbDatabaseField( true ),
     422             :     mbServerField( false ),
     423             :     mbUniqueList( true ),
     424          48 :     mbMemberPropField( false )
     425             : {
     426          48 : }
     427             : 
     428          48 : PCSharedItemsModel::PCSharedItemsModel() :
     429             :     mbHasSemiMixed( true ),
     430             :     mbHasNonDate( true ),
     431             :     mbHasDate( false ),
     432             :     mbHasString( true ),
     433             :     mbHasBlank( false ),
     434             :     mbHasMixed( false ),
     435             :     mbIsNumeric( false ),
     436             :     mbIsInteger( false ),
     437             :     mbHasLongText( false ),
     438          48 :     mbHasLongIndexes( false )
     439             : {
     440          48 : }
     441             : 
     442          48 : PCFieldGroupModel::PCFieldGroupModel() :
     443             :     mfStartValue( 0.0 ),
     444             :     mfEndValue( 0.0 ),
     445             :     mfInterval( 1.0 ),
     446             :     mnParentField( -1 ),
     447             :     mnBaseField( -1 ),
     448             :     mnGroupBy( XML_range ),
     449             :     mbRangeGroup( false ),
     450             :     mbDateGroup( false ),
     451             :     mbAutoStart( true ),
     452          48 :     mbAutoEnd( true )
     453             : {
     454          48 : }
     455             : 
     456           0 : void PCFieldGroupModel::setBiffGroupBy( sal_uInt8 nGroupBy )
     457             : {
     458             :     static const sal_Int32 spnGroupBy[] = { XML_range,
     459             :         XML_seconds, XML_minutes, XML_hours, XML_days, XML_months, XML_quarters, XML_years };
     460           0 :     mnGroupBy = STATIC_ARRAY_SELECT( spnGroupBy, nGroupBy, XML_range );
     461           0 : }
     462             : 
     463          48 : PivotCacheField::PivotCacheField( const WorkbookHelper& rHelper, bool bIsDatabaseField ) :
     464             :     WorkbookHelper( rHelper ),
     465             :     maSharedItems( rHelper ),
     466          48 :     maGroupItems( rHelper )
     467             : {
     468          48 :     maFieldModel.mbDatabaseField = bIsDatabaseField;
     469          48 : }
     470             : 
     471          48 : void PivotCacheField::importCacheField( const AttributeList& rAttribs )
     472             : {
     473          48 :     maFieldModel.maName            = rAttribs.getXString( XML_name, OUString() );
     474          48 :     maFieldModel.maCaption         = rAttribs.getXString( XML_caption, OUString() );
     475          48 :     maFieldModel.maPropertyName    = rAttribs.getXString( XML_propertyName, OUString() );
     476          48 :     maFieldModel.maFormula         = rAttribs.getXString( XML_formula, OUString() );
     477          48 :     maFieldModel.mnNumFmtId        = rAttribs.getInteger( XML_numFmtId, 0 );
     478          48 :     maFieldModel.mnSqlType         = rAttribs.getInteger( XML_sqlType, 0 );
     479          48 :     maFieldModel.mnHierarchy       = rAttribs.getInteger( XML_hierarchy, 0 );
     480          48 :     maFieldModel.mnLevel           = rAttribs.getInteger( XML_level, 0 );
     481          48 :     maFieldModel.mnMappingCount    = rAttribs.getInteger( XML_mappingCount, 0 );
     482          48 :     maFieldModel.mbDatabaseField   = rAttribs.getBool( XML_databaseField, true );
     483          48 :     maFieldModel.mbServerField     = rAttribs.getBool( XML_serverField, false );
     484          48 :     maFieldModel.mbUniqueList      = rAttribs.getBool( XML_uniqueList, true );
     485          48 :     maFieldModel.mbMemberPropField = rAttribs.getBool( XML_memberPropertyField, false );
     486          48 : }
     487             : 
     488          48 : void PivotCacheField::importSharedItems( const AttributeList& rAttribs )
     489             : {
     490             :     OSL_ENSURE( maSharedItems.empty(), "PivotCacheField::importSharedItems - multiple shared items elements" );
     491          48 :     maSharedItemsModel.mbHasSemiMixed = rAttribs.getBool( XML_containsSemiMixedTypes, true );
     492          48 :     maSharedItemsModel.mbHasNonDate   = rAttribs.getBool( XML_containsNonDate, true );
     493          48 :     maSharedItemsModel.mbHasDate      = rAttribs.getBool( XML_containsDate, false );
     494          48 :     maSharedItemsModel.mbHasString    = rAttribs.getBool( XML_containsString, true );
     495          48 :     maSharedItemsModel.mbHasBlank     = rAttribs.getBool( XML_containsBlank, false );
     496          48 :     maSharedItemsModel.mbHasMixed     = rAttribs.getBool( XML_containsMixedTypes, false );
     497          48 :     maSharedItemsModel.mbIsNumeric    = rAttribs.getBool( XML_containsNumber, false );
     498          48 :     maSharedItemsModel.mbIsInteger    = rAttribs.getBool( XML_containsInteger, false );
     499          48 :     maSharedItemsModel.mbHasLongText  = rAttribs.getBool( XML_longText, false );
     500          48 : }
     501             : 
     502          98 : void PivotCacheField::importSharedItem( sal_Int32 nElement, const AttributeList& rAttribs )
     503             : {
     504          98 :     maSharedItems.importItem( nElement, rAttribs );
     505          98 : }
     506             : 
     507           0 : void PivotCacheField::importFieldGroup( const AttributeList& rAttribs )
     508             : {
     509           0 :     maFieldGroupModel.mnParentField = rAttribs.getInteger( XML_par, -1 );
     510           0 :     maFieldGroupModel.mnBaseField   = rAttribs.getInteger( XML_base, -1 );
     511           0 : }
     512             : 
     513           0 : void PivotCacheField::importRangePr( const AttributeList& rAttribs )
     514             : {
     515           0 :     maFieldGroupModel.maStartDate    = rAttribs.getDateTime( XML_startDate, css::util::DateTime() );
     516           0 :     maFieldGroupModel.maEndDate      = rAttribs.getDateTime( XML_endDate, css::util::DateTime() );
     517           0 :     maFieldGroupModel.mfStartValue   = rAttribs.getDouble( XML_startNum, 0.0 );
     518           0 :     maFieldGroupModel.mfEndValue     = rAttribs.getDouble( XML_endNum, 0.0 );
     519           0 :     maFieldGroupModel.mfInterval     = rAttribs.getDouble( XML_groupInterval, 1.0 );
     520           0 :     maFieldGroupModel.mnGroupBy      = rAttribs.getToken( XML_groupBy, XML_range );
     521           0 :     maFieldGroupModel.mbRangeGroup   = true;
     522           0 :     maFieldGroupModel.mbDateGroup    = maFieldGroupModel.mnGroupBy != XML_range;
     523           0 :     maFieldGroupModel.mbAutoStart    = rAttribs.getBool( XML_autoStart, true );
     524           0 :     maFieldGroupModel.mbAutoEnd      = rAttribs.getBool( XML_autoEnd, true );
     525           0 : }
     526             : 
     527           0 : void PivotCacheField::importDiscretePrItem( sal_Int32 nElement, const AttributeList& rAttribs )
     528             : {
     529             :     OSL_ENSURE( nElement == XLS_TOKEN( x ), "PivotCacheField::importDiscretePrItem - unexpected element" );
     530           0 :     if( nElement == XLS_TOKEN( x ) )
     531           0 :         maDiscreteItems.push_back( rAttribs.getInteger( XML_v, -1 ) );
     532           0 : }
     533             : 
     534           0 : void PivotCacheField::importGroupItem( sal_Int32 nElement, const AttributeList& rAttribs )
     535             : {
     536           0 :     maGroupItems.importItem( nElement, rAttribs );
     537           0 : }
     538             : 
     539           0 : void PivotCacheField::importPCDField( SequenceInputStream& rStrm )
     540             : {
     541             :     sal_uInt16 nFlags;
     542           0 :     rStrm >> nFlags >> maFieldModel.mnNumFmtId;
     543           0 :     maFieldModel.mnSqlType = rStrm.readInt16();
     544           0 :     rStrm >> maFieldModel.mnHierarchy >> maFieldModel.mnLevel >> maFieldModel.mnMappingCount >> maFieldModel.maName;
     545           0 :     if( getFlag( nFlags, BIFF12_PCDFIELD_HASCAPTION ) )
     546           0 :         rStrm >> maFieldModel.maCaption;
     547           0 :     if( getFlag( nFlags, BIFF12_PCDFIELD_HASFORMULA ) )
     548           0 :         rStrm.skip( ::std::max< sal_Int32 >( rStrm.readInt32(), 0 ) );
     549           0 :     if( maFieldModel.mnMappingCount > 0 )
     550           0 :         rStrm.skip( ::std::max< sal_Int32 >( rStrm.readInt32(), 0 ) );
     551           0 :     if( getFlag( nFlags, BIFF12_PCDFIELD_HASPROPERTYNAME ) )
     552           0 :         rStrm >> maFieldModel.maPropertyName;
     553             : 
     554           0 :     maFieldModel.mbDatabaseField   = getFlag( nFlags, BIFF12_PCDFIELD_DATABASEFIELD );
     555           0 :     maFieldModel.mbServerField     = getFlag( nFlags, BIFF12_PCDFIELD_SERVERFIELD );
     556           0 :     maFieldModel.mbUniqueList      = !getFlag( nFlags, BIFF12_PCDFIELD_NOUNIQUEITEMS );
     557           0 :     maFieldModel.mbMemberPropField = getFlag( nFlags, BIFF12_PCDFIELD_MEMBERPROPFIELD );
     558           0 : }
     559             : 
     560           0 : void PivotCacheField::importPCDFSharedItems( SequenceInputStream& rStrm )
     561             : {
     562             :     sal_uInt16 nFlags;
     563           0 :     rStrm >> nFlags;
     564           0 :     maSharedItemsModel.mbHasSemiMixed = getFlag( nFlags, BIFF12_PCDFSITEMS_HASSEMIMIXED );
     565           0 :     maSharedItemsModel.mbHasNonDate   = getFlag( nFlags, BIFF12_PCDFSITEMS_HASNONDATE );
     566           0 :     maSharedItemsModel.mbHasDate      = getFlag( nFlags, BIFF12_PCDFSITEMS_HASDATE );
     567           0 :     maSharedItemsModel.mbHasString    = getFlag( nFlags, BIFF12_PCDFSITEMS_HASSTRING );
     568           0 :     maSharedItemsModel.mbHasBlank     = getFlag( nFlags, BIFF12_PCDFSITEMS_HASBLANK );
     569           0 :     maSharedItemsModel.mbHasMixed     = getFlag( nFlags, BIFF12_PCDFSITEMS_HASMIXED );
     570           0 :     maSharedItemsModel.mbIsNumeric    = getFlag( nFlags, BIFF12_PCDFSITEMS_ISNUMERIC );
     571           0 :     maSharedItemsModel.mbIsInteger    = getFlag( nFlags, BIFF12_PCDFSITEMS_ISINTEGER );
     572           0 :     maSharedItemsModel.mbHasLongText  = getFlag( nFlags, BIFF12_PCDFSITEMS_HASLONGTEXT );
     573           0 : }
     574             : 
     575           0 : void PivotCacheField::importPCDFSharedItem( sal_Int32 nRecId, SequenceInputStream& rStrm )
     576             : {
     577           0 :     maSharedItems.importItem( nRecId, rStrm );
     578           0 : }
     579             : 
     580           0 : void PivotCacheField::importPCDFieldGroup( SequenceInputStream& rStrm )
     581             : {
     582           0 :     rStrm >> maFieldGroupModel.mnParentField >> maFieldGroupModel.mnBaseField;
     583           0 : }
     584             : 
     585           0 : void PivotCacheField::importPCDFRangePr( SequenceInputStream& rStrm )
     586             : {
     587             :     sal_uInt8 nGroupBy, nFlags;
     588           0 :     rStrm >> nGroupBy >> nFlags >> maFieldGroupModel.mfStartValue >> maFieldGroupModel.mfEndValue >> maFieldGroupModel.mfInterval;
     589             : 
     590           0 :     maFieldGroupModel.setBiffGroupBy( nGroupBy );
     591           0 :     maFieldGroupModel.mbRangeGroup   = true;
     592           0 :     maFieldGroupModel.mbDateGroup    = getFlag( nFlags, BIFF12_PCDFRANGEPR_DATEGROUP );
     593           0 :     maFieldGroupModel.mbAutoStart    = getFlag( nFlags, BIFF12_PCDFRANGEPR_AUTOSTART );
     594           0 :     maFieldGroupModel.mbAutoEnd      = getFlag( nFlags, BIFF12_PCDFRANGEPR_AUTOEND );
     595             : 
     596             :     OSL_ENSURE( maFieldGroupModel.mbDateGroup == (maFieldGroupModel.mnGroupBy != XML_range), "PivotCacheField::importPCDFRangePr - wrong date flag" );
     597           0 :     if( maFieldGroupModel.mbDateGroup )
     598             :     {
     599           0 :         maFieldGroupModel.maStartDate = getUnitConverter().calcDateTimeFromSerial( maFieldGroupModel.mfStartValue );
     600           0 :         maFieldGroupModel.maEndDate   = getUnitConverter().calcDateTimeFromSerial( maFieldGroupModel.mfEndValue );
     601             :     }
     602           0 : }
     603             : 
     604           0 : void PivotCacheField::importPCDFDiscretePrItem( sal_Int32 nRecId, SequenceInputStream& rStrm )
     605             : {
     606             :     OSL_ENSURE( nRecId == BIFF12_ID_PCITEM_INDEX, "PivotCacheField::importPCDFDiscretePrItem - unexpected record" );
     607           0 :     if( nRecId == BIFF12_ID_PCITEM_INDEX )
     608           0 :         maDiscreteItems.push_back( rStrm.readInt32() );
     609           0 : }
     610             : 
     611           0 : void PivotCacheField::importPCDFGroupItem( sal_Int32 nRecId, SequenceInputStream& rStrm )
     612             : {
     613           0 :     maGroupItems.importItem( nRecId, rStrm );
     614           0 : }
     615             : 
     616           0 : void PivotCacheField::importPCDField( BiffInputStream& rStrm )
     617             : {
     618             :     sal_uInt16 nFlags, nGroupItems, nBaseItems, nSharedItems;
     619           0 :     rStrm >> nFlags;
     620           0 :     maFieldGroupModel.mnParentField  = rStrm.readuInt16();
     621           0 :     maFieldGroupModel.mnBaseField    = rStrm.readuInt16();
     622           0 :     rStrm.skip( 2 );    // number of unique items (either shared or group)
     623           0 :     rStrm >> nGroupItems >> nBaseItems >> nSharedItems;
     624           0 :     maFieldModel.maName = (getBiff() == BIFF8) ? rStrm.readUniString() : rStrm.readByteStringUC( true, getTextEncoding() );
     625             : 
     626           0 :     maFieldModel.mbServerField          = getFlag( nFlags, BIFF_PCDFIELD_SERVERFIELD );
     627           0 :     maFieldModel.mbUniqueList           = !getFlag( nFlags, BIFF_PCDFIELD_NOUNIQUEITEMS );
     628           0 :     maSharedItemsModel.mbHasSemiMixed   = getFlag( nFlags, BIFF_PCDFIELD_HASSEMIMIXED );
     629           0 :     maSharedItemsModel.mbHasNonDate     = getFlag( nFlags, BIFF_PCDFIELD_HASNONDATE );
     630           0 :     maSharedItemsModel.mbHasDate        = getFlag( nFlags, BIFF_PCDFIELD_HASDATE );
     631           0 :     maSharedItemsModel.mbIsNumeric      = getFlag( nFlags, BIFF_PCDFIELD_ISNUMERIC );
     632           0 :     maSharedItemsModel.mbHasLongIndexes = getFlag( nFlags, BIFF_PCDFIELD_HASLONGINDEX );
     633           0 :     maFieldGroupModel.mbRangeGroup      = getFlag( nFlags, BIFF_PCDFIELD_RANGEGROUP );
     634             : 
     635             :     // in BIFF, presence of parent group field is denoted by a flag
     636           0 :     if( !getFlag( nFlags, BIFF_PCDFIELD_HASPARENT ) )
     637           0 :         maFieldGroupModel.mnParentField = -1;
     638             : 
     639             :     // following PCDFSQLTYPE record contains SQL type
     640           0 :     if( (rStrm.getNextRecId() == BIFF_ID_PCDFSQLTYPE) && rStrm.startNextRecord() )
     641           0 :         maFieldModel.mnSqlType = rStrm.readInt16();
     642             : 
     643             :     // read group items, if any
     644           0 :     if( nGroupItems > 0 )
     645             :     {
     646             :         SAL_WARN_IF(
     647             :             !getFlag(nFlags, BIFF_PCDFIELD_HASITEMS), "sc.filter",
     648             :             "PivotCacheField::importPCDField - missing items flag");
     649           0 :         maGroupItems.importItemList( rStrm, nGroupItems );
     650             : 
     651           0 :         sal_uInt16 nNextRecId = rStrm.getNextRecId();
     652           0 :         bool bHasRangePr = nNextRecId == BIFF_ID_PCDFRANGEPR;
     653           0 :         bool bHasDiscretePr = nNextRecId == BIFF_ID_PCDFDISCRETEPR;
     654             : 
     655             :         OSL_ENSURE( bHasRangePr || bHasDiscretePr, "PivotCacheField::importPCDField - missing group properties record" );
     656             :         OSL_ENSURE( bHasRangePr == maFieldGroupModel.mbRangeGroup, "PivotCacheField::importPCDField - invalid range grouping flag" );
     657           0 :         if( bHasRangePr && rStrm.startNextRecord() )
     658           0 :             importPCDFRangePr( rStrm );
     659           0 :         else if( bHasDiscretePr && rStrm.startNextRecord() )
     660           0 :             importPCDFDiscretePr( rStrm );
     661             :     }
     662             : 
     663             :     // read the shared items, if any
     664           0 :     if( nSharedItems > 0 )
     665             :     {
     666             :         SAL_WARN_IF(
     667             :             !getFlag(nFlags, BIFF_PCDFIELD_HASITEMS), "sc.filter",
     668             :             "PivotCacheField::importPCDField - missing items flag");
     669           0 :         maSharedItems.importItemList( rStrm, nSharedItems );
     670             :     }
     671           0 : }
     672             : 
     673           0 : void PivotCacheField::importPCDFRangePr( BiffInputStream& rStrm )
     674             : {
     675             :     sal_uInt16 nFlags;
     676           0 :     rStrm >> nFlags;
     677           0 :     maFieldGroupModel.setBiffGroupBy( extractValue< sal_uInt8 >( nFlags, 2, 3 ) );
     678           0 :     maFieldGroupModel.mbRangeGroup = true;
     679           0 :     maFieldGroupModel.mbDateGroup  = maFieldGroupModel.mnGroupBy != XML_range;
     680           0 :     maFieldGroupModel.mbAutoStart  = getFlag( nFlags, BIFF_PCDFRANGEPR_AUTOSTART );
     681           0 :     maFieldGroupModel.mbAutoEnd    = getFlag( nFlags, BIFF_PCDFRANGEPR_AUTOEND );
     682             : 
     683             :     /*  Start, end, and interval are stored in 3 separate item records. Type of
     684             :         the items is dependent on numeric/date mode. Numeric groups expect
     685             :         three PCITEM_DOUBLE records, date groups expect two PCITEM_DATE records
     686             :         and one PCITEM_INT record. */
     687           0 :     PivotCacheItemList aLimits( *this );
     688           0 :     aLimits.importItemList( rStrm, 3 );
     689             :     OSL_ENSURE( aLimits.size() == 3, "PivotCacheField::importPCDFRangePr - missing grouping records" );
     690           0 :     const PivotCacheItem* pStartValue = aLimits.getCacheItem( 0 );
     691           0 :     const PivotCacheItem* pEndValue = aLimits.getCacheItem( 1 );
     692           0 :     const PivotCacheItem* pInterval = aLimits.getCacheItem( 2 );
     693           0 :     if( pStartValue && pEndValue && pInterval )
     694             :     {
     695           0 :         if( maFieldGroupModel.mbDateGroup )
     696             :         {
     697           0 :             bool bHasTypes = (pStartValue->getType() == XML_d) && (pEndValue->getType() == XML_d) && (pInterval->getType() == XML_i);
     698             :             OSL_ENSURE( bHasTypes, "PivotCacheField::importPCDFRangePr - wrong data types in grouping items" );
     699           0 :             if( bHasTypes )
     700             :             {
     701           0 :                 maFieldGroupModel.maStartDate = pStartValue->getValue().get< css::util::DateTime >();
     702           0 :                 maFieldGroupModel.maEndDate   = pEndValue->getValue().get< css::util::DateTime >();
     703           0 :                 maFieldGroupModel.mfInterval  = pInterval->getValue().get< sal_Int16 >();
     704             :             }
     705             :         }
     706             :         else
     707             :         {
     708           0 :             bool bHasTypes = (pStartValue->getType() == XML_n) && (pEndValue->getType() == XML_n) && (pInterval->getType() == XML_n);
     709             :             OSL_ENSURE( bHasTypes, "PivotCacheField::importPCDFRangePr - wrong data types in grouping items" );
     710           0 :             if( bHasTypes )
     711             :             {
     712           0 :                 maFieldGroupModel.mfStartValue = pStartValue->getValue().get< double >();
     713           0 :                 maFieldGroupModel.mfEndValue   = pEndValue->getValue().get< double >();
     714           0 :                 maFieldGroupModel.mfInterval   = pInterval->getValue().get< double >();
     715             :             }
     716             :         }
     717           0 :     }
     718           0 : }
     719             : 
     720           0 : void PivotCacheField::importPCDFDiscretePr( BiffInputStream& rStrm )
     721             : {
     722           0 :     sal_Int32 nCount = static_cast< sal_Int32 >( rStrm.size() / 2 );
     723           0 :     for( sal_Int32 nIndex = 0; !rStrm.isEof() && (nIndex < nCount); ++nIndex )
     724           0 :         maDiscreteItems.push_back( rStrm.readuInt16() );
     725           0 : }
     726             : 
     727          30 : const PivotCacheItem* PivotCacheField::getCacheItem( sal_Int32 nItemIdx ) const
     728             : {
     729          30 :     if( hasGroupItems() )
     730           0 :         return maGroupItems.getCacheItem( nItemIdx );
     731          30 :     if( hasSharedItems() )
     732          30 :         return maSharedItems.getCacheItem( nItemIdx );
     733           0 :     return 0;
     734             : }
     735             : 
     736           0 : void PivotCacheField::applyItemCaptions( const IdCaptionPairList& vCaptions )
     737             : {
     738           0 :     if( hasGroupItems() )
     739           0 :         maGroupItems.applyItemCaptions( vCaptions );
     740           0 :     if( hasSharedItems() )
     741           0 :         maSharedItems.applyItemCaptions( vCaptions );
     742           0 : }
     743             : 
     744           0 : void PivotCacheField::getCacheItemNames( ::std::vector< OUString >& orItemNames ) const
     745             : {
     746           0 :     if( hasGroupItems() )
     747           0 :         maGroupItems.getCacheItemNames( orItemNames );
     748           0 :     else if( hasSharedItems() )
     749           0 :         maSharedItems.getCacheItemNames( orItemNames );
     750           0 : }
     751             : 
     752           0 : const PivotCacheItemList& PivotCacheField::getCacheItems() const
     753             : {
     754           0 :     if( hasGroupItems() )
     755           0 :         return maGroupItems;
     756           0 :     return maSharedItems;
     757             : }
     758             : 
     759           0 : void PivotCacheField::convertNumericGrouping( const Reference< XDataPilotField >& rxDPField ) const
     760             : {
     761             :     OSL_ENSURE( hasGroupItems() && hasNumericGrouping(), "PivotCacheField::convertNumericGrouping - not a numeric group field" );
     762           0 :     PropertySet aPropSet( rxDPField );
     763           0 :     if( hasGroupItems() && hasNumericGrouping() && aPropSet.is() )
     764             :     {
     765           0 :         DataPilotFieldGroupInfo aGroupInfo;
     766           0 :         aGroupInfo.HasAutoStart  = maFieldGroupModel.mbAutoStart;
     767           0 :         aGroupInfo.HasAutoEnd    = maFieldGroupModel.mbAutoEnd;
     768           0 :         aGroupInfo.HasDateValues = sal_False;
     769           0 :         aGroupInfo.Start         = maFieldGroupModel.mfStartValue;
     770           0 :         aGroupInfo.End           = maFieldGroupModel.mfEndValue;
     771           0 :         aGroupInfo.Step          = maFieldGroupModel.mfInterval;
     772           0 :         aGroupInfo.GroupBy       = 0;
     773           0 :         aPropSet.setProperty( PROP_GroupInfo, aGroupInfo );
     774           0 :     }
     775           0 : }
     776             : 
     777           0 : OUString PivotCacheField::createDateGroupField( const Reference< XDataPilotField >& rxBaseDPField ) const
     778             : {
     779             :     OSL_ENSURE( hasGroupItems() && hasDateGrouping(), "PivotCacheField::createDateGroupField - not a numeric group field" );
     780           0 :     Reference< XDataPilotField > xDPGroupField;
     781           0 :     PropertySet aPropSet( rxBaseDPField );
     782           0 :     if( hasGroupItems() && hasDateGrouping() && aPropSet.is() )
     783             :     {
     784           0 :         bool bDayRanges = (maFieldGroupModel.mnGroupBy == XML_days) && (maFieldGroupModel.mfInterval >= 2.0);
     785             : 
     786           0 :         DataPilotFieldGroupInfo aGroupInfo;
     787           0 :         aGroupInfo.HasAutoStart  = maFieldGroupModel.mbAutoStart;
     788           0 :         aGroupInfo.HasAutoEnd    = maFieldGroupModel.mbAutoEnd;
     789           0 :         aGroupInfo.HasDateValues = sal_True;
     790           0 :         aGroupInfo.Start         = getUnitConverter().calcSerialFromDateTime( maFieldGroupModel.maStartDate );
     791           0 :         aGroupInfo.End           = getUnitConverter().calcSerialFromDateTime( maFieldGroupModel.maEndDate );
     792           0 :         aGroupInfo.Step          = bDayRanges ? maFieldGroupModel.mfInterval : 0.0;
     793             : 
     794             :         using namespace ::com::sun::star::sheet::DataPilotFieldGroupBy;
     795           0 :         switch( maFieldGroupModel.mnGroupBy )
     796             :         {
     797           0 :             case XML_years:     aGroupInfo.GroupBy = YEARS;     break;
     798           0 :             case XML_quarters:  aGroupInfo.GroupBy = QUARTERS;  break;
     799           0 :             case XML_months:    aGroupInfo.GroupBy = MONTHS;    break;
     800           0 :             case XML_days:      aGroupInfo.GroupBy = DAYS;      break;
     801           0 :             case XML_hours:     aGroupInfo.GroupBy = HOURS;     break;
     802           0 :             case XML_minutes:   aGroupInfo.GroupBy = MINUTES;   break;
     803           0 :             case XML_seconds:   aGroupInfo.GroupBy = SECONDS;   break;
     804             :             default:    OSL_FAIL( "PivotCacheField::convertRangeGrouping - unknown date/time interval" );
     805             :         }
     806             : 
     807             :         try
     808             :         {
     809           0 :             Reference< XDataPilotFieldGrouping > xDPGrouping( rxBaseDPField, UNO_QUERY_THROW );
     810           0 :             xDPGroupField = xDPGrouping->createDateGroup( aGroupInfo );
     811             :         }
     812           0 :         catch( Exception& )
     813             :         {
     814           0 :         }
     815             :     }
     816             : 
     817           0 :     Reference< XNamed > xFieldName( xDPGroupField, UNO_QUERY );
     818           0 :     return xFieldName.is() ? xFieldName->getName() : OUString();
     819             : }
     820             : 
     821           0 : OUString PivotCacheField::createParentGroupField( const Reference< XDataPilotField >& rxBaseDPField, const PivotCacheField& rBaseCacheField, PivotCacheGroupItemVector& orItemNames ) const
     822             : {
     823             :     OSL_ENSURE( hasGroupItems() && !maDiscreteItems.empty(), "PivotCacheField::createParentGroupField - not a group field" );
     824             :     OSL_ENSURE( maDiscreteItems.size() == orItemNames.size(), "PivotCacheField::createParentGroupField - number of item names does not match grouping info" );
     825           0 :     Reference< XDataPilotFieldGrouping > xDPGrouping( rxBaseDPField, UNO_QUERY );
     826           0 :     if( !xDPGrouping.is() ) return OUString();
     827             : 
     828             :     // map the group item indexes from maGroupItems to all item indexes from maDiscreteItems
     829             :     typedef ::std::vector< sal_Int32 > GroupItemList;
     830             :     typedef ::std::vector< GroupItemList > GroupItemMap;
     831           0 :     GroupItemMap aItemMap( maGroupItems.size() );
     832           0 :     for( IndexVector::const_iterator aBeg = maDiscreteItems.begin(), aIt = aBeg, aEnd = maDiscreteItems.end(); aIt != aEnd; ++aIt )
     833             :     {
     834           0 :         if( GroupItemList* pItems = ContainerHelper::getVectorElementAccess( aItemMap, *aIt ) )
     835             :         {
     836           0 :             if ( const PivotCacheItem* pItem = rBaseCacheField.getCacheItems().getCacheItem( aIt - aBeg ) )
     837             :             {
     838             :                 // Skip unspecified or ununsed entries or errors
     839           0 :                 if ( pItem->isUnused() || ( pItem->getType() == XML_m ) ||  ( pItem->getType() == XML_e ) )
     840           0 :                     continue;
     841             :             }
     842           0 :             pItems->push_back( static_cast< sal_Int32 >( aIt - aBeg ) );
     843             :         }
     844             :     }
     845             : 
     846             :     // process all groups
     847           0 :     Reference< XDataPilotField > xDPGroupField;
     848           0 :     for( GroupItemMap::iterator aBeg = aItemMap.begin(), aIt = aBeg, aEnd = aItemMap.end(); aIt != aEnd; ++aIt )
     849             :     {
     850             :         OSL_ENSURE( !aIt->empty(), "PivotCacheField::createParentGroupField - item/group should not be empty" );
     851           0 :         if( !aIt->empty() )
     852             :         {
     853             :             /*  Insert the names of the items that are part of this group. Calc
     854             :                 expects the names of the members of the field whose members are
     855             :                 grouped (which may be the names of groups too). Excel provides
     856             :                 the names of the base field items instead (no group names
     857             :                 involved). Therefore, the passed collection of current item
     858             :                 names as they are already grouped is used here to resolve the
     859             :                 item names. */
     860           0 :             ::std::vector< OUString > aMembers;
     861           0 :             for( GroupItemList::iterator aBeg2 = aIt->begin(), aIt2 = aBeg2, aEnd2 = aIt->end(); aIt2 != aEnd2; ++aIt2 )
     862           0 :                 if( const PivotCacheGroupItem* pName = ContainerHelper::getVectorElement( orItemNames, *aIt2 ) )
     863           0 :                     if( ::std::find( aMembers.begin(), aMembers.end(), pName->maGroupName ) == aMembers.end() )
     864           0 :                         aMembers.push_back( pName->maGroupName );
     865             : 
     866             :             /*  Check again, that this is not just a group that is not grouped
     867             :                 further with other items. */
     868           0 :             if( !aMembers.empty() ) try
     869             :             {
     870             :                 // only the first call of createNameGroup() returns the new field
     871           0 :                 Reference< XDataPilotField > xDPNewField = xDPGrouping->createNameGroup( ContainerHelper::vectorToSequence( aMembers ) );
     872             :                 OSL_ENSURE( xDPGroupField.is() != xDPNewField.is(), "PivotCacheField::createParentGroupField - missing group field" );
     873           0 :                 if( !xDPGroupField.is() )
     874           0 :                     xDPGroupField = xDPNewField;
     875             : 
     876             :                 // get current grouping info
     877           0 :                 DataPilotFieldGroupInfo aGroupInfo;
     878           0 :                 PropertySet aPropSet( xDPGroupField );
     879           0 :                 aPropSet.getProperty( aGroupInfo, PROP_GroupInfo );
     880             : 
     881             :                 /*  Find the group object and the auto-generated group name.
     882             :                     The returned field contains all groups derived from the
     883             :                     previous field if that is grouped too. To find the correct
     884             :                     group, the first item used to create the group is serached.
     885             :                     Calc provides the original item names of the base field
     886             :                     when the group is querried for its members. Its does not
     887             :                     provide the names of members that are already groups in the
     888             :                     field used to create the new groups. (Is this a bug?)
     889             :                     Therefore, a name from the passed list of original item
     890             :                     names is used to find the correct group. */
     891           0 :                 OUString aFirstItem;
     892           0 :                 if( const PivotCacheGroupItem* pName = ContainerHelper::getVectorElement( orItemNames, aIt->front() ) )
     893           0 :                     aFirstItem = pName->maOrigName;
     894           0 :                 Reference< XNamed > xGroupName;
     895           0 :                 OUString aAutoName;
     896           0 :                 Reference< XIndexAccess > xGroupsIA( aGroupInfo.Groups, UNO_QUERY_THROW );
     897           0 :                 for( sal_Int32 nIdx = 0, nCount = xGroupsIA->getCount(); (nIdx < nCount) && (aAutoName.isEmpty()); ++nIdx ) try
     898             :                 {
     899           0 :                     Reference< XNameAccess > xItemsNA( xGroupsIA->getByIndex( nIdx ), UNO_QUERY_THROW );
     900           0 :                     if( xItemsNA->hasByName( aFirstItem ) )
     901             :                     {
     902           0 :                         xGroupName.set( xGroupsIA->getByIndex( nIdx ), UNO_QUERY_THROW );
     903           0 :                         aAutoName = xGroupName->getName();
     904           0 :                     }
     905             :                 }
     906           0 :                 catch( Exception& )
     907             :                 {
     908             :                 }
     909             :                 OSL_ENSURE( !aAutoName.isEmpty(), "PivotCacheField::createParentGroupField - cannot find auto-generated group name" );
     910             : 
     911             :                 // get the real group name from the list of group items
     912           0 :                 OUString aGroupName;
     913           0 :                 if( const PivotCacheItem* pGroupItem = maGroupItems.getCacheItem( static_cast< sal_Int32 >( aIt - aBeg ) ) )
     914           0 :                     aGroupName = pGroupItem->getName();
     915             :                 OSL_ENSURE( !aGroupName.isEmpty(), "PivotCacheField::createParentGroupField - cannot find group name" );
     916           0 :                 if( aGroupName.isEmpty() )
     917           0 :                     aGroupName = aAutoName;
     918             : 
     919           0 :                 if( xGroupName.is() && !aGroupName.isEmpty() )
     920             :                 {
     921             :                     // replace the auto-generated group name with the real name
     922           0 :                     if( aAutoName != aGroupName )
     923             :                     {
     924           0 :                         xGroupName->setName( aGroupName );
     925           0 :                         aPropSet.setProperty( PROP_GroupInfo, aGroupInfo );
     926             :                     }
     927             :                     // replace original item names in passed vector with group name
     928           0 :                     for( GroupItemList::iterator aIt2 = aIt->begin(), aEnd2 = aIt->end(); aIt2 != aEnd2; ++aIt2 )
     929           0 :                         if( PivotCacheGroupItem* pName = ContainerHelper::getVectorElementAccess( orItemNames, *aIt2 ) )
     930           0 :                             pName->maGroupName = aGroupName;
     931           0 :                 }
     932             :             }
     933           0 :             catch( Exception& )
     934             :             {
     935           0 :             }
     936             :         }
     937             :     }
     938             : 
     939           0 :     Reference< XNamed > xFieldName( xDPGroupField, UNO_QUERY );
     940           0 :     return xFieldName.is() ? xFieldName->getName() : OUString();
     941             : }
     942             : 
     943           0 : void PivotCacheField::writeSourceHeaderCell( WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow ) const
     944             : {
     945           0 :     CellModel aModel;
     946           0 :     aModel.maCellAddr = CellAddress( rSheetHelper.getSheetIndex(), nCol, nRow );
     947           0 :     rSheetHelper.getSheetData().setStringCell( aModel, maFieldModel.maName );
     948           0 : }
     949             : 
     950           0 : void PivotCacheField::writeSourceDataCell( WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow, const PivotCacheItem& rItem ) const
     951             : {
     952           0 :     bool bHasIndex = rItem.getType() == XML_x;
     953             :     OSL_ENSURE( bHasIndex != maSharedItems.empty(), "PivotCacheField::writeSourceDataCell - shared items missing or not expected" );
     954           0 :     if( bHasIndex )
     955           0 :         writeSharedItemToSourceDataCell( rSheetHelper, nCol, nRow, rItem.getValue().get< sal_Int32 >() );
     956             :     else
     957           0 :         writeItemToSourceDataCell( rSheetHelper, nCol, nRow, rItem );
     958           0 : }
     959             : 
     960           0 : void PivotCacheField::importPCRecordItem( SequenceInputStream& rStrm, WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow ) const
     961             : {
     962           0 :     if( hasSharedItems() )
     963             :     {
     964           0 :         writeSharedItemToSourceDataCell( rSheetHelper, nCol, nRow, rStrm.readInt32() );
     965             :     }
     966             :     else
     967             :     {
     968           0 :         PivotCacheItem aItem;
     969           0 :         if( maSharedItemsModel.mbIsNumeric )
     970           0 :            aItem.readDouble( rStrm );
     971           0 :         else if( maSharedItemsModel.mbHasDate && !maSharedItemsModel.mbHasString )
     972           0 :            aItem.readDate( rStrm );
     973             :         else
     974           0 :            aItem.readString( rStrm );
     975           0 :         writeItemToSourceDataCell( rSheetHelper, nCol, nRow, aItem );
     976             :     }
     977           0 : }
     978             : 
     979           0 : void PivotCacheField::importPCItemIndex( BiffInputStream& rStrm, WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow ) const
     980             : {
     981             :     OSL_ENSURE( hasSharedItems(), "PivotCacheField::importPCItemIndex - invalid call, no shared items found" );
     982           0 :     sal_Int32 nIndex = maSharedItemsModel.mbHasLongIndexes ? rStrm.readuInt16() : rStrm.readuInt8();
     983           0 :     writeSharedItemToSourceDataCell( rSheetHelper, nCol, nRow, nIndex );
     984           0 : }
     985             : 
     986             : // private --------------------------------------------------------------------
     987             : 
     988           0 : void PivotCacheField::writeItemToSourceDataCell( WorksheetHelper& rSheetHelper,
     989             :         sal_Int32 nCol, sal_Int32 nRow, const PivotCacheItem& rItem ) const
     990             : {
     991           0 :     if( rItem.getType() != XML_m )
     992             :     {
     993           0 :         CellModel aModel;
     994           0 :         aModel.maCellAddr = CellAddress( rSheetHelper.getSheetIndex(), nCol, nRow );
     995           0 :         SheetDataBuffer& rSheetData = rSheetHelper.getSheetData();
     996           0 :         switch( rItem.getType() )
     997             :         {
     998           0 :             case XML_s: rSheetData.setStringCell( aModel, rItem.getValue().get< OUString >() );                             break;
     999           0 :             case XML_n: rSheetData.setValueCell( aModel, rItem.getValue().get< double >() );                                break;
    1000           0 :             case XML_i: rSheetData.setValueCell( aModel, rItem.getValue().get< sal_Int16 >() );                             break;
    1001           0 :             case XML_d: rSheetData.setDateTimeCell( aModel, rItem.getValue().get< css::util::DateTime >() );                           break;
    1002           0 :             case XML_b: rSheetData.setBooleanCell( aModel, rItem.getValue().get< bool >() );                                break;
    1003           0 :             case XML_e: rSheetData.setErrorCell( aModel, static_cast< sal_uInt8 >( rItem.getValue().get< sal_Int32 >() ) ); break;
    1004             :             default:    OSL_FAIL( "PivotCacheField::writeItemToSourceDataCell - unexpected item data type" );
    1005             :         }
    1006             :     }
    1007           0 : }
    1008             : 
    1009           0 : void PivotCacheField::writeSharedItemToSourceDataCell(
    1010             :         WorksheetHelper& rSheetHelper, sal_Int32 nCol, sal_Int32 nRow, sal_Int32 nItemIdx ) const
    1011             : {
    1012           0 :     if( const PivotCacheItem* pCacheItem = maSharedItems.getCacheItem( nItemIdx ) )
    1013           0 :         writeItemToSourceDataCell( rSheetHelper, nCol, nRow, *pCacheItem );
    1014           0 : }
    1015             : 
    1016           8 : PCDefinitionModel::PCDefinitionModel() :
    1017             :     mfRefreshedDate( 0.0 ),
    1018             :     mnRecords( 0 ),
    1019             :     mnMissItemsLimit( 0 ),
    1020             :     mnDatabaseFields( 0 ),
    1021             :     mbInvalid( false ),
    1022             :     mbSaveData( true ),
    1023             :     mbRefreshOnLoad( false ),
    1024             :     mbOptimizeMemory( false ),
    1025             :     mbEnableRefresh( true ),
    1026             :     mbBackgroundQuery( false ),
    1027             :     mbUpgradeOnRefresh( false ),
    1028             :     mbTupleCache( false ),
    1029             :     mbSupportSubquery( false ),
    1030           8 :     mbSupportDrill( false )
    1031             : {
    1032           8 : }
    1033             : 
    1034           8 : PCSourceModel::PCSourceModel() :
    1035             :     mnSourceType( XML_TOKEN_INVALID ),
    1036           8 :     mnConnectionId( 0 )
    1037             : {
    1038           8 : }
    1039             : 
    1040           8 : PCWorksheetSourceModel::PCWorksheetSourceModel()
    1041             : {
    1042           8 :     maRange.StartColumn = maRange.StartRow = maRange.EndColumn = maRange.EndRow = -1;
    1043           8 : }
    1044             : 
    1045           8 : PivotCache::PivotCache( const WorkbookHelper& rHelper ) :
    1046             :     WorkbookHelper( rHelper ),
    1047             :     mnCurrRow( -1 ),
    1048             :     mbValidSource( false ),
    1049           8 :     mbDummySheet( false )
    1050             : {
    1051           8 : }
    1052             : 
    1053           8 : void PivotCache::importPivotCacheDefinition( const AttributeList& rAttribs )
    1054             : {
    1055           8 :     maDefModel.maRelId            = rAttribs.getString( R_TOKEN( id ), OUString() );
    1056           8 :     maDefModel.maRefreshedBy      = rAttribs.getXString( XML_refreshedBy, OUString() );
    1057           8 :     maDefModel.mfRefreshedDate    = rAttribs.getDouble( XML_refreshedDate, 0.0 );
    1058           8 :     maDefModel.mnRecords          = rAttribs.getInteger( XML_recordCount, 0 );
    1059           8 :     maDefModel.mnMissItemsLimit   = rAttribs.getInteger( XML_missingItemsLimit, 0 );
    1060           8 :     maDefModel.mbInvalid          = rAttribs.getBool( XML_invalid, false );
    1061           8 :     maDefModel.mbSaveData         = rAttribs.getBool( XML_saveData, true );
    1062           8 :     maDefModel.mbRefreshOnLoad    = rAttribs.getBool( XML_refreshOnLoad, false );
    1063           8 :     maDefModel.mbOptimizeMemory   = rAttribs.getBool( XML_optimizeMemory, false );
    1064           8 :     maDefModel.mbEnableRefresh    = rAttribs.getBool( XML_enableRefresh, true );
    1065           8 :     maDefModel.mbBackgroundQuery  = rAttribs.getBool( XML_backgroundQuery, false );
    1066           8 :     maDefModel.mbUpgradeOnRefresh = rAttribs.getBool( XML_upgradeOnRefresh, false );
    1067           8 :     maDefModel.mbTupleCache       = rAttribs.getBool( XML_tupleCache, false );
    1068           8 :     maDefModel.mbSupportSubquery  = rAttribs.getBool( XML_supportSubquery, false );
    1069           8 :     maDefModel.mbSupportDrill     = rAttribs.getBool( XML_supportAdvancedDrill, false );
    1070           8 : }
    1071             : 
    1072           8 : void PivotCache::importCacheSource( const AttributeList& rAttribs )
    1073             : {
    1074           8 :     maSourceModel.mnSourceType   = rAttribs.getToken( XML_type, XML_TOKEN_INVALID );
    1075           8 :     maSourceModel.mnConnectionId = rAttribs.getInteger( XML_connectionId, 0 );
    1076           8 : }
    1077             : 
    1078           8 : void PivotCache::importWorksheetSource( const AttributeList& rAttribs, const Relations& rRelations )
    1079             : {
    1080           8 :     maSheetSrcModel.maRelId   = rAttribs.getString( R_TOKEN( id ), OUString() );
    1081           8 :     maSheetSrcModel.maSheet   = rAttribs.getXString( XML_sheet, OUString() );
    1082           8 :     maSheetSrcModel.maDefName = rAttribs.getXString( XML_name, OUString() );
    1083             : 
    1084             :     // resolve URL of external document
    1085           8 :     maTargetUrl = rRelations.getExternalTargetFromRelId( maSheetSrcModel.maRelId );
    1086             :     // store range address unchecked with sheet index 0, will be resolved/checked later
    1087           8 :     getAddressConverter().convertToCellRangeUnchecked( maSheetSrcModel.maRange, rAttribs.getString( XML_ref, OUString() ), 0 );
    1088           8 : }
    1089             : 
    1090           0 : void PivotCache::importPCDefinition( SequenceInputStream& rStrm )
    1091             : {
    1092             :     sal_uInt8 nFlags1, nFlags2;
    1093           0 :     rStrm.skip( 3 );    // create/refresh version id's
    1094           0 :     rStrm >> nFlags1 >> maDefModel.mnMissItemsLimit >> maDefModel.mfRefreshedDate >> nFlags2 >> maDefModel.mnRecords;
    1095           0 :     if( getFlag( nFlags2, BIFF12_PCDEFINITION_HASUSERNAME ) )
    1096           0 :         rStrm >> maDefModel.maRefreshedBy;
    1097           0 :     if( getFlag( nFlags2, BIFF12_PCDEFINITION_HASRELID ) )
    1098           0 :         rStrm >> maDefModel.maRelId;
    1099             : 
    1100           0 :     maDefModel.mbInvalid          = getFlag( nFlags1, BIFF12_PCDEFINITION_INVALID );
    1101           0 :     maDefModel.mbSaveData         = getFlag( nFlags1, BIFF12_PCDEFINITION_SAVEDATA );
    1102           0 :     maDefModel.mbRefreshOnLoad    = getFlag( nFlags1, BIFF12_PCDEFINITION_REFRESHONLOAD );
    1103           0 :     maDefModel.mbOptimizeMemory   = getFlag( nFlags1, BIFF12_PCDEFINITION_OPTIMIZEMEMORY );
    1104           0 :     maDefModel.mbEnableRefresh    = getFlag( nFlags1, BIFF12_PCDEFINITION_ENABLEREFRESH );
    1105           0 :     maDefModel.mbBackgroundQuery  = getFlag( nFlags1, BIFF12_PCDEFINITION_BACKGROUNDQUERY );
    1106           0 :     maDefModel.mbUpgradeOnRefresh = getFlag( nFlags1, BIFF12_PCDEFINITION_UPGRADEONREFR );
    1107           0 :     maDefModel.mbTupleCache       = getFlag( nFlags1, BIFF12_PCDEFINITION_TUPELCACHE );
    1108           0 :     maDefModel.mbSupportSubquery  = getFlag( nFlags2, BIFF12_PCDEFINITION_SUPPORTSUBQUERY );
    1109           0 :     maDefModel.mbSupportDrill     = getFlag( nFlags2, BIFF12_PCDEFINITION_SUPPORTDRILL );
    1110           0 : }
    1111             : 
    1112           0 : void PivotCache::importPCDSource( SequenceInputStream& rStrm )
    1113             : {
    1114             :     sal_Int32 nSourceType;
    1115           0 :     rStrm >> nSourceType >> maSourceModel.mnConnectionId;
    1116             :     static const sal_Int32 spnSourceTypes[] = { XML_worksheet, XML_external, XML_consolidation, XML_scenario };
    1117           0 :     maSourceModel.mnSourceType = STATIC_ARRAY_SELECT( spnSourceTypes, nSourceType, XML_TOKEN_INVALID );
    1118           0 : }
    1119             : 
    1120           0 : void PivotCache::importPCDSheetSource( SequenceInputStream& rStrm, const Relations& rRelations )
    1121             : {
    1122             :     sal_uInt8 nIsDefName, nIsBuiltinName, nFlags;
    1123           0 :     rStrm >> nIsDefName >> nIsBuiltinName >> nFlags;
    1124           0 :     if( getFlag( nFlags, BIFF12_PCDWBSOURCE_HASSHEET ) )
    1125           0 :         rStrm >> maSheetSrcModel.maSheet;
    1126           0 :     if( getFlag( nFlags, BIFF12_PCDWBSOURCE_HASRELID ) )
    1127           0 :         rStrm >> maSheetSrcModel.maRelId;
    1128             : 
    1129             :     // read cell range or defined name
    1130           0 :     if( nIsDefName == 0 )
    1131             :     {
    1132           0 :         BinRange aBinRange;
    1133           0 :         rStrm >> aBinRange;
    1134             :         // store range address unchecked with sheet index 0, will be resolved/checked later
    1135           0 :         getAddressConverter().convertToCellRangeUnchecked( maSheetSrcModel.maRange, aBinRange, 0 );
    1136             :     }
    1137             :     else
    1138             :     {
    1139           0 :         rStrm >> maSheetSrcModel.maDefName;
    1140           0 :         if( nIsBuiltinName != 0 )
    1141           0 :             maSheetSrcModel.maDefName = "_xlnm." + maSheetSrcModel.maDefName;
    1142             :     }
    1143             : 
    1144             :     // resolve URL of external document
    1145           0 :     maTargetUrl = rRelations.getExternalTargetFromRelId( maSheetSrcModel.maRelId );
    1146           0 : }
    1147             : 
    1148           0 : void PivotCache::importPCDefinition( BiffInputStream& rStrm )
    1149             : {
    1150             :     sal_uInt16 nFlags, nUserNameLen;
    1151           0 :     rStrm >> maDefModel.mnRecords;
    1152           0 :     rStrm.skip( 2 );    // repeated cache ID
    1153           0 :     rStrm >> nFlags;
    1154           0 :     rStrm.skip( 2 );    // unused
    1155           0 :     rStrm >> maDefModel.mnDatabaseFields;
    1156           0 :     rStrm.skip( 6 );    // total field count, report record count, (repeated) cache type
    1157           0 :     rStrm >> nUserNameLen;
    1158           0 :     if( nUserNameLen != BIFF_PC_NOSTRING )
    1159           0 :         maDefModel.maRefreshedBy = (getBiff() == BIFF8) ?
    1160             :             rStrm.readUniString( nUserNameLen ) :
    1161           0 :             rStrm.readCharArrayUC( nUserNameLen, getTextEncoding() );
    1162             : 
    1163           0 :     maDefModel.mbInvalid          = getFlag( nFlags, BIFF_PCDEFINITION_INVALID );
    1164           0 :     maDefModel.mbSaveData         = getFlag( nFlags, BIFF_PCDEFINITION_SAVEDATA );
    1165           0 :     maDefModel.mbRefreshOnLoad    = getFlag( nFlags, BIFF_PCDEFINITION_REFRESHONLOAD );
    1166           0 :     maDefModel.mbOptimizeMemory   = getFlag( nFlags, BIFF_PCDEFINITION_OPTIMIZEMEMORY );
    1167           0 :     maDefModel.mbEnableRefresh    = getFlag( nFlags, BIFF_PCDEFINITION_ENABLEREFRESH );
    1168           0 :     maDefModel.mbBackgroundQuery  = getFlag( nFlags, BIFF_PCDEFINITION_BACKGROUNDQUERY );
    1169             : 
    1170           0 :     if( (rStrm.getNextRecId() == BIFF_ID_PCDEFINITION2) && rStrm.startNextRecord() )
    1171           0 :         rStrm >> maDefModel.mfRefreshedDate;
    1172           0 : }
    1173             : 
    1174          48 : PivotCacheField& PivotCache::createCacheField( bool bInitDatabaseField )
    1175             : {
    1176          48 :     bool bIsDatabaseField = !bInitDatabaseField || (maFields.size() < maDefModel.mnDatabaseFields);
    1177          48 :     PivotCacheFieldVector::value_type xCacheField( new PivotCacheField( *this, bIsDatabaseField ) );
    1178          48 :     maFields.push_back( xCacheField );
    1179          48 :     return *xCacheField;
    1180             : }
    1181             : 
    1182           8 : void PivotCache::finalizeImport()
    1183             : {
    1184             :     // collect all fields that are based on source data (needed to finalize source data below)
    1185             :     OSL_ENSURE( !maFields.empty(), "PivotCache::finalizeImport - no pivot cache fields found" );
    1186          56 :     for( PivotCacheFieldVector::const_iterator aIt = maFields.begin(), aEnd = maFields.end(); aIt != aEnd; ++aIt )
    1187             :     {
    1188          48 :         if( (*aIt)->isDatabaseField() )
    1189             :         {
    1190             :             OSL_ENSURE( (aIt == maFields.begin()) || (*(aIt - 1))->isDatabaseField(),
    1191             :                 "PivotCache::finalizeImport - database field follows a calculated field" );
    1192          48 :             maDatabaseIndexes.push_back( static_cast< sal_Int32 >( maDatabaseFields.size() ) );
    1193          48 :             maDatabaseFields.push_back( *aIt );
    1194             :         }
    1195             :         else
    1196             :         {
    1197           0 :             maDatabaseIndexes.push_back( -1 );
    1198             :         }
    1199             :     }
    1200             :     OSL_ENSURE( !maDatabaseFields.empty(), "PivotCache::finalizeImport - no pivot cache source fields found" );
    1201             : 
    1202             :     // finalize source data depending on source type
    1203           8 :     switch( maSourceModel.mnSourceType )
    1204             :     {
    1205             :         case XML_worksheet:
    1206             :         {
    1207             :             // decide whether an external document is used
    1208           8 :             bool bInternal = maTargetUrl.isEmpty() && maSheetSrcModel.maRelId.isEmpty();
    1209           8 :             bool bExternal = !maTargetUrl.isEmpty();   // relation ID may be empty, e.g. BIFF import
    1210             :             OSL_ENSURE( bInternal || bExternal, "PivotCache::finalizeImport - invalid external document URL" );
    1211           8 :             if( bInternal )
    1212           8 :                 finalizeInternalSheetSource();
    1213           0 :             else if( bExternal )
    1214           0 :                 finalizeExternalSheetSource();
    1215             :         }
    1216           8 :         break;
    1217             : 
    1218             :         // currently, we only support worksheet data sources
    1219             :         case XML_external:
    1220           0 :         break;
    1221             :         case XML_consolidation:
    1222           0 :         break;
    1223             :         case XML_scenario:
    1224           0 :         break;
    1225             :     }
    1226           8 : }
    1227             : 
    1228           0 : sal_Int32 PivotCache::getCacheFieldCount() const
    1229             : {
    1230           0 :     return static_cast< sal_Int32 >( maFields.size() );
    1231             : }
    1232             : 
    1233         108 : const PivotCacheField* PivotCache::getCacheField( sal_Int32 nFieldIdx ) const
    1234             : {
    1235         108 :     return maFields.get( nFieldIdx ).get();
    1236             : }
    1237             : 
    1238          48 : sal_Int32 PivotCache::getCacheDatabaseIndex( sal_Int32 nFieldIdx ) const
    1239             : {
    1240          48 :     return ContainerHelper::getVectorElement( maDatabaseIndexes, nFieldIdx, -1 );
    1241             : }
    1242             : 
    1243           0 : void PivotCache::writeSourceHeaderCells( WorksheetHelper& rSheetHelper ) const
    1244             : {
    1245             :     OSL_ENSURE( static_cast< size_t >( maSheetSrcModel.maRange.EndColumn - maSheetSrcModel.maRange.StartColumn + 1 ) == maDatabaseFields.size(),
    1246             :         "PivotCache::writeSourceHeaderCells - source cell range width does not match number of source fields" );
    1247           0 :     sal_Int32 nCol = maSheetSrcModel.maRange.StartColumn;
    1248           0 :     sal_Int32 nMaxCol = getAddressConverter().getMaxApiAddress().Column;
    1249           0 :     sal_Int32 nRow = maSheetSrcModel.maRange.StartRow;
    1250           0 :     mnCurrRow = -1;
    1251           0 :     updateSourceDataRow( rSheetHelper, nRow );
    1252           0 :     for( PivotCacheFieldVector::const_iterator aIt = maDatabaseFields.begin(), aEnd = maDatabaseFields.end(); (aIt != aEnd) && (nCol <= nMaxCol); ++aIt, ++nCol )
    1253           0 :         (*aIt)->writeSourceHeaderCell( rSheetHelper, nCol, nRow );
    1254           0 : }
    1255             : 
    1256           0 : void PivotCache::writeSourceDataCell( WorksheetHelper& rSheetHelper, sal_Int32 nColIdx, sal_Int32 nRowIdx, const PivotCacheItem& rItem ) const
    1257             : {
    1258           0 :     sal_Int32 nCol = maSheetSrcModel.maRange.StartColumn + nColIdx;
    1259             :     OSL_ENSURE( (maSheetSrcModel.maRange.StartColumn <= nCol) && (nCol <= maSheetSrcModel.maRange.EndColumn), "PivotCache::writeSourceDataCell - invalid column index" );
    1260           0 :     sal_Int32 nRow = maSheetSrcModel.maRange.StartRow + nRowIdx;
    1261             :     OSL_ENSURE( (maSheetSrcModel.maRange.StartRow < nRow) && (nRow <= maSheetSrcModel.maRange.EndRow), "PivotCache::writeSourceDataCell - invalid row index" );
    1262           0 :     updateSourceDataRow( rSheetHelper, nRow );
    1263           0 :     if( const PivotCacheField* pCacheField = maDatabaseFields.get( nColIdx ).get() )
    1264           0 :         pCacheField->writeSourceDataCell( rSheetHelper, nCol, nRow, rItem );
    1265           0 : }
    1266             : 
    1267           0 : void PivotCache::importPCRecord( SequenceInputStream& rStrm, WorksheetHelper& rSheetHelper, sal_Int32 nRowIdx ) const
    1268             : {
    1269           0 :     sal_Int32 nRow = maSheetSrcModel.maRange.StartRow + nRowIdx;
    1270             :     OSL_ENSURE( (maSheetSrcModel.maRange.StartRow < nRow) && (nRow <= maSheetSrcModel.maRange.EndRow), "PivotCache::importPCRecord - invalid row index" );
    1271           0 :     sal_Int32 nCol = maSheetSrcModel.maRange.StartColumn;
    1272           0 :     sal_Int32 nMaxCol = getAddressConverter().getMaxApiAddress().Column;
    1273           0 :     for( PivotCacheFieldVector::const_iterator aIt = maDatabaseFields.begin(), aEnd = maDatabaseFields.end(); !rStrm.isEof() && (aIt != aEnd) && (nCol <= nMaxCol); ++aIt, ++nCol )
    1274           0 :         (*aIt)->importPCRecordItem( rStrm, rSheetHelper, nCol, nRow );
    1275           0 : }
    1276             : 
    1277           0 : void PivotCache::importPCItemIndexList( BiffInputStream& rStrm, WorksheetHelper& rSheetHelper, sal_Int32 nRowIdx ) const
    1278             : {
    1279           0 :     sal_Int32 nRow = maSheetSrcModel.maRange.StartRow + nRowIdx;
    1280             :     OSL_ENSURE( (maSheetSrcModel.maRange.StartRow < nRow) && (nRow <= maSheetSrcModel.maRange.EndRow), "PivotCache::importPCItemIndexList - invalid row index" );
    1281           0 :     sal_Int32 nCol = maSheetSrcModel.maRange.StartColumn;
    1282           0 :     sal_Int32 nMaxCol = getAddressConverter().getMaxApiAddress().Column;
    1283           0 :     for( PivotCacheFieldVector::const_iterator aIt = maDatabaseFields.begin(), aEnd = maDatabaseFields.end(); !rStrm.isEof() && (aIt != aEnd) && (nCol <= nMaxCol); ++aIt, ++nCol )
    1284           0 :         if( (*aIt)->hasSharedItems() )
    1285           0 :             (*aIt)->importPCItemIndex( rStrm, rSheetHelper, nCol, nRow );
    1286           0 : }
    1287             : 
    1288             : // private --------------------------------------------------------------------
    1289             : 
    1290           8 : void PivotCache::finalizeInternalSheetSource()
    1291             : {
    1292             :     // resolve sheet name to sheet index
    1293           8 :     sal_Int16 nSheet = getWorksheets().getCalcSheetIndex( maSheetSrcModel.maSheet );
    1294             : 
    1295             :     // if cache is based on a defined name or table, try to resolve to cell range
    1296           8 :     if( !maSheetSrcModel.maDefName.isEmpty() )
    1297             :     {
    1298             :         // local or global defined name
    1299           0 :         if( const DefinedName* pDefName = getDefinedNames().getByModelName( maSheetSrcModel.maDefName, nSheet ).get() )
    1300             :         {
    1301           0 :             mbValidSource = pDefName->getAbsoluteRange( maSheetSrcModel.maRange );
    1302             :         }
    1303             :         // table
    1304           0 :         else if( const Table* pTable = getTables().getTable( maSheetSrcModel.maDefName ).get() )
    1305             :         {
    1306             :             // get original range from table, but exclude the totals row(s)
    1307           0 :             maSheetSrcModel.maRange = pTable->getOriginalRange();
    1308           0 :             mbValidSource = (pTable->getHeight() - pTable->getTotalsRows()) > 1;
    1309           0 :             if( mbValidSource )
    1310           0 :                 maSheetSrcModel.maRange.EndRow -= pTable->getTotalsRows();
    1311             :         }
    1312             :     }
    1313             :     // else try the cell range (if the sheet exists)
    1314           8 :     else if( nSheet >= 0 )
    1315             :     {
    1316             :         // insert sheet index into the range, range address will be checked below
    1317           8 :         maSheetSrcModel.maRange.Sheet = nSheet;
    1318           8 :         mbValidSource = true;
    1319             :     }
    1320             :     // else sheet has been deleted, generate the source data from cache
    1321           0 :     else if( !maSheetSrcModel.maSheet.isEmpty() )
    1322             :     {
    1323           0 :         prepareSourceDataSheet();
    1324             :         // return here to skip the source range check below
    1325           8 :         return;
    1326             :     }
    1327             : 
    1328             :     // check range location, do not allow ranges that overflow the sheet partly
    1329           8 :     mbValidSource = mbValidSource &&
    1330          16 :         getAddressConverter().checkCellRange( maSheetSrcModel.maRange, false, true ) &&
    1331          16 :         (maSheetSrcModel.maRange.StartRow < maSheetSrcModel.maRange.EndRow);
    1332             : }
    1333             : 
    1334           0 : void PivotCache::finalizeExternalSheetSource()
    1335             : {
    1336             :     /*  If pivot cache is based on external sheet data, try to restore sheet
    1337             :         data from cache records. No support for external defined names or tables,
    1338             :         sheet name and path to cache records fragment (OOXML only) are required. */
    1339           0 :     bool bHasRelation = (getFilterType() == FILTER_BIFF) || !maDefModel.maRelId.isEmpty();
    1340           0 :     if( bHasRelation && maSheetSrcModel.maDefName.isEmpty() && !maSheetSrcModel.maSheet.isEmpty() )
    1341           0 :         prepareSourceDataSheet();
    1342           0 : }
    1343             : 
    1344           0 : void PivotCache::prepareSourceDataSheet()
    1345             : {
    1346           0 :     CellRangeAddress& rRange = maSheetSrcModel.maRange;
    1347             :     // data will be inserted in top-left cell, sheet index is still set to 0 (will be set below)
    1348           0 :     rRange.EndColumn -= rRange.StartColumn;
    1349           0 :     rRange.StartColumn = 0;
    1350           0 :     rRange.EndRow -= rRange.StartRow;
    1351           0 :     rRange.StartRow = 0;
    1352             :     // check range location, do not allow ranges that overflow the sheet partly
    1353           0 :     if( getAddressConverter().checkCellRange( rRange, false, true ) )
    1354             :     {
    1355           0 :         maColSpans.insert( ValueRange( rRange.StartColumn, rRange.EndColumn ) );
    1356           0 :         OUString aSheetName = "DPCache_" + maSheetSrcModel.maSheet;
    1357           0 :         rRange.Sheet = getWorksheets().insertEmptySheet( aSheetName, false );
    1358           0 :         mbValidSource = mbDummySheet = rRange.Sheet >= 0;
    1359             :     }
    1360           0 : }
    1361             : 
    1362           0 : void PivotCache::updateSourceDataRow( WorksheetHelper& rSheetHelper, sal_Int32 nRow ) const
    1363             : {
    1364           0 :     if( mnCurrRow != nRow )
    1365             :     {
    1366           0 :         rSheetHelper.getSheetData().setColSpans( nRow, maColSpans );
    1367           0 :         mnCurrRow = nRow;
    1368             :     }
    1369           0 : }
    1370             : 
    1371         128 : PivotCacheBuffer::PivotCacheBuffer( const WorkbookHelper& rHelper ) :
    1372         128 :     WorkbookHelper( rHelper )
    1373             : {
    1374         128 : }
    1375             : 
    1376           8 : void PivotCacheBuffer::registerPivotCacheFragment( sal_Int32 nCacheId, const OUString& rFragmentPath )
    1377             : {
    1378             :     OSL_ENSURE( nCacheId >= 0, "PivotCacheBuffer::registerPivotCacheFragment - invalid pivot cache identifier" );
    1379             :     OSL_ENSURE( maFragmentPaths.count( nCacheId ) == 0, "PivotCacheBuffer::registerPivotCacheFragment - fragment path exists already" );
    1380           8 :     if( (nCacheId >= 0) && !rFragmentPath.isEmpty() )
    1381           8 :         maFragmentPaths[ nCacheId ] = rFragmentPath;
    1382           8 : }
    1383             : 
    1384           8 : PivotCache* PivotCacheBuffer::importPivotCacheFragment( sal_Int32 nCacheId )
    1385             : {
    1386           8 :     switch( getFilterType() )
    1387             :     {
    1388             :         /*  OOXML/BIFF12 filter: On first call for the cache ID, the pivot
    1389             :             cache object is created and inserted into maCaches. Then, the cache
    1390             :             definition fragment is read and the cache is returned. On
    1391             :             subsequent calls, the created cache will be found in maCaches and
    1392             :             returned immediately. */
    1393             :         case FILTER_OOXML:
    1394             :         {
    1395             :             // try to find an imported pivot cache
    1396           8 :             if( PivotCache* pCache = maCaches.get( nCacheId ).get() )
    1397           0 :                 return pCache;
    1398             : 
    1399             :             // check if a fragment path exists for the passed cache identifier
    1400           8 :             FragmentPathMap::iterator aIt = maFragmentPaths.find( nCacheId );
    1401           8 :             if( aIt == maFragmentPaths.end() )
    1402           0 :                 return 0;
    1403             : 
    1404             :             /*  Import the cache fragment. This may create a dummy data sheet
    1405             :                 for external sheet sources. */
    1406           8 :             PivotCache& rCache = createPivotCache( nCacheId );
    1407           8 :             importOoxFragment( new PivotCacheDefinitionFragment( *this, aIt->second, rCache ) );
    1408           8 :             return &rCache;
    1409             :         }
    1410             : 
    1411             :         /*  BIFF filter: Pivot table provides 0-based index into list of pivot
    1412             :             cache source links (PIVOTCACHE/PCDSOURCE/... record blocks in
    1413             :             workbook stream). First, this index has to be resolved to the cache
    1414             :             identifier that is used to manage the cache stream names (the
    1415             :             maFragmentPaths member). The cache object itself exists already
    1416             :             before the first call for the cache source index, because source data
    1417             :             link is part of workbook data, not of the cache stream. To detect
    1418             :             subsequent calls with an already initialized cache, the entry in
    1419             :             maFragmentPaths will be removed after reading the cache stream. */
    1420             :         case FILTER_BIFF:
    1421             :         {
    1422             :             /*  Resolve cache index to cache identifier and try to find pivot
    1423             :                 cache. Cache must exist already for a valid cache index. */
    1424           0 :             nCacheId = ContainerHelper::getVectorElement( maCacheIds, nCacheId, -1 );
    1425           0 :             PivotCache* pCache = maCaches.get( nCacheId ).get();
    1426           0 :             if( !pCache )
    1427           0 :                 return 0;
    1428             : 
    1429             :             /*  Try to find fragment path entry (stream name). If missing, the
    1430             :                 stream has been read already, and the cache can be returned. */
    1431           0 :             FragmentPathMap::iterator aIt = maFragmentPaths.find( nCacheId );
    1432           0 :             if( aIt != maFragmentPaths.end() )
    1433             :             {
    1434             :                 /*  Import the cache stream. This may create a dummy data sheet
    1435             :                     for external sheet sources. */
    1436           0 :                 BiffPivotCacheFragment( *this, aIt->second, *pCache ).importFragment();
    1437             :                 // remove the fragment entry to mark that the cache is initialized
    1438           0 :                 maFragmentPaths.erase( aIt );
    1439             :             }
    1440           0 :             return pCache;
    1441             :         }
    1442             : 
    1443             :         case FILTER_UNKNOWN:
    1444             :             OSL_FAIL( "PivotCacheBuffer::importPivotCacheFragment - unknown filter type" );
    1445             :     }
    1446           0 :     return 0;
    1447             : }
    1448             : 
    1449           8 : PivotCache& PivotCacheBuffer::createPivotCache( sal_Int32 nCacheId )
    1450             : {
    1451           8 :     maCacheIds.push_back( nCacheId );
    1452           8 :     PivotCacheMap::mapped_type& rxCache = maCaches[ nCacheId ];
    1453           8 :     rxCache.reset( new PivotCache( *this ) );
    1454           8 :     return *rxCache;
    1455             : }
    1456             : 
    1457             : } // namespace xls
    1458          48 : } // namespace oox
    1459             : 
    1460             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10