Pojo classes film database

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

10th October 2015
admin-marketing

These are some Pojo that can be used in combination with the movie database. These examples were written in the context of the book "iText in Action - Second Edition".

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


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