-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractExtractAction.java
More file actions
146 lines (126 loc) · 6.08 KB
/
Copy pathAbstractExtractAction.java
File metadata and controls
146 lines (126 loc) · 6.08 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
/*
* 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.*;
import org.apache.log4j.Logger;
import com.salesforce.dataloader.action.progress.ILoaderProgress;
import com.salesforce.dataloader.action.visitor.IQueryVisitor;
import com.salesforce.dataloader.config.Config;
import com.salesforce.dataloader.config.Messages;
import com.salesforce.dataloader.controller.Controller;
import com.salesforce.dataloader.dao.*;
import com.salesforce.dataloader.exception.*;
import com.salesforce.dataloader.mapping.SOQLMapper;
/**
* Parent class for all extract dataloader actions.
*
* @author Colin jarvis
* @since 21.0
*/
abstract class AbstractExtractAction extends AbstractAction {
protected AbstractExtractAction(Controller controller, ILoaderProgress monitor)
throws DataAccessObjectInitializationException {
super(controller, monitor);
}
@Override
protected boolean visit() throws OperationException, DataAccessObjectException, ParameterLoadException {
getVisitor().visit();
return false;
}
@Override
protected boolean writeStatus() {
return getConfig().getBoolean(Config.ENABLE_EXTRACT_STATUS_OUTPUT);
}
@Override
protected void checkDao(DataAccessObject dao) throws DataAccessObjectInitializationException {
if (!(dao instanceof DataWriter)) {
final String errMsg = getMessage("errorWrongDao", getConfig().getString(Config.DAO_TYPE),
DataAccessObjectFactory.CSV_WRITE_TYPE + " or " + DataAccessObjectFactory.DATABASE_WRITE_TYPE,
getConfig().getString(Config.OPERATION));
getLogger().fatal(errMsg);
throw new DataAccessObjectInitializationException(errMsg);
}
}
@Override
protected DataWriter getDao() {
return (DataWriter)super.getDao();
}
@Override
protected IQueryVisitor getVisitor() {
return (IQueryVisitor)super.getVisitor();
}
private List<String> getDaoColumns() {
((SOQLMapper)getController().getMapper()).initSoqlMapping(getConfig().getString(Config.EXTRACT_SOQL));
return ((SOQLMapper)getController().getMapper()).getDaoColumnsForSoql();
}
/** static so that this can be easily called form ExtractTest */
static List<String> getColumnsFromSoql(String soql, Logger logger) throws ExtractException {
if (soql == null || soql.length() == 0) {
final String errMsg = Messages.getMessage(AbstractExtractAction.class, "errorEmptyQuery");
logger.error(errMsg);
throw new ExtractException(errMsg);
}
// normalize the SOQL string and find the field list
final String trimmedSoql = soql.trim().replaceAll("[\\s]*,[\\s]*", ",");
final String upperSOQL = trimmedSoql.toUpperCase();
final int selectPos = upperSOQL.indexOf("SELECT ");
if (selectPos == -1) {
final String errMsg = Messages.getMessage(AbstractExtractAction.class, "errorMissingSelect", soql);
logger.error(errMsg);
throw new ExtractException(errMsg);
}
final int fieldListStart = selectPos + "SELECT ".length(); //$NON-NLS-1$
final int fieldListEnd = upperSOQL.indexOf(" FROM "); //$NON-NLS-1$
try {
final String fieldString = trimmedSoql.substring(fieldListStart, fieldListEnd).trim();
final String[] fields = fieldString.split(","); //$NON-NLS-1$
return new ArrayList<String>(Arrays.asList(fields));
} catch (final Exception e) {
String errMsg;
if (fieldListStart < "SELECT ".length()) {
errMsg = Messages.getMessage(AbstractExtractAction.class, "errorMissingSelect", soql); //$NON-NLS-1$
} else if (fieldListEnd < 0) {
errMsg = Messages.getMessage(AbstractExtractAction.class, "errorMissingFrom", soql); //$NON-NLS-1$
} else {
errMsg = Messages.getMessage(AbstractExtractAction.class, "errorMalformedQuery", soql); //$NON-NLS-1$
}
logger.error(errMsg, e);
throw new ExtractException(errMsg);
}
}
@Override
protected List<String> getStatusColumns() throws ExtractException {
return getDaoColumns();
}
@Override
protected void initOperation() throws DataAccessObjectInitializationException, OperationException {
// get columns that will be output from the query and open the outputs
final List<String> daoColumns = getDaoColumns();
getDao().setColumnNames(daoColumns);
}
@Override
protected void flush() {}
}