1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 
6 package com.sun.star.wizards.db;
7 
8 import com.sun.star.beans.PropertyValue;
9 import com.sun.star.container.NoSuchElementException;
10 import com.sun.star.frame.XController;
11 import com.sun.star.frame.XFrame;
12 import com.sun.star.lang.IllegalArgumentException;
13 import com.sun.star.lang.XMultiServiceFactory;
14 import com.sun.star.sdb.application.XDatabaseDocumentUI;
15 import com.sun.star.sdbc.SQLException;
16 import com.sun.star.uno.UnoRuntime;
17 import com.sun.star.wizards.common.Desktop;
18 import com.sun.star.wizards.common.NamedValueCollection;
19 import com.sun.star.wizards.common.Properties;
20 import com.sun.star.wizards.ui.WizardDialog;
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.Method;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 
26 /**
27  * is a base class for a wizard creating a database object
28  * @author frank.schoenheit@sun.com
29  */
30 public abstract class DatabaseObjectWizard extends WizardDialog
31 {
32     protected final PropertyValue[]     m_wizardContext;
33     protected final XDatabaseDocumentUI m_docUI;
34     protected final XFrame              m_frame;
35 
36     protected DatabaseObjectWizard( final XMultiServiceFactory i_orb, final int i_helpIDBase, final PropertyValue[] i_wizardContext )
37     {
38         super( i_orb, i_helpIDBase );
39         m_wizardContext = i_wizardContext;
40 
41         final NamedValueCollection wizardContext = new NamedValueCollection( m_wizardContext );
42         m_docUI = wizardContext.queryOrDefault( "DocumentUI", (XDatabaseDocumentUI)null, XDatabaseDocumentUI.class );
43 
44         if ( m_docUI != null )
45         {
46             XController docController = UnoRuntime.queryInterface( XController.class, m_docUI );
47             m_frame = docController.getFrame();
48         }
49         else
50         {
51             XFrame parentFrame = wizardContext.queryOrDefault( "ParentFrame", (XFrame)null, XFrame.class );
52             if ( parentFrame != null )
53                 m_frame = parentFrame;
54             else
55                 m_frame = Desktop.getActiveFrame( xMSF );
56         }
57     }
58 
59     protected final void loadSubComponent( final int i_type, final String i_name, final boolean i_forEditing )
60     {
61         try
62         {
63             if ( m_docUI != null )
64                 m_docUI.loadComponent( i_type, i_name, i_forEditing );
65         }
66         catch ( IllegalArgumentException ex )
67         {
68             Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, null, ex );
69         }
70         catch ( NoSuchElementException ex )
71         {
72             Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, null, ex );
73         }
74         catch ( SQLException ex )
75         {
76             Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, null, ex );
77         }
78     }
79 
80     protected static void executeWizardFromCommandLine( final String i_args[], final String i_className )
81     {
82         final String settings[] = new String[] { null, null, null };
83         final int IDX_PIPE_NAME = 0;
84         final int IDX_LOCATION = 1;
85         final int IDX_DSN = 2;
86 
87         // some simple parsing
88         boolean failure = false;
89         int settingsIndex = -1;
90         for ( int i=0; i<i_args.length; ++i )
91         {
92             if ( settingsIndex >= 0 )
93             {
94                 settings[ settingsIndex ] = i_args[i];
95                 settingsIndex = -1;
96                 continue;
97             }
98 
99             if ( i_args[i].equals( "--pipe-name" ) )
100             {
101                 settingsIndex = IDX_PIPE_NAME;
102                 continue;
103             }
104 
105             if ( i_args[i].equals( "--database-location" ) )
106             {
107                 settingsIndex = IDX_LOCATION;
108                 continue;
109             }
110 
111             if ( i_args[i].equals( "--data-source-name" ) )
112             {
113                 settingsIndex = IDX_DSN;
114                 continue;
115             }
116 
117             failure = true;
118         }
119 
120         if ( settings[ IDX_PIPE_NAME ] == null )
121             failure = true;
122 
123         if ( ( settings[ IDX_DSN ] == null ) && ( settings[ IDX_LOCATION ] == null ) )
124             failure = true;
125 
126         if ( failure )
127         {
128             System.err.println( "supported arguments: " );
129             System.err.println( "  --pipe-name <name>           : specifies the name of the pipe to connect to the running OOo instance" );
130             System.err.println( "  --database-location <url>    : specifies the URL of the database document to work with" );
131             System.err.println( "  --data-source-name <name>    : specifies the name of the data source to work with" );
132             return;
133         }
134 
135         final String ConnectStr = "uno:pipe,name=" + settings[IDX_PIPE_NAME] + ";urp;StarOffice.ServiceManager";
136         try
137         {
138             final XMultiServiceFactory serviceFactory = Desktop.connect(ConnectStr);
139             if (serviceFactory != null)
140             {
141                 PropertyValue[] curproperties = new PropertyValue[1];
142                 if ( settings[ IDX_LOCATION ] != null )
143                     curproperties[0] = Properties.createProperty( "DatabaseLocation", settings[ IDX_LOCATION ] );
144                 else
145                     curproperties[0] = Properties.createProperty( "DataSourceName", settings[ IDX_DSN ] );
146 
147                 final Class wizardClass = Class.forName( i_className );
148                 final Constructor ctor = wizardClass.getConstructor( XMultiServiceFactory.class, PropertyValue[].class );
149                 final Method invokeMethod = wizardClass.getMethod( "start", new Class[0] );
150                 final Object wizardInstance = ctor.newInstance( serviceFactory, curproperties );
151                 invokeMethod.invoke( wizardInstance );
152             }
153         }
154         catch (java.lang.Exception jexception)
155         {
156             jexception.printStackTrace(System.out);
157         }
158     }
159 }
160