package ensimag.cloud.projet.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import ensimag.cloud.projet.datastore.MusicDataStore;
import ensimag.cloud.projet.datastore.Util;
import ensimag.cloud.projet.entity.Movie;
import ensimag.cloud.projet.entity.Music;
import ensimag.cloud.projet.webservice.JSonPuller;
public class GetInfoMovieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String title = request.getParameter("title").trim();
StringBuffer formatTitle = new StringBuffer();
String strErrorMsg;
if (title == null || title.length() == 0) {
strErrorMsg = "Invalid title !";
goBackToIndex(request, response, strErrorMsg);
} else {
JSonPuller jSonPuller = new JSonPuller();
strErrorMsg = jSonPuller.searchJSonFilmInformation(title, formatTitle);
if (strErrorMsg != null) {
goBackToIndex(request, response, strErrorMsg);
} else {
Key key = KeyFactory.createKey("Movie", formatTitle.toString());
Entity entityMovie = Util.findEntity(key);
displayInfo(entityMovie, request, formatTitle.toString(), response);
}
}
}
private void goBackToIndex(HttpServletRequest request,
HttpServletResponse response, String strMsgError)
throws ServletException, IOException {
request.setAttribute("error", strMsgError);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
private void displayInfo(Entity entityMovie, ServletRequest request,
String title, ServletResponse response) throws ServletException,
IOException {
Movie movie = new Movie((String) entityMovie.getProperty("title"),
(String) entityMovie.getProperty("year"),
(String) entityMovie.getProperty("genre"),
(String) entityMovie.getProperty("director"),
(String) entityMovie.getProperty("writer"),
(String) entityMovie.getProperty("actors"),
(String) entityMovie.getProperty("plot"),
(String) entityMovie.getProperty("poster"),
(String) entityMovie.getProperty("runtime"),
(String) entityMovie.getProperty("rating"));
request.setAttribute("movie", movie);
List<Music> listMusics = new ArrayList<Music>();
// Iterator<Entity> itEntity = Util.listEntities("Music",
// "nameFilm",
// title).iterator();
Iterator<Entity> itEntity = MusicDataStore.getAllMusic().iterator();
// Iterator<Entity> itEntity = Util.listEntities("Music", "", "")
// .iterator();
Entity entityMusicTmp = null;
System.out.println(itEntity.hasNext());
while (itEntity.hasNext()) {
entityMusicTmp = itEntity.next();
if (entityMusicTmp.getProperty("nameFilm").equals(title)) {
Music music = new Music(
(String) entityMusicTmp.getProperty("title"),
(String) entityMusicTmp.getProperty("nameFilm"),
(String) entityMusicTmp.getProperty("link"),
(String) entityMusicTmp.getProperty("duration"),
(String) entityMusicTmp.getProperty("album"),
(String) entityMusicTmp.getProperty("artist"));
listMusics.add(music);
}
}
request.setAttribute("musics", listMusics);
request.getRequestDispatcher("infoMovie.jsp")
.forward(request, response);
}
}