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.SQLException;
25 import java.util.List;
26
27 import org.apache.log4j.LogManager;
28 import org.apache.log4j.Logger;
29
30 import com.explosion.expfmodules.rdbmsconn.dbom.DBEntity;
31 import com.explosion.expfmodules.rdbmsconn.dbom.utils.Transaction;
32 import com.explosion.utilities.exception.ExceptionHandler;
33 import com.explosion.utilities.exception.ExceptionManagerFactory;
34 import com.explosion.utilities.process.StackableSimpleProcess;
35 import com.explosion.utilities.process.threads.Finishable;
36 import com.explosion.utilities.process.threads.ProcessThread;
37
38 /***
39 * @author Stephen Cowx Date created:@14-Feb-2003
40 * This process stacks the inserts edits and deletes into a single process.
41 */
42 public class UpdateDBWithAllEditsProcess extends StackableSimpleProcess
43 {
44
45 private static Logger log = LogManager.getLogger(UpdateDBWithAllEditsProcess.class);
46 private DBEntity entity;
47 private List insertValueBindings;
48 private List deleteRowIdentifierBindings;
49 private List updateValueBindings;
50 private List updateRowIdentifierBindings;
51 private int connectionIdentifier = -1;
52 private Connection conn;
53 private Throwable error;
54
55 /***
56 * Constructor for UpdateDBWithAllEditsProcess.
57 */
58 public UpdateDBWithAllEditsProcess(Finishable finishable, DBEntity entity, List insertValueBindings, List deleteRowIdentifierBindings, List updateValueBindings, List updateRowIdentifierBindings, Connection conn)
59 {
60 super(finishable, null);
61 this.entity = entity;
62 this.insertValueBindings = insertValueBindings;
63 this.deleteRowIdentifierBindings = deleteRowIdentifierBindings;
64 this.updateValueBindings = updateValueBindings;
65 this.updateRowIdentifierBindings = updateRowIdentifierBindings;
66 this.conn = conn;
67 this.setIsUserProcess(true);
68 }
69
70 /***
71 * This allows subprocesses to report errors to the main process
72 *
73 */
74 public void reportError(Throwable error)
75 {
76 this.error = error;
77 }
78
79 /***
80 * This method executes the updates one set ata atime
81 * @see com.explosion.utilities.process.threads.SimpleProcess#process()
82 */
83 public void process()
84 {
85 Transaction t = new Transaction(conn);
86
87 try
88 {
89 t.begin();
90
91 if (isStopped())
92 return;
93
94 setPercentComplete(0);
95
96
97 ExceptionHandler ehand = new ExceptionHandler(){
98 public void handleException(Throwable e, String message)
99 {
100 error = e;
101 }
102 };
103 this.addExceptionHandler(ehand);
104
105 setStatusText("Executing inserts.");
106 InsertProcess insert = new InsertProcess(this, entity, insertValueBindings , conn);
107 insert.process();
108
109 if (error != null)
110 {
111 throw error;
112 }
113
114 if (isStopped())
115 {
116 setPercentComplete(100);
117 return;
118 }
119 setPercentComplete(33);
120
121 setStatusText("Executing deletes.");
122 DeleteProcess delete = new DeleteProcess(this, entity, deleteRowIdentifierBindings , conn);
123 delete.process();
124 if (error != null)
125 {
126 throw error;
127 }
128
129 if (isStopped())
130 {
131 setPercentComplete(100);
132 return;
133 }
134 setPercentComplete(66);
135
136 setStatusText("Executing updates.");
137 UpdateProcess update = new UpdateProcess(this, entity, updateValueBindings, updateRowIdentifierBindings , conn);
138 update.process();
139 if (error != null)
140 {
141 throw error;
142 }
143
144 if (isStopped())
145 {
146 setPercentComplete(100);
147 return;
148 }
149
150
151 t.commit();
152
153 setStatusText("Done, updates completed successfully.");
154
155 this.setPercentComplete(100);
156 log.debug(getStatusText());
157 }
158 catch (Throwable e)
159 {
160 if (t!= null)
161 {
162 t.rollback();
163 }
164
165 setStatusText("Error, unable to update.");
166 setPercentComplete(100);
167 if (getProcessControl().getStatus() != ProcessThread.THREAD_STOPPED)
168 {
169
170 ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while updating database.");
171 }
172 }
173
174 }
175
176 }