1*a1b4a26bSAndrew Rist /**************************************************************
2*a1b4a26bSAndrew Rist  *
3*a1b4a26bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a1b4a26bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a1b4a26bSAndrew Rist  * distributed with this work for additional information
6*a1b4a26bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a1b4a26bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a1b4a26bSAndrew Rist  * "License"); you may not use this file except in compliance
9*a1b4a26bSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a1b4a26bSAndrew Rist  *
11*a1b4a26bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a1b4a26bSAndrew Rist  *
13*a1b4a26bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a1b4a26bSAndrew Rist  * software distributed under the License is distributed on an
15*a1b4a26bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a1b4a26bSAndrew Rist  * KIND, either express or implied.  See the License for the
17*a1b4a26bSAndrew Rist  * specific language governing permissions and limitations
18*a1b4a26bSAndrew Rist  * under the License.
19*a1b4a26bSAndrew Rist  *
20*a1b4a26bSAndrew Rist  *************************************************************/
21*a1b4a26bSAndrew Rist 
22cdf0e10cSrcweir /*
23cdf0e10cSrcweir  * To change this template, choose Tools | Templates
24cdf0e10cSrcweir  * and open the template in the editor.
25cdf0e10cSrcweir  */
26cdf0e10cSrcweir 
27cdf0e10cSrcweir package com.sun.star.wizards.db;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
30cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException;
31cdf0e10cSrcweir import com.sun.star.frame.XController;
32cdf0e10cSrcweir import com.sun.star.frame.XFrame;
33cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException;
34cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35cdf0e10cSrcweir import com.sun.star.sdb.application.XDatabaseDocumentUI;
36cdf0e10cSrcweir import com.sun.star.sdbc.SQLException;
37cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
38cdf0e10cSrcweir import com.sun.star.wizards.common.Desktop;
39cdf0e10cSrcweir import com.sun.star.wizards.common.NamedValueCollection;
40cdf0e10cSrcweir import com.sun.star.wizards.common.Properties;
41cdf0e10cSrcweir import com.sun.star.wizards.ui.WizardDialog;
42cdf0e10cSrcweir import java.lang.reflect.Constructor;
43cdf0e10cSrcweir import java.lang.reflect.Method;
44cdf0e10cSrcweir import java.util.logging.Level;
45cdf0e10cSrcweir import java.util.logging.Logger;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir /**
48cdf0e10cSrcweir  * is a base class for a wizard creating a database object
49cdf0e10cSrcweir  * @author frank.schoenheit@sun.com
50cdf0e10cSrcweir  */
51cdf0e10cSrcweir public abstract class DatabaseObjectWizard extends WizardDialog
52cdf0e10cSrcweir {
53cdf0e10cSrcweir     protected final PropertyValue[]     m_wizardContext;
54cdf0e10cSrcweir     protected final XDatabaseDocumentUI m_docUI;
55cdf0e10cSrcweir     protected final XFrame              m_frame;
56cdf0e10cSrcweir 
DatabaseObjectWizard( final XMultiServiceFactory i_orb, final int i_helpIDBase, final PropertyValue[] i_wizardContext )57cdf0e10cSrcweir     protected DatabaseObjectWizard( final XMultiServiceFactory i_orb, final int i_helpIDBase, final PropertyValue[] i_wizardContext )
58cdf0e10cSrcweir     {
59cdf0e10cSrcweir         super( i_orb, i_helpIDBase );
60cdf0e10cSrcweir         m_wizardContext = i_wizardContext;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir         final NamedValueCollection wizardContext = new NamedValueCollection( m_wizardContext );
63cdf0e10cSrcweir         m_docUI = wizardContext.queryOrDefault( "DocumentUI", (XDatabaseDocumentUI)null, XDatabaseDocumentUI.class );
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         if ( m_docUI != null )
66cdf0e10cSrcweir         {
67cdf0e10cSrcweir             XController docController = UnoRuntime.queryInterface( XController.class, m_docUI );
68cdf0e10cSrcweir             m_frame = docController.getFrame();
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir         else
71cdf0e10cSrcweir         {
72cdf0e10cSrcweir             XFrame parentFrame = wizardContext.queryOrDefault( "ParentFrame", (XFrame)null, XFrame.class );
73cdf0e10cSrcweir             if ( parentFrame != null )
74cdf0e10cSrcweir                 m_frame = parentFrame;
75cdf0e10cSrcweir             else
76cdf0e10cSrcweir                 m_frame = Desktop.getActiveFrame( xMSF );
77cdf0e10cSrcweir         }
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
loadSubComponent( final int i_type, final String i_name, final boolean i_forEditing )80cdf0e10cSrcweir     protected final void loadSubComponent( final int i_type, final String i_name, final boolean i_forEditing )
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         try
83cdf0e10cSrcweir         {
84cdf0e10cSrcweir             if ( m_docUI != null )
85cdf0e10cSrcweir                 m_docUI.loadComponent( i_type, i_name, i_forEditing );
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir         catch ( IllegalArgumentException ex )
88cdf0e10cSrcweir         {
89cdf0e10cSrcweir             Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, null, ex );
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir         catch ( NoSuchElementException ex )
92cdf0e10cSrcweir         {
93cdf0e10cSrcweir             Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, null, ex );
94cdf0e10cSrcweir         }
95cdf0e10cSrcweir         catch ( SQLException ex )
96cdf0e10cSrcweir         {
97cdf0e10cSrcweir             Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, null, ex );
98cdf0e10cSrcweir         }
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir 
executeWizardFromCommandLine( final String i_args[], final String i_className )101cdf0e10cSrcweir     protected static void executeWizardFromCommandLine( final String i_args[], final String i_className )
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         final String settings[] = new String[] { null, null, null };
104cdf0e10cSrcweir         final int IDX_PIPE_NAME = 0;
105cdf0e10cSrcweir         final int IDX_LOCATION = 1;
106cdf0e10cSrcweir         final int IDX_DSN = 2;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         // some simple parsing
109cdf0e10cSrcweir         boolean failure = false;
110cdf0e10cSrcweir         int settingsIndex = -1;
111cdf0e10cSrcweir         for ( int i=0; i<i_args.length; ++i )
112cdf0e10cSrcweir         {
113cdf0e10cSrcweir             if ( settingsIndex >= 0 )
114cdf0e10cSrcweir             {
115cdf0e10cSrcweir                 settings[ settingsIndex ] = i_args[i];
116cdf0e10cSrcweir                 settingsIndex = -1;
117cdf0e10cSrcweir                 continue;
118cdf0e10cSrcweir             }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir             if ( i_args[i].equals( "--pipe-name" ) )
121cdf0e10cSrcweir             {
122cdf0e10cSrcweir                 settingsIndex = IDX_PIPE_NAME;
123cdf0e10cSrcweir                 continue;
124cdf0e10cSrcweir             }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir             if ( i_args[i].equals( "--database-location" ) )
127cdf0e10cSrcweir             {
128cdf0e10cSrcweir                 settingsIndex = IDX_LOCATION;
129cdf0e10cSrcweir                 continue;
130cdf0e10cSrcweir             }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir             if ( i_args[i].equals( "--data-source-name" ) )
133cdf0e10cSrcweir             {
134cdf0e10cSrcweir                 settingsIndex = IDX_DSN;
135cdf0e10cSrcweir                 continue;
136cdf0e10cSrcweir             }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir             failure = true;
139cdf0e10cSrcweir         }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         if ( settings[ IDX_PIPE_NAME ] == null )
142cdf0e10cSrcweir             failure = true;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         if ( ( settings[ IDX_DSN ] == null ) && ( settings[ IDX_LOCATION ] == null ) )
145cdf0e10cSrcweir             failure = true;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         if ( failure )
148cdf0e10cSrcweir         {
149cdf0e10cSrcweir             System.err.println( "supported arguments: " );
150cdf0e10cSrcweir             System.err.println( "  --pipe-name <name>           : specifies the name of the pipe to connect to the running OOo instance" );
151cdf0e10cSrcweir             System.err.println( "  --database-location <url>    : specifies the URL of the database document to work with" );
152cdf0e10cSrcweir             System.err.println( "  --data-source-name <name>    : specifies the name of the data source to work with" );
153cdf0e10cSrcweir             return;
154cdf0e10cSrcweir         }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         final String ConnectStr = "uno:pipe,name=" + settings[IDX_PIPE_NAME] + ";urp;StarOffice.ServiceManager";
157cdf0e10cSrcweir         try
158cdf0e10cSrcweir         {
159cdf0e10cSrcweir             final XMultiServiceFactory serviceFactory = Desktop.connect(ConnectStr);
160cdf0e10cSrcweir             if (serviceFactory != null)
161cdf0e10cSrcweir             {
162cdf0e10cSrcweir                 PropertyValue[] curproperties = new PropertyValue[1];
163cdf0e10cSrcweir                 if ( settings[ IDX_LOCATION ] != null )
164cdf0e10cSrcweir                     curproperties[0] = Properties.createProperty( "DatabaseLocation", settings[ IDX_LOCATION ] );
165cdf0e10cSrcweir                 else
166cdf0e10cSrcweir                     curproperties[0] = Properties.createProperty( "DataSourceName", settings[ IDX_DSN ] );
167cdf0e10cSrcweir 
168cdf0e10cSrcweir                 final Class wizardClass = Class.forName( i_className );
169cdf0e10cSrcweir                 final Constructor ctor = wizardClass.getConstructor( XMultiServiceFactory.class, PropertyValue[].class );
170cdf0e10cSrcweir                 final Method invokeMethod = wizardClass.getMethod( "start", new Class[0] );
171cdf0e10cSrcweir                 final Object wizardInstance = ctor.newInstance( serviceFactory, curproperties );
172cdf0e10cSrcweir                 invokeMethod.invoke( wizardInstance );
173cdf0e10cSrcweir             }
174cdf0e10cSrcweir         }
175cdf0e10cSrcweir         catch (java.lang.Exception jexception)
176cdf0e10cSrcweir         {
177cdf0e10cSrcweir             jexception.printStackTrace(System.out);
178cdf0e10cSrcweir         }
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir }
181