UI hangs due to multiple loops:Need code optimisation
<p>Can somebody please help to optimize the code below. </p>

<p>The problem statement is : the user on the ui selects the various courses and add them to the a list of selected students. The courses can be applied to students in 2 ways :</p>

<ol>
<li><p>selected courses are applied to selected students on ui</p></li>
<li><p>slected courses to be applied to all the students on the class. </p></li>
</ol>

<p>The code below works as expected but the UI hangs becuase of multiple loops, can somebody please help optimise it.</p>

<p>Map  <strong>coursesToApply</strong> and Map  <strong>coursesApplyToAll</strong> : have couse Id and teacher(provided by user from the ui at run time).</p>

<pre><code>@RequestMapping(value="/applycoursestostudents")
     public @ResponseBody List&lt;ModificationStudentData&gt; applycoursestostudentsToTrade(Model model, HttpServletRequest request,@RequestBody applyCoursesToStudents data){    
         ModificationStudentData ModificationStudentData = new ModificationStudentData();
         boolean saveAsDraft=data.isSaveAsDraft();
         String username=request.getUserPrincipal().getName();
         Map&lt;Integer,String&gt;  coursesToApply=new HashMap&lt;Integer, String&gt;();
         Map&lt;Integer,String&gt;  coursesApplyToAll=new HashMap&lt;Integer, String&gt;();
         List&lt;ModificationStudentData&gt; errorInRuleApplication=new ArrayList&lt;ModificationStudentData&gt;();
         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()&gt;0 || coursesApplyToAll.size()&gt;0){
                     for(Map.Entry&lt;Integer, String&gt; 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&lt;Integer, String&gt; 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;
     }
</code></pre>