Wrong partition length raises exception
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / URLSetupControlFactory.java
1 package com.quantum.wizards;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6
7 import com.quantum.adapters.AdapterFactory;
8 import com.quantum.model.JDBCDriver;
9
10 import org.eclipse.swt.widgets.Composite;
11
12
13 /**
14  * @author BC Holmes
15  */
16 public class URLSetupControlFactory {
17         
18         public static boolean hasControl(JDBCDriver driver) {
19                 return null != getControlClass(driver);
20         }
21
22         public static URLSetupControl create(JDBCDriver driver, Composite composite) {
23                 Class controlClass = getControlClass(driver);
24                 
25                 if (BasicThreePartURLSetupControl.class == controlClass) {
26                         URLSetupControl control =  new BasicThreePartURLSetupControl(composite, driver);
27                         control.createPart();
28                         return control;
29                 } else if (BasicOnePartURLSetupControl.class == controlClass) {
30                         String[] properties = getProperties(driver);
31                         URLSetupControl control = new BasicOnePartURLSetupControl(composite, driver, properties[0]);
32                         control.createPart();
33                         return control;
34                 } else if (InformixURLSetupControl.class == controlClass) {
35                         URLSetupControl control = new InformixURLSetupControl(composite, driver);
36                         control.createPart();
37                         return control;
38                 } else {
39                         return null;
40                 }
41         }
42         
43         private static Class getControlClass(JDBCDriver driver) {
44                 String[] parameters = getProperties(driver);
45                 List parametersList = new ArrayList();
46                 if (parameters != null) {
47                         parametersList.addAll(Arrays.asList(parameters));
48                 }
49                 
50                 if (parametersList.size() == 3 
51                                 && parametersList.contains("dbname")
52                                 && parametersList.contains("hostname")
53                                 && parametersList.contains("port")) {
54                         return BasicThreePartURLSetupControl.class;
55                 } else if (parametersList.size() == 4 
56                                         && parametersList.contains("dbname")
57                                         && parametersList.contains("hostname")
58                                         && parametersList.contains("informixserver")
59                                         && parametersList.contains("port")) {
60                                 return InformixURLSetupControl.class;
61                 } else if (parametersList.size() == 1 
62                                         && (parametersList.contains("dbname")
63                                                         || parametersList.contains("datasource"))) {
64                                 return BasicOnePartURLSetupControl.class;
65                 } else {
66                         return null;
67                 }
68         }
69
70         /**
71          * @param driver
72          * @return
73          */
74         private static String[] getProperties(JDBCDriver driver) {
75                 String driverClassName = driver.getClassName();
76                 String urlPattern = AdapterFactory.getInstance().getURLPattern(driverClassName);
77                 String[] parameters = URLBuilder.getVariableNames(urlPattern);
78                 return parameters;
79         }
80 }