/** * ProjectsController.java * * $Author: cysun $ * $Date: 2009-03-16 08:08:21 -0700 (Mon, 16 Mar 2009) $ * $Revision: 242 $ */ package csns.spring.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; import csns.model.Assignment; import csns.model.Course; import csns.model.Enrollment; import csns.model.File; import csns.model.Grade; import csns.model.Quarter; import csns.model.Section; import csns.model.Submission; import csns.model.User; import csns.model.assessment.SeniorProject; import csns.model.dao.CourseDao; import csns.model.dao.QuarterDao; import csns.model.dao.SectionDao; public class ProjectsController extends AbstractController { private String downloadUrl; private CourseDao courseDao; private QuarterDao quarterDao; private SectionDao sectionDao; protected ModelAndView handleRequestInternal( HttpServletRequest request, HttpServletResponse response ) throws Exception { Course cs491b = courseDao.getCourseByCode( "CS491B" ); Quarter currentQuarter = new Quarter(); String quarterCode = request.getParameter( "quarter" ); Quarter quarter = quarterCode == null ? currentQuarter.previous() : new Quarter( Integer.parseInt( quarterCode ) ); List quarters = quarterDao.getQuartersByCourse( cs491b ); for( int i = 0; i < quarters.size(); ++i ) if( quarters.get( i ).equals( currentQuarter ) ) { quarters.remove( i ); break; } List projects = new ArrayList(); Map indexedProjects = new HashMap(); List
sections = sectionDao.getSections( quarter, cs491b ); for( Section section : sections ) { for( Enrollment enrollment : section.getEnrollments() ) { Grade grade = enrollment.getGrade(); if( grade == null || grade.getValue() == null || grade.getValue() < 2 ) continue; SeniorProject project = new SeniorProject( enrollment .getStudent() ); projects.add( project ); indexedProjects.put( enrollment.getStudent(), project ); } for( Assignment assignment : section.getAssignments() ) { String name = assignment.getName(); if( name.equals( "Project" ) ) setProjectInfo( assignment, indexedProjects ); if( name.equals( "Project Report" ) || name.equals( "Project Report (Final)" ) ) setReportInfo( assignment, indexedProjects ); if( name.equals( "Project Presentation" ) || name.equals( "Final Presentation" ) ) setPresentationInfo( assignment, indexedProjects ); } } return new ModelAndView( "projects" ).addObject( "projects", projects ) .addObject( "quarter", quarter ).addObject( "quarters", quarters ); } private void setProjectInfo( Assignment assignment, Map indexedProjects ) { for( Submission submission : assignment.getSubmissions() ) { SeniorProject project = indexedProjects.get( submission .getStudent() ); String notes = submission.getNotes(); if( notes != null && notes.equalsIgnoreCase( "good" ) ) project.setGood( true ); } } private void setReportInfo( Assignment assignment, Map indexedProjects ) { for( Submission submission : assignment.getSubmissions() ) { SeniorProject project = indexedProjects.get( submission .getStudent() ); File file = getLatestFile( submission.getFiles() ); if( project != null ) { project.setTitle( submission.getNotes() ); if( file != null ) project.setReportUrl( downloadUrl + "?fileId=" + file.getId() ); } } } private void setPresentationInfo( Assignment assignment, Map indexedProjects ) { for( Submission submission : assignment.getSubmissions() ) { SeniorProject project = indexedProjects.get( submission .getStudent() ); File file = getLatestFile( submission.getFiles() ); if( project != null ) { project.setVideoUrl( submission.getNotes() ); if( file != null ) project.setSlidesUrl( downloadUrl + "?fileId=" + file.getId() ); } } } private File getLatestFile( Set files ) { File latestFile = null; for( File file : files ) if( latestFile == null || latestFile.getDate().before( file.getDate() ) ) latestFile = file; return latestFile; } public void setDownloadUrl( String downloadUrl ) { this.downloadUrl = downloadUrl; } public void setCourseDao( CourseDao courseDao ) { this.courseDao = courseDao; } public void setQuarterDao( QuarterDao quarterDao ) { this.quarterDao = quarterDao; } public void setSectionDao( SectionDao sectionDao ) { this.sectionDao = sectionDao; } }