View Javadoc

1   /**
2    * 
3    */
4   package de.tivsource.page.helper.odf;
5   
6   
7   import java.text.SimpleDateFormat;
8   import java.util.Iterator;
9   import java.util.List;
10  
11  import org.odftoolkit.odfdom.dom.element.style.StyleMasterPageElement;
12  import org.odftoolkit.odfdom.incubator.doc.style.OdfStylePageLayout;
13  import org.odftoolkit.simple.SpreadsheetDocument;
14  import org.odftoolkit.simple.style.Font;
15  import org.odftoolkit.simple.style.PageLayoutProperties;
16  import org.odftoolkit.simple.style.StyleTypeDefinitions.FontStyle;
17  import org.odftoolkit.simple.style.StyleTypeDefinitions.PrintOrientation;
18  import org.odftoolkit.simple.table.Cell;
19  import org.odftoolkit.simple.table.Table;
20  
21  import de.tivsource.page.entity.event.Event;
22  import de.tivsource.page.entity.location.Location;
23  import de.tivsource.page.entity.reservation.Reservation;
24  
25  /**
26   * @author Marc Michele
27   *
28   */
29  public class CreateReservationODF {
30      
31      private SpreadsheetDocument spreadsheetDocument;
32  
33      private Table table;
34  
35      private Cell cell;
36  
37      public SpreadsheetDocument create(List<Reservation> reservations) throws Exception {
38          
39          // Zum formatieren des Datums
40          SimpleDateFormat formatDate = new SimpleDateFormat("dd.MM.yyyy");
41  
42          // Erstelle neues Tabellen Dokument
43          spreadsheetDocument = SpreadsheetDocument.newSpreadsheetDocument();
44  
45          // Formatiere Seite
46          generatePageLayout(spreadsheetDocument);
47  
48          // Hole die erste Tabelle aus dem Dokument
49          table = spreadsheetDocument.getTableList().get(0);
50  
51          // Hole Event aus den Daten
52          Event event = reservations.get(0).getEvent();
53          // Hole Location aus den Daten
54          Location location = reservations.get(0).getEvent().getLocation();
55  
56          // Erzeuge die Überschrift der Tabelle
57          StringBuilder stringBuilder = new StringBuilder();
58          stringBuilder.append("Reservierungen für das ");
59          stringBuilder.append(event.getName("de"));
60          stringBuilder.append(" am ");
61          stringBuilder.append(formatDate.format(event.getBeginning()));
62          stringBuilder.append(" im ");
63          stringBuilder.append(location.getName("de"));
64  
65          table.setTableName(stringBuilder.toString());
66          
67  
68          // Setze die gloabe Überschrift in die Tabelle 
69  //        cell = table.getCellByPosition(0,0);
70  //        cell.setStringValue(stringBuilder.toString());
71  //        cell.setFont(new Font("Arial", FontStyle.BOLD, 12));
72          // Verbinde die Zellen
73  //        CellRange cellRange = table.getCellRangeByPosition(0, 0, 9, 0);
74  //        cellRange.merge();
75          
76  
77          // Erzeuge die Überschriften
78          generateHeader();
79          
80          // Erzeuge Inhalt
81          generateContent(reservations);
82          
83          return spreadsheetDocument;
84      }
85  
86      private void generatePageLayout(SpreadsheetDocument spreadsheetDocument) {
87          StyleMasterPageElement defaultPage = spreadsheetDocument.getOfficeMasterStyles().getMasterPage("Default");
88          String pageLayoutName = defaultPage.getStylePageLayoutNameAttribute();
89          OdfStylePageLayout pageLayoutStyle = defaultPage.getAutomaticStyles().getPageLayout(pageLayoutName);
90          PageLayoutProperties pageLayoutProperties = PageLayoutProperties.getOrCreatePageLayoutProperties(pageLayoutStyle);
91          pageLayoutProperties.setPageHeight(210);
92          pageLayoutProperties.setPageWidth(297);
93          pageLayoutProperties.setPrintOrientation(PrintOrientation.LANDSCAPE);
94      }
95      
96      private void generateHeader() {
97          // Erzeuge die Überschrift der ersten Spalte
98          cell = table.getCellByPosition(0,0);
99          cell.setStringValue("Anrede");
100         cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
101         
102         // Erzeuge die Überschrift der zweiten Spalte
103         cell = table.getCellByPosition(1,0);
104         cell.setStringValue("Vorname");
105         cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
106 
107         cell = table.getCellByPosition(2,0);
108         cell.setStringValue("Nachname");
109         cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
110 
111         cell = table.getCellByPosition(3,0);
112         cell.setStringValue("Telefon");
113         cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
114 
115         cell = table.getCellByPosition(4,0);
116         cell.setStringValue("Personen");
117         cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
118 
119         cell = table.getCellByPosition(5,0);
120         cell.setStringValue("Uhrzeit");
121         cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
122 
123         cell = table.getCellByPosition(6,0);
124         cell.setStringValue("Wünsche");
125         cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
126 
127         /*
128         cell = table.getCellByPosition(7,0);
129         cell.setStringValue("Mail");
130         cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
131         
132         cell = table.getCellByPosition(8,0);
133         cell.setStringValue("IP-Adresse");
134         cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
135 
136         cell = table.getCellByPosition(9,0);
137         cell.setStringValue("Erstellt am");
138         cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
139         */
140     }
141 
142     private void generateContent(List<Reservation> reservations) {
143         SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm");
144         SimpleDateFormat formatDate = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
145         Iterator<Reservation> iterator = reservations.iterator();
146         Integer row = 1;
147         while(iterator.hasNext()) {
148             Reservation next = iterator.next();
149 
150             // Erzeuge die Überschrift der ersten Spalte
151             cell = table.getCellByPosition(0,row);
152             cell.setStringValue( next.getGender() ? "Frau" : "Herr" );
153 
154             // Erzeuge die Überschrift der zweiten Spalte
155             cell = table.getCellByPosition(1,row);
156             cell.setStringValue(next.getFirstname());
157 
158             cell = table.getCellByPosition(2,row);
159             cell.setStringValue(next.getLastname());
160 
161             cell = table.getCellByPosition(3,row);
162             cell.setStringValue(next.getTelephone());
163 
164             cell = table.getCellByPosition(4,row);
165             cell.setDoubleValue(next.getQuantity().doubleValue());
166 
167             cell = table.getCellByPosition(5,row);
168             cell.setStringValue(formatTime.format(next.getTime()));
169 
170             cell = table.getCellByPosition(6,row);
171             cell.setStringValue(next.getWishes());
172 
173             /*
174             cell = table.getCellByPosition(7,row);
175             cell.setStringValue(next.getEmail());
176             
177             cell = table.getCellByPosition(8,row);
178             cell.setStringValue(next.getIp());
179 
180             cell = table.getCellByPosition(9,row);
181             cell.setStringValue(formatDate.format(next.getCreated()));
182             */
183 
184             row++;
185         }
186     }
187     
188 }// Ende class