Pojo classes film database

This is a code example of iText PDF, discover more.

10th October 2015
admin-marketing

Switch code for this example

Movie.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package com.lowagie.filmfestival;
  9.  
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. /**
  14.  * POJO for an object that corresponds with a record
  15.  * in the table film_movietitle.
  16.  */
  17. public class Movie implements Comparable {
  18.    
  19.     /** The title of the movie. */
  20.     protected String title;
  21.     /** The original title (if different from title). */
  22.     protected String originalTitle = null;
  23.     /** Code used by IMDB */
  24.     protected String imdb;
  25.     /** The year the movie was released. */
  26.     protected int year;
  27.     /** The duration of the movie in minutes. */
  28.     protected int duration;
  29.     /** The list of directors. */
  30.     protected List directors = new ArrayList();
  31.     /** The list of countries. */
  32.     protected List countries= new ArrayList();
  33.     /** The filmfestival entry info. */
  34.     protected Entry entry = null;
  35.    
  36.     /**
  37.      * Adds a director.
  38.      * @param director one of the directors of the movie
  39.      */
  40.     public void addDirector(Director director) {
  41.         directors.add(director);
  42.     }
  43.    
  44.     /**
  45.      * Adds a country.
  46.      * @param country  one of the countries the movie was made by.
  47.      */
  48.     public void addCountry(Country country) {
  49.         countries.add(country);
  50.     }
  51.    
  52.     /**
  53.      * Returns the title in the correct form.
  54.      * @return a title
  55.      */
  56.     public String getMovieTitle() {
  57.         if (title.endsWith(", A"))
  58.             return "A " + title.substring(0, title.length() - 3);
  59.         if (title.endsWith(", The"))
  60.             return "The " + title.substring(0, title.length() - 5);
  61.         return title;
  62.     }
  63.    
  64.     /**
  65.      * Returns the title in the correct form.
  66.      * @return a title
  67.      */
  68.     public String getMovieTitle(boolean prefix) {
  69.         if (title.endsWith(", A"))
  70.             if (prefix)
  71.                 return "A ";
  72.             else
  73.                 return title.substring(0, title.length() - 3);
  74.         if (title.endsWith(", The"))
  75.             if (prefix)
  76.                 return "The ";
  77.             else
  78.                 return title.substring(0, title.length() - 5);
  79.         if (prefix)
  80.             return null;
  81.         else
  82.             return title;
  83.     }
  84.    
  85.     /**
  86.      * @return the title
  87.      */
  88.     public String getTitle() {
  89.         return title;
  90.     }
  91.     /**
  92.      * @param title the title to set
  93.      */
  94.     public void setTitle(String title) {
  95.         this.title = title;
  96.     }
  97.     /**
  98.      * @return the originalTitle
  99.      */
  100.     public String getOriginalTitle() {
  101.         return originalTitle;
  102.     }
  103.     /**
  104.      * @param originalTitle the originalTitle to set
  105.      */
  106.     public void setOriginalTitle(String originalTitle) {
  107.         this.originalTitle = originalTitle;
  108.     }
  109.     /**
  110.      * @return the imdb
  111.      */
  112.     public String getImdb() {
  113.         return imdb;
  114.     }
  115.     /**
  116.      * @param imdb the imdb to set
  117.      */
  118.     public void setImdb(String imdb) {
  119.         this.imdb = imdb;
  120.     }
  121.     /**
  122.      * @return the year
  123.      */
  124.     public int getYear() {
  125.         return year;
  126.     }
  127.     /**
  128.      * @param year the year to set
  129.      */
  130.     public void setYear(int year) {
  131.         this.year = year;
  132.     }
  133.     /**
  134.      * @return the duration
  135.      */
  136.     public int getDuration() {
  137.         return duration;
  138.     }
  139.     /**
  140.      * @param duration the duration to set
  141.      */
  142.     public void setDuration(int duration) {
  143.         this.duration = duration;
  144.     }
  145.     /**
  146.      * @return the directors
  147.      */
  148.     public List getDirectors() {
  149.         return directors;
  150.     }
  151.     /**
  152.      * @return the countries
  153.      */
  154.     public List getCountries() {
  155.         return countries;
  156.     }
  157.     /**
  158.      * @return the entry
  159.      */
  160.     public Entry getEntry() {
  161.         return entry;
  162.     }
  163.     /**
  164.      * @param entry the entry to set
  165.      */
  166.     public void setEntry(Entry entry) {
  167.         this.entry = entry;
  168.         if (entry.getMovie() == null)
  169.             entry.setMovie(this);
  170.     }
  171.  
  172.     public int compareTo(Movie o) {
  173.         return title.compareTo(o.title);
  174.     }
  175.    
  176. }
Director.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package com.lowagie.filmfestival;
  9.  
  10. /**
  11.  * POJO for an object that corresponds with a record
  12.  * in the table film_director.
  13.  */
  14. public class Director {
  15.    
  16.     /** The family name of the director. */
  17.     protected String name;
  18.     /** The given name of the director. */
  19.     protected String givenName;
  20.    
  21.     /**
  22.      * @return the name
  23.      */
  24.     public String getName() {
  25.         return name;
  26.     }
  27.     /**
  28.      * @param name the name to set
  29.      */
  30.     public void setName(String name) {
  31.         this.name = name;
  32.     }
  33.     /**
  34.      * @return the givenName
  35.      */
  36.     public String getGivenName() {
  37.         return givenName;
  38.     }
  39.     /**
  40.      * @param givenName the givenName to set
  41.      */
  42.     public void setGivenName(String givenName) {
  43.         this.givenName = givenName;
  44.     }
  45. }
Country.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package com.lowagie.filmfestival;
  9.  
  10. /**
  11.  * POJO for an object that corresponds with a record
  12.  * in the table film_country.
  13.  */
  14. public class Country {
  15.     /** The name of a country. */
  16.     protected String country;
  17.  
  18.     /**
  19.      * @return the country
  20.      */
  21.     public String getCountry() {
  22.         return country;
  23.     }
  24.  
  25.     /**
  26.      * @param country the country to set
  27.      */
  28.     public void setCountry(String country) {
  29.         this.country = country;
  30.     }
  31. }
Entry.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package com.lowagie.filmfestival;
  9.  
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. /**
  14.  * POJO for an object that corresponds with a record
  15.  * in the table festival_entry.
  16.  */
  17. public class Entry {
  18.    
  19.     /** The festival year. */
  20.     protected int year;
  21.     /** The movie. */
  22.     protected Movie movie;
  23.     /** The category. */
  24.     protected Category category;
  25.     /** The screenings. */
  26.     protected List screenings = new ArrayList();
  27.    
  28.     /**
  29.      * Adds a screening to this entry.
  30.      */
  31.     public void addScreening(Screening screening) {
  32.         screenings.add(screening);
  33.     }
  34.    
  35.     /**
  36.      * @return the year
  37.      */
  38.     public int getYear() {
  39.         return year;
  40.     }
  41.     /**
  42.      * @param year the year to set
  43.      */
  44.     public void setYear(int year) {
  45.         this.year = year;
  46.     }
  47.     /**
  48.      * @return the movie
  49.      */
  50.     public Movie getMovie() {
  51.         return movie;
  52.     }
  53.     /**
  54.      * @param movie the movie to set
  55.      */
  56.     public void setMovie(Movie movie) {
  57.         this.movie = movie;
  58.         if (movie.getEntry() == null)
  59.             movie.setEntry(this);
  60.     }
  61.     /**
  62.      * @return the category
  63.      */
  64.     public Category getCategory() {
  65.         return category;
  66.     }
  67.     /**
  68.      * @param category the category to set
  69.      */
  70.     public void setCategory(Category category) {
  71.         this.category = category;
  72.     }
  73.     /**
  74.      * @return the screenings
  75.      */
  76.     public List getScreenings() {
  77.         return screenings;
  78.     }
  79. }
