forked from daptin/daptin
/
action_import_data.go
146 lines (112 loc) · 3.59 KB
/
action_import_data.go
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
package resource
import (
"encoding/base64"
"encoding/json"
"github.com/artpar/api2go"
log "github.com/sirupsen/logrus"
"strings"
)
type ImportDataPerformer struct {
cmsConfig *CmsConfig
cruds map[string]*DbResource
}
func (d *ImportDataPerformer) Name() string {
return "__data_import"
}
func (d *ImportDataPerformer) DoAction(request ActionRequest, inFields map[string]interface{}) (api2go.Responder, []ActionResponse, []error) {
responses := make([]ActionResponse, 0)
tableName, isSubjected := inFields["table_name"]
user, isUserPresent := inFields["user"]
userReferenceId := ""
userIdInt := int64(1)
var err error
if isUserPresent {
userMap := user.(map[string]interface{})
userReferenceId = userMap["reference_id"].(string)
userIdInt, err = d.cruds["user_account"].GetReferenceIdToId("user_account", userReferenceId)
if err != nil {
log.Errorf("Failed to get user id from user reference id: %v", err)
}
}
files := inFields["dump_file"].([]interface{})
truncate_before_insert := inFields["truncate_before_insert"].(bool)
//execute_middleware_chain := inFields["execute_middleware_chain"].(bool)
imports := make(map[string][]interface{})
for _, fileInterface := range files {
file := fileInterface.(map[string]interface{})
fileName := file["name"].(string)
fileContentsBase64 := file["file"].(string)
fileBytes, err := base64.StdEncoding.DecodeString(strings.Split(fileContentsBase64, ",")[1])
if err != nil {
log.Errorf("Failed to read file contents as base64 encoded: %v", err)
continue
}
log.Infof("Processing file: %v", fileName)
var jsonData map[string]interface{}
err = json.Unmarshal(fileBytes, &jsonData)
if err != nil {
log.Errorf("Failed to read data as list: %v", err)
continue
}
if isSubjected {
//log.Infof("Subject isntance: %v", subjectInstance)
//subjectInstanceMap := subjectInstance.(map[string]interface{})
subjectTableName := tableName.(string)
_, ok := imports[subjectTableName]
if !ok {
arr := make([]interface{}, 0)
imports[subjectTableName] = arr
}
imports[subjectTableName] = append(imports[subjectTableName], jsonData[subjectTableName])
} else {
for tableName, val := range jsonData {
_, ok := imports[tableName]
if !ok {
arr := make([]interface{}, 0)
imports[tableName] = arr
}
imports[tableName] = append(imports[tableName], val)
}
}
}
for tableName, importedDatas := range imports {
if truncate_before_insert {
instance, ok := d.cruds[tableName]
if !ok {
log.Infof("Wanted to truncate table, but no instance yet: ", tableName)
d.cruds["world"].TruncateTable(tableName)
continue
}
err := instance.TruncateTable(tableName)
if err != nil {
log.Errorf("Failed to truncate table before importing data: %v", err)
}
}
for _, importedData := range importedDatas {
dataAsArray, ok := importedData.([]interface{})
if !ok {
log.Errorf("Data for [%v] in invalid format", tableName)
continue
}
for _, row := range dataAsArray {
data := row.(map[string]interface{})
if isUserPresent {
data["user_account"] = userIdInt
}
err := d.cruds[tableName].DirectInsert(tableName, data)
if err != nil {
log.Errorf("Was about to inser this: %v", data)
log.Errorf("Failed to direct insert into table [%v] : %v", tableName, err)
}
}
}
}
return nil, responses, nil
}
func NewImportDataPerformer(initConfig *CmsConfig, cruds map[string]*DbResource) (ActionPerformerInterface, error) {
handler := ImportDataPerformer{
cmsConfig: initConfig,
cruds: cruds,
}
return &handler, nil
}