/// <summary> /// Generates the internal JSON representation to be used internally in EL COLOMBIANO web pages. The topic is the movies category. /// </summary> /// <param name="sender">Sender object which fired the event</param> /// <param name="e">Parameters sent from the event manager.</param> protected void Page_Load(object sender, EventArgs e) { WebClient client = new WebClient(); string url = "https://www.planepoly.com:8181/PlanepolyCoreWeb/OSConcierge?lat=6.210506&long=-75.57096&idTipos=201&consulta=eventos&bypass=626262"; Stream data = client.OpenRead(url); StreamReader reader = new StreamReader(data); string s = reader.ReadToEnd(); data.Close(); reader.Close(); Response.AddHeader("Access-Control-Allow-Origin", "*"); var movieList = JsonConvert.DeserializeObject <Movie>(s); var movieServiceList = movieList.servicios; foreach (var it in movieServiceList) { it.nombre = it.nombre.Trim(); } var movieListOrdered = movieServiceList.OrderByDescending(x => x.estr).OrderBy(x => x.nombre).ToList(); movieList.servicios = movieListOrdered; List <string> theaterNameList = new List <string>(); movieList.servicios.ForEach(x => x.ptos.Select(y => y.nombre).ToList().ForEach(z => theaterNameList.Add(z))); var theatersList = theaterNameList.Distinct().OrderBy(y => y).ToList(); var genresList = (from service in movieServiceList orderby service.genero select service.genero).Distinct().ToList <String>(); var allMovieNameList = (from service in movieServiceList orderby service.nombre select service.nombre).Distinct().ToList <string>(); MovieCatalog movieCatalog = new MovieCatalog(); movieCatalog.theaters = theatersList; movieCatalog.genres = genresList; movieCatalog.movies = allMovieNameList; Dictionary <string, List <string> > theaterMoviesList = new Dictionary <string, List <string> >(); foreach (var item in movieCatalog.theaters) { List <string> movieNameList = new List <string>(); theaterMoviesList.Add(item, movieNameList); } // Creates now a list of all movies mapped to our structure, given // the Planepoly JSON structure. List <MovieLookup> movieLookupList = new List <MovieLookup>(); foreach (var service in movieList.servicios) { MovieLookup movieLookup = new MovieLookup(); movieLookup.name = service.nombre; movieLookup.img = service.img; movieLookup.url = service.url; movieLookup.premiere = service.estr; movieLookup.genre = service.genero; movieLookup.locations = new List <MovieLookupLocation>(); foreach (var location in service.ptos) { if (theaterMoviesList.ContainsKey(location.nombre)) { var tempList = theaterMoviesList[location.nombre]; tempList.Add(service.nombre); } MovieLookupLocation mlLocation = new MovieLookupLocation(); mlLocation.name = location.nombre; mlLocation.address = location.direccion; List <MovieLookupShow> mlShowList = new List <MovieLookupShow>(); Dictionary <int, string> shows = new Dictionary <int, string>(); shows.Add(0, ""); shows.Add(1, ""); shows.Add(2, ""); shows.Add(3, ""); shows.Add(4, ""); shows.Add(5, ""); shows.Add(6, ""); shows.Add(7, ""); foreach (var show in location.funcs) { if (shows.ContainsKey(show.dia)) { var valDay = shows[show.dia]; valDay += show.hora + " "; shows[show.dia] = valDay; } } foreach (var it in shows) { if (it.Value.Trim() != "") { MovieLookupShow mls = new MovieLookupShow(); mls.frequency = it.Key; mls.hours = it.Value.Trim(); switch (mls.frequency) { case 0: mls.name = "Diario"; break; case 1: mls.name = "Lunes"; break; case 2: mls.name = "Martes"; break; case 3: mls.name = "Miércoles"; break; case 4: mls.name = "Jueves"; break; case 5: mls.name = "Viernes"; break; case 6: mls.name = "Sábado"; break; case 7: mls.name = "Domingo"; break; default: break; } mlShowList.Add(mls); } } mlLocation.schedule = mlShowList; movieLookup.locations.Add(mlLocation); } movieLookupList.Add(movieLookup); } foreach (var it in theaterMoviesList) { it.Value.Sort(); } movieCatalog.theaterMovies = theaterMoviesList; var movieLookupListOrdered = (from item in movieLookupList orderby item.premiere descending, item.name select item).ToList(); string movieCatalogJSON = JsonConvert.SerializeObject(movieCatalog); string movieLookupJSON = JsonConvert.SerializeObject(movieLookupListOrdered); // Now that we have just gathered all the information, create static JSON versions // Now there are two files to consume the feed // Full movie (mapped from origin). string fileName = @"D:\SitiosWeb\Sitio\EC100A_Servicios\EC100A_PlanepolyWidget\planepoly-movies.json"; using (StreamWriter writer = new StreamWriter(fileName)) { writer.Write(movieLookupJSON); } // Full movie catalog (mapped from origin) fileName = @"D:\SitiosWeb\Sitio\EC100A_Servicios\EC100A_PlanepolyWidget\planepoly-movies-catalog.json"; using (StreamWriter writer = new StreamWriter(fileName)) { writer.Write(movieCatalogJSON); } // This is the page result. Response.Write(movieLookupJSON); }
/// <summary> /// Main method to compute DB records to JSON files. All JSON files are stored in a /// configurable folder. /// </summary> public void CompileAllMoviesSchedule() { string sql = ""; MovieCatalog mc = new MovieCatalog(); mc.theaters = new List <MovieTheaterInfo>(); mc.formats = new List <MovieFormatInfo>(); mc.movies = new List <MovieShortInfo>(); mc.theaterMovies = new Dictionary <string, List <MovieShortInfo> >(); sql += "select * from vw_datospelicula "; sql += "where activo = 'S' "; sql += "order by idPelicula, idTeatro, frecuencia, idformato "; List <MovieLookup> movieList = new List <MovieLookup>(); HandleDatabase hdb = new HandleDatabase(dbConnection); hdb.Open(); SqlTransaction transaction = hdb.BeginTransaction("CompileAllMoviesSchedule"); SqlDataReader rdr = hdb.ExecSelectSQLStmtAsReader(transaction, sql); int oldMovie = -1; int newMovie = 0; int oldTheater = -1; int newTheater = 0; int oldFormat = -1; int newFormat = 0; bool doMovieRecord = false; MovieLookup m = null; MovieLookupLocation movieLocation = null; while (rdr.Read()) { newMovie = Convert.ToInt32(rdr["idPelicula"]); newFormat = Convert.ToInt32(rdr["idFormato"]); if (oldMovie != newMovie) { // Create Movie detail when movie break control is met. oldMovie = newMovie; oldFormat = newFormat; doMovieRecord = true; } else { if (oldFormat != newFormat) { oldFormat = newFormat; doMovieRecord = true; } } if (doMovieRecord) { doMovieRecord = false; oldTheater = -1; m = new MovieLookup(); m.id = newMovie; m.name = rdr["nombrePelicula"].ToString(); m.idFormat = newFormat; m.format = rdr["nombreFormato"].ToString(); m.nameFull = m.name + " " + m.format; m.img = rdr["imagenCartelera"].ToString(); m.url = rdr["urlArticuloEC"].ToString(); m.active = rdr["activo"].ToString(); m.idGenre = Convert.ToInt32(rdr["idGeneroPelicula"]); m.genre = rdr["nombreGenero"].ToString(); movieList.Add(m); m.locations = new List <MovieLookupLocation>(); } newTheater = Convert.ToInt32(rdr["idTeatro"]); if (oldTheater != newTheater) { oldTheater = newTheater; movieLocation = new MovieLookupLocation(); movieLocation.schedule = new List <MovieLookupShow>(); movieLocation.id = newTheater; movieLocation.name = rdr["nombreCine"].ToString(); movieLocation.branchName = rdr["nombreTeatro"].ToString(); movieLocation.nameFull = movieLocation.name + " " + movieLocation.branchName; movieLocation.address = rdr["direccionTeatro"].ToString(); m.locations.Add(movieLocation); } // Now Load Location Hours MovieLookupShow mls = new MovieLookupShow(); mls.id = Convert.ToInt32(rdr["idHorarioPelicula"]); mls.frequency = Convert.ToInt32(rdr["frecuencia"]); mls.name = rdr["nombreDiaSemanaHorarioPelicula"].ToString(); mls.year = Convert.ToInt32(rdr["annoHorarioPelicula"]); mls.month = Convert.ToInt32(rdr["mesHorarioPelicula"]); mls.day = Convert.ToInt32(rdr["diaHorarioPelicula"]); mls.hours = LoadHoursFor(mls.id); movieLocation.schedule.Add(mls); } rdr.Close(); transaction.Commit(); hdb.Close(); // Up to this point we have all movies gathered, now we build the movie Catalog in order // to ease search manipulation when required and to speed data processing. // 1. Load Movie Name list var movieNameList = (from it in movieList orderby it.active descending, it.nameFull select new MovieShortInfo() { id = it.id, idFormat = it.idFormat, name = it.name, nameFull = it.nameFull }).ToList <MovieShortInfo>(); mc.movies = movieNameList; // 2. Movie Formats Name list var movieFormatNameList = (from it in movieList orderby it.format select new MovieFormatInfo() { id = it.idFormat, name = it.format } ).ToList <MovieFormatInfo>(); List <MovieFormatInfo> formatListFiltered = new List <MovieFormatInfo>(); oldFormat = -1; newFormat = 0; foreach (var item in movieFormatNameList) { newFormat = item.id; if (oldFormat != newFormat) { oldFormat = newFormat; formatListFiltered.Add(new MovieFormatInfo() { id = item.id, name = item.name }); } } mc.formats = formatListFiltered; // 3. Movie Genre Name list var movieGenreNameList = (from it in movieList orderby it.genre select new MovieGenreInfo { id = it.idGenre, name = it.genre }).ToList <MovieGenreInfo>(); List <MovieGenreInfo> genreListFiltered = new List <MovieGenreInfo>(); int oldGenre = -1; int newGenre = 0; foreach (var item in movieGenreNameList) { newGenre = item.id; if (oldGenre != newGenre) { oldGenre = newGenre; genreListFiltered.Add(new MovieGenreInfo() { id = item.id, name = item.name }); } } mc.genres = genreListFiltered; // 4. Load all theaters referenced in the schedule. foreach (var movieItem in movieList) { foreach (var loc in movieItem.locations) { MovieTheaterInfo mti = new MovieTheaterInfo(); mti.id = loc.id; mti.name = loc.name; mti.branchName = loc.branchName; mti.nameFull = loc.nameFull; mc.theaters.Add(mti); } } // Lets order by id mc.theaters = (from t in mc.theaters orderby t.id select t).ToList <MovieTheaterInfo>(); // We now have theater names, but not sorted and not distinct. Let's sort by name excluding repeated. List <MovieTheaterInfo> allTheatersDistinctList = new List <MovieTheaterInfo>(); oldTheater = -1; newTheater = 0; foreach (var t in mc.theaters) { newTheater = t.id; if (oldTheater != newTheater) { MovieTheaterInfo mti = new MovieTheaterInfo(); mti.id = t.id; mti.name = t.name; mti.branchName = t.branchName; mti.nameFull = t.nameFull; allTheatersDistinctList.Add(mti); oldTheater = newTheater; } } // Now list has all distinct IDs, now let's sort by name mc.theaters = (from t in allTheatersDistinctList orderby t.nameFull select t).Distinct().ToList <MovieTheaterInfo>(); // 5. Load Movie Name List and its associated theaters where each movie is being shown. Dictionary <string, List <MovieShortInfo> > theaterMovies = new Dictionary <string, List <MovieShortInfo> >(); // Initialize theaterMovies for computation. mc.theaters.ForEach(t => theaterMovies.Add(t.nameFull, new List <MovieShortInfo>())); foreach (var movieItem in movieList) { int currentMovieId = movieItem.id; foreach (var loc in movieItem.locations) { var theaterItem = theaterMovies[loc.nameFull]; bool movieExists = false; foreach (var movieShortInfo in theaterItem) { if (movieShortInfo.id == currentMovieId) { movieExists = true; break; } } if (!movieExists) { theaterItem.Add(new MovieShortInfo() { id = currentMovieId, idFormat = movieItem.idFormat, name = movieItem.name, nameFull = movieItem.nameFull }); } } } mc.theaterMovies = theaterMovies; // Serialize string movieLookupJSON = JsonConvert.SerializeObject(movieList); string movieCatalogJSON = JsonConvert.SerializeObject(mc); // Now that we have just gathered all the information, create static JSON versions // Now there are two files to consume the feed // Full movie catalog (mapped from origin). string fileName = moviesFileName; using (StreamWriter writer = new StreamWriter(fileName)) { writer.Write(movieLookupJSON); } fileName = catalogNameFileName; using (StreamWriter writer = new StreamWriter(fileName)) { writer.Write(movieCatalogJSON); } }