Category.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package com.lowagie.filmfestival;
  9.  
  10. /**
  11.  * POJO for an object that corresponds with a record
  12.  * in the table festival_category.
  13.  */
  14. public class Category {
  15.    
  16.     /** The name of the category. */
  17.     protected String name;
  18.     /** A short keyword for the category. */
  19.     protected String keyword;
  20.     /** The color code of the category. */
  21.     protected String color;
  22.     /** The parent category (if any). */
  23.     protected Category parent = null;
  24.    
  25.     /**
  26.      * @return the name
  27.      */
  28.     public String getName() {
  29.         return name;
  30.     }
  31.     /**
  32.      * @param name the name to set
  33.      */
  34.     public void setName(String name) {
  35.         this.name = name;
  36.     }
  37.     /**
  38.      * @return the keyword
  39.      */
  40.     public String getKeyword() {
  41.         return keyword;
  42.     }
  43.     /**
  44.      * @param keyword the keyword to set
  45.      */
  46.     public void setKeyword(String keyword) {
  47.         this.keyword = keyword;
  48.     }
  49.     /**
  50.      * @return the parent
  51.      */
  52.     public Category getParent() {
  53.         return parent;
  54.     }
  55.     /**
  56.      * @param parent the parent to set
  57.      */
  58.     public void setParent(Category parent) {
  59.         this.parent = parent;
  60.     }
  61.     /**
  62.      * @return the color
  63.      */
  64.     public String getColor() {
  65.         return color;
  66.     }
  67.     /**
  68.      * @param color the color to set
  69.      */
  70.     public void setColor(String color) {
  71.         this.color = color;
  72.     }
  73. }
Screening.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package com.lowagie.filmfestival;
  9.  
  10. import java.sql.Date;
  11. import java.sql.Time;
  12.  
  13. /**
  14.  * POJO for an object that corresponds with a record
  15.  * in the table festival_screening.
  16.  */
  17. public class Screening {
  18.     /** The date of the screening. */
  19.     protected Date date;
  20.     /** The time of the screening. */
  21.     protected Time time;
  22.     /** The location of the screening. */
  23.     protected String location;
  24.     /** Is this a screening for the press only? */
  25.     protected boolean press;
  26.     /** The movie that will be screened. */
  27.     protected Movie movie = null;
  28.    
  29.     /**
  30.      * @return the date
  31.      */
  32.     public Date getDate() {
  33.         return date;
  34.     }
  35.     /**
  36.      * @param date the date to set
  37.      */
  38.     public void setDate(Date date) {
  39.         this.date = date;
  40.     }
  41.     /**
  42.      * @return the time
  43.      */
  44.     public Time getTime() {
  45.         return time;
  46.     }
  47.     /**
  48.      * @param time the time to set
  49.      */
  50.     public void setTime(Time time) {
  51.         this.time = time;
  52.     }
  53.     /**
  54.      * @return the location
  55.      */
  56.     public String getLocation() {
  57.         return location;
  58.     }
  59.     /**
  60.      * @param location the location to set
  61.      */
  62.     public void setLocation(String location) {
  63.         this.location = location;
  64.     }
  65.     /**
  66.      * @return the press
  67.      */
  68.     public boolean isPress() {
  69.         return press;
  70.     }
  71.     /**
  72.      * @param press the press to set
  73.      */
  74.     public void setPress(boolean press) {
  75.         this.press = press;
  76.     }
  77.     /**
  78.      * @return the movie
  79.      */
  80.     public Movie getMovie() {
  81.         return movie;
  82.     }
  83.     /**
  84.      * @param movie the movie to set
  85.      */
  86.     public void setMovie(Movie movie) {
  87.         this.movie = movie;
  88.     }
  89. }
