Coverage Report - org.kuali.student.enrollment.class2.grading.service.impl.GradingViewHelperServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
GradingViewHelperServiceImpl
0%
0/134
0%
0/72
5.5
 
 1  
 package org.kuali.student.enrollment.class2.grading.service.impl;
 2  
 
 3  
 /*
 4  
  * Copyright 2007 The Kuali Foundation Licensed under the Educational Community
 5  
  * License, Version 1.0 (the "License"); you may not use this file except in
 6  
  * compliance with the License. You may obtain a copy of the License at
 7  
  * http://www.opensource.org/licenses/ecl1.php Unless required by applicable law
 8  
  * or agreed to in writing, software distributed under the License is
 9  
  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 10  
  * KIND, either express or implied. See the License for the specific language
 11  
  * governing permissions and limitations under the License.
 12  
  */
 13  
 
 14  
 import org.apache.commons.lang.StringUtils;
 15  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 16  
 import org.kuali.rice.core.api.util.ConcreteKeyValue;
 17  
 import org.kuali.rice.kim.api.identity.IdentityService;
 18  
 import org.kuali.rice.kim.api.identity.entity.Entity;
 19  
 import org.kuali.rice.kim.api.identity.name.EntityName;
 20  
 import org.kuali.rice.krad.uif.UifConstants;
 21  
 import org.kuali.rice.krad.uif.container.CollectionGroup;
 22  
 import org.kuali.rice.krad.uif.control.SelectControl;
 23  
 import org.kuali.rice.krad.uif.field.InputField;
 24  
 import org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl;
 25  
 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
 26  
 import org.kuali.rice.krad.uif.view.View;
 27  
 import org.kuali.rice.krad.util.GlobalVariables;
 28  
 import org.kuali.student.enrollment.academicrecord.dto.StudentCourseRecordInfo;
 29  
 import org.kuali.student.enrollment.academicrecord.infc.StudentCourseRecord;
 30  
 import org.kuali.student.enrollment.academicrecord.service.AcademicRecordService;
 31  
 import org.kuali.student.enrollment.acal.constants.AcademicCalendarServiceConstants;
 32  
 import org.kuali.student.enrollment.acal.dto.TermInfo;
 33  
 import org.kuali.student.enrollment.acal.service.AcademicCalendarService;
 34  
 import org.kuali.student.enrollment.class2.grading.dataobject.GradeStudent;
 35  
 import org.kuali.student.enrollment.class2.grading.dataobject.StudentCredit;
 36  
 import org.kuali.student.enrollment.class2.grading.form.GradingForm;
 37  
 import org.kuali.student.enrollment.class2.grading.form.StudentGradeForm;
 38  
 import org.kuali.student.enrollment.class2.grading.service.GradingViewHelperService;
 39  
 import org.kuali.student.enrollment.class2.grading.util.GradingConstants;
 40  
 import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
 41  
 import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
 42  
 import org.kuali.student.enrollment.grading.dto.GradeRosterEntryInfo;
 43  
 import org.kuali.student.enrollment.grading.dto.GradeRosterInfo;
 44  
 import org.kuali.student.enrollment.grading.dto.GradeValuesGroupInfo;
 45  
 import org.kuali.student.enrollment.grading.service.GradingService;
 46  
 import org.kuali.student.r2.common.dto.ContextInfo;
 47  
 import org.kuali.student.r2.common.exceptions.*;
 48  
 import org.kuali.student.r2.common.util.constants.*;
 49  
 import org.kuali.student.r2.lum.lrc.dto.ResultValueInfo;
 50  
 import org.kuali.student.mock.utilities.TestHelper;
 51  
 
 52  
 import javax.xml.namespace.QName;
 53  
 import java.util.ArrayList;
 54  
 import java.util.Collection;
 55  
 import java.util.List;
 56  
 
 57  0
 public class GradingViewHelperServiceImpl extends ViewHelperServiceImpl implements GradingViewHelperService {
 58  
 
 59  
     private AcademicCalendarService acalService;
 60  
     private CourseOfferingService coService;
 61  
     private GradingService gradingService;
 62  
     private AcademicRecordService academicRecordService;
 63  
 
 64  
     @Override
 65  
     public void populateGradeOptions(InputField field, GradingForm gradingForm) {
 66  0
         List keyValues = new ArrayList();
 67  0
         keyValues.add(new ConcreteKeyValue("", ""));
 68  
 
 69  0
         if (field.getContext().get(UifConstants.ContextVariableNames.LINE) != null
 70  
                 && field.getControl() instanceof SelectControl) {
 71  0
             GradeStudent student = (GradeStudent) field.getContext().get(UifConstants.ContextVariableNames.LINE);
 72  0
             for (ResultValueInfo option : student.getAvailabeGradingOptions()) {
 73  0
                 keyValues.add(new ConcreteKeyValue(option.getKey(), option.getValue()));
 74  
             }
 75  0
             ((SelectControl) field.getControl()).setOptions(keyValues);
 76  
         }
 77  
 
 78  0
     }
 79  
 
 80  
     @Override
 81  
     public void unAssignGrade(View view, Object model, String selectedCollectionPath, Integer selectedLine) {
 82  0
         CollectionGroup collectionGroup = view.getViewIndex().getCollectionGroupByPath(selectedCollectionPath);
 83  
 
 84  0
         Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(model, selectedCollectionPath);
 85  0
         if (collection == null) {
 86  0
             throw new RuntimeException("Unable to get collection property from model for path: "
 87  
                     + selectedCollectionPath);
 88  
         }
 89  
 
 90  0
         GradeStudent student = (GradeStudent) ((List<Object>) collection).get(selectedLine);
 91  0
         student.setSelectedGrade("");
 92  0
     }
 93  
 
 94  
     @Override
 95  
     public List<GradeStudent> loadStudents(String selectedCourse,GradingForm gradingForm) throws Exception {
 96  
 
 97  0
         ContextInfo context = TestHelper.getContext1();
 98  
 
 99  0
         IdentityService identityService = (IdentityService) GlobalResourceLoader.getService(new QName(
 100  
                 GradingConstants.IDENTITY_SERVICE_URL, GradingConstants.IDENTITY_SERVICE_NAME));
 101  
 
 102  0
         List<GradeStudent> students = new ArrayList();
 103  0
         List<GradeRosterInfo> rosterInfos = getGradingService().getFinalGradeRostersForCourseOffering(selectedCourse, context);
 104  0
         gradingForm.setRosterInfos(rosterInfos);
 105  
 
 106  0
         if (rosterInfos != null) {
 107  0
             for (GradeRosterInfo rosterInfo : rosterInfos) {
 108  0
                 if (StringUtils.equals(LuiPersonRelationServiceConstants.LPRROSTER_COURSE_FINAL_GRADEROSTER_SUBMITTED_STATE_KEY,rosterInfo.getStateKey())){
 109  0
                     gradingForm.setReadOnly(true);
 110  
                 }
 111  
 
 112  0
                 if (rosterInfo.getGradeRosterEntryIds().isEmpty()){
 113  0
                     return students;
 114  
                 }
 115  
 
 116  0
                 List<GradeRosterEntryInfo> entryInfos = getGradingService().getGradeRosterEntriesByIds(rosterInfo.getGradeRosterEntryIds(), context);
 117  
 
 118  0
                 if (!entryInfos.isEmpty() && entryInfos.get(0).getValidGradeGroupKeys().isEmpty()){
 119  0
                     GlobalVariables.getMessageMap().putWarning("selectedGrade",GradingConstants.WARNING_GRADING_OPTIONS_NOT_FOUND,"test");
 120  0
                     gradingForm.setReadOnly(true);
 121  
                 }
 122  
 
 123  0
                 for (GradeRosterEntryInfo entryInfo : entryInfos) {
 124  0
                     GradeStudent student = new GradeStudent();
 125  0
                     student.setGradeRosterEntryInfo(entryInfo);
 126  0
                     student.setStudentId(entryInfo.getStudentId());
 127  0
                     Entity entityInfo = identityService.getEntityByPrincipalId(entryInfo.getStudentId());
 128  0
                     List<EntityName> entityNameInfos = entityInfo.getNames();
 129  0
                     for (EntityName entityNameInfo : entityNameInfos) {
 130  0
                         if (entityNameInfo.isDefaultValue()) {
 131  0
                             student.setFirstName(entityNameInfo.getFirstNameUnmasked());
 132  0
                             student.setLastName(entityNameInfo.getLastNameUnmasked());
 133  
                         }
 134  
                     }
 135  
 
 136  0
                     List<String> validGradeGroupKeys = entryInfo.getValidGradeGroupKeys();
 137  
 
 138  0
                     List<GradeValuesGroupInfo> gradeValueInfos = getGradingService().getGradeGroupsByKeyList(validGradeGroupKeys, context);
 139  0
                     student.setGradeValuesGroupInfoList(gradeValueInfos);
 140  
 
 141  0
                     for (GradeValuesGroupInfo grade : gradeValueInfos) {
 142  0
                         if (grade.getResultValueInfos() != null && !grade.getResultValueInfos().isEmpty()) {
 143  0
                             student.getAvailabeGradingOptions().addAll(grade.getResultValueInfos());
 144  0
                             student.setPercentGrade(false);
 145  0
                         } else if (grade.getResultValueRange() != null) {
 146  0
                             student.setPercentGrade(true);
 147  
                         }
 148  
                     }
 149  
 
 150  0
                     if (StringUtils.isNotBlank(entryInfo.getAssignedGradeKey())) {
 151  0
                         student.setSelectedGrade(entryInfo.getAssignedGradeKey());
 152  
                     }
 153  
 
 154  0
                     students.add(student);
 155  0
                 }
 156  0
             }
 157  
         }
 158  
 
 159  0
         return students;
 160  
     }
 161  
 
 162  
     public boolean saveGrades(GradingForm gradingForm)throws Exception {
 163  
 
 164  0
         ContextInfo context = TestHelper.getContext1();
 165  
 
 166  0
         List<GradeStudent> gradeStudentList = gradingForm.getStudents();
 167  0
         boolean updateRoster = false;
 168  0
         for (GradeStudent gradeStudent : gradeStudentList) {
 169  0
             GradeRosterEntryInfo gradeRosterEntryInfo = gradeStudent.getGradeRosterEntryInfo();
 170  0
             String assignedGradeKey = gradeStudent.getSelectedGrade();
 171  0
             boolean returnValue = getGradingService().updateGrade(gradeRosterEntryInfo.getId(), assignedGradeKey, context);
 172  0
             if (returnValue){
 173  0
                 updateRoster = true;
 174  
             }
 175  0
         }
 176  
 
 177  0
         if (updateRoster){
 178  0
             for (GradeRosterInfo info : gradingForm.getRosterInfos()){
 179  0
                 getGradingService().updateFinalGradeRosterState(info.getId(), LuiPersonRelationServiceConstants.LPRROSTER_COURSE_FINAL_GRADEROSTER_SAVED_STATE_KEY,context);
 180  
             }
 181  0
             return true;
 182  
         }
 183  
 
 184  0
         return false;
 185  
     }
 186  
 
 187  
     public boolean submitGradeRoster(GradingForm gradingForm)throws Exception {
 188  0
         boolean save = saveGrades(gradingForm);
 189  0
         ContextInfo context = TestHelper.getContext1();
 190  
 
 191  0
         for (GradeRosterInfo info : gradingForm.getRosterInfos()){
 192  0
             getGradingService().updateFinalGradeRosterState(info.getId(), LuiPersonRelationServiceConstants.LPRROSTER_COURSE_FINAL_GRADEROSTER_SUBMITTED_STATE_KEY,context);
 193  
         }
 194  
 
 195  0
         return true;
 196  
     }
 197  
 
 198  
     public void loadStudentGrades(StudentGradeForm studentGradeForm)
 199  
     throws Exception {
 200  
 
 201  0
         List creditList = new ArrayList();
 202  
 
 203  0
         ContextInfo context = new ContextInfo();
 204  
 
 205  0
         TermInfo term = getAcalService().getTerm(studentGradeForm.getSelectedTerm(), context);
 206  
 
 207  0
         List<StudentCourseRecordInfo> courseRecords = getAcademicRecordService().getCompletedCourseRecordsForTerm(context.getPrincipalId(), term.getId(), context);
 208  0
         if (null != courseRecords) {
 209  0
             for (StudentCourseRecord courseRecord : courseRecords) {
 210  0
                 StudentCredit credit = new StudentCredit();
 211  
                 // TODO - is this correct?
 212  0
                 credit.setCourseId(courseRecord.getCourseCode());
 213  
                 // TODO - is this correct?
 214  0
                 credit.setCourseName(courseRecord.getCourseTitle());
 215  
 
 216  0
                 credit.setGrade(courseRecord.getAssignedGradeValue());
 217  0
                 credit.setCredits(courseRecord.getCreditsEarned());
 218  0
                 creditList.add(credit);
 219  0
             }
 220  
         }
 221  
 
 222  0
         studentGradeForm.setCreditList(creditList);
 223  0
         studentGradeForm.setTitle(term.getName() + " Grades");
 224  0
     }
 225  
 
 226  
     private TermInfo getCurrentACal(){
 227  0
         ContextInfo context = new ContextInfo();
 228  
 
 229  
         try{
 230  0
                 return getAcalService().getCurrentTerms(null,context).get(0);
 231  0
         } catch (DoesNotExistException e) {
 232  0
             throw new RuntimeException("No Terms found for current AcademicCalendar(s)! There should be some in the database.", e);
 233  0
         } catch (InvalidParameterException e) {
 234  0
             throw new RuntimeException(e);
 235  0
         } catch (MissingParameterException e) {
 236  0
             throw new RuntimeException(e);
 237  0
         } catch (OperationFailedException e) {
 238  0
             throw new RuntimeException(e);
 239  0
         } catch (PermissionDeniedException e) {
 240  0
             throw new RuntimeException(e);
 241  
         }
 242  
 
 243  
     }
 244  
 
 245  
     public void loadCourses(GradingForm form)throws Exception{
 246  
 
 247  0
         ContextInfo context = new ContextInfo();
 248  
 
 249  0
         TermInfo term = getAcalService().getTerm(form.getSelectedTerm(), context);
 250  
 
 251  0
         if (term == null){
 252  0
             throw new RuntimeException("No record found for the selected term");
 253  
         }
 254  
 
 255  0
         form.setSelectedTerm(term.getName());
 256  
 
 257  
 //        List<CourseOfferingInfo> courseOfferingInfoList = new ArrayList<CourseOfferingInfo>();
 258  
 
 259  
         try{
 260  0
             List<CourseOfferingInfo> cos = getCOService().getCourseOfferingsByTermAndInstructor(term.getId(), context.getPrincipalId(), context);
 261  
 
 262  0
             if (cos == null || cos.isEmpty()){
 263  0
                 GlobalVariables.getMessageMap().putInfo("firstName",GradingConstants.INFO_COURSE_NOT_FOUND_TO_GRADE,term.getName());
 264  0
                 return;
 265  
             }
 266  
 
 267  0
             form.setCourseOfferingInfoList(new ArrayList<CourseOfferingInfo>());
 268  0
             if (!cos.isEmpty()){
 269  0
                 for (CourseOfferingInfo co : cos) {
 270  0
                     if (StringUtils.equals(co.getStateKey(), LuiServiceConstants.LUI_OFFERED_STATE_KEY) &&
 271  
                         StringUtils.equals(co.getTypeKey(),LuiServiceConstants.COURSE_OFFERING_TYPE_KEY)){
 272  0
                         form.getCourseOfferingInfoList().add(co);
 273  
                     }
 274  
                 }
 275  
             }
 276  0
         }catch(Exception e){
 277  
             //FIXME: Change it to use proper error handling
 278  0
             throw new RuntimeException(e);
 279  0
         }
 280  0
     }
 281  
 
 282  
     protected AcademicCalendarService getAcalService() {
 283  0
         if(acalService == null) {
 284  0
             acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
 285  
         }
 286  0
         return this.acalService;
 287  
     }
 288  
 
 289  
     protected CourseOfferingService getCOService(){
 290  0
         if (coService == null){
 291  0
             coService = (CourseOfferingService)GlobalResourceLoader.getService(new QName(CourseOfferingServiceConstants.NAMESPACE, CourseOfferingServiceConstants.SERVICE_NAME_LOCAL_PART));
 292  
         }
 293  0
         return coService;
 294  
     }
 295  
 
 296  
     protected GradingService getGradingService() {
 297  0
         if (gradingService == null){
 298  0
             gradingService = (GradingService) GlobalResourceLoader.getService(new QName(GradingServiceConstants.NAMESPACE, GradingServiceConstants.SERVICE_NAME_LOCAL_PART));
 299  
         }
 300  0
         return gradingService;
 301  
     }
 302  
 
 303  
     protected AcademicRecordService getAcademicRecordService() {
 304  0
         if (academicRecordService == null){
 305  0
             academicRecordService = (AcademicRecordService) GlobalResourceLoader.getService(new QName(AcademicRecordServiceConstants.NAMESPACE, AcademicRecordServiceConstants.SERVICE_NAME));
 306  
         }
 307  0
         return academicRecordService;
 308  
     }
 309  
 }