package hello.pages;

import hello.entities.*;

import java.util.*;
import java.util.concurrent.*;
import java.io.IOException;

import org.apache.tapestry5.*;
import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.ioc.annotations.*;
import org.apache.tapestry5.json.*;
import org.apache.tapestry5.util.*;
import org.apache.tapestry5.services.*;
import org.hibernate.*;
import com.fasterxml.jackson.databind.*;

/**
 * Database Mapping Test
 */
public class HelloDB
{
  @Inject
  private org.hibernate.Session session;

  @Inject
  private Request request;

  private static final int DB_ROWS = 10000;

  private static final ObjectMapper mapper = new ObjectMapper();

  StreamResponse onActivate() {

    // Read queries from URL, but don't bother validating
    int queries = 1;
    String qString = this.request.getParameter("queries");
    if (qString != null) {
      queries = Integer.parseInt(qString);
    }
    if (queries <= 0) {
      queries = 1;
    }
    final World[] worlds = new World[queries];

    // For generating a random row ID
    final Random rand = ThreadLocalRandom.current();

    for (int i = 0; i < queries; i++) {
      // Read object from database
      worlds[i] = (World)session.get(World.class, new Integer(rand.nextInt(DB_ROWS) + 1));
    }

    // Send reponse
    String response = "";
    try
    {
      response = HelloDB.mapper.writeValueAsString(worlds);
    }
    catch (IOException ex)
    {
      // do nothing
    }
    return new TextStreamResponse("application/json", response);
  }
}

@@ -1342,24 +1342,151 @@
   try%0A    %7B%0A
+      if (queries == 1)%0A      %7B%0A        response = HelloDB.mapper.writeValueAsString(worlds%5B0%5D); %0A      %7D%0A      else%0A      %7B%0A  
       respon
@@ -1533,16 +1533,24 @@
 orlds);%0A
+      %7D%0A
     %7D%0A  

verify Fixed tapestry's db test