-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoteShapeLinkDB.go
626 lines (484 loc) · 22.8 KB
/
NoteShapeLinkDB.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
// generated by stacks/gong/go/models/orm_file_per_struct_back_repo.go
package orm
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"time"
"gorm.io/gorm"
"github.com/tealeg/xlsx/v3"
"github.com/fullstack-lang/gongdoc/go/models"
)
// dummy variable to have the import declaration wihthout compile failure (even if no code needing this import is generated)
var dummy_NoteShapeLink_sql sql.NullBool
var dummy_NoteShapeLink_time time.Duration
var dummy_NoteShapeLink_sort sort.Float64Slice
// NoteShapeLinkAPI is the input in POST API
//
// for POST, API, one needs the fields of the model as well as the fields
// from associations ("Has One" and "Has Many") that are generated to
// fullfill the ORM requirements for associations
//
// swagger:model noteshapelinkAPI
type NoteShapeLinkAPI struct {
gorm.Model
models.NoteShapeLink_WOP
// encoding of pointers
// for API, it cannot be embedded
NoteShapeLinkPointersEncoding NoteShapeLinkPointersEncoding
}
// NoteShapeLinkPointersEncoding encodes pointers to Struct and
// reverse pointers of slice of poitners to Struct
type NoteShapeLinkPointersEncoding struct {
// insertion for pointer fields encoding declaration
}
// NoteShapeLinkDB describes a noteshapelink in the database
//
// It incorporates the GORM ID, basic fields from the model (because they can be serialized),
// the encoded version of pointers
//
// swagger:model noteshapelinkDB
type NoteShapeLinkDB struct {
gorm.Model
// insertion for basic fields declaration
// Declation for basic field noteshapelinkDB.Name
Name_Data sql.NullString
// Declation for basic field noteshapelinkDB.Identifier
Identifier_Data sql.NullString
// Declation for basic field noteshapelinkDB.Type
Type_Data sql.NullString
// encoding of pointers
// for GORM serialization, it is necessary to embed to Pointer Encoding declaration
NoteShapeLinkPointersEncoding
}
// NoteShapeLinkDBs arrays noteshapelinkDBs
// swagger:response noteshapelinkDBsResponse
type NoteShapeLinkDBs []NoteShapeLinkDB
// NoteShapeLinkDBResponse provides response
// swagger:response noteshapelinkDBResponse
type NoteShapeLinkDBResponse struct {
NoteShapeLinkDB
}
// NoteShapeLinkWOP is a NoteShapeLink without pointers (WOP is an acronym for "Without Pointers")
// it holds the same basic fields but pointers are encoded into uint
type NoteShapeLinkWOP struct {
ID int `xlsx:"0"`
// insertion for WOP basic fields
Name string `xlsx:"1"`
Identifier string `xlsx:"2"`
Type models.NoteShapeLinkType `xlsx:"3"`
// insertion for WOP pointer fields
}
var NoteShapeLink_Fields = []string{
// insertion for WOP basic fields
"ID",
"Name",
"Identifier",
"Type",
}
type BackRepoNoteShapeLinkStruct struct {
// stores NoteShapeLinkDB according to their gorm ID
Map_NoteShapeLinkDBID_NoteShapeLinkDB map[uint]*NoteShapeLinkDB
// stores NoteShapeLinkDB ID according to NoteShapeLink address
Map_NoteShapeLinkPtr_NoteShapeLinkDBID map[*models.NoteShapeLink]uint
// stores NoteShapeLink according to their gorm ID
Map_NoteShapeLinkDBID_NoteShapeLinkPtr map[uint]*models.NoteShapeLink
db *gorm.DB
stage *models.StageStruct
}
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) GetStage() (stage *models.StageStruct) {
stage = backRepoNoteShapeLink.stage
return
}
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) GetDB() *gorm.DB {
return backRepoNoteShapeLink.db
}
// GetNoteShapeLinkDBFromNoteShapeLinkPtr is a handy function to access the back repo instance from the stage instance
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) GetNoteShapeLinkDBFromNoteShapeLinkPtr(noteshapelink *models.NoteShapeLink) (noteshapelinkDB *NoteShapeLinkDB) {
id := backRepoNoteShapeLink.Map_NoteShapeLinkPtr_NoteShapeLinkDBID[noteshapelink]
noteshapelinkDB = backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkDB[id]
return
}
// BackRepoNoteShapeLink.CommitPhaseOne commits all staged instances of NoteShapeLink to the BackRepo
// Phase One is the creation of instance in the database if it is not yet done to get the unique ID for each staged instance
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) CommitPhaseOne(stage *models.StageStruct) (Error error) {
for noteshapelink := range stage.NoteShapeLinks {
backRepoNoteShapeLink.CommitPhaseOneInstance(noteshapelink)
}
// parse all backRepo instance and checks wether some instance have been unstaged
// in this case, remove them from the back repo
for id, noteshapelink := range backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkPtr {
if _, ok := stage.NoteShapeLinks[noteshapelink]; !ok {
backRepoNoteShapeLink.CommitDeleteInstance(id)
}
}
return
}
// BackRepoNoteShapeLink.CommitDeleteInstance commits deletion of NoteShapeLink to the BackRepo
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) CommitDeleteInstance(id uint) (Error error) {
noteshapelink := backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkPtr[id]
// noteshapelink is not staged anymore, remove noteshapelinkDB
noteshapelinkDB := backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkDB[id]
query := backRepoNoteShapeLink.db.Unscoped().Delete(¬eshapelinkDB)
if query.Error != nil {
log.Fatal(query.Error)
}
// update stores
delete(backRepoNoteShapeLink.Map_NoteShapeLinkPtr_NoteShapeLinkDBID, noteshapelink)
delete(backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkPtr, id)
delete(backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkDB, id)
return
}
// BackRepoNoteShapeLink.CommitPhaseOneInstance commits noteshapelink staged instances of NoteShapeLink to the BackRepo
// Phase One is the creation of instance in the database if it is not yet done to get the unique ID for each staged instance
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) CommitPhaseOneInstance(noteshapelink *models.NoteShapeLink) (Error error) {
// check if the noteshapelink is not commited yet
if _, ok := backRepoNoteShapeLink.Map_NoteShapeLinkPtr_NoteShapeLinkDBID[noteshapelink]; ok {
return
}
// initiate noteshapelink
var noteshapelinkDB NoteShapeLinkDB
noteshapelinkDB.CopyBasicFieldsFromNoteShapeLink(noteshapelink)
query := backRepoNoteShapeLink.db.Create(¬eshapelinkDB)
if query.Error != nil {
log.Fatal(query.Error)
}
// update stores
backRepoNoteShapeLink.Map_NoteShapeLinkPtr_NoteShapeLinkDBID[noteshapelink] = noteshapelinkDB.ID
backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkPtr[noteshapelinkDB.ID] = noteshapelink
backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkDB[noteshapelinkDB.ID] = ¬eshapelinkDB
return
}
// BackRepoNoteShapeLink.CommitPhaseTwo commits all staged instances of NoteShapeLink to the BackRepo
// Phase Two is the update of instance with the field in the database
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) CommitPhaseTwo(backRepo *BackRepoStruct) (Error error) {
for idx, noteshapelink := range backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkPtr {
backRepoNoteShapeLink.CommitPhaseTwoInstance(backRepo, idx, noteshapelink)
}
return
}
// BackRepoNoteShapeLink.CommitPhaseTwoInstance commits {{structname }} of models.NoteShapeLink to the BackRepo
// Phase Two is the update of instance with the field in the database
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) CommitPhaseTwoInstance(backRepo *BackRepoStruct, idx uint, noteshapelink *models.NoteShapeLink) (Error error) {
// fetch matching noteshapelinkDB
if noteshapelinkDB, ok := backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkDB[idx]; ok {
noteshapelinkDB.CopyBasicFieldsFromNoteShapeLink(noteshapelink)
// insertion point for translating pointers encodings into actual pointers
query := backRepoNoteShapeLink.db.Save(¬eshapelinkDB)
if query.Error != nil {
log.Fatalln(query.Error)
}
} else {
err := errors.New(
fmt.Sprintf("Unkown NoteShapeLink intance %s", noteshapelink.Name))
return err
}
return
}
// BackRepoNoteShapeLink.CheckoutPhaseOne Checkouts all BackRepo instances to the Stage
//
// Phase One will result in having instances on the stage aligned with the back repo
// pointers are not initialized yet (this is for phase two)
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) CheckoutPhaseOne() (Error error) {
noteshapelinkDBArray := make([]NoteShapeLinkDB, 0)
query := backRepoNoteShapeLink.db.Find(¬eshapelinkDBArray)
if query.Error != nil {
return query.Error
}
// list of instances to be removed
// start from the initial map on the stage and remove instances that have been checked out
noteshapelinkInstancesToBeRemovedFromTheStage := make(map[*models.NoteShapeLink]any)
for key, value := range backRepoNoteShapeLink.stage.NoteShapeLinks {
noteshapelinkInstancesToBeRemovedFromTheStage[key] = value
}
// copy orm objects to the the map
for _, noteshapelinkDB := range noteshapelinkDBArray {
backRepoNoteShapeLink.CheckoutPhaseOneInstance(¬eshapelinkDB)
// do not remove this instance from the stage, therefore
// remove instance from the list of instances to be be removed from the stage
noteshapelink, ok := backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkPtr[noteshapelinkDB.ID]
if ok {
delete(noteshapelinkInstancesToBeRemovedFromTheStage, noteshapelink)
}
}
// remove from stage and back repo's 3 maps all noteshapelinks that are not in the checkout
for noteshapelink := range noteshapelinkInstancesToBeRemovedFromTheStage {
noteshapelink.Unstage(backRepoNoteShapeLink.GetStage())
// remove instance from the back repo 3 maps
noteshapelinkID := backRepoNoteShapeLink.Map_NoteShapeLinkPtr_NoteShapeLinkDBID[noteshapelink]
delete(backRepoNoteShapeLink.Map_NoteShapeLinkPtr_NoteShapeLinkDBID, noteshapelink)
delete(backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkDB, noteshapelinkID)
delete(backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkPtr, noteshapelinkID)
}
return
}
// CheckoutPhaseOneInstance takes a noteshapelinkDB that has been found in the DB, updates the backRepo and stages the
// models version of the noteshapelinkDB
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) CheckoutPhaseOneInstance(noteshapelinkDB *NoteShapeLinkDB) (Error error) {
noteshapelink, ok := backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkPtr[noteshapelinkDB.ID]
if !ok {
noteshapelink = new(models.NoteShapeLink)
backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkPtr[noteshapelinkDB.ID] = noteshapelink
backRepoNoteShapeLink.Map_NoteShapeLinkPtr_NoteShapeLinkDBID[noteshapelink] = noteshapelinkDB.ID
// append model store with the new element
noteshapelink.Name = noteshapelinkDB.Name_Data.String
noteshapelink.Stage(backRepoNoteShapeLink.GetStage())
}
noteshapelinkDB.CopyBasicFieldsToNoteShapeLink(noteshapelink)
// in some cases, the instance might have been unstaged. It is necessary to stage it again
noteshapelink.Stage(backRepoNoteShapeLink.GetStage())
// preserve pointer to noteshapelinkDB. Otherwise, pointer will is recycled and the map of pointers
// Map_NoteShapeLinkDBID_NoteShapeLinkDB)[noteshapelinkDB hold variable pointers
noteshapelinkDB_Data := *noteshapelinkDB
preservedPtrToNoteShapeLink := ¬eshapelinkDB_Data
backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkDB[noteshapelinkDB.ID] = preservedPtrToNoteShapeLink
return
}
// BackRepoNoteShapeLink.CheckoutPhaseTwo Checkouts all staged instances of NoteShapeLink to the BackRepo
// Phase Two is the update of instance with the field in the database
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) CheckoutPhaseTwo(backRepo *BackRepoStruct) (Error error) {
// parse all DB instance and update all pointer fields of the translated models instance
for _, noteshapelinkDB := range backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkDB {
backRepoNoteShapeLink.CheckoutPhaseTwoInstance(backRepo, noteshapelinkDB)
}
return
}
// BackRepoNoteShapeLink.CheckoutPhaseTwoInstance Checkouts staged instances of NoteShapeLink to the BackRepo
// Phase Two is the update of instance with the field in the database
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) CheckoutPhaseTwoInstance(backRepo *BackRepoStruct, noteshapelinkDB *NoteShapeLinkDB) (Error error) {
noteshapelink := backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkPtr[noteshapelinkDB.ID]
noteshapelinkDB.DecodePointers(backRepo, noteshapelink)
return
}
func (noteshapelinkDB *NoteShapeLinkDB) DecodePointers(backRepo *BackRepoStruct, noteshapelink *models.NoteShapeLink) {
// insertion point for checkout of pointer encoding
return
}
// CommitNoteShapeLink allows commit of a single noteshapelink (if already staged)
func (backRepo *BackRepoStruct) CommitNoteShapeLink(noteshapelink *models.NoteShapeLink) {
backRepo.BackRepoNoteShapeLink.CommitPhaseOneInstance(noteshapelink)
if id, ok := backRepo.BackRepoNoteShapeLink.Map_NoteShapeLinkPtr_NoteShapeLinkDBID[noteshapelink]; ok {
backRepo.BackRepoNoteShapeLink.CommitPhaseTwoInstance(backRepo, id, noteshapelink)
}
backRepo.CommitFromBackNb = backRepo.CommitFromBackNb + 1
}
// CommitNoteShapeLink allows checkout of a single noteshapelink (if already staged and with a BackRepo id)
func (backRepo *BackRepoStruct) CheckoutNoteShapeLink(noteshapelink *models.NoteShapeLink) {
// check if the noteshapelink is staged
if _, ok := backRepo.BackRepoNoteShapeLink.Map_NoteShapeLinkPtr_NoteShapeLinkDBID[noteshapelink]; ok {
if id, ok := backRepo.BackRepoNoteShapeLink.Map_NoteShapeLinkPtr_NoteShapeLinkDBID[noteshapelink]; ok {
var noteshapelinkDB NoteShapeLinkDB
noteshapelinkDB.ID = id
if err := backRepo.BackRepoNoteShapeLink.db.First(¬eshapelinkDB, id).Error; err != nil {
log.Fatalln("CheckoutNoteShapeLink : Problem with getting object with id:", id)
}
backRepo.BackRepoNoteShapeLink.CheckoutPhaseOneInstance(¬eshapelinkDB)
backRepo.BackRepoNoteShapeLink.CheckoutPhaseTwoInstance(backRepo, ¬eshapelinkDB)
}
}
}
// CopyBasicFieldsFromNoteShapeLink
func (noteshapelinkDB *NoteShapeLinkDB) CopyBasicFieldsFromNoteShapeLink(noteshapelink *models.NoteShapeLink) {
// insertion point for fields commit
noteshapelinkDB.Name_Data.String = noteshapelink.Name
noteshapelinkDB.Name_Data.Valid = true
noteshapelinkDB.Identifier_Data.String = noteshapelink.Identifier
noteshapelinkDB.Identifier_Data.Valid = true
noteshapelinkDB.Type_Data.String = noteshapelink.Type.ToString()
noteshapelinkDB.Type_Data.Valid = true
}
// CopyBasicFieldsFromNoteShapeLink_WOP
func (noteshapelinkDB *NoteShapeLinkDB) CopyBasicFieldsFromNoteShapeLink_WOP(noteshapelink *models.NoteShapeLink_WOP) {
// insertion point for fields commit
noteshapelinkDB.Name_Data.String = noteshapelink.Name
noteshapelinkDB.Name_Data.Valid = true
noteshapelinkDB.Identifier_Data.String = noteshapelink.Identifier
noteshapelinkDB.Identifier_Data.Valid = true
noteshapelinkDB.Type_Data.String = noteshapelink.Type.ToString()
noteshapelinkDB.Type_Data.Valid = true
}
// CopyBasicFieldsFromNoteShapeLinkWOP
func (noteshapelinkDB *NoteShapeLinkDB) CopyBasicFieldsFromNoteShapeLinkWOP(noteshapelink *NoteShapeLinkWOP) {
// insertion point for fields commit
noteshapelinkDB.Name_Data.String = noteshapelink.Name
noteshapelinkDB.Name_Data.Valid = true
noteshapelinkDB.Identifier_Data.String = noteshapelink.Identifier
noteshapelinkDB.Identifier_Data.Valid = true
noteshapelinkDB.Type_Data.String = noteshapelink.Type.ToString()
noteshapelinkDB.Type_Data.Valid = true
}
// CopyBasicFieldsToNoteShapeLink
func (noteshapelinkDB *NoteShapeLinkDB) CopyBasicFieldsToNoteShapeLink(noteshapelink *models.NoteShapeLink) {
// insertion point for checkout of basic fields (back repo to stage)
noteshapelink.Name = noteshapelinkDB.Name_Data.String
noteshapelink.Identifier = noteshapelinkDB.Identifier_Data.String
noteshapelink.Type.FromString(noteshapelinkDB.Type_Data.String)
}
// CopyBasicFieldsToNoteShapeLink_WOP
func (noteshapelinkDB *NoteShapeLinkDB) CopyBasicFieldsToNoteShapeLink_WOP(noteshapelink *models.NoteShapeLink_WOP) {
// insertion point for checkout of basic fields (back repo to stage)
noteshapelink.Name = noteshapelinkDB.Name_Data.String
noteshapelink.Identifier = noteshapelinkDB.Identifier_Data.String
noteshapelink.Type.FromString(noteshapelinkDB.Type_Data.String)
}
// CopyBasicFieldsToNoteShapeLinkWOP
func (noteshapelinkDB *NoteShapeLinkDB) CopyBasicFieldsToNoteShapeLinkWOP(noteshapelink *NoteShapeLinkWOP) {
noteshapelink.ID = int(noteshapelinkDB.ID)
// insertion point for checkout of basic fields (back repo to stage)
noteshapelink.Name = noteshapelinkDB.Name_Data.String
noteshapelink.Identifier = noteshapelinkDB.Identifier_Data.String
noteshapelink.Type.FromString(noteshapelinkDB.Type_Data.String)
}
// Backup generates a json file from a slice of all NoteShapeLinkDB instances in the backrepo
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) Backup(dirPath string) {
filename := filepath.Join(dirPath, "NoteShapeLinkDB.json")
// organize the map into an array with increasing IDs, in order to have repoductible
// backup file
forBackup := make([]*NoteShapeLinkDB, 0)
for _, noteshapelinkDB := range backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkDB {
forBackup = append(forBackup, noteshapelinkDB)
}
sort.Slice(forBackup[:], func(i, j int) bool {
return forBackup[i].ID < forBackup[j].ID
})
file, err := json.MarshalIndent(forBackup, "", " ")
if err != nil {
log.Fatal("Cannot json NoteShapeLink ", filename, " ", err.Error())
}
err = ioutil.WriteFile(filename, file, 0644)
if err != nil {
log.Fatal("Cannot write the json NoteShapeLink file", err.Error())
}
}
// Backup generates a json file from a slice of all NoteShapeLinkDB instances in the backrepo
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) BackupXL(file *xlsx.File) {
// organize the map into an array with increasing IDs, in order to have repoductible
// backup file
forBackup := make([]*NoteShapeLinkDB, 0)
for _, noteshapelinkDB := range backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkDB {
forBackup = append(forBackup, noteshapelinkDB)
}
sort.Slice(forBackup[:], func(i, j int) bool {
return forBackup[i].ID < forBackup[j].ID
})
sh, err := file.AddSheet("NoteShapeLink")
if err != nil {
log.Fatal("Cannot add XL file", err.Error())
}
_ = sh
row := sh.AddRow()
row.WriteSlice(&NoteShapeLink_Fields, -1)
for _, noteshapelinkDB := range forBackup {
var noteshapelinkWOP NoteShapeLinkWOP
noteshapelinkDB.CopyBasicFieldsToNoteShapeLinkWOP(¬eshapelinkWOP)
row := sh.AddRow()
row.WriteStruct(¬eshapelinkWOP, -1)
}
}
// RestoreXL from the "NoteShapeLink" sheet all NoteShapeLinkDB instances
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) RestoreXLPhaseOne(file *xlsx.File) {
// resets the map
BackRepoNoteShapeLinkid_atBckpTime_newID = make(map[uint]uint)
sh, ok := file.Sheet["NoteShapeLink"]
_ = sh
if !ok {
log.Fatal(errors.New("sheet not found"))
}
// log.Println("Max row is", sh.MaxRow)
err := sh.ForEachRow(backRepoNoteShapeLink.rowVisitorNoteShapeLink)
if err != nil {
log.Fatal("Err=", err)
}
}
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) rowVisitorNoteShapeLink(row *xlsx.Row) error {
log.Printf("row line %d\n", row.GetCoordinate())
log.Println(row)
// skip first line
if row.GetCoordinate() > 0 {
var noteshapelinkWOP NoteShapeLinkWOP
row.ReadStruct(¬eshapelinkWOP)
// add the unmarshalled struct to the stage
noteshapelinkDB := new(NoteShapeLinkDB)
noteshapelinkDB.CopyBasicFieldsFromNoteShapeLinkWOP(¬eshapelinkWOP)
noteshapelinkDB_ID_atBackupTime := noteshapelinkDB.ID
noteshapelinkDB.ID = 0
query := backRepoNoteShapeLink.db.Create(noteshapelinkDB)
if query.Error != nil {
log.Fatal(query.Error)
}
backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkDB[noteshapelinkDB.ID] = noteshapelinkDB
BackRepoNoteShapeLinkid_atBckpTime_newID[noteshapelinkDB_ID_atBackupTime] = noteshapelinkDB.ID
}
return nil
}
// RestorePhaseOne read the file "NoteShapeLinkDB.json" in dirPath that stores an array
// of NoteShapeLinkDB and stores it in the database
// the map BackRepoNoteShapeLinkid_atBckpTime_newID is updated accordingly
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) RestorePhaseOne(dirPath string) {
// resets the map
BackRepoNoteShapeLinkid_atBckpTime_newID = make(map[uint]uint)
filename := filepath.Join(dirPath, "NoteShapeLinkDB.json")
jsonFile, err := os.Open(filename)
// if we os.Open returns an error then handle it
if err != nil {
log.Fatal("Cannot restore/open the json NoteShapeLink file", filename, " ", err.Error())
}
// read our opened jsonFile as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
var forRestore []*NoteShapeLinkDB
err = json.Unmarshal(byteValue, &forRestore)
// fill up Map_NoteShapeLinkDBID_NoteShapeLinkDB
for _, noteshapelinkDB := range forRestore {
noteshapelinkDB_ID_atBackupTime := noteshapelinkDB.ID
noteshapelinkDB.ID = 0
query := backRepoNoteShapeLink.db.Create(noteshapelinkDB)
if query.Error != nil {
log.Fatal(query.Error)
}
backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkDB[noteshapelinkDB.ID] = noteshapelinkDB
BackRepoNoteShapeLinkid_atBckpTime_newID[noteshapelinkDB_ID_atBackupTime] = noteshapelinkDB.ID
}
if err != nil {
log.Fatal("Cannot restore/unmarshall json NoteShapeLink file", err.Error())
}
}
// RestorePhaseTwo uses all map BackRepo<NoteShapeLink>id_atBckpTime_newID
// to compute new index
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) RestorePhaseTwo() {
for _, noteshapelinkDB := range backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkDB {
// next line of code is to avert unused variable compilation error
_ = noteshapelinkDB
// insertion point for reindexing pointers encoding
// update databse with new index encoding
query := backRepoNoteShapeLink.db.Model(noteshapelinkDB).Updates(*noteshapelinkDB)
if query.Error != nil {
log.Fatal(query.Error)
}
}
}
// BackRepoNoteShapeLink.ResetReversePointers commits all staged instances of NoteShapeLink to the BackRepo
// Phase Two is the update of instance with the field in the database
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) ResetReversePointers(backRepo *BackRepoStruct) (Error error) {
for idx, noteshapelink := range backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkPtr {
backRepoNoteShapeLink.ResetReversePointersInstance(backRepo, idx, noteshapelink)
}
return
}
func (backRepoNoteShapeLink *BackRepoNoteShapeLinkStruct) ResetReversePointersInstance(backRepo *BackRepoStruct, idx uint, noteshapelink *models.NoteShapeLink) (Error error) {
// fetch matching noteshapelinkDB
if noteshapelinkDB, ok := backRepoNoteShapeLink.Map_NoteShapeLinkDBID_NoteShapeLinkDB[idx]; ok {
_ = noteshapelinkDB // to avoid unused variable error if there are no reverse to reset
// insertion point for reverse pointers reset
// end of insertion point for reverse pointers reset
}
return
}
// this field is used during the restauration process.
// it stores the ID at the backup time and is used for renumbering
var BackRepoNoteShapeLinkid_atBckpTime_newID map[uint]uint