1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef CHART2_INTERNALDATAPROVIDER_HXX
24 #define CHART2_INTERNALDATAPROVIDER_HXX
25 
26 #include "InternalData.hxx"
27 
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/chart/XDateCategories.hpp>
30 #include <com/sun/star/chart2/XAnyDescriptionAccess.hpp>
31 #include <com/sun/star/chart2/data/XDataProvider.hpp>
32 #include <com/sun/star/chart2/XInternalDataProvider.hpp>
33 #include <com/sun/star/chart2/data/XLabeledDataSequence.hpp>
34 #include <com/sun/star/chart2/data/XRangeXMLConversion.hpp>
35 #include <com/sun/star/chart2/XChartDocument.hpp>
36 #include <com/sun/star/lang/XInitialization.hpp>
37 #include <com/sun/star/util/XCloneable.hpp>
38 #include <cppuhelper/implbase7.hxx>
39 #include "ServiceMacros.hxx"
40 
41 #include "CachedDataSequence.hxx"
42 
43 #include <map>
44 #include <memory>
45 
46 namespace chart
47 {
48 
49 namespace impl
50 {
51 
52 typedef ::cppu::WeakImplHelper7<
53         ::com::sun::star::chart2::XInternalDataProvider,
54         ::com::sun::star::chart2::data::XRangeXMLConversion,
55         ::com::sun::star::chart2::XAnyDescriptionAccess,
56         ::com::sun::star::chart::XDateCategories,
57         ::com::sun::star::util::XCloneable,
58 		::com::sun::star::lang::XInitialization,
59         ::com::sun::star::lang::XServiceInfo >
60     InternalDataProvider_Base;
61 }
62 
63 /** Data provider that handles data internally.  This is used for charts with
64     their own data.
65 
66     <p>The format for single ranges is "categories|label n|n" where n is a
67     non-negative number. Meaning return all categories, the label of sequence n,
68     or the data of sequence n.</p>
69 
70     <p>The format for a complete range is "all". (Do we need more than
71     that?)</p>
72  */
73 class InternalDataProvider :
74         public impl::InternalDataProvider_Base
75 {
76 public:
77     explicit InternalDataProvider(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > & _xContext);
78     explicit InternalDataProvider( const ::com::sun::star::uno::Reference<
79                                        ::com::sun::star::chart2::XChartDocument > & xChartDoc, bool bConnectToModel );
80     explicit InternalDataProvider( const InternalDataProvider & rOther );
81 	virtual ~InternalDataProvider();
82 
83     void createDefaultData();
84 
85     /// declare XServiceInfo methods
86     APPHELPER_XSERVICEINFO_DECL()
87     APPHELPER_SERVICE_FACTORY_HELPER(InternalDataProvider)
88 
89     // ____ XInternalDataProvider ____
90     virtual ::sal_Bool SAL_CALL hasDataByRangeRepresentation( const ::rtl::OUString& aRange )
91         throw (::com::sun::star::uno::RuntimeException);
92     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > SAL_CALL
93         getDataByRangeRepresentation( const ::rtl::OUString& aRange )
94         throw (::com::sun::star::uno::RuntimeException);
95     virtual void SAL_CALL setDataByRangeRepresentation(
96         const ::rtl::OUString& aRange,
97         const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aNewData )
98         throw (::com::sun::star::uno::RuntimeException);
99     virtual void SAL_CALL insertSequence( ::sal_Int32 nAfterIndex )
100         throw (::com::sun::star::uno::RuntimeException);
101     virtual void SAL_CALL deleteSequence( ::sal_Int32 nAtIndex )
102         throw (::com::sun::star::uno::RuntimeException);
103     virtual void SAL_CALL appendSequence()
104         throw (::com::sun::star::uno::RuntimeException);
105     virtual void SAL_CALL insertComplexCategoryLevel( ::sal_Int32 nLevel )
106         throw (::com::sun::star::uno::RuntimeException);
107     virtual void SAL_CALL deleteComplexCategoryLevel( ::sal_Int32 nLevel )
108         throw (::com::sun::star::uno::RuntimeException);
109     virtual void SAL_CALL insertDataPointForAllSequences( ::sal_Int32 nAfterIndex )
110         throw (::com::sun::star::uno::RuntimeException);
111     virtual void SAL_CALL deleteDataPointForAllSequences( ::sal_Int32 nAtIndex )
112         throw (::com::sun::star::uno::RuntimeException);
113     virtual void SAL_CALL swapDataPointWithNextOneForAllSequences( ::sal_Int32 nAtIndex )
114         throw (::com::sun::star::uno::RuntimeException);
115     virtual void SAL_CALL registerDataSequenceForChanges(
116         const ::com::sun::star::uno::Reference< ::com::sun::star::chart2::data::XDataSequence >& xSeq )
117         throw (::com::sun::star::uno::RuntimeException);
118 
119     // ____ XDataProvider (base of XInternalDataProvider) ____
120     virtual ::sal_Bool SAL_CALL createDataSourcePossible(
121         const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& aArguments )
122         throw (::com::sun::star::uno::RuntimeException);
123     virtual ::com::sun::star::uno::Reference< ::com::sun::star::chart2::data::XDataSource > SAL_CALL createDataSource(
124         const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& aArguments )
125         throw (::com::sun::star::lang::IllegalArgumentException,
126                ::com::sun::star::uno::RuntimeException);
127     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > SAL_CALL detectArguments(
128         const ::com::sun::star::uno::Reference< ::com::sun::star::chart2::data::XDataSource >& xDataSource )
129         throw (::com::sun::star::uno::RuntimeException);
130     virtual ::sal_Bool SAL_CALL createDataSequenceByRangeRepresentationPossible(
131         const ::rtl::OUString& aRangeRepresentation )
132         throw (::com::sun::star::uno::RuntimeException);
133     virtual ::com::sun::star::uno::Reference< ::com::sun::star::chart2::data::XDataSequence > SAL_CALL createDataSequenceByRangeRepresentation(
134         const ::rtl::OUString& aRangeRepresentation )
135         throw (::com::sun::star::lang::IllegalArgumentException,
136                ::com::sun::star::uno::RuntimeException);
137     virtual ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XRangeSelection > SAL_CALL getRangeSelection()
138         throw (::com::sun::star::uno::RuntimeException);
139 
140     // ____ XRangeXMLConversion ____
141     virtual ::rtl::OUString SAL_CALL convertRangeToXML(
142         const ::rtl::OUString& aRangeRepresentation )
143         throw (::com::sun::star::lang::IllegalArgumentException,
144                ::com::sun::star::uno::RuntimeException);
145     virtual ::rtl::OUString SAL_CALL convertRangeFromXML(
146         const ::rtl::OUString& aXMLRange )
147         throw (::com::sun::star::lang::IllegalArgumentException,
148                ::com::sun::star::uno::RuntimeException);
149 
150     // ____ XDateCategories ____
151     virtual ::com::sun::star::uno::Sequence< double > SAL_CALL getDateCategories() throw (::com::sun::star::uno::RuntimeException);
152     virtual void SAL_CALL setDateCategories( const ::com::sun::star::uno::Sequence< double >& rDates ) throw (::com::sun::star::uno::RuntimeException);
153 
154     // ____ XAnyDescriptionAccess ____
155     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > > SAL_CALL
156         getAnyRowDescriptions() throw (::com::sun::star::uno::RuntimeException);
157     virtual void SAL_CALL setAnyRowDescriptions(
158         const ::com::sun::star::uno::Sequence<
159         ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > >& aRowDescriptions )
160         throw (::com::sun::star::uno::RuntimeException);
161     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > > SAL_CALL
162         getAnyColumnDescriptions() throw (::com::sun::star::uno::RuntimeException);
163     virtual void SAL_CALL setAnyColumnDescriptions(
164         const ::com::sun::star::uno::Sequence<
165         ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > >& aColumnDescriptions )
166         throw (::com::sun::star::uno::RuntimeException);
167 
168     // ____ XComplexDescriptionAccess (base of XAnyDescriptionAccess) ____
169     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::rtl::OUString > > SAL_CALL
170         getComplexRowDescriptions() throw (::com::sun::star::uno::RuntimeException);
171     virtual void SAL_CALL setComplexRowDescriptions(
172         const ::com::sun::star::uno::Sequence<
173         ::com::sun::star::uno::Sequence< ::rtl::OUString > >& aRowDescriptions )
174         throw (::com::sun::star::uno::RuntimeException);
175     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::rtl::OUString > > SAL_CALL
176         getComplexColumnDescriptions() throw (::com::sun::star::uno::RuntimeException);
177     virtual void SAL_CALL setComplexColumnDescriptions(
178         const ::com::sun::star::uno::Sequence<
179         ::com::sun::star::uno::Sequence< ::rtl::OUString > >& aColumnDescriptions )
180         throw (::com::sun::star::uno::RuntimeException);
181 
182     // ____ XChartDataArray (base of XComplexDescriptionAccess) ____
183     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< double > > SAL_CALL getData()
184         throw (::com::sun::star::uno::RuntimeException);
185     virtual void SAL_CALL setData(
186         const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< double > >& aData )
187         throw (::com::sun::star::uno::RuntimeException);
188     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getRowDescriptions()
189         throw (::com::sun::star::uno::RuntimeException);
190     virtual void SAL_CALL setRowDescriptions(
191         const ::com::sun::star::uno::Sequence< ::rtl::OUString >& aRowDescriptions )
192         throw (::com::sun::star::uno::RuntimeException);
193     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getColumnDescriptions()
194         throw (::com::sun::star::uno::RuntimeException);
195     virtual void SAL_CALL setColumnDescriptions(
196         const ::com::sun::star::uno::Sequence< ::rtl::OUString >& aColumnDescriptions )
197         throw (::com::sun::star::uno::RuntimeException);
198 
199     // ____ XChartData (base of XChartDataArray) ____
200     virtual void SAL_CALL addChartDataChangeEventListener(
201         const ::com::sun::star::uno::Reference< ::com::sun::star::chart::XChartDataChangeEventListener >& aListener )
202         throw (::com::sun::star::uno::RuntimeException);
203     virtual void SAL_CALL removeChartDataChangeEventListener(
204         const ::com::sun::star::uno::Reference< ::com::sun::star::chart::XChartDataChangeEventListener >& aListener )
205         throw (::com::sun::star::uno::RuntimeException);
206     virtual double SAL_CALL getNotANumber()
207         throw (::com::sun::star::uno::RuntimeException);
208     virtual ::sal_Bool SAL_CALL isNotANumber(
209         double nNumber )
210         throw (::com::sun::star::uno::RuntimeException);
211 
212     // ____ XCloneable ____
213     virtual ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloneable > SAL_CALL createClone()
214         throw (::com::sun::star::uno::RuntimeException);
215 	// ::com::sun::star::lang::XInitialization:
216     virtual void SAL_CALL initialize(const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > & aArguments)
217 		throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::uno::Exception);
218 
219 private:
220     void lcl_addDataSequenceToMap(
221         const ::rtl::OUString & rRangeRepresentation,
222         const ::com::sun::star::uno::Reference<
223             ::com::sun::star::chart2::data::XDataSequence > & xSequence );
224 
225     ::com::sun::star::uno::Reference<
226             ::com::sun::star::chart2::data::XDataSequence >
227         lcl_createDataSequenceAndAddToMap( const ::rtl::OUString & rRangeRepresentation,
228                                        const ::rtl::OUString & rRole );
229     ::com::sun::star::uno::Reference<
230             ::com::sun::star::chart2::data::XDataSequence >
231         lcl_createDataSequenceAndAddToMap( const ::rtl::OUString & rRangeRepresentation );
232 
233     void lcl_deleteMapReferences( const ::rtl::OUString & rRangeRepresentation );
234 
235     void lcl_adaptMapReferences(
236         const ::rtl::OUString & rOldRangeRepresentation,
237         const ::rtl::OUString & rNewRangeRepresentation );
238 
239     void lcl_increaseMapReferences( sal_Int32 nBegin, sal_Int32 nEnd );
240     void lcl_decreaseMapReferences( sal_Int32 nBegin, sal_Int32 nEnd );
241 
242     typedef ::std::multimap< ::rtl::OUString,
243             ::com::sun::star::uno::WeakReference< ::com::sun::star::chart2::data::XDataSequence > >
244         tSequenceMap;
245     typedef ::std::pair< tSequenceMap::iterator, tSequenceMap::iterator > tSequenceMapRange;
246     typedef ::std::pair< tSequenceMap::const_iterator, tSequenceMap::const_iterator > tConstSequenceMapRange;
247 
248     /** cache for all sequences that have been returned.
249 
250         If the range-representation of a sequence changes and it is still
251         referred to by some component (weak reference is valid), the range will
252         be adapted.
253      */
254     tSequenceMap m_aSequenceMap;
255     InternalData m_aInternalData;
256     bool m_bDataInColumns;
257 };
258 
259 } //  namespace chart
260 
261 // CHART2_INTERNALDATAPROVIDER_HXX
262 #endif
263