-
Notifications
You must be signed in to change notification settings - Fork 2
/
requirement.go
218 lines (186 loc) · 4.49 KB
/
requirement.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
package main
import (
"crypto/md5"
"encoding/json"
"fmt"
"os"
)
type Requirement struct {
Item
id int64
}
func NewRequirement(id int64) Requirement {
req := Requirement{}
req.id = id
if id == 0 {
fmt.Fprintln(os.Stderr, "warning: trying to create requirement with id 0")
}
return req
}
func (req Requirement) ID() int64 {
return req.id
}
func (req Requirement) UID() int64 {
return req.GetValueInt64("uid")
}
func (req Requirement) SetUID(uid int64) {
req.SetValue("uid", uid)
}
func (req Requirement) Description() string {
return req.GetValueString("description")
}
func (req Requirement) SetDescription(value string) {
req.SetValue("description", value)
}
func (req *Requirement) Rationale() string {
return req.GetValueString("rationale")
}
func (req *Requirement) SetRationale(value string) {
req.SetValue("rationale", value)
}
func (req *Requirement) FitCriterion() string {
return req.GetValueString("fitCriterion")
}
func (req *Requirement) SetFitCriterion(value string) {
req.SetValue("fitCriterion", value)
}
func (req Requirement) Pos() (int, int) {
var x, y int
req.GetValues(map[string]interface{}{
"x": &x,
"y": &y,
})
return x, y
}
func (req Requirement) SetPos(x, y int) {
req.SetValues(map[string]interface{} {
"x": x,
"y": y,
})
}
func (req Requirement) Size() (int, int) {
var width, height int
req.GetValues(map[string]interface{}{
"width": &width,
"height": &height,
})
return width, height
}
func (req Requirement) SetSize(w, h int) {
req.SetValues(map[string]interface{} {
"width": w,
"height": h,
})
}
func (req Requirement) Parent() Item {
// Check if item has parent
if req.IsPropertyNull("parent") {
return nil
}
// If parent is not null, assume parent type isn't null either
return NewItem(req.GetValueInt64("parent"), ItemType(req.GetValueInt("parentType")))
}
func (req Requirement) SetParent(parent Item) {
if parent == nil {
req.SetValues(map[string]interface{}{
"parent": nil,
"parentType": nil,
})
} else {
req.SetValues(map[string]interface{}{
"parent": parent.ID(),
"parentType": GetItemType(parent),
})
}
}
func (req Requirement) Hash() [16]byte {
return md5.Sum([]byte(fmt.Sprintf("%v", req)))
}
func (req *Requirement) GetValue(name string, value interface{}) {
req.GetValues(map[string]interface{}{
name: value,
})
}
func (req *Requirement) GetValues(nameValues map[string]interface{}) {
db := currentProject.Data()
defer db.Close()
for key, value := range nameValues {
if err := db.GetItemValue(req.ID(), "Requirements", key, value); err != nil {
fmt.Fprintln(os.Stderr, "warning: failed to get property", key, ":", err)
}
}
}
func (req *Requirement) GetValueString(name string) string {
var val string
req.GetValue(name, &val)
return val
}
func (req *Requirement) GetValueInt(name string) int {
var val int
req.GetValue(name, &val)
return val
}
func (req *Requirement) GetValueInt64(name string) int64 {
var val int64
req.GetValue(name, &val)
return val
}
func (req *Requirement) SetValue(name string, value interface{}) {
req.SetValues(map[string]interface{}{
name: value,
})
}
func (req *Requirement) SetValues(nameValues map[string]interface{}) {
db := currentProject.Data()
defer db.Close()
for key, value := range nameValues {
db.SetItemValue(req.ID(), "Requirements", key, value)
}
}
func (req Requirement) IsPropertyNull(columnName string) bool {
db := currentProject.Data()
defer db.Close()
return db.IsItemPropertyNull(req.id, "Requirements", columnName)
}
func (req Requirement) ToString() string {
return fmt.Sprintf("problem %v", req.id)
}
func (req Requirement) Children() []Item {
children := make([]Item, 0)
for _, link := range links[req] {
if link.parent == req {
children = append(children, link.child)
}
}
return children
}
type RequirementData struct {
ID string
Description, Rationale, FitCriterion string
LinkText string
Look []uint
Pos []int
Size []int
Children []Item
}
func (req Requirement) MarshalJSON() ([]byte, error) {
var description, rationale, fitCriterion string
req.GetValues(map[string]interface{}{
"description": &description,
"rationale": &rationale,
"fitCriterion": &fitCriterion,
})
x, y := req.Pos()
w, h := req.Size()
jsonData, err := json.Marshal(RequirementData{
ID: fmt.Sprintf("%x", req.UID()),
Description: description,
Rationale: rationale,
FitCriterion: fitCriterion,
Children: req.Children(),
Look: []uint{0, 0, 0},
Pos: []int{x, y},
Size: []int{w, h},
})
return jsonData, err
}