Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CourseHandler {
- private static Map<Id,Course__c> TriggeredCoursesNew {get;set;}
- private static Map<Id,Course__c> TriggeredCoursesOld {get;set;}
- private static List<Course__c> Old_list {get;set;}
- private static List<Course__c> New_list {get;set;}
- public CourseHandler(Map<Id,Course__c> courcesNew, Map<Id,Course__c> courcesOld, List<SObject> old_l, List<SObject> new_l ){
- TriggeredCoursesNew = courcesNew;
- TriggeredCoursesOld = courcesOld;
- Old_list = old_l;
- New_list = new_l;
- }
- public void beforeInsert(){
- List<Lesson__c> coursesLessons = new List<Lesson__c>();
- for(Course__c course :New_list){
- if(!(course.Final_Date__c >= Date.today())){
- course.Final_Date__c = Date.today().addMonths(1);
- }
- }
- }
- public void afterInsert(){
- List<Lesson__c> coursesLessons = new List<Lesson__c>();
- for(Course__c course : TriggeredCoursesNew.values()){
- Integer numDOfays = (course.Final_Date__c.daysBetween(Date.today())/5);
- Integer firstlessonAddition = Math.mod(numDOfays,5);
- Date lastLessonDate = course.Final_Date__c;
- for(integer i = 0; i<5; i++){
- if(i == 5){
- coursesLessons.add(new Lesson__c(Name = course.Name+' Lesson '+ (5 - i), Course__c = course.Id, Date__c = lastLessonDate.addDays(firstlessonAddition)));
- }
- else{
- coursesLessons.add(new Lesson__c(Name = course.Name+' Lesson '+ (5 - i), Course__c = course.Id, Date__c = lastLessonDate));
- lastLessonDate = lastLessonDate.addDays(numDOfays);
- }
- }
- }
- insert coursesLessons;
- }
- public void afterUpdate(){
- if(CheckRecursive.run()){
- List<Lesson__c> lessonsToRecalc = new List<Lesson__c>();
- Map<Id, List<Lesson__c>> coursesToLessons = new Map<Id, List<Lesson__c>>();
- for(Lesson__c lesson : [SELECT Id, Date__c, Course__c FROM Lesson__c WHERE Course__c IN :TriggeredCoursesOld.keySet()]){
- if(!coursesToLessons.containsKey(lesson.Course__c)){
- coursesToLessons.put(lesson.Course__c, new List<Lesson__c>{lesson});
- }
- else{
- coursesToLessons.get(lesson.Course__c).add(lesson);
- }
- }
- for(Course__c course :TriggeredCoursesNew.values()){
- if(TriggeredCoursesOld.get(course.Id).Final_Date__c <= course.Final_Date__c){
- Integer numDOfays = (course.Final_Date__c.daysBetween(Date.today())/coursesToLessons.get(course.Id).size());
- Integer firstlessonAddition = Math.mod(numDOfays,coursesToLessons.get(course.Id).size());
- Date lastLessonDate = course.Final_Date__c;
- integer i = 0;
- for(Lesson__c lesson :coursesToLessons.get(course.Id)){
- if(i == 5){
- lesson.Date__c = lastLessonDate.addDays(firstlessonAddition);
- }
- else{
- lesson.Date__c = lastLessonDate;
- lastLessonDate = lastLessonDate.addDays(numDOfays);
- }
- i++;
- lessonsToRecalc.add(lesson);
- }
- }
- else {
- throw new DateException('Set date not erlier than privious!');
- }
- }
- update lessonsToRecalc;
- }
- }
- public class DateException extends Exception {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement