View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.enrollment.class2.courseofferingset.service.impl;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingInfo;
20  import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
21  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
22  import org.kuali.student.enrollment.courseofferingset.dto.SocInfo;
23  import org.kuali.student.enrollment.courseofferingset.service.CourseOfferingSetService;
24  import org.kuali.student.r2.common.dto.ContextInfo;
25  import org.kuali.student.r2.common.dto.StatusInfo;
26  import org.kuali.student.r2.common.util.constants.CourseOfferingSetServiceConstants;
27  import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
28  import org.kuali.student.r2.core.scheduling.service.SchedulingService;
29  
30  import java.util.Date;
31  import java.util.List;
32  
33  /**
34   * This class handles the asynchronous process of submitting schedule requests from activity offerings
35   * within a SoC to the scheduler
36   *
37   * @author andrewlubbers
38   */
39  public class CourseOfferingSetSchedulingRunner implements Runnable {
40  
41      final static Logger logger = Logger.getLogger(CourseOfferingSetSchedulingRunner.class);
42  
43      private final StringBuffer logBuffer = new StringBuffer();
44  
45      private final String socId;
46  
47      private CourseOfferingSetService socService;
48  
49      private SchedulingService schedulingService;
50  
51      private CourseOfferingService coService;
52  
53      private ContextInfo contextInfo;
54  
55      public CourseOfferingSetSchedulingRunner (String socId) {
56          this.socId = socId;
57      }
58  
59      public CourseOfferingService getCoService() {
60          return coService;
61      }
62  
63      public void setCoService(CourseOfferingService coService) {
64          this.coService = coService;
65      }
66  
67      public SchedulingService getSchedulingService() {
68          return schedulingService;
69      }
70  
71      public void setSchedulingService(SchedulingService schedulingService) {
72          this.schedulingService = schedulingService;
73      }
74  
75      public CourseOfferingSetService getSocService() {
76          return socService;
77      }
78  
79      public void setSocService(CourseOfferingSetService socService) {
80          this.socService = socService;
81      }
82  
83      public ContextInfo getContextInfo() {
84          return contextInfo;
85      }
86  
87      public void setContextInfo(ContextInfo contextInfo) {
88          this.contextInfo = contextInfo;
89      }
90  
91      /**
92       * !!! This method has to state change the SOC back to a sane value for success ("completed") or fail ("notstarted").
93       */
94      @Override
95      public void run() {
96          try {
97              SocInfo soc = socService.getSoc(socId, contextInfo);
98              List<String> coIds = socService.getCourseOfferingIdsBySoc(soc.getId(), contextInfo);
99  
100             log("Submitting ", coIds.size(), " course offerings for scheduling");
101 
102             for (String id : coIds) {
103                 CourseOfferingInfo coInfo = coService.getCourseOffering(id, contextInfo);
104                 log("Processing schedule requests in AOs for CO, id=", coInfo.getId(), " , coCode=",coInfo.getCourseOfferingCode());
105 
106                 List<ActivityOfferingInfo> activityOfferings = coService.getActivityOfferingsByCourseOffering(id, contextInfo);
107 
108                 for (ActivityOfferingInfo aoInfo : activityOfferings) {
109                     log("\tProcessing schedule requests in AO, id=", aoInfo.getId(), " , code=",aoInfo.getActivityCode(), " type=", aoInfo.getTypeKey());
110 
111                     if(aoInfo.getStateKey().equals(LuiServiceConstants.LUI_AO_STATE_APPROVED_KEY)) {
112                         StatusInfo status = coService.scheduleActivityOffering(aoInfo.getId(), contextInfo);
113                         log("\t...scheduleActivityOffering returned with a status of ", status.getIsSuccess(), " , message=", status.getMessage());
114                     }
115                     else {
116                         log("\t...Activity Offering not sent to scheduler, not in a valid state to schedule: ", aoInfo.getStateKey());
117                     }
118                 }
119             }
120 
121             // set the scheduling status of the SoC to completed
122             contextInfo.setCurrentDate(new Date());
123             socService.updateSocState(socId, CourseOfferingSetServiceConstants.SOC_SCHEDULING_STATE_COMPLETED, contextInfo);
124 
125         } catch (Exception e) {
126             // When SoC failed state is created, set SoC scheduling state as failed here
127             logger.fatal(e);
128             throw new RuntimeException(e);
129         }
130     }
131 
132     private void log(Object ... params) {
133         logBuffer.setLength(0);
134         for (Object o : params) {
135             logBuffer.append(o);
136         }
137         logger.info(logBuffer.toString());
138         logBuffer.setLength(0);
139     }
140 
141 }