Skip to content

Commit 1929f67

Browse files
committed
Petclinic is RESTful.
1 parent e9649b3 commit 1929f67

27 files changed

+144
-60
lines changed

org.springframework.samples.petclinic/db/hsqldb/petclinic.script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ CREATE MEMORY TABLE OWNERS(ID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WIT
1010
CREATE INDEX OWNERS_LAST_NAME ON OWNERS(LAST_NAME)
1111
CREATE MEMORY TABLE PETS(ID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 0) NOT NULL PRIMARY KEY,NAME VARCHAR(30),BIRTH_DATE DATE,TYPE_ID INTEGER NOT NULL,OWNER_ID INTEGER NOT NULL,CONSTRAINT FK_PETS_OWNERS FOREIGN KEY(OWNER_ID) REFERENCES OWNERS(ID),CONSTRAINT FK_PETS_TYPES FOREIGN KEY(TYPE_ID) REFERENCES TYPES(ID))
1212
CREATE INDEX PETS_NAME ON PETS(NAME)
13-
CREATE MEMORY TABLE VISITS(ID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 0) NOT NULL PRIMARY KEY,PET_ID INTEGER NOT NULL,VISIT_DATE DATE,DESCRIPTION VARCHAR(255),CONSTRAINT FK_VISITS_PETS FOREIGN KEY(PET_ID) REFERENCES PETS(ID))
13+
CREATE MEMORY TABLE VISITS(ID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 0) NOT NULL PRIMARY KEY,PET_ID INTEGER NOT NULL,VISIT_DATE DATE,DESCRIPTION VARCHAR(255),CONSTRAINT FK_VISITS_PETS FOREIGN KEY(PET_ID) REFERENCES PETS(ID) ON DELETE CASCADE)
1414
CREATE INDEX VISITS_PET_ID ON VISITS(PET_ID)
1515
ALTER TABLE VETS ALTER COLUMN ID RESTART WITH 7
1616
ALTER TABLE SPECIALTIES ALTER COLUMN ID RESTART WITH 4

org.springframework.samples.petclinic/ivy.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<dependency org="javax.servlet" name="com.springsource.javax.servlet.jsp.jstl" rev="1.1.2" conf="compile->runtime"/>
2626
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.logging" rev="1.1.1" conf="compile->compile"/>
2727
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.dbcp" rev="1.2.2.osgi" conf="compile->runtime"/>
28+
<dependency org="org.apache.log4j" name="com.springsource.org.apache.log4j" rev="1.2.15" conf="compile->runtime"/>
2829
<dependency org="org.apache.taglibs" name="com.springsource.org.apache.taglibs.standard" rev="1.1.2" conf="compile->runtime"/>
2930
<dependency org="org.aspectj" name="com.springsource.org.aspectj.weaver" rev="1.5.4" conf="compile->compile"/>
3031
<dependency org="org.hibernate" name="com.springsource.org.hibernate" rev="3.2.6.ga" conf="compile->compile"/>

org.springframework.samples.petclinic/petclinic.iml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
<attribute name="method" value="1" />
3737
<attribute name="URI" value="/WEB-INF/classes" />
3838
</containerElement>
39+
<containerElement type="library" level="module">
40+
<attribute name="method" value="1" />
41+
<attribute name="URI" value="/WEB-INF/lib" />
42+
<url>jar://$IVY_CACHE$/org.apache.taglibs/com.springsource.org.apache.taglibs.standard/1.1.2/com.springsource.org.apache.taglibs.standard-1.1.2.jar!/</url>
43+
</containerElement>
3944
</packaging>
4045
</configuration>
4146
</facet>
@@ -47,6 +52,7 @@
4752
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
4853
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
4954
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" isTestSource="true" />
55+
<excludeFolder url="file://$MODULE_DIR$/target" />
5056
</content>
5157
<orderEntry type="inheritedJdk" />
5258
<orderEntry type="sourceFolder" forTests="false" />
@@ -130,6 +136,15 @@
130136
<SOURCES />
131137
</library>
132138
</orderEntry>
139+
<orderEntry type="module-library">
140+
<library>
141+
<CLASSES>
142+
<root url="jar://$IVY_CACHE$/org.apache.taglibs/com.springsource.org.apache.taglibs.standard/1.1.2/com.springsource.org.apache.taglibs.standard-1.1.2.jar!/" />
143+
</CLASSES>
144+
<JAVADOC />
145+
<SOURCES />
146+
</library>
147+
</orderEntry>
133148
</component>
134149
</module>
135150

org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/Clinic.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,9 @@ public interface Clinic {
7474
*/
7575
void storeVisit(Visit visit) throws DataAccessException;
7676

77+
/**
78+
* Deletes a <code>Pet</code> from the data store.
79+
*/
80+
void deletePet(int id) throws DataAccessException;
81+
7782
}

org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/hibernate/HibernateClinic.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.springframework.samples.petclinic.Visit;
1414
import org.springframework.stereotype.Repository;
1515
import org.springframework.transaction.annotation.Transactional;
16+
import org.springframework.dao.DataAccessException;
1617

