1*cdf0e10cSrcweir /*
2*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*cdf0e10cSrcweir  *
4*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
7*cdf0e10cSrcweir  *
8*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
9*cdf0e10cSrcweir  *
10*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
11*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
12*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
13*cdf0e10cSrcweir  *
14*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
15*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
18*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
19*cdf0e10cSrcweir  *
20*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
21*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
22*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
23*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
24*cdf0e10cSrcweir  *
25*cdf0e10cSrcweir  */
26*cdf0e10cSrcweir 
27*cdf0e10cSrcweir package util.db;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
30*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
31*cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException;
32*cdf0e10cSrcweir import com.sun.star.frame.XModel;
33*cdf0e10cSrcweir import com.sun.star.frame.XStorable;
34*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35*cdf0e10cSrcweir import com.sun.star.sdb.XDocumentDataSource;
36*cdf0e10cSrcweir import com.sun.star.sdb.XOfficeDatabaseDocument;
37*cdf0e10cSrcweir import com.sun.star.sdbc.XDataSource;
38*cdf0e10cSrcweir import com.sun.star.uno.Exception;
39*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
40*cdf0e10cSrcweir import com.sun.star.uno.XNamingService;
41*cdf0e10cSrcweir import java.util.logging.Level;
42*cdf0e10cSrcweir import java.util.logging.Logger;
43*cdf0e10cSrcweir import lib.StatusException;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir /** wraps a com.sun.star.sdb.DataSource
46*cdf0e10cSrcweir  *
47*cdf0e10cSrcweir  * @author fs93730
48*cdf0e10cSrcweir  */
49*cdf0e10cSrcweir public class DataSource
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir     protected DataSource( XMultiServiceFactory _orb, DataSourceDescriptor _descriptor )
52*cdf0e10cSrcweir     {
53*cdf0e10cSrcweir         m_orb = _orb;
54*cdf0e10cSrcweir         try
55*cdf0e10cSrcweir         {
56*cdf0e10cSrcweir             m_dataSource = (XDataSource)UnoRuntime.queryInterface( XDataSource.class,
57*cdf0e10cSrcweir                 m_orb.createInstance( "com.sun.star.sdb.DataSource" ) );
58*cdf0e10cSrcweir             m_properties = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class,
59*cdf0e10cSrcweir                 m_dataSource );
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir             Object[] descriptorProperties = new Object[] {
62*cdf0e10cSrcweir                 _descriptor.Name, _descriptor.URL, _descriptor.Info, _descriptor.User, _descriptor.Password,
63*cdf0e10cSrcweir                 _descriptor.IsPasswordRequired };
64*cdf0e10cSrcweir             String[] propertyNames = new String[] {
65*cdf0e10cSrcweir                 "Name", "URL", "Info", "User", "Password", "IsPasswordRequired" };
66*cdf0e10cSrcweir             for ( int i=0; i < descriptorProperties.length; ++i )
67*cdf0e10cSrcweir                 if ( descriptorProperties[i] != null )
68*cdf0e10cSrcweir                     m_properties.setPropertyValue( propertyNames[i], descriptorProperties[i] );
69*cdf0e10cSrcweir         }
70*cdf0e10cSrcweir         catch ( Exception e )
71*cdf0e10cSrcweir         {
72*cdf0e10cSrcweir             throw new StatusException( "could not create/fill a css.sdb.DataSource object", e );
73*cdf0e10cSrcweir         }
74*cdf0e10cSrcweir     }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     public XDataSource getDataSource()
77*cdf0e10cSrcweir     {
78*cdf0e10cSrcweir         return m_dataSource;
79*cdf0e10cSrcweir     }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     /**
82*cdf0e10cSrcweir      * retrieves the css.sdb.OfficeDatabaseDocument associated with the data source
83*cdf0e10cSrcweir      * @return
84*cdf0e10cSrcweir      */
85*cdf0e10cSrcweir     public DatabaseDocument getDatabaseDocument()
86*cdf0e10cSrcweir     {
87*cdf0e10cSrcweir         synchronized ( this )
88*cdf0e10cSrcweir         {
89*cdf0e10cSrcweir             if ( m_document == null )
90*cdf0e10cSrcweir                 m_document = new DatabaseDocument( m_orb, this );
91*cdf0e10cSrcweir         }
92*cdf0e10cSrcweir         return m_document;
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     public void revokeRegistration()
96*cdf0e10cSrcweir     {
97*cdf0e10cSrcweir         String dataSourceName = "";
98*cdf0e10cSrcweir         try
99*cdf0e10cSrcweir         {
100*cdf0e10cSrcweir             dataSourceName = (String)m_properties.getPropertyValue( "Name" );
101*cdf0e10cSrcweir             XNamingService dbContext = (XNamingService)UnoRuntime.queryInterface( XNamingService.class,
102*cdf0e10cSrcweir             m_orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) );
103*cdf0e10cSrcweir             dbContext.revokeObject( dataSourceName );
104*cdf0e10cSrcweir         }
105*cdf0e10cSrcweir         catch ( Exception e )
106*cdf0e10cSrcweir         {
107*cdf0e10cSrcweir             throw new StatusException( "DataSource.revokeRegistration: could not revoke the object (" + dataSourceName + ")", e );
108*cdf0e10cSrcweir         }
109*cdf0e10cSrcweir     }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     public void registerAs( final String _registrationName, final boolean _revokeIfRegistered )
112*cdf0e10cSrcweir     {
113*cdf0e10cSrcweir         String doing = null;
114*cdf0e10cSrcweir         try
115*cdf0e10cSrcweir         {
116*cdf0e10cSrcweir             doing = "creating database context";
117*cdf0e10cSrcweir             XNamingService dbContext = UnoRuntime.queryInterface( XNamingService.class,
118*cdf0e10cSrcweir                 m_orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) );
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir             if ( _revokeIfRegistered )
121*cdf0e10cSrcweir             {
122*cdf0e10cSrcweir                 doing = "revoking previously registered data source";
123*cdf0e10cSrcweir                 try
124*cdf0e10cSrcweir                 {
125*cdf0e10cSrcweir                     dbContext.revokeObject( _registrationName );
126*cdf0e10cSrcweir                 }
127*cdf0e10cSrcweir                 catch( NoSuchElementException e )
128*cdf0e10cSrcweir                 {  /* allowed here */ }
129*cdf0e10cSrcweir             }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir             // if the document associated with the database document has not yet been saved, then we need to do so
132*cdf0e10cSrcweir             DatabaseDocument doc = getDatabaseDocument();
133*cdf0e10cSrcweir             String docURL = doc.getURL();
134*cdf0e10cSrcweir             if ( docURL.length() == 0 )
135*cdf0e10cSrcweir             {
136*cdf0e10cSrcweir                 final java.io.File tempFile = java.io.File.createTempFile( _registrationName + "_", ".odb" );
137*cdf0e10cSrcweir                 if ( tempFile.exists() )
138*cdf0e10cSrcweir                     // we did not really want to create that file, we just wanted its local name, but
139*cdf0e10cSrcweir                     // createTempFile actually creates it => throw it away
140*cdf0e10cSrcweir                     // (This is necessary since some JVM/platform combinations seem to actually lock the file)
141*cdf0e10cSrcweir                     tempFile.delete();
142*cdf0e10cSrcweir                 String localPart = tempFile.toURI().toURL().toString();
143*cdf0e10cSrcweir                 localPart = localPart.substring( localPart.lastIndexOf( '/' ) + 1 );
144*cdf0e10cSrcweir                 docURL = util.utils.getOfficeTemp( m_orb ) + localPart;
145*cdf0e10cSrcweir                 doing = "storing database document to temporary location (" + docURL + ")";
146*cdf0e10cSrcweir                 doc.storeAsURL( docURL );
147*cdf0e10cSrcweir             }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir             // register the data soource
150*cdf0e10cSrcweir             doing = "registering the data source at the database context";
151*cdf0e10cSrcweir             dbContext.registerObject( _registrationName, m_dataSource );
152*cdf0e10cSrcweir         }
153*cdf0e10cSrcweir         catch( final java.lang.Exception e )
154*cdf0e10cSrcweir         {
155*cdf0e10cSrcweir             throw new StatusException( "DataSource.registerAs: error during " + doing, e );
156*cdf0e10cSrcweir         }
157*cdf0e10cSrcweir     }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir     private XMultiServiceFactory    m_orb = null;
160*cdf0e10cSrcweir     private XDataSource             m_dataSource = null;
161*cdf0e10cSrcweir     private XPropertySet            m_properties = null;
162*cdf0e10cSrcweir     private DatabaseDocument        m_document = null;
163*cdf0e10cSrcweir }
164