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.ArrayList;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.log4j.LogManager;
31  import org.apache.log4j.Logger;
32  
33  import com.explosion.expfmodules.rdbmsconn.dbom.DBEntity;
34  import com.explosion.expfmodules.rdbmsconn.dbom.sql.UpdateStatement;
35  import com.explosion.utilities.exception.ExceptionManagerFactory;
36  import com.explosion.utilities.process.StackableSimpleProcess;
37  import com.explosion.utilities.process.threads.Finishable;
38  import com.explosion.utilities.process.threads.ProcessThread;
39  
40  /***
41   * @author Stephen Cowx Date created:@14-Feb-2003 This class executes a series
42   *         of delete statements
43   */
44  public class UpdateProcess extends StackableSimpleProcess
45  {
46  
47      private static Logger log = LogManager.getLogger(UpdateProcess.class);
48      private DBEntity entity;
49      private List updateValueBindings;
50      private List updateRowIdentifierBindings;
51      private int connectionIdentifier = -1;
52      private Connection conn;
53  
54      /***
55       * Constructor for DeleteProcess.
56       */
57      public UpdateProcess(StackableSimpleProcess parentProcess, DBEntity entity, List updateValueBindings, List updateRowIdentifierBindings, Connection conn)
58      {
59          super(parentProcess);
60          this.entity = entity;
61          this.updateValueBindings = updateValueBindings;
62          this.updateRowIdentifierBindings = updateRowIdentifierBindings; 
63          this.conn = conn;
64          this.setIsUserProcess(true);
65      }
66  
67      /***
68       * Constructor for DeleteProcess.
69       */
70      public UpdateProcess(Finishable finishable, DBEntity entity, List updateValueBindings, List updateRowIdentifierBindings, Connection conn)
71      {
72          super(finishable, null);
73          this.entity = entity;
74          this.updateValueBindings = updateValueBindings;
75          this.updateRowIdentifierBindings = updateRowIdentifierBindings;
76          this.conn = conn;
77          this.setIsUserProcess(true);
78      }
79  
80      /***
81       * This method loops around the bindVariable sets binding the variables to 
82       * a prepared statement and executing them. 
83       * @see com.explosion.utilities.process.threads.SimpleProcess#process()
84       */
85      public void process()
86      {
87          try
88          {
89              if (isStopped())
90                  return;
91              
92              if (updateValueBindings.size() < 1)
93              {
94                  setPercentComplete(100);
95                  return;
96              }
97              
98              setPercentComplete(5);
99              
100             setStatusText("Starting updates");
101             int increment = 95 / updateValueBindings.size();
102             int index = 1;
103             for (int i=0;i<updateValueBindings.size(); i++)
104             {
105                /* We need a set of columns for each row because each row may be inserting different values into different columns columns */
106                Map map = (Map) updateValueBindings.get(i);
107                Map identifiers = (Map) updateRowIdentifierBindings.get(i);
108                
109                List columns = new ArrayList();
110                for (Iterator cols = map.keySet().iterator(); cols.hasNext();)
111                {
112                    columns.add((String)cols.next());
113                }
114                
115                setStatusText("Creating update statement");
116                
117                UpdateStatement updateStatement = new UpdateStatement(entity, columns); 
118                
119                String sql = updateStatement.getPreparedStatementString(conn.getMetaData());
120                PreparedStatement statement = conn.prepareStatement(sql);
121                
122                updateStatement.bindVariables(map, identifiers, statement);
123                log.debug("Statement: " + sql);
124                setStatusText("Executing update " + index);
125                statement.executeUpdate();
126                setPercentComplete(getPercentComplete() + increment);
127                index++;
128             }
129 
130             setStatusText("Done, updates completed successfully.");
131 
132             this.setPercentComplete(100);
133             log.debug(getStatusText());
134         }
135         catch (Exception e)
136         {
137             setStatusText("Error, unable to update.");
138             setPercentComplete(100);
139             if (getProcessControl().getStatus() != ProcessThread.THREAD_STOPPED)
140             {
141                 //ie, it hasn't been cancelled
142                 if (this.getProcessControl().getProcess() != null)
143                 {
144                     ((StackableSimpleProcess) this.getProcessControl().getProcess()).manageException(e,"Exception caught while updating rows");
145                     try {
146 						this.getProcessControl().stop();
147 					} catch (InterruptedException e1) {
148 //						ignore
149 						log.error("Interrupted", e);
150 					}
151                 }
152                 else
153                 {
154                   ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while updating rows.");
155                 }
156             }
157         }
158 
159     }
160 }