From 090136418a5e2d939ee4fe09e9be74757f497bb8 Mon Sep 17 00:00:00 2001
From: Keith Donald <kdonald@vmware.com>
Date: Wed, 6 May 2009 21:43:08 +0000
Subject: [PATCH] owner pages

---
 .../samples/petclinic/owners/Owner.java       | 49 ++++++++++++++++++-
 .../petclinic/owners/OwnerController.java     | 15 +++---
 .../petclinic/owners/OwnerRepository.java     |  2 +-
 .../petclinic/owners/OwnerSearchForm.java     | 15 ++++++
 .../petclinic/owners/OwnersController.java    | 12 ++---
 .../petclinic/owners/StubOwnerRepository.java |  5 +-
 .../petclinic/owners/pets/PetController.java  |  6 +--
 .../petclinic/util/ExternalContext.java       | 22 +++++++++
 .../petclinic/util/ResponseContext.java       |  9 ----
 .../src/main/webapp/WEB-INF/layouts/page.jsp  |  6 +--
 .../main/webapp/WEB-INF/owners/addNewForm.jsp | 15 ++++++
 .../main/webapp/WEB-INF/owners/content.jsp    | 11 +++++
 .../WEB-INF/owners/{search.jsp => owner.jsp}  |  2 +-
 .../main/webapp/WEB-INF/owners/searchForm.jsp | 12 +++++
 .../webapp/WEB-INF/owners/searchResults.jsp   |  2 +
 .../src/main/webapp/WEB-INF/tiles.xml         | 42 ++++++++++++++--
 16 files changed, 189 insertions(+), 36 deletions(-)
 create mode 100644 org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnerSearchForm.java
 create mode 100644 org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/util/ExternalContext.java
 delete mode 100644 org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/util/ResponseContext.java
 create mode 100644 org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/addNewForm.jsp
 create mode 100644 org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/content.jsp
 rename org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/{search.jsp => owner.jsp} (74%)
 create mode 100644 org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/searchForm.jsp
 create mode 100644 org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/searchResults.jsp