PojoFactory.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package com.lowagie.filmfestival;
  9.  
  10. import java.io.UnsupportedEncodingException;
  11. import java.sql.Date;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18.  
  19. import com.lowagie.database.DatabaseConnection;
  20.  
  21. /**
  22.  * A factory that makes it easy to query the database using
  23.  * a series of static methods.
  24.  */
  25. public class PojoFactory {
  26.     /** SQL statement to get all the movies of the festival. */
  27.     public static final String MOVIES =
  28.         "SELECT m.id, m.title, m.original_title, m.imdb, m.year, m.duration, "
  29.         + "e.year, c.name, c.keyword, c.color "
  30.         + "FROM film_movietitle m, festival_entry e, festival_category c "
  31.         + "WHERE m.id = e.film_id AND e.category_id = c.id "
  32.         + "ORDER BY m.title";
  33.     /** SQL statement to get the directors of a specific movie. */
  34.     public static final String DIRECTORS =
  35.         "SELECT d.name, d.given_name "
  36.         + "FROM film_director d, film_movie_director md "
  37.         + "WHERE md.film_id = ? AND md.director_id = d.id";
  38.     /** SQL statement to get the movies of a specific director. */
  39.     public static final String MOVIEDIRECTORS =
  40.         "SELECT m.id, m.title, m.original_title, m.imdb, m.year, m.duration "
  41.         + "FROM film_movietitle m, film_movie_director md "
  42.         + "WHERE md.director_id = ? AND md.film_id = m.id "
  43.         + "ORDER BY m.title";
  44.     /** SQL statement to get the countries of a specific movie. */
  45.     public static final String COUNTRIES =
  46.         "SELECT c.country "
  47.         + "FROM film_country c, film_movie_country mc "
  48.         + "WHERE mc.film_id = ? AND mc.country_id = c.id";
  49.     /** SQL statement to get the movies from a specific country. */
  50.     public static final String MOVIECOUNTRIES =
  51.         "SELECT m.id, m.title, m.original_title, m.imdb, m.year, m.duration "
  52.         + "FROM film_movietitle m, film_movie_country mc "
  53.         + "WHERE mc.country_id = ? AND mc.film_id = m.id "
  54.         + "ORDER BY m.title";
  55.     /** SQL statement to get all the days of the festival. */
  56.     public static final String DAYS =
  57.         "SELECT DISTINCT day FROM festival_screening ORDER BY day";
  58.     /** SQL statament to get all the locations at the festival */
  59.     public static final String LOCATIONS =
  60.         "SELECT DISTINCT location FROM festival_screening ORDER by location";
  61.     /** SQL statement to get screenings. */
  62.     public static final String SCREENINGS =
  63.         "SELECT m.title, m.original_title, m.imdb, m.year, m.duration,"
  64.         + "s.day, s.time, s.location, s.press, "
  65.         + "e.year, c.name, c.keyword, c.color, m.id "
  66.         + "FROM festival_screening s, film_movietitle m, "
  67.         + "festival_entry e, festival_category c "
  68.         + "WHERE day = ? AND s.film_id = m.id "
  69.         + "AND m.id = e.film_id AND e.category_id = c.id";
  70.     /** SQL statement to get screenings. */
  71.     public static final String MOVIESCREENINGS =
  72.         "SELECT s.day, s.time, s.location, s.press "
  73.         + "FROM festival_screening s "
  74.         + "WHERE s.film_id = ?";
  75.     /** SQL statement to get screenings. */
  76.     public static final String PRESS =
  77.         "SELECT m.title, m.original_title, m.imdb, m.year, m.duration,"
  78.         + "s.day, s.time, s.location, s.press, "
  79.         + "e.year, c.name, c.keyword, c.color, m.id "
  80.         + "FROM festival_screening s, film_movietitle m, "
  81.         + "festival_entry e, festival_category c "
  82.         + "WHERE s.press=1 AND s.film_id = m.id "
  83.         + "AND m.id = e.film_id AND e.category_id = c.id "
  84.         + "ORDER BY day, time ASC";
  85.    
  86.     /**
  87.      * Fills a Movie POJO using a ResultSet.
  88.      * @param rs a ResultSet with records from table film_movietitle
  89.      * @return a Movie POJO
  90.      * @throws UnsupportedEncodingException
  91.      */
  92.     public static Movie getMovie(ResultSet rs)
  93.         throws SQLException, UnsupportedEncodingException {
  94.         Movie movie = new Movie();
  95.         movie.setTitle(new String(rs.getBytes("title"), "UTF-8"));
  96.         if (rs.getObject("original_title") != null)
  97.             movie.setOriginalTitle(
  98.                new String(rs.getBytes("original_title"), "UTF-8"));
  99.         movie.setImdb(rs.getString("imdb"));
  100.         movie.setYear(rs.getInt("year"));
  101.         movie.setDuration(rs.getInt("duration"));
  102.         return movie;
  103.     }
  104.    
  105.     /**
  106.      * Fills a Director POJO using a ResultSet.
  107.      * @param rs a ResultSet with records from table file_director
  108.      * @return a Director POJO
  109.      */
  110.     public static Director getDirector(ResultSet rs)
  111.         throws SQLException, UnsupportedEncodingException {
  112.         Director director = new Director();
  113.         director.setName(new String(rs.getBytes("name"), "UTF-8"));
  114.         director.setGivenName(new String(rs.getBytes("given_name"),"UTF-8"));
  115.         return director;
  116.     }
  117.    
  118.     /**
  119.      * Fills a Country POJO using a ResultSet.
  120.      * @param rs a ResultSet with records from table file_director
  121.      * @return a Director POJO
  122.      */
  123.     public static Country getCountry(ResultSet rs) throws SQLException {
  124.         Country country = new Country();
  125.         country.setCountry(rs.getString("country"));
  126.         return country;
  127.     }
  128.    
  129.     /**
  130.      * Fills an Entry POJO using a ResultSet.
  131.      * @param rs a resultSet with records from table festival_entry
  132.      * @return an Entry POJO
  133.      */
  134.     public static Entry getEntry(ResultSet rs) throws SQLException {
  135.         Entry entry = new Entry();
  136.         entry.setYear(rs.getInt("year"));
  137.         return entry;
  138.     }
  139.    
  140.     /**
  141.      * Fills a Category POJO using a ResultSet.
  142.      * @param rs a resultSet with records from table festival_category
  143.      * @return a Category POJO
  144.      */
  145.     public static Category getCategory(ResultSet rs) throws SQLException {
  146.         Category category = new Category();
  147.         category.setName(rs.getString("name"));
  148.         category.setKeyword(rs.getString("keyword"));
  149.         category.setColor(rs.getString("color"));
  150.         return category;
  151.     }
  152.    
  153.     /**
  154.      * Fills a Screening POJO using a ResultSet.
  155.      * @param rs a ResultSet with records from table festival_screening
  156.      * @return a Screening POJO
  157.      */
  158.     public static Screening getScreening(ResultSet rs) throws SQLException {
  159.         Screening screening = new Screening();
  160.         screening.setDate(rs.getDate("day"));
  161.         screening.setTime(rs.getTime("time"));
  162.         screening.setLocation(rs.getString("location"));
  163.         screening.setPress(rs.getInt("press") == 1);
  164.         return screening;
  165.     }
  166.  
  167.     /**
  168.      * Returns a list with Screening objects, if you pass
  169.      * a DatabaseConnection and a day.
  170.      * @param connection a connection to the film festival database
  171.      * @param day a day (java.sql.Date)
  172.      * @return a List of Screening POJOs
  173.      * @throws UnsupportedEncodingException
  174.      */
  175.     public static List getScreenings(
  176.         DatabaseConnection connection, Date day)
  177.         throws SQLException, UnsupportedEncodingException {
  178.         List list = new ArrayList();
  179.         PreparedStatement stm =
  180.             connection.createPreparedStatement(SCREENINGS);
  181.         stm.setDate(1, day);
  182.         ResultSet rs = stm.executeQuery();
  183.         Screening screening;
  184.         Movie movie;
  185.         Entry entry;
  186.         Category category;
  187.         while (rs.next()) {
  188.             screening = getScreening(rs);
  189.             movie = getMovie(rs);
  190.             for (Director director :
  191.                 getDirectors(connection, rs.getInt("id"))) {
  192.                 movie.addDirector(director);
  193.             }
  194.             for (Country country :
  195.                 getCountries(connection, rs.getInt("id"))) {
  196.                 movie.addCountry(country);
  197.             }
  198.             entry = getEntry(rs);
  199.             category = getCategory(rs);
  200.             entry.setCategory(category);
  201.             entry.setMovie(movie);
  202.             movie.setEntry(entry);
  203.             screening.setMovie(movie);
  204.             list.add(screening);
  205.         }
  206.         stm.close();
  207.         return list;
  208.     }
  209.  
  210.     /**
  211.      * Returns a list with Screening objects, if you pass
  212.      * a DatabaseConnection and a day.
  213.      * @param connection a connection to the film festival database
  214.      * @param film_id a movie id
  215.      * @return a List of Screening POJOs
  216.      * @throws UnsupportedEncodingException
  217.      */
  218.     public static List getScreenings(
  219.         DatabaseConnection connection, int film_id)
  220.         throws SQLException, UnsupportedEncodingException {
  221.         List list = new ArrayList();
  222.         PreparedStatement stm =
  223.             connection.createPreparedStatement(MOVIESCREENINGS);
  224.         stm.setInt(1, film_id);
  225.         ResultSet rs = stm.executeQuery();
  226.         Screening screening;
  227.         while (rs.next()) {
  228.             screening = getScreening(rs);
  229.             list.add(screening);
  230.         }
  231.         stm.close();
  232.         return list;
  233.     }
  234.  
  235.     /**
  236.      * Returns a list with Screening objects, if you pass
  237.      * a DatabaseConnection and a day.
  238.      * @param connection a connection to the film festival database
  239.      * @param day a day (java.sql.Date)
  240.      * @return a List of Screening POJOs
  241.      * @throws UnsupportedEncodingException
  242.      */
  243.     public static List getPressPreviews(DatabaseConnection connection)
  244.         throws SQLException, UnsupportedEncodingException {
  245.         List list = new ArrayList();
  246.         Statement stm = connection.createStatement();
  247.         ResultSet rs = stm.executeQuery(PRESS);
  248.         Screening screening;
  249.         Movie movie;
  250.         Entry entry;
  251.         Category category;
  252.         while (rs.next()) {
  253.             screening = getScreening(rs);
  254.             movie = getMovie(rs);
  255.             for (Director director : getDirectors(connection, rs.getInt("id"))) {
  256.                 movie.addDirector(director);
  257.             }
  258.             for (Country country : getCountries(connection, rs.getInt("id"))) {
  259.                 movie.addCountry(country);
  260.             }
  261.             entry = getEntry(rs);
  262.             category = getCategory(rs);
  263.             entry.setCategory(category);
  264.             entry.setMovie(movie);
  265.             movie.setEntry(entry);
  266.             screening.setMovie(movie);
  267.             list.add(screening);
  268.         }
  269.         stm.close();
  270.         return list;
  271.     }
  272.    
  273.     /**
  274.      * Returns a list with Movie objects.
  275.      * @param connection a connection to the filmfestival database
  276.      * @return a List of Screening POJOs
  277.      * @throws SQLException
  278.      * @throws UnsupportedEncodingException
  279.      */
  280.     public static List getMovies(DatabaseConnection connection)
  281.         throws SQLException, UnsupportedEncodingException {
  282.         List list = new ArrayList();
  283.         Statement stm = connection.createStatement();
  284.         ResultSet rs = stm.executeQuery(MOVIES);
  285.         Movie movie;
  286.         Entry entry;
  287.         Category category;
  288.         while (rs.next()) {
  289.             movie = getMovie(rs);
  290.             entry = getEntry(rs);
  291.             category = getCategory(rs);
  292.             entry.setCategory(category);
  293.             for (Screening screening : getScreenings(connection, rs.getInt("id"))) {
  294.                 entry.addScreening(screening);
  295.             }
  296.             movie.setEntry(entry);
  297.             for (Director director : getDirectors(connection, rs.getInt("id"))) {
  298.                 movie.addDirector(director);
  299.             }
  300.             for (Country country : getCountries(connection, rs.getInt("id"))) {
  301.                 movie.addCountry(country);
  302.             }
  303.             list.add(movie);
  304.         }
  305.         return list;
  306.     }
  307.    
  308.     /**
  309.      * Returns a list with Director objects.
  310.      * @param connection a connection to the filmfestival database
  311.      * @param director_id the id of a movie
  312.      * @return a List of Screening POJOs
  313.      * @throws SQLException
  314.      * @throws UnsupportedEncodingException
  315.      */
  316.     public static List getDirectors(
  317.         DatabaseConnection connection, int movie_id)
  318.         throws SQLException, UnsupportedEncodingException {
  319.         List list = new ArrayList();
  320.         PreparedStatement directors =
  321.             connection.createPreparedStatement(DIRECTORS);
  322.         directors.setInt(1, movie_id);
  323.         ResultSet rs = directors.executeQuery();
  324.         while (rs.next()) {
  325.             list.add(getDirector(rs));
  326.         }
  327.         return list;
  328.     }
  329.    
  330.     /**
  331.      * Returns a list with Country objects.
  332.      * @param connection a connection to the filmfestival database
  333.      * @param movie_id the id of a movie
  334.      * @return a List of Screening POJOs
  335.      * @throws SQLException
  336.      * @throws UnsupportedEncodingException
  337.      */
  338.     public static List getCountries(
  339.         DatabaseConnection connection, int movie_id)
  340.         throws SQLException, UnsupportedEncodingException {
  341.         List list = new ArrayList();
  342.         PreparedStatement countries =
  343.             connection.createPreparedStatement(COUNTRIES);
  344.         countries.setInt(1, movie_id);
  345.         ResultSet rs = countries.executeQuery();
  346.         while (rs.next()) {
  347.             list.add(getCountry(rs));
  348.         }
  349.         return list;
  350.     }
  351.    
  352.     /**
  353.      * Returns a list with Movie objects.
  354.      * @param connection a connection to the filmfestival database
  355.      * @param director_id the id of a director
  356.      * @return a List of Screening POJOs
  357.      * @throws SQLException
  358.      * @throws UnsupportedEncodingException
  359.      */
  360.     public static List getMovies(
  361.         DatabaseConnection connection, int director_id)
  362.         throws SQLException, UnsupportedEncodingException {
  363.         List list = new ArrayList();
  364.         PreparedStatement movies =
  365.             connection.createPreparedStatement(MOVIEDIRECTORS);
  366.         movies.setInt(1, director_id);
  367.         ResultSet rs = movies.executeQuery();
  368.         Movie movie;
  369.         while (rs.next()) {
  370.             movie = getMovie(rs);
  371.             for (Country country : getCountries(connection, rs.getInt("id"))) {
  372.                 movie.addCountry(country);
  373.             }
  374.             list.add(movie);
  375.         }
  376.         return list;
  377.     }
  378.    
  379.     /**
  380.      * Returns a list with Movie objects.
  381.      * @param connection a connection to the filmfestival database
  382.      * @param country_id the id of a country
  383.      * @return a List of Screening POJOs
  384.      * @throws SQLException
  385.      * @throws UnsupportedEncodingException
  386.      */
  387.     public static List getMovies(
  388.        DatabaseConnection connection, String country_id)
  389.         throws SQLException, UnsupportedEncodingException {
  390.         List list = new ArrayList();
  391.         PreparedStatement movies =
  392.             connection.createPreparedStatement(MOVIECOUNTRIES);
  393.         movies.setString(1, country_id);
  394.         ResultSet rs = movies.executeQuery();
  395.         while (rs.next()) {
  396.             Movie movie = getMovie(rs);
  397.             for (Director director : getDirectors(connection, rs.getInt("id"))) {
  398.                 movie.addDirector(director);
  399.             }
  400.             list.add(movie);
  401.         }
  402.         return list;
  403.     }
  404.    
  405.     /**
  406.      * Returns an ArrayList containing all the filmfestival days.
  407.      * @param connection a connection to the database.
  408.      * @return a list containing dates.
  409.      */
  410.     public static List getDays(DatabaseConnection connection)
  411.         throws SQLException {
  412.         List list = new ArrayList();
  413.         Statement stm = connection.createStatement();
  414.         ResultSet rs = stm.executeQuery(DAYS);
  415.         while (rs.next()) {
  416.             list.add(rs.getDate("day"));
  417.         }
  418.         stm.close();
  419.         return list;
  420.     }
  421.    
  422.     /**
  423.      * Returns an ArrayList containing all the screening locations.
  424.      * @param connection a connection to the database.
  425.      * @return a list containing location codes.
  426.      */
  427.     public static List getLocations(DatabaseConnection connection)
  428.         throws SQLException {
  429.         List list = new ArrayList();
  430.         Statement stm = connection.createStatement();
  431.         ResultSet rs =
  432.             stm.executeQuery(LOCATIONS);
  433.         while (rs.next()) {
  434.             list.add(rs.getString("location"));
  435.         }
  436.         stm.close();
  437.         return list;
  438.     }
  439. }
FilmFonts.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package com.lowagie.filmfestival;
  9.  
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Font.FontFamily;
  12.  
  13. /**
  14.  * Contains a series of static Font objects that are used throughout the book.
  15.  */
  16. public class FilmFonts {
  17.  
  18.     /** A font used in our PDF file */
  19.     public static final Font NORMAL = new Font(FontFamily.HELVETICA, 12, Font.NORMAL);
  20.     /** A font used in our PDF file */
  21.     public static final Font BOLD = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
  22.     /** A font used in our PDF file */
  23.     public static final Font ITALIC = new Font(FontFamily.HELVETICA, 12, Font.ITALIC);
  24.     /** A font used in our PDF file */
  25.     public static final Font BOLDITALIC = new Font(FontFamily.HELVETICA, 12, Font.BOLDITALIC);
  26.  
  27. }
MovieComparator.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package com.lowagie.filmfestival;
  9.  
  10. import java.util.Comparator;
  11.  
  12. /**
  13.  * Compares movies based on their title or the year, they're made in.
  14.  */
  15. public class MovieComparator implements Comparator {
  16.  
  17.     /** Type for movies have to be sorted by title. */
  18.     public static int BY_TITLE = 0;
  19.     /** Type for movies have to be sorted by production year. */
  20.     public static int BY_YEAR = 1;
  21.    
  22.     /** The type; can be BY_TITLE or BY_YEAR. */
  23.     protected int type;
  24.    
  25.     /**
  26.      * Creates an instance of the MovieComparator.
  27.      * @param type defines how the movies will be sorted;
  28.      * can be BY_TITLE or BY_YEAR
  29.      */
  30.     public MovieComparator(int type) {
  31.         this.type = type;
  32.     }
  33.  
  34.     /**
  35.      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
  36.      */
  37.     public int compare(Movie m1, Movie m2) {
  38.         if (type == BY_YEAR) {
  39.             int c = m1.getYear() - m2.getYear();
  40.             if (c != 0)
  41.                 return c;
  42.         }
  43.         return m1.getTitle().compareTo(m2.getTitle());
  44.     }
  45.  
  46. }
PojoToElementFactory.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package com.lowagie.filmfestival;
  9.  
  10. import com.itextpdf.text.Chunk;
  11. import com.itextpdf.text.Element;
  12. import com.itextpdf.text.List;
  13. import com.itextpdf.text.Phrase;
  14.  
  15. public class PojoToElementFactory {
  16.  
  17.     /**
  18.      * Creates a Phrase containing the title of a Movie.
  19.      * @param movie a Movie object
  20.      * @return a Phrase object
  21.      */
  22.     public static final Phrase getMovieTitlePhrase(final Movie movie) {
  23.         return new Phrase(movie.getMovieTitle(), FilmFonts.NORMAL);
  24.     }
  25.    
  26.     /**
  27.      * Creates a Phrase containing the original title of a Movie.
  28.      * @param movie a Movie object
  29.      * @return a Phrase object
  30.      */
  31.     public static final Phrase getOriginalTitlePhrase(final Movie movie) {
  32.         if (movie.getOriginalTitle() == null)
  33.             return new Phrase("", FilmFonts.NORMAL);
  34.         return new Phrase(movie.getOriginalTitle(), FilmFonts.ITALIC);
  35.     }
  36.    
  37.     /**
  38.      * Creates a Phrase containing the name of a Director.
  39.      * @param director a Director object
  40.      * @return a Phrase object
  41.      */
  42.     public static final Phrase getDirectorPhrase(final Director director) {
  43.         Phrase phrase = new Phrase();
  44.         phrase.add(new Chunk(director.getName(), FilmFonts.BOLD));
  45.         phrase.add(new Chunk(", ", FilmFonts.BOLD));
  46.         phrase.add(new Chunk(director.getGivenName(), FilmFonts.NORMAL));
  47.         return phrase;
  48.     }
  49.    
  50.     /**
  51.      * Creates a Phrase containing the name of a Country.
  52.      * @param country a Country object
  53.      * @return a Phrase object
  54.      */
  55.     public static final Phrase getCountryPhrase(final Country country) {
  56.         return new Phrase(country.getCountry(), FilmFonts.NORMAL);
  57.     }
  58.    
  59.     /**
  60.      * Creates a list with directors.
  61.      * @param movie a Movie object
  62.      * @return a List object
  63.      */
  64.     public static final List getDirectorList(Movie movie) {
  65.         List list = new List();
  66.         for (Director director : movie.getDirectors()) {
  67.             list.add(String.format(
  68.                "%s, %s", director.getName(), director.getGivenName()));
  69.         }
  70.         return list;
  71.     }
  72.    
  73.     /**
  74.      * Creates a list with countries.
  75.      * @param movie a Movie object
  76.      * @return a List object
  77.      */
  78.     public static final List getCountryList(Movie movie) {
  79.         List list = new List();
  80.         for (Country country : movie.getCountries()) {
  81.             list.add(country.getCountry());
  82.         }
  83.         return list;
  84.     }
  85.  
  86.     /**
  87.      * Creates a phrase with the production year of a movie.
  88.      * @param movie a Movie object
  89.      * @return a Phrase object
  90.      */
  91.     public static final Element getYearPhrase(Movie movie) {
  92.         Phrase p = new Phrase();
  93.         p.add(new Chunk("Year: ", FilmFonts.BOLD));
  94.         p.add(new Chunk(String.valueOf(movie.getYear()), FilmFonts.NORMAL));
  95.         return p;
  96.     }
  97.  
  98.     /**
  99.      * Creates a phrase with the run length of a movie.
  100.      * @param movie a Movie object
  101.      * @return a Phrase object
  102.      */
  103.     public static final Element getDurationPhrase(Movie movie) {
  104.         Phrase p = new Phrase();
  105.         p.add(new Chunk("Duration: ", FilmFonts.BOLD));
  106.         p.add(new Chunk(String.valueOf(movie.getDuration()), FilmFonts.NORMAL));
  107.         return p;
  108.     }
  109. }
Movie.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System.Collections.Generic;
  8.  
  9. /**
  10.  * POJO for an object that corresponds with a record
  11.  * in the table film_movietitle.
  12.  */
  13.  namespace kuujinbo.iTextInAction2Ed.Intro_1_2 {
  14.   public class Movie {
  15. // ===========================================================================
  16.     /** The title of the movie. */
  17.     public string Title { get; set; }
  18.     /** The original title (if different from title). */
  19.     public string OriginalTitle { get; set; }
  20.     /** Code used by IMDB */
  21.     public string Imdb { get; set; }
  22.     /** The year the movie was released. */
  23.     public int Year { get; set; }
  24.     /** The duration of the movie in minutes. */
  25.     public int Duration { get; set; }
  26.     /** The list of directors. */
  27.     private List _directors = new List();
  28.     public List Directors {
  29.       get { return _directors; }
  30.     }
  31.     /** The list of countries. */
  32.     private List _countries = new List();
  33.     public List Countries {
  34.       get { return _countries; }
  35.     }
  36. // ---------------------------------------------------------------------------
  37.     /** The filmfestival entry info. */
  38.     private Entry _entry;
  39.     public Entry entry {
  40.       get { return _entry; }
  41.       set {
  42.         _entry = value;
  43.         if (_entry.movie == null) _entry.movie = this;
  44.       }
  45.     }
  46. // ---------------------------------------------------------------------------
  47.     /**
  48.      * Adds a director.
  49.      * @param director one of the directors of the movie
  50.      */
  51.     public void AddDirector(Director director) {
  52.       _directors.Add(director);
  53.     }
  54. // ---------------------------------------------------------------------------
  55.     /**
  56.      * Adds a country.
  57.      * @param country  one of the countries the movie was made by.
  58.      */
  59.     public void AddCountry(Country country) {
  60.       _countries.Add(country);    
  61.     }
  62. // ---------------------------------------------------------------------------
  63.     /**
  64.      * Returns the title in the correct form.
  65.      * @return a title
  66.      */
  67.     public string MovieTitle {
  68.       get {
  69.         var title = Title;
  70.         if (!string.IsNullOrEmpty(title)) {
  71.           if (title.EndsWith(", A"))
  72.               return "A " + title.Substring(0, title.Length - 3);
  73.           if (title.EndsWith(", The"))
  74.               return "The " + title.Substring(0, title.Length - 5);
  75.           return title;
  76.         }
  77.         return null;
  78.       }
  79.     }
  80. // ---------------------------------------------------------------------------    
  81.     /**
  82.      * Returns the title in the correct form.
  83.      * @return a title
  84.      */
  85.     public string GetMovieTitle(bool prefix) {
  86.       var title = Title;
  87.       if (title.EndsWith(", A")) {
  88.       return prefix ? "A " : title.Substring(0, title.Length - 3);
  89.       }
  90.      
  91.       if (title.EndsWith(", The")) {
  92.         return prefix ? "The " : title.Substring(0, title.Length - 5);
  93.       }
  94.      
  95.       return prefix ? null : title;
  96.     }    
  97. // ===========================================================================
  98.   }
  99. }
Director.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. /**
  9.  * POJO for an object that corresponds with a record
  10.  * in the table film_director.
  11.  */
  12. namespace kuujinbo.iTextInAction2Ed.Intro_1_2 {
  13.   public class Director {
  14. // ===========================================================================
  15.     public string Name { get; set; }
  16.     public string GivenName { get; set; }
  17. // ===========================================================================
  18.   }
  19. }
Country.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. /**
  9.  * POJO for an object that corresponds with a record
  10.  * in the table film_country.
  11.  */
  12. namespace kuujinbo.iTextInAction2Ed.Intro_1_2 {
  13.   public class Country {
  14. // ===========================================================================
  15.     public string Name { get; set; }
  16. // ===========================================================================
  17.   }
  18. }
Entry.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.Collections.Generic;
  9. /**
  10.  * POJO for an object that corresponds with a record
  11.  * in the table festival_entry.
  12.  */
  13. namespace kuujinbo.iTextInAction2Ed.Intro_1_2 {
  14.   public class Entry {
  15. // ===========================================================================
  16.     /** The festival year. */
  17.     public int Year { get; set; }
  18.     /** The category. */
  19.     public Category category { get; set; }
  20. // ---------------------------------------------------------------------------
  21.     /** The movie. */
  22.     private Movie _movie;
  23.     public Movie movie {
  24.       get { return _movie; }
  25.       set {
  26.         _movie = value;
  27.         if (_movie.entry == null) _movie.entry = this;
  28.       }
  29.     }    
  30. // ---------------------------------------------------------------------------
  31.     /** The screenings. */
  32.     private List _screenings = new List();
  33.     public List Screenings {
  34.       get { return _screenings; }
  35.     }
  36. // ---------------------------------------------------------------------------
  37.     /**
  38.      * Adds a screening to this entry.
  39.      */
  40.     public void AddScreening(Screening screening) {
  41.       _screenings.Add(screening);    
  42.     }
  43. // ===========================================================================
  44.   }
  45. }
Category.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. /**
  9.  * POJO for an object that corresponds with a record
  10.  * in the table festival_category.
  11.  */
  12. namespace kuujinbo.iTextInAction2Ed.Intro_1_2 {
  13.   public class Category {
  14. // ===========================================================================
  15.     /** The name of the category. */
  16.     public string Name { get; set; }
  17.     /** A short keyword for the category. */
  18.     public string Keyword { get; set; }
  19.     /** The color code of the category. */
  20.     public string color { get; set; }
  21.     /** The parent category (if any). */
  22.     public Category Parent { get; set; }    
  23. // ===========================================================================
  24.   }
  25. }
Screening.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8.  
  9. /**
  10.  * POJO for an object that corresponds with a record
  11.  * in the table festival_screening.
  12.  */
  13. namespace kuujinbo.iTextInAction2Ed.Intro_1_2 {
  14.   public class Screening {
  15. // ===========================================================================
  16.     /** The date of the screening. */
  17.     public string Date { get; set; }
  18.     /** The time of the screening. */
  19.     public string Time { get; set; }
  20.     /** The location of the screening. */
  21.     public String Location { get; set; }
  22.     /** Is this a screening for the press only? */
  23.     public bool Press { get; set; }
  24.     /** The movie that will be screened. */
  25.     public Movie movie { get; set; }
  26. // ===========================================================================
  27.   }
  28. }
