View Javadoc

1   package at.ac.tuwien.vitalab.hrcrm.pdf.address;
2   
3   import java.awt.Color;
4   import java.lang.reflect.Field;
5   import java.util.List;
6   import java.util.Map;
7   
8   import javax.servlet.http.HttpServletRequest;
9   import javax.servlet.http.HttpServletResponse;
10  
11  import org.springframework.beans.factory.annotation.Required;
12  import org.springframework.web.servlet.view.document.AbstractPdfView;
13  
14  import at.ac.tuwien.vitalab.hrcrm.adaptor.AddressAdaptor;
15  import at.ac.tuwien.vitalab.hrcrm.dto.address.AddressType;
16  
17  import com.lowagie.text.Document;
18  import com.lowagie.text.Table;
19  import com.lowagie.text.pdf.PdfWriter;
20  
21  public class AddressPdfView<T> extends AbstractPdfView {
22  	private AddressAdaptor<T>	addressAdaptor;
23  
24  	@Override
25  	@SuppressWarnings( "unchecked" )
26  	protected void buildPdfDocument( final Map model, final Document document, final PdfWriter writer,
27  			final HttpServletRequest request, final HttpServletResponse response ) throws Exception {
28  
29  		List<AddressType> addressTypes = (List<AddressType>) model.get( "address.pdf" );
30  		if( addressTypes.size() == 0 )
31  			throw new IllegalArgumentException( "Unable to generate pdf file. Dadabase is empty!" );
32  
33  		T object = this.addressAdaptor.addressType2Address( addressTypes.get( 0 ) );
34  		Table table = generateTableStub( object );
35  
36  		this.generateTableHeader( object, table );
37  		this.generateTableBody( addressTypes, table );
38  
39  		document.add( table );
40  	}
41  
42  	/***
43  	 * {@inheritDoc}
44  	 */
45  	@Override
46  	@SuppressWarnings( "unchecked" )
47  	protected void buildPdfMetadata( Map model, Document document, HttpServletRequest request ) {
48  		document.addCreationDate();
49  		document.addAuthor( "HR-CRM Service" );
50  		document.addTitle( "All Available Addresses" );
51  	}
52  
53  	private Table generateTableStub( T object ) throws Exception {
54  		Table table = new Table( object.getClass().getDeclaredFields().length );
55  		table.setWidth( 90 );
56  		table.setBorderWidth( 1 );
57  		table.setBorderColor( Color.BLACK );
58  		table.setBackgroundColor( Color.LIGHT_GRAY );
59  		table.setPadding( 3 );
60  		return table;
61  	}
62  
63  	private void generateTableHeader( final T object, final Table table ) throws Exception {
64  		Field[] fields = object.getClass().getDeclaredFields();
65  		for( Field field : fields ) {
66  			field.setAccessible( true );
67  			table.addCell( field.getName().toUpperCase() );
68  		}
69  	}
70  
71  	private void generateTableBody( final List<AddressType> addressTypes, final Table table ) throws Exception {
72  		for( AddressType addressType : addressTypes ) {
73  			T object = this.addressAdaptor.addressType2Address( addressType );
74  			Field[] fields = object.getClass().getDeclaredFields();
75  			for( Field field : fields ) {
76  				field.setAccessible( true );
77  				table.addCell( String.valueOf( field.get( object ) ) );
78  			}
79  		}
80  	}
81  
82  	/***
83  	 * Write access to private field.
84  	 * @param addressAdaptor
85  	 *        The addressAdaptor to set.
86  	 */
87  	@Required
88  	public void setAddressAdaptor( final AddressAdaptor<T> addressAdaptor ) {
89  		this.addressAdaptor = addressAdaptor;
90  	}
91  
92  }