forked from hwstartup/TheMakerMap.com
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexport.htm
219 lines (207 loc) · 6.71 KB
/
export.htm
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
<script src="./js/parse-1.4.2.js"></script>
<script src="./js/model.js"></script>
<script src="jquery-2.1.4.min.js"></script>
<script>
// data mappings to enable export and import
// format
// var DATA = [class1,class2,class3]
// where classX is like
// {
// 'class': Model class (from js/model.js)
// 'displayName': text representation of the model for display purposes
// 'includes' :[ // needed for relationships
// {
// 'dbColumn': the column in the parent model of the relationship
// 'mappedModel': Model of relationship (must exist in)
// 'displayName': text representation of relationship display purposes
// }
// ]
// }
// source parse config
var PARSE_APP_ID = '';
var PARSE_JS_ID = '';
// destination parse config
var EXPORT_PARSE_APP_ID = '';
var EXPORT_PARSE_JS_ID = '';
var DATA = [
{
'class': MakerMap.Model.PrimaryClassification,
'displayName': 'PrimaryClassification'
},
{
'class': MakerMap.Model.BusinessType,
'displayName': 'BusinessType'
},
{
'class': MakerMap.Model.Maker,
'displayName': 'Maker',
'includes':
[
{
'dbColumn': 'classification',
'mappedModel': MakerMap.Model.PrimaryClassification,
'displayName': 'PrimaryClassification'
},
{
'dbColumn': 'business_type',
'mappedModel': MakerMap.Model.BusinessType,
'displayName': 'BusinessType'
}
]
},
{
'class': MakerMap.Model.MaterialType,
'displayName': 'MaterialType'
},
{
'class': MakerMap.Model.Asset,
'displayName': 'Asset',
'includes':
[
{
'dbColumn': 'material_type',
'mappedModel': MakerMap.Model.MaterialType,
'displayName': 'MaterialType'
}
]
}
];
Parse.initialize(PARSE_APP_ID, PARSE_JS_ID);
var dataExported = dataExport(DATA, function(d){
console.log("Start import");
Parse.initialize(EXPORT_PARSE_APP_ID, EXPORT_PARSE_JS_ID);
dataImport(d, null, function(){
console.log("Restore old parse config");
Parse.initialize(PARSE_APP_ID, PARSE_JS_ID);
});
});
function dataExport(DATA, cb){
var urls = [];
for(var i = 0; i < DATA.length; i++){
var item = DATA[i];
var query = new Parse.Query(DATA[i].class);
urls.push(query.find());
}
Parse.Promise.when(urls).done(function() {
for(var i = 0; i < DATA.length; i++){
DATA[i].rawExport = arguments[i];
}
console.log("data", DATA);
if(typeof(cb) === "function") cb(DATA);
});
}
function dataImport(DATA,classesImported, cb){
var data;
var classesImported = classesImported || [];
var urls = [];
var itemsToProcess = [];
console.log("classes imported", classesImported);
for(var i = 0; i < DATA.length; i++){
var item = DATA[i];
var doExport = false;
if(classesImported.indexOf(item.displayName) == -1)
{
doExport = true;
if(item.includes && item.includes.length > 0){
for(var j = 0; j < item.includes.length; j++){
if(classesImported.indexOf(item.includes[j].displayName) == -1){
doExport = false;
break;
}
}
}
}
if(doExport){
itemsToProcess.push(i);
}
}
if(itemsToProcess.length == 0)
{
console.log("No more to process");
if(typeof(cb) === "function") cb();
return;
}
console.log("itemsToProcess", itemsToProcess.map(function(i){ return DATA[i].displayName}));
var itemsToSave = [];
var dataToSave = [];
for(var i = 0; i < itemsToProcess.length; i++){
var itemToProcess = DATA[itemsToProcess[i]];
var list = [];
for(var j = 0; j < itemToProcess.rawExport.length; j++)
{
var itemData = itemToProcess.rawExport[j];
var item = new itemToProcess.class();
for(var attribute in itemData.attributes){
var val = itemData.attributes[attribute];
if(typeof(val) === "object")
{
if(val.className && val.id)
{
var mappedId = 0;
for(var k = 0; k < DATA.length; k++){
var tmp = new DATA[k].class()
if(tmp.className === val.className)
{
for(var a = 0; a < DATA[k].rawExport.length; a++){
if(DATA[k].rawExport[a].id == val.id){
mappedId = DATA[k].rawImport[a].id;
break;
}
}
}
}
var classObject;
eval('classObject = new MakerMap.Model.' + val.className + '()');
classObject.id = mappedId;
item.set(attribute, classObject);
}
else if(val instanceof Parse.GeoPoint)
{
item.set(attribute, new Parse.GeoPoint({ longitude: val.longitude, latitude: val.latitude }));
}
else
{
console.log(" unknown attriute " + attribute);
}
}
else
{
if(attribute == "createdAt" || attribute == "updatedAt") continue;
if(attribute == "objectId")
item.set(attribute + "_old", val);
else
item.set(attribute, val);
}
}
list.push(item);
}
if(list.length > 0){
itemsToSave.push(itemsToProcess[i]);
dataToSave.push(list);
}
}
console.log("itemsToSave", itemsToSave.map(function(i){ return DATA[i].displayName}));
if(itemsToSave.length == 0)
{
console.log("No more to save");
if(typeof(cb) === "function") cb();
return;
}
if(itemsToSave.length > 0){
for(var i = 0; i < dataToSave.length; i++){
urls.push(Parse.Object.saveAll(dataToSave[i]))
//console.log('save me', dataToSave[i]);
}
}
Parse.Promise.when(urls).done(function( a1, a2 ) {
console.log("calls: "+ arguments.length);
for(var i = 0; i < itemsToSave.length; i++){
classesImported.push(DATA[itemsToSave[i]].displayName);
console.log("result " + i + ": ", arguments[i]);
DATA[itemsToSave[i]].rawImport = arguments[i];
}
dataImport(DATA, classesImported, cb);
});
}
</script>
</html>