Skip to content

Commit

Permalink
code cleanup and commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ReidCarlberg committed Apr 30, 2012
1 parent 7c2d08a commit fff3433
Show file tree
Hide file tree
Showing 36 changed files with 3,929 additions and 1,137 deletions.
4 changes: 2 additions & 2 deletions .settings/com.salesforce.ide.core.prefs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Mon Mar 05 20:18:23 CST 2012
#Mon Apr 30 11:09:07 CDT 2012
eclipse.preferences.version=1
endpointApiVersion=23.0
endpointEnvironment=Production/Developer Edition
Expand All @@ -10,4 +10,4 @@ metadataFormatVersion=23.0
namespacePrefix=
packageName=unpackaged
readTimeout=400
username=labs@labs.230
username=labs@labs.261
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<targetField>Project_Snapshot__c.Date__c</targetField>
</mappings>
<name>Project Status Analytic Snapshot</name>
<runningUser>labs@labs.230</runningUser>
<runningUser>labs@labs.261</runningUser>
<sourceReport>Milestone_Project_Management/Project_Snapshot</sourceReport>
<targetObject>Project_Snapshot__c</targetObject>
</AnalyticSnapshot>
103 changes: 61 additions & 42 deletions src/classes/Milestone1_Clone_Milestone_Controller.cls
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
/*
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:
Expand Down Expand Up @@ -32,9 +32,12 @@ public with sharing class Milestone1_Clone_Milestone_Controller {
public Milestone1_Milestone__c dummyMS {get; set;}
private List<Milestone1_Milestone__c> milestones {get; set;}
private List<Milestone1_Task__c> tasks {get; set;}
private List<User> statusList{get; set;}
public boolean isMilestoneOwnerActive {get;set;}
public boolean isInactiveUserToBeReplaced{get;set;}

public Milestone1_Clone_Milestone_Controller(ApexPages.StandardController stc){
//query main milestone record
// query main milestone record
rec = [SELECT Id,
Name,
Project__c,
Expand All @@ -50,7 +53,7 @@ public with sharing class Milestone1_Clone_Milestone_Controller {
WHERE Id = :stc.getId()
];

//query sub milestone records
// query sub milestone records
milestones = [SELECT Id,
Name,
Parent_Milestone__c,
Expand All @@ -64,7 +67,7 @@ public with sharing class Milestone1_Clone_Milestone_Controller {
WHERE Parent_Milestone__c = :rec.Id
];

//query task records
// query task records
tasks = [SELECT Id,
Name,
Project_Milestone__c,
Expand All @@ -74,55 +77,71 @@ public with sharing class Milestone1_Clone_Milestone_Controller {
Start_Date__c,
Due_Date__c,
Priority__c,
Task_Stage__c,
Class__c,
Blocked__c,
Blocked_Reason__c,
Last_Email_Received__c,
Estimated_Hours__c,
Estimated_Expense__c
Task_Stage__c,
Class__c,
Blocked__c,
Blocked_Reason__c,
Last_Email_Received__c,
Estimated_Hours__c,
Estimated_Expense__c
FROM Milestone1_Task__c
WHERE Project_Milestone__c = :rec.Id
OR Project_Milestone__r.Parent_Milestone__c = :rec.Id
];

dummyMS = new Milestone1_Milestone__c();
dummyMS.Name = 'Copy of ' + rec.Name;

statusList = [SELECT IsActive from User where id = :rec.OwnerId];
if(statusList != null && statusList.size() > 0 )
isMilestoneOwnerActive = statusList[0].IsActive;
}

public PageReference createClone(){

PageReference pageReference = null;
try{
//clone and insert main milestone record
List<Milestone1_Milestone__c> mainMilestoneList = new List<Milestone1_Milestone__c>();
rec.Name = dummyMS.Name;
mainMilestoneList.add(rec);
Map<String, Milestone1_Milestone__c> newTopMilestoneMap = Milestone1_Clone_Utility.cloneMilestonesIntoMap(mainMilestoneList);
insert newTopMilestoneMap.values();

//clone and insert sub milestone records
Map<String, Milestone1_Milestone__c> newBottomMilestoneMap = Milestone1_Clone_Utility.cloneMilestonesIntoMap(milestones);
for(Milestone1_Milestone__c newMS : newBottomMilestoneMap.values()){
newMS.Parent_Milestone__c = newTopMilestoneMap.get(newMS.Parent_Milestone__c).Id;
}
insert newBottomMilestoneMap.values();

//collect all milestones into one map
Map<String, Milestone1_Milestone__c> allNewMilestoneMap = new Map<String, Milestone1_Milestone__c>();
allNewMilestoneMap.putAll(newTopMilestoneMap);
allNewMilestoneMap.putAll(newBottomMilestoneMap);

//clone and insert task records
List<Milestone1_Task__c> newTasks = Milestone1_Clone_Utility.cloneTasksIntoList(tasks);
for(Milestone1_Task__c newTask : newTasks){
newTask.Project_Milestone__c = allNewMilestoneMap.get(newTask.Project_Milestone__c).Id;
}
insert newTasks;
pageReference = new ApexPages.StandardController(newTopMilestoneMap.values()[0]).view();
PageReference pageReference = null;
try{
if(isMilestoneOwnerActive == false){
if(dummyMS.OwnerId == null){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,system.label.Milestone1_InactiveUsersError);
ApexPages.addMessage(myMsg);
isInactiveUserToBeReplaced = true;
return pageReference;
}if (dummyMS.OwnerId != null){
rec.ownerId = dummyMS.OwnerId;
}
}

//clone and insert main milestone record
List<Milestone1_Milestone__c> mainMilestoneList = new List<Milestone1_Milestone__c>();
rec.Name = dummyMS.Name;
mainMilestoneList.add(rec);
Map<String, Milestone1_Milestone__c> newTopMilestoneMap = Milestone1_Clone_Utility.cloneMilestonesIntoMap(mainMilestoneList);
insert newTopMilestoneMap.values();

//clone and insert sub milestone records
Map<String, Milestone1_Milestone__c> newBottomMilestoneMap = Milestone1_Clone_Utility.cloneMilestonesIntoMap(milestones);
for(Milestone1_Milestone__c newMS : newBottomMilestoneMap.values()){
newMS.Parent_Milestone__c = newTopMilestoneMap.get(newMS.Parent_Milestone__c).Id;
}
insert newBottomMilestoneMap.values();

//collect all milestones into one map
Map<String, Milestone1_Milestone__c> allNewMilestoneMap = new Map<String, Milestone1_Milestone__c>();
allNewMilestoneMap.putAll(newTopMilestoneMap);
allNewMilestoneMap.putAll(newBottomMilestoneMap);

//clone and insert task records
List<Milestone1_Task__c> newTasks = Milestone1_Clone_Utility.cloneTasksIntoList(tasks);
for(Milestone1_Task__c newTask : newTasks){
newTask.Project_Milestone__c = allNewMilestoneMap.get(newTask.Project_Milestone__c).Id;
}
insert newTasks;
pageReference = new ApexPages.StandardController(newTopMilestoneMap.values()[0]).view();

}catch(Exception ex){
Apexpages.addMessages(ex);
}
Apexpages.addMessages(ex);
}
//direct user to cloned milestone's detail page
return pageReference;
}
Expand Down
146 changes: 83 additions & 63 deletions src/classes/Milestone1_Clone_Project_Controller.cls
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
/*
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.
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.
Expand All @@ -32,7 +32,10 @@ public with sharing class Milestone1_Clone_Project_Controller {
public Milestone1_Project__c dummyProj {get; set;}
private List<Milestone1_Milestone__c> milestones {get; set;}
private List<Milestone1_Task__c> tasks {get; set;}

private List<User> statusList{get; set;}
public Boolean isProjOwnerActive{get;set;}
public boolean isInactiveUserToBeReplaced{get;set;}

public Milestone1_Clone_Project_Controller(ApexPages.StandardController stc){
//query project record
rec = [SELECT Id,
Expand All @@ -42,7 +45,8 @@ public with sharing class Milestone1_Clone_Project_Controller {
Status__c,
Total_Expense_Budget__c,
Total_Hours_Budget__c,
KickOff__c
KickOff__c,
OwnerId
FROM Milestone1_Project__c
WHERE Id = :stc.getId()
];
Expand All @@ -62,7 +66,7 @@ public with sharing class Milestone1_Clone_Project_Controller {
WHERE Project__c = :rec.Id
];

//query task records
//query task records
tasks = [SELECT Id,
Name,
Project_Milestone__c,
Expand All @@ -72,74 +76,90 @@ public with sharing class Milestone1_Clone_Project_Controller {
Start_Date__c,
Due_Date__c,
Priority__c,
Task_Stage__c,
Class__c,
Blocked__c,
Blocked_Reason__c,
Last_Email_Received__c,
Estimated_Hours__c,
Estimated_Expense__c
Task_Stage__c,
Class__c,
Blocked__c,
Blocked_Reason__c,
Last_Email_Received__c,
Estimated_Hours__c,
Estimated_Expense__c
FROM Milestone1_Task__c
WHERE Project_Milestone__r.Project__c = :rec.Id
];

dummyProj = new Milestone1_Project__c();
dummyProj.Name = 'Copy of ' + rec.Name;
dummyProj.Name = 'Copy of ' + rec.Name;
statusList = [SELECT IsActive from User where id = :rec.OwnerId];
if(statusList != null && statusList.size() > 0 )
isProjOwnerActive = statusList[0].IsActive;
}

public PageReference createClone(){
public PageReference createClone(){
Savepoint preSave = Database.setSavepoint(); //set savepoint so we can rollback the whole save if there are errors
PageReference pageReference = null;
try{
//create new project record
rec.Name = dummyProj.Name;
Milestone1_Project__c newProj = Milestone1_Clone_Utility.cloneProject(rec);
insert newProj;

//separate milestone records into parents and subs
List<Milestone1_Milestone__c> topMilestones = new List<Milestone1_Milestone__c>();
List<Milestone1_Milestone__c> bottomMilestones = new List<Milestone1_Milestone__c>();
for(Milestone1_Milestone__c oldMS : milestones){
if(oldMS.Parent_Milestone__c == null){
topMilestones.add(oldMS);
} else {
bottomMilestones.add(oldMS);
}
}

//clone and insert top milestone records
Map<String, Milestone1_Milestone__c> newTopMilestoneMap = Milestone1_Clone_Utility.cloneMilestonesIntoMap(topMilestones);
for(Milestone1_Milestone__c newMS : newTopMilestoneMap.values()){
newMS.Project__c = newProj.Id;
}
insert newTopMilestoneMap.values();

//clone and insert sub milestone records
Map<String, Milestone1_Milestone__c> newBottomMilestoneMap = Milestone1_Clone_Utility.cloneMilestonesIntoMap(bottomMilestones);
for(Milestone1_Milestone__c newMS : newBottomMilestoneMap.values()){
newMS.Project__c = newProj.Id;
newMS.Parent_Milestone__c = newTopMilestoneMap.get(newMS.Parent_Milestone__c).Id;
}
insert newBottomMilestoneMap.values();

//collect all milestones into one map
Map<String, Milestone1_Milestone__c> allNewMilestoneMap = new Map<String, Milestone1_Milestone__c>();
allNewMilestoneMap.putAll(newTopMilestoneMap);
allNewMilestoneMap.putAll(newBottomMilestoneMap);

//clone and insert task records
List<Milestone1_Task__c> newTasks = Milestone1_Clone_Utility.cloneTasksIntoList(tasks);
for(Milestone1_Task__c newTask : newTasks){
newTask.Project_Milestone__c = allNewMilestoneMap.get(newTask.Project_Milestone__c).Id;
}
insert newTasks;

//direct user to cloned project's detail page
return new ApexPages.StandardController(newProj).view();
}catch(System.DMLException e){
ApexPages.addMessages(e); //show save error(s) on the visualforce page
Database.rollback(preSave); //rollback any completed saves if the transaction has errors

if(isProjOwnerActive == false){
if(dummyProj.OwnerId == null){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,system.label.Milestone1_InactiveUsersError);
ApexPages.addMessage(myMsg);
isInactiveUserToBeReplaced = true;
return pageReference;
}if(dummyProj.OwnerId != null){
rec.ownerId = dummyProj.OwnerId;
}
}

//create new project record
rec.Name = dummyProj.Name;

Milestone1_Project__c newProj = Milestone1_Clone_Utility.cloneProject(rec);
insert newProj;
//separate milestone records into parents and subs
List<Milestone1_Milestone__c> topMilestones = new List<Milestone1_Milestone__c>();
List<Milestone1_Milestone__c> bottomMilestones = new List<Milestone1_Milestone__c>();
for(Milestone1_Milestone__c oldMS : milestones){
if(oldMS.Parent_Milestone__c == null){
topMilestones.add(oldMS);
} else {
bottomMilestones.add(oldMS);
}
}
//clone and insert top milestone records
Map<String, Milestone1_Milestone__c> newTopMilestoneMap = Milestone1_Clone_Utility.cloneMilestonesIntoMap(topMilestones);
for(Milestone1_Milestone__c newMS : newTopMilestoneMap.values()){
newMS.Project__c = newProj.Id;
}
insert newTopMilestoneMap.values();

//clone and insert sub milestone records
Map<String, Milestone1_Milestone__c> newBottomMilestoneMap = Milestone1_Clone_Utility.cloneMilestonesIntoMap(bottomMilestones);
for(Milestone1_Milestone__c newMS : newBottomMilestoneMap.values()){
newMS.Project__c = newProj.Id;
newMS.Parent_Milestone__c = newTopMilestoneMap.get(newMS.Parent_Milestone__c).Id;
}
insert newBottomMilestoneMap.values();

//collect all milestones into one map
Map<String, Milestone1_Milestone__c> allNewMilestoneMap = new Map<String, Milestone1_Milestone__c>();
allNewMilestoneMap.putAll(newTopMilestoneMap);
allNewMilestoneMap.putAll(newBottomMilestoneMap);

//clone and insert task records
List<Milestone1_Task__c> newTasks = Milestone1_Clone_Utility.cloneTasksIntoList(tasks);
for(Milestone1_Task__c newTask : newTasks){
newTask.Project_Milestone__c = allNewMilestoneMap.get(newTask.Project_Milestone__c).Id;
}
insert newTasks;

//direct user to cloned project's detail page
return new ApexPages.StandardController(newProj).view();

}catch(Exception e){
ApexPages.addMessages(e); //show save error(s) on the visualforce page
Database.rollback(preSave); //rollback any completed saves if the transaction has errors
}
return null;
return pageReference;
}

}
Loading

0 comments on commit fff3433

Please sign in to comment.