View Javadoc

1   package com.explosion.datastream.exql.processes;
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  import java.sql.Connection;
23  import java.sql.ResultSet;
24  import java.sql.SQLException;
25  
26  import org.apache.log4j.LogManager;
27  import org.apache.log4j.Logger;
28  
29  import com.explosion.datastream.exql.gui.EXQLBaseTool;
30  import com.explosion.datastream.exql.gui.ExqlTreeNode;
31  import com.explosion.expfmodules.rdbmsconn.dbom.DBEntity;
32  import com.explosion.expfmodules.rdbmsconn.dbom.utils.MetadataUtils;
33  import com.explosion.utilities.process.StackableSimpleProcess;
34  import com.explosion.utilities.process.threads.Finishable;
35  
36  /***
37   * @author Stephen Cowx
38   * Date created:@14-Feb-2003
39   */
40  public class RefreshCatalogsAndSchemasProcess extends StackableSimpleProcess
41  {
42    private static Logger log = LogManager.getLogger(RefreshCatalogsAndSchemasProcess.class);
43    private Connection conn;
44    private EXQLBaseTool tool;    
45    private ExqlTreeNode rootNode;
46    
47    /***
48     * Constructor for GetDBCatalogs.
49     * @param finishable
50     */
51    public RefreshCatalogsAndSchemasProcess(Finishable finishable,  StackableSimpleProcess parentProcess ,Connection conn, ExqlTreeNode rootNode, EXQLBaseTool tool)
52    {
53      super(finishable,parentProcess);
54      this.conn = conn;
55      this.tool = tool;
56      this.rootNode = rootNode;
57      this.setIsUserProcess(true);
58    }
59    
60    public static void refresh(Connection conn, StackableSimpleProcess parentProcess, EXQLBaseTool tool, ExqlTreeNode rootNode) throws Exception
61    {
62      RefreshCatalogsAndSchemasProcess action = new RefreshCatalogsAndSchemasProcess(null,parentProcess,conn, rootNode,tool);
63      rootNode.removeAllChildren();
64      action.process();
65    }
66    
67    /***
68     * @see com.explosion.utilities.process.threads.SimpleProcess#process()
69     */
70    public void process() throws Exception
71    {
72      if (isStopped()) return;
73      
74      String schemaTerm = MetadataUtils.getDBMD(conn).getSchemaTerm();
75      String catalogTerm = MetadataUtils.getDBMD(conn).getCatalogTerm();
76      
77      ResultSet set = null;
78      ResultSet set2 = null;
79      try
80      {
81        log.debug("About to fetch " + catalogTerm + "s.");
82        set = MetadataUtils.getDBMD(conn).getCatalogs();
83        if (isStopped()) return;
84        addCatalogNodes(set, catalogTerm);
85        log.debug("Finished fetching " + catalogTerm + "s.");
86        log.debug("About to fetch " + schemaTerm + "s.");
87        set2 = MetadataUtils.getDBMD(conn).getSchemas();
88        if (isStopped()) return;
89        addSchemaNodes(set2, schemaTerm);
90        
91      }
92      catch (SQLException e)
93      {
94        log.debug("Resultset MetaData getCatalogs() has not been implemented.");
95      }
96      finally
97      {
98        try { if (set != null) set.close();  } catch (Exception e)  { }
99        try { if (set2 != null) set2.close();  } catch (Exception e)  { }
100     }
101   }
102 
103   private void addSchemaNodes(ResultSet set, String schemaTerm) throws SQLException
104   {
105       while (set.next())
106       {
107         if (isStopped()) 
108           return;
109         
110         String name = set.getString(1);
111         log.debug("Adding " + schemaTerm + ": " + name);
112         setStatusText("Adding " + schemaTerm + ": "+name );
113 
114         DBEntity schemaDescriptor = new DBEntity(null,name, name, DBEntity.TYPE_SCHEMA);
115         ExqlTreeNode schemaNode = new ExqlTreeNode(schemaDescriptor);
116         rootNode.add(schemaNode);
117       }  
118   }
119   
120   public void addCatalogNodes(ResultSet set, String catalogTerm) throws SQLException
121   {
122     while (set.next())
123     {
124       if (isStopped())
125         return;
126       
127       String name = set.getString(1);
128       log.debug("Adding " + catalogTerm + ": " + name);
129       
130       DBEntity catalogDescriptor = new DBEntity(name, null, name, DBEntity.TYPE_CATALOG);
131 	setStatusText("Adding " + catalogTerm + ": "+name );
132 	ExqlTreeNode catalogNode = new ExqlTreeNode(catalogDescriptor);
133 	
134 	rootNode.add(catalogNode);
135     }  
136   }
137 
138 
139 }
140