1 package at.ac.tuwien.vitalab.hrcrm.pdf.party; 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.PartyAdaptor; 15 import at.ac.tuwien.vitalab.hrcrm.dto.party.PartyType; 16 17 import com.lowagie.text.Document; 18 import com.lowagie.text.Table; 19 import com.lowagie.text.pdf.PdfWriter; 20 21 public class PartyPdfView<T> extends AbstractPdfView { 22 private PartyAdaptor<T> partyAdaptor; 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<PartyType> partyTypes = (List<PartyType>) model.get( "partyTypes" ); 30 if( partyTypes.size() == 0 ) 31 throw new IllegalArgumentException( "Unable to generate pdf file. Dadabase is empty!" ); 32 33 T object = this.partyAdaptor.partyType2Party( partyTypes.get( 0 ) ); 34 Table table = generateTableStub( object ); 35 36 this.generateTableHeader( object, table ); 37 this.generateTableBody( partyTypes, 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 Party Names" ); 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<PartyType> partyTypes, final Table table ) throws Exception { 72 for( PartyType partyType : partyTypes ) { 73 T object = this.partyAdaptor.partyType2Party( partyType ); 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 partyAdaptor 85 * The partyAdaptor to set. 86 */ 87 @Required 88 public void setPartyAdaptor( final PartyAdaptor<T> partyAdaptor ) { 89 this.partyAdaptor = partyAdaptor; 90 } 91 92 }