1 package at.ac.tuwien.vitalab.hrcrm.xsl.party;
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.PartyAdaptor;
20 import at.ac.tuwien.vitalab.hrcrm.dto.party.PartyType;
21
22 public class PartyXslView<T> extends AbstractExcelView {
23 private PartyAdaptor<T> partyAdaptor;
24
25 @Override
26 @SuppressWarnings( "unchecked" )
27 protected void buildExcelDocument( Map model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response )
28 throws Exception {
29 List<PartyType> partyTypes = (List<PartyType>) model.get( "party.xsl" );
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
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 < partyTypes.size(); i++ ) {
40 object = this.partyAdaptor.partyType2Party( partyTypes.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 partyAdaptor
78 * The partyAdaptor to set.
79 */
80 @Required
81 public void setPartyAdaptor( final PartyAdaptor<T> partyAdaptor ) {
82 this.partyAdaptor = partyAdaptor;
83 }
84
85 }