Advertisement
YuraSidorets

Untitled

Aug 14th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. public class CourseHandler {
  2.     private static Map<Id,Course__c> TriggeredCoursesNew {get;set;}
  3.     private static Map<Id,Course__c> TriggeredCoursesOld {get;set;}
  4.  
  5.     private static List<Course__c>  Old_list {get;set;}
  6.     private static List<Course__c>  New_list {get;set;}
  7.  
  8.     public CourseHandler(Map<Id,Course__c> courcesNew, Map<Id,Course__c> courcesOld, List<SObject> old_l, List<SObject> new_l ){
  9.         TriggeredCoursesNew = courcesNew;
  10.         TriggeredCoursesOld = courcesOld;
  11.         Old_list = old_l;
  12.         New_list = new_l;
  13.     }
  14.  
  15.    
  16.     public void beforeInsert(){
  17.         List<Lesson__c> coursesLessons = new List<Lesson__c>();
  18.         for(Course__c course :New_list){
  19.             if(!(course.Final_Date__c >= Date.today())){
  20.                 course.Final_Date__c = Date.today().addMonths(1);
  21.             }
  22.         }  
  23.     }  
  24.  
  25.  
  26.     public void afterInsert(){
  27.         List<Lesson__c> coursesLessons = new List<Lesson__c>();
  28.         for(Course__c course : TriggeredCoursesNew.values()){
  29.             Integer numDOfays = (course.Final_Date__c.daysBetween(Date.today())/5);
  30.             Integer firstlessonAddition = Math.mod(numDOfays,5);
  31.             Date lastLessonDate = course.Final_Date__c;
  32.  
  33.             for(integer i = 0; i<5; i++){
  34.                 if(i == 5){
  35.                     coursesLessons.add(new Lesson__c(Name = course.Name+' Lesson '+ (5 - i), Course__c = course.Id, Date__c = lastLessonDate.addDays(firstlessonAddition)));
  36.                 }
  37.                 else{
  38.                 coursesLessons.add(new Lesson__c(Name = course.Name+' Lesson '+ (5 - i), Course__c = course.Id, Date__c = lastLessonDate));
  39.                 lastLessonDate = lastLessonDate.addDays(numDOfays);
  40.                 }
  41.             }
  42.            
  43.         }
  44.         insert coursesLessons;
  45.     }
  46.  
  47.     public void afterUpdate(){
  48.      if(CheckRecursive.run()){
  49.         List<Lesson__c> lessonsToRecalc = new List<Lesson__c>();
  50.         Map<Id, List<Lesson__c>> coursesToLessons = new Map<Id, List<Lesson__c>>();
  51.  
  52.         for(Lesson__c lesson : [SELECT Id, Date__c, Course__c  FROM Lesson__c WHERE Course__c IN :TriggeredCoursesOld.keySet()]){
  53.             if(!coursesToLessons.containsKey(lesson.Course__c)){
  54.                 coursesToLessons.put(lesson.Course__c, new List<Lesson__c>{lesson});
  55.             }
  56.             else{
  57.                 coursesToLessons.get(lesson.Course__c).add(lesson);
  58.             }
  59.         }
  60.         for(Course__c course :TriggeredCoursesNew.values()){
  61.             if(TriggeredCoursesOld.get(course.Id).Final_Date__c <= course.Final_Date__c){
  62.                
  63.                 Integer numDOfays = (course.Final_Date__c.daysBetween(Date.today())/coursesToLessons.get(course.Id).size());
  64.                 Integer firstlessonAddition = Math.mod(numDOfays,coursesToLessons.get(course.Id).size());
  65.                 Date lastLessonDate = course.Final_Date__c;
  66.                 integer i = 0;
  67.                 for(Lesson__c lesson :coursesToLessons.get(course.Id)){
  68.                     if(i == 5){
  69.                     lesson.Date__c = lastLessonDate.addDays(firstlessonAddition);
  70.                     }
  71.                     else{
  72.                         lesson.Date__c = lastLessonDate;
  73.                         lastLessonDate = lastLessonDate.addDays(numDOfays);
  74.                         }
  75.                     i++;
  76.                     lessonsToRecalc.add(lesson);
  77.                 }
  78.                
  79.             }
  80.             else {
  81.                 throw new DateException('Set date not erlier than privious!');
  82.             }
  83.         }
  84.         update lessonsToRecalc;
  85.         }
  86.     }
  87.     public class DateException extends Exception {}
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement