/// <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); }
private void LoadDetailedData(string m, ref string f, ref string t) { DateTime now = DateTime.Now; string s = ""; int movieId = Convert.ToInt32(m); int movieTheaterId = Convert.ToInt32(t); int movieFormatId = Convert.ToInt32(f); // Always in variable movieListFiltered we compute the records to show. List <MovieLookup> movieListFiltered = null; if (movieId != 0 && movieTheaterId == -1 && movieFormatId == -1) { // Select only movies movieListFiltered = (from it in movieList where it.id == movieId select it).ToList(); } else { // Select only movies and formats if (movieId != 0 && movieFormatId != -1 && movieTheaterId != -1) { // At this point all fiters are selected, that is, // movieId <> 0, movieFormatId <> -1 and movieTheaterId <> -1 var movieFormatListFiltered = (from it in movieList where it.id == movieId && it.idFormat == movieFormatId select it).ToList <MovieLookup>(); movieListFiltered = new List <MovieLookup>(); foreach (var mv in movieFormatListFiltered) { foreach (var loc in mv.locations) { if (loc.id == movieTheaterId) { // We must only add the location being looked up. MovieLookup mvFiltered = new MovieLookup(); mvFiltered.id = mv.id; mvFiltered.name = mv.name; mvFiltered.idFormat = mv.idFormat; mvFiltered.format = mv.format; mvFiltered.nameFull = mv.name; mvFiltered.img = mv.img; mvFiltered.url = mv.url; mvFiltered.active = mv.active; mvFiltered.idGenre = mv.idGenre; mvFiltered.genre = mv.genre; mvFiltered.locations = new List <MovieLookupLocation>(); mvFiltered.locations.Add(loc); movieListFiltered.Add(mvFiltered); } } } } else { if (movieId != 0 && movieTheaterId != -1) { // Select only movies and the theater desired var movieAllList = (from it in movieList where it.id == movieId select it).ToList(); movieListFiltered = new List <MovieLookup>(); foreach (var mv in movieAllList) { foreach (var loc in mv.locations) { if (loc.id == movieTheaterId) { // We must only add the location being looked up. MovieLookup mvFiltered = new MovieLookup(); mvFiltered.id = mv.id; mvFiltered.name = mv.name; mvFiltered.idFormat = mv.idFormat; mvFiltered.format = mv.format; mvFiltered.nameFull = mv.name; mvFiltered.img = mv.img; mvFiltered.url = mv.url; mvFiltered.active = mv.active; mvFiltered.idGenre = mv.idGenre; mvFiltered.genre = mv.genre; mvFiltered.locations = new List <MovieLookupLocation>(); mvFiltered.locations.Add(loc); movieListFiltered.Add(mvFiltered); } } } } else { if (movieId != 0 && movieFormatId != -1) { movieListFiltered = (from it in movieList where it.id == movieId && it.idFormat == movieFormatId select it).ToList(); } } } } if (movieListFiltered != null) { foreach (var it in movieListFiltered) { foreach (var loc in it.locations) { //s += "<div class='address-mc'><p>" + it.format + " - " + loc.nameFull + " - " + loc.address + "</p></div>"; s += "<div class='address-mc'>"; s += "<p>" + loc.nameFull + "</p>"; s += "<p>" + loc.address + "</p>"; if (movieTheaterId == -1) { s += "<p>" + it.format + "</p>"; } s += "</div>"; // Now schedule for this theater. foreach (var sc in loc.schedule) { DateTime dt = new DateTime(sc.year, sc.month, sc.day); //if (dt < now) //{ // s += "<div class='list-days old'>"; //} //else //{ s += "<div class='list-days'>"; //} s += "<p>" + sc.name + "<span>" + sc.day.ToString().PadLeft(2, '0') + " de " + MonthToName(sc.month) + " de " + sc.year + "</span></p>"; s += "<ul class='clearfix'>"; if (sc.hours.Count == 1) { int i = 0; var h = sc.hours[i]; DateTime dtProgrammed = new DateTime(sc.year, sc.month, sc.day, h.timeHour, h.timeMinute, 0); if (DateTime.Compare(dtProgrammed, now) < 0) { if (i == 0) { s += "<li class='last old'>"; } else { s += "<li class='old'>"; } } else { if (i == 0) { s += "<li class='last'>"; } else { s += "<li>"; } } s += h.timeFull + "</li>"; } else { DateTime dtProgrammed; for (int i = 0; i < sc.hours.Count; i++) { var h = sc.hours[i]; dtProgrammed = new DateTime(sc.year, sc.month, sc.day, h.timeHour, h.timeMinute, 0); if (DateTime.Compare(dtProgrammed, now) < 0) { if (i == 0) { s += "<li class='first old'>"; } else if (i == sc.hours.Count - 1) { s += "<li class='last old'>"; } else { s += "<li class='old'>"; } } else { if (i == 0) { s += "<li class='first'>"; } else if (i == sc.hours.Count - 1) { s += "<li class='last'>"; } else { s += "<li>"; } } s += h.timeFull + "</li>"; } } s += "</ul>"; s += "</div>"; } } s += "<div class='bg-footer'></div>"; } } movieAllData.Text = s; }
/// <summary> /// Given parameters 't:for theater id', 'm:Movie Id', and 'g:Gender id', computes a lookup in the catalog /// matched records. /// </summary> /// <param name="t">Theater Id. Possible value are '-1', or '2'</param> /// <param name="m">Movie Id. Possible value are '-1', or '1|29' this means movieId=1 and movieIdFormat=29</param> /// <param name="g">Gender Id. Possible value are '-1', or '2'</param> /// <returns>A list of 'MovieLookup' objects serialized as JSON. If list is empty, serialized should be empty.</returns> public string Search(string t, string m, string g) { int theaterId = Convert.ToInt32(t); int movieId = 0; int movieIdFormat = 0; // Only used where movieId is not set to -1 int genreId = Convert.ToInt32(g); string rslt = ""; // We must split 'm' parameter to its constituent values. // If m = -1", no need to split if (m == "-1") { movieId = Convert.ToInt32(m); } else { var movieValuesArray = m.Split('|'); movieId = Convert.ToInt32(movieValuesArray[0]); movieIdFormat = Convert.ToInt32(movieValuesArray[1]); } List <MovieLookup> movieList = RetrieveMovieList(); if (theaterId == -1 && movieId == -1 && genreId == -1) { // Retrieves all records. var movieListOrdered = (from it in movieList orderby it.nameFull select it).ToList(); int oldMovie = -1; int newMovie = 0; List <MovieLookup> movieListSearch = new List <MovieLookup>(); foreach (var mv in movieListOrdered) { newMovie = mv.id; if (oldMovie != newMovie) { oldMovie = newMovie; movieListSearch.Add(mv); } } movieListSearch = (from it in movieListSearch orderby it.nameFull select it).ToList(); rslt = JsonConvert.SerializeObject(movieListSearch.ToList()); } else { if (theaterId == -1 && movieId == -1 && genreId != -1) { // Search only by Genre var movieListOrdered = (from it in movieList orderby it.nameFull where it.idGenre == genreId select it).ToList(); int oldMovie = -1; int newMovie = 0; List <MovieLookup> movieListSearch = new List <MovieLookup>(); foreach (var mv in movieListOrdered) { newMovie = mv.id; if (oldMovie != newMovie) { oldMovie = newMovie; movieListSearch.Add(mv); } } movieListSearch = (from it in movieListSearch orderby it.nameFull select it).ToList(); rslt = JsonConvert.SerializeObject(movieListSearch.ToList()); } else { if (theaterId != -1 && movieId != -1 && genreId == -1) { // Search a specific movie by theater. Any Genre. // No idFormat is required herein. // 1. Select movies var movieListOrdered = (from it in movieList orderby it.nameFull where it.id == movieId select it).ToList(); // 2. Select theater bool theaterFound = false; MovieLookup mvSelected = null; foreach (var mv in movieListOrdered) { var loc = (from l in mv.locations where l.id == theaterId select l).FirstOrDefault(); if (loc != null) { theaterFound = true; mvSelected = mv; break; } } List <MovieLookup> movieListSearch = new List <MovieLookup>(); if (theaterFound) { movieListSearch.Add(mvSelected); } movieListSearch = (from it in movieListSearch orderby it.nameFull select it).ToList(); rslt = JsonConvert.SerializeObject(movieListSearch.ToList()); } else { if (theaterId == -1 && movieId != -1 && genreId == -1) { // Only one movie must be selected. // Movie record must match criteria for movieId and movieIdFormat as well var mvSelected = (from it in movieList orderby it.nameFull where it.id == movieId && it.idFormat == movieIdFormat select it).FirstOrDefault(); List <MovieLookup> movieListSearch = new List <MovieLookup>(); movieListSearch.Add(mvSelected); movieListSearch = (from it in movieListSearch orderby it.nameFull select it).ToList(); rslt = JsonConvert.SerializeObject(movieListSearch.ToList()); } else { if (theaterId != -1 && movieId == -1 && genreId == -1) { // Find all movies by given theaterId // Remove duplicates in the process. List <MovieLookup> movieListSearch = new List <MovieLookup>(); foreach (var mv in movieList) { foreach (var loc in mv.locations) { if (loc.id == theaterId) { bool movieExists = false; int curMovieId = mv.id; foreach (var ml in movieListSearch) { if (curMovieId == ml.id) { movieExists = true; } } if (!movieExists) { movieListSearch.Add(mv); } } } } // Order by nameFull field. movieListSearch = (from it in movieListSearch orderby it.nameFull select it).ToList(); rslt = JsonConvert.SerializeObject(movieListSearch.ToList()); } } } } } return(rslt); }
/// <summary> /// It makes a search for movies that meets the search criteria. /// Receives three parameters. /// t=Theater: Holds the theater to look for. /// m=Movie: Holds the movie to look for. /// g=Genre: Holds the genre to look for. /// /// NOTE: Consumes the own EL COLOMBIANO JSON Format. /// </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) { string movieSearchListJSON = ""; string fileNameMovies = @"D:\SitiosWeb\Sitio\EC100A_Servicios\EC100A_PlanepolyWidget\planepoly-movies.json"; string t = Request.QueryString["t"]; string m = Request.QueryString["m"]; string g = Request.QueryString["g"]; string s; using (StreamReader reader = new StreamReader(fileNameMovies)) { s = reader.ReadToEnd(); } if ((t == null && m == null & g == null) || (t == "-1" && m == "-1" && g == "-1")) { // Returns all movies available. // All movies are expanded in order to give to each movie all of its locations. List <MovieLookup> movieLookupList = JsonConvert.DeserializeObject <List <MovieLookup> >(s); List <MovieLookup> movieLookupAllList = new List <MovieLookup>(); foreach (var movieInfo in movieLookupList) { MovieLookup ml = new MovieLookup(); ml.name = movieInfo.name; ml.img = movieInfo.img; ml.url = movieInfo.url; ml.premiere = movieInfo.premiere; ml.genre = movieInfo.genre; movieLookupAllList.Add(ml); } movieLookupAllList = (from item in movieLookupAllList orderby item.premiere descending, item.name select item).Distinct().ToList(); string allMoviesJSON = JsonConvert.SerializeObject(movieLookupAllList); movieSearchListJSON = allMoviesJSON; } else { List <MovieLookup> movieSearchList = new List <MovieLookup>(); List <MovieLookup> movieLookupList = JsonConvert.DeserializeObject <List <MovieLookup> >(s); List <MovieLookup> movieByGenre = null; List <MovieLookup> movieByMovie = null; List <MovieLookup> movieByTheater = null; List <MovieLookup> movieByTheaterMovie = null; if (t == "-1" && m == "-1" && g != "-1") { // Search only by genre var movieGenreList = (from item in movieLookupList where item.genre == g select item).ToList(); // Now we have all movies by genre, now it is needed to expand by location. movieByGenre = new List <MovieLookup>(); foreach (var ml in movieGenreList) { // Sort locations var sortedLocations = (from l in ml.locations orderby l.name select l).ToList(); foreach (var l in sortedLocations) { MovieLookup movieLookupInfo = new MovieLookup(); movieLookupInfo.name = ml.name; movieLookupInfo.img = ml.img; movieLookupInfo.url = ml.url; movieLookupInfo.premiere = ml.premiere; movieLookupInfo.genre = ml.genre; List <MovieLookupLocation> foundMovieLocationList = new List <MovieLookupLocation>(); MovieLookupLocation loc = new MovieLookupLocation(); loc.name = l.name; loc.address = l.address; loc.schedule = l.schedule; foundMovieLocationList.Add(loc); movieLookupInfo.locations = foundMovieLocationList; movieByGenre.Add(movieLookupInfo); } } movieByGenre = (from it in movieByGenre orderby it.premiere descending, it.name select it).Distinct().ToList(); } else { if (t != "-1" && m != "-1") { // Search both theater and movie in that theater movieByTheaterMovie = new List <MovieLookup>(); foreach (var ml in movieLookupList) { var sortedLocations = (from loc in ml.locations orderby loc.name select loc).Distinct().ToList(); foreach (var l in sortedLocations) { if (l.name == t && ml.name == m) { MovieLookup foundMovie = new MovieLookup(); foundMovie.name = ml.name; foundMovie.img = ml.img; foundMovie.url = ml.url; foundMovie.premiere = ml.premiere; foundMovie.genre = ml.genre; List <MovieLookupLocation> foundMovieLocationList = new List <MovieLookupLocation>(); foundMovieLocationList.Add(l); foundMovie.locations = foundMovieLocationList; movieByTheaterMovie.Add(foundMovie); } } } } else { if (t == "-1" && m != "-1") { // Search only by movie name (this gives many movies in different locations --aka theaters). movieByMovie = new List <MovieLookup>(); var existingMoviesList = (from item in movieLookupList where item.name == m select item).ToList(); // The existing movie must be expanded one item by all of its locations. foreach (var ml in existingMoviesList) { var sortedLocations = (from loc in ml.locations orderby loc.name select loc).Distinct().ToList(); foreach (var l in sortedLocations) { MovieLookup expando = new MovieLookup(); expando.name = ml.name; expando.img = ml.img; expando.url = ml.url; expando.premiere = ml.premiere; expando.genre = ml.genre; List <MovieLookupLocation> expandoLocationList = new List <MovieLookupLocation>(); expandoLocationList.Add(l); expando.locations = expandoLocationList; movieByMovie.Add(expando); } } movieByMovie = (from ml in movieByMovie orderby ml.premiere descending, ml.name select ml).Distinct().ToList(); } else { // Search only by theater movieByTheater = new List <MovieLookup>(); foreach (var ml in movieLookupList) { var sortedLocations = (from loc in ml.locations orderby loc.name select loc).Distinct().ToList(); foreach (var l in sortedLocations) { if (l.name == t) { MovieLookup foundMovie = new MovieLookup(); foundMovie.name = ml.name; foundMovie.img = ml.img; foundMovie.url = ml.url; foundMovie.premiere = ml.premiere; foundMovie.genre = ml.genre; List <MovieLookupLocation> foundMovieLocationList = new List <MovieLookupLocation>(); foundMovieLocationList.Add(l); foundMovie.locations = foundMovieLocationList; movieByTheater.Add(foundMovie); } } } } } } if (movieByGenre != null) { movieSearchList.AddRange(movieByGenre); } if (movieByTheater != null) { movieSearchList.AddRange(movieByTheater); } if (movieByTheaterMovie != null) { movieSearchList.AddRange(movieByTheaterMovie); } if (movieByMovie != null) { movieSearchList.AddRange(movieByMovie); } movieSearchList = (from item in movieSearchList orderby item.premiere descending, item.name select item).Distinct().ToList(); movieSearchListJSON = JsonConvert.SerializeObject(movieSearchList); } Response.Write(movieSearchListJSON); Response.AddHeader("Access-Control-Allow-Origin", "*"); }
/// <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); } }