View Javadoc

1   package at.ac.tuwien.vitalab.hrcrm.xsl.address;
2   
3   import java.lang.reflect.Field;
4   import java.util.List;
5   import java.util.Map;
6   
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpServletResponse;
9   
10  import org.apache.poi.hssf.usermodel.HSSFCell;
11  import org.apache.poi.hssf.usermodel.HSSFCellStyle;
12  import org.apache.poi.hssf.usermodel.HSSFRow;
13  import org.apache.poi.hssf.usermodel.HSSFSheet;
14  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
15  import org.apache.poi.hssf.util.HSSFColor;
16  import org.springframework.beans.factory.annotation.Required;
17  import org.springframework.web.servlet.view.document.AbstractExcelView;
18  
19  import at.ac.tuwien.vitalab.hrcrm.adaptor.AddressAdaptor;
20  import at.ac.tuwien.vitalab.hrcrm.dto.address.AddressType;
21  
22  public class AddressXslView<T> extends AbstractExcelView {
23  	private AddressAdaptor<T>	addressAdaptor;
24  
25  	@Override
26  	@SuppressWarnings( "unchecked" )
27  	protected void buildExcelDocument( Map model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response )
28  			throws Exception {
29  		List<AddressType> addressTypes = (List<AddressType>) model.get( "address.xsl" );
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  
35  		HSSFSheet sheet = workbook.createSheet( "Availabvle Adresses" );
36  		int rowNum = 0;
37  		rowNum = this.generateTableHeader( workbook, object, sheet, rowNum );
38  
39  		for( int i = 0; i < addressTypes.size(); i++ ) {
40  			object = this.addressAdaptor.addressType2Address( addressTypes.get( i ) );
41  			Field[] fields = object.getClass().getDeclaredFields();
42  
43  			for( int j = 0; j < fields.length; j++ ) {
44  				Field field = fields[j];
45  				field.setAccessible( true );
46  
47  				HSSFRow row = sheet.createRow( rowNum );
48  				HSSFCell cell = row.createCell( (short) j );
49  				cell.setCellValue( String.valueOf( field.get( object ) ) );
50  			}
51  			rowNum++;
52  		}
53  	}
54  
55  	private int generateTableHeader( final HSSFWorkbook workbook, final T object, final HSSFSheet sheet, int rowNum ) throws Exception {
56  		Field[] fields = object.getClass().getDeclaredFields();
57  		for( int i = 0; i < fields.length; i++ ) {
58  			Field field = fields[i];
59  			field.setAccessible( true );
60  
61  			HSSFRow row = sheet.createRow( rowNum );
62  			HSSFCell cell = row.createCell( (short) i );
63  			cell.setCellValue( field.getName().toUpperCase() );
64  
65  			HSSFCellStyle style = workbook.createCellStyle();
66  			style.setFillBackgroundColor( HSSFColor.AQUA.index );
67  			style.setFillPattern( HSSFCellStyle.BIG_SPOTS );
68  			cell.setCellStyle( style );
69  		}
70  		return ++rowNum;
71  	}
72  
73  	//	}
74  
75  	/***
76  	 * Write access to private field.
77  	 * @param addressAdaptor
78  	 *            The addressAdaptor to set.
79  	 */
80  	@Required
81  	public void setAddressAdaptor( final AddressAdaptor<T> addressAdaptor ) {
82  		this.addressAdaptor = addressAdaptor;
83  	}
84  
85  }