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 
24 package util.db;
25 
26 import com.sun.star.beans.PropertyValue;
27 import com.sun.star.beans.XPropertySet;
28 import com.sun.star.container.NoSuchElementException;
29 import com.sun.star.frame.XModel;
30 import com.sun.star.frame.XStorable;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.sdb.XDocumentDataSource;
33 import com.sun.star.sdb.XOfficeDatabaseDocument;
34 import com.sun.star.sdbc.XDataSource;
35 import com.sun.star.uno.Exception;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.uno.XNamingService;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
40 import lib.StatusException;
41 
42 /** wraps a com.sun.star.sdb.DataSource
43  *
44  * @author fs93730
45  */
46 public class DataSource
47 {
DataSource( XMultiServiceFactory _orb, DataSourceDescriptor _descriptor )48     protected DataSource( XMultiServiceFactory _orb, DataSourceDescriptor _descriptor )
49     {
50         m_orb = _orb;
51         try
52         {
53             m_dataSource = (XDataSource)UnoRuntime.queryInterface( XDataSource.class,
54                 m_orb.createInstance( "com.sun.star.sdb.DataSource" ) );
55             m_properties = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class,
56                 m_dataSource );
57 
58             Object[] descriptorProperties = new Object[] {
59                 _descriptor.Name, _descriptor.URL, _descriptor.Info, _descriptor.User, _descriptor.Password,
60                 _descriptor.IsPasswordRequired };
61             String[] propertyNames = new String[] {
62                 "Name", "URL", "Info", "User", "Password", "IsPasswordRequired" };
63             for ( int i=0; i < descriptorProperties.length; ++i )
64                 if ( descriptorProperties[i] != null )
65                     m_properties.setPropertyValue( propertyNames[i], descriptorProperties[i] );
66         }
67         catch ( Exception e )
68         {
69             throw new StatusException( "could not create/fill a css.sdb.DataSource object", e );
70         }
71     }
72 
getDataSource()73     public XDataSource getDataSource()
74     {
75         return m_dataSource;
76     }
77 
78     /**
79      * retrieves the css.sdb.OfficeDatabaseDocument associated with the data source
80      * @return
81      */
getDatabaseDocument()82     public DatabaseDocument getDatabaseDocument()
83     {
84         synchronized ( this )
85         {
86             if ( m_document == null )
87                 m_document = new DatabaseDocument( m_orb, this );
88         }
89         return m_document;
90     }
91 
revokeRegistration()92     public void revokeRegistration()
93     {
94         String dataSourceName = "";
95         try
96         {
97             dataSourceName = (String)m_properties.getPropertyValue( "Name" );
98             XNamingService dbContext = (XNamingService)UnoRuntime.queryInterface( XNamingService.class,
99             m_orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) );
100             dbContext.revokeObject( dataSourceName );
101         }
102         catch ( Exception e )
103         {
104             throw new StatusException( "DataSource.revokeRegistration: could not revoke the object (" + dataSourceName + ")", e );
105         }
106     }
107 
registerAs( final String _registrationName, final boolean _revokeIfRegistered )108     public void registerAs( final String _registrationName, final boolean _revokeIfRegistered )
109     {
110         String doing = null;
111         try
112         {
113             doing = "creating database context";
114             XNamingService dbContext = UnoRuntime.queryInterface( XNamingService.class,
115                 m_orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) );
116 
117             if ( _revokeIfRegistered )
118             {
119                 doing = "revoking previously registered data source";
120                 try
121                 {
122                     dbContext.revokeObject( _registrationName );
123                 }
124                 catch( NoSuchElementException e )
125                 {  /* allowed here */ }
126             }
127 
128             // if the document associated with the database document has not yet been saved, then we need to do so
129             DatabaseDocument doc = getDatabaseDocument();
130             String docURL = doc.getURL();
131             if ( docURL.length() == 0 )
132             {
133                 final java.io.File tempFile = java.io.File.createTempFile( _registrationName + "_", ".odb" );
134                 if ( tempFile.exists() )
135                     // we did not really want to create that file, we just wanted its local name, but
136                     // createTempFile actually creates it => throw it away
137                     // (This is necessary since some JVM/platform combinations seem to actually lock the file)
138                     tempFile.delete();
139                 String localPart = tempFile.toURI().toURL().toString();
140                 localPart = localPart.substring( localPart.lastIndexOf( '/' ) + 1 );
141                 docURL = util.utils.getOfficeTemp( m_orb ) + localPart;
142                 doing = "storing database document to temporary location (" + docURL + ")";
143                 doc.storeAsURL( docURL );
144             }
145 
146             // register the data soource
147             doing = "registering the data source at the database context";
148             dbContext.registerObject( _registrationName, m_dataSource );
149         }
150         catch( final java.lang.Exception e )
151         {
152             throw new StatusException( "DataSource.registerAs: error during " + doing, e );
153         }
154     }
155 
156     private XMultiServiceFactory    m_orb = null;
157     private XDataSource             m_dataSource = null;
158     private XPropertySet            m_properties = null;
159     private DatabaseDocument        m_document = null;
160 }
161