001: /*
002: * Enhydra Java Application Server
003: * The Initial Developer of the Original Code is Lutris Technologies Inc.
004: * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
005: * Inc.
006: * All Rights Reserved.
007: *
008: * The contents of this file are subject to the Enhydra Public License Version
009: * 1.0 (the "License"); you may not use this file except in compliance with the
010: * License. You may obtain a copy of the License at
011: * http://www.enhydra.org/software/license/epl.html
012: *
013: * Software distributed under the License is distributed on an "AS IS" basis,
014: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
015: * License for the specific language governing rights and limitations under the
016: * License.
017: *
018: *
019: */
020:
021: package golfShop.data.item;
022:
023: import java.util.Vector;
024: import java.util.Enumeration;
025: import java.lang.Object;
026:
027: // TODO - convert longs to ulong
028: // TODO - autogenerate objectID
029:
030: /**
031: * Categorize the items we have for sale.
032: *
033: * @author Scott Pirie
034: * @version $Revision: 1.1 $
035: */
036: public class CategoryDO {
037: /*----------------------------------------------------------------------*/
038: // Class Data
039: /*----------------------------------------------------------------------*/
040: private static CategoryStore storage = null;
041:
042: /*----------------------------------------------------------------------*/
043: // Class Methods
044: /*----------------------------------------------------------------------*/
045:
046: /* Determine the storage media selected for the category and item
047: * database.
048: */
049:
050: public static void InitializeStorageOption(String opt, String dir) {
051:
052: try {
053:
054: if (opt.equalsIgnoreCase("memory")) {
055: storage = new CategoryStoreMemory();
056: storage.initializeCategoryStore();
057: } else if (opt.equalsIgnoreCase("file")) {
058: storage = new CategoryStoreFile();
059: storage.initializeCategoryStore(dir);
060:
061: } else {
062: storage = new CategoryStoreMemory();
063: storage.initializeCategoryStore();
064: }
065: } catch (Exception e) {
066: }
067:
068: }
069:
070: /* Constructor. This is private so non item classes must go
071: * through the provided interfaces below to add new objects.
072: */
073: protected CategoryDO(long objectId, long parentId, String name) {
074: this .objectId = objectId;
075: this .parentId = parentId;
076: this .name = name;
077: this .subList = new Vector();
078: this .itemList = new Vector();
079: this .isDirty = false;
080:
081: // Update parent with subcategory.
082: CategoryDO parent = getCategory(parentId);
083: if (parent != null)
084: parent.addSubCategory(objectId);
085: }
086:
087: /*
088: * Find the CategoryDO given the object id.
089: */
090: public static CategoryDO getCategory(long objid) {
091: return (storage.findCategoryInStore(objid));
092: }
093:
094: /*----------------------------------------------------------------------*/
095: // Instance Data
096: /*----------------------------------------------------------------------*/
097: private long objectId; // Application index
098: private long parentId; // Parent category index
099: private String name; // Human read-able
100: private Vector subList; // Sub Category List
101: private Vector itemList; // Items in this Category
102: private boolean isDirty; // Set if instance has been modified.
103:
104: /*----------------------------------------------------------------------*/
105: // Instance Methods
106: /*----------------------------------------------------------------------*/
107:
108: public long getObjectId() {
109: return this .objectId;
110: }
111:
112: // This is the key so it shouldn't be changed by anyone.
113: protected void setObjectId(long newId) {
114: this .objectId = newId;
115: }
116:
117: public long getParentId() {
118: return this .parentId;
119: }
120:
121: public void setParentId(long newId) {
122: this .parentId = newId;
123: }
124:
125: public String getName() {
126: return this .name;
127: }
128:
129: public void setName(String newName) {
130: this .name = newName;
131: }
132:
133: public void addSubCategory(long subObjectId) {
134: // Verify the id isn't already a sub category
135: for (Enumeration e = this .subList.elements(); e
136: .hasMoreElements();) {
137: Long o = (Long) e.nextElement();
138: if (o.longValue() == subObjectId)
139: return;
140: }
141:
142: // Add the sub category
143: Long newsub = new Long(subObjectId);
144: this .subList.addElement(newsub);
145: }
146:
147: public void removeSubCategory(long subObjectId) {
148: for (Enumeration e = this .subList.elements(); e
149: .hasMoreElements();) {
150: Long o = (Long) e.nextElement();
151: if (o.longValue() == subObjectId)
152: this .subList.removeElement((Object) o);
153: }
154: }
155:
156: /*
157: * Return an array of subcategory object ids.
158: */
159: public long[] getSubCategories() {
160: Enumeration e;
161: int count;
162:
163: long[] list = new long[this .subList.size()];
164:
165: for (e = this .subList.elements(), count = 0; e
166: .hasMoreElements(); count++) {
167: Long o = (Long) e.nextElement();
168: list[count] = o.longValue();
169: }
170: return list;
171: }
172:
173: /*
174: * Add an item to this category.
175: */
176: public void addItem(long objectId) {
177: // Verify the id isn't already in the list
178: for (Enumeration e = this .itemList.elements(); e
179: .hasMoreElements();) {
180: Long o = (Long) e.nextElement();
181: if (o.longValue() == objectId)
182: return;
183: }
184:
185: // Add the item
186: Long newitem = new Long(objectId);
187: this .itemList.addElement(newitem);
188: }
189:
190: public void removeItem(long objectId) {
191: for (Enumeration e = this .itemList.elements(); e
192: .hasMoreElements();) {
193: Long o = (Long) e.nextElement();
194: if (o.longValue() == objectId)
195: this .itemList.removeElement(o);
196: }
197: }
198:
199: /*
200: * Return an array of Item object ids.
201: */
202: public long[] getItems() {
203: Enumeration e;
204: int count;
205:
206: long[] list = new long[this .itemList.size()];
207:
208: for (e = this .itemList.elements(), count = 0; e
209: .hasMoreElements(); count++) {
210: Long o = (Long) e.nextElement();
211: list[count] = o.longValue();
212: }
213: return list;
214: }
215: }
|