001: /**
002: * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: * Initial Developers: Greg Lapouchnian
022: * Patrick Smith
023: *
024: */package olstore.domain.logic;
025:
026: import java.util.Collection;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.Map;
030: import java.util.Vector;
031:
032: import olstore.domain.manager.CartManager;
033: import olstore.domain.manager.ItemManager;
034: import olstore.domain.manager.OrderManager;
035: import olstore.domain.manager.TypeManager;
036: import olstore.domain.manager.UserManager;
037: import olstore.dto.AddressValue;
038: import olstore.dto.ItemValue;
039: import olstore.dto.OrderEntry;
040: import olstore.dto.PictureValue;
041: import olstore.dto.TypeValue;
042: import olstore.dto.UserValue;
043: import olstore.entity.OrderLocal;
044: import olstore.session.ShoppingCartLocalHome;
045:
046: /**
047: * The implementation of the OlstoreFacade class, used for interacting
048: * with the backend beans without directly referencing objects that can be replaced.
049: * Actions are done through Manager classes which wrap the classes that can be removed and
050: * replaced with new implementations.
051: */
052: public class OlstoreImpl implements OlstoreFacade {
053:
054: // A wrapper that handles item related actions.
055: private ItemManager item;
056:
057: // A wrapper that handles order related actions.
058: private OrderManager order;
059:
060: // A wrapper that handles user related actions.
061: private UserManager user;
062:
063: // A wrapper that handles cart related actions.
064: private ShoppingCartLocalHome cart;
065:
066: // A wrapper that handles type related actions.
067: private TypeManager type;
068:
069: // The default type for a new item.
070: private String DEFAULT_TYPE;
071:
072: //-------------------------------------------------------------------------
073: // Setter methods for dependency injection
074: //-------------------------------------------------------------------------
075:
076: public void setItemManager(ItemManager item) {
077: this .item = item;
078: }
079:
080: public void setOrderManager(OrderManager order) {
081: this .order = order;
082: }
083:
084: public void setUserManager(UserManager user) {
085: this .user = user;
086: }
087:
088: public void setShoppingCart(ShoppingCartLocalHome cart) {
089: this .cart = cart;
090: }
091:
092: public void setTypeManager(TypeManager type) {
093: this .type = type;
094: }
095:
096: public void setDefaultType(String type) {
097: DEFAULT_TYPE = type;
098: }
099:
100: //-------------------------------------------------------------------------
101: // Operation methods, implementing the OlstoreFacade interface
102: //-------------------------------------------------------------------------
103:
104: /**
105: * Returns an ItemValue that matches the given ID.
106: * @param itemId the ID of the item to be returned.
107: * @return an ItemValue for the item represted by the item ID.
108: */
109: public ItemValue getItemById(String itemId) throws Exception {
110: return item.getItemById(itemId);
111: }
112:
113: /**
114: * Returns a PictureValue that matches the given item's ID.
115: * @param itemId the ID for the item whose picture is to be returned.
116: * @return a picture that belongs to the item given by itemId.
117: */
118: public PictureValue getPictureById(String itemId) throws Exception {
119: return item.getPictureById(itemId);
120: }
121:
122: /**
123: * Returns all items in the database.
124: * @return all items in the database as a Collection.
125: */
126: public Collection getAll() {
127: return item.getAllItems();
128: }
129:
130: /**
131: * Returns all items of the given type.
132: * @param type the type of item being requested.
133: * @return a collection of all items of the requested type.
134: */
135: public Collection getItemsByType(String type) {
136: return item.getItemsByType(type);
137: }
138:
139: /**
140: * Returns a list of all types that are available.
141: * @return a collection of all types that are available.
142: */
143: public Collection getTypes() {
144: try {
145: return type.findAll();
146: } catch (Exception e) {
147: return null;
148: }
149: }
150:
151: /**
152: * Returns a cart manager for the given user.
153: * @param username the username for the desired user's cart.
154: * @return a cart manager object that can be used to interact with the user's shopping cart.
155: */
156: public CartManager getShoppingCart(String username) {
157: return new CartManager(cart, user, username);
158: }
159:
160: /**
161: * Returns a collection of all orders placed by the user.
162: * @param username the user whose orders we are returning.
163: * @return a Collection of all orders by username.
164: */
165: public Collection getOrders(String username) {
166: return user.getOrders(username);
167: }
168:
169: /**
170: * Returns the user value for username.
171: * @param username the users name to return.
172: * @return a UserValue representation of the user.
173: */
174: public UserValue getUser(String username) {
175: return user.findUserValue(username);
176: }
177:
178: /**
179: * Returns all users in the database.
180: * @return a Collection of all users in the database.
181: */
182: public Collection getAllUsers() {
183: return user.findAll();
184: }
185:
186: /**
187: * Returns all items that have been purchased by the user.
188: * @param username the name of the user.
189: * @return a Collection of all items that have been purchased by the user.
190: */
191: public Collection getUserPurchased(String username) {
192: return user.getUserPurchased(username);
193: }
194:
195: /**
196: * Finds all items that have been marked for the main page, and
197: * have been purchased by friends of the user.
198: * @param username the user's name.
199: * @return a collection of all items that have been marked for the main page,
200: * and have been purchased by friends of the user.
201: */
202: public Collection findByMainPageAndFriendsOf(String username) {
203: return item.findByMainPageAndFriendsOf(username);
204: }
205:
206: /**
207: * Finds all items that have been marked for the main page, and are on special.
208: * @return a collection of all items that have been marked for the main page and are
209: * on special.
210: */
211: public Collection findByMainPageAndSpecialOffer() {
212: return item.findByMainPageAndSpecialOffer();
213: }
214:
215: /**
216: * Returns all items that have been marked for the main page.
217: * @return a collection of all items that have been marked for the main page.
218: */
219: public Collection findByMainPage() {
220: return item.findByMainPage();
221: }
222:
223: /**
224: * Saves the item given into the backend storage.
225: * @param item the item to save.
226: */
227: public void saveItem(ItemValue item) throws Exception {
228: this .item.saveItem(item);
229: }
230:
231: /**
232: * Saves the type given into the backend storage.
233: * @param type the type to save.
234: */
235: public void saveType(TypeValue type) throws Exception {
236: this .type.saveType(type);
237: }
238:
239: /**
240: * Saves the user given into backend storage.
241: * @param user the user to save.
242: * @param address the address of the user.
243: * @param username the user's name.
244: */
245: public void saveUser(UserValue user, AddressValue address,
246: String username) throws Exception {
247: this .user.saveUser(user, address, username);
248: }
249:
250: /**
251: * Creates a new user with the given values and address.
252: * @param user the user's information.
253: * @param address the user's address information.
254: */
255: public void createUser(UserValue user, AddressValue address)
256: throws Exception {
257: this .user.createUser(user, address);
258: }
259:
260: /**
261: * Returns the default type.
262: * @return the default type.
263: */
264: public String getDefaultType() {
265: return DEFAULT_TYPE;
266: }
267:
268: /**
269: * Returns a collection of properties for the given type.
270: * @param type the type whose properties will be returned.
271: * @return a collection of properties for the given type.
272: */
273: public Collection getPropertiesForType(String type)
274: throws Exception {
275: return this .item.getPropertiesForType(type);
276: }
277:
278: /**
279: * Returns if the database has been populated.
280: * @return true if the database isn't empty of users, false otherwise.
281: */
282: public boolean isPopulated() {
283: return !this .user.findAll().isEmpty();
284: }
285:
286: /**
287: * Returns a collection of all orders whose status can be updated.
288: * @return collection of all orders whose status can be updated.
289: */
290: public Collection findOrdersForUpdate() {
291: Collection orders = order.findOrdersForUpdate();
292: Vector newOrders = new Vector();
293: Iterator ordersIt = orders.iterator();
294: while (ordersIt.hasNext()) {
295: OrderEntry order = new OrderEntry((OrderLocal) ordersIt
296: .next());
297: newOrders.add(order);
298: }
299: return newOrders;
300: }
301:
302: /**
303: * Sets the status of the order given by ID with the given status.
304: * @param id the ID of the order.
305: * @param status the new status for the order.
306: */
307: public void setStatus(Integer id, String status) {
308: order.setStatus(id, status);
309: }
310:
311: /**
312: * Record this item as interesting to the user.
313: *
314: * @param itemId the ID of the item the user wants to mark as interesting
315: * @param user the username of the currently logged in user
316: * @throws Exception if there is an error making the change
317: */
318: public void markItemAsInteresting(Integer itemId, String user)
319: throws Exception {
320: item.markItemAsInteresting(itemId, user);
321: }
322:
323: /**
324: * Get a map of all the friends of the current user for each one of the items
325: * in the collection.
326: *
327: * @param username the currently logged in user
328: * @param items the items that will be included in the friends map
329: */
330: public Map getFriendsMap(String username, Collection items) {
331: Iterator i = items.iterator();
332: Map m = new HashMap();
333: while (i.hasNext()) {
334: Integer itemId = ((ItemValue) i.next()).getItemId();
335: Collection friends = user.findFriendsThatPurchasedItem(
336: username, itemId);
337: m.put(itemId, friends);
338: }
339: return m;
340: }
341: }
|