View Javadoc

1   package com.explosion.expfmodules.rdbmsconn.connectwizard.actions;
2   
3   /* =============================================================================
4    *       
5    *     Copyright 2004 Stephen Cowx
6    *
7    *     Licensed under the Apache License, Version 2.0 (the "License");
8    *     you may not use this file except in compliance with the License.
9    *     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, software
14   *     distributed under the License is distributed on an "AS IS" BASIS,
15   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *     See the License for the specific language governing permissions and
17   *     limitations under the License.
18   * 
19   * =============================================================================
20   */
21  
22  /***
23   * @author Stephen Cowx
24   * Created on 21-Feb-2005
25   */
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import com.explosion.expfmodules.rdbmsconn.ConnectionDescriptorManager;
30  import com.explosion.expfmodules.rdbmsconn.RdbmsConnConstants;
31  import com.explosion.expfmodules.rdbmsconn.RdbmsConnModuleManager;
32  import com.explosion.expfmodules.wizard.StepAction;
33  import com.explosion.expfmodules.wizard.Wizard;
34  import com.explosion.expfmodules.wizard.standard.PreferenceDataItem;
35  import com.explosion.utilities.exception.ExceptionManagerFactory;
36  import com.explosion.utilities.preferences.groups.PreferenceGroup;
37  
38  public class JTDSConnectionAction implements StepAction {
39  
40  	/***
41  	 * Creates a MySQL database connection based on the information 
42  	 * collected in the wizard.
43  	 */
44  	public boolean execute(Wizard wizard) {
45  		
46  		try {
47  		    
48  		    /* Get the values for the connection from the wizard values */
49  		    String server = (String) ((PreferenceDataItem) wizard.getWizardDataValues().get("server")).getPreference().getValue();
50  			Integer port = (Integer) ((PreferenceDataItem) wizard.getWizardDataValues().get("port")).getPreference().getValue();
51  			String database = (String) ((PreferenceDataItem) wizard.getWizardDataValues().get("database")).getPreference().getValue();
52  			String user = (String) ((PreferenceDataItem) wizard.getWizardDataValues().get("username")).getPreference().getValue();
53  			String pass = (String) ((PreferenceDataItem) wizard.getWizardDataValues().get("password")).getPreference().getValue();
54  			String servertype = (String)  ((PreferenceDataItem) wizard.getWizardDataValues().get("servertype")).getPreference().getValue();
55  		    String connectmethod =  (String)  ((PreferenceDataItem) wizard.getWizardDataValues().get("connectmethod")).getPreference().getValue();
56  		    
57  			/* Use a manager to create a ConnectionDescriptor preference group */
58  			Map store = new HashMap();
59  			ConnectionDescriptorManager manager = new ConnectionDescriptorManager(RdbmsConnModuleManager.instance().getDriverDescriptorManager(), store);
60  			PreferenceGroup group = manager.createGroup("New connection");
61  			group.setPreferenceValue(RdbmsConnConstants.CD_DRIVER,"jTDS 1.0.3");
62  			group.setPreferenceValue(RdbmsConnConstants.CD_USER,user);
63  			group.setPreferenceValue(RdbmsConnConstants.CD_PASS,pass);
64  			group.setPreferenceValue(RdbmsConnConstants.DRVR_DB_SUPPORTS_NAMESPACES,new Boolean(false));
65  
66  			String parameters = new String();
67  			if (connectmethod.equals("Named Pipes"))
68  			{
69  			    parameters = ";namedPipe=true";
70  			}
71  			
72  			//jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]
73  			group.setPreferenceValue(RdbmsConnConstants.CD_URL,"jdbc:jtds:"+servertype+"://"+server+":"+port+"/"+database+parameters);
74  			
75  			wizard.getWizardDataValues().put("connection_descriptor", group);
76  			
77  			return true;
78  			
79  		} catch (Exception e) {
80  			ExceptionManagerFactory.getExceptionManager().manageException(e, "Caught exception while creating new instance of connection.");
81  		}
82  		return false;
83  	}
84  }