/
building_Hist.go
executable file
·214 lines (176 loc) · 5.27 KB
/
building_Hist.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
package model
import (
"encoding/json"
"log"
"time"
"github.com/DanielRenne/GoCore/core/dbServices"
"github.com/DanielRenne/GoCore/core/store"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"sync"
)
var BuildingsHistory modelBuildingsHistory
type modelBuildingsHistory struct{}
var mongoBuildingsHistoryCollection *mgo.Collection
var collectionBuildingsHistoryMutex *sync.RWMutex
func init() {
store.RegisterHistoryStore(&BuildingsHistory)
collectionBuildingsHistoryMutex = &sync.RWMutex{}
}
type BuildingHistoryRecord struct {
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
TId string `json:"tId" dbIndex:"index" bson:"tId"`
ObjId string `json:"objId" dbIndex:"index" bson:"objId"`
Data string `json:"data" bson:"data"`
Type int `json:"type" bson:"type"`
CreateDate time.Time `json:"createDate" dbIndex:"index" bson:"createDate"`
}
func (obj modelBuildingsHistory) SetCollection(mdb *mgo.Database) {
collectionBuildingsHistoryMutex.Lock()
mongoBuildingsHistoryCollection = mdb.C("BuildingsHistory")
ci := mgo.CollectionInfo{ForceIdIndex: true}
mongoBuildingsHistoryCollection.Create(&ci)
collectionBuildingsHistoryMutex.Unlock()
}
func (obj modelBuildingsHistory) Query() *Query {
var query Query
for {
collectionBuildingsHistoryMutex.RLock()
collection := mongoBuildingsHistoryCollection
collectionBuildingsHistoryMutex.RUnlock()
if collection != nil {
break
}
time.Sleep(time.Millisecond * 1000)
}
collectionBuildingsHistoryMutex.RLock()
collection := mongoBuildingsHistoryCollection
collectionBuildingsHistoryMutex.RUnlock()
query.collection = collection
return &query
}
func (self *modelBuildingsHistory) Rollback(transactionId string) error {
var rows []BuildingHistoryRecord
err := self.Query().Filter(Criteria("tId", transactionId)).All(&rows)
if err != nil {
return err
}
for _, entity := range rows {
originalEntityData := []byte(entity.Data)
var rollbackEntity Building
err = rollbackEntity.Unmarshal(originalEntityData)
if err != nil {
return err
}
rollbackType := entity.Type
if rollbackType == TRANSACTION_CHANGETYPE_DELETE || rollbackType == TRANSACTION_CHANGETYPE_UPDATE {
err = rollbackEntity.Save()
} else {
err = rollbackEntity.Delete()
}
if err == nil {
dbServices.CollectionCache{}.Remove("Building", rollbackEntity.Id.Hex())
}
}
return err
}
func (obj *modelBuildingsHistory) Index() error {
log.Println("Building Indexes for MongoDB collection BuildingsHistory:")
for key, value := range dbServices.GetDBIndexes(BuildingHistoryRecord{}) {
index := mgo.Index{
Key: []string{key},
Unique: false,
Background: true,
}
if value == "unique" {
index.Unique = true
}
collectionBuildingsHistoryMutex.RLock()
collection := mongoBuildingsHistoryCollection
collectionBuildingsHistoryMutex.RUnlock()
err := collection.EnsureIndex(index)
if err != nil {
log.Println("Failed to create index for BuildingHistoryRecord." + key + ": " + err.Error())
} else {
log.Println("Successfully created index for BuildingHistoryRecord." + key)
}
}
return nil
}
func (obj *modelBuildingsHistory) New() *BuildingHistoryRecord {
return &BuildingHistoryRecord{}
}
func (obj *BuildingHistoryRecord) DoesIdExist(objectID interface{}) bool {
var retObj BuildingHistoryRecord
row := modelBuildingsHistory{}
q := row.Query()
err := q.ById(objectID, &retObj)
if err == nil {
return true
} else {
return false
}
}
func (self *BuildingHistoryRecord) Save() error {
objectId := bson.NewObjectId()
if self.Id != "" {
objectId = self.Id
}
collectionBuildingsHistoryMutex.RLock()
changeInfo, err := mongoBuildingsHistoryCollection.UpsertId(objectId, &self)
collectionBuildingsHistoryMutex.RUnlock()
if err != nil {
log.Println("Failed to upsertId for BuildingHistoryRecord: " + err.Error())
return err
}
if changeInfo.UpsertedId != nil {
self.Id = changeInfo.UpsertedId.(bson.ObjectId)
}
return nil
}
func (obj *BuildingHistoryRecord) Reflect() []Field {
return Reflect(BuildingHistoryRecord{})
}
func (self *BuildingHistoryRecord) Delete() error {
collectionBuildingsHistoryMutex.RLock()
collection := mongoBuildingsHistoryCollection
collectionBuildingsHistoryMutex.RUnlock()
return collection.Remove(self)
}
func (self *BuildingHistoryRecord) SaveWithTran(t *Transaction) error {
return nil
}
func (self *BuildingHistoryRecord) JoinFields(s string, q *Query, x int) error {
return nil
}
func (self *BuildingHistoryRecord) GetType() int {
return self.Type
}
func (self *BuildingHistoryRecord) GetData() string {
return self.Data
}
func (self *BuildingHistoryRecord) Unmarshal(data []byte) error {
err := bson.Unmarshal(data, &self)
if err != nil {
return err
}
return nil
}
func (obj *BuildingHistoryRecord) JSONString() (string, error) {
bytes, err := json.Marshal(obj)
return string(bytes), err
}
func (obj *BuildingHistoryRecord) JSONBytes() ([]byte, error) {
return json.Marshal(obj)
}
func (obj *BuildingHistoryRecord) BSONString() (string, error) {
bytes, err := bson.Marshal(obj)
return string(bytes), err
}
func (obj *BuildingHistoryRecord) BSONBytes() (in []byte, err error) {
err = bson.Unmarshal(in, obj)
return
}
func (self *BuildingHistoryRecord) GetId() string {
return self.Id.Hex()
}