View Javadoc

1   package org.kuali.student.enrollment.class2.courseoffering.util;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.rice.core.api.criteria.Predicate;
5   import org.kuali.rice.core.api.criteria.PredicateFactory;
6   import org.kuali.rice.core.api.criteria.QueryByCriteria;
7   import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
8   import org.kuali.rice.kim.api.identity.Person;
9   import org.kuali.rice.krad.uif.UifParameters;
10  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
11  import org.kuali.rice.krad.util.GlobalVariables;
12  import org.kuali.rice.krad.util.KRADConstants;
13  import org.kuali.student.common.uif.form.KSUifForm;
14  import org.kuali.student.enrollment.class2.courseoffering.dto.ActivityOfferingClusterWrapper;
15  import org.kuali.student.enrollment.class2.courseoffering.dto.ActivityOfferingWrapper;
16  import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingEditWrapper;
17  import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingListSectionWrapper;
18  import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingWrapper;
19  import org.kuali.student.enrollment.class2.courseoffering.dto.RegistrationGroupWrapper;
20  import org.kuali.student.enrollment.class2.courseoffering.form.CourseOfferingManagementForm;
21  import org.kuali.student.enrollment.class2.courseoffering.service.CourseOfferingManagementViewHelperService;
22  import org.kuali.student.enrollment.class2.courseoffering.service.facade.CourseOfferingServiceFacade;
23  import org.kuali.student.enrollment.class2.courseoffering.service.facade.CSRServiceFacade;
24  import org.kuali.student.enrollment.class2.courseoffering.service.impl.CourseInfoByTermLookupableImpl;
25  import org.kuali.student.enrollment.class2.courseoffering.service.impl.DefaultOptionKeysService;
26  import org.kuali.student.enrollment.class2.courseoffering.service.impl.DefaultOptionKeysServiceImpl;
27  import org.kuali.student.enrollment.class2.coursewaitlist.service.facade.CourseWaitListServiceFacade;
28  import org.kuali.student.enrollment.class2.coursewaitlist.service.facade.CourseWaitListServiceFacadeConstants;
29  import org.kuali.student.enrollment.class2.examoffering.service.facade.ExamOfferingServiceFacade;
30  import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingClusterInfo;
31  import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingSetInfo;
32  import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
33  import org.kuali.student.enrollment.courseoffering.dto.FormatOfferingInfo;
34  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
35  import org.kuali.student.enrollment.courseofferingset.dto.SocInfo;
36  import org.kuali.student.enrollment.courseofferingset.service.CourseOfferingSetService;
37  import org.kuali.student.enrollment.coursewaitlist.service.CourseWaitListService;
38  import org.kuali.student.enrollment.examoffering.service.ExamOfferingService;
39  import org.kuali.student.r2.common.constants.CommonServiceConstants;
40  import org.kuali.student.r2.common.dto.AttributeInfo;
41  import org.kuali.student.r2.common.dto.ContextInfo;
42  import org.kuali.student.r2.common.dto.DtoConstants;
43  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
44  import org.kuali.student.r2.common.util.ContextUtils;
45  import org.kuali.student.r2.common.util.constants.CourseOfferingServiceConstants;
46  import org.kuali.student.r2.common.util.constants.CourseOfferingSetServiceConstants;
47  import org.kuali.student.r2.common.util.constants.ExamOfferingServiceConstants;
48  import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
49  import org.kuali.student.r2.common.util.date.DateFormatters;
50  import org.kuali.student.r2.core.acal.dto.TermInfo;
51  import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
52  import org.kuali.student.r2.core.atp.dto.AtpInfo;
53  import org.kuali.student.r2.core.atp.service.AtpService;
54  import org.kuali.student.r2.core.class1.state.service.StateService;
55  import org.kuali.student.r2.core.class1.type.service.TypeService;
56  import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
57  import org.kuali.student.r2.core.constants.PopulationServiceConstants;
58  import org.kuali.student.r2.core.organization.dto.OrgInfo;
59  import org.kuali.student.r2.core.organization.service.OrganizationService;
60  import org.kuali.student.r2.core.population.service.PopulationService;
61  import org.kuali.student.r2.core.scheduling.service.SchedulingService;
62  import org.kuali.student.r2.core.search.dto.SearchParamInfo;
63  import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
64  import org.kuali.student.r2.core.search.dto.SearchResultCellInfo;
65  import org.kuali.student.r2.core.search.dto.SearchResultInfo;
66  import org.kuali.student.r2.core.search.dto.SearchResultRowInfo;
67  import org.kuali.student.r2.core.search.service.SearchService;
68  import org.kuali.student.r2.lum.clu.service.CluService;
69  import org.kuali.student.r2.lum.course.dto.CourseInfo;
70  import org.kuali.student.r2.lum.course.service.CourseService;
71  import org.kuali.student.r2.lum.lrc.dto.ResultValuesGroupInfo;
72  import org.kuali.student.r2.lum.lrc.service.LRCService;
73  import org.kuali.student.r2.lum.util.constants.LrcServiceConstants;
74  
75  import javax.xml.namespace.QName;
76  import java.util.ArrayList;
77  import java.util.Arrays;
78  import java.util.Collection;
79  import java.util.Formatter;
80  import java.util.List;
81  import java.util.Locale;
82  import java.util.Properties;
83  
84  import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
85  
86  /**
87   * Created with IntelliJ IDEA.
88   * User: aliabad4
89   * Date: 3/4/13
90   * Time: 3:27 PM
91   * Utility Class for common auto generated reg group functions
92   */
93  public class CourseOfferingManagementUtil {
94      private static CourseOfferingManagementViewHelperService viewHelperService;
95      private static OrganizationService organizationService;
96      private static StateService stateService;
97      private static LRCService lrcService;
98      private static TypeService typeService;
99      private static AcademicCalendarService academicCalendarService;
100     private static CourseOfferingServiceFacade courseOfferingServiceFacade;
101     private static CSRServiceFacade csrServiceFacade;
102     private static CourseService courseService;
103     private static CourseOfferingSetService socService;
104     private static AtpService atpService;
105     private static CluService cluService;
106     private static CourseOfferingSetService courseOfferingSetService;
107     private static CourseOfferingService courseOfferingService;
108     private static SearchService searchService;
109     private static CourseWaitListServiceFacade courseWaitListServiceFacade;
110     private static ExamOfferingServiceFacade examOfferingServiceFacade;
111     private static ExamOfferingService examOfferingService;
112     private static SchedulingService schedulingService;
113     private static PopulationService populationService;
114     private static CourseWaitListService courseWaitListService;
115     private static DefaultOptionKeysService defaultOptionKeysService;
116 
117     public static CourseOfferingManagementViewHelperService getViewHelperService(CourseOfferingManagementForm theForm) {
118 
119         if (viewHelperService == null) {
120             if (theForm.getView().getViewHelperServiceClass() != null) {
121                 viewHelperService = (CourseOfferingManagementViewHelperService) theForm.getView().getViewHelperService();
122             } else {
123                 viewHelperService = (CourseOfferingManagementViewHelperService) theForm.getPostedView().getViewHelperService();
124             }
125         }
126 
127         return viewHelperService;
128     }
129 
130     public static OrganizationService getOrganizationService() {
131         if (organizationService == null) {
132             organizationService = (OrganizationService) GlobalResourceLoader.getService(new QName(CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX + "organization", "OrganizationService"));
133 
134         }
135         return organizationService;
136     }
137 
138     public static StateService getStateService() {
139         if (stateService == null) {
140             stateService = CourseOfferingResourceLoader.loadStateService();
141         }
142         return stateService;
143     }
144 
145     public static LRCService getLrcService() {
146         if (lrcService == null) {
147             lrcService = CourseOfferingResourceLoader.loadLrcService();
148         }
149         return lrcService;
150     }
151 
152     public static TypeService getTypeService() {
153         if (typeService == null) {
154             typeService = CourseOfferingResourceLoader.loadTypeService();
155         }
156         return typeService;
157     }
158 
159     public static AcademicCalendarService getAcademicCalendarService() {
160         if (academicCalendarService == null) {
161             academicCalendarService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
162         }
163         return academicCalendarService;
164     }
165 
166     public static CourseOfferingServiceFacade getCourseOfferingServiceFacade() {
167         if (courseOfferingServiceFacade == null) {
168             courseOfferingServiceFacade = (CourseOfferingServiceFacade) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/courseOfferingServiceFacade", "CourseOfferingServiceFacade"));
169         }
170         return courseOfferingServiceFacade;
171     }
172 
173     public static CSRServiceFacade getCsrServiceFacade() {
174         if (csrServiceFacade == null) {
175             csrServiceFacade = (CSRServiceFacade) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/csrServiceFacade", "CSRServiceFacade"));
176         }
177         return csrServiceFacade;
178     }
179 
180     public static CourseWaitListServiceFacade getCourseWaitListServiceFacade() {
181         if (courseWaitListServiceFacade == null) {
182             courseWaitListServiceFacade = (CourseWaitListServiceFacade) GlobalResourceLoader.getService(CourseWaitListServiceFacadeConstants.getQName());
183         }
184         return courseWaitListServiceFacade;
185     }
186 
187     public static ExamOfferingServiceFacade getExamOfferingServiceFacade() {
188         if (examOfferingServiceFacade == null) {
189             examOfferingServiceFacade = (ExamOfferingServiceFacade) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/examOfferingServiceFacade", "examOfferingServiceFacade"));
190         }
191         return examOfferingServiceFacade;
192     }
193 
194     public static ExamOfferingService getExamOfferingService() {
195         if (examOfferingService == null) {
196             examOfferingService = (ExamOfferingService) GlobalResourceLoader.getService(new QName(ExamOfferingServiceConstants.NAMESPACE, ExamOfferingServiceConstants.SERVICE_NAME_LOCAL_PART));
197         }
198         return examOfferingService;
199     }
200 
201 
202     public static CourseService getCourseService() {
203         if(courseService == null) {
204             courseService = CourseOfferingResourceLoader.loadCourseService();
205         }
206         return courseService;
207     }
208 
209     private static CourseOfferingSetService getSocService() {
210         if (socService == null) {
211             socService = CourseOfferingResourceLoader.loadSocService();
212         }
213         return socService;
214     }
215 
216     private static AtpService getAtpService() {
217         if(atpService == null){
218             atpService = CourseOfferingResourceLoader.loadAtpService();
219         }
220         return atpService;
221     }
222 
223     private static CluService getCluService() {
224         if (cluService == null) {
225             cluService = CourseOfferingResourceLoader.loadCluService();
226         }
227         return cluService;
228     }
229 
230     public static CourseOfferingService getCourseOfferingService() {
231         if (courseOfferingService == null) {
232             courseOfferingService = CourseOfferingResourceLoader.loadCourseOfferingService();
233         }
234         return courseOfferingService;
235     }
236 
237     public static CourseOfferingSetService getCourseOfferingSetService(){
238         if (courseOfferingSetService == null){
239             courseOfferingSetService = (CourseOfferingSetService) GlobalResourceLoader.getService(new QName(CourseOfferingSetServiceConstants.NAMESPACE, CourseOfferingSetServiceConstants.SERVICE_NAME_LOCAL_PART));
240         }
241         return courseOfferingSetService;
242     }
243 
244     public static SearchService getSearchService() {
245         if (searchService == null) {
246             searchService = (SearchService) GlobalResourceLoader.getService(new QName(CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX + "search", SearchService.class.getSimpleName()));
247         }
248         return searchService;
249     }
250 
251     public static SchedulingService getSchedulingService() {
252         if(schedulingService == null) {
253             schedulingService = CourseOfferingResourceLoader.loadSchedulingService();
254         }
255         return schedulingService;
256     }
257 
258     public static PopulationService getPopulationService() {
259         if(populationService == null) {
260             populationService = (PopulationService) GlobalResourceLoader.getService(new QName(PopulationServiceConstants.NAMESPACE, PopulationServiceConstants.SERVICE_NAME_LOCAL_PART));
261         }
262         return populationService;
263     }
264 
265     public static CourseWaitListService getCourseWaitListService() {
266         if(courseWaitListService == null) {
267             courseWaitListService = CourseOfferingResourceLoader.loadCourseWaitlistService();
268         }
269         return courseWaitListService;
270     }
271 
272     public static DefaultOptionKeysService getDefaultOptionKeysService() {
273         if (defaultOptionKeysService == null) {
274             defaultOptionKeysService = new DefaultOptionKeysServiceImpl();
275         }
276         return defaultOptionKeysService;
277     }
278 
279     public static boolean checkEditViewAuthz(CourseOfferingManagementForm theForm) {
280         Person user = GlobalVariables.getUserSession().getPerson();
281         return theForm.getView().getAuthorizer().canEditView(theForm.getView(), theForm, user);
282     }
283 
284     public static void prepareManageAOsModelAndView(CourseOfferingManagementForm form, CourseOfferingListSectionWrapper selectedCO) throws Exception {
285 
286         CourseOfferingWrapper currentCOWrapper = new CourseOfferingWrapper(selectedCO.isCrossListed(),selectedCO.getCourseOfferingCode(),selectedCO.getCourseOfferingDesc(),selectedCO.getAlternateCOCodes(),selectedCO.getCourseOfferingId());
287         try{
288             currentCOWrapper.setExamPeriodId(getExamOfferingServiceFacade().getExamPeriodId(form.getTermInfo().getId(), ContextUtils.createDefaultContextInfo()));
289         }catch (DoesNotExistException e){
290 
291         }
292         form.setSubjectCode(selectedCO.getSubjectArea());
293         prepare_AOs_RGs_AOCs_Lists(form, currentCOWrapper);
294     }
295 
296     public static void prepare_AOs_RGs_AOCs_Lists (CourseOfferingManagementForm form, CourseOfferingWrapper currentCOWrapper) throws Exception {
297         currentCOWrapper.setTerm( form.getTermInfo() );
298 
299         CourseOfferingInfo coInfo = getCourseOfferingService().getCourseOffering(currentCOWrapper.getCourseOfferingId(),ContextUtils.createDefaultContextInfo());
300         currentCOWrapper.setCourseOfferingInfo(coInfo);
301 
302         //set the ownerCode if not set
303         if (currentCOWrapper.getOwnerCode()==null ||currentCOWrapper.getOwnerCode().equals("") ) {
304             for (CourseOfferingListSectionWrapper courseOfferingListSectionWrapper : form.getCourseOfferingResultList()) {
305                if (courseOfferingListSectionWrapper.getCourseOfferingCode().equals(currentCOWrapper.getCourseOfferingCode())) {
306                    currentCOWrapper.setOwnerCode(courseOfferingListSectionWrapper.getOwnerCode());
307                    break;
308                }
309             }
310         }
311 
312         ContextInfo contextInfo =  ContextUtils.createDefaultContextInfo();
313         List<String> orgIds = coInfo.getUnitsDeploymentOrgIds();
314         if(orgIds !=null && !orgIds.isEmpty()){
315             OrgInfo org = getOrganizationService().getOrg(orgIds.get(0), contextInfo);
316             currentCOWrapper.setCoOwningDeptName(org.getShortName());
317             // managing multiple orgs
318             StringBuilder sb = new StringBuilder("");
319             for (String orgId : orgIds) {
320                 sb.append(orgId).append(",");
321             }
322             String orgIDs = sb.toString();
323             if (orgIDs.length() > 0) {
324                 form.setAdminOrg(orgIDs.substring(0, orgIDs.length()- 1));
325             }
326         }
327 
328         form.setCurrentCourseOfferingWrapper(currentCOWrapper);
329         form.setInputCode(currentCOWrapper.getCourseOfferingCode());
330 
331         form.setFormatIdForNewAO(null);
332         form.setFormatOfferingIdForNewAO(null);
333         form.setActivityIdForNewAO(null);
334         form.setNoOfActivityOfferings(null);
335         form.setPrivateClusterNamePopover("");
336         form.setPublishedClusterNamePopover("");
337 
338         getViewHelperService(form).loadPreviousAndNextCourseOffering(form);
339 
340         getViewHelperService(form).build_AOs_RGs_AOCs_Lists_For_TheCourseOffering(form);
341 
342         CourseOfferingManagementToolbarUtil.processAoToolbarForUser(form.getActivityWrapperList(), form);
343     }
344 
345     /*
346      *  Determine if any COs were check-boxed.
347      *  @return True if any COs where selected. Otherwise, false.
348      */
349     public static Object getSelectedObject(KSUifForm theForm, String actionLink) {
350         String selectedCollectionPath = theForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
351         if (StringUtils.isBlank(selectedCollectionPath)) {
352             throw new RuntimeException("Selected collection was not set for " + actionLink);
353         }
354 
355         int selectedLineIndex = -1;
356         String selectedLine = theForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
357         if (StringUtils.isNotBlank(selectedLine)) {
358             selectedLineIndex = Integer.parseInt(selectedLine);
359         }
360 
361         if (selectedLineIndex == -1) {
362             throw new RuntimeException("Selected line index was not set");
363         }
364 
365         Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(theForm, selectedCollectionPath);
366         Object selectedObject;
367         selectedObject = ((List<Object>) collection).get(selectedLineIndex);
368 
369         return selectedObject;
370     }
371 
372     public static void reloadCourseOfferings(CourseOfferingManagementForm theForm) throws Exception {
373         getViewHelperService(theForm).loadCourseOfferingsByTermAndCourseCode(theForm.getTermInfo().getId(), theForm.getInputCode(), theForm);
374         CourseOfferingManagementToolbarUtil.processCoToolbarForUser(theForm.getCourseOfferingResultList(), theForm);
375     }
376 
377     public static void reloadTheCourseOfferingWithAOs_RGs_Clusters(CourseOfferingManagementForm theForm) throws Exception {
378         // Reload the AOs
379         CourseOfferingInfo theCourseOffering = theForm.getCurrentCourseOfferingWrapper().getCourseOfferingInfo();
380 
381         CourseOfferingWrapper coWrapper = new CourseOfferingWrapper(theCourseOffering);
382         getViewHelperService(theForm).build_AOs_RGs_AOCs_Lists_For_TheCourseOffering(theForm);
383 
384         getViewHelperService(theForm).loadPreviousAndNextCourseOffering(theForm);
385 
386         CourseOfferingManagementToolbarUtil.processAoToolbarForUser(theForm.getActivityWrapperList(), theForm);
387     }
388 
389     public static String getGradingOption(String gradingOptionId) throws Exception {
390         String gradingOption = "";
391         if (StringUtils.isNotBlank(gradingOptionId)) {
392             ResultValuesGroupInfo rvg = getLrcService().getResultValuesGroup(gradingOptionId, ContextUtils.createDefaultContextInfo());
393             if (rvg != null && StringUtils.isNotBlank(rvg.getName())) {
394                 gradingOption = rvg.getName();
395             }
396         }
397 
398         return gradingOption;
399     }
400 
401     public static String getOrgNameDescription(String orgShortName) {
402         String shortName = "shortName";
403         String longName = "";
404 
405         QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
406         if (StringUtils.isNotBlank(orgShortName) && !orgShortName.isEmpty()) {
407             qBuilder.setPredicates(PredicateFactory.or(
408                     PredicateFactory.equal(shortName, orgShortName)));
409         } else {
410             throw new RuntimeException("Org short name is null!");
411         }
412         try {
413             QueryByCriteria query = qBuilder.build();
414 
415             OrganizationService organizationService = getOrganizationService();
416 
417             java.util.List<OrgInfo> orgInfos = organizationService.searchForOrgs(query, ContextUtils.createDefaultContextInfo());
418             if (!orgInfos.isEmpty()) {
419                 longName = orgInfos.get(0).getLongName();
420             }
421         } catch (Exception e) {
422             throw new RuntimeException("Error! No long name description found.", e);
423         }
424         return longName;
425     }
426 
427     public static Properties _buildCOURLParameters(CourseOfferingInfo courseOfferingInfo, String methodToCall) {
428         Properties props = new Properties();
429         props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
430         props.put("courseOfferingInfo.id", courseOfferingInfo.getId());
431         props.put(KRADConstants.DATA_OBJECT_CLASS_ATTRIBUTE, CourseOfferingEditWrapper.class.getName());
432 
433         // UrlParams.SHOW_HISTORY and SHOW_HOME no longer exist
434         // https://fisheye.kuali.org/changelog/rice?cs=39034
435         // TODO KSENROLL-8469
436         //props.put(UifConstants.UrlParams.SHOW_HOME, BooleanUtils.toStringTrueFalse(false));
437         return props;
438     }
439 
440     public static boolean _isClusterUniqueWithinCO(CourseOfferingManagementForm form, String courseOfferingId, String privateName) throws Exception{
441         List<String> foIds = new ArrayList<String>();
442         //fetch all the formatOfferingIds associated with the given courseOfferingId
443         //For performance, if FOIds are already in the form, use it (most likely it is). Otherwise, fetch FOs by COId
444         if (form.getFoId2aoTypeMap()==null || form.getFoId2aoTypeMap().isEmpty()) {
445             List<FormatOfferingInfo> formatOfferingList = getCourseOfferingService().getFormatOfferingsByCourseOffering(courseOfferingId,ContextUtils.createDefaultContextInfo());
446             for(FormatOfferingInfo foInfo:formatOfferingList){
447                 foIds.add(foInfo.getId());
448             }
449         } else {
450             foIds = new ArrayList<String>(form.getFoId2aoTypeMap().keySet());
451         }
452 
453         //Build up a term search criteria
454         if (!foIds.isEmpty()) {
455             QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
456             qbcBuilder.setPredicates(PredicateFactory.and(
457                     PredicateFactory.in("formatOfferingId", foIds.toArray()),
458                     PredicateFactory.equalIgnoreCase("privateName", privateName)));
459             QueryByCriteria criteria = qbcBuilder.build();
460 
461             List<ActivityOfferingClusterInfo> aoClusterList = getCourseOfferingService().searchForActivityOfferingClusters(criteria, ContextUtils.createDefaultContextInfo());
462             return aoClusterList.size() <= 0;
463         }
464 
465         return true;
466     }
467 
468 
469     public static ActivityOfferingClusterInfo _buildEmptyAOCluster (String formatOfferingId, String privateName, String publishedName){
470         ActivityOfferingClusterInfo emptyCluster = new ActivityOfferingClusterInfo();
471         emptyCluster.setTypeKey(CourseOfferingServiceConstants.AOC_ROOT_TYPE_KEY);
472         emptyCluster.setStateKey(CourseOfferingServiceConstants.AOC_ACTIVE_STATE_KEY);
473         emptyCluster.setPrivateName(privateName);
474         emptyCluster.setName(publishedName);
475         emptyCluster.setFormatOfferingId(formatOfferingId);
476         List<ActivityOfferingSetInfo> activityOfferingSets = new ArrayList<ActivityOfferingSetInfo>();
477         emptyCluster.setActivityOfferingSets(activityOfferingSets);
478         return emptyCluster;
479     }
480 
481     public static boolean _clusterForFormatOfferingValidation (FormatOfferingInfo formatOfferingInfo, ContextInfo context) {
482         try {
483             if (formatOfferingInfo.getActivityOfferingTypeKeys()!=null && formatOfferingInfo.getActivityOfferingTypeKeys().size()>1) {
484                 return true;
485             } else if (formatOfferingInfo.getActivityOfferingTypeKeys()!=null && formatOfferingInfo.getActivityOfferingTypeKeys().size()==1) {
486                 QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
487                 qbcBuilder.setPredicates(PredicateFactory.equal("formatOfferingId", formatOfferingInfo.getId()));
488                 QueryByCriteria criteria = qbcBuilder.build();
489 
490                 List<String> aoClusterIds = getCourseOfferingService().searchForActivityOfferingClusterIds(criteria, context);
491                 if (aoClusterIds!=null && aoClusterIds.size()>=1) {
492                     return false;
493                 } else {
494                     return true;
495                 }
496             } else {
497                 return false;
498             }
499         } catch (Exception e) {
500             throw new RuntimeException(e);
501         }
502 
503     }
504 
505     public static void clearForm (CourseOfferingManagementForm form) throws Exception {
506         form.setAdminOrg(null);
507         form.setCourseOfferingResultList(new ArrayList<CourseOfferingListSectionWrapper>());
508         form.setActivityWrapperList(new ArrayList<ActivityOfferingWrapper>());
509         form.setClusterResultList(new ArrayList<ActivityOfferingClusterWrapper>());
510         form.setRgResultList(new ArrayList<RegistrationGroupWrapper>());
511 
512         form.setSelectedToDeleteList(new ArrayList<ActivityOfferingWrapper>());
513         form.setSelectedCoToDeleteList(new ArrayList<CourseOfferingListSectionWrapper>());
514 
515         form.setCourseOfferingCopyWrapper(null);
516         form.setFormatOfferingIdForNewAO(null);
517 
518         form.setHasMoreThanOneFormat(false);
519         form.setHasMoreThanOneCluster(false);
520         form.setTermInfo(null);
521         form.setSubjectCode(null);
522         form.setSubjectCodeDescription(null);
523 
524         form.setFormatIdForNewAO(null);
525         form.setFormatOfferingIdForNewAO(null);
526         form.setActivityIdForNewAO(null);
527         form.setClusterIdForNewAO(null);
528         form.setNoOfActivityOfferings(null);
529 
530         form.setCurrentCourseOfferingWrapper(null);
531         form.setPreviousCourseOfferingWrapper(null);
532         form.setNextCourseOfferingWrapper(null);
533 
534         form.setSocState(null);
535         form.setSocStateKey(null);
536 
537         form.setSelectedIllegalAOInDeletion(false);
538 
539         form.setEnableAddButton(false);
540         form.setEnableMoveAOButton(false);
541         form.setEnableAddClusterButton(false);
542 
543     }
544 
545     public static String getTermStartEndDate(String termId, TermInfo term) {
546         // Return Term as String display like 'FALL 2020 (9/26/2020-12/26/2020)'
547         StringBuilder stringBuilder = new StringBuilder();
548         Formatter formatter = new Formatter(stringBuilder, Locale.US);
549         String displayString = termId; // use termId as a default.
550         if (term != null) {
551             String startDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getStartDate());
552             String endDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getEndDate());
553             formatter.format("%s - %s", startDate, endDate);
554             displayString = stringBuilder.toString();
555         }
556         return displayString;
557     }
558 
559     public static String getTermDisplayString(String termId, TermInfo term) {
560         // Return Term as String display like 'FALL 2020 (9/26/2020-12/26/2020)'
561         StringBuilder stringBuilder = new StringBuilder();
562         Formatter formatter = new Formatter(stringBuilder, Locale.US);
563         String displayString = termId; // use termId as a default.
564         if (term != null) {
565             String startDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getStartDate());
566             String endDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getEndDate());
567             String termType = term.getName();
568             formatter.format("%s (%s to %s)", termType, startDate, endDate);
569             displayString = stringBuilder.toString();
570         }
571         return displayString;
572     }
573 
574     public static CourseOfferingInfo createCourseOfferingInfo(String termId, CourseInfo courseInfo) throws Exception {
575 
576         int firstGradingOption = 0;
577         CourseOfferingInfo courseOffering = new CourseOfferingInfo();
578 
579         courseOffering.setTermId(termId);
580         courseOffering.setCourseOfferingTitle(courseInfo.getCourseTitle());
581         courseOffering.setCourseId(courseInfo.getId());
582         courseOffering.setCourseCode(courseInfo.getCode());
583         courseOffering.setTypeKey(LuiServiceConstants.COURSE_OFFERING_TYPE_KEY);
584         courseOffering.setStateKey(LuiServiceConstants.LUI_CO_STATE_DRAFT_KEY);
585         //need to setup the default value though ks-enroll-config.xml
586 //        courseOffering.setHasWaitlist(true);
587         courseOffering.setWaitlistTypeKey(null);
588         courseOffering.setWaitlistLevelTypeKey(null);
589         courseOffering.setCourseOfferingCode(courseInfo.getCode());
590 
591         //Copy grading and credit options
592         if(!courseInfo.getCreditOptions().isEmpty()){
593             courseOffering.setCreditOptionId(courseInfo.getCreditOptions().get(firstGradingOption).getKey());
594         }
595         //Remove these two special student registration options and set them on the CO
596         List<String> courseGradingOptions = new ArrayList<String>(courseInfo.getGradingOptions());
597         if(courseGradingOptions.remove(LrcServiceConstants.RESULT_GROUP_KEY_GRADE_PASSFAIL) ){
598             courseOffering.getStudentRegistrationGradingOptions().add(LrcServiceConstants.RESULT_GROUP_KEY_GRADE_PASSFAIL);
599         }
600         if(courseGradingOptions.remove(LrcServiceConstants.RESULT_GROUP_KEY_GRADE_AUDIT) ){
601             courseOffering.getStudentRegistrationGradingOptions().add(LrcServiceConstants.RESULT_GROUP_KEY_GRADE_AUDIT);
602         }
603         //set the first remaining grading option on the CO
604         if(!courseGradingOptions.isEmpty()){
605             courseOffering.setGradingOptionId(courseGradingOptions.get(firstGradingOption));
606         }
607 
608         // make sure we set attribute information from the course
609         if(!courseInfo.getAttributes().isEmpty()){
610             for(AttributeInfo info: courseInfo.getAttributes()){
611                 // Default the CourseOffering Final Exam Type to the Final Exam type in the Course
612                 if(info.getKey().equals("finalExamStatus")){
613                     courseOffering.setFinalExamType(convertCourseFinalExamTypeToCourseOfferingFinalExamType(info.getValue()));
614                 }
615             }
616         }
617 
618         return courseOffering;
619     }
620 
621     private static String convertCourseFinalExamTypeToCourseOfferingFinalExamType(String courseFinalExamType){
622         String sRet = null;
623         if("STD".equals(courseFinalExamType))   {
624             sRet = CourseOfferingConstants.COURSEOFFERING_FINAL_EXAM_TYPE_STANDARD;
625         } else if("ALT".equals(courseFinalExamType)) {
626             sRet = CourseOfferingConstants.COURSEOFFERING_FINAL_EXAM_TYPE_ALTERNATE;
627         } else {
628             sRet = CourseOfferingConstants.COURSEOFFERING_FINAL_EXAM_TYPE_NONE;
629         }
630         return sRet;
631     }
632 
633     public static TermInfo getTerm(String termCode) {
634 
635         int firstTerm = 0;
636 
637         QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
638         List<Predicate> pList = new ArrayList<Predicate>();
639         Predicate p = null;
640 
641         qBuilder.setPredicates();
642         if (StringUtils.isNotBlank(termCode)) {
643             p = equal("atpCode", termCode);
644             pList.add(p);
645         }
646         qBuilder.setPredicates(p);
647 
648         try {
649             List<TermInfo> terms = getAcademicCalendarService().searchForTerms(qBuilder.build(), ContextUtils.createDefaultContextInfo());
650             if (terms.size() > 1) {
651                 return null;
652             } else if (terms.isEmpty()) {
653                 return null;
654             }
655             return terms.get(firstTerm);
656         } catch (Exception e) {
657             throw new RuntimeException(e);
658         }
659     }
660 
661     public static List<CourseInfo> retrieveMatchingCourses(String courseCode, TermInfo term) {
662 
663         int firstTerm = 0;
664         ContextInfo context = ContextUtils.createDefaultContextInfo();
665         CourseInfo returnCourseInfo;
666         String courseId;
667         List<SearchParamInfo> searchParams = new ArrayList<SearchParamInfo>();
668         List<CourseInfo> courseInfoList = new ArrayList<CourseInfo>();
669 
670         SearchParamInfo qpv = new SearchParamInfo();
671         qpv.setKey("lu.queryParam.luOptionalType");
672         qpv.getValues().add("kuali.lu.type.CreditCourse");
673         searchParams.add(qpv);
674 
675         //Include course states of: Active, Superseded and Retired
676         qpv = new SearchParamInfo();
677         qpv.setKey("lu.queryParam.luOptionalState");
678         qpv.setValues(Arrays.asList(
679                 DtoConstants.STATE_ACTIVE,
680                 DtoConstants.STATE_SUPERSEDED,
681                 DtoConstants.STATE_RETIRED));
682         searchParams.add(qpv);
683 
684         /**Note:  TermCode lookup criteria field takes the special bus route:  it is used to retrieve
685          * term begin/end dates to in-turn constrain courses by their effective and
686          * retire dates, respectively.  AND NOTE that termId is not itself used
687          * directly in the course lookup query.
688          */
689         List<AtpInfo> atps;
690         try {
691             atps = getAtpService().getAtpsByCode(term.getCode(), context);
692         } catch (Exception e) {
693             return courseInfoList;
694         }
695         if (atps == null || atps.size() != 1) {
696             return courseInfoList;
697         }
698 
699         qpv = new SearchParamInfo();
700         qpv.setKey(CourseInfoByTermLookupableImpl.QueryParamEnum.TERM_START.getQueryKey());
701         qpv.getValues().add(DateFormatters.QUERY_SERVICE_TIMESTAMP_FORMATTER.format(atps.get(firstTerm).getStartDate()));
702         searchParams.add(qpv);
703 
704         qpv = new SearchParamInfo();
705         qpv.setKey(CourseInfoByTermLookupableImpl.QueryParamEnum.TERM_END.getQueryKey());
706         qpv.getValues().add(DateFormatters.QUERY_SERVICE_TIMESTAMP_FORMATTER.format(atps.get(firstTerm).getEndDate()));
707         searchParams.add(qpv);
708 
709         //set courseCode to query by
710         qpv = new SearchParamInfo();
711         qpv.setKey(CourseInfoByTermLookupableImpl.QueryParamEnum.CODE.getQueryKey());
712         qpv.getValues().add(courseCode);
713         searchParams.add(qpv);
714 
715         SearchRequestInfo searchRequest = new SearchRequestInfo();
716         searchRequest.setParams(searchParams);
717         searchRequest.setSearchKey("lu.search.courseCodes");
718 
719         try {
720             SearchResultInfo searchResult = getCluService().search(searchRequest, ContextUtils.getContextInfo());
721             if (searchResult.getRows().size() > 0) {
722                 for (SearchResultRowInfo row : searchResult.getRows()) {
723                     List<SearchResultCellInfo> srCells = row.getCells();
724                     if (srCells != null && srCells.size() > 0) {
725                         for (SearchResultCellInfo cell : srCells) {
726                             if ("lu.resultColumn.cluId".equals(cell.getKey())) {
727                                 courseId = cell.getValue();
728                                 returnCourseInfo = getCourseService().getCourse(courseId, ContextUtils.getContextInfo());
729                                 courseInfoList.add(returnCourseInfo);
730                             }
731                         }
732                     }
733                 }
734             }
735         } catch (Exception e) {
736             throw new RuntimeException(e);
737         }
738 
739         return courseInfoList;
740     }
741 
742     public static Properties _buildCOURLParameters(String courseId, String termId, String socId, String methodToCall) {
743         Properties props = new Properties();
744         props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
745         props.put(CourseOfferingConstants.COURSE_ID, courseId);
746         props.put(CourseOfferingConstants.TARGET_TERM_ID, termId);
747         props.put(CourseOfferingConstants.SOC_ID, socId);
748         props.put(CourseOfferingConstants.CREATE_COURSEOFFERING, "true");
749         props.put(KRADConstants.DATA_OBJECT_CLASS_ATTRIBUTE, CourseOfferingEditWrapper.class.getName());
750 
751         return props;
752     }
753 
754     public static SocInfo getMainSocForTerm(TermInfo term, ContextInfo contextInfo) throws Exception {
755         List<String> socIds = getSocService().getSocIdsByTerm(term.getId(), contextInfo);
756         List<SocInfo> socInfos = getSocService().getSocsByIds(socIds, contextInfo);
757         for (SocInfo socInfo: socInfos) {
758             if (socInfo.getTypeKey().equals(CourseOfferingSetServiceConstants.MAIN_SOC_TYPE_KEY)) {
759                 return socInfo;
760             }
761         }
762         return null;
763     }
764 
765 }