PojoFactory.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.Data.Common;
  12. using System.Linq;
  13. using System.Text;
  14.  
  15. /**
  16.  * A factory that makes it easy to query the database using
  17.  * a series of static methods using System.Data.SQLite:
  18.  * http://sqlite.phxsoftware.com/
  19.  * ###########################################################################
  20.  * System.Data.SQLite is the ADO.NET 2.0/3.5 provider for the
  21.  * SQLite database engine:
  22.  * http://www.sqlite.org/
  23.  * ###########################################################################
  24.  *
  25.  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  26.  * before July 2010, ADO.NET data provider **DEPENDENT** code was used.
  27.  * this has been fixed. if you want to use a different provider see the
  28.  * README.txt file. the change requires minimumal effort.
  29.  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  30.  */
  31. namespace kuujinbo.iTextInAction2Ed.Intro_1_2 {
  32.   public class PojoFactory {
  33. // ===========================================================================
  34.     /**
  35.      * Fills a Movie POJO using a DbDataReader.
  36.      * @param rs a DbDataReader with records from table film_movietitle
  37.      * @return a Movie POJO
  38.      */
  39.     public static Movie GetMovie(DbDataReader r) {
  40.       return new Movie() {
  41.         Title = r["title"].ToString(),
  42.         OriginalTitle = r["original_title"] != null
  43.             ? r["original_title"].ToString() : "",
  44.         Imdb = r["imdb"].ToString(),
  45.         Year = Convert.ToInt32(r["year"]),
  46.         Duration = Convert.ToInt32(r["duration"])
  47.       };
  48.     }
  49. // ---------------------------------------------------------------------------
  50.     /**
  51.      * Fills a Director POJO using a DbDataReader.
  52.      * @param rs a DbDataReader with records from table file_director
  53.      * @return a Director POJO
  54.      */
  55.     public static Director GetDirector(DbDataReader r) {
  56.       return new Director() {
  57.         Name = r["name"].ToString(),
  58.         GivenName = r["given_name"].ToString()
  59.       };
  60.     }
  61. // ---------------------------------------------------------------------------
  62.     /**
  63.      * Fills a Country POJO using a DbDataReader.
  64.      * @param rs a DbDataReader with records from table file_director
  65.      * @return a Country POJO
  66.      */
  67.     public static Country GetCountry(DbDataReader r) {
  68.       return new Country() {
  69.         Name = r["country"].ToString()
  70.       };
  71.     }
  72. // ---------------------------------------------------------------------------
  73.     /**
  74.      * Fills an Entry POJO using a DbDataReader.
  75.      * @param rs a DbDataReader with records from table festival_entry
  76.      * @return an Entry POJO
  77.      */
  78.     public static Entry GetEntry(DbDataReader r) {
  79.       return new Entry() {
  80.         Year = Convert.ToInt32(r["year"])
  81.       };
  82.     }
  83. // ---------------------------------------------------------------------------
  84.     /**
  85.      * Fills a Category POJO using a DbDataReader.
  86.      * @param rs a DbDataReader with records from table festival_category
  87.      * @return a Category POJO
  88.      */
  89.     public static Category GetCategory(DbDataReader r) {
  90.       return new Category() {
  91.         Name = r["name"].ToString(),
  92.         Keyword = r["keyword"].ToString(),
  93.         color = r["color"].ToString()
  94.       };
  95.     }
  96. // ---------------------------------------------------------------------------
  97.     /**
  98.      * Fills a Screening POJO using a DbDataReader.
  99.      * @param rs a DbDataReader with records from table festival_screening
  100.      * @return a Screening POJO
  101.      */
  102.     public static Screening GetScreening(DbDataReader r)  {
  103.       return new Screening() {
  104.         Date = r["day"].ToString(),
  105.         Time = r["time"].ToString(),
  106.         Location = r["location"].ToString(),
  107.         Press = Convert.ToInt32(r["press"]) == 1
  108.       };
  109.     }
  110. // ---------------------------------------------------------------------------
  111.     /**
  112.      * Returns a list with Screening objects
  113.      * @param film_id a movie id
  114.      * @return a List of Screening POJOs
  115.      */
  116.     public static List GetScreenings(int film_id) {
  117.       List list = new List();
  118.       using (var c =  AdoDB.Provider.CreateConnection()) {
  119.         c.ConnectionString = AdoDB.CS;
  120.         using (DbCommand cmd = c.CreateCommand()) {
  121.           cmd.CommandText = AdoDB.MOVIESCREENINGS;
  122.           cmd.Parameters.Add(cmd.CreateParameter());
  123.           cmd.Parameters[0].ParameterName = "@film_id";
  124.           cmd.Parameters[0].Value = film_id;
  125.           c.Open();
  126.           using (var r = cmd.ExecuteReader()) {
  127.             while (r.Read()) {
  128.               list.Add(GetScreening(r));
  129.             }
  130.           }
  131.         }
  132.       }
  133.       return list;
  134.     }    
  135. // ---------------------------------------------------------------------------
  136.     /**
  137.      * Returns a list with Screening objects, if you pass
  138.      * a stringified date.
  139.      * @param day stringified date "yyyy-MM-dd"
  140.      * @return a List of Screening POJOs
  141.      */
  142.     public static List GetScreenings(string day)  {
  143.       List list = new List();
  144.       using (var c =  AdoDB.Provider.CreateConnection()) {
  145.         c.ConnectionString = AdoDB.CS;
  146.         using (DbCommand cmd = c.CreateCommand()) {
  147.           cmd.CommandText = AdoDB.SCREENINGS;
  148.           cmd.Parameters.Add(cmd.CreateParameter());
  149.           cmd.Parameters[0].ParameterName = "@day";
  150.           cmd.Parameters[0].Value = day;
  151.           c.Open();
  152.           using (var r = cmd.ExecuteReader()) {
  153.             while (r.Read()) {
  154.               Screening screening = GetScreening(r);
  155.               Movie movie = GetMovie(r);
  156.               foreach (var d in GetDirectors(Convert.ToInt32(r["id"]))) {
  157.                 movie.AddDirector(d);
  158.               }
  159.               foreach (var cn in GetCountries(Convert.ToInt32(r["id"]))) {
  160.                 movie.AddCountry(cn);
  161.               }
  162.               Entry entry = GetEntry(r);
  163.               Category category = GetCategory(r);
  164.               entry.category = category;
  165.               entry.movie = movie;
  166.               movie.entry = entry;
  167.               screening.movie = movie;
  168.               list.Add(screening);
  169.             }
  170.           }
  171.         }
  172.       }
  173.       return list;
  174.     }
  175. // ---------------------------------------------------------------------------    
  176.     /**
  177.      * Returns a list with Movie objects.
  178.      */
  179.     public static IEnumerable GetMovies() {
  180.       return GetMovies(false);
  181.     }
  182.     /*
  183.      * @param sort_by_year => LINQ sort by movie year
  184.      */
  185.     public static IEnumerable GetMovies(bool sort_by_year) {
  186.       List list = new List();
  187.       using (var c =  AdoDB.Provider.CreateConnection()) {
  188.         c.ConnectionString = AdoDB.CS;
  189.         using (DbCommand cmd = c.CreateCommand()) {
  190.           cmd.CommandText = AdoDB.MOVIES;
  191.           c.Open();
  192.           using (var r = cmd.ExecuteReader()) {
  193.             while (r.Read()) {
  194.               Movie movie = GetMovie(r);
  195.               Entry entry = GetEntry(r);
  196.               Category category = GetCategory(r);
  197.               entry.category = category;
  198.               int film_id = Convert.ToInt32(r["id"]);
  199.               foreach ( Screening screening in GetScreenings(film_id) ) {
  200.                 entry.AddScreening(screening);
  201.               }
  202.               movie.entry = entry;
  203.               foreach ( Director director in GetDirectors(film_id) ) {
  204.                 movie.AddDirector(director);
  205.               }
  206.               foreach ( Country country in GetCountries(film_id) ) {
  207.                 movie.AddCountry(country);
  208.               }
  209.               list.Add(movie);
  210.             }
  211.           }
  212.         }
  213.       }
  214.       if (!sort_by_year) {
  215.         return list;
  216.       }
  217.       else {
  218.         return from m in list orderby m.Year, m.Title select m;
  219.       }
  220.     }    
  221. // ---------------------------------------------------------------------------
  222.     /**
  223.      * Returns a list with Movie objects.
  224.      * @param director_id the id of a director
  225.      * @return a List of Screening POJOs
  226.      */
  227.     // default => return collection
  228.     public static IEnumerable GetMovies(int director_id)
  229.     { return GetMovies(director_id, false);  }
  230.     /*
  231.      * @param sort_by_year => LINQ sort by movie year
  232.      */
  233.     public static IEnumerable GetMovies(
  234.         int director_id, bool sort_by_year)
  235.     {
  236.       List list = new List();
  237.       using (var c =  AdoDB.Provider.CreateConnection()) {
  238.         c.ConnectionString = AdoDB.CS;
  239.         using (DbCommand cmd = c.CreateCommand()) {
  240.           cmd.CommandText = AdoDB.MOVIEDIRECTORS;
  241.           cmd.Parameters.Add(cmd.CreateParameter());
  242.           cmd.Parameters[0].ParameterName = "@director_id";
  243.           cmd.Parameters[0].Value = director_id;
  244.           c.Open();
  245.           using (var r = cmd.ExecuteReader()) {
  246.             while (r.Read()) {
  247.               list.Add(GetMovie(r));
  248.             }
  249.           }
  250.         }
  251.       }
  252.       if (!sort_by_year) {
  253.         return list;
  254.       }
  255.       else {
  256.         return from m in list orderby m.Year, m.Title select m;      
  257.       }      
  258.     }
  259. // ---------------------------------------------------------------------------    
  260.     /**
  261.      * Returns a list with Movie objects.
  262.      * @param country_id the id of a country
  263.      * @return a List of Screening POJOs
  264.      */
  265.     // default => return collection
  266.     public static IEnumerable GetMovies(string country_id)
  267.     { return GetMovies(country_id, false); }
  268.     /*
  269.      * @param sort_by_year => LINQ sort by movie year
  270.      */
  271.     public static IEnumerable GetMovies(
  272.         string country_id, bool sort_by_year
  273.     ) {
  274.       List list = new List();
  275.       using (var c =  AdoDB.Provider.CreateConnection()) {
  276.         c.ConnectionString = AdoDB.CS;
  277.         using (DbCommand cmd = c.CreateCommand()) {
  278.           cmd.CommandText = AdoDB.MOVIECOUNTRIES;
  279.           cmd.Parameters.Add(cmd.CreateParameter());
  280.           cmd.Parameters[0].ParameterName = "@country_id";
  281.           cmd.Parameters[0].Value = country_id;
  282.           c.Open();
  283.           using (var r = cmd.ExecuteReader()) {
  284.             while (r.Read()) {
  285.               Movie movie = GetMovie(r);
  286.               foreach ( Director d in GetDirectors(Convert.ToInt32(r["id"])) ) {
  287.                 movie.AddDirector(d);
  288.               }            
  289.               list.Add(movie);
  290.             }
  291.           }
  292.         }
  293.       }    
  294.       if (!sort_by_year) {
  295.         return list;
  296.       }
  297.       else {
  298.         return from m in list orderby m.Year, m.Title select m;    
  299.       }
  300.     }      
  301. // ---------------------------------------------------------------------------
  302.     /**
  303.      * Returns a list with Country objects.
  304.      * @param connection a connection to the filmfestival database
  305.      * @param movie_id the id of a movie
  306.      * @return a List of Screening POJOs
  307.      */
  308.     public static List GetCountries(int film_id) {
  309.       List list = new List();
  310.       using (var c =  AdoDB.Provider.CreateConnection()) {
  311.         c.ConnectionString = AdoDB.CS;
  312.         using (DbCommand cmd = c.CreateCommand()) {
  313.           cmd.CommandText = AdoDB.COUNTRIES;
  314.           cmd.Parameters.Add(cmd.CreateParameter());
  315.           cmd.Parameters[0].ParameterName = "@film_id";
  316.           cmd.Parameters[0].Value = film_id;
  317.           c.Open();
  318.           using (var r = cmd.ExecuteReader()) {
  319.             while (r.Read()) {
  320.               list.Add(GetCountry(r));
  321.             }
  322.           }
  323.         }
  324.       }
  325.       return list;
  326.     }
  327. // ---------------------------------------------------------------------------
  328.     /**
  329.      * Returns a list with Screening objects
  330.      * @return a List of Screening POJOs
  331.      */
  332.     public static List GetPressPreviews() {
  333.       List list = new List();
  334.       using (var c =  AdoDB.Provider.CreateConnection()) {
  335.         c.ConnectionString = AdoDB.CS;
  336.         using (DbCommand cmd = c.CreateCommand()) {
  337.           cmd.CommandText = AdoDB.PRESS;
  338.           c.Open();
  339.           using (var r = cmd.ExecuteReader()) {
  340.             while (r.Read()) {
  341.               Screening screening = GetScreening(r);
  342.               Movie movie = GetMovie(r);
  343.               int film_id = Convert.ToInt32(r["id"]);
  344.               foreach (Director d in GetDirectors(film_id) ) {
  345.                 movie.AddDirector(d);
  346.               }
  347.               foreach ( Country country in GetCountries(film_id) ) {
  348.                 movie.AddCountry(country);
  349.               }
  350.               Entry entry = GetEntry(r);
  351.               Category category = GetCategory(r);
  352.               entry.category = category;
  353.               entry.movie = movie;
  354.               movie.entry = entry;
  355.               screening.movie = movie;
  356.               list.Add(screening);            
  357.             }
  358.           }
  359.         }
  360.       }      
  361.       return list;
  362.     }
  363. // ---------------------------------------------------------------------------    
  364.     /**
  365.      * Returns a list with Director objects.
  366.      * @param film_id the id of a movie
  367.      */
  368.     public static List GetDirectors(int film_id) {
  369.       List list = new List();
  370.       using (var c =  AdoDB.Provider.CreateConnection()) {
  371.         c.ConnectionString = AdoDB.CS;
  372.         using (DbCommand cmd = c.CreateCommand()) {
  373.           cmd.CommandText = AdoDB.DIRECTORS;
  374.           cmd.Parameters.Add(cmd.CreateParameter());
  375.           cmd.Parameters[0].ParameterName = "@film_id";
  376.           cmd.Parameters[0].Value = film_id;
  377.           c.Open();
  378.           using (var r = cmd.ExecuteReader()) {
  379.             while (r.Read()) {
  380.               list.Add(GetDirector(r));
  381.             }
  382.           }
  383.         }
  384.       }
  385.       return list;
  386.     }
  387. // ---------------------------------------------------------------------------        
  388.     /**
  389.      * Returns an List containing all the filmfestival days.
  390.      * @return a list containing days.
  391.      */
  392.     public static List GetDays() {
  393.       List list = new List();
  394.       using (var c =  AdoDB.Provider.CreateConnection()) {
  395.         c.ConnectionString = AdoDB.CS;
  396.         using (DbCommand cmd = c.CreateCommand()) {
  397.           cmd.CommandText = AdoDB.DAYS;
  398.           c.Open();
  399.           using (var r = cmd.ExecuteReader()) {
  400.             while (r.Read()) {
  401.               list.Add(r.GetString(0));
  402.             }
  403.           }
  404.         }
  405.       }        
  406.       return list;
  407.     }
  408. // ---------------------------------------------------------------------------    
  409. //    /**
  410. //     * Returns an List containing all the screening locations.
  411. //     * @return a list containing location codes.
  412. //     */
  413.     public static List GetLocations() {
  414.       List list = new List();
  415.       using (var c =  AdoDB.Provider.CreateConnection()) {
  416.         c.ConnectionString = AdoDB.CS;
  417.         using (DbCommand cmd = c.CreateCommand()) {
  418.           cmd.CommandText = AdoDB.LOCATIONS;
  419.           c.Open();
  420.           using (var r = cmd.ExecuteReader()) {
  421.             while (r.Read()) {
  422.               list.Add(r.GetString(0));
  423.             }
  424.           }
  425.         }
  426.       }        
  427.       return list;
  428.     }
  429. // ===========================================================================
  430.   }
  431. }
