A simple search in Play Framework 2.1? - java

I am having a hell of a time figuring this out. I want to make a basic search engine in my Play-Framework 2.1 Java app. The app has a list of video game records stored in a MySQL database. The UI lets you type in a video game title and search for it. The Play app should return a list of all games that match the query that the user entered. If the user searches for "Mario", it should return a list of all games that contain that text.
Currently what I have in my Controller is:
public class Games extends controller {
//NOTE: instance fields omitted
public static Result search() {
DynamicForm form = form().bindFromRequest();
//Query is now stored in form.get("q"), though I can't figure out how to use it
List<Game> games = new Model.Finder(String.class, Game.class).all();
//This returns every game currently in the database
return ok(search.render(games);
}
}
I looked all throughout the Play 2.0.3 documentation (NOTE: I could not find documentation for 2.1 that covers this, which is the version I'm using) for the Model.Finder class and could not find out how to search custom queries, or how to build a SQL query that I want. I am quite lost and intermediate at Java. HELP! :-(

This is specific question for ebean not for Play.
You may do like this:
List<Game> games = new Model.Finder(String.class, Game.class)
.where().like("name", "foo%").findList();

Related

JDA quzzing system using reactions: checking answers using if-then statements

So I have been working on JDA for a while now and I wanted to create a quiz system in which people can answer using reactions. The problem is: I am stuck on how to check who voted which reaction. I have my code here:
public void onMessageReactionAdd(MessageReactionAddEvent event) {
MessageReaction reaction = event.getReaction();
ReactionEmote emote = reaction.getReactionEmote();
//if user reaction = certain emote then ...
}
I am stuck on that because I can't seem to find any API regarding a user's reaction. Any help will be appreaciated!
You need to save every single vote with the voting user in a Database.
Use MongoDB or MySQL for example.

automated testing webdrive webpage translation

I am trying to write an app using automated testing with webdriver in Java (I am really new to this), I can already log in and crawl the data I need from a website, the problem is that the page is in chinese and I am trying to display it in English in my app. I have found information about using right click but only on a WebElement, is there anyway I can right click on the page and translate to English or any other method to achieve this?
Thanks
Personally I would continue to retrieve the information in Chinese.
Store each type for example an id as a String. Then use an external library such as the Google Cloud Translate and then pass that id as the following:
public static void translateText(id, String sourceLang, String targetLang, PrintStream out)
{
Translate trans = createTranslateService();
TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
TranslateOption targLang = TranslateOption.sourceLanguage(targetLang);
TranslateOption model = TranslateOption.model("nmt");
Translation translation = translate.translate(id, srcLang, targLang, model);
translation.getTranslatedText());
// Then you can save this into a new variable and pass it onto your website as you need to.
}

How to set Record Selection Formula on Crystal Reports java

How do you create/set Record Selection Formula programatically on crystal reports using java? I tried searching on the internet but the only option is through IFilter which requires a Crystal Report Server. My program only uses the JRC library. Also this is a java desktop application using swing.
It may be a bit late, but maybe this is useful for someone:
reportClientDoc.getDataDefController().getRecordFilterController().setFormulaText("your record selection formula here");
I was doing some research about this and noticed that there are 3 methods with which you can do this:
Using the IFilter interface as shown in this example provided by SAP
// Set the filter string to be used as the Record Filter
String freeEditingFilter = "{Customer.Country} = 'Canada'";
// Retrieve the record filter for the Data Definition Controller
IFilter iFilter = clientDoc.getDataDefController().getDataDefinition().getRecordFilter();
// Set the filter to free editing text filter string
iFilter.setFreeEditingText(freeEditingFilter);
// Modify the filter through the Record Filter Controller to the report
clientDoc.getDataDefController().getRecordFilterController().modify(iFilter);
I am using the JRC only without a Crystal Report Server and the above example worked for me.
As Francisco said in his answer, using the setFormulaText method:
clientDoc.getDataDefController().getRecordFilterController().setFormulaText("{Customer.Country} = 'Canada'");
Using parameters. Parameters can be passed to the report using code (you can use the addDiscreteParameterValue function in the helper class) or else they can be filled in by the user during runtime. I chose not to opt for this option because they can not be set to optional
If you want to create a crystal report of your program, you need another jar file of software.
You can create your program in NetBeans IDE and link your IDE with IReport software which is used in NetBeans for creating Reporting in java.
You get many example from internet about this.

Vanity URLs with Playframework

I would like to create Vanity URLs for web application built ontop of Playframework.
I have tried to add a parameter for index method in main page Application, this will work if i call http://localhost:9000/?vanity_url and not for http://localhost:9000/vanity_url.
How would i achieve this? I would like users to create their own urls for the solution has to be dynamic, just like on Facebook
In the route you would put
GET /{<[a-z]+>fanPage}/? Page.showFanPage
With this one you could obtain:
http://localhost:9000/facebook
I also recommend using the slugify method provided by play framework, usually you would both check if the slug exists in the database, and look up for slug to grab the data (title of the fanpage/who's the owner of the fan page/how many views/etc etc)
In the tables:
Name: fanpage
pageID integer or bigint
title varchar
slug varchar unique
owner integer
content text
In code:
public static void showFanPage(String fanPage) {
// use models to look for the slug
// grab the data
// do what you need to do
}
I'm going on examples here on how to create the urls since I don't know what app you are building:
GET /post/slug/? Page.createPage
POST /post/slug/? Page.processPage
public static void createPage() {
// remember to create the template
render();
}
public static void processPage(String slugName) {
// slugName refers to the input field
// check if the slug exists and throw error if it does
// process
}
(note this is just an example, I don't know what kind of application you are building)
I hope this helps, let me know if this is what you meant

How do I write Facebook apps in Java?

I have looked in vain for a good example or starting point to write a java based facebook application... I was hoping that someone here would know of one. As well, I hear that facebook will no longer support their java API is this true and if yes does that mean that we should no longer use java to write facebook apps??
There's a community project which is intended to keep the Facebook Java API up to date, using the old official Facebook code as a starting point.
You can find it here along with a Getting Started guide and a few bits of sample code.
Facebook stopped supporting the official Java API on 5 May 2008 according to their developer wiki.
In no way does that mean you shouldn't use Java any more to write FB apps. There are several alternative Java approaches outlined on the wiki.
You might also want to check this project out; however, it only came out a few days ago so YMMV.
I write an example using facebook java api
It use FacebookXmlRestClient in order to make client request and print
all user infos
http://programmaremobile.blogspot.com/2009/01/facebook-java-apieng.html
BatchFB provides a modern Java API that lets you easily optimize your Facebook calls down to a minimum set:
http://code.google.com/p/batchfb/
Here's the example taken from the main page of what you can effectively do in a single FB request:
/** You write your own Jackson user mapping for the pieces you care about */
public class User {
long uid;
#JsonProperty("first_name") String firstName;
String pic_square;
String timezone;
}
Batcher batcher = new FacebookBatcher(accessToken);
Later<User> me = batcher.graph("me", User.class);
Later<User> mark = batcher.graph("markzuckerberg", User.class);
Later<List<User>> myFriends = batcher.query(
"SELECT uid, first_name, pic_square FROM user WHERE uid IN" +
"(SELECT uid2 FROM friend WHERE uid1 = " + myId + ")", User.class);
Later<User> bob = batcher.queryFirst("SELECT timezone FROM user WHERE uid = " + bobsId, User.class);
PagedLater<Post> feed = batcher.paged("me/feed", Post.class);
// No calls to Facebook have been made yet. The following get() will execute the
// whole batch as a single Facebook call.
String timezone = bob.get().timezone;
// You can just get simple values forcing immediate execution of the batch at any time.
User ivan = batcher.graph("ivan", User.class).get();
You might want to try Spring Social. It might be limited in terms of Facebook features, but lets you also connect to Twitter, LinkedIn, TripIt, GitHub, and Gowalla.
The other side of things is that as Facebook adds features some of the old API's might break, so using a simpler pure FB api (that you can update when things don't work) might be a good idea.
This tutorial will literally step you through everything you need to do: http://ocpsoft.org/opensource/creating-a-facebook-app-setup-and-tool-installation/
It comes in 3 parts. The other 2 are linked from there.

Categories

Resources