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.manager;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029:
030: import olstore.dto.ItemValue;
031: import olstore.dto.PictureValue;
032: import olstore.entity.ItemLocal;
033: import olstore.entity.ItemLocalHome;
034: import olstore.session.helper.ItemHelper;
035:
036: /**
037: * A wrapper class for item actions so that there is no need to reference the
038: * beans directly, allowing us to swap out the backend beans for a different
039: * implementation.
040: */
041: public class ItemManager {
042:
043: // An item local home to use the finder methods.
044: private ItemLocalHome home;
045:
046: // A helper class for item bean operations.
047: private ItemHelper helper;
048:
049: /**
050: * Constructs a new manager with the appropriate beans supplied via Spring.
051: * @param home the ItemLocalHome that provides finder methods.
052: * @param helper the ItemHelper which provides all other item methods.
053: */
054: public ItemManager(ItemLocalHome home, ItemHelper helper) {
055: this .home = home;
056: this .helper = helper;
057: }
058:
059: /**
060: * Returns an ItemValue from the helper bean that matches the ID.
061: * @param itemId the id of the item to return.
062: * @return an ItemValue for the item requested.
063: * @throws Exception
064: */
065: public ItemValue getItemById(String itemId) throws Exception {
066: return helper.getItemValueForId(itemId);
067: }
068:
069: /**
070: * Returns a PictureValue for the picture that belongs to the item
071: * given by the itemId.
072: * @param itemId the item whose picture we want to return.
073: * @return the picture associated with the item given by itemId.
074: * @throws Exception
075: */
076: public PictureValue getPictureById(String itemId) throws Exception {
077: return helper.getPicture(itemId);
078: }
079:
080: /**
081: * Returns a collection of DTOs that have the same content as the collection of
082: * given locals.
083: * @param locals a collection of locals that need to be converted to DTOs.
084: * @return a Collection of DTOs that have the same content as the collection of locals given.
085: * @throws Exception
086: */
087: private Collection convertToDTOs(Collection locals)
088: throws Exception {
089: Iterator i = locals.iterator();
090: Collection dtos = new ArrayList();
091:
092: while (i.hasNext()) {
093: dtos.add(helper.convertToDTO((ItemLocal) i.next()));
094: }
095:
096: return dtos;
097: }
098:
099: /**
100: * Returns a collection of all items in the database.
101: * @return a collectio of all items in the database.
102: */
103: public Collection getAllItems() {
104: try {
105: return convertToDTOs(home.findAll());
106: } catch (Exception e) {
107: return null;
108: }
109: }
110:
111: /**
112: * Returns a collection of all items whose type matches the supplied type.
113: * @param type the type of items that will be returned.
114: * @return a collection of all items whose type matches the supplied type.
115: */
116: public Collection getItemsByType(String type) {
117: try {
118: return convertToDTOs(home.findByType(type));
119: } catch (Exception e) {
120: return null;
121: }
122: }
123:
124: /**
125: * Return a collection of all items that have been marked for the main page and
126: * whose friends of username have purchased.
127: * @param username the user whose friends are considered into the collection.
128: * @return a collection of all items that have been marked for the main page and
129: * whose friends of username have purchased.
130: */
131: public Collection findByMainPageAndFriendsOf(String username) {
132: try {
133: return home.findByMainPageAndFriendsOf(username);
134: } catch (Exception e) {
135: return null;
136: }
137: }
138:
139: /**
140: * Returns a collection of items that have been marked for the main page and
141: * have been marked as a special offer.
142: * @return a collectino of items that have been marked for the main page and
143: * have been marked as a special offer.
144: */
145: public Collection findByMainPageAndSpecialOffer() {
146: try {
147: return home.findByMainPageAndSpecialOffer();
148: } catch (Exception e) {
149: return null;
150: }
151: }
152:
153: /**
154: * Returns a collection of all items that have been marked
155: * for the main page.
156: * @return a collection of all items that have been marked
157: * for the main page.
158: */
159: public Collection findByMainPage() {
160: try {
161: return home.findByMainPage();
162: } catch (Exception e) {
163: return null;
164: }
165: }
166:
167: /**
168: * Saves the given item into the backend storage.
169: * @param item the item to save.
170: * @throws Exception
171: */
172: public void saveItem(ItemValue item) throws Exception {
173: helper.saveItem(item);
174: }
175:
176: /**
177: * A collection of all properties for the given type.
178: * @param type the type for which properties will be returned.
179: * @return a collection of all properties for the given type.
180: * @throws Exception
181: */
182: public Collection getPropertiesForType(String type)
183: throws Exception {
184: return helper.getPropertiesForType(type);
185: }
186:
187: /**
188: * Record this item as interesting to the user.
189: *
190: * @param itemId the ID of the item the user wants to mark as interesting
191: * @param user the username of the currently logged in user
192: * @throws Exception if there is an error making the change
193: */
194: public void markItemAsInteresting(Integer itemId, String user)
195: throws Exception {
196: helper.markItemAsInteresting(itemId.toString(), user);
197: }
198:
199: }
|