merge the formfield patch from ooo-build
[ooovba.git] / configmgr / source / localbe / localdataimportsvc.cxx
bloba06472a589e73da991573c72716ae9a4f01196ba
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: localdataimportsvc.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
34 #include "localdataimportsvc.hxx"
35 #include "localsinglebackend.hxx"
37 #ifndef CONFIGMGR_API_FACTORY_HXX_
38 #include "confapifactory.hxx"
39 #endif
40 #include <rtl/ustrbuf.hxx>
41 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
42 #include <com/sun/star/configuration/backend/XLayerImporter.hpp>
44 // -----------------------------------------------------------------------------
45 #define OUSTRING( constascii ) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(constascii))
46 // -----------------------------------------------------------------------------
47 namespace configmgr
49 // -----------------------------------------------------------------------------
50 namespace localbe
52 // -----------------------------------------------------------------------------
53 namespace backend = ::com::sun::star::configuration::backend;
54 // -----------------------------------------------------------------------------
56 sal_Char const * const aLocalDataImportServices[] =
58 "com.sun.star.configuration.backend.LocalDataImporter",
60 "com.sun.star.configuration.backend.DataImporter",
63 const ServiceImplementationInfo aLocalDataImportSI =
65 "com.sun.star.comp.configuration.backend.LocalDataImporter",
66 aLocalDataImportServices,
67 aLocalDataImportServices + 3
69 // -----------------------------------------------------------------------------
71 const ServiceRegistrationInfo* getLocalDataImportServiceInfo()
72 { return getRegistrationInfo(& aLocalDataImportSI); }
73 // -----------------------------------------------------------------------------
75 inline
76 ServiceInfoHelper LocalDataImportService::getServiceInfo()
78 return & aLocalDataImportSI;
80 // -----------------------------------------------------------------------------
82 uno::Reference< uno::XInterface > SAL_CALL instantiateLocalDataImporter
83 ( uno::Reference< uno::XComponentContext > const& xContext )
85 return * new LocalDataImportService( xContext );
87 // -----------------------------------------------------------------------------
89 LocalDataImportService::LocalDataImportService(uno::Reference< uno::XComponentContext > const & _xContext)
90 : m_xServiceFactory(_xContext->getServiceManager(), uno::UNO_QUERY)
92 if (!m_xServiceFactory.is())
94 rtl::OUString sMessage = OUSTRING("Configuration Importer: Context has no service manager (or interface is missing)");
95 throw lang::NullPointerException(sMessage,NULL);
98 // -----------------------------------------------------------------------------
100 LocalDataImportService::~LocalDataImportService()
102 // -----------------------------------------------------------------------------
104 namespace
106 // -----------------------------------------------------------------------------
107 struct JobDesc
109 explicit JobDesc(task::XJob * pJob, const uno::Sequence< beans::NamedValue >& aArguments);
111 rtl::OUString aLayerDataUrl;
112 rtl::OUString aImporterService;
114 rtl::OUString aComponent;
115 rtl::OUString aEntity;
117 uno::Reference< backend::XLayer > xLayerFilter;
119 sal_Bool overwrite;
120 sal_Bool truncate;
122 sal_Bool use_component;
123 sal_Bool use_entity;
124 sal_Bool use_overwrite;
125 sal_Bool use_truncate;
127 // -----------------------------------------------------------------------------
129 JobDesc::JobDesc(task::XJob * pJob, const uno::Sequence< beans::NamedValue >& aArguments)
130 : aLayerDataUrl()
131 , aImporterService()
132 , aComponent()
133 , aEntity()
134 , xLayerFilter()
135 , overwrite(true)
136 , truncate(false)
137 , use_component(false)
138 , use_entity(false)
139 , use_overwrite(false)
140 , use_truncate(false)
142 sal_Int16 const nCount = static_cast<sal_Int16>(aArguments.getLength());
144 if (sal_Int32(nCount) != aArguments.getLength())
146 rtl::OUString sMessage = OUSTRING("Too many arguments for LocalDataImporter Job");
147 throw lang::IllegalArgumentException(sMessage,pJob,0);
150 for (sal_Int16 i=0; i < nCount; ++i)
152 sal_Bool bKnown = false;
153 sal_Bool bGood = false;
155 if (aArguments[i].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("LayerDataUrl")))
157 bKnown = true;
158 bGood = (aArguments[i].Value >>= aLayerDataUrl);
160 else if (aArguments[i].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("ImporterService")))
162 bKnown = true;
163 bGood = (aArguments[i].Value >>= aImporterService);
165 else if (aArguments[i].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Component")))
167 bKnown = true;
168 bGood = (aArguments[i].Value >>= aComponent);
169 use_component = bGood && (aComponent.getLength() != 0);
171 else if (aArguments[i].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Entity")))
173 bKnown = true;
174 bGood = (aArguments[i].Value >>= aEntity);
175 use_entity = bGood && (aEntity.getLength() != 0);
177 else if (aArguments[i].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("LayerFilter")))
179 bKnown = true;
180 bGood = (aArguments[i].Value >>= xLayerFilter);
181 if (xLayerFilter.is() && !uno::Reference<lang::XInitialization>::query(xLayerFilter).is())
182 bGood = false;
184 else if (aArguments[i].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("OverwriteExisting")))
186 bKnown = true;
187 bGood = (aArguments[i].Value >>= overwrite);
188 use_overwrite = bGood;
190 else if (aArguments[i].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("TruncateExisting")))
192 bKnown = true;
193 bGood = (aArguments[i].Value >>= truncate);
194 use_truncate = bGood;
197 if (!bGood)
199 rtl::OUStringBuffer sMsg;
200 sMsg.appendAscii("LocalDataImportService - Illegal argument: ");
201 if (bKnown)
202 sMsg.appendAscii("Wrong value type for argument '");
203 else
204 sMsg.appendAscii("Unknown argument '");
206 sMsg.append(aArguments[i].Name).appendAscii("'.");
208 throw lang::IllegalArgumentException(sMsg.makeStringAndClear(),pJob,i+1);
211 if (aLayerDataUrl.getLength() == 0)
213 rtl::OUStringBuffer sMsg;
214 sMsg.appendAscii("LocalDataImportService - Missing argument: ");
215 sMsg.appendAscii("No data URL available");
216 throw lang::IllegalArgumentException(sMsg.makeStringAndClear(),pJob,0);
218 if (aImporterService.getLength() == 0)
220 if ( (use_truncate && truncate) || (use_overwrite && !overwrite) )
221 aImporterService = OUSTRING("com.sun.star.configuration.backend.CopyImporter");
222 else
223 aImporterService = OUSTRING("com.sun.star.configuration.backend.MergeImporter");
226 // -----------------------------------------------------------------------------
228 static
229 inline
230 uno::Type getOverwriteFailedExceptionType()
232 lang::IllegalAccessException const * const selected = 0;
233 return ::getCppuType(selected);
235 // -----------------------------------------------------------------------------
237 // -----------------------------------------------------------------------------
238 // XJob
240 uno::Any SAL_CALL
241 LocalDataImportService::execute( const uno::Sequence< beans::NamedValue >& Arguments )
242 throw (lang::IllegalArgumentException, uno::Exception, uno::RuntimeException)
244 JobDesc const aJob(this,Arguments);
246 uno::Reference< lang::XMultiServiceFactory > aFactory = this->getServiceFactory();
248 uno::Reference< backend::XLayer > xLayer = aJob.use_component ?
249 LocalSingleBackend::createSimpleLayer(aFactory,aJob.aLayerDataUrl, aJob.aComponent ):
250 LocalSingleBackend::createSimpleLayer(aFactory,aJob.aLayerDataUrl) ;
252 if (!xLayer.is())
254 rtl::OUString sMessage = OUSTRING("LocalDataImportService - Cannot create layer to import from");
255 throw lang::NullPointerException(sMessage,*this);
258 uno::Reference< lang::XInitialization > xFilterInit(aJob.xLayerFilter,uno::UNO_QUERY);
259 if (xFilterInit.is())
261 beans::NamedValue argvalue(
262 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Source") ),
263 uno::makeAny( xLayer) );
265 uno::Sequence< uno::Any > args(1);
266 args[0] <<= argvalue;
268 xFilterInit->initialize(args);
270 xLayer = aJob.xLayerFilter;
273 uno::Reference< backend::XLayerImporter > xImporter;
275 int nArgCount = 0;
276 if (aJob.use_overwrite) ++nArgCount;
277 if (aJob.use_truncate) ++nArgCount;
279 if (nArgCount)
281 uno::Sequence< uno::Any > aArgs(nArgCount);
283 int n = 0;
284 if (aJob.use_overwrite)
285 aArgs[n++] <<= beans::NamedValue(OUSTRING("Overwrite"), uno::makeAny(aJob.overwrite));
287 if (aJob.use_truncate)
288 aArgs[n++] <<= beans::NamedValue(OUSTRING("Truncate"), uno::makeAny(aJob.truncate));
290 OSL_ASSERT(n == nArgCount);
292 xImporter.set( aFactory->createInstanceWithArguments(aJob.aImporterService,aArgs), uno::UNO_QUERY);
294 else
295 xImporter.set( aFactory->createInstance(aJob.aImporterService), uno::UNO_QUERY);
297 if (!xImporter.is())
299 rtl::OUString sMessage = OUSTRING("LocalDataImportService - Cannot create importer service: ") + aJob.aImporterService;
300 throw lang::NullPointerException(sMessage,*this);
305 if (aJob.use_entity)
306 xImporter->importLayerForEntity(xLayer,aJob.aEntity);
308 else
309 xImporter->importLayer(xLayer);
311 catch (lang::WrappedTargetException & e)
313 if (aJob.overwrite || !e.TargetException.isExtractableTo(getOverwriteFailedExceptionType())) throw;
314 return e.TargetException;
316 catch (lang::WrappedTargetRuntimeException & e)
318 if (aJob.overwrite || !e.TargetException.isExtractableTo(getOverwriteFailedExceptionType())) throw;
319 return e.TargetException;
322 return uno::Any();
324 // -----------------------------------------------------------------------------
326 // XServiceInfo
328 rtl::OUString SAL_CALL
329 LocalDataImportService::getImplementationName( )
330 throw (uno::RuntimeException)
332 return getServiceInfo().getImplementationName( );
334 // -----------------------------------------------------------------------------
337 sal_Bool SAL_CALL
338 LocalDataImportService::supportsService( const rtl::OUString& ServiceName )
339 throw (uno::RuntimeException)
341 return getServiceInfo().supportsService( ServiceName );
343 // -----------------------------------------------------------------------------
346 uno::Sequence< ::rtl::OUString > SAL_CALL
347 LocalDataImportService::getSupportedServiceNames( )
348 throw (uno::RuntimeException)
350 return getServiceInfo().getSupportedServiceNames( );
352 // -----------------------------------------------------------------------------
354 // -----------------------------------------------------------------------------
355 // -----------------------------------------------------------------------------
356 } // namespace
358 // -----------------------------------------------------------------------------
359 } // namespace