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