-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSAPImportHelper.cs
246 lines (226 loc) · 8.44 KB
/
SAPImportHelper.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Globalization;
using SAP.Middleware.Connector;
using System.Data;
using System.Threading;
namespace ManageCompositeRole
{
class SAPTypeConverter : TypeConverter
{
// Overrides the ConvertTo method of TypeConverter.
public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture,
object value,
Type destinationType)
{
if (destinationType == typeof(DateTime))
{
DateTime result;
if (DateTime.TryParseExact(value.ToString().Trim(),
new String[] { "yyyyMMdd", "HHmmss" },
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal | DateTimeStyles.NoCurrentDateDefault,
out result))
return result;
return result;
}
if (destinationType == typeof(Int32))
{
Int32 result;
if (Int32.TryParse(value.ToString().Trim(), out result))
return result;
return result;
}
if (destinationType == typeof(byte))
{
byte result;
if (byte.TryParse(value.ToString().Trim(), out result))
return result;
return result;
}
if (destinationType == typeof(TimeSpan))
{
DateTime result;
if (DateTime.TryParseExact(value.ToString().Trim(),
new String[] { "yyyyMMdd", "HHmmss" },
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal | DateTimeStyles.NoCurrentDateDefault,
out result))
return result.TimeOfDay;
}
return base.ConvertTo(context, culture, value.ToString().Trim(),
destinationType);
}
}
public class SAPImportHelper
{
private ManageCompositeRole connect;
private int ROWCOUNT = 30000;
private int ROWSKIPS = 0;
private int MAX_RETRY = 10;
// equivalente al carattere 254 tabella ASCII
private String delimiter = "" + (char)254;
private IRfcFunction function;
public event LogEventHandler OperationStatus;
public event CompleteEventHandler CompleteStatus;
private delegate DataTable ImportTable(String table);
public SAPImportHelper(ProxyParameter param)
{
this.connect = new ManageCompositeRole();
this.connect.Login(param);
}
protected virtual void OnLog(LogEventArgs e)
{
LogEventHandler handler = OperationStatus;
if (handler != null)
{
handler(this, e);
}
}
protected virtual void OnComplete(LogEventArgs e)
{
CompleteEventHandler handler = CompleteStatus;
if (handler != null)
{
handler(this, e);
}
}
public DataTable Import(String tableName, DataTable filter)
{
DataTable dt = new DataTable();
dt = InternalImportTable(tableName, filter);
return dt;
}
private DataColumn[] InternalGetColumn(IRfcTable fields)
{
List<DataColumn> result = new List<DataColumn>();
for (int i = 0; i < fields.Count; i++)
result.Add(new DataColumn(fields[i].GetString("FIELDNAME"),
intTypeToType(fields[i].GetChar("TYPE"))));
return result.ToArray();
}
private DataRow InternalGetRow(String row, DataTable dt)
{
DataRow drRow = dt.NewRow();
String[] rowSplit = row.Split(new String[] { delimiter },
StringSplitOptions.None);
if (rowSplit.Length != dt.Columns.Count)
throw new ApplicationException("FIELD COUNT EXCEPTION");
for (int j = 0; j < rowSplit.Length; j++)
{
object value = new SAPTypeConverter().ConvertTo(null,
CultureInfo.InvariantCulture, rowSplit[j],
dt.Columns[j].DataType);
drRow[dt.Columns[j]] = (value == null) ? Convert.DBNull : value;
}
return drRow;
}
private DataTable InternalImportTable(String table, DataTable filter)
{
String whereClause = "";
int downloadedRows = ROWSKIPS = 0;
if (filter != null)
{
for (int i = 0; i < filter.Rows.Count; i++)
{
DataRow filterRow = filter.Rows[i];
if (String.IsNullOrEmpty(whereClause))
whereClause = (String)filterRow["FIELDNAME"] + " " +
(String)filterRow["OPTIONS"];
else
whereClause += " AND " + (String)filterRow["FIELDNAME"] +
" " + (String)filterRow["OPTIONS"];
}
}
DataTable result = new DataTable(table);
bool firstDownload = true;
do
{
ROWSKIPS += downloadedRows;
#region Retry
bool fail = true;
int retryCount = 0;
while (fail)
{
try
{
function = connect.Destination.Repository.
CreateFunction("RFC_READ_TABLE");
function.SetValue("DELIMITER", delimiter);
function.SetValue("ROWSKIPS", ROWSKIPS);
function.SetValue("ROWCOUNT", ROWCOUNT);
function.SetValue("QUERY_TABLE", table);
if (!String.IsNullOrEmpty(whereClause))
{
IRfcTable options = function.GetTable("OPTIONS");
options.Append();
options[0].SetValue("TEXT", whereClause);
}
function.Invoke(connect.Destination);
fail = false;
}
catch (Exception exc)
{
#if DEBUG
OnLog(new LogEventArgs("Errore durante la connessione, tentativo in corso: " + exc.Message));
#endif
Thread.Sleep(500);
retryCount++;
if (retryCount > MAX_RETRY)
throw new ApplicationException("MAX RETRY ATTEMPED");
}
}
#endregion
#if DEBUG
OnLog(new LogEventArgs("Inizio download"));
#endif
IRfcTable dataResult = function.GetTable("DATA");
if (firstDownload)
{
IRfcTable fields = function.GetTable("FIELDS");
result.Columns.AddRange(InternalGetColumn(fields));
firstDownload = false;
}
for (int i = 0; i < dataResult.Count; i++)
{
String row = dataResult[i].GetString("WA");
result.Rows.Add(InternalGetRow(row, result));
}
downloadedRows = dataResult.Count;
} while (ROWCOUNT <= downloadedRows);
OnComplete(new LogEventArgs(table + " completata"));
function = null;
GC.Collect();
return result;
}
private Type intTypeToType(char p)
{
switch (p)
{
case 'b': return typeof(Byte);
case 'D': return typeof(DateTime);
case 'T': return typeof(TimeSpan);
case 'F': return typeof(float);
case 'I':
case 'P':
case 's': return typeof(Int32);
case 'h':
case 'l':
case 'r':
case 'u':
case 'v': return typeof(object);
case 'C':
case 'g':
case 'N':
case 'V':
case 'X':
case 'y': return typeof(String);
default: return typeof(object);
}
}
}
}