View Javadoc

1   package org.kuali.student.enrollment.class2.autogen.controller;
2   
3   import org.apache.commons.lang.BooleanUtils;
4   import org.apache.commons.lang.StringUtils;
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.UifConstants;
10  import org.kuali.rice.krad.uif.UifParameters;
11  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
12  import org.kuali.rice.krad.util.GlobalVariables;
13  import org.kuali.rice.krad.util.KRADConstants;
14  import org.kuali.student.common.uif.form.KSUifForm;
15  import org.kuali.student.enrollment.class2.autogen.form.ARGCourseOfferingManagementForm;
16  import org.kuali.student.enrollment.class2.autogen.service.ARGCourseOfferingManagementViewHelperService;
17  import org.kuali.student.enrollment.class2.autogen.util.ARGToolbarUtil;
18  import org.kuali.student.enrollment.class2.courseoffering.dto.ActivityOfferingClusterWrapper;
19  import org.kuali.student.enrollment.class2.courseoffering.dto.ActivityOfferingWrapper;
20  import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingEditWrapper;
21  import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingListSectionWrapper;
22  import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingWrapper;
23  import org.kuali.student.enrollment.class2.courseoffering.dto.RegistrationGroupWrapper;
24  import org.kuali.student.enrollment.class2.courseoffering.service.adapter.AutogenRegGroupServiceAdapter;
25  import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader;
26  import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingClusterInfo;
27  import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingSetInfo;
28  import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
29  import org.kuali.student.enrollment.courseoffering.dto.FormatOfferingInfo;
30  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
31  import org.kuali.student.r2.common.constants.CommonServiceConstants;
32  import org.kuali.student.r2.common.dto.ContextInfo;
33  import org.kuali.student.r2.common.util.ContextUtils;
34  import org.kuali.student.r2.common.util.constants.CourseOfferingServiceConstants;
35  import org.kuali.student.r2.common.util.date.DateFormatters;
36  import org.kuali.student.r2.core.acal.dto.TermInfo;
37  import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
38  import org.kuali.student.r2.core.class1.state.service.StateService;
39  import org.kuali.student.r2.core.class1.type.service.TypeService;
40  import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
41  import org.kuali.student.r2.core.organization.dto.OrgInfo;
42  import org.kuali.student.r2.core.organization.service.OrganizationService;
43  import org.kuali.student.r2.lum.lrc.dto.ResultValuesGroupInfo;
44  import org.kuali.student.r2.lum.lrc.service.LRCService;
45  
46  import javax.xml.namespace.QName;
47  import java.util.*;
48  
49  /**
50   * Created with IntelliJ IDEA.
51   * User: aliabad4
52   * Date: 3/4/13
53   * Time: 3:27 PM
54   * Utility Class for common auto generated reg group functions
55   */
56  public class ARGUtil {
57      private static ARGCourseOfferingManagementViewHelperService viewHelperService;
58      private static OrganizationService organizationService;
59      private static StateService stateService;
60      private static LRCService lrcService;
61      private static TypeService typeService;
62      private static AcademicCalendarService academicCalendarService;
63      private static AutogenRegGroupServiceAdapter argServiceAdapter;
64  
65      public static CourseOfferingService getCourseOfferingService() {
66          return CourseOfferingResourceLoader.loadCourseOfferingService();
67      }
68  
69      public static ARGCourseOfferingManagementViewHelperService getViewHelperService(ARGCourseOfferingManagementForm theForm) {
70  
71          if (viewHelperService == null) {
72              if (theForm.getView().getViewHelperServiceClass() != null) {
73                  viewHelperService = (ARGCourseOfferingManagementViewHelperService) theForm.getView().getViewHelperService();
74              } else {
75                  viewHelperService = (ARGCourseOfferingManagementViewHelperService) theForm.getPostedView().getViewHelperService();
76              }
77          }
78  
79          return viewHelperService;
80      }
81  
82      public static OrganizationService getOrganizationService() {
83          if (organizationService == null) {
84              organizationService = (OrganizationService) GlobalResourceLoader.getService(new QName(CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX + "organization", "OrganizationService"));
85  
86          }
87          return organizationService;
88      }
89  
90      public static StateService getStateService() {
91          if (stateService == null) {
92              stateService = CourseOfferingResourceLoader.loadStateService();
93          }
94          return stateService;
95      }
96  
97      public static LRCService getLrcService() {
98          if (lrcService == null) {
99              lrcService = CourseOfferingResourceLoader.loadLrcService();
100         }
101         return lrcService;
102     }
103 
104     public static TypeService getTypeService() {
105         if (typeService == null) {
106             typeService = CourseOfferingResourceLoader.loadTypeService();
107         }
108         return typeService;
109     }
110 
111     public static AcademicCalendarService getAcademicCalendarService() {
112         if (academicCalendarService == null) {
113             academicCalendarService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
114         }
115         return academicCalendarService;
116     }
117 
118     public static AutogenRegGroupServiceAdapter getArgServiceAdapter() {
119         if (argServiceAdapter == null) {
120             argServiceAdapter = (AutogenRegGroupServiceAdapter) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/autogenRegistrationGroupAppLayer", "AutogenRegGroupServiceAdapter"));
121         }
122         return argServiceAdapter;
123     }
124 
125     public static boolean checkEditViewAuthz(ARGCourseOfferingManagementForm theForm) {
126         Person user = GlobalVariables.getUserSession().getPerson();
127         return theForm.getView().getAuthorizer().canEditView(theForm.getView(), theForm, user);
128     }
129 
130     public static void prepareManageAOsModelAndView(ARGCourseOfferingManagementForm form, CourseOfferingListSectionWrapper selectedCO) throws Exception {
131 
132         CourseOfferingWrapper currentCOWrapper = new CourseOfferingWrapper(selectedCO.isCrossListed(),selectedCO.getCourseOfferingCode(),selectedCO.getCourseOfferingDesc(),selectedCO.getAlternateCOCodes(),selectedCO.getCourseOfferingId());
133         form.setSubjectCode(selectedCO.getSubjectArea());
134         prepare_AOs_RGs_AOCs_Lists(form, currentCOWrapper);
135     }
136 
137     public static void prepare_AOs_RGs_AOCs_Lists (ARGCourseOfferingManagementForm form, CourseOfferingWrapper currentCOWrapper) throws Exception {
138         currentCOWrapper.setTerm( form.getTermInfo() );
139 
140         CourseOfferingInfo coInfo = getCourseOfferingService().getCourseOffering(currentCOWrapper.getCourseOfferingId(),ContextUtils.createDefaultContextInfo());
141         currentCOWrapper.setCourseOfferingInfo(coInfo);
142 
143         //set the ownerCode if not set
144         if (currentCOWrapper.getOwnerCode()==null ||currentCOWrapper.getOwnerCode().equals("") ) {
145             for (CourseOfferingListSectionWrapper courseOfferingListSectionWrapper : form.getCourseOfferingResultList()) {
146                if (courseOfferingListSectionWrapper.getCourseOfferingCode().equals(currentCOWrapper.getCourseOfferingCode())) {
147                    currentCOWrapper.setOwnerCode(courseOfferingListSectionWrapper.getOwnerCode());
148                    break;
149                }
150             }
151         }
152 
153         ContextInfo contextInfo =  ContextUtils.createDefaultContextInfo();
154         List<String> orgIds = coInfo.getUnitsDeploymentOrgIds();
155         if(orgIds !=null && !orgIds.isEmpty()){
156             OrgInfo org = getOrganizationService().getOrg(orgIds.get(0), contextInfo);
157             currentCOWrapper.setCoOwningDeptName(org.getShortName());
158             // managing multiple orgs
159             String orgIDs = "";
160             for (String orgId : orgIds) {
161                 orgIDs = orgIDs + orgId + ",";
162             }
163             if (orgIDs.length() > 0) {
164                 form.setAdminOrg(orgIDs.substring(0, orgIDs.length()- 1));
165             }
166         }
167 
168         form.setCurrentCourseOfferingWrapper(currentCOWrapper);
169         form.setInputCode(currentCOWrapper.getCourseOfferingCode());
170 
171         form.setFormatIdForNewAO(null);
172         form.setFormatOfferingIdForNewAO(null);
173         form.setActivityIdForNewAO(null);
174         form.setNoOfActivityOfferings(null);
175         form.setPrivateClusterNamePopover("");
176         form.setPublishedClusterNamePopover("");
177 
178         getViewHelperService(form).loadPreviousAndNextCourseOffering(form);
179 
180         getViewHelperService(form).build_AOs_RGs_AOCs_Lists_For_TheCourseOffering(form, currentCOWrapper);
181 
182         ARGToolbarUtil.processAoToolbarForUser(form.getActivityWrapperList(), form);
183     }
184 
185     /*
186      *  Determine if any COs were check-boxed.
187      *  @return True if any COs where selected. Otherwise, false.
188      */
189     public static Object getSelectedObject(KSUifForm theForm, String actionLink) {
190         String selectedCollectionPath = theForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
191         if (StringUtils.isBlank(selectedCollectionPath)) {
192             throw new RuntimeException("Selected collection was not set for " + actionLink);
193         }
194 
195         int selectedLineIndex = -1;
196         String selectedLine = theForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
197         if (StringUtils.isNotBlank(selectedLine)) {
198             selectedLineIndex = Integer.parseInt(selectedLine);
199         }
200 
201         if (selectedLineIndex == -1) {
202             throw new RuntimeException("Selected line index was not set");
203         }
204 
205         Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(theForm, selectedCollectionPath);
206         Object selectedObject;
207         selectedObject = ((List<Object>) collection).get(selectedLineIndex);
208 
209         return selectedObject;
210     }
211 
212     public static void reloadCourseOfferings(ARGCourseOfferingManagementForm theForm) throws Exception {
213         getViewHelperService(theForm).loadCourseOfferingsByTermAndCourseCode(theForm.getTermInfo().getId(), theForm.getInputCode(), theForm);
214         ARGToolbarUtil.processCoToolbarForUser(theForm.getCourseOfferingResultList(), theForm);
215     }
216 
217     public static void reloadTheCourseOfferingWithAOs_RGs_Clusters(ARGCourseOfferingManagementForm theForm) throws Exception {
218         // Reload the AOs
219         CourseOfferingInfo theCourseOffering = theForm.getCurrentCourseOfferingWrapper().getCourseOfferingInfo();
220 
221         CourseOfferingWrapper coWrapper = new CourseOfferingWrapper(theCourseOffering);
222         getViewHelperService(theForm).build_AOs_RGs_AOCs_Lists_For_TheCourseOffering(theForm, coWrapper);
223 
224         getViewHelperService(theForm).loadPreviousAndNextCourseOffering(theForm);
225 
226         ARGToolbarUtil.processAoToolbarForUser(theForm.getActivityWrapperList(), theForm);
227     }
228 
229     public static String getGradingOption(String gradingOptionId) throws Exception {
230         String gradingOption = "";
231         if (StringUtils.isNotBlank(gradingOptionId)) {
232             ResultValuesGroupInfo rvg = getLrcService().getResultValuesGroup(gradingOptionId, ContextUtils.createDefaultContextInfo());
233             if (rvg != null && StringUtils.isNotBlank(rvg.getName())) {
234                 gradingOption = rvg.getName();
235             }
236         }
237 
238         return gradingOption;
239     }
240 
241     public static String getOrgNameDescription(String orgShortName) {
242         String shortName = "shortName";
243         String longName = "";
244 
245         QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
246         if (StringUtils.isNotBlank(orgShortName) && !orgShortName.isEmpty()) {
247             qBuilder.setPredicates(PredicateFactory.or(
248                     PredicateFactory.equal(shortName, orgShortName)));
249         } else {
250             throw new RuntimeException("Org short name is null!");
251         }
252         try {
253             QueryByCriteria query = qBuilder.build();
254 
255             OrganizationService organizationService = getOrganizationService();
256 
257             java.util.List<OrgInfo> orgInfos = organizationService.searchForOrgs(query, ContextUtils.createDefaultContextInfo());
258             if (!orgInfos.isEmpty()) {
259                 longName = orgInfos.get(0).getLongName();
260             }
261         } catch (Exception e) {
262             throw new RuntimeException("Error! No long name description found.", e);
263         }
264         return longName;
265     }
266 
267     public static Properties _buildCOURLParameters(CourseOfferingInfo courseOfferingInfo, String methodToCall) {
268         Properties props = new Properties();
269         props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
270         props.put("courseOfferingInfo.id", courseOfferingInfo.getId());
271         props.put(KRADConstants.DATA_OBJECT_CLASS_ATTRIBUTE, CourseOfferingEditWrapper.class.getName());
272         props.put(UifConstants.UrlParams.SHOW_HOME, BooleanUtils.toStringTrueFalse(false));
273         return props;
274     }
275 
276     public static boolean _isClusterUniqueWithinCO(ARGCourseOfferingManagementForm form, String courseOfferingId, String privateName) throws Exception{
277         List<String> foIds = new ArrayList<String>();
278         //fetch all the formatOfferingIds associated with the given courseOfferingId
279         //For performance, if FOIds are already in the form, use it (most likely it is). Otherwise, fetch FOs by COId
280         if (form.getFoId2aoTypeMap()==null || form.getFoId2aoTypeMap().isEmpty()) {
281             List<FormatOfferingInfo> formatOfferingList = getCourseOfferingService().getFormatOfferingsByCourseOffering(courseOfferingId,ContextUtils.createDefaultContextInfo());
282             for(FormatOfferingInfo foInfo:formatOfferingList){
283                 foIds.add(foInfo.getId());
284             }
285         } else {
286             foIds = new ArrayList<String>(form.getFoId2aoTypeMap().keySet());
287         }
288 
289         //Build up a term search criteria
290         if (!foIds.isEmpty()) {
291             QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
292             qbcBuilder.setPredicates(PredicateFactory.and(
293                     PredicateFactory.in("formatOfferingId", foIds.toArray()),
294                     PredicateFactory.equalIgnoreCase("privateName", privateName)));
295             QueryByCriteria criteria = qbcBuilder.build();
296 
297             List<ActivityOfferingClusterInfo> aoClusterList = getCourseOfferingService().searchForActivityOfferingClusters(criteria, ContextUtils.createDefaultContextInfo());
298             return aoClusterList.size() <= 0;
299         }
300 
301         return true;
302     }
303 
304 
305     public static ActivityOfferingClusterInfo _buildEmptyAOCluster (String formatOfferingId, String privateName, String publishedName){
306         ActivityOfferingClusterInfo emptyCluster = new ActivityOfferingClusterInfo();
307         emptyCluster.setTypeKey(CourseOfferingServiceConstants.AOC_ROOT_TYPE_KEY);
308         emptyCluster.setStateKey(CourseOfferingServiceConstants.AOC_ACTIVE_STATE_KEY);
309         emptyCluster.setPrivateName(privateName);
310         emptyCluster.setName(publishedName);
311         emptyCluster.setFormatOfferingId(formatOfferingId);
312         List<ActivityOfferingSetInfo> activityOfferingSets = new ArrayList<ActivityOfferingSetInfo>();
313         emptyCluster.setActivityOfferingSets(activityOfferingSets);
314         return emptyCluster;
315     }
316 
317     public static boolean _clusterForFormatOfferingValidation (FormatOfferingInfo formatOfferingInfo, ContextInfo context) {
318         try {
319             if (formatOfferingInfo.getActivityOfferingTypeKeys()!=null && formatOfferingInfo.getActivityOfferingTypeKeys().size()>1) {
320                 return true;
321             } else if (formatOfferingInfo.getActivityOfferingTypeKeys()!=null && formatOfferingInfo.getActivityOfferingTypeKeys().size()==1) {
322                 QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
323                 qbcBuilder.setPredicates(PredicateFactory.equal("formatOfferingId", formatOfferingInfo.getId()));
324                 QueryByCriteria criteria = qbcBuilder.build();
325 
326                 List<String> aoClusterIds = getCourseOfferingService().searchForActivityOfferingClusterIds(criteria, context);
327                 if (aoClusterIds!=null && aoClusterIds.size()>=1) {
328                     return false;
329                 } else {
330                     return true;
331                 }
332             } else {
333                 return false;
334             }
335         } catch (Exception e) {
336             throw new RuntimeException(e);
337         }
338 
339     }
340 
341     public static void clearForm (ARGCourseOfferingManagementForm form) throws Exception {
342         form.setAdminOrg(null);
343         form.setCourseOfferingResultList(new ArrayList<CourseOfferingListSectionWrapper>());
344         form.setActivityWrapperList(new ArrayList<ActivityOfferingWrapper>());
345         form.setClusterResultList(new ArrayList<ActivityOfferingClusterWrapper>());
346         form.setRgResultList(new ArrayList<RegistrationGroupWrapper>());
347 
348         form.setSelectedToDeleteList(new ArrayList<ActivityOfferingWrapper>());
349         form.setSelectedCoToDeleteList(new ArrayList<CourseOfferingListSectionWrapper>());
350 
351         form.setCourseOfferingCopyWrapper(null);
352         form.setFormatOfferingIdForNewAO(null);
353 
354         form.setHasMoreThanOneFormat(false);
355         form.setHasMoreThanOneCluster(false);
356         form.setTermInfo(null);
357         form.setSubjectCode(null);
358         form.setSubjectCodeDescription(null);
359 
360         form.setFormatIdForNewAO(null);
361         form.setFormatOfferingIdForNewAO(null);
362         form.setActivityIdForNewAO(null);
363         form.setClusterIdForNewAO(null);
364         form.setNoOfActivityOfferings(null);
365 
366         form.setCurrentCourseOfferingWrapper(null);
367         form.setPreviousCourseOfferingWrapper(null);
368         form.setNextCourseOfferingWrapper(null);
369 
370         form.setSocState(null);
371         form.setSocStateKey(null);
372 
373         form.setSelectedIllegalAOInDeletion(false);
374 
375         form.setEnableAddButton(false);
376         form.setEnableMoveAOButton(false);
377         form.setEnableAddClusterButton(false);
378 
379     }
380 
381     public static String getTermStartEndDate(String termId, TermInfo term) {
382         // Return Term as String display like 'FALL 2020 (9/26/2020-12/26/2020)'
383         StringBuilder stringBuilder = new StringBuilder();
384         Formatter formatter = new Formatter(stringBuilder, Locale.US);
385         String displayString = termId; // use termId as a default.
386         if (term != null) {
387             String startDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getStartDate());
388             String endDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getEndDate());
389             formatter.format("%s - %s", startDate, endDate);
390             displayString = stringBuilder.toString();
391         }
392         return displayString;
393     }
394 
395     public static String getTermDisplayString(String termId, TermInfo term) {
396         // Return Term as String display like 'FALL 2020 (9/26/2020-12/26/2020)'
397         StringBuilder stringBuilder = new StringBuilder();
398         Formatter formatter = new Formatter(stringBuilder, Locale.US);
399         String displayString = termId; // use termId as a default.
400         if (term != null) {
401             String startDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getStartDate());
402             String endDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getEndDate());
403             String termType = term.getName();
404             formatter.format("%s (%s to %s)", termType, startDate, endDate);
405             displayString = stringBuilder.toString();
406         }
407         return displayString;
408     }
409 
410 
411 }