FilmFonts.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using iTextSharp.text;
  8. using iTextSharp.text.pdf;
  9.  
  10. /**
  11.  * Contains a series of static Font objects that are used throughout the book.
  12.  */
  13. namespace kuujinbo.iTextInAction2Ed.Intro_1_2 {
  14.   public class FilmFonts {
  15. // ===========================================================================
  16.     /** A font used in our PDF file */
  17.     public static Font NORMAL = new Font(Font.FontFamily.HELVETICA, 12, Font.NORMAL);
  18.     /** A font used in our PDF file */
  19.     public static Font BOLD = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD);
  20.     /** A font used in our PDF file */
  21.     public static Font ITALIC = new Font(Font.FontFamily.HELVETICA, 12, Font.ITALIC);
  22.     /** A font used in our PDF file */
  23.     public static Font BOLDITALIC = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLDITALIC);
  24. // ===========================================================================
  25.   }
  26. }
PojoToElementFactory.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System.Collections.Generic;
  8. using iTextSharp.text;
  9. using iTextSharp.text.pdf;
  10.  
  11. namespace kuujinbo.iTextInAction2Ed.Intro_1_2 {
  12.   public class PojoToElementFactory {
  13. // ===========================================================================
  14.     /**
  15.      * Creates a Phrase containing the title of a Movie.
  16.      * @param movie a Movie object
  17.      * @return a Phrase object
  18.      */
  19.     public static Phrase GetMovieTitlePhrase(Movie movie) {
  20.       return new Phrase(movie.MovieTitle, FilmFonts.NORMAL);
  21.     }
  22. // ---------------------------------------------------------------------------    
  23.     /**
  24.      * Creates a Phrase containing the original title of a Movie.
  25.      * @param movie a Movie object
  26.      * @return a Phrase object
  27.      */
  28.     public static Phrase GetOriginalTitlePhrase(Movie movie) {
  29.       return  string.IsNullOrEmpty(movie.OriginalTitle)
  30.         ? new Phrase("", FilmFonts.NORMAL)
  31.         : new Phrase(movie.OriginalTitle, FilmFonts.ITALIC)
  32.         ;
  33.     }
  34. // ---------------------------------------------------------------------------    
  35.     /**
  36.      * Creates a Phrase containing the name of a Director.
  37.      * @param director a Director object
  38.      * @return a Phrase object
  39.      */
  40.     public static Phrase GetDirectorPhrase(Director director) {
  41.       Phrase phrase = new Phrase();
  42.       phrase.Add(new Chunk(director.Name, FilmFonts.BOLD));
  43.       phrase.Add(new Chunk(", ", FilmFonts.BOLD));
  44.       phrase.Add(new Chunk(director.GivenName, FilmFonts.NORMAL));
  45.       return phrase;
  46.     }
  47. // ---------------------------------------------------------------------------    
  48.     /**
  49.      * Creates a Phrase containing the name of a Country.
  50.      * @param country a Country object
  51.      * @return a Phrase object
  52.      */
  53.     public static Phrase GetCountryPhrase(Country country) {
  54.       return new Phrase(country.Name, FilmFonts.NORMAL);
  55.     }
  56. // ---------------------------------------------------------------------------    
  57.     /**
  58.      * Creates a list with directors.
  59.      * @param movie a Movie object
  60.      * @return a List object
  61.      */
  62.     public static List GetDirectorList(Movie movie) {
  63.       var list = new List();
  64.       foreach (Director director in movie.Directors ) {
  65.         list.Add(string.Format(
  66.           "{0}, {1}", director.Name, director.GivenName
  67.         ));
  68.       }
  69.       return list;
  70.     }
  71. // ---------------------------------------------------------------------------    
  72.     /**
  73.      * Creates a list with countries.
  74.      * @param movie a Movie object
  75.      * @return a List object
  76.      */
  77.     public static List GetCountryList(Movie movie) {
  78.       var list = new List();
  79.       foreach (Country country in movie.Countries) {
  80.         list.Add(country.Name);
  81.       }
  82.       return list;
  83.     }
  84. // ---------------------------------------------------------------------------
  85.     /**
  86.      * Creates a phrase with the production year of a movie.
  87.      * @param movie a Movie object
  88.      * @return a Phrase object
  89.      */
  90.     // public static Element GetYearPhrase(Movie movie) {
  91.     public static Phrase GetYearPhrase(Movie movie) {
  92.       Phrase p = new Phrase();
  93.       p.Add(new Chunk("Year: ", FilmFonts.BOLD));
  94.       p.Add(new Chunk(movie.Year.ToString(), FilmFonts.NORMAL));
  95.       return p;
  96.     }
  97. // ---------------------------------------------------------------------------
  98.     /**
  99.      * Creates a phrase with the run length of a movie.
  100.      * @param movie a Movie object
  101.      * @return a Phrase object
  102.      */
  103.     // public static Element getDurationPhrase(Movie movie) {
  104.     public static Phrase GetDurationPhrase(Movie movie) {
  105.       Phrase p = new Phrase();
  106.       p.Add(new Chunk("Duration: ", FilmFonts.BOLD));
  107.       p.Add(new Chunk(movie.Duration.ToString(), FilmFonts.NORMAL));
  108.       return p;
  109.     }
  110. // ===========================================================================
  111.   }
  112. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now