You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sk...@apache.org on 2008/02/11 22:57:18 UTC

svn commit: r620628 - in /myfaces/orchestra/trunk/examples/src/main: java/org/apache/myfaces/examples/simple/ java/org/apache/myfaces/examples/simple/backings/ java/org/apache/myfaces/examples/simple/model/ webapp/simple/

Author: skitching
Date: Mon Feb 11 13:57:11 2008
New Revision: 620628

URL: http://svn.apache.org/viewvc?rev=620628&view=rev
Log:
Add simple master/detail example without persistence.

Added:
    myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/
    myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/backings/
    myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/backings/ReservationBookView.java   (with props)
    myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/backings/ReservationView.java   (with props)
    myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/model/
    myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/model/Reservation.java   (with props)
    myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/model/ReservationBook.java   (with props)
    myfaces/orchestra/trunk/examples/src/main/webapp/simple/
    myfaces/orchestra/trunk/examples/src/main/webapp/simple/reservation.jsp   (with props)
    myfaces/orchestra/trunk/examples/src/main/webapp/simple/reservationBook.jsp   (with props)

Added: myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/backings/ReservationBookView.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/backings/ReservationBookView.java?rev=620628&view=auto
==============================================================================
--- myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/backings/ReservationBookView.java (added)
+++ myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/backings/ReservationBookView.java Mon Feb 11 13:57:11 2008
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.examples.simple.backings;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.examples.simple.model.ReservationBook;
+
+/**
+ * JSF backing-bean (aka controller class) for reservationBook.jsp.
+ */
+public class ReservationBookView
+{
+	private final Log log = LogFactory.getLog(ReservationBookView.class);
+
+	private ReservationBook  book = new ReservationBook();
+	
+	public ReservationBook getBook()
+	{
+		return book;
+	}
+}

Propchange: myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/backings/ReservationBookView.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/backings/ReservationView.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/backings/ReservationView.java?rev=620628&view=auto
==============================================================================
--- myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/backings/ReservationView.java (added)
+++ myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/backings/ReservationView.java Mon Feb 11 13:57:11 2008
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.examples.simple.backings;
+
+import java.util.Calendar;
+import java.util.Date;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.examples.simple.model.Reservation;
+import org.apache.myfaces.examples.simple.model.ReservationBook;
+
+/**
+ * JSF backing-bean (aka controller class) for reservation.jsp.
+ */
+public class ReservationView
+{
+	private final Log log = LogFactory.getLog(ReservationView.class);
+
+	private ReservationBook book;
+	private Reservation reservation;
+	private boolean viewMode = false;
+	private String reservationState;
+	
+
+	public int getInstanceId() {
+		return System.identityHashCode(this);
+	}
+
+	public void setBook(ReservationBook book) {
+		this.book = book;
+	}
+
+	public Reservation getReservation() {
+		if (reservation == null)
+			reservation = new Reservation();
+		return reservation;
+	}
+
+	public void setReservation(Reservation reservation) {
+		this.reservation = reservation;
+		this.viewMode = true;
+	}
+
+	public boolean getViewMode() {
+		return viewMode;
+	}
+	
+	public String getReservationState() {
+		return reservationState;
+	}
+
+	private boolean isAvailable(Date start, int days) {
+		// the logic here would normally be on some "business service" class rather
+		// than buried here inside the presentation class...
+		
+		// The hotel is closed on sat/sun nights.
+		// Calendar.SUNDAY=1, SATURDAY=6
+		Calendar cal = Calendar.getInstance();
+		cal.setTime(start);
+		int dow = cal.get(Calendar.DAY_OF_WEEK);
+		if (dow == Calendar.SUNDAY)
+			return false;
+		if (dow + days > Calendar.SATURDAY)
+			return false;
+
+		// otherwise, ok
+		return true;
+	}
+
+	public String checkAvailability() {
+		if (isAvailable(reservation.getStart(), reservation.getDays())) {
+			reservationState = "Available";
+		} else {
+			reservationState = "Not available";
+		}
+		return null;
+	}
+	
+	public String save() {
+		if (isAvailable(reservation.getStart(), reservation.getDays())) {
+			book.getReservations().add(reservation);
+			return "save";
+		}
+		reservationState = "Not Available";
+		return null;
+	}
+	
+	public String remove() {
+		book.getReservations().remove(reservation);
+		return "delete";
+	}
+}