1718
/**
1819
* Hibernate implementation of the Clinic interface.
@@ -86,4 +87,9 @@ public void storeVisit(Visit visit) {
8687
sessionFactory.getCurrentSession().merge(visit);
8788
}
8889

90+
public void deletePet(int id) throws DataAccessException {
91+
Pet pet = loadPet(id);
92+
sessionFactory.getCurrentSession().delete(pet);
93+
}
94+
8995
}

org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/jdbc/SimpleJdbcClinic.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ public void storeVisit(Visit visit) throws DataAccessException {
240240
}
241241
}
242242

243+
public void deletePet(int id) throws DataAccessException {
244+
this.simpleJdbcTemplate.update("DELETE FROM pets WHERE id=?", id);
245+
}
246+
243247
// END of Clinic implementation section ************************************
244248

245249

org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/jpa/EntityManagerClinic.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.springframework.samples.petclinic.Visit;
1515
import org.springframework.stereotype.Repository;
1616
import org.springframework.transaction.annotation.Transactional;
17+
import org.springframework.dao.DataAccessException;
1718

1819
/**
1920
* JPA implementation of the Clinic interface using EntityManager.
@@ -87,4 +88,9 @@ public void storeVisit(Visit visit) {
8788
visit.setId(merged.getId());
8889
}
8990

91+
public void deletePet(int id) throws DataAccessException {
92+
Pet pet = loadPet(id);
93+
this.em.remove(pet);
94+
}
95+
9096
}

org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/AddOwnerForm.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
*
2222
* @author Juergen Hoeller
2323
* @author Ken Krebs
24+
* @author Arjen Poutsma
2425
*/
2526
@Controller
26-
@RequestMapping("/addOwner.do")
27+
@RequestMapping("/owners/new")
2728
@SessionAttributes(types = Owner.class)
2829
public class AddOwnerForm {
2930

@@ -55,7 +56,7 @@ public String processSubmit(@ModelAttribute Owner owner, BindingResult result, S
5556
else {
5657
this.clinic.storeOwner(owner);
5758
status.setComplete();
58-
return "redirect:owner.do?ownerId=" + owner.getId();
59+
return "redirect:/clinic/owners/" + owner.getId();
5960
}
6061
}
6162

org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/AddPetForm.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.springframework.web.bind.annotation.RequestParam;
1818
import org.springframework.web.bind.annotation.SessionAttributes;
1919
import org.springframework.web.bind.annotation.InitBinder;
20+
import org.springframework.web.bind.annotation.PathVariable;
2021
import org.springframework.web.bind.support.SessionStatus;
2122
import org.springframework.web.bind.WebDataBinder;
2223

@@ -26,9 +27,10 @@
2627
*
2728
* @author Juergen Hoeller
2829
* @author Ken Krebs
30+
* @author Arjen Poutsma
2931
*/
3032
@Controller
31-
@RequestMapping("/addPet.do")
33+
@RequestMapping("/owners/{ownerId}/pets/new")
3234
@SessionAttributes("pet")
3335
public class AddPetForm {
3436

@@ -50,7 +52,7 @@ public void setAllowedFields(WebDataBinder dataBinder) {
5052
}
5153

5254
@RequestMapping(method = RequestMethod.GET)
53-
public String setupForm(@RequestParam("ownerId") int ownerId, Model model) {
55+
public String setupForm(@PathVariable("ownerId") int ownerId, Model model) {
5456
Owner owner = this.clinic.loadOwner(ownerId);
5557
Pet pet = new Pet();
5658
owner.addPet(pet);
@@ -67,7 +69,7 @@ public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result
6769
else {
6870
this.clinic.storePet(pet);
6971
status.setComplete();
70-
return "redirect:owner.do?ownerId=" + pet.getOwner().getId();
72+
return "redirect:/clinic/owners/" + pet.getOwner().getId();
7173
}
7274
}
7375

org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/AddVisitForm.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.springframework.web.bind.annotation.RequestParam;
1515
import org.springframework.web.bind.annotation.SessionAttributes;
1616
import org.springframework.web.bind.annotation.InitBinder;
17+
import org.springframework.web.bind.annotation.PathVariable;
1718
import org.springframework.web.bind.support.SessionStatus;
1819
import org.springframework.web.bind.WebDataBinder;
1920

@@ -23,9 +24,10 @@
2324
*
2425
* @author Juergen Hoeller
2526
* @author Ken Krebs
27+
* @author Arjen Poutsma
2628
*/
2729
@Controller
28-
@RequestMapping("/addVisit.do")
30+
@RequestMapping("/owners/*/pets/{petId}/visits/new")
2931
@SessionAttributes("visit")
3032
public class AddVisitForm {
3133

@@ -42,7 +44,7 @@ public void setAllowedFields(WebDataBinder dataBinder) {
4244
}
4345

4446
@RequestMapping(method = RequestMethod.GET)
45-
public String setupForm(@RequestParam("petId") int petId, Model model) {
47+
public String setupForm(@PathVariable("petId") int petId, Model model) {
4648
Pet pet = this.clinic.loadPet(petId);
4749
Visit visit = new Visit();
4850
pet.addVisit(visit);
@@ -59,7 +61,7 @@ public String processSubmit(@ModelAttribute("visit") Visit visit, BindingResult
5961
else {
6062
this.clinic.storeVisit(visit);
6163
status.setComplete();
62-
return "redirect:owner.do?ownerId=" + visit.getPet().getOwner().getId();
64+
return "redirect:/clinic/owners/" + visit.getPet().getOwner().getId();
6365
}
6466
}
6567

0 commit comments

Comments
 (0)