2016-08-10 42 views
0

我有兩個控制器(類)。在其中一箇中,我正在提出請求,並將其結果數據傳輸到另一個控制器。但不知何故,它只返回一個空列表。 第一控制器,使一個請求,併發送數據:如何從我的控制器獲取數據?

package controllers; 

import com.querydsl.core.Tuple; 
import com.querydsl.jpa.impl.JPAQueryFactory; 
import models.*; 
import play.db.jpa.JPAApi; 
import play.db.jpa.Transactional; 
import play.mvc.Controller; 
import play.mvc.Result; 

import javax.inject.Inject; 
import javax.persistence.EntityManager; 
import java.time.LocalDate; 
import java.util.ArrayList; 
import java.util.List; 

import static java.util.Collections.addAll; 

public class TestingController extends Controller { 



    private JPAApi jpaApi; 
    @Inject 
    public TestingController(JPAApi jpaApi) { 
     this.jpaApi = jpaApi; 
    } 

    private static ArrayList<ArrayList<Object>> listOfLists = new ArrayList<>(); 

    @Transactional 
    public Result resolving_requests() { 
     QClaim claim = QClaim.claim; 
     QFinancialClaimData financialClaimData = QFinancialClaimData.financialClaimData; 
     QDate_diff date_diff=QDate_diff.date_diff; 
     EntityManager em=jpaApi.em(); 
     JPAQueryFactory queryFactory = new JPAQueryFactory(em); 

     //QUERY1: Costs and amounts of different claims for the last year 
     String columns[] = {"Month, Year", "Type of risk", "Risks count", "Risks amount"}; 
     List<Tuple> q1=queryFactory.select(claim.typeOfRisk, financialClaimData.overAllAmount.sum(), claim.claimDate_finish.count(), claim.claimDate_start.month(), claim.claimDate_start.year()) 
       .from(claim) 
       .where(claim.claimDate_start.year().gt(LocalDate.now().getYear()-2).and(claim.claimDate_start.month().gt(LocalDate.now().getMonthValue())).or(claim.claimDate_start.year().eq(LocalDate.now().getYear()))) 
       .groupBy(claim.typeOfRisk, claim.claimDate_start.month(), claim.claimDate_start.year()) 
       .orderBy(claim.claimDate_start.year().asc(), claim.claimDate_start.month().asc(), claim.typeOfRisk.asc()) 
       .fetch(); 
     ArrayList<Object> singleList = new ArrayList<>(); 
     addAll(singleList, columns); //Adding all columns' names to the first row, simailar to foreach 
     this.listOfLists.add(singleList); 
     for (Tuple l: q1){ 
      singleList = new ArrayList<>(); 
      singleList.add(l.get(claim.claimDate_start.month()).toString() + "/" + l.get(claim.claimDate_start.year()).toString()); 
      singleList.add(l.get(claim.typeOfRisk)); 
      singleList.add(l.get(claim.claimDate_finish.count())); 
      singleList.add(l.get(financialClaimData.overAllAmount.sum())); 
      this.listOfLists.add(singleList); 
     } 

     System.out.println(listOfLists); 
     for (List i: listOfLists){ 
      for (Object i_inner: i){ 
       System.out.print(i_inner+"|"); //That works fine and I see a result 
      } 
      System.out.print("\n"); 
     } 
     return ok("Your new application is ready."); 
    } 

    public static ArrayList<ArrayList<Object>> get_arr_list() { 
     return listOfLists; //Probably that returns empty list 
    } 
} 

第二控制器,應接收的ArrayList: 包控制器;

import com.fasterxml.jackson.databind.JsonNode; 
import play.mvc.*; 
import java.util.ArrayList; 


public class ReportsController extends Controller { 

    public Result generating_reports(){ 
       ArrayList<ArrayList<Object>> listOfLists = TestingController.get_arr_list(); 
       System.out.println("Here should be a list: "+listOfLists); //This returns an empty list [] 

     return ok("All is ready!"); 
    } 
} 

我該如何解決它?

+1

[MCVE]如何用最少的代碼來證明問題? – c0der

+1

@Bogdan除非執行了'resolving_requests()'(通過自己的http req或調用),否則該列表將爲空...另外,併發設置中的靜態ArrayList會給您帶來很多問題... – Salem

+0

@Salem我該如何執行它? –

回答

1

首先發佈一個最小的可運行代碼來證明問題至關重要。 這意味着刪除所有不重要的東西,包括代碼編譯和運行所需的所有東西。以下是一個示例(請參閱註釋以獲得解釋):

import static java.util.Collections.addAll; 
import java.util.ArrayList; 

public class TestingController { 

    private static ArrayList<ArrayList<Object>> listOfLists = new ArrayList<>(); 

    //in the code you posted this method never gets executed 
    public void resolving_requests() { 

     String columns[] = {"Month, Year", "Type of risk", "Risks count", "Risks amount"}; 

     ArrayList<Object> singleList = new ArrayList<>(); 
     addAll(singleList, columns); //Adding all columns' names to the first row, simailar to foreach 

     listOfLists.add(singleList); 
    } 

    public static ArrayList<ArrayList<Object>> get_arr_list() { 
     return listOfLists; //Probably that returns empty list 
    } 

    public static void main(String args[]) { 

     //this will print an empty list because resolving_requests was not executed 
     System.out.println("listOfLists "+ TestingController.get_arr_list()) ; 

     //execute resolving_requests 
     new TestingController().resolving_requests(); 

     //now array list is not empty 
     System.out.println("listOfLists "+ TestingController.get_arr_list()) ; 
    } 
} 
+0

謝謝!這樣可行! 但是隻有JPAApi變量: 'new TestingController(jpaApi).resolving_requests(); System.out.println(「listOfLists」+ TestingController.get_arr_list());' 儘管我已經在TestingController中聲明瞭它...你知道我該如何解決這個需求嗎? –

+0

該代碼不包含JPAApi,但它演示了可能的問題。 MINIMAL可幫助您識別問題。帶上runnable幫助別人幫助你,並且尊重別人的時間。 – c0der

+0

我不知道我關注你,但你可能想保留一個對測試控制器的引用:'TestingController tc = TestingController(jpaApi);然後在需要時使用它:'tc.get_arr_list();' – c0der

相關問題