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); }
private static void startStream(List<String> terms) throws TwitterException { if (twitter != null) { twitter.cleanUp(); } if (esClient != null) { esClient.close(); esClient = null; } play.Configuration pconf = Play.application().configuration(); String elasticSearchCluster = pconf.getString("tweet.elasticsearch.cluster.name"); if (elasticSearchCluster != null) { Logger.info("Configuring ElasticSearch..."); Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", elasticSearchCluster).build(); esClient = new TransportClient(settings) .addTransportAddress( new InetSocketTransportAddress( pconf.getString("tweet.elasticsearch.transport.host"), pconf.getInt("tweet.elasticsearch.transport.port"))); } else { esClient = null; } twitter4j.conf.Configuration tconf = Application.getTwitterConfiguration(); TwitterStreamFactory tf = new TwitterStreamFactory(tconf); twitter = tf.getInstance(); StatusListener l = new TweetListener( terms, esClient, pconf.getString("tweet.elasticsearch.index"), pconf.getString("tweet.elasticsearch.type")); twitter.addListener(l); String[] tracks = new String[terms.size()]; StringBuffer termsString = new StringBuffer(); for (int i = 0; i < terms.size(); i++) { tracks[i] = terms.get(i); if (i != 0) termsString.append(","); termsString.append(terms.get(i)); } FilterQuery q = new FilterQuery().track(tracks); twitter.filter(q); Logger.info("Starting listening for tweets using terms " + termsString.toString() + "..."); }
private static void showPage(String custId, String errMsg) { // Obviously this is inefficient -- the list of customer and products never changes, // so we could cache it. List<Customer> customers = Customer.find("order by name").fetch(); List<Product> products = Product.find("order by name").fetch(); Customer currentCustomer; if (custId == null || custId.trim().length() == 0) currentCustomer = customers.get(0); else currentCustomer = Customer.findById(new Long(custId)); renderTemplate("Application/index.html", customers, currentCustomer, products, errMsg); }
public TweetListener(List<String> terms, Client esClient, String esIndex, String esType) { this.terms = terms; this.esClient = esClient; StringBuilder query = new StringBuilder(); for (int i = 0; i < terms.size(); i++) { if (i > 0) query.append("|"); // query.append("(").append(Pattern.quote(terms.get(i))).append(")"); // query.append("(").append(terms.get(i)).append(")"); query.append(terms.get(i)); } Logger.info("Query match pattern: " + query.toString()); this.matchPattern = Pattern.compile(query.toString(), Pattern.CASE_INSENSITIVE); Logger.info("Query match pattern: " + matchPattern.toString()); }
public static void ajaxDeleteDocument(Long documentId) { // If it is not the root document we are going to delete if (documentId != 0L) { Document document = Document.findById(documentId); List<Document> children = Document.find("select p from Document p where p.parentId=?", document.id).fetch(); // If target document contains children documents // then remove all children, don't leave scala documents int i = 0; for (i = 0; i < children.size(); i++) { children.get(i).delete(); } document.delete(); } }
private static StreamConfig getConfig() { List<StreamConfig> configs = StreamConfig.find.findList(); if (configs.size() > 1) { Logger.error("Multiple stream configurations present!"); } StreamConfig config; if (configs.isEmpty()) { String[] tracks = new String[3]; tracks[0] = "nl-alert"; tracks[1] = "nlalert"; tracks[2] = "\"nl alert\""; config = new StreamConfig(tracks); config.save(); } else { config = configs.get(0); } return config; }
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); }