-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractAction.java
More file actions
281 lines (242 loc) · 10.8 KB
/
Copy pathAbstractAction.java
File metadata and controls
281 lines (242 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Copyright (c) 2015, salesforce.com, inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.salesforce.dataloader.action;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Logger;
import com.salesforce.dataloader.action.progress.ILoaderProgress;
import com.salesforce.dataloader.action.visitor.IVisitor;
import com.salesforce.dataloader.config.Config;
import com.salesforce.dataloader.config.Messages;
import com.salesforce.dataloader.controller.Controller;
import com.salesforce.dataloader.dao.DataAccessObject;
import com.salesforce.dataloader.dao.DataWriter;
import com.salesforce.dataloader.dao.csv.CSVFileWriter;
import com.salesforce.dataloader.exception.DataAccessObjectException;
import com.salesforce.dataloader.exception.DataAccessObjectInitializationException;
import com.salesforce.dataloader.exception.LoadException;
import com.salesforce.dataloader.exception.MappingInitializationException;
import com.salesforce.dataloader.exception.OperationException;
import com.salesforce.dataloader.exception.ParameterLoadException;
import com.sforce.async.AsyncApiException;
import com.sforce.soap.partner.fault.ApiFault;
import com.sforce.ws.ConnectionException;
/**
* Abstract class for all dataloader actions.
*
* @author Colin Jarvis
* @since 21.0
*/
abstract class AbstractAction implements IAction {
private final ILoaderProgress monitor;
private final Controller controller;
private final IVisitor visitor;
private final DataWriter successWriter;
private final DataWriter errorWriter;
private final DataAccessObject dao;
private final Logger logger;
protected AbstractAction(Controller controller, ILoaderProgress monitor)
throws DataAccessObjectInitializationException {
this.logger = Logger.getLogger(getClass());
this.monitor = monitor;
this.controller = controller;
checkDao(getController().getDao());
this.dao = getController().getDao();
if (writeStatus()) {
this.successWriter = createSuccesWriter();
this.errorWriter = createErrorWriter();
} else {
this.successWriter = null;
this.errorWriter = null;
}
this.visitor = createVisitor();
}
/** This method should throw an error if the data access object is configured incorrectly */
protected abstract void checkDao(DataAccessObject dao) throws DataAccessObjectInitializationException;
/** @return a new IVisitor object to be used by this action */
protected abstract IVisitor createVisitor();
/** flushes any remaining records to or from the dao */
protected abstract void flush() throws OperationException, DataAccessObjectException;
/** subclasses should do operation specific initialization here */
protected abstract void initOperation() throws DataAccessObjectInitializationException, OperationException,
MappingInitializationException, DataAccessObjectException;
/** @return a list of the data columns that will be in the success and error files */
protected abstract List<String> getStatusColumns() throws OperationException;
/**
* Process some records.
*
* @return true If there are more rows to process
* @throws com.sforce.ws.ConnectionException
*/
protected abstract boolean visit() throws OperationException, DataAccessObjectException, ParameterLoadException,
ConnectionException, AsyncApiException, com.sforce.ws.ConnectionException;
/**
* @return true if error and success files should be used in this operation
*/
protected abstract boolean writeStatus();
@Override
public final void execute() {
List<Exception> exceptions = new ArrayList<>();
try {
getLogger().info(getMessage("loading", getConfig().getString(Config.OPERATION)));
getDao().open();
initOperation();
if (writeStatus()) {
final List<String> statusColumns = getStatusColumns();
openSuccessWriter(statusColumns);
openErrorWriter(statusColumns);
}
while (!getMonitor().isCanceled() && visit()) {}
} catch (final Exception e) {
exceptions.add(e);
} finally {
try{
flush(); //make sure we don't early abort here
} catch (Exception e) {
exceptions.add(e);
}
try {
closeAll(); //make sure we don't early abort here
} catch (Exception e){
exceptions.add(e);
}
try {
//if no exceptions occurred then display success/error
if (exceptions.size() == 0) {
final Object[] args = {String.valueOf(getVisitor().getNumberSuccesses()),
getConfig().getString(Config.OPERATION), String.valueOf(getVisitor().getNumberErrors())};
// set the monitor to done
if (getMonitor().isCanceled()) {
getMonitor().doneSuccess(getMessage("cancel", args)); //$NON-NLS-1$
} else {
getMonitor().doneSuccess(getMessage("success", args)); //$NON-NLS-1$
}
}
} catch (Exception e){
exceptions.add(e);
}
//handle each recorded exception
if (exceptions.size() > 0){
exceptions.forEach(this::handleException);
}
}
}
private void closeAll() {
getDao().close();
if (writeStatus()) {
getSuccessWriter().close();
getErrorWriter().close();
}
}
protected Config getConfig() {
return getController().getConfig();
}
protected Controller getController() {
return this.controller;
}
protected DataAccessObject getDao() {
return this.dao;
}
protected DataWriter getErrorWriter() {
return this.errorWriter;
}
protected Logger getLogger() {
return this.logger;
}
protected String getMessage(String key, Object... args) {
final String msg = Messages.getMessage(getClass(), key, true, args);
return msg == null ? Messages.getMessage("Action", key, false, args) : msg;
}
protected ILoaderProgress getMonitor() {
return this.monitor;
}
protected DataWriter getSuccessWriter() {
return this.successWriter;
}
protected IVisitor getVisitor() {
return this.visitor;
}
protected void handleException(Exception e) {
String errMsg = e.getMessage();
if (e instanceof ApiFault) {
errMsg = ((ApiFault)e).getExceptionMessage();
} else if (e instanceof AsyncApiException) {
errMsg = ((AsyncApiException)e).getExceptionMessage();
}
getLogger().error(getMessage("exception"), e);
getMonitor().doneError(errMsg);
}
/**
* @return Error Writer
* @throws DataAccessObjectInitializationException
*/
private DataWriter createErrorWriter() throws DataAccessObjectInitializationException {
final String filename = getConfig().getString(Config.OUTPUT_ERROR);
if (filename == null || filename.length() == 0)
throw new DataAccessObjectInitializationException(getMessage("errorMissingErrorFile"));
// TODO: Make sure that specific DAO is not mentioned: use DataReader, DataWriter, or DataAccessObject
return new CSVFileWriter(filename, getConfig());
}
/**
* @return Success Writer
* @throws DataAccessObjectInitializationException
*/
private DataWriter createSuccesWriter() throws DataAccessObjectInitializationException {
final String filename = getConfig().getString(Config.OUTPUT_SUCCESS);
if (filename == null || filename.length() == 0)
throw new DataAccessObjectInitializationException(getMessage("errorMissingSuccessFile"));
// TODO: Make sure that specific DAO is not mentioned: use DataReader, DataWriter, or DataAccessObject
return new CSVFileWriter(filename, getConfig());
}
private void openErrorWriter(List<String> headers) throws OperationException {
headers = new LinkedList<String>(headers);
// add the ERROR column
headers.add(Config.ERROR_COLUMN_NAME);
try {
getErrorWriter().open();
getErrorWriter().setColumnNames(headers);
} catch (final DataAccessObjectInitializationException e) {
throw new OperationException(
getMessage("errorOpeningErrorFile", getConfig().getString(Config.OUTPUT_ERROR)), e);
}
}
private void openSuccessWriter(List<String> headers) throws LoadException {
headers = new LinkedList<String>(headers);
// add the ID column if not there already
if (!Config.ID_COLUMN_NAME.equals(headers.get(0))) {
headers.add(0, Config.ID_COLUMN_NAME);
}
headers.add(Config.STATUS_COLUMN_NAME);
try {
getSuccessWriter().open();
getSuccessWriter().setColumnNames(headers);
} catch (final DataAccessObjectInitializationException e) {
throw new LoadException(
getMessage("errorOpeningSuccessFile", getConfig().getString(Config.OUTPUT_SUCCESS)), e);
}
}
}