-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathganttChart.cls
More file actions
executable file
·230 lines (188 loc) · 9.57 KB
/
ganttChart.cls
File metadata and controls
executable file
·230 lines (188 loc) · 9.57 KB
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
public with sharing class ganttChart {
@AuraEnabled
public static Map<String, Object> getChartData(String recordId, String startTime, String endTime, Integer slotSize, List<String> filterProjects, List<String> filterColors, List<String> filterRoles, String filterStatus) {
Map<String, Object> data = new Map<String, Object>();
String query = 'SELECT Resource__c, Resource__r.Name, Resource__r.Active__c, Resource__r.Default_Role__c, Resource__r.Status__c, Project__c, Project__r.Name, Project__r.Active__c, Project__r.Color__c, Start_Date__c, End_Date__c, Status__c, Effort__c FROM Allocation__c WHERE Start_Date__c <= :endDate AND End_Date__c >= :startDate AND (Project__c = NULL OR Project__r.Active__c = TRUE) AND Resource__r.Active__c = TRUE';
List<Allocation__c> allocations = new List<Allocation__c>();
Map<String, Object> projectById = new Map<String, Object>();
Map<String, Object> resourceById = new Map<String, Object>();
Set<String> roles = new Set<String>();
//キム追記
Set<String> colors = new Set<String>();
if (!filterProjects.isEmpty()) {
query += ' AND Project__c IN :filterProjects';
}
if (!filterColors.isEmpty()) {
query += ' AND Project__r.Color__c IN :filterColors';
}
/*
if (!filterProjectRecords.isEmpty()) {
query += ' AND Project__c IN :filterProjectRecords';
}
*/
if (!filterRoles.isEmpty()) {
query += ' AND Resource__r.Default_Role__c IN :filterRoles';
}
if (String.isNotEmpty(filterStatus)) {
query += ' AND Status__c = :filterStatus';
}
if (String.isNotEmpty(startTime) && String.isNotEmpty(endTime)) {
Date startDate = DateTime.newInstance(Long.valueOf(startTime)).date();
Date endDate = DateTime.newInstance(Long.valueOf(endTime)).date();
Integer days = startDate.daysBetween(endDate) + 1;
slotSize = Integer.valueOf(slotSize);
Decimal slots = days / slotSize;
// Overview View
if (String.isEmpty(recordId)) {
query += ' ORDER BY Resource__r.Name, Project__r.Name NULLS FIRST, Start_Date__c';
System.debug(query);
System.debug(startTime);
System.debug(endTime);
System.debug(slotSize);
allocations = Database.query(query);
// display all active resources
for (Object r : ganttChart.getResources()) {
Map<String, Object> resource = (Map<String, Object>)r;
resourceById.put((String)resource.get('Id'), new Map<String, Object> {
'Id' => resource.get('Id'),
'Name' => resource.get('Name'),
'Default_Role__c' => resource.get('Default_Role__c'),
'Status__c' => resource.get('Status__c'),
'allocationsByProject' => new Map<String, Object>()
});
}
} else {
if (Id.valueOf(recordId).getSobjectType().getDescribe().getName().endsWith('Project__c')) {
data.put('projectId', recordId);
}
query += ' AND (Project__c = :recordId OR Resource__c = :recordId)';
query += ' ORDER BY Resource__r.Name, Project__r.Name NULLS FIRST, Start_Date__c';
allocations = Database.query(query);
// empty state on resource page
if (allocations.isEmpty() && Id.valueOf(recordId).getSobjectType().getDescribe().getName().endsWith('Resource__c')) {
Resource__c resource = [SELECT Id, Name, Active__c, Default_Role__c, Status__c
FROM Resource__c
WHERE Id = :recordId];
resourceById.put(resource.Id, new Map<String, Object> {
'Id' => resource.Id,
'Name' => resource.Name,
'Default_Role__c' => resource.Default_Role__c,
'Status__c' => resource.Status__c,
'allocationsByProject' => new Map<String, Object>()
});
}
}
// organize allocations by resource and project
for (Allocation__c allocation : allocations) {
if (!resourceById.containsKey(allocation.Resource__c)) {
resourceById.put(allocation.Resource__c, new Map<String, Object> {
'Id' => allocation.Resource__c,
'Name' => allocation.Resource__r.Name,
'Default_Role__c' => allocation.Resource__r.Default_Role__c,
'Status__c' => allocation.Resource__r.Status__c,
'allocationsByProject' => new Map<String, Object>()
});
}
Map<String, Object> resource = (Map<String, Object>)resourceById.get(allocation.Resource__c);
Map<String, Object> allocationsByProject = (Map<String, Object>)resource.get('allocationsByProject');
if (!allocationsByProject.containsKey(allocation.Project__c)) {
allocationsByProject.put(allocation.Project__c, new List<Object>());
}
projectById.put(allocation.Project__c, new Map<String, Object> {
'Id' => allocation.Project__c,
'Name' => allocation.Project__r.Name,
'Color'=> allocation.Project__r.Color__c
});
List<Object> projectAllocation = (List<Object>)allocationsByProject.get(allocation.Project__c);
Decimal left = Decimal.valueOf(startDate.daysBetween(allocation.Start_Date__c)) / slotSize;
left = left.round(System.RoundingMode.FLOOR);
Decimal right = Decimal.valueOf(startDate.daysBetween(allocation.End_Date__c)) / slotSize;
right = right.round(System.RoundingMode.FLOOR);
projectAllocation.add(new Map<String, Object> {
'Id' => allocation.Id,
'Start_Date__c' => allocation.Start_Date__c,
'End_Date__c' => allocation.End_Date__c,
'Status__c' => allocation.Status__c,
'Effort__c' => allocation.Effort__c,
'projectName' => allocation.Project__r.Name,
'color' => allocation.Project__r.Color__c,
'left' => left,
'right' => right
});
roles.add(allocation.Resource__r.Default_Role__c);
System.debug('roles' + roles);
//キム追記
colors.add(allocation.Project__r.Color__c);
System.debug('colors' + colors);
}
}
data.put('projects', projectById.values());
data.put('resources', resourceById.values());
data.put('roles', roles);
System.debug('data.putroles' + roles);
data.put('colors', colors);
System.debug('data.putcolors' + colors);
return data;
}
@AuraEnabled
public static List<Object> getResources() {
List<Object> resources = new List<Object>();
for (Resource__c r : [SELECT Id, Name, Default_Role__c, Status__c
FROM Resource__c
WHERE Active__c = true
ORDER BY Default_Role__c, Name]) {
resources.add(new Map<String, Object> {
'Id' => r.Id,
'Name' => r.Name,
'Default_Role__c' => r.Default_Role__c,
'Status__c' => r.Status__c
});
}
return resources;
}
@AuraEnabled
public static List<Project__c> getProjects() {
return [SELECT Id, Name
FROM Project__c
WHERE Active__c = true
ORDER BY Name];
}
@AuraEnabled
public static void saveAllocation(Id allocationId, Id projectId, Id resourceId, String effort, String status, String startDate, String endDate) {
Allocation__c allocation = new Allocation__c(
Start_Date__c = DateTime.newInstance(Long.valueOf(startDate)).date(),
End_Date__c = DateTime.newInstance(Long.valueOf(endDate)).date()
);
// update allocation
if (null != allocationId) {
allocation.Id = allocationId;
} else {
if (null != projectId) {
allocation.Project__c = projectId;
}
allocation.Resource__c = resourceId;
}
if (String.isNotEmpty(effort)) {
allocation.Effort__c = effort;
}
if (String.isNotEmpty(status)) {
allocation.Status__c = status;
// remove project
if ('Unavailable' == status) {
allocation.Project__c = null;
}
}
upsert allocation;
}
@AuraEnabled
public static void deleteAllocation(Id allocationId) {
delete new Allocation__c(Id = allocationId);
}
@AuraEnabled(cacheable=true)
public static List<Holidays__c> getHolidays() {
List<Holidays__c> holidays =[SELECT Id, Name,HolidayDate__c
FROM Holidays__c
];
return holidays;
}
}