View Javadoc

1   package com.explosion.datastream.exql.processes;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.sql.Connection;
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.DataSet;
33  import com.explosion.expfmodules.rdbmsconn.dbom.sql.SingleTableSelectStatement;
34  import com.explosion.utilities.exception.ExceptionManagerFactory;
35  import com.explosion.utilities.process.StackableSimpleProcess;
36  import com.explosion.utilities.process.threads.Finishable;
37  import com.explosion.utilities.process.threads.ProcessThread;
38  
39  /***
40   * This process performs a full refresh of the table.  This includes the metadata and the 
41   * data
42   * @author Stephen Cowx Date created:@14-Feb-2003 This class executes a series
43   *         of delete statements
44   */
45  public class RefreshTableProcess extends StackableSimpleProcess
46  {
47  
48      private static Logger log = LogManager.getLogger(RefreshTableProcess.class);
49      private DBEntity entity;
50      private ExqlTreeNode node;
51      private int connectionIdentifier = -1;
52      private Connection conn;
53      private EXQLBaseTool tool;
54      private QueryProcess queryProcess;
55  
56      /***
57       * Constructor for DeleteProcess.
58       */
59      public RefreshTableProcess(StackableSimpleProcess parentProcess, DBEntity entity, ExqlTreeNode node, Connection conn, EXQLBaseTool tool)
60      {
61          super(parentProcess);
62          this.entity = entity;
63          this.node = node;
64          this.conn = conn;
65          this.tool = tool;
66          this.setIsUserProcess(true);
67      }
68  
69      /***
70       * Constructor for DeleteProcess.
71       */
72      public RefreshTableProcess(Finishable finishable, DBEntity entity, ExqlTreeNode node, Connection conn, EXQLBaseTool tool)
73      {
74          super(finishable, null);
75          this.entity = entity;
76          this.node = node;
77          this.conn = conn;
78          this.tool = tool;
79          this.setIsUserProcess(true);
80      }
81      
82      /***
83       * Returns the set.
84       * @return DataSet
85       */
86      public DataSet getSet()
87      {
88        if (queryProcess != null)
89        {
90          return queryProcess.getSet();
91        }
92        else
93        {
94            return null;
95        }
96      }
97   
98      /***
99       * This method loops around the bindVariable sets binding the variables to 
100      * a prepared statement and executing them. 
101      * @see com.explosion.utilities.process.threads.SimpleProcess#process()
102      */
103     public void process()
104     {
105         try
106         {
107             if (isStopped())
108                 return;
109             
110             setStatusText("Fetching table metadata");
111             RefreshTableInformationProcess process = new RefreshTableInformationProcess(this, null, conn, node, entity, tool);
112             process.process();
113             
114             setPercentComplete(20);
115             
116             setStatusText("Fetching table data");
117             fetchData(entity);
118             setStatusText(queryProcess.getStatusText());
119 
120             this.setPercentComplete(100);
121         }
122         catch (Exception e)
123         {
124             setStatusText("Error, unable to fetch data.");
125             setPercentComplete(100);
126             if (getProcessControl().getStatus() != ProcessThread.THREAD_STOPPED)
127             {
128                 //ie, it hasn't been cancelled
129                 if (this.getProcessControl().getProcess() != null)
130                 {
131                     ((StackableSimpleProcess) this.getProcessControl().getProcess()).manageException(e,"Exception caught while fetching data");
132                     try {
133 						this.getProcessControl().stop();
134 					} catch (InterruptedException e1) {
135 //						ignore
136 						log.error("Interrupted", e);
137 					}
138                 }
139                 else
140                 {
141                   ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while fetching data.");
142                 }
143             }
144         }
145 
146     }
147     
148     /***
149      * fetches the data from the table
150      * @throws SQLException
151      */
152     private void fetchData(DBEntity dbed) throws SQLException
153     {
154       String command = "";
155               
156   	  SingleTableSelectStatement clause = new SingleTableSelectStatement(dbed);
157   	  command = clause.getSelectString(conn.getMetaData());
158   	      	        
159       if (command != null && command.length() > 0)
160       {
161         dbed.getDBEntityBuffer().setFetchSQL(command);
162         /* Execute the query */
163         queryProcess = new QueryProcess(this, null,conn,command,false,tool);
164         queryProcess.process();
165       }
166     }
167 }