KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
24 import java.util.*;
25 import java.lang.Double JavaDoc;
26 import com.lutris.logging.Logger;
27 import com.lutris.logging.LogChannel;
28 import com.lutris.appserver.server.Enhydra;
29 import com.lutris.util.Config;
30 import com.lutris.util.ConfigException;
31 import golfShop.data.item.ItemDO;
32 import golfShop.data.item.ItemStore;
33
34
35 /**
36  *
37  * @author Scott Pirie
38  * @version $Revision: 1.1 $
39  */

40 public class ItemStoreFile extends ItemStore
41 {
42
43     /*----------------------------------------------------------------------*/
44     // Class Data
45
/*----------------------------------------------------------------------*/
46    
47     private static Vector allItems = new Vector();
48
49     /*----------------------------------------------------------------------*/
50     // Class Methods
51
/*----------------------------------------------------------------------*/
52    protected void initializeItemStore()
53     {}
54    
55     protected void initializeItemStore(String JavaDoc dir)
56     {
57
58
59     // Find all items in the inventory tree
60
findAllItems(dir, "");
61     }
62
63
64     protected boolean isItemInStore(long findId)
65     {
66     Enumeration e = allItems.elements();
67     while (e.hasMoreElements())
68     {
69         ItemDO c = (ItemDO) e.nextElement();
70         if (c.getObjectId() == findId)
71         return true;
72     }
73     return false;
74     }
75
76     protected ItemDO findItemInStore(long findId)
77     {
78         
79     Enumeration e = allItems.elements();
80     while (e.hasMoreElements())
81     {
82         ItemDO c = (ItemDO) e.nextElement();
83         if (c.getObjectId() == findId)
84         return c;
85     }
86     return null;
87     }
88
89     /*----------------------------------------------------------------------*/
90     // Private Methods
91
/*----------------------------------------------------------------------*/
92
93     private static void findAllItems (String JavaDoc root, String JavaDoc dir)
94     {
95     String JavaDoc names[] = new String JavaDoc[0]; // list of files in a diretory
96
String JavaDoc filename;
97     File path, fpath;
98
99     
100     path = new File(root +
101             ((dir.length())>0 ? File.separator + dir : ""));
102
103     // get list of files in this dir
104

105     
106     names = path.list();
107   
108     for (int i=0; i < names.length; i++)
109     {
110         filename = root +
111             ((dir.length())>0 ? File.separator + dir : "") +
112             File.separator + names[i];
113         
114         fpath = new File(filename);
115         if ( fpath.isDirectory() )
116         {
117         // recursively descend dir tree
118
findAllItems(root, dir+File.separator+names[i]);
119         }
120         if ( fpath.isFile() )
121         {
122         try
123         {
124
125             // Read details from file
126
BufferedReader br = new BufferedReader(new FileReader(filename));
127
128             allItems.addElement(new ItemDO(
129                         (Long.parseLong(names[i])),
130                         br.readLine(),
131                         (Double.valueOf(br.readLine())).doubleValue(),
132                         br.readLine(),
133                         br.readLine()));
134         } catch (Exception JavaDoc ioErr) {
135             LogChannel chan = Enhydra.getLogChannel();
136             if (chan != null)
137                 chan.write(Logger.ERROR, "Error reading products.\n" +
138                            "Couln't open file " + filename);
139             }
140         }
141         
142     }
143     }
144     
145 }
146
Popular Tags