view servlet/src/digilib/servlet/PDFTitlePage.java @ 509:ab94692bff0c digilibPDF

*** empty log message ***
author cmielack
date Fri, 13 Mar 2009 15:54:37 +0100
parents
children e706a777798b
line wrap: on
line source

package digilib.servlet;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.log4j.Logger;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

import digilib.io.DocuDirCache;

/** A class for the generation of title pages for the generated pdf documents.
 * 
 * 
 */
public class PDFTitlePage {
	
	private PDFJobInformation job_info = null;
	private DigilibInfoReader info_reader= null;
	private DocuDirCache dirCache = null;
	protected static Logger logger = Logger.getLogger("digilib.servlet");

	
	public PDFTitlePage(PDFJobInformation pdfji){
		job_info = pdfji;
		dirCache = (DocuDirCache) job_info.getDlConfig().getValue("servlet.dir.cache");

		String fn = getBase(dirCache.getDirectory(pdfji.getImageJobInformation().getAsString("fn")).getDir().getPath()) + "presentation/info.xml";
		
		info_reader = new DigilibInfoReader(fn);
	}
	
	
	public Element getPageContents(){
		Paragraph content = new Paragraph();
		content.setAlignment(Element.ALIGN_CENTER);

		// add vertical whitespace
		for(int i=0; i<8; i++){
			content.add(Chunk.NEWLINE);
		}
		
		
		// add logo
		content.add(getLogo());
		content.add(Chunk.NEWLINE);
		content.add(Chunk.NEWLINE);

		// add title
		Anchor title = new Anchor(new Paragraph(getTitle(),FontFactory.getFont(FontFactory.HELVETICA,16)));
		String burl = job_info.getImageJobInformation().getAsString("base.url");
		
		title.setReference(burl+"digilib.jsp?fn="+job_info.getImageJobInformation().getAsString("fn"));
		content.add(title);		
		content.add(Chunk.NEWLINE);

		// add author
		if(getDate()!=" ")
			content.add(new Paragraph(getAuthor()+" ("+getDate()+")",FontFactory.getFont(FontFactory.HELVETICA,14)));
		else
			content.add(new Paragraph(getAuthor(),FontFactory.getFont(FontFactory.HELVETICA,14)));
		
		content.add(Chunk.NEWLINE);
		
		// add page numbers
		content.add(new Paragraph(getPages(), FontFactory.getFont(FontFactory.HELVETICA, 12)));


		content.add(Chunk.NEWLINE);
		content.add(Chunk.NEWLINE);
		content.add(Chunk.NEWLINE);

		// add credits
		content.add(new Paragraph("MPIWG Berlin 2009", FontFactory.getFont(FontFactory.HELVETICA,10)));

		// add digilib version
		content.add(new Paragraph(getDigilibVersion(),FontFactory.getFont(FontFactory.HELVETICA,10)));

		for(int i=0; i<8; i++){
			content.add(Chunk.NEWLINE);
		}
		Anchor address = new Anchor(
				new Paragraph(burl+"digilib.jsp?fn="+job_info.getImageJobInformation().getAsString("fn"), FontFactory.getFont(FontFactory.COURIER, 9))
									);
		address.setReference(burl+"digilib.jsp?fn="+job_info.getImageJobInformation().getAsString("fn"));
		
		content.add(address);

		
		return content;
	}
	
	private String getBase(String path){
		// return base directory of an image directory
		if(path.contains("/")){
			String[] x = path.split("/");
			String newpath = "";
			for(int i=0; i<x.length-1; i++){
				newpath += x[i]+"/";
			}
			return newpath;
		}
		else
			return "";
	}
	
	
	private Image getLogo(){
		
		try {
			// loads a local image. 
			// In order to use a remote image, getInstance needs to be called using a URL object
			URL url = new URL(job_info.getDlConfig().getAsString("pdf-logo"));
			if(url!=null && !url.equals("")){
				Image logo = Image.getInstance(url);
				logo.setAlignment(Element.ALIGN_CENTER);
				return logo;
			}
		} catch (BadElementException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} catch (MalformedURLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	
	
	private String getTitle(){
		if(info_reader.hasInfo())
			return info_reader.getAsString("title");
		else
			return job_info.getImageJobInformation().getAsString("fn");
	}
	private String getAuthor(){
		if(info_reader.hasInfo())
			return info_reader.getAsString("author");
		else
			return " ";
	}
	private String getDate(){
		if(info_reader.hasInfo())
			return info_reader.getAsString("date");
		else
			return " ";
	}
	private String getPages(){
		return "Pages "+job_info.getAsString("pgs") + " (scan page numbers)";
	}

	private String getDigilibVersion(){
		return "Digilib PDFMaker v."+PDFMaker.version;
	}
	
}