-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
176 lines (142 loc) · 4.28 KB
/
validate.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
/*
Copyright (C) 2020 Daniele Rondina <geaaru@sabayonlinux.org>
Credits goes also to Gogs authors, some code portions and re-implemented design
are also coming from the Gogs project, which is using the go-macaron framework
and was really source of ispiration. Kudos to them!
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package loader
import (
"errors"
"fmt"
"strings"
)
func (i *TimeMasterInstance) Validate(ignoreError bool) error {
dupClients := 0
dupActivities := 0
dupTasks := 0
clientsMap := make(map[string]bool, 0)
activitiesMap := make(map[string]bool, 0)
// Check tasks
tasksMap := make(map[string]bool, 0)
// Check for task or activities with dot on name
for _, c := range i.Clients {
if _, isPresent := clientsMap[c.Name]; isPresent {
if !ignoreError {
return errors.New("Duplicated client " + c.Name)
}
i.Logger.Warning("Found duplicated client " + c.Name)
dupClients++
} else {
clientsMap[c.Name] = true
}
for _, a := range *c.GetActivities() {
if _, isPresent := activitiesMap[a.Name]; isPresent {
if !ignoreError {
return errors.New("Duplicated activity " + a.Name)
}
i.Logger.Warning("Found duplicated activity " + a.Name + " for client " + c.Name)
dupActivities++
} else {
activitiesMap[a.Name] = true
}
if strings.Contains(a.Name, ".") {
i.Logger.Error(
fmt.Sprintf("Activity name %s contains [.] that is a special char.",
a.Name))
if !ignoreError {
return errors.New("Invalid name on activity " + a.Name)
}
}
// Check name task
for _, t := range *a.GetTasks() {
err := t.Validate(ignoreError)
if err != nil {
if !ignoreError {
return err
}
i.Logger.Warning("Invalid task " + t.Name + ": " + err.Error())
}
}
for _, t := range a.GetAllTasksList() {
i.Logger.Debug(
fmt.Sprintf("[%s] [%s] Checking task %s...", c.Name, a.Name, t.Name))
if _, isPresent := tasksMap[t.Name]; isPresent {
if !ignoreError {
return errors.New("Duplicated task " + t.Name + " on activity " + a.Name)
}
i.Logger.Warning("Found duplicated task " + t.Name + " on activity " + a.Name)
dupTasks++
} else {
tasksMap[t.Name] = true
}
}
}
}
// Check if timesheet are related to valid tasks
for _, agenda := range i.Timesheets {
for _, timesheet := range *agenda.GetTimesheets() {
if ok := tasksMap[timesheet.Task]; !ok {
errMsg := fmt.Sprintf("No task %s found for timesheet of user %s of period %s",
timesheet.Task,
timesheet.User,
timesheet.Period.StartPeriod,
)
if !ignoreError {
return errors.New(errMsg)
}
i.Logger.Warning(errMsg)
}
}
}
// Check depends of all tasks and resources allocations
for _, c := range i.Clients {
for _, a := range *c.GetActivities() {
for _, t := range a.GetAllTasksList() {
if len(t.Depends) > 0 {
for _, depend := range t.Depends {
if ok := tasksMap[depend]; !ok {
errMsg := fmt.Sprintf("Invalid dependency %s for task %s",
depend, t.Name)
if !ignoreError {
return errors.New(errMsg)
}
i.Logger.Warning(errMsg)
}
}
}
for _, t := range *a.GetTasks() {
if len(t.AllocatedResource) == 0 && t.Milestone == "" {
errMsg := fmt.Sprintf("No resources defined on task %s.%s",
a.Name, t.Name)
if !ignoreError {
return errors.New(errMsg)
}
i.Logger.Warning(errMsg)
}
}
}
}
}
// Validate resources.
for _, r := range i.Resources {
err := r.Validate()
if err != nil {
errMsg := fmt.Sprintf("Invalid status of the resource %s: %s", r.User, err.Error())
if !ignoreError {
return errors.New(errMsg)
}
i.Logger.Warning(errMsg)
}
}
return nil
}