-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathImportService.php
More file actions
296 lines (275 loc) · 9.85 KB
/
ImportService.php
File metadata and controls
296 lines (275 loc) · 9.85 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
* This is import class
*
* @package bugfree.protected.service
*/
class ImportService
{
/**
* label convert to field
*
* @param string $label
* @param string $productId
* @param string $infoType
* @param array
*/
private function fieldConv($label, $productId, $infoType)
{
$field = false;
$isBasic = false;
$clazz = ucfirst($infoType) . 'Info';
// $basicFields = $clazz::model()->attributeLabels();
$targetModel = new $clazz();
$basicFields = $targetModel->attributeLabels();
$customFields = FieldConfigService::getCustomFieldLabel($infoType, $productId);
$notAllowFields = array('created_by', 'updated_by', 'created_by_name',
'updated_by_name', 'created_at', 'updated_at', 'modified_by', 'related_result');
foreach($notAllowFields as $field)
{
if(isset($basicFields[$field]))
{
unset($basicFields[$field]);
}
}
$field = array_search($label, $basicFields);
if($field)
{
// hard code for productmodule_id&module_name, assign_to&assign_to_name
if('assign_to' == $field)
{
$field = 'assign_to_name';
}
if('module_name' == $field)
{
$field = 'productmodule_id';
}
$isBasic = true;
}
else
{
$field = array_search($label, $customFields);
}
return array($field, $isBasic);
}
/**
* basic info convert
*
* @todo convert $action for bug import
*
* @param array $basicInfo
* @param string $infoType
* @return string
*/
private function basicInfoConv($basicInfo, $infoType)
{
// hard code for productmodule_id
if(isset($basicInfo['productmodule_id']))
{
$moduleSplitterPos = strpos($basicInfo['productmodule_id'], ProductModule::MODULE_SPLITTER);
if(false !== $moduleSplitterPos)
{
$moduleName = substr($basicInfo['productmodule_id'], $moduleSplitterPos + 1);
$moduleInfo = ProductModule::model()->findByAttributes(array('product_id' => $basicInfo['product_id'], 'full_path_name' => $moduleName));
if(!empty($moduleInfo))
{
$basicInfo['productmodule_id'] = $moduleInfo->id;
}
}
else
{
//$basicInfo['productmodule_id'] = 0;
}
}
// hard code for id
if(isset($basicInfo['id']) && '' == $basicInfo['id'])
{
unset($basicInfo['id']);
}
// hard code for delete_flag
if(isset($basicInfo['delete_flag']))
{
$basicInfo['delete_flag'] = CommonService::getTrueFalseValue($basicInfo['delete_flag']);
}
if(isset($basicInfo['priority']))
{
if(Info::TYPE_CASE == $infoType)
{
$basicInfo['priority'] = ProductService::getCasePriorityValueByName($basicInfo['product_id'], $basicInfo['priority']);
}
else if(Info::TYPE_BUG == $infoType)
{
$basicInfo['priority'] = ProductService::getBugPriorityValueByName($basicInfo['product_id'], $basicInfo['priority']);
}
}
if(isset($basicInfo['severity']) && (Info::TYPE_BUG == $infoType))
{
$basicInfo['severity'] = ProductService::getBugSeverityValueByName($basicInfo['product_id'], $basicInfo['severity']);
}
// @TODO convert for bug import
$bugUserKeyArr = array('resolved_by', 'closed_by');
foreach($bugUserKeyArr as $bugUserKey)
{
if(isset($basicInfo[$bugUserKey]))
{
$resolvedByInfo = TestUserService::getUserInfoByRealname($basicInfo[$bugUserKey]);
if(!empty($resolvedByInfo))
{
$basicInfo[$bugUserKey] = $resolvedByInfo['id'];
}
else
{
unset($basicInfo[$bugUserKey]);
}
}
}
$bugDateKeyArr = array('resolved_at', 'closed_at');
foreach($bugDateKeyArr as $bugDateKey)
{
if(empty($basicInfo[$bugDateKey]))
{
unset($basicInfo[$bugDateKey]);
}
}
return $basicInfo;
}
/**
* get result msg
*
* @param array $results
* @param string $infoType
* @return string
*/
private function getResultMsg($results)
{
$msgGroup = 'Common';
$msg = '';
$rowCount = $failedCount = 0;
// sheet index
foreach($results as $sidx => $sheetResults)
{
foreach($sheetResults as $ridx => $result)
{
$rowCount++;
if(CommonService::$ApiResult['FAIL'] == $result['status'])
{
$failedCount++;
$msg .= Yii::t($msgGroup, 'Sheet {sidx} row {ridx} import failed',
array('{sidx}' => $sidx + 1, '{ridx}' => $ridx + 2));
$msg .= '(';
$infos = array();
foreach($result['detail'] as $info)
{
if(is_array($info))
{
$infos[] = join(' ', $info);
}
else
{
$infos[] = $info;
}
}
$msg .= join(', ', $infos) . ")\n";
}
}
}
if(empty($result))
{
$msg = Yii::t($msgGroup, 'Parse sheet file error');
}
else
{
$msg = Yii::t($msgGroup,
'Import finished! Total: {param0}, success: {param1}, fail: {param2}',
array('{param0}' => $rowCount, '{param1}' => $rowCount - $failedCount, '{param2}' => $failedCount))
. "\n\n" . $msg;
}
return $msg;
}
/**
* import sheet data to bugfree
*
* @param mixed $sheet file or string
* @param string $infoType
*/
public function import($sheet, $productId, $infoType,$productModuleId)
{
$sheetConv = new SheetConv();
$data = $sheetConv->xml2array($sheet);
$results = array();
// sheet index
foreach($data as $sidx => $items)
{
$sheetResult = array();
// item index
foreach($items as $iidx => $item)
{
// hard code for product_id
$basicInfo = array('product_id' => $productId, 'productmodule_id' => $productModuleId);
$customInfo = array();
$isEmpty = true;
foreach($item as $key => $val)
{
unset($item[$key]);
list($field, $isBasic) = $this->fieldConv($key, $productId, $infoType);
if($field)
{
if($isBasic)
{
$basicInfo[$field] = $val;
}
else
{
$customInfo[$field] = $val;
}
}
if('' != $val)
{
$isEmpty = false;
}
}
if(!$isEmpty)
{
$action = Info::ACTION_IMPORT;
if(isset($basicInfo['id']) && ('' != trim($basicInfo['id'])))
{
if(Info::TYPE_BUG == $infoType)//bug not allow update
{
$sheetResult[] = array('status' => CommonService::$ApiResult['FAIL'],
'detail' => array(Yii::t('Common', 'can not update bug by import')));
continue;
}
else
{
$existedCaseInfo = CaseInfo::model()->findByPk((int) $basicInfo['id']);
$resultInfo = array();
if(empty($existedCaseInfo))
{
$resultInfo['status'] = CommonService::$ApiResult['FAIL'];
$resultInfo['detail']['id'] = '[id:' . $basicInfo['id'] . ']' . Yii::t('Common', 'Requested object does not exist');
$sheetResult[] = $resultInfo;
continue;
}
else
{
$basicInfo['action_note'] = ''; //import action_note only when doing new action
if($productId != $existedCaseInfo['product_id'])
{
$resultInfo['status'] = CommonService::$ApiResult['FAIL'];
$resultInfo['detail']['id'] = Yii::t('Common', 'product is different');
$sheetResult[] = $resultInfo;
continue;
}
}
}
}
$basicInfo = $this->basicInfoConv($basicInfo, $infoType);
$sheetResult[] = InfoService::saveInfo($infoType, $action, $basicInfo, $customInfo, array(), array());
}
}
$results[$sidx] = $sheetResult;
}
return $this->getResultMsg($results);
}
}
?>