separated service-layer code from flow code for "Create booking" and "cancel"

This commit is contained in:
Keith Donald
2007-08-21 04:58:45 +00:00
parent 39677b50e5
commit df0675b20c
7 changed files with 30 additions and 42 deletions

View File

@@ -10,7 +10,4 @@ public interface BookingService {
public Hotel findHotelById(Long id);
public Booking bookHotel(Hotel hotel, User user);
public void cancelBooking(Long id);
}

View File

@@ -41,19 +41,4 @@ public class JpaBookingService implements BookingService {
public Hotel findHotelById(Long id) {
return em.find(Hotel.class, id);
}
@Transactional(readOnly = true)
public Booking bookHotel(Hotel hotel, User user) {
Booking booking = new Booking(hotel, user);
em.persist(booking);
return booking;
}
@Transactional
public void cancelBooking(Long id) {
Booking booking = em.find(Booking.class, id);
if (booking != null) {
em.remove(booking);
}
}
}
}

View File

@@ -4,16 +4,28 @@ import java.util.Calendar;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import org.springframework.webflow.action.AbstractAction;
import org.springframework.webflow.action.MultiAction;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
import org.springframework.webflow.samples.booking.app.Booking;
import org.springframework.webflow.samples.booking.app.Hotel;
import org.springframework.webflow.samples.booking.app.User;
public class ValidateBookingAction extends AbstractAction {
public class BookingActions extends MultiAction {
@Override
protected Event doExecute(RequestContext context) throws Exception {
public Event createBooking(RequestContext context) {
Hotel hotel = (Hotel) context.getFlowScope().get("hotel");
User user = (User) context.getFlowScope().get("user");
Booking booking = new Booking(hotel, user);
EntityManager em = (EntityManager) context.getFlowScope().get("entityManager");
em.persist(booking);
context.getFlowScope().put("booking", booking);
return success();
}
public Event validateBooking(RequestContext context) throws Exception {
Booking booking = (Booking) context.getFlowScope().get("booking");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -1);

View File

@@ -27,7 +27,7 @@ public class MainActions extends MultiAction {
public Event findCurrentUserBookings(RequestContext context) {
User user = (User) context.getConversationScope().get("user");
List<Booking> bookings = bookingService.findBookings(user.getName());
List<Booking> bookings = bookingService.findBookings(user.getUsername());
context.getFlowScope().put("bookings", new SerializableListDataModel(bookings));
return success();
}
@@ -40,4 +40,8 @@ public class MainActions extends MultiAction {
return success();
}
public Event removeBooking(RequestContext context) {
return success();
}
}

View File

@@ -6,6 +6,6 @@
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="validateBookingAction" class="org.springframework.webflow.samples.booking.flow.booking.ValidateBookingAction"/>
<bean id="bookingActions" class="org.springframework.webflow.samples.booking.flow.booking.BookingActions"/>
</beans>

View File

@@ -21,19 +21,13 @@
<method-result name="hotel" scope="flow" />
</bean-action>
</render-actions>
<transition on="book" to="bookHotel" />
<transition on="book" to="createBooking" />
<transition on="cancel" to="cancel" />
</view-state>
<action-state id="bookHotel">
<bean-action method="bookHotel" bean="bookingService">
<method-arguments>
<argument expression="hotel" />
<argument expression="user" />
</method-arguments>
<method-result name="booking" scope="flow" />
</bean-action>
<transition to="enterBookingDetails" />
<action-state id="createBooking">
<action method="createBooking" bean="bookingActions"/>
<transition on="success" to="enterBookingDetails" />
</action-state>
<view-state id="enterBookingDetails" view="/flow/booking/bookingForm.xhtml">
@@ -42,7 +36,7 @@
</view-state>
<action-state id="validateBooking">
<action bean="validateBookingAction" />
<action method="validateBooking" bean="bookingActions" />
<transition on="success" to="confirmBooking" />
<transition on="error" to="enterBookingDetails" />
</action-state>

View File

@@ -34,12 +34,8 @@
<transition on="cancel" to="displayMain" />
</subflow-state>
<action-state id="cancelBooking">
<bean-action bean="bookingService" method="cancelBooking">
<method-arguments>
<argument expression="param.bookingId" parameter-type="long" />
</method-arguments>
</bean-action>
<action-state id="removeBooking">
<action method="removeBooking" bean="mainActions" />
<transition on="success" to="reloadCurrentUserBookings" />
</action-state>