classroom.filmfestival_a.Movies01.java Source code

Java tutorial

Introduction

Here is the source code for classroom.filmfestival_a.Movies01.java

Source

/*
 * This example was written by Bruno Lowagie, author of the book
 * 'iText in Action' by Manning Publications (ISBN: 1932394796).
 * You can use this example as inspiration for your own applications.
 * The following license applies:
 * http://www.1t3xt.com/about/copyright/index.php?page=MIT
 */

package classroom.filmfestival_a;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

import database.MySessionFactory;
import database.festivaldatabase.FilmTitle;

public class Movies01 {

    public static final String RESULT = "results/classroom/filmfestival/movies01.pdf";
    public static final Logger LOGGER = Logger.getLogger(Movies01.class.getName());

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        // step 1
        Document document = new Document();
        try {
            // step 2
            PdfWriter.getInstance(document, new FileOutputStream(RESULT));
            // step 3
            document.open();
            // step 4
            Session session = (Session) MySessionFactory.currentSession();
            Query q = session.createQuery("from FilmTitle order by title");
            java.util.List<FilmTitle> results = q.list();
            for (FilmTitle movie : results) {
                document.add(new Paragraph(movie.getTitle()));
            }
            // step 5
            document.close();
        } catch (IOException e) {
            LOGGER.error("IOException: ", e);
        } catch (DocumentException e) {
            LOGGER.error("DocumentException: ", e);
        }
    }
}