View Javadoc

1   package at.ac.tuwien.vitalab.hrcrm.pdf.name;
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.NameAdaptor;
15  import at.ac.tuwien.vitalab.hrcrm.dto.name.PartyNameType;
16  
17  import com.lowagie.text.Document;
18  import com.lowagie.text.Table;
19  import com.lowagie.text.pdf.PdfWriter;
20  
21  public class NamesPdfView<T> extends AbstractPdfView {
22  	private NameAdaptor<T>	nameAdaptor;
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<PartyNameType> partyNameTypes = (List<PartyNameType>) model.get( "partyNameTypes" );
30  		if( partyNameTypes.size() == 0 )
31  			throw new IllegalArgumentException( "Unable to generate pdf file. Dadabase is empty!" );
32  
33  		T object = this.nameAdaptor.partyNameType2Name( partyNameTypes.get( 0 ) );
34  		Table table = generateTableStub( object );
35  
36  		this.generateTableHeader( object, table );
37  		this.generateTableBody( partyNameTypes, table );
38  
39  		document.add( table );
40  	}
41  	
42  
43  	/***
44  	 * {@inheritDoc}
45  	 */
46  	@Override
47  	@SuppressWarnings("unchecked")
48  	protected void buildPdfMetadata( Map model, Document document, HttpServletRequest request ) {
49  		document.addCreationDate();
50  		document.addAuthor( "HR-CRM Service" );
51  		document.addTitle( "All Available Party Names" );
52  	}
53  
54  
55  	private Table generateTableStub( T object ) throws Exception {
56  		Table table = new Table( object.getClass().getDeclaredFields().length );
57  		table.setWidth( 90 );
58  		table.setBorderWidth( 1 );
59  		table.setBorderColor( Color.BLACK );
60  		table.setBackgroundColor( Color.LIGHT_GRAY );
61  		table.setPadding( 3 );
62  		return table;
63  	}
64  
65  	private void generateTableHeader( final T object, final Table table ) throws Exception {
66  		Field[] fields = object.getClass().getDeclaredFields();
67  		for( Field field : fields ) {
68  			field.setAccessible( true );
69  			table.addCell( field.getName().toUpperCase() );
70  		}
71  	}
72  
73  	private void generateTableBody( final List<PartyNameType> partyNameTypes, final Table table ) throws Exception {
74  		for( PartyNameType partyNameType : partyNameTypes ) {
75  			T object = this.nameAdaptor.partyNameType2Name( partyNameType );
76  			Field[] fields = object.getClass().getDeclaredFields();
77  			for( Field field : fields ) {
78  				field.setAccessible( true );
79  				table.addCell( String.valueOf( field.get( object ) ) );
80  			}
81  		}
82  	}
83  
84  	/***
85  	 * Write access to private field.
86  	 * @param nameAdaptor
87  	 *            The nameAdaptor to set.
88  	 */
89  	@Required
90  	public void setNameAdaptor( final NameAdaptor<T> nameAdaptor ) {
91  		this.nameAdaptor = nameAdaptor;
92  	}
93  
94  }