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