diff --git a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/Owner.java b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/Owner.java
index ec19625..934accd 100644
--- a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/Owner.java
+++ b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/Owner.java
@@ -14,12 +14,57 @@ public class Owner {
 
 	private String telephone;
 	
+	public Owner() {
+		
+	}
+	
+	public Owner(Long id) {
+		this.id = id;
+	}
+	
 	public Long getId() {
 		return id;
 	}
-	
-	public String getName() {
+
+	public String getFirstName() {
+		return firstName;
+	}
+
+	public void setFirstName(String firstName) {
+		this.firstName = firstName;
+	}
+
+	public String getLastName() {
 		return lastName;
 	}
 
+	public void setLastName(String lastName) {
+		this.lastName = lastName;
+	}
+
+	public String getAddress() {
+		return address;
+	}
+
+	public void setAddress(String address) {
+		this.address = address;
+	}
+
+	public String getCity() {
+		return city;
+	}
+
+	public void setCity(String city) {
+		this.city = city;
+	}
+
+	public String getTelephone() {
+		return telephone;
+	}
+
+	public void setTelephone(String telephone) {
+		this.telephone = telephone;
+	}
+
+
 }
diff --git a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnerController.java b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnerController.java
index 11be755..d357c04 100644
--- a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnerController.java
+++ b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnerController.java
@@ -1,8 +1,10 @@
 package org.springframework.samples.petclinic.owners;
 
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.samples.petclinic.util.ResponseContext;
+import org.springframework.samples.petclinic.util.ExternalContext;
 import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 
@@ -18,19 +20,20 @@ public class OwnerController {
 	}
 	
 	@RequestMapping(method=RequestMethod.GET)
-	public Owner get(Long owner) {
-		return repository.getOwner(owner);
+	public String get(@PathVariable Long owner, Model model) {
+		model.addAttribute(repository.getOwner(owner));
+		return "owner";
 	}
 	
 	@RequestMapping(value="/edit", method=RequestMethod.GET)
-	public Owner getEditForm(Long owner) {
+	public Owner getEditForm(@PathVariable Long owner) {
 		return repository.getOwner(owner);
 	}
 	
 	@RequestMapping(method = RequestMethod.PUT)
-	public void put(Owner owner, ResponseContext response) {
+	public void put(Owner owner, ExternalContext response) {
 		repository.saveOwner(owner);
-		response.redirect(owner.getName());
+		response.redirect(owner.getId());
 	}	
 	
 }
\ No newline at end of file
diff --git a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnerRepository.java b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnerRepository.java
index 762ba5d..1070224 100644
--- a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnerRepository.java
+++ b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnerRepository.java
@@ -8,6 +8,6 @@ public interface OwnerRepository {
 
 	Owner getOwner(Long id);
 
-	void saveOwner(Owner owner);
+	Long saveOwner(Owner owner);
 
 }
\ No newline at end of file
diff --git a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnerSearchForm.java b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnerSearchForm.java
new file mode 100644
index 0000000..c9c5896
--- /dev/null
+++ b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnerSearchForm.java
@@ -0,0 +1,15 @@
+package org.springframework.samples.petclinic.owners;
+
+public class OwnerSearchForm {
+	
+	private String lastName;
+
+	public String getLastName() {
+		return lastName;
+	}
+
+	public void setLastName(String lastName) {
+		this.lastName = lastName;
+	}
+	
+}
diff --git a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnersController.java b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnersController.java
index 93fd5b9..f41e895 100644
--- a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnersController.java
+++ b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/OwnersController.java
@@ -3,7 +3,6 @@ package org.springframework.samples.petclinic.owners;
 import java.util.Collection;
 
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.samples.petclinic.util.ResponseContext;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -21,8 +20,8 @@ public class OwnersController {
 	}
 	
 	@RequestMapping(method = RequestMethod.GET)
-	public void get() {
-		
+	public OwnerSearchForm get() {
+		return new OwnerSearchForm();
 	}
 	
 	@RequestMapping(value="/search", method = RequestMethod.GET)
@@ -36,9 +35,10 @@ public class OwnersController {
 	}
 	
 	@RequestMapping(method = RequestMethod.POST)
-	public void post(Owner owner, ResponseContext response) {
-		repository.saveOwner(owner);
-		response.redirect(owner.getName());
+	public String post(Owner owner) {
+		Long ownerId = repository.saveOwner(owner);
+		// TODO simplify this since /owners is the current resource already?
+		return "redirect:/owners/" + ownerId;
 	}	
 
 }
diff --git a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/StubOwnerRepository.java b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/StubOwnerRepository.java
index 5c3406b..d442ee9 100644
--- a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/StubOwnerRepository.java
+++ b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/StubOwnerRepository.java
@@ -12,10 +12,11 @@ public class StubOwnerRepository implements OwnerRepository {
 	}
 
 	public Owner getOwner(Long id) {
-		return null;
+		return new Owner(id);
 	}
 
-	public void saveOwner(Owner owner) {
+	public Long saveOwner(Owner owner) {
+		return 1L;
 	}
 
 }
diff --git a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/pets/PetController.java b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/pets/PetController.java
index d789f91..a0a7e74 100644
--- a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/pets/PetController.java
+++ b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/owners/pets/PetController.java
@@ -1,7 +1,7 @@
 package org.springframework.samples.petclinic.owners.pets;
 
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.samples.petclinic.util.ResponseContext;
+import org.springframework.samples.petclinic.util.ExternalContext;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -28,13 +28,13 @@ public class PetController {
 	}
 	
 	@RequestMapping(method = RequestMethod.PUT)
-	public void put(Pet pet, ResponseContext response) {
+	public void put(Pet pet, ExternalContext response) {
 		repository.savePet(pet);
 		response.redirect(pet.getName());
 	}
 	
 	@RequestMapping(method = RequestMethod.DELETE)
-	public void delete(Long owner, String pet, ResponseContext context) {
+	public void delete(Long owner, String pet, ExternalContext context) {
 		context.forResource("owners").redirect(owner);
 	}
 	
diff --git a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/util/ExternalContext.java b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/util/ExternalContext.java
new file mode 100644
index 0000000..5f4fc1e
--- /dev/null
+++ b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/util/ExternalContext.java
@@ -0,0 +1,22 @@
+package org.springframework.samples.petclinic.util;
+
+import org.springframework.ui.Model;
+
+// This is an idea as a context object to make avail to @Controller methods to interact with Dispatcher
+public interface ExternalContext {
+
+	Model getModel();
+	
+	void selectView(String viewName);
+	
+	void renderFragment(String fragment);
+	
+	void redirect(Object resource);
+
+	ExternalContext forResource(Object resource);
+
+	Object getNativeRequest();
+
+	Object getNativeResponse();
+
+}
diff --git a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/util/ResponseContext.java b/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/util/ResponseContext.java
deleted file mode 100644
index fd9d5f6..0000000
--- a/org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/util/ResponseContext.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package org.springframework.samples.petclinic.util;
-
-public interface ResponseContext {
-
-	void redirect(Object resource);
-
-	ResponseContext forResource(Object resource);
-
-}
diff --git a/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/layouts/page.jsp b/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/layouts/page.jsp
index df5ed7b..b92d0ff 100644
--- a/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/layouts/page.jsp
+++ b/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/layouts/page.jsp
@@ -22,9 +22,9 @@
 			</ul>
 			<div id="nav">
 				<ul>
-					<li><a href="<c:url value="/"/>">Home</a></li>
-					<li><a href="appointments">Appointments</a></li>
-					<li><a href="owners">Owners</a></li>
+					<li><a href="${pageContext.request.contextPath}">Home</a></li>
+					<li><a href="${pageContext.request.contextPath}/appointments">Appointments</a></li>
+					<li><a href="${pageContext.request.contextPath}/owners">Owners</a></li>
 				</ul>
 			</div>
 		</div>
diff --git a/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/addNewForm.jsp b/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/addNewForm.jsp
new file mode 100644
index 0000000..68ad9dd
--- /dev/null
+++ b/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/addNewForm.jsp
@@ -0,0 +1,15 @@
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
+<h2>Add New Owner</h2>
+
+<form:form id="addNewForm" action="${pageContext.request.contextPath}/owners" modelAttribute="owner" method="post">
+	<form:label for="firstName" path="firstName">
+		First Name
+		<form:input path="firstName" />
+	</form:label>
+	<form:label for="lastName" path="lastName">
+		Last Name
+		<form:input path="lastName" />
+	</form:label>
+	<input type="submit" value="Add" />	
+</form:form>
\ No newline at end of file
diff --git a/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/content.jsp b/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/content.jsp
new file mode 100644
index 0000000..c0dae0e
--- /dev/null
+++ b/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/content.jsp
@@ -0,0 +1,11 @@
+<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
+
+<div id="sidebar">
+	<ul id="sub-nav">
+		<li><a href="${pageContext.request.contextPath}/owners">Search</a></li>
+		<li><a href="${pageContext.request.contextPath}/owners/new">Add New</a></li>
+	</ul>
+</div>
+<div id="main">
+	<tiles:insertAttribute name="main" />
+</div>
\ No newline at end of file
diff --git a/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/search.jsp b/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/owner.jsp
similarity index 74%
rename from org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/search.jsp
rename to org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/owner.jsp
index f098b9d..85dcec1 100644
--- a/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/search.jsp
+++ b/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/owner.jsp
@@ -1,2 +1,2 @@
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
-<h2>Search Owners</h2>
\ No newline at end of file
+<h2>Owner Details</h2>
\ No newline at end of file
diff --git a/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/searchForm.jsp b/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/searchForm.jsp
new file mode 100644
index 0000000..1a5830d
--- /dev/null
+++ b/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/searchForm.jsp
@@ -0,0 +1,12 @@
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
+
+<h2>Search Owners</h2>
+
+<form:form id="searchForm" action="owners/search" modelAttribute="ownerSearchForm" method="get">
+	<form:label for="lastName" path="lastName">
+		Last Name
+		<form:input path="lastName" />
+	</form:label>
+	<input type="submit" value="Search" />	
+</form:form>
\ No newline at end of file
diff --git a/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/searchResults.jsp b/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/searchResults.jsp
new file mode 100644
index 0000000..c01fd8a
--- /dev/null
+++ b/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/owners/searchResults.jsp
@@ -0,0 +1,2 @@
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<h2>Search Results</h2>
\ No newline at end of file
diff --git a/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/tiles.xml b/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/tiles.xml
index 64d5e66..87229ac 100644
--- a/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/tiles.xml
+++ b/org.springframework.samples.petclinic/src/main/webapp/WEB-INF/tiles.xml
@@ -18,14 +18,50 @@
 
 	<!-- APPOINTMENTS PAGES -->
 	<definition name="appointments" extends="page">
-		<put-attribute name="title" value="Appointment" type="string" />
+		<put-attribute name="title" value="Appointments" type="string" />
 		<put-attribute name="content" value="/WEB-INF/appointments/calendar.jsp" type="template" />
 	</definition>
 	
 	<!-- OWNERS PAGES -->
+	
+	<!-- SEARCH PAGE -->
 	<definition name="owners" extends="page">
+		<put-attribute name="title" value="Owners" type="string" />
+		<put-attribute name="content" value="owners.content" type="definition" />
+	</definition>
+
+	<definition name="owners.content" template="/WEB-INF/owners/content.jsp">
+		<put-attribute name="main" value="/WEB-INF/owners/searchForm.jsp" type="template" />
+	</definition>
+
+	<!-- ADD NEW PAGE -->
+	<definition name="owners/new" extends="page">
+		<put-attribute name="title" value="Owners" type="string" />
+		<put-attribute name="content" value="owners/new.content" type="definition" />
+	</definition>
+
+	<definition name="owners/new.content" template="/WEB-INF/owners/content.jsp">
+		<put-attribute name="main" value="/WEB-INF/owners/addNewForm.jsp" type="template" />
+	</definition>
+
+	<!-- SEARCH RESULTS PAGE -->
+	<definition name="owners/search" extends="page">
 		<put-attribute name="title" value="Owner Search" type="string" />
-		<put-attribute name="content" value="/WEB-INF/owners/search.jsp" type="template" />
-	</definition>	
+		<put-attribute name="content" value="owners/search.content" type="definition" />
+	</definition>
+
+	<definition name="owners/search.content" template="/WEB-INF/owners/content.jsp">
+		<put-attribute name="main" value="/WEB-INF/owners/searchResults.jsp" type="template" />
+	</definition>
+	
+	<!-- OWNER DETAIL PAGE -->
+	<definition name="owner" extends="page">
+		<put-attribute name="title" value="Owner" type="string" />
+		<put-attribute name="content" value="owner.content" type="definition" />
+	</definition>
 
+	<definition name="owner.content" template="/WEB-INF/owners/content.jsp">
+		<put-attribute name="main" value="/WEB-INF/owners/owner.jsp" type="template" />
+	</definition>
+	
 </tiles-definitions>
\ No newline at end of file
-- 
GitLab