1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "oox/xls/pivotcachefragment.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "oox/helper/attributelist.hxx"
31*cdf0e10cSrcweir #include "oox/xls/addressconverter.hxx"
32*cdf0e10cSrcweir #include "oox/xls/biffinputstream.hxx"
33*cdf0e10cSrcweir #include "oox/xls/pivotcachebuffer.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir namespace oox {
36*cdf0e10cSrcweir namespace xls {
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir // ============================================================================
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
41*cdf0e10cSrcweir using namespace ::oox::core;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir using ::rtl::OUString;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir // ============================================================================
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir PivotCacheFieldContext::PivotCacheFieldContext( WorkbookFragmentBase& rFragment, PivotCacheField& rCacheField ) :
48*cdf0e10cSrcweir     WorkbookContextBase( rFragment ),
49*cdf0e10cSrcweir     mrCacheField( rCacheField )
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir }
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir ContextHandlerRef PivotCacheFieldContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir     switch( getCurrentElement() )
56*cdf0e10cSrcweir     {
57*cdf0e10cSrcweir         case XLS_TOKEN( cacheField ):
58*cdf0e10cSrcweir             if( nElement == XLS_TOKEN( sharedItems ) )  { mrCacheField.importSharedItems( rAttribs );   return this; }
59*cdf0e10cSrcweir             if( nElement == XLS_TOKEN( fieldGroup ) )   { mrCacheField.importFieldGroup( rAttribs );    return this; }
60*cdf0e10cSrcweir         break;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir         case XLS_TOKEN( fieldGroup ):
63*cdf0e10cSrcweir             switch( nElement )
64*cdf0e10cSrcweir             {
65*cdf0e10cSrcweir                 case XLS_TOKEN( rangePr ):      mrCacheField.importRangePr( rAttribs );     break;
66*cdf0e10cSrcweir                 case XLS_TOKEN( discretePr ):   return this;
67*cdf0e10cSrcweir                 case XLS_TOKEN( groupItems ):   return this;
68*cdf0e10cSrcweir             }
69*cdf0e10cSrcweir         break;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir         case XLS_TOKEN( sharedItems ):  mrCacheField.importSharedItem( nElement, rAttribs );        break;
72*cdf0e10cSrcweir         case XLS_TOKEN( discretePr ):   mrCacheField.importDiscretePrItem( nElement, rAttribs );    break;
73*cdf0e10cSrcweir         case XLS_TOKEN( groupItems ):   mrCacheField.importGroupItem( nElement, rAttribs );         break;
74*cdf0e10cSrcweir     }
75*cdf0e10cSrcweir     return 0;
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir void PivotCacheFieldContext::onStartElement( const AttributeList& rAttribs )
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir     if( isRootElement() )
81*cdf0e10cSrcweir         mrCacheField.importCacheField( rAttribs );
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir ContextHandlerRef PivotCacheFieldContext::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir     switch( getCurrentElement() )
87*cdf0e10cSrcweir     {
88*cdf0e10cSrcweir         case BIFF12_ID_PCDFIELD:
89*cdf0e10cSrcweir             switch( nRecId )
90*cdf0e10cSrcweir             {
91*cdf0e10cSrcweir                 case BIFF12_ID_PCDFSHAREDITEMS: mrCacheField.importPCDFSharedItems( rStrm );  return this;
92*cdf0e10cSrcweir                 case BIFF12_ID_PCDFIELDGROUP:   mrCacheField.importPCDFieldGroup( rStrm );    return this;
93*cdf0e10cSrcweir             }
94*cdf0e10cSrcweir         break;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir         case BIFF12_ID_PCDFIELDGROUP:
97*cdf0e10cSrcweir             switch( nRecId )
98*cdf0e10cSrcweir             {
99*cdf0e10cSrcweir                 case BIFF12_ID_PCDFRANGEPR:     mrCacheField.importPCDFRangePr( rStrm );    break;
100*cdf0e10cSrcweir                 case BIFF12_ID_PCDFDISCRETEPR:  return this;
101*cdf0e10cSrcweir                 case BIFF12_ID_PCDFGROUPITEMS:  return this;
102*cdf0e10cSrcweir             }
103*cdf0e10cSrcweir         break;
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir         case BIFF12_ID_PCDFSHAREDITEMS: mrCacheField.importPCDFSharedItem( nRecId, rStrm );     break;
106*cdf0e10cSrcweir         case BIFF12_ID_PCDFDISCRETEPR:  mrCacheField.importPCDFDiscretePrItem( nRecId, rStrm ); break;
107*cdf0e10cSrcweir         case BIFF12_ID_PCDFGROUPITEMS:  mrCacheField.importPCDFGroupItem( nRecId, rStrm );      break;
108*cdf0e10cSrcweir     }
109*cdf0e10cSrcweir     return 0;
110*cdf0e10cSrcweir }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir void PivotCacheFieldContext::onStartRecord( SequenceInputStream& rStrm )
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir     if( isRootElement() )
115*cdf0e10cSrcweir         mrCacheField.importPCDField( rStrm );
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir // ============================================================================
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir PivotCacheDefinitionFragment::PivotCacheDefinitionFragment(
121*cdf0e10cSrcweir         const WorkbookHelper& rHelper, const OUString& rFragmentPath, PivotCache& rPivotCache ) :
122*cdf0e10cSrcweir     WorkbookFragmentBase( rHelper, rFragmentPath ),
123*cdf0e10cSrcweir     mrPivotCache( rPivotCache )
124*cdf0e10cSrcweir {
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir ContextHandlerRef PivotCacheDefinitionFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
128*cdf0e10cSrcweir {
129*cdf0e10cSrcweir     switch( getCurrentElement() )
130*cdf0e10cSrcweir     {
131*cdf0e10cSrcweir         case XML_ROOT_CONTEXT:
132*cdf0e10cSrcweir             if( nElement == XLS_TOKEN( pivotCacheDefinition ) ) { mrPivotCache.importPivotCacheDefinition( rAttribs ); return this; }
133*cdf0e10cSrcweir         break;
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir         case XLS_TOKEN( pivotCacheDefinition ):
136*cdf0e10cSrcweir             switch( nElement )
137*cdf0e10cSrcweir             {
138*cdf0e10cSrcweir                 case XLS_TOKEN( cacheSource ):  mrPivotCache.importCacheSource( rAttribs ); return this;
139*cdf0e10cSrcweir                 case XLS_TOKEN( cacheFields ):  return this;
140*cdf0e10cSrcweir             }
141*cdf0e10cSrcweir         break;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir         case XLS_TOKEN( cacheSource ):
144*cdf0e10cSrcweir             if( nElement == XLS_TOKEN( worksheetSource ) ) mrPivotCache.importWorksheetSource( rAttribs, getRelations() );
145*cdf0e10cSrcweir         break;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         case XLS_TOKEN( cacheFields ):
148*cdf0e10cSrcweir             if( nElement == XLS_TOKEN( cacheField ) ) return new PivotCacheFieldContext( *this, mrPivotCache.createCacheField() );
149*cdf0e10cSrcweir         break;
150*cdf0e10cSrcweir     }
151*cdf0e10cSrcweir     return 0;
152*cdf0e10cSrcweir }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir ContextHandlerRef PivotCacheDefinitionFragment::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
155*cdf0e10cSrcweir {
156*cdf0e10cSrcweir     switch( getCurrentElement() )
157*cdf0e10cSrcweir     {
158*cdf0e10cSrcweir         case XML_ROOT_CONTEXT:
159*cdf0e10cSrcweir             if( nRecId == BIFF12_ID_PCDEFINITION ) { mrPivotCache.importPCDefinition( rStrm ); return this; }
160*cdf0e10cSrcweir         break;
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir         case BIFF12_ID_PCDEFINITION:
163*cdf0e10cSrcweir             switch( nRecId )
164*cdf0e10cSrcweir             {
165*cdf0e10cSrcweir                 case BIFF12_ID_PCDSOURCE: mrPivotCache.importPCDSource( rStrm ); return this;
166*cdf0e10cSrcweir                 case BIFF12_ID_PCDFIELDS: return this;
167*cdf0e10cSrcweir             }
168*cdf0e10cSrcweir         break;
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir         case BIFF12_ID_PCDSOURCE:
171*cdf0e10cSrcweir             if( nRecId == BIFF12_ID_PCDSHEETSOURCE ) mrPivotCache.importPCDSheetSource( rStrm, getRelations() );
172*cdf0e10cSrcweir         break;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         case BIFF12_ID_PCDFIELDS:
175*cdf0e10cSrcweir             if( nRecId == BIFF12_ID_PCDFIELD ) return new PivotCacheFieldContext( *this, mrPivotCache.createCacheField() );
176*cdf0e10cSrcweir         break;
177*cdf0e10cSrcweir     }
178*cdf0e10cSrcweir     return 0;
179*cdf0e10cSrcweir }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir const RecordInfo* PivotCacheDefinitionFragment::getRecordInfos() const
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir     static const RecordInfo spRecInfos[] =
184*cdf0e10cSrcweir     {
185*cdf0e10cSrcweir         { BIFF12_ID_PCDEFINITION,       BIFF12_ID_PCDEFINITION + 1      },
186*cdf0e10cSrcweir         { BIFF12_ID_PCDFDISCRETEPR,     BIFF12_ID_PCDFDISCRETEPR + 1    },
187*cdf0e10cSrcweir         { BIFF12_ID_PCDFGROUPITEMS,     BIFF12_ID_PCDFGROUPITEMS + 1    },
188*cdf0e10cSrcweir         { BIFF12_ID_PCDFIELD,           BIFF12_ID_PCDFIELD + 1          },
189*cdf0e10cSrcweir         { BIFF12_ID_PCDFIELDGROUP,      BIFF12_ID_PCDFIELDGROUP + 1     },
190*cdf0e10cSrcweir         { BIFF12_ID_PCDFIELDS,          BIFF12_ID_PCDFIELDS + 1         },
191*cdf0e10cSrcweir         { BIFF12_ID_PCDFRANGEPR,        BIFF12_ID_PCDFRANGEPR + 1       },
192*cdf0e10cSrcweir         { BIFF12_ID_PCDFSHAREDITEMS,    BIFF12_ID_PCDFSHAREDITEMS + 1   },
193*cdf0e10cSrcweir         { BIFF12_ID_PCITEM_ARRAY,       BIFF12_ID_PCITEM_ARRAY + 1      },
194*cdf0e10cSrcweir         { BIFF12_ID_PCDSHEETSOURCE,     BIFF12_ID_PCDSHEETSOURCE + 1    },
195*cdf0e10cSrcweir         { BIFF12_ID_PCDSOURCE,          BIFF12_ID_PCDSOURCE + 1         },
196*cdf0e10cSrcweir         { -1,                           -1                              }
197*cdf0e10cSrcweir     };
198*cdf0e10cSrcweir     return spRecInfos;
199*cdf0e10cSrcweir }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir void PivotCacheDefinitionFragment::finalizeImport()
202*cdf0e10cSrcweir {
203*cdf0e10cSrcweir     // finalize the cache (check source range etc.)
204*cdf0e10cSrcweir     mrPivotCache.finalizeImport();
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir     // load the cache records, if the cache is based on a deleted or an external worksheet
207*cdf0e10cSrcweir     if( mrPivotCache.isValidDataSource() && mrPivotCache.isBasedOnDummySheet() )
208*cdf0e10cSrcweir     {
209*cdf0e10cSrcweir         OUString aRecFragmentPath = getRelations().getFragmentPathFromRelId( mrPivotCache.getRecordsRelId() );
210*cdf0e10cSrcweir         if( aRecFragmentPath.getLength() > 0 )
211*cdf0e10cSrcweir         {
212*cdf0e10cSrcweir             sal_Int16 nSheet = mrPivotCache.getSourceRange().Sheet;
213*cdf0e10cSrcweir             WorksheetGlobalsRef xSheetGlob = WorksheetHelper::constructGlobals( *this, ISegmentProgressBarRef(), SHEETTYPE_WORKSHEET, nSheet );
214*cdf0e10cSrcweir             if( xSheetGlob.get() )
215*cdf0e10cSrcweir                 importOoxFragment( new PivotCacheRecordsFragment( *xSheetGlob, aRecFragmentPath, mrPivotCache ) );
216*cdf0e10cSrcweir         }
217*cdf0e10cSrcweir     }
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir // ============================================================================
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir PivotCacheRecordsFragment::PivotCacheRecordsFragment( const WorksheetHelper& rHelper,
223*cdf0e10cSrcweir         const OUString& rFragmentPath, const PivotCache& rPivotCache ) :
224*cdf0e10cSrcweir     WorksheetFragmentBase( rHelper, rFragmentPath ),
225*cdf0e10cSrcweir     mrPivotCache( rPivotCache ),
226*cdf0e10cSrcweir     mnColIdx( 0 ),
227*cdf0e10cSrcweir     mnRowIdx( 0 ),
228*cdf0e10cSrcweir     mbInRecord( false )
229*cdf0e10cSrcweir {
230*cdf0e10cSrcweir     // prepare sheet: insert column header names into top row
231*cdf0e10cSrcweir     rPivotCache.writeSourceHeaderCells( *this );
232*cdf0e10cSrcweir }
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir ContextHandlerRef PivotCacheRecordsFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
235*cdf0e10cSrcweir {
236*cdf0e10cSrcweir     switch( getCurrentElement() )
237*cdf0e10cSrcweir     {
238*cdf0e10cSrcweir         case XML_ROOT_CONTEXT:
239*cdf0e10cSrcweir             if( nElement == XLS_TOKEN( pivotCacheRecords ) ) return this;
240*cdf0e10cSrcweir         break;
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir         case XLS_TOKEN( pivotCacheRecords ):
243*cdf0e10cSrcweir             if( nElement == XLS_TOKEN( r ) ) { startCacheRecord(); return this; }
244*cdf0e10cSrcweir         break;
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir         case XLS_TOKEN( r ):
247*cdf0e10cSrcweir         {
248*cdf0e10cSrcweir             PivotCacheItem aItem;
249*cdf0e10cSrcweir             switch( nElement )
250*cdf0e10cSrcweir             {
251*cdf0e10cSrcweir                 case XLS_TOKEN( m ):                                                        break;
252*cdf0e10cSrcweir                 case XLS_TOKEN( s ):    aItem.readString( rAttribs );                       break;
253*cdf0e10cSrcweir                 case XLS_TOKEN( n ):    aItem.readNumeric( rAttribs );                      break;
254*cdf0e10cSrcweir                 case XLS_TOKEN( d ):    aItem.readDate( rAttribs );                         break;
255*cdf0e10cSrcweir                 case XLS_TOKEN( b ):    aItem.readBool( rAttribs );                         break;
256*cdf0e10cSrcweir                 case XLS_TOKEN( e ):    aItem.readError( rAttribs, getUnitConverter() );    break;
257*cdf0e10cSrcweir                 case XLS_TOKEN( x ):    aItem.readIndex( rAttribs );                        break;
258*cdf0e10cSrcweir                 default:    OSL_ENSURE( false, "PivotCacheRecordsFragment::onCreateContext - unexpected element" );
259*cdf0e10cSrcweir             }
260*cdf0e10cSrcweir             mrPivotCache.writeSourceDataCell( *this, mnColIdx, mnRowIdx, aItem );
261*cdf0e10cSrcweir             ++mnColIdx;
262*cdf0e10cSrcweir         }
263*cdf0e10cSrcweir         break;
264*cdf0e10cSrcweir     }
265*cdf0e10cSrcweir     return 0;
266*cdf0e10cSrcweir }
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir ContextHandlerRef PivotCacheRecordsFragment::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
269*cdf0e10cSrcweir {
270*cdf0e10cSrcweir     switch( getCurrentElement() )
271*cdf0e10cSrcweir     {
272*cdf0e10cSrcweir         case XML_ROOT_CONTEXT:
273*cdf0e10cSrcweir             if( nRecId == BIFF12_ID_PCRECORDS ) return this;
274*cdf0e10cSrcweir         break;
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir         case BIFF12_ID_PCRECORDS:
277*cdf0e10cSrcweir             switch( nRecId )
278*cdf0e10cSrcweir             {
279*cdf0e10cSrcweir                 case BIFF12_ID_PCRECORD:    importPCRecord( rStrm );                break;
280*cdf0e10cSrcweir                 case BIFF12_ID_PCRECORDDT:  startCacheRecord();                     break;
281*cdf0e10cSrcweir                 default:                    importPCRecordItem( nRecId, rStrm );    break;
282*cdf0e10cSrcweir             }
283*cdf0e10cSrcweir         break;
284*cdf0e10cSrcweir     }
285*cdf0e10cSrcweir     return 0;
286*cdf0e10cSrcweir }
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir const RecordInfo* PivotCacheRecordsFragment::getRecordInfos() const
289*cdf0e10cSrcweir {
290*cdf0e10cSrcweir     static const RecordInfo spRecInfos[] =
291*cdf0e10cSrcweir     {
292*cdf0e10cSrcweir         { BIFF12_ID_PCRECORDS,  BIFF12_ID_PCRECORDS + 1 },
293*cdf0e10cSrcweir         { -1,                   -1                      }
294*cdf0e10cSrcweir     };
295*cdf0e10cSrcweir     return spRecInfos;
296*cdf0e10cSrcweir }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir // private --------------------------------------------------------------------
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir void PivotCacheRecordsFragment::startCacheRecord()
301*cdf0e10cSrcweir {
302*cdf0e10cSrcweir     mnColIdx = 0;
303*cdf0e10cSrcweir     ++mnRowIdx;
304*cdf0e10cSrcweir     mbInRecord = true;
305*cdf0e10cSrcweir }
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir void PivotCacheRecordsFragment::importPCRecord( SequenceInputStream& rStrm )
308*cdf0e10cSrcweir {
309*cdf0e10cSrcweir     startCacheRecord();
310*cdf0e10cSrcweir     mrPivotCache.importPCRecord( rStrm, *this, mnRowIdx );
311*cdf0e10cSrcweir     mbInRecord = false;
312*cdf0e10cSrcweir }
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir void PivotCacheRecordsFragment::importPCRecordItem( sal_Int32 nRecId, SequenceInputStream& rStrm )
315*cdf0e10cSrcweir {
316*cdf0e10cSrcweir     if( mbInRecord )
317*cdf0e10cSrcweir     {
318*cdf0e10cSrcweir         PivotCacheItem aItem;
319*cdf0e10cSrcweir         switch( nRecId )
320*cdf0e10cSrcweir         {
321*cdf0e10cSrcweir             case BIFF12_ID_PCITEM_MISSING:                              break;
322*cdf0e10cSrcweir             case BIFF12_ID_PCITEM_STRING:   aItem.readString( rStrm );  break;
323*cdf0e10cSrcweir             case BIFF12_ID_PCITEM_DOUBLE:   aItem.readDouble( rStrm );  break;
324*cdf0e10cSrcweir             case BIFF12_ID_PCITEM_DATE:     aItem.readDate( rStrm );    break;
325*cdf0e10cSrcweir             case BIFF12_ID_PCITEM_BOOL:     aItem.readBool( rStrm );    break;
326*cdf0e10cSrcweir             case BIFF12_ID_PCITEM_ERROR:    aItem.readError( rStrm );   break;
327*cdf0e10cSrcweir             case BIFF12_ID_PCITEM_INDEX:    aItem.readIndex( rStrm );   break;
328*cdf0e10cSrcweir             default:    OSL_ENSURE( false, "PivotCacheRecordsFragment::importPCRecordItem - unexpected record" );
329*cdf0e10cSrcweir         }
330*cdf0e10cSrcweir         mrPivotCache.writeSourceDataCell( *this, mnColIdx, mnRowIdx, aItem );
331*cdf0e10cSrcweir         ++mnColIdx;
332*cdf0e10cSrcweir     }
333*cdf0e10cSrcweir }
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir // ============================================================================
336*cdf0e10cSrcweir // ============================================================================
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir namespace {
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir bool lclSeekToPCDField( BiffInputStream& rStrm )
341*cdf0e10cSrcweir {
342*cdf0e10cSrcweir     sal_Int64 nRecHandle = rStrm.getRecHandle();
343*cdf0e10cSrcweir     while( rStrm.startNextRecord() )
344*cdf0e10cSrcweir         if( rStrm.getRecId() == BIFF_ID_PCDFIELD )
345*cdf0e10cSrcweir             return true;
346*cdf0e10cSrcweir     rStrm.startRecordByHandle( nRecHandle );
347*cdf0e10cSrcweir     return false;
348*cdf0e10cSrcweir }
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir } // namespace
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir // ----------------------------------------------------------------------------
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir BiffPivotCacheFragment::BiffPivotCacheFragment(
355*cdf0e10cSrcweir         const WorkbookHelper& rHelper, const OUString& rStrmName, PivotCache& rPivotCache ) :
356*cdf0e10cSrcweir     BiffWorkbookFragmentBase( rHelper, rStrmName, true ),
357*cdf0e10cSrcweir     mrPivotCache( rPivotCache )
358*cdf0e10cSrcweir {
359*cdf0e10cSrcweir }
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir bool BiffPivotCacheFragment::importFragment()
362*cdf0e10cSrcweir {
363*cdf0e10cSrcweir     BiffInputStream& rStrm = getInputStream();
364*cdf0e10cSrcweir     if( rStrm.startNextRecord() && (rStrm.getRecId() == BIFF_ID_PCDEFINITION) )
365*cdf0e10cSrcweir     {
366*cdf0e10cSrcweir         // read PCDEFINITION and optional PCDEFINITION2 records
367*cdf0e10cSrcweir         mrPivotCache.importPCDefinition( rStrm );
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir         // read cache fields as long as another PCDFIELD record can be found
370*cdf0e10cSrcweir         while( lclSeekToPCDField( rStrm ) )
371*cdf0e10cSrcweir             mrPivotCache.createCacheField( true ).importPCDField( rStrm );
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir         // finalize the cache (check source range etc.)
374*cdf0e10cSrcweir         mrPivotCache.finalizeImport();
375*cdf0e10cSrcweir 
376*cdf0e10cSrcweir         // load the cache records, if the cache is based on a deleted or an external worksheet
377*cdf0e10cSrcweir         if( mrPivotCache.isValidDataSource() && mrPivotCache.isBasedOnDummySheet() )
378*cdf0e10cSrcweir         {
379*cdf0e10cSrcweir             /*  Last call of lclSeekToPCDField() failed and kept stream position
380*cdf0e10cSrcweir                 unchanged. Stream should point to source data table now. */
381*cdf0e10cSrcweir             sal_Int16 nSheet = mrPivotCache.getSourceRange().Sheet;
382*cdf0e10cSrcweir             WorksheetGlobalsRef xSheetGlob = WorksheetHelper::constructGlobals( *this, ISegmentProgressBarRef(), SHEETTYPE_WORKSHEET, nSheet );
383*cdf0e10cSrcweir             if( xSheetGlob.get() )
384*cdf0e10cSrcweir             {
385*cdf0e10cSrcweir                 BiffPivotCacheRecordsContext aContext( *xSheetGlob, mrPivotCache );
386*cdf0e10cSrcweir                 while( rStrm.startNextRecord() && (rStrm.getRecId() != BIFF_ID_EOF) )
387*cdf0e10cSrcweir                     aContext.importRecord( rStrm );
388*cdf0e10cSrcweir             }
389*cdf0e10cSrcweir         }
390*cdf0e10cSrcweir     }
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir     return rStrm.getRecId() == BIFF_ID_EOF;
393*cdf0e10cSrcweir }
394*cdf0e10cSrcweir 
395*cdf0e10cSrcweir // ============================================================================
396*cdf0e10cSrcweir 
397*cdf0e10cSrcweir BiffPivotCacheRecordsContext::BiffPivotCacheRecordsContext( const WorksheetHelper& rHelper, const PivotCache& rPivotCache ) :
398*cdf0e10cSrcweir     BiffWorksheetContextBase( rHelper ),
399*cdf0e10cSrcweir     mrPivotCache( rPivotCache ),
400*cdf0e10cSrcweir     mnColIdx( 0 ),
401*cdf0e10cSrcweir     mnRowIdx( 0 ),
402*cdf0e10cSrcweir     mbHasShared( false ),
403*cdf0e10cSrcweir     mbInRow( false )
404*cdf0e10cSrcweir {
405*cdf0e10cSrcweir     // prepare sheet: insert column header names into top row
406*cdf0e10cSrcweir     mrPivotCache.writeSourceHeaderCells( *this );
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir     // find all fields without shared items, remember column indexes in source data
409*cdf0e10cSrcweir     for( sal_Int32 nFieldIdx = 0, nFieldCount = mrPivotCache.getCacheFieldCount(), nCol = 0; nFieldIdx < nFieldCount; ++nFieldIdx )
410*cdf0e10cSrcweir     {
411*cdf0e10cSrcweir         const PivotCacheField* pCacheField = mrPivotCache.getCacheField( nFieldIdx );
412*cdf0e10cSrcweir         if( pCacheField && pCacheField->isDatabaseField() )
413*cdf0e10cSrcweir         {
414*cdf0e10cSrcweir             if( pCacheField->hasSharedItems() )
415*cdf0e10cSrcweir                 mbHasShared = true;
416*cdf0e10cSrcweir             else
417*cdf0e10cSrcweir                 maUnsharedCols.push_back( nCol );
418*cdf0e10cSrcweir             ++nCol;
419*cdf0e10cSrcweir         }
420*cdf0e10cSrcweir     }
421*cdf0e10cSrcweir }
422*cdf0e10cSrcweir 
423*cdf0e10cSrcweir void BiffPivotCacheRecordsContext::importRecord( BiffInputStream& rStrm )
424*cdf0e10cSrcweir {
425*cdf0e10cSrcweir     if( rStrm.getRecId() == BIFF_ID_PCITEM_INDEXLIST )
426*cdf0e10cSrcweir     {
427*cdf0e10cSrcweir         OSL_ENSURE( mbHasShared, "BiffPivotCacheRecordsContext::importRecord - unexpected PCITEM_INDEXLIST record" );
428*cdf0e10cSrcweir         // PCITEM_INDEXLIST record always in front of a new data row
429*cdf0e10cSrcweir         startNextRow();
430*cdf0e10cSrcweir         mrPivotCache.importPCItemIndexList( rStrm, *this, mnRowIdx );
431*cdf0e10cSrcweir         mbInRow = !maUnsharedCols.empty();  // mbInRow remains true, if unshared items are expected
432*cdf0e10cSrcweir         return;
433*cdf0e10cSrcweir     }
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir     PivotCacheItem aItem;
436*cdf0e10cSrcweir     switch( rStrm.getRecId() )
437*cdf0e10cSrcweir     {
438*cdf0e10cSrcweir         case BIFF_ID_PCITEM_MISSING:                                        break;
439*cdf0e10cSrcweir         case BIFF_ID_PCITEM_STRING:     aItem.readString( rStrm, *this );   break;
440*cdf0e10cSrcweir         case BIFF_ID_PCITEM_DOUBLE:     aItem.readDouble( rStrm );          break;
441*cdf0e10cSrcweir         case BIFF_ID_PCITEM_INTEGER:    aItem.readInteger( rStrm );         break;
442*cdf0e10cSrcweir         case BIFF_ID_PCITEM_DATE:       aItem.readDate( rStrm );            break;
443*cdf0e10cSrcweir         case BIFF_ID_PCITEM_BOOL:       aItem.readBool( rStrm );            break;
444*cdf0e10cSrcweir         case BIFF_ID_PCITEM_ERROR:      aItem.readError( rStrm );           break;
445*cdf0e10cSrcweir         default:                        return; // unknown record, ignore
446*cdf0e10cSrcweir     }
447*cdf0e10cSrcweir 
448*cdf0e10cSrcweir     // find next column index, might start new row if no fields with shared items exist
449*cdf0e10cSrcweir     if( mbInRow && (mnColIdx == maUnsharedCols.size()) )
450*cdf0e10cSrcweir     {
451*cdf0e10cSrcweir         OSL_ENSURE( !mbHasShared, "BiffPivotCacheRecordsContext::importRecord - PCITEM_INDEXLIST record missing" );
452*cdf0e10cSrcweir         mbInRow = mbHasShared;  // do not leave current row if PCITEM_INDEXLIST is expected
453*cdf0e10cSrcweir     }
454*cdf0e10cSrcweir     // start next row on first call, or on row wrap without shared items
455*cdf0e10cSrcweir     if( !mbInRow )
456*cdf0e10cSrcweir         startNextRow();
457*cdf0e10cSrcweir 
458*cdf0e10cSrcweir     // write the item data to the sheet cell
459*cdf0e10cSrcweir     OSL_ENSURE( mnColIdx < maUnsharedCols.size(), "BiffPivotCacheRecordsContext::importRecord - invalid column index" );
460*cdf0e10cSrcweir     if( mnColIdx < maUnsharedCols.size() )
461*cdf0e10cSrcweir         mrPivotCache.writeSourceDataCell( *this, maUnsharedCols[ mnColIdx ], mnRowIdx, aItem );
462*cdf0e10cSrcweir     ++mnColIdx;
463*cdf0e10cSrcweir }
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir void BiffPivotCacheRecordsContext::startNextRow()
466*cdf0e10cSrcweir {
467*cdf0e10cSrcweir     mnColIdx = 0;
468*cdf0e10cSrcweir     ++mnRowIdx;
469*cdf0e10cSrcweir     mbInRow = true;
470*cdf0e10cSrcweir }
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir // ============================================================================
473*cdf0e10cSrcweir 
474*cdf0e10cSrcweir } // namespace xls
475*cdf0e10cSrcweir } // namespace oox
476