public static void viewAd(String id) { Ad ad = Ad.findById(Long.parseLong(id)); List<Category> cats = Category.find("categorytype_id=?1 order by id", "1").fetch(); EntityManager entityManager = play.db.jpa.JPA.em(); List<BigInteger> bCounts = entityManager .createNativeQuery( "select count(*) as maxCount from Ad as a group by category_id order by maxCount") .getResultList(); int min = bCounts.get(0).intValue(); int max = bCounts.get(bCounts.size() - 1).intValue(); bCounts = entityManager .createNativeQuery( "select count(*) as maxCount from Ad as a group by category_id order by category_id ") .getResultList(); List<String> fonts = new ArrayList<String>(); for (int i = 0; i < bCounts.size(); i++) { BigInteger count = bCounts.get(i); int x = Ads.getFontSize(count.intValue(), min, max); fonts.add(String.valueOf(x)); } render(ad, fonts, min, max, cats); }
public static void main(String[] args) { ProcessBuilder process = new ProcessBuilder(); Integer port; if (process.environment().get("PORT") != null) { port = Integer.parseInt(process.environment().get("PORT")); } else { port = 4567; } staticFileLocation("/public"); String layout = "templates/layout.vtl"; get( "/", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/index.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // get(/) type of route get this url.. get( "/tasks", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("tasks", Task.all()); model.put("template", "templates/tasks.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // After they submit the form, this is where they will be taken /tasks.vtl // get("tasks/new", (request, response) -> { // HashMap<String, Object> model = new HashMap<String, Object>(); // model.put("template", "templates/task-form.vtl"); // return new ModelAndView(model, layout); // }, new VelocityTemplateEngine()); // //task-form is where client inputs data and hits submit post( "/tasks", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); Category category = Category.find(Integer.parseInt(request.queryParams("categoryId"))); String description = request.queryParams("description"); Task newTask = new Task(description); category.addTask(newTask); model.put("category", category); model.put("template", "templates/success.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // grabs information and makes a new description of the information in the array // takes you to a new page get( "/tasks/:id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); Task task = Task.find(Integer.parseInt(request.params(":id"))); model.put("task", task); model.put("template", "templates/task.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/categories", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("categories", Category.all()); model.put("template", "templates/categories.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/categories/new", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/category-form.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); post( "/categories", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); String name = request.queryParams("name"); Category newCategory = new Category(name); model.put("category", newCategory); model.put("template", "templates/success.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/categories/:id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("category", Category.find(Integer.parseInt(request.params(":id")))); model.put("template", "templates/category.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/categories/:id/tasks/new", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("category", Category.find(Integer.parseInt(request.params(":id")))); model.put("template", "templates/category-tasks-form.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); } // end of main
public static void list( String search, int category, Integer size, Integer page, int firstPage, int lastPage) { List<Ad> ads = null; List<Category> cats = Category.find("categorytype_id=?1", "1").fetch(); EntityManager entityManager = play.db.jpa.JPA.em(); List<BigInteger> bCounts = entityManager .createNativeQuery( "select count(*) as maxCount from Ad as a group by category_id order by maxCount") .getResultList(); int min = bCounts.get(0).intValue(); int max = bCounts.get(bCounts.size() - 1).intValue(); bCounts = entityManager .createNativeQuery( "select count(*) as maxCount from Ad as a group by category_id order by category_id ") .getResultList(); List<String> fonts = new ArrayList<String>(); for (int i = 0; i < bCounts.size(); i++) { BigInteger count = bCounts.get(i); int x = Ads.getFontSize(count.intValue(), min, max); fonts.add(String.valueOf(x)); } int pagesCount = 0; page = page != null ? page : 1; if (search.trim().length() == 0) { Long l = null; if (category == 0) { ads = Ad.find("order by createDate desc").fetch(page, size); l = Ad.count(); } else { ads = Ad.find(" category_id=?1 order by createDate desc", category).fetch(page, size); l = Ad.count(" category_id=?1 ", category); } Long l2 = (l / 10); if ((l % 10) > 0) l2 = (long) (Math.floor(l2) + 1); pagesCount = Integer.valueOf(l2.intValue()); } else { search = search.toLowerCase(); Long l = null; if (category == 0) { ads = Ad.find( "(lower(headline) like ?1 OR lower(description) like ?2)", "%" + search + "%", "%" + search + "%") .fetch(page, size); l = Ad.count( "(lower(headline) like ?1 OR lower(description) like ?2)", "%" + search + "%", "%" + search + "%"); } else { ads = Ad.find( " category_id=?1 and (lower(headline) like ?2 OR lower(description) like ?3)", category, "%" + search + "%", "%" + search + "%") .fetch(page, size); l = Ad.count( "category_id=?1 and (lower(headline) like ?2 OR lower(description) like ?3)", category, "%" + search + "%", "%" + search + "%"); } Long l2 = (l / 10); if ((l % 10) > 0) l2 = (long) (Math.floor(l2) + 1); pagesCount = Integer.valueOf(l2.intValue()); } if ((lastPage - page) <= 2) { firstPage = page - 2; lastPage = page + 7; if (lastPage > pagesCount) lastPage = pagesCount; } else if ((page - firstPage) <= 2) { firstPage = page - 7; lastPage = page + 2; if (firstPage < 1) { firstPage = 1; lastPage = 10; } } if (lastPage > pagesCount) lastPage = pagesCount; render(ads, search, size, page, pagesCount, firstPage, lastPage, cats, fonts); }