public static double distance(double lat1, double lon1, double lat2, double lon2) { double theta = lon1 - lon2; double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta)); dist = Math.acos(dist); dist = rad2deg(dist); dist = dist * 60 * 1.85315962; return (dist); }
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); }