1 package com.explosion.datastream.exql.processes;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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
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 }