KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > golfShop > data > item > CategoryDO


1 /*
2  * Enhydra Java Application Server
3  * The Initial Developer of the Original Code is Lutris Technologies Inc.
4  * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
5  * Inc.
6  * All Rights Reserved.
7  *
8  * The contents of this file are subject to the Enhydra Public License Version
9  * 1.0 (the "License"); you may not use this file except in compliance with the
10  * License. You may obtain a copy of the License at
11  * http://www.enhydra.org/software/license/epl.html
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15  * License for the specific language governing rights and limitations under the
16  * License.
17  *
18  *
19  */

20
21 package golfShop.data.item;
22
23 import java.util.Vector JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.lang.Object JavaDoc;
26
27
28 // TODO - convert longs to ulong
29
// TODO - autogenerate objectID
30

31 /**
32  * Categorize the items we have for sale.
33  *
34  * @author Scott Pirie
35  * @version $Revision: 1.1 $
36  */

37 public class CategoryDO
38 {
39     /*----------------------------------------------------------------------*/
40     // Class Data
41
/*----------------------------------------------------------------------*/
42     private static CategoryStore storage = null;
43
44     /*----------------------------------------------------------------------*/
45     // Class Methods
46
/*----------------------------------------------------------------------*/
47
48     /* Determine the storage media selected for the category and item
49      * database.
50      */

51  
52 public static void InitializeStorageOption(String JavaDoc opt,String JavaDoc dir) {
53             
54     try
55     {
56   
57           if (opt.equalsIgnoreCase("memory")){
58        storage = new CategoryStoreMemory();
59        storage.initializeCategoryStore();
60        }
61         else if (opt.equalsIgnoreCase("file"))
62         {
63        storage = new CategoryStoreFile();
64        storage.initializeCategoryStore(dir);
65         
66        }
67         else
68         {
69             storage = new CategoryStoreMemory();
70             storage.initializeCategoryStore();
71         }
72     }
73     catch (Exception JavaDoc e)
74     {
75     }
76             
77 }
78
79    
80    
81    
82     /* Constructor. This is private so non item classes must go
83      * through the provided interfaces below to add new objects.
84      */

85     protected CategoryDO(long objectId, long parentId, String JavaDoc name)
86     {
87     this.objectId = objectId;
88     this.parentId = parentId;
89     this.name = name;
90     this.subList = new Vector JavaDoc();
91     this.itemList = new Vector JavaDoc();
92     this.isDirty = false;
93
94     // Update parent with subcategory.
95
CategoryDO parent = getCategory(parentId);
96     if ( parent != null )
97         parent.addSubCategory(objectId);
98     }
99
100     /*
101      * Find the CategoryDO given the object id.
102      */

103     public static CategoryDO getCategory(long objid)
104     {
105     return(storage.findCategoryInStore(objid));
106     }
107
108     /*----------------------------------------------------------------------*/
109     // Instance Data
110
/*----------------------------------------------------------------------*/
111     private long objectId; // Application index
112
private long parentId; // Parent category index
113
private String JavaDoc name; // Human read-able
114
private Vector JavaDoc subList; // Sub Category List
115
private Vector JavaDoc itemList; // Items in this Category
116
private boolean isDirty; // Set if instance has been modified.
117

118     /*----------------------------------------------------------------------*/
119     // Instance Methods
120
/*----------------------------------------------------------------------*/
121
122     public long getObjectId()
123     {
124     return this.objectId;
125     }
126     // This is the key so it shouldn't be changed by anyone.
127
protected void setObjectId(long newId)
128     {
129     this.objectId = newId;
130     }
131
132     public long getParentId()
133     {
134     return this.parentId;
135     }
136     public void setParentId(long newId)
137     {
138     this.parentId = newId;
139     }
140
141     public String JavaDoc getName()
142     {
143     return this.name;
144     }
145     public void setName(String JavaDoc newName)
146     {
147     this.name = newName;
148     }
149
150     public void addSubCategory(long subObjectId)
151     {
152     // Verify the id isn't already a sub category
153
for ( Enumeration JavaDoc e = this.subList.elements(); e.hasMoreElements() ; )
154     {
155         Long JavaDoc o = (Long JavaDoc) e.nextElement() ;
156         if (o.longValue() == subObjectId)
157         return;
158     }
159
160     // Add the sub category
161
Long JavaDoc newsub = new Long JavaDoc(subObjectId);
162     this.subList.addElement(newsub);
163     }
164     public void removeSubCategory(long subObjectId)
165     {
166     for ( Enumeration JavaDoc e = this.subList.elements(); e.hasMoreElements() ; )
167     {
168         Long JavaDoc o = (Long JavaDoc) e.nextElement() ;
169         if (o.longValue() == subObjectId)
170         this.subList.removeElement((Object JavaDoc) o);
171     }
172     }
173
174     /*
175      * Return an array of subcategory object ids.
176      */

177     public long[] getSubCategories()
178     {
179         Enumeration JavaDoc e;
180         int count;
181
182     long[] list = new long[this.subList.size()];
183
184     for ( e = this.subList.elements(), count = 0 ;
185         e.hasMoreElements() ;
186             count++ )
187     {
188         Long JavaDoc o = (Long JavaDoc) e.nextElement() ;
189         list[count] = o.longValue();
190     }
191     return list;
192     }
193
194     /*
195      * Add an item to this category.
196      */

197     public void addItem(long objectId)
198     {
199     // Verify the id isn't already in the list
200
for ( Enumeration JavaDoc e = this.itemList.elements(); e.hasMoreElements() ; )
201     {
202         Long JavaDoc o = (Long JavaDoc) e.nextElement() ;
203         if (o.longValue() == objectId)
204         return;
205     }
206
207     // Add the item
208
Long JavaDoc newitem = new Long JavaDoc(objectId);
209     this.itemList.addElement(newitem);
210     }
211     public void removeItem(long objectId)
212     {
213     for ( Enumeration JavaDoc e = this.itemList.elements(); e.hasMoreElements() ; )
214     {
215         Long JavaDoc o = (Long JavaDoc) e.nextElement() ;
216         if (o.longValue() == objectId)
217         this.itemList.removeElement(o);
218     }
219     }
220     /*
221      * Return an array of Item object ids.
222      */

223     public long[] getItems()
224     {
225     Enumeration JavaDoc e;
226     int count;
227
228     long[] list = new long[this.itemList.size()];
229
230     for ( e = this.itemList.elements(), count = 0 ;
231         e.hasMoreElements() ;
232             count ++)
233     {
234         Long JavaDoc o = (Long JavaDoc) e.nextElement() ;
235         list[count] = o.longValue();
236     }
237     return list;
238     }
239 }
240
Popular Tags