-
Notifications
You must be signed in to change notification settings - Fork 521
/
Copy pathMilestone1_GettingStartedController.cls
executable file
·366 lines (283 loc) · 16.6 KB
/
Milestone1_GettingStartedController.cls
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
/*
Copyright (c) 2011, salesforce.com, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the salesforce.com, Inc. nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* * NEW WITH VERSION 2
* Date : October 3, 2016
* Copyright : salesforce.com, Inc. (c) 2016
* * @author : Shae Selix
*
* Lightning styling and compatibility
* -- Added SLDS css to all custom pages and components
* -- Replaced dynamic reports with Lightning component on Summary page
*
* Increased security
* -- XML Import, Move and Clone tools only enabled for users with all
* field-level edit-access
* -- Safe deletions within triggers
*
* Removed Deprecated/Non-functional Elements
* -- Add-task Homepage component
* -- Change chatter follow settings within Task trigger
*
* * TODO
* Refactor Lightning component for individual projects
* Add simple 'create task' feature to Lightning component
* Replace Google charts with Charts.js Lightning components
* Refactor for Mobile-readiness / separate mobile app
* Eventually, replace all VF pages with Lightning pages
*/
public with sharing class Milestone1_GettingStartedController {
public Boolean settingsReady {get;set;}
public Boolean projectReady {get;set;}
public String lastResult {get;set;}
public Milestone1_GettingStartedController() {
settingsReady = Milestone1_Settings__c.getOrgDefaults() != null;
}
public void setupStandardSettings() {
Milestone1_Settings__c settings = Milestone1_Settings__c.getOrgDefaults();
if (settings != null) {
lastResult = system.label.Milestone1_SettingsAlreadyExistNoChanges;
} else {
settings = getInitialSettingsWithoutSave();
Milestone1_Security_Utility.oneRecordFieldCreateable(
settings,
Milestone1_Settings__c.SObjectType.getDescribe());
if (Schema.sObjectType.Milestone1_Settings__c.isCreateable()) {
insert settings;
} else {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: User is unauthorized to act on this object/field.'));
}
settingsReady = true;
handleGettingStartedProjectInitialization();
}
}
public Milestone1_Project__c handleGettingStartedProjectInitialization() {
List<Milestone1_Task__c> tasks = new List<Milestone1_Task__c>();
List<Milestone1_Milestone__c> milestones = new List<Milestone1_Milestone__c>();
Milestone1_Project__c project = null;
List<Document> docs = new List<Document>();
if (Schema.sObjectType.Document.isAccessible()) {
docs = [Select d.Name, d.Id, d.Body From Document d Where d.Name = 'Starter Project (LPM1)'];
} else {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: User is unauthorized to act on this object/field.'));
}
if (docs.size() == 1) {
Document startProject = docs.get(0);
Milestone1_XML_Import_Utility util = new Milestone1_XML_Import_Utility();
project = util.importProject(startProject.body.toString());
//now we need to go through and update the project with start date and deadlines.
//Milestones
List<Milestone1_Milestone__c> milestonesRetrieved = new List<Milestone1_Milestone__c>();
if (Schema.sObjectType.Milestone1_Milestone__c.isAccessible() && Schema.sObjectType.Milestone1_Project__c.isAccessible()
&& Schema.sObjectType.Milestone1_Task__c.isAccessible()) {
milestonesRetrieved = [Select m.Name, m.Kickoff__c, m.Id, m.Alias__c, m.Deadline__c, m.Project__c,
(Select Id, Name, Blocked_Reason__c, Blocked__c, Complete__c, Due_Date__c, Priority__c, Start_Date__c
From Project_Tasks__r) From Milestone1_Milestone__c m WHERE m.Project__c = :project.id Order By m.Name];
} else {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: User is unauthorized to act on this object/field.'));
}
//start day = 3 days ago
if (Schema.sObjectType.Milestone1_Project__c.fields.Kickoff__c.isUpdateable()) {
project.Kickoff__c = Date.today().addDays(-5);
} else {
throw new Milestone1_Exception('Insufficient Privileges');
}
if (Schema.sObjectType.Milestone1_Project__c.fields.Deadline__c.isUpdateable()) {
project.Deadline__c = Date.today().addDays(28);
} else {
throw new Milestone1_Exception('Insufficient Privileges');
}
if (Schema.sObjectType.Milestone1_Project__c.fields.Status__c.isUpdateable()) {
project.Status__c = 'Active';
} else {
throw new Milestone1_Exception('Insufficient Privileges');
}
if (Schema.sObjectType.Milestone1_Project__c.isUpdateable()) {
update project;
} else {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: User is unauthorized to act on this object/field.'));
}
Integer offset = -4;
Date max_Deadline_Date = Date.today();
integer aliasCount;
//milestones
if (Schema.sObjectType.Milestone1_Milestone__c.isAccessible()) {
aliasCount = [Select count() from Milestone1_Milestone__c Where Alias__c LIKE 'DEMO_%'];
} else {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: User is unauthorized to act on this object/field.'));
}
for (Milestone1_Milestone__c current : milestonesRetrieved) {
aliasCount++;
Date max_Due_Date = Date.today();
if (Schema.sObjectType.Milestone1_Milestone__c.fields.Kickoff__c.isUpdateable()) {
current.Kickoff__c = Date.today().addDays(offset);
} else {
throw new Milestone1_Exception('Insufficient Privileges');
}
for (Milestone1_Task__c task : current.Project_Tasks__r) {
offset++;
if (Schema.sObjectType.Milestone1_Task__c.fields.Start_Date__c.isUpdateable()) {
task.Start_Date__c = Date.today().addDays(offset);
} else {
throw new Milestone1_Exception('Insufficient Privileges');
}
if (Schema.sObjectType.Milestone1_Task__c.fields.Due_Date__c.isUpdateable()) {
task.Due_Date__c = Date.today().addDays(offset+1);
} else {
throw new Milestone1_Exception('Insufficient Privileges');
}
if (Schema.sObjectType.Milestone1_Task__c.fields.Complete__c.isUpdateable()) {
if (current.Name.startsWith('1')) {
task.Complete__c = true;
}
} else {
throw new Milestone1_Exception('Insufficient Privileges');
}
if (max_Due_Date < task.Due_Date__c){
max_Due_Date = task.Due_Date__c;
}
tasks.add(task);
}
if (Schema.sObjectType.Milestone1_Milestone__c.fields.Deadline__c.isUpdateable()) {
current.Deadline__c = max_Due_Date;
} else {
throw new Milestone1_Exception('Insufficient Privileges');
}
if (Schema.sObjectType.Milestone1_Milestone__c.fields.Complete__c.isUpdateable()) {
if (current.Name.startsWith('1')) {
current.Complete__c = true;
}
} else {
throw new Milestone1_Exception('Insufficient Privileges');
}
if (current.Deadline__c > max_Deadline_Date){
max_Deadline_Date = current.Deadline__c;
}
//get a unique milestone alias
String targetAlias = current.Name.substring(0,1);
if (aliasCount > 0) {
targetAlias = ''+(aliasCount+1);
}
if (Schema.sObjectType.Milestone1_Milestone__c.fields.Alias__c.isUpdateable()) {
current.Alias__c = 'DEMO_' + targetAlias;
} else {
throw new Milestone1_Exception('Insufficient Privileges');
}
milestones.add(current);
}
if (Schema.sObjectType.Milestone1_Task__c.isUpdateable()) {
update tasks;
} else {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: User is unauthorized to act on this object/field.'));
}
if (Schema.sObjectType.Milestone1_Milestone__c.isUpdateable()) {
update milestones;
} else {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: User is unauthorized to act on this object/field.'));
}
}
return project;
}
public Milestone1_Settings__c getInitialSettingsWithoutSave() {
Milestone1_Settings__c settings = new Milestone1_Settings__c();
/*
List<Report> reports = [Select Id, Name from Report Where Name like '%(LPM1)'];
Id blockedTasks;
Id myTasks;
Id overdueTasks;
Id overallTaskStatus;
Id openTaskStatus;
Id projMilestones;
Id reportMyTasksAllProjects;
Id reportMyTasksByPriority;
Id summaryBlockedTasks;
Id summaryLateTasks;
//these change from install to install so we have to update in settings
for (Report current : reports) {
System.debug(current.name);
if (current.name.indexOf('Blocked Tasks by Project') > -1) {
blockedTasks = current.Id;
} else if (current.name.indexOf('Summary: My Blocked Tasks') > -1) {
summaryBlockedTasks = current.Id;
} else if (current.name.indexOf('Summary: My Late Tasks') > -1) {
summaryLateTasks = current.Id;
} else if (current.name.indexOf('My Incomplete Tasks, By Project') > -1) {
reportMyTasksAllProjects = current.id;
} else if (current.name.indexOf('My Incomplete Tasks, By Priority') > -1) {
reportMyTasksByPriority = current.id;
} else if (current.name.indexOf('My') > -1) {
myTasks = current.Id;
} else if (current.name.indexOf('Overdue') > -1) {
overdueTasks = current.Id;
} else if (current.name.indexOf('Milestones and Open Tasks') > -1) {
projMilestones = current.id;
} else if (current.name.indexOf('Open Tasks') > -1) {
openTaskStatus = current.Id;
} else if (current.name.indexOf('Tasks By Project') > -1) {
overallTaskStatus = current.id;
}
}
settings.Report_Id_Blocked_Tasks_by_Project__c = String.valueOf(blockedTasks).substring(0,15);
settings.Report_Id_My_tasks_by_project__c = String.valueOf(myTasks).substring(0,15);
settings.Report_Id_Overdue_Tasks_by_Project__c = String.valueOf(overdueTasks).substring(0,15);
settings.Report_Id_Task_Status_By_Project__c = String.valueOf(overallTaskStatus).substring(0,15);
settings.Report_Id_Open_Task_Status_by_Project__c = String.valueOf(openTaskStatus).substring(0,15);
settings.Report_Id_Project_Milestone_Open_Task__c = String.valueOf(projMilestones).substring(0,15);
settings.Report_Id_My_Tasks_By_Project_Milesto__c = String.valueOf(reportMyTasksAllProjects).substring(0,15);
settings.Report_Id_My_Tasks_By_Priority_Project__c = String.valueOf(reportMyTasksByPriority).substring(0,15);
settings.Report_Id_My_Blocked_Tasks__c = String.valueOf(summaryBlockedTasks).substring(0,15);
settings.Report_Id_My_Late_Tasks__c = String.valueOf(summaryLateTasks).substring(0,15);
*/
settings.Auto_Follow_Task__c = false;
settings.Auto_Unfollow_Reassignment__c = false;
settings.Auto_Follow_Complete_Task__c = false;
return settings;
}
static testMethod void testGetSettings() {
Milestone1_GettingStartedController cont = new Milestone1_GettingStartedController();
Milestone1_Settings__c settings = cont.getInitialSettingsWithoutSave();
/*
System.assert(settings.Report_Id_Blocked_Tasks_by_Project__c != null);
System.assert(settings.Report_Id_My_tasks_by_project__c != null);
System.assert(settings.Report_Id_Overdue_Tasks_by_Project__c != null);
System.assert(settings.Report_Id_Task_Status_By_Project__c != null);
System.assert(settings.Report_Id_Open_Task_Status_by_Project__c != null);
System.assert(settings.Report_Id_Project_Milestone_Open_Task__c != null);
System.assert(settings.Report_Id_My_Tasks_By_Project_Milesto__c != null);
System.assert(settings.Report_Id_My_Tasks_By_Priority_Project__c != null);
System.assert(settings.Report_Id_My_Blocked_Tasks__c != null);
System.assert(settings.Report_Id_My_Late_Tasks__c != null);
*/
System.assert(!settings.Auto_Follow_Complete_Task__c);
System.assert(!settings.Auto_Follow_Task__c);
System.assert(!settings.Auto_Unfollow_Reassignment__c);
}
static testMethod void testGettingStartedProject() {
Test.startTest();
Milestone1_GettingStartedController cont = new Milestone1_GettingStartedController();
Milestone1_Project__c project = cont.handleGettingStartedProjectInitialization();
System.assert(project != null);
Test.stopTest();
}
}