Propchange: myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/backings/ReservationView.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/model/Reservation.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/model/Reservation.java?rev=620628&view=auto
==============================================================================
--- myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/model/Reservation.java (added)
+++ myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/model/Reservation.java Mon Feb 11 13:57:11 2008
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.examples.simple.model;
+
+import java.util.Date;
+
+
+public class Reservation
+{
+	private String name;
+	private Date start;
+	private int days;
+
+	public String getName() {
+		return name;
+	}
+	
+	public void setName(String name) {
+		this.name = name;
+	}
+	
+	public Date getStart() {
+		return start;
+	}
+	
+	public void setStart(Date start) {
+		this.start = start;
+	}
+	
+	public int getDays() {
+		return days;
+	}
+	
+	public void setDays(int days) {
+		this.days = days;
+	}
+}

Propchange: myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/model/Reservation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/model/ReservationBook.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/model/ReservationBook.java?rev=620628&view=auto
==============================================================================
--- myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/model/ReservationBook.java (added)
+++ myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/model/ReservationBook.java Mon Feb 11 13:57:11 2008
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.examples.simple.model;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ReservationBook
+{
+	private List reservations = new ArrayList();
+
+	public List getReservations() {
+		return reservations;
+	}
+}

Propchange: myfaces/orchestra/trunk/examples/src/main/java/org/apache/myfaces/examples/simple/model/ReservationBook.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/orchestra/trunk/examples/src/main/webapp/simple/reservation.jsp
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/examples/src/main/webapp/simple/reservation.jsp?rev=620628&view=auto
==============================================================================
--- myfaces/orchestra/trunk/examples/src/main/webapp/simple/reservation.jsp (added)
+++ myfaces/orchestra/trunk/examples/src/main/webapp/simple/reservation.jsp Mon Feb 11 13:57:11 2008
@@ -0,0 +1,79 @@
+<%--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied.  See the License for the
+  specific language governing permissions and limitations
+  under the License.
+  --%>
+
+<%@ page pageEncoding="UTF-8"%>
+<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
+<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
+<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
+<%@ taglib uri="http://myfaces.apache.org/sandbox" prefix="s"%>
+
+<f:view>
+  <%@ include file="/header.jspf"%>
+  <div class="pagedoc">
+    <p>
+    This hotel has an unlimited number of rooms, but is closed on Saturday and
+    Sunday nights.
+    </p>
+    <p>
+    The ReservationView backing bean for this page is in "access" scope, meaning that
+    the same instance is used when this view is rendered multiple times. However when
+    control is passed back to the parent view it is deleted; on return to this view a
+    new instance is then created with all member properties of course reset to their
+    default values.
+    </p>
+    <p>
+    The access-scope works where "request" scope would not; it retains all the properties
+    needed to render this page across repeated renders of this page. However it is cleaner
+    than "session" scope because (a) memory is released when this bean is no longer in use,
+    (b) a new instance is created upon later return to this page, and (c) it works with
+    multiple concurrent windows as a new instance exists for each window. 
+    </p>
+  </div>
+
+  <h:form id="main">
+    <h3>Reservation Information</h3>
+    <h:panelGrid columns="2">
+      <h:outputText value="Backing Bean instance:"/>
+      <h:outputText value="#{reservationView.instanceId}"/>
+    </h:panelGrid>
+
+    <h:panelGrid columns="2">
+      <h:outputText value="name"/>
+      <h:inputText id="name" size="20" value="#{reservationView.reservation.name}" required="true" readonly="#{reservationView.viewMode}"/>
+      <h:outputText value="start"/>
+      <t:inputCalendar id="start" value="#{reservationView.reservation.start}"
+        renderAsPopup="true" popupDateFormat="dd-MMM-yyyy"
+        required="true"
+        readonly="#{reservationView.viewMode}"/>
+      <h:outputText value="days"/>
+      <h:inputText id="days" value="#{reservationView.reservation.days}" required="true" readonly="#{reservationView.viewMode}">
+        <f:validateLongRange minimum="1" maximum="5"/>
+      </h:inputText>
+      <h:outputText value="reservation state:"/>
+      <h:outputText rendered="#{!reservationView.viewMode}" value="#{reservationView.reservationState}"/>
+    </h:panelGrid>
+
+    <h:commandButton id="check" action="#{reservationView.checkAvailability}" value="Check Availability" rendered="#{!reservationView.viewMode}"/>
+    <h:commandButton id="save" action="#{reservationView.save}" value="Save" rendered="#{!reservationView.viewMode}"/>
+    <h:commandButton id="delete" action="#{reservationView.remove}" value="Delete" rendered="#{reservationView.viewMode}"/>
+    <h:commandButton id="cancel" action="cancel" value="Cancel" immediate="true"/>
+  </h:form>
+
+  <%@ include file="/footer.jspf"%>
+</f:view>
\ No newline at end of file

