有人可以帮助优化下面的代码。
问题陈述是:ui上的用户选择各种课程并将其添加到所选学生的列表中。这些课程可以通过两种方式应用于学生:
所选课程适用于ui的选定学生
选修课程适用于全班同学。
下面的代码按预期工作,但UI挂起是因为有多个循环,有人可以帮助优化它。
映射 coursesToApply 并映射 coursesApplyToAll :拥有couse Id和教师(在运行时由ui用户提供)。
@RequestMapping(value="/applycoursestostudents")
public @ResponseBody List<ModificationStudentData> applycoursestostudentsToTrade(Model model, HttpServletRequest request,@RequestBody applyCoursesToStudents data){
ModificationStudentData ModificationStudentData = new ModificationStudentData();
boolean saveAsDraft=data.isSaveAsDraft();
String username=request.getUserPrincipal().getName();
Map<Integer,String> coursesToApply=new HashMap<Integer, String>();
Map<Integer,String> coursesApplyToAll=new HashMap<Integer, String>();
List<ModificationStudentData> errorInRuleApplication=new ArrayList<ModificationStudentData>();
int[] applyCoursesToStudents=data.getapplyCoursesToStudents();
int[] allStudentRollNumbers=data.getallStudentRollNumbers();
String caseRef=data.getCaseRef();
int seq=data.getSeq();
try{
int StudentClass=service.getModificationStudentData(dataSource,caseRef,seq);
coursesToApply=data.getcoursesToApply();
coursesApplyToAll=data.getcoursesApplyToAll();
try{
if(saveAsDraft){
removeExistingCourses(datSource,applyCoursesToStudents, username,StudentClass);
}
if(coursesToApply.size()>0 || coursesApplyToAll.size()>0){
for(Map.Entry<Integer, String> entry : coursesApplyToAll.entrySet()) {
for(int rollNum:allStudentRollNumbers){
try{
service.addCoursesToStudents(dataSource, entry.getKey(), rollNum, username,entry.getValue());
}catch(Exception e){
e.printStackTrace();
}
if(!saveAsDraft){
try{
service.generateTermPlan(dataSource,StudentClass, rollNum, username);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
for(Map.Entry<Integer, String> entry : coursesToApply.entrySet()) {
for(int rollNum:applyCoursesToStudents){
try{
service.addCoursesToStudents(dataSource, entry.getKey(), rollNum, username,entry.getValue());
}catch(Exception e){
e.printStackTrace();
}
if(!saveAsDraft){
try{
service.generateTermPlan(dataSource,StudentClass, rollNum, username);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
}catch(NullPointerException e){
for(int rollNum:applyCoursesToStudents){
if(!saveAsDraft){
try{
service.generateTermPlan(dataSource,StudentClass, rollNum, username);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
if(!saveAsDraft){
try{
service.getGeneratedReports(dataSource, StudentClass);
}
catch(Exception exp){
exp.printStackTrace();
}
}
}
catch(Exception e){
e.printStackTrace();
}
retrun null;
}