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.PreparedStatement;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.log4j.LogManager;
30  import org.apache.log4j.Logger;
31  
32  import com.explosion.expfmodules.rdbmsconn.dbom.DBEntity;
33  import com.explosion.expfmodules.rdbmsconn.dbom.sql.DeleteStatement;
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   * @author Stephen Cowx Date created:@14-Feb-2003 This class executes a series
41   *         of delete statements
42   */
43  public class DeleteProcess extends StackableSimpleProcess
44  {
45      private static Logger log = LogManager.getLogger(DeleteProcess.class);
46      private DBEntity entity;
47      private List bindVariableSets;
48      private int connectionIdentifier = -1;
49      private Connection conn;
50  
51      /***
52       * Constructor for DeleteProcess.
53       */
54      public DeleteProcess(Finishable finishable, DBEntity entity, List bindVariableSets, Connection conn)
55      {
56          super(finishable, null);
57          this.entity = entity;
58          this.bindVariableSets = bindVariableSets;
59          this.conn = conn;
60          this.setIsUserProcess(true);
61      }
62      
63      /***
64       * Constructor for DeleteProcess.
65       */
66      public DeleteProcess(StackableSimpleProcess parentProcess, DBEntity entity, List bindVariableSets, Connection conn)
67      {
68          super(parentProcess);
69          this.entity = entity;
70          this.bindVariableSets = bindVariableSets;
71          this.conn = conn;
72          this.setIsUserProcess(true);
73      }
74  
75      /***
76       * This method loops around the bindVariable sets binding the variables to 
77       * a prepared statement and executing them. 
78       * @see com.explosion.utilities.process.threads.SimpleProcess#process()
79       */
80      public void process()
81      {
82          try
83          {
84              if (isStopped())
85                  return;
86              
87              if (bindVariableSets.size() < 1)
88              {
89                  setPercentComplete(100);
90                  return;
91              }
92              setPercentComplete(5);
93              
94              setStatusText("Creating delete statement");
95              DeleteStatement delete = new DeleteStatement(entity);
96              PreparedStatement statement = conn.prepareStatement(delete.getPreparedStatementString(conn.getMetaData()));
97              
98              setStatusText("Starting deletes");
99              try {
100 				int increment = 95 / bindVariableSets.size();
101 				int index = 1;
102 				for (Iterator it = bindVariableSets.iterator(); it.hasNext();)
103 				{
104 				   delete.bindVariables(statement, (Map) it.next());
105                    setStatusText("Executing delete number " + index);
106 				   statement.addBatch();
107 				   setPercentComplete(getPercentComplete() + increment);
108 				   index++;
109 				}
110 				statement.executeBatch();
111 			} finally {
112 				statement.close();
113 			}
114 
115             setStatusText("Done, deletes completed successfully.");
116 
117             this.setPercentComplete(100);
118             log.debug(getStatusText());
119         }
120         catch (Exception e)
121         {
122             setStatusText("Error, unable to delete.");
123             setPercentComplete(100);
124             if (getProcessControl().getStatus() != ProcessThread.THREAD_STOPPED)
125             {
126                 //ie, it hasn't been cancelled
127                 if (this.getProcessControl().getProcess() != null)
128                 {
129                     ((StackableSimpleProcess) this.getProcessControl().getProcess()).manageException(e,"Exception caught while deleting rows");                   
130                 }
131                 else
132                 {
133                   ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while deleting rows.");
134                 }
135             }
136         }
137 
138     }
139 }