Propchange: myfaces/orchestra/trunk/examples/src/main/webapp/simple/reservation.jsp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/orchestra/trunk/examples/src/main/webapp/simple/reservationBook.jsp
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/examples/src/main/webapp/simple/reservationBook.jsp?rev=620628&view=auto
==============================================================================
--- myfaces/orchestra/trunk/examples/src/main/webapp/simple/reservationBook.jsp (added)
+++ myfaces/orchestra/trunk/examples/src/main/webapp/simple/reservationBook.jsp Mon Feb 11 13:57:11 2008
@@ -0,0 +1,83 @@
+<%--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied.  See the License for the
+  specific language governing permissions and limitations
+  under the License.
+  --%>
+
+<%@ page pageEncoding="UTF-8"%>
+<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
+<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
+<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
+<%@ taglib uri="http://myfaces.apache.org/sandbox" prefix="s"%>
+
+<f:view>
+  <%@ include file="/header.jspf"%>
+  <div class="pagedoc">
+    <p>
+    This trivial webapp presents a simple room-reservation system for a hotel.
+    This hotel has an unlimited number of rooms, but is closed on Saturday and
+    Sunday nights.
+    </p>
+    <p>
+    The presentation logic for this page is in bean <code>reservationBookView</code>
+    which is in a "manual" conversation. As this manual conversation is never
+    explicitly invalidated, this is effectively like "session" scope - except that it
+    integrates correctly with "conversation contexts", ie multiple concurrent windows
+    are supported.
+    </p>
+    <p>
+    The reservation subpage uses bean <code>reservationView</code> which is in an
+    "access" conversation. Before navigating to that view, this page pushes needed
+    parameters data into it using the t:updateActionListener (for JSF1.2, use the
+    standard f:setPropertyActionListener component instead). This creates the bean
+    instance.
+    </p>
+  </div>
+
+  <h:form id="main">
+    <h3>Reservations List</h3>
+    <h:dataTable var="reservation" value="#{reservationBookView.book.reservations}">
+      <h:column>
+        <f:facet name="header">
+          <h:outputText value="Name"/>
+        </f:facet>
+        <h:commandLink id="edit" action="edit" value="#{reservation.name}">
+          <t:updateActionListener value="#{reservationBookView.book}" property="#{reservationView.book}"/>
+          <t:updateActionListener value="#{reservation}" property="#{reservationView.reservation}"/>
+        </h:commandLink>
+      </h:column>
+      <h:column>
+        <f:facet name="header">
+          <h:outputText value="Start"/>
+        </f:facet>
+        <h:outputText value="#{reservation.start}">
+          <f:convertDateTime pattern="dd-MMM-yyyy"/>
+        </h:outputText>
+      </h:column>
+      <h:column>
+        <f:facet name="header">
+          <h:outputText value="Days"/>
+        </f:facet>
+        <h:outputText value="#{reservation.days}"/>
+      </h:column>
+    </h:dataTable>
+    <h:commandButton id="new" action="new" value="New">
+      <t:updateActionListener value="#{reservationBookView.book}" property="#{reservationView.book}"/>
+    </h:commandButton>
+  </h:form>
+
+  <%@ include file="/footer.jspf"%>
+</f:view>
\ No newline at end of file

Propchange: myfaces/orchestra/trunk/examples/src/main/webapp/simple/reservationBook.jsp
------------------------------------------------------------------------------
    svn:eol-style = native