View Javadoc

1   package org.apache.myfaces.tobago.example.test;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import org.apache.commons.logging.LogFactory;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.fileupload.FileItem;
23  import org.apache.myfaces.tobago.component.UIData;
24  import org.apache.myfaces.tobago.component.UIColumn;
25  import org.apache.myfaces.tobago.component.UICommand;
26  import org.apache.myfaces.tobago.component.UIMenu;
27  import org.apache.myfaces.tobago.component.UIMenuCommand;
28  
29  import javax.faces.component.UIOutput;
30  import javax.faces.component.UIPanel;
31  import javax.faces.context.FacesContext;
32  import javax.faces.el.ValueBinding;
33  import javax.faces.el.MethodBinding;
34  import javax.faces.application.Application;
35  import java.sql.Statement;
36  import java.sql.DriverManager;
37  import java.sql.Connection;
38  import java.sql.PreparedStatement;
39  import java.sql.ResultSet;
40  import java.sql.SQLException;
41  import java.sql.ResultSetMetaData;
42  import java.sql.Types;
43  import java.util.Map;
44  import java.util.Date;
45  import java.io.IOException;
46  
47  /*
48   * Date: 18.02.2006
49   * Time: 11:08:45
50   */
51  public class TestBean {
52    private static final Log LOG = LogFactory.getLog(TestBean.class);
53  
54    private ResultSet resultSet = null;
55    private Connection connection = null;
56  
57    private String name;
58    private String number;
59    private String orbit;
60    private String distance;
61    private String period;
62    private String incl;
63    private String eccen;
64    private String discoverer;
65    private String discoverYear;
66    private FileItem file;
67    private UIData table;
68    private String value;
69    private Date date;
70    private Date date1;
71    private UIMenu fileMenu;
72  
73  
74    public UIPanel getFileMenu() {
75      if (fileMenu == null) {
76        FacesContext context = FacesContext.getCurrentInstance();
77  
78        fileMenu = (UIMenu) context.getApplication().createComponent(UIMenu.COMPONENT_TYPE);
79  
80        fileMenu.getAttributes().put("label", "File");
81      }
82      if (fileMenu.getChildCount() == 0) {
83        for(int i = 0; i < 5; i++) {
84          addMenuCommand(fileMenu);
85        }
86      }
87  
88      return fileMenu;
89  
90    }
91  
92    private void addMenuCommand(UIMenu fileMenu) {
93      UIMenuCommand command = (UIMenuCommand)
94          FacesContext.getCurrentInstance().getApplication().createComponent(UIMenuCommand.COMPONENT_TYPE);
95      command.getAttributes().put("label", "test"+fileMenu.getChildCount());
96      // TODO setAction
97      fileMenu.getChildren().add(command);
98    }
99  
100   public String layout() {
101     this.date1 = date;
102     return "layout";
103   }
104 
105   public String export() throws IOException {
106     FacesContext context =  FacesContext.getCurrentInstance();
107     ExportUIDataToWorkbookUtil.writeWorkbook(table, "workbook.xls", context);
108     context.responseComplete();
109     return null;
110   }
111 
112   public Date getDate() {
113     return date;
114   }
115 
116   public void setDate(Date date) {
117     this.date = date;
118   }
119 
120   public Date getDate1() {
121     return date1;
122   }
123 
124   public void setDate1(Date date1) {
125     this.date1 = date1;
126   }
127 
128   public String getValue() {
129     return value;
130   }
131 
132   public void setValue(String value) {
133     this.value = value;
134   }
135 
136   public TestBean() {
137 
138     try {
139       //Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
140       Class.forName("org.hsqldb.jdbcDriver").newInstance();
141       LOG.info("Loaded the appropriate driver.");
142 
143       //Properties props = new Properties();
144       //props.put("user", "user1");
145       //props.put("password", "user1");
146 
147       /*
148         The connection specifies create=true to cause
149         the database to be created. To remove the database,
150         remove the directory derbyDB and its contents.
151         The directory derbyDB will be created under
152         the directory that the system property
153         derby.system.home points to, or the current
154         directory if derby.system.home is not set.
155       */
156       //Connection conn = DriverManager.getConnection("jdbc:derby:" +
157       //    "derbyDB;create=true", props);
158       connection = DriverManager.getConnection("jdbc:hsqldb:mem:aname", "sa", "");
159 
160       LOG.info("Connected to and created database derbyDB");
161 
162       connection.setAutoCommit(false);
163 
164       Statement statement = connection.createStatement();
165       try {
166         statement.execute("create table solarObject(name varchar(10), number varchar(5), "
167             + "orbit varchar(10), distance INTEGER, period FLOAT, "
168             + "incl FLOAT, eccen FLOAT, discoverer varchar(20), "
169             + "discoverYear INTEGER)");
170         PreparedStatement ps =
171             connection.prepareStatement("insert into solarObject values (?,?,?,?,?,?,?,?,?)");
172         for (String[] aSTRINGS : STRINGS) {
173           for (int j = 0; j < aSTRINGS.length; j++) {
174             ps.setString(j + 1, aSTRINGS[j]);
175           }
176           int inserted = ps.executeUpdate();
177           if (LOG.isDebugEnabled()) {
178             LOG.debug(inserted + " Row(s) inserted");
179           }
180         }
181         connection.commit();
182       } catch (SQLException e) {
183         LOG.error("", e);
184       }
185 
186       Statement query = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
187       resultSet = query.executeQuery("select * from solarObject");
188 
189 
190     } catch (Exception e) {
191       LOG.error("", e);
192     }
193   }
194 
195 
196   public UIData getTable() {
197     return table;
198   }
199 
200   public void setTable(UIData table) {
201     this.table = table;
202     if (table.getChildCount() == 0) {
203       if (table.getVar() == null) {
204         table.setVar("solarObject");
205       }
206       Application application = FacesContext.getCurrentInstance().getApplication();
207       try {
208         ResultSetMetaData metaData = resultSet.getMetaData();
209         String columns = "";
210         for (int i = 1; i <= metaData.getColumnCount(); i++) {
211 
212           UIColumn column = (UIColumn) application.createComponent(UIColumn.COMPONENT_TYPE);
213           String name = metaData.getColumnName(i);
214           int displaySize = metaData.getColumnDisplaySize(i);
215 
216           if (i > 1) {
217             columns += ";";
218           }
219           if (displaySize < 10) {
220             columns += "1*";
221           } else if (displaySize < 20) {
222             columns += "2*";
223           } else {
224             columns += "4*";
225           }
226           if (metaData.getColumnType(i) == Types.INTEGER || metaData.getColumnType(i) == Types.FLOAT) {
227             column.setAlign("right");
228           }
229           column.setLabel(name);
230           String ref = "#{" + table.getVar() + "." + name + "}";
231           ValueBinding binding = application.createValueBinding(ref);
232           if (name.equals("NAME")) {
233             UICommand command = (UICommand) application.createComponent(UICommand.COMPONENT_TYPE);
234             command.setRendererType("Link");
235             command.setValueBinding("label", binding);
236             MethodBinding action = application.createMethodBinding("#{test.select}", new Class[0]);
237             command.setAction(action);
238             column.getChildren().add(command);
239           } else {
240             UIOutput output = (UIOutput) application.createComponent(UIOutput.COMPONENT_TYPE);
241             output.setValueBinding("value", binding);
242             column.getChildren().add(output);
243           }
244           table.getChildren().add(column);
245         }
246 
247         table.setColumns(columns);
248       } catch (SQLException e) {
249         LOG.error("", e);
250       }
251 
252     }
253   }
254 
255   public String getName() {
256     return name;
257   }
258 
259   public String getNumber() {
260     return number;
261   }
262 
263   public String getOrbit() {
264     return orbit;
265   }
266 
267   public String getDistance() {
268     return distance;
269   }
270 
271   public String getPeriod() {
272     return period;
273   }
274 
275   public String getIncl() {
276     return incl;
277   }
278 
279   public String getEccen() {
280     return eccen;
281   }
282 
283   public String getDiscoverer() {
284     return discoverer;
285   }
286 
287   public String getDiscoverYear() {
288     return discoverYear;
289   }
290 
291   public String select() {
292     try {
293       Map rowData = (Map) table.getRowData();
294 
295       name = (String) rowData.get("NAME");
296       number = (String) rowData.get("NUMBER");
297       orbit = (String) rowData.get("ORBIT");
298       distance = (String) rowData.get("DISTANCE");
299       period = (String) rowData.get("PERIOD");
300       incl = (String) rowData.get("INCL");
301       eccen = (String) rowData.get("ECCEN");
302       discoverer = (String) rowData.get("DISCOVERER");
303       discoverYear = (String) rowData.get("DISCOVERYEAR");
304     } catch (Exception e) {
305       LOG.error("", e);
306     }
307     return "solarDetail";
308   }
309 
310   public String select(String id) {
311     PreparedStatement ps = null;
312     ResultSet rs = null;
313     try {
314       ps = connection.prepareStatement("select * from solarObject where name = ? ");
315       ps.setString(1, id);
316       rs = ps.executeQuery();
317       if (rs.next()) {
318         name = rs.getString(1);
319         number = rs.getString(2);
320         orbit = rs.getString(3);
321         distance = rs.getString(4);
322         period = rs.getString(5);
323         incl = rs.getString(6);
324         eccen = rs.getString(7);
325         discoverer = rs.getString(8);
326         discoverYear = rs.getString(9);
327         return "solarDetail";
328       } else {
329         name = null;
330         number = null;
331         orbit = null;
332         distance = null;
333         period = null;
334         incl = null;
335         eccen = null;
336         discoverer = null;
337         discoverYear = null;
338       }
339     } catch (SQLException e) {
340       LOG.error("", e);
341     } finally {
342       if (rs != null) {
343         try {
344           rs.close();
345         } catch (SQLException e) {
346           // ignore
347         }
348       }
349       if (ps != null) {
350         try {
351           ps.close();
352         } catch (SQLException e) {
353           // ignore
354         }
355       }
356     }
357 
358     return "solarList";
359 
360   }
361 
362   public FileItem getFile() {
363     return file;
364   }
365 
366   public void setFile(FileItem file) {
367     LOG.error("Setting fileItem " + file);
368     this.file = file;
369   }
370 
371   public ResultSet getSolarObjects() {
372     if (LOG.isDebugEnabled()) {
373       LOG.debug("getting solar objects");
374     }
375     return resultSet;
376   }
377 
378 
379   private static final String[][] STRINGS =
380       {
381           {"Sun", "-", "-", "0", "0", "0", "0", "-", "0"},
382           {"Mercury", "I", "Sun", "57910", "87.97", "7.00", "0.21", "-", null},
383           {"Venus", "II", "Sun", "108200", "224.70", "3.39", "0.01", "-", null},
384           {"Earth", "III", "Sun", "149600", "365.26", "0.00", "0.02", "-", null},
385           {"Mars", "IV", "Sun", "227940", "686.98", "1.85", "0.09", "-", null},
386           {"Jupiter", "V", "Sun", "778330", "4332.71", "1.31", "0.05", "-", null},
387           {"Saturn", "VI", "Sun", "1429400", "10759.50", "2.49", "0.06", "-", null},
388           {"Uranus", "VII", "Sun", "2870990", "30685.00", "0.77", "0.05", "Herschel", "1781"},
389           {"Neptune", "VIII", "Sun", "4504300", "60190.00", "1.77", "0.01", "Adams", "1846"},
390           {"Pluto", "IX", "Sun", "5913520", "90800", "17.15", "0.25", "Tombaugh", "1930"},
391           {"Moon", "I", "Earth", "384", "27.32", "5.14", "0.05", "-", null},
392           {"Phobos", "I", "Mars", "9", "0.32", "1.00", "0.02", "Hall", "1877"},
393           {"Deimos", "II", "Mars", "23", "1.26", "1.80", "0.00", "Hall", "1877"},
394           {"Metis", "XVI", "Jupiter", "128", "0.29", "0.00", "0.00", "Synnott", "1979"},
395           {"Adrastea", "XV", "Jupiter", "129", "0.30", "0.00", "0.00", "Jewitt", "1979"},
396           {"Amalthea", "V", "Jupiter", "181", "0.50", "0.40", "0.00", "Barnard", "1892"},
397           {"Thebe", "XIV", "Jupiter", "222", "0.67", "0.80", "0.02", "Synnott", "1979"},
398           {"Io", "I", "Jupiter", "422", "1.77", "0.04", "0.00", "Galileo", "1610"},
399           {"Europa", "II", "Jupiter", "671", "3.55", "0.47", "0.01", "Galileo", "1610"},
400           {"Ganymede", "III", "Jupiter", "1070", "7.15", "0.19", "0.00", "Galileo", "1610"},
401           {"Callisto", "IV", "Jupiter", "1883", "16.69", "0.28", "0.01", "Galileo", "1610"},
402           {"Themisto", "XVIII", "Jupiter", "7507", "0", null, null, "Sheppard", "2000"},
403           {"Leda", "XIII", "Jupiter", "11094", "238.72", "27.00", "0.15", "Kowal", "1974"},
404           {"Himalia", "VI", "Jupiter", "11480", "250.57", "28.00", "0.16", "Perrine", "1904"},
405           {"Lysithea", "X", "Jupiter", "11720", "259.22", "29.00", "0.11", "Nicholson", "1938"},
406           {"Elara", "VII", "Jupiter", "11737", "259.65", "28.00", "0.21", "Perrine", "1905"},
407           {"Ananke", "XII", "Jupiter", "21200", "-631", "147.00", "0.17", "Nicholson", "1951"},
408           {"Carme", "XI", "Jupiter", "22600", "-692", "163.00", "0.21", "Nicholson", "1938"},
409           {"Pasiphae", "VIII", "Jupiter", "23500", "-735", "147.00", "0.38", "Melotte", "1908"},
410           {"Sinope", "IX", "Jupiter", "23700", "-758", "153.00", "0.28", "Nicholson", "1914"},
411           {"Iocaste", "XXIV", "Jupiter", "20216", "0", null, null, "Sheppard", "2000"},
412           {"Harpalyke", "XXII", "Jupiter", "21132", "0", null, null, "Sheppard", "2000"},
413           {"Praxidike", "XXVII", "Jupiter", "20964", "0", null, null, "Sheppard", "2000"},
414           {"Taygete", "XX", "Jupiter", "23312", "0", null, null, "Sheppard", "2000"},
415           {"Chaldene", "XXI", "Jupiter", "23387", "0", null, null, "Sheppard", "2000"},
416           {"Kalyke", "XXIII", "Jupiter", "23745", "0", null, null, "Sheppard", "2000"},
417           {"Callirrhoe", "XVII", "Jupiter", "24100", "0", null, null, "Sheppard", "2000"},
418           {"Megaclite", "XIX", "Jupiter", "23911", "0", null, null, "Sheppard", "2000"},
419           {"Isonoe", "XXVI", "Jupiter", "23078", "0", null, null, "Sheppard", "2000"},
420           {"Erinome", "XXV", "Jupiter", "23168", "0", null, null, "Sheppard", "2000"},
421           {"Pan", "XVIII", "Saturn", "134", "0.58", "0.00", "0.00", "Showalter", "1990"},
422           {"Atlas", "XV", "Saturn", "138", "0.60", "0.00", "0.00", "Terrile", "1980"},
423           {"Prometheus", "XVI", "Saturn", "139", "0.61", "0.00", "0.00", "Collins", "1980"},
424           {"Pandora", "XVII", "Saturn", "142", "0.63", "0.00", "0.00", "Collins", "1980"},
425           {"Epimetheus", "XI", "Saturn", "151", "0.69", "0.34", "0.01", "Walker", "1980"},
426           {"Janus", "X", "Saturn", "151", "0.69", "0.14", "0.01", "Dollfus", "1966"},
427           {"Mimas", "I", "Saturn", "186", "0.94", "1.53", "0.02", "Herschel", "1789"},
428           {"Enceladus", "II", "Saturn", "238", "1.37", "0.02", "0.00", "Herschel", "1789"},
429           {"Tethys", "III", "Saturn", "295", "1.89", "1.09", "0.00", "Cassini", "1684"},
430           {"Telesto", "XIII", "Saturn", "295", "1.89", "0.00", "0.00", "Smith", "1980"},
431           {"Calypso", "XIV", "Saturn", "295", "1.89", "0.00", "0.00", "Pascu", "1980"},
432           {"Dione", "IV", "Saturn", "377", "2.74", "0.02", "0.00", "Cassini", "1684"},
433           {"Helene", "XII", "Saturn", "377", "2.74", "0.20", "0.01", "Laques", "1980"},
434           {"Rhea", "V", "Saturn", "527", "4.52", "0.35", "0.00", "Cassini", "1672"},
435           {"Titan", "VI", "Saturn", "1222", "15.95", "0.33", "0.03", "Huygens", "1655"},
436           {"Hyperion", "VII", "Saturn", "1481", "21.28", "0.43", "0.10", "Bond", "1848"},
437           {"Iapetus", "VIII", "Saturn", "3561", "79.33", "14.72", "0.03", "Cassini", "1671"},
438           {"Phoebe", "IX", "Saturn", "12952", "-550.48", "175.30", "0.16", "Pickering", "1898"},
439           {"Cordelia", "VI", "Uranus", "50", "0.34", "0.14", "0.00", "Voyager 2", "1986"},
440           {"Ophelia", "VII", "Uranus", "54", "0.38", "0.09", "0.00", "Voyager 2", "1986"},
441           {"Bianca", "VIII", "Uranus", "59", "0.43", "0.16", "0.00", "Voyager 2", "1986"},
442           {"Cressida", "IX", "Uranus", "62", "0.46", "0.04", "0.00", "Voyager 2", "1986"},
443           {"Desdemona", "X", "Uranus", "63", "0.47", "0.16", "0.00", "Voyager 2", "1986"},
444           {"Juliet", "XI", "Uranus", "64", "0.49", "0.06", "0.00", "Voyager 2", "1986"},
445           {"Portia", "XII", "Uranus", "66", "0.51", "0.09", "0.00", "Voyager 2", "1986"},
446           {"Rosalind", "XIII", "Uranus", "70", "0.56", "0.28", "0.00", "Voyager 2", "1986"},
447           {"Belinda", "XIV", "Uranus", "75", "0.62", "0.03", "0.00", "Voyager 2", "1986"},
448           {"1986U10", "", "Uranus", "76", "0.64", null, null, "Karkoschka", "1999"},
449           {"Puck", "XV", "Uranus", "86", "0.76", "0.31", "0.00", "Voyager 2", "1985"},
450           {"Miranda", "V", "Uranus", "130", "1.41", "4.22", "0.00", "Kuiper", "1948"},
451           {"Ariel", "I", "Uranus", "191", "2.52", "0.00", "0.00", "Lassell", "1851"},
452           {"Umbriel", "II", "Uranus", "266", "4.14", "0.00", "0.00", "Lassell", "1851"},
453           {"Titania", "III", "Uranus", "436", "8.71", "0.00", "0.00", "Herschel", "1787"},
454           {"Oberon", "IV", "Uranus", "583", "13.46", "0.00", "0.00", "Herschel", "1787"},
455           {"Caliban", "XVI", "Uranus", "7169", "-580", "140.", "0.08", "Gladman", "1997"},
456           {"Stephano", "XX", "Uranus", "7948", "-674", "143.", "0.24", "Gladman", "1999"},
457           {"Sycorax", "XVII", "Uranus", "12213", "-1289", "153.", "0.51", "Nicholson", "1997"},
458           {"Prospero", "XVIII", "Uranus", "16568", "-2019", "152.", "0.44", "Holman", "1999"},
459           {"Setebos", "XIX", "Uranus", "17681", "-2239", "158.", "0.57", "Kavelaars", "1999"},
460           {"Naiad", "III", "Neptune", "48", "0.29", "0.00", "0.00", "Voyager 2", "1989"},
461           {"Thalassa", "IV", "Neptune", "50", "0.31", "4.50", "0.00", "Voyager 2", "1989"},
462           {"Despina", "V", "Neptune", "53", "0.33", "0.00", "0.00", "Voyager 2", "1989"},
463           {"Galatea", "VI", "Neptune", "62", "0.43", "0.00", "0.00", "Voyager 2", "1989"},
464           {"Larissa", "VII", "Neptune", "74", "0.55", "0.00", "0.00", "Reitsema", "1989"},
465           {"Proteus", "VIII", "Neptune", "118", "1.12", "0.00", "0.00", "Voyager 2", "1989"},
466           {"Triton", "I", "Neptune", "355", "-5.88", "157.00", "0.00", "Lassell", "1846"},
467           {"Nereid", "II", "Neptune", "5513", "360.13", "29.00", "0.75", "Kuiper", "1949"},
468           {"Charon", "I", "Pluto", "20", "6.39", "98.80", "0.00", "Christy", "1978"}
469       };
470 
471 }
472