001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package twopagecrudtable;
043:
044: import com.sun.data.provider.RowKey;
045: import com.sun.data.provider.impl.CachedRowSetDataProvider;
046: import com.sun.rave.web.ui.appbase.AbstractPageBean;
047: import com.sun.webui.jsf.component.Body;
048: import com.sun.webui.jsf.component.Button;
049: import com.sun.webui.jsf.component.Calendar;
050: import com.sun.webui.jsf.component.DropDown;
051: import com.sun.webui.jsf.component.Form;
052: import com.sun.webui.jsf.component.Head;
053: import com.sun.webui.jsf.component.Html;
054: import com.sun.webui.jsf.component.Label;
055: import com.sun.webui.jsf.component.Link;
056: import com.sun.webui.jsf.component.MessageGroup;
057: import com.sun.webui.jsf.component.Page;
058: import com.sun.webui.jsf.component.TextField;
059: import javax.faces.FacesException;
060: import javax.faces.component.html.HtmlPanelGrid;
061: import javax.faces.convert.IntegerConverter;
062:
063: /**
064: * <p>Page bean that corresponds to a similarly named JSP page. This
065: * class contains component definitions (and initialization code) for
066: * all components that you have defined on this page, as well as
067: * lifecycle methods and event handlers where you may add behavior
068: * to respond to incoming events.</p>
069: */
070: public class CreatePage extends AbstractPageBean {
071: // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
072: private int __placeholder;
073:
074: /**
075: * <p>Automatically managed component initialization. <strong>WARNING:</strong>
076: * This method is automatically generated, so any user-specified code inserted
077: * here is subject to being replaced.</p>
078: */
079: private void _init() throws Exception {
080: triptypeDataProvider
081: .setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.triptypeRowSet}"));
082: tripDataProvider
083: .setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.tripRowSet}"));
084: }
085:
086: private Page page1 = new Page();
087:
088: public Page getPage1() {
089: return page1;
090: }
091:
092: public void setPage1(Page p) {
093: this .page1 = p;
094: }
095:
096: private Html html1 = new Html();
097:
098: public Html getHtml1() {
099: return html1;
100: }
101:
102: public void setHtml1(Html h) {
103: this .html1 = h;
104: }
105:
106: private Head head1 = new Head();
107:
108: public Head getHead1() {
109: return head1;
110: }
111:
112: public void setHead1(Head h) {
113: this .head1 = h;
114: }
115:
116: private Link link1 = new Link();
117:
118: public Link getLink1() {
119: return link1;
120: }
121:
122: public void setLink1(Link l) {
123: this .link1 = l;
124: }
125:
126: private Body body1 = new Body();
127:
128: public Body getBody1() {
129: return body1;
130: }
131:
132: public void setBody1(Body b) {
133: this .body1 = b;
134: }
135:
136: private Form form1 = new Form();
137:
138: public Form getForm1() {
139: return form1;
140: }
141:
142: public void setForm1(Form f) {
143: this .form1 = f;
144: }
145:
146: private HtmlPanelGrid mainPanel = new HtmlPanelGrid();
147:
148: public HtmlPanelGrid getMainPanel() {
149: return mainPanel;
150: }
151:
152: public void setMainPanel(HtmlPanelGrid hpg) {
153: this .mainPanel = hpg;
154: }
155:
156: private HtmlPanelGrid tripPanel = new HtmlPanelGrid();
157:
158: public HtmlPanelGrid getTripPanel() {
159: return tripPanel;
160: }
161:
162: public void setTripPanel(HtmlPanelGrid hpg) {
163: this .tripPanel = hpg;
164: }
165:
166: private HtmlPanelGrid buttonPanel = new HtmlPanelGrid();
167:
168: public HtmlPanelGrid getButtonPanel() {
169: return buttonPanel;
170: }
171:
172: public void setButtonPanel(HtmlPanelGrid hpg) {
173: this .buttonPanel = hpg;
174: }
175:
176: private Label label1 = new Label();
177:
178: public Label getLabel1() {
179: return label1;
180: }
181:
182: public void setLabel1(Label l) {
183: this .label1 = l;
184: }
185:
186: private Label label2 = new Label();
187:
188: public Label getLabel2() {
189: return label2;
190: }
191:
192: public void setLabel2(Label l) {
193: this .label2 = l;
194: }
195:
196: private Label label3 = new Label();
197:
198: public Label getLabel3() {
199: return label3;
200: }
201:
202: public void setLabel3(Label l) {
203: this .label3 = l;
204: }
205:
206: private Label label4 = new Label();
207:
208: public Label getLabel4() {
209: return label4;
210: }
211:
212: public void setLabel4(Label l) {
213: this .label4 = l;
214: }
215:
216: private Calendar dateCalendar = new Calendar();
217:
218: public Calendar getDateCalendar() {
219: return dateCalendar;
220: }
221:
222: public void setDateCalendar(Calendar c) {
223: this .dateCalendar = c;
224: }
225:
226: private TextField fromCity = new TextField();
227:
228: public TextField getFromCity() {
229: return fromCity;
230: }
231:
232: public void setFromCity(TextField tf) {
233: this .fromCity = tf;
234: }
235:
236: private TextField toCity = new TextField();
237:
238: public TextField getToCity() {
239: return toCity;
240: }
241:
242: public void setToCity(TextField tf) {
243: this .toCity = tf;
244: }
245:
246: private DropDown tripType = new DropDown();
247:
248: public DropDown getTripType() {
249: return tripType;
250: }
251:
252: public void setTripType(DropDown dd) {
253: this .tripType = dd;
254: }
255:
256: private Button addButton = new Button();
257:
258: public Button getAddButton() {
259: return addButton;
260: }
261:
262: public void setAddButton(Button b) {
263: this .addButton = b;
264: }
265:
266: private Button cancel = new Button();
267:
268: public Button getCancel() {
269: return cancel;
270: }
271:
272: public void setCancel(Button b) {
273: this .cancel = b;
274: }
275:
276: private MessageGroup messageGroup1 = new MessageGroup();
277:
278: public MessageGroup getMessageGroup1() {
279: return messageGroup1;
280: }
281:
282: public void setMessageGroup1(MessageGroup mg) {
283: this .messageGroup1 = mg;
284: }
285:
286: private CachedRowSetDataProvider triptypeDataProvider = new CachedRowSetDataProvider();
287:
288: public CachedRowSetDataProvider getTriptypeDataProvider() {
289: return triptypeDataProvider;
290: }
291:
292: public void setTriptypeDataProvider(CachedRowSetDataProvider crsdp) {
293: this .triptypeDataProvider = crsdp;
294: }
295:
296: private IntegerConverter tripTypeConverter = new IntegerConverter();
297:
298: public IntegerConverter getTripTypeConverter() {
299: return tripTypeConverter;
300: }
301:
302: public void setTripTypeConverter(IntegerConverter ic) {
303: this .tripTypeConverter = ic;
304: }
305:
306: private CachedRowSetDataProvider tripDataProvider = new CachedRowSetDataProvider();
307:
308: public CachedRowSetDataProvider getTripDataProvider() {
309: return tripDataProvider;
310: }
311:
312: public void setTripDataProvider(CachedRowSetDataProvider crsdp) {
313: this .tripDataProvider = crsdp;
314: }
315:
316: // </editor-fold>
317:
318: /**
319: * <p>Construct a new Page bean instance.</p>
320: */
321: public CreatePage() {
322: }
323:
324: /**
325: * <p>Callback method that is called whenever a page is navigated to,
326: * either directly via a URL, or indirectly via page navigation.
327: * Customize this method to acquire resources that will be needed
328: * for event handlers and lifecycle methods, whether or not this
329: * page is performing post back processing.</p>
330: *
331: * <p>Note that, if the current request is a postback, the property
332: * values of the components do <strong>not</strong> represent any
333: * values submitted with this request. Instead, they represent the
334: * property values that were saved for this view when it was rendered.</p>
335: */
336: @Override
337: public void init() {
338: // Perform initializations inherited from our superclass
339: super .init();
340: // Perform application initialization that must complete
341: // *before* managed components are initialized
342: // TODO - add your own initialiation code here
343:
344: // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
345: // Initialize automatically managed components
346: // *Note* - this logic should NOT be modified
347: try {
348: _init();
349: } catch (Exception e) {
350: log("CreatePage Initialization Failure", e);
351: throw e instanceof FacesException ? (FacesException) e
352: : new FacesException(e);
353: }
354:
355: // </editor-fold>
356: // Perform application initialization that must complete
357: // *after* managed components are initialized
358: // TODO - add your own initialization code here
359: }
360:
361: /**
362: * <p>Callback method that is called after the component tree has been
363: * restored, but before any event processing takes place. This method
364: * will <strong>only</strong> be called on a postback request that
365: * is processing a form submit. Customize this method to allocate
366: * resources that will be required in your event handlers.</p>
367: */
368: @Override
369: public void preprocess() {
370: }
371:
372: /**
373: * <p>Callback method that is called just before rendering takes place.
374: * This method will <strong>only</strong> be called for the page that
375: * will actually be rendered (and not, for example, on a page that
376: * handled a postback and then navigated to a different page). Customize
377: * this method to allocate resources that will be required for rendering
378: * this page.</p>
379: */
380: @Override
381: public void prerender() {
382: }
383:
384: /**
385: * <p>Callback method that is called after rendering is completed for
386: * this request, if <code>init()</code> was called (regardless of whether
387: * or not this was the page that was actually rendered). Customize this
388: * method to release resources acquired in the <code>init()</code>,
389: * <code>preprocess()</code>, or <code>prerender()</code> methods (or
390: * acquired during execution of an event handler).</p>
391: */
392: @Override
393: public void destroy() {
394: triptypeDataProvider.close();
395: tripDataProvider.close();
396: }
397:
398: /**
399: * <p>Return a reference to the scoped data bean.</p>
400: */
401: protected RequestBean1 getRequestBean1() {
402: return (RequestBean1) getBean("RequestBean1");
403: }
404:
405: /**
406: * <p>Return a reference to the scoped data bean.</p>
407: */
408: protected SessionBean1 getSessionBean1() {
409: return (SessionBean1) getBean("SessionBean1");
410: }
411:
412: /**
413: * <p>Return a reference to the scoped data bean.</p>
414: */
415: protected ApplicationBean1 getApplicationBean1() {
416: return (ApplicationBean1) getBean("ApplicationBean1");
417: }
418:
419: public String addButton_action() {
420: // TODO: Process the action. Return value is a navigation
421: // case name where null will return to the same page.
422: if (tripDataProvider.canAppendRow()) {
423: try {
424: RowKey rowKey = tripDataProvider.appendRow();
425: tripDataProvider.setCursorRow(rowKey);
426: Integer tid = nextPK();
427: tripDataProvider.setValue("TRIP.TRIPID", rowKey, tid);
428: Integer pid = getSessionBean1().getCurrentPersonId();
429: tripDataProvider.setValue("TRIP.PERSONID", rowKey, pid);
430: java.util.Date depDate = (java.util.Date) dateCalendar
431: .getValue();
432: if (depDate != null) {
433: java.sql.Date date = new java.sql.Date(depDate
434: .getTime());
435: tripDataProvider.setValue("TRIP.DEPDATE", rowKey,
436: date);
437: }
438: tripDataProvider.setValue("TRIP.DEPCITY", rowKey,
439: fromCity.getValue());
440: tripDataProvider.setValue("TRIP.DESTCITY", rowKey,
441: toCity.getValue());
442: tripDataProvider.setValue("TRIP.TRIPTYPEID", rowKey,
443: tripType.getSelected());
444: tripDataProvider.commitChanges();
445: } catch (Exception e) {
446: error("Cannot append new trip: " + e);
447: }
448: } else {
449: error("Cannot append a new row");
450: }
451: return "created";
452: }
453:
454: public String cancel_action() {
455: // TODO: Process the action. Return value is a navigation
456: // case name where null will return to the same page.
457: return "canceled";
458: }
459:
460: // Generate an integer Primary Key
461: // Return next primary key for PERSON table
462: private Integer nextPK() throws java.sql.SQLException {
463: // create a new rowset
464: com.sun.sql.rowset.CachedRowSetXImpl pkRowSet = new com.sun.sql.rowset.CachedRowSetXImpl();
465: try {
466: // set the rowset to use the Travel database
467: pkRowSet
468: .setDataSourceName("java:comp/env/jdbc/TRAVEL_ApacheDerby");
469: // find the highest person id and add one to it
470: pkRowSet
471: .setCommand("SELECT MAX(TRAVEL.TRIP.TRIPID) + 1 FROM TRAVEL.TRIP");
472: pkRowSet.setTableName("TRAVEL.TRIP");
473: // execute the rowset -- which will contain a single row and single column
474: pkRowSet.execute();
475: pkRowSet.next();
476: // get the key
477: int counter = pkRowSet.getInt(1);
478: return new Integer(counter);
479: } catch (Exception e) {
480: error("Error fetching Max(TRIPID)+1 : " + e.getMessage());
481: } finally {
482: pkRowSet.close();
483: }
484: return null;
485: }
486: }
|