summaryrefslogtreecommitdiff
path: root/ucb/source/ucp/cmis/cmis_datasupplier.cxx
blob: 59ffd73ff609ff27c3300734085e0da65c29079b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Copyright 2012 LibreOffice contributors.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <vector>

#include <ucbhelper/contentidentifier.hxx>
#include <ucbhelper/providerhelper.hxx>

#include <com/sun/star/ucb/OpenMode.hpp>

#include "cmis_datasupplier.hxx"
#include "cmis_content.hxx"
#include "cmis_provider.hxx"

using namespace com::sun::star;
using namespace std;

namespace cmis
{

    typedef std::vector< ResultListEntry* > ResultList;

    DataSupplier::DataSupplier( const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
        const uno::Reference< ::cmis::Content >& rContent, sal_Int32 nOpenMode )
        : mxContent(rContent), m_xSMgr(rxSMgr), mnOpenMode(nOpenMode), mbCountFinal(false)
    {
    }

    bool DataSupplier::getData()
    {
        if ( mbCountFinal )
            return true;

        libcmis::ObjectPtr pObject = mxContent->getObject();
        libcmis::Folder* pFolder = dynamic_cast< libcmis::Folder* >( pObject.get( ) );
        if ( NULL != pFolder )
        {
            // Get the children from pObject
            try
            {
                vector< libcmis::ObjectPtr > children = pFolder->getChildren( );

                // Loop over the results and filter them
                for ( vector< libcmis::ObjectPtr >::iterator it = children.begin();
                        it != children.end(); ++it )
                {
                    bool bIsFolder = ( *it )->getBaseType( ) == "cmis:folder";
                    if ( ( mnOpenMode == ucb::OpenMode::FOLDERS && bIsFolder ) ||
                         ( mnOpenMode == ucb::OpenMode::DOCUMENTS && !bIsFolder ) ||
                         ( mnOpenMode == ucb::OpenMode::ALL ) )
                    {
                        maResults.push_back( new ResultListEntry( *it ) );
                    }
                }
                mbCountFinal = sal_True;

                return true;
            }
            catch ( const libcmis::Exception& e )
            {
                SAL_INFO( "cmisucp", "Exception thrown: " << e.what() );
                return false;
            }
        }

        return false;
    }

    DataSupplier::~DataSupplier()
    {
    }

    ::rtl::OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
    {
        if ( nIndex < maResults.size() )
        {
            ::rtl::OUString aId = maResults[ nIndex ]->aId;
            if ( aId.getLength() )
            {
                // Already cached.
                return aId;
            }
        }

        if ( getResult( nIndex ) )
        {
            string sObjectPath;
            vector< string > paths = maResults[nIndex]->pObject->getPaths( );
            if ( paths.size( ) > 0 )
                sObjectPath = paths.front( );
            // TODO Handle the unfiled objects with their id... but can they manage to come here?

            // Get the URL from the Path
            URL aUrl( mxContent->getIdentifier( )->getContentIdentifier( ) );
            aUrl.setObjectPath( rtl::OUString::createFromAscii( sObjectPath.c_str( ) ) );
            rtl::OUString aId = aUrl.asString( );

            maResults[ nIndex ]->aId = aId;
            return aId;
        }

        return ::rtl::OUString();
    }

    uno::Reference< ucb::XContentIdentifier > DataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
    {
        if ( nIndex < maResults.size() )
        {
            uno::Reference< ucb::XContentIdentifier > xId = maResults[ nIndex ]->xId;
            if ( xId.is() )
            {
                // Already cached.
                return xId;
            }
        }

        ::rtl::OUString aId = queryContentIdentifierString( nIndex );
        if ( aId.getLength() )
        {
            uno::Reference< ucb::XContentIdentifier > xId = new ucbhelper::ContentIdentifier( aId );
            maResults[ nIndex ]->xId = xId;
            return xId;
        }

        return uno::Reference< ucb::XContentIdentifier >();
    }

    uno::Reference< ucb::XContent > DataSupplier::queryContent( sal_uInt32 nIndex )
    {
        if ( nIndex < maResults.size() )
        {
            uno::Reference< ucb::XContent > xContent = maResults[ nIndex ]->xContent;
            if ( xContent.is() )
            {
                // Already cached.
                return xContent;
            }
        }

        uno::Reference< ucb::XContentIdentifier > xId = queryContentIdentifier( nIndex );
        if ( xId.is() )
        {
            try
            {
                uno::Reference< ucb::XContent > xContent = mxContent->getProvider()->queryContent( xId );
                maResults[ nIndex ]->xContent = xContent;
                return xContent;
            }
            catch ( ucb::IllegalIdentifierException& )
            {
            }
        }
        return uno::Reference< ucb::XContent >();
    }

    sal_Bool DataSupplier::getResult( sal_uInt32 nIndex )
    {
        if ( maResults.size() > nIndex ) // Result already present.
            return sal_True;

        if ( getData() && maResults.size() > nIndex )
            return sal_True;

        return sal_False;
    }

    sal_uInt32 DataSupplier::totalCount()
    {
        getData();
        return maResults.size();
    }

    sal_uInt32 DataSupplier::currentCount()
    {
        return maResults.size();
    }

    sal_Bool DataSupplier::isCountFinal()
    {
        return mbCountFinal;
    }

    uno::Reference< sdbc::XRow > DataSupplier::queryPropertyValues( sal_uInt32 nIndex  )
    {
        if ( nIndex < maResults.size() )
        {
            uno::Reference< sdbc::XRow > xRow = maResults[ nIndex ]->xRow;
            if ( xRow.is() )
            {
                // Already cached.
                return xRow;
            }
        }

        if ( getResult( nIndex ) )
        {
            uno::Reference< ucb::XContent > xContent( queryContent( nIndex ) );
            if ( xContent.is() )
            {
                try
                {
                    uno::Reference< ucb::XCommandProcessor > xCmdProc(
                        xContent, uno::UNO_QUERY_THROW );
                    sal_Int32 nCmdId( xCmdProc->createCommandIdentifier() );
                    ucb::Command aCmd;
                    aCmd.Name = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("getPropertyValues"));
                    aCmd.Handle = -1;
                    aCmd.Argument <<= getResultSet()->getProperties();
                    uno::Any aResult( xCmdProc->execute(
                        aCmd, nCmdId, getResultSet()->getEnvironment() ) );
                    uno::Reference< sdbc::XRow > xRow;
                    if ( aResult >>= xRow )
                    {
                        maResults[ nIndex ]->xRow = xRow;
                        return xRow;
                    }
                }
                catch ( uno::Exception const & )
                {
                }
            }
        }
        return uno::Reference< sdbc::XRow >();
    }

    void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
    {
        if ( nIndex < maResults.size() )
            maResults[ nIndex ]->xRow = uno::Reference< sdbc::XRow >();
    }

    void DataSupplier::close()
    {
    }

    void DataSupplier::validate() throw( ucb::ResultSetException )
    {
    }

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */