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.io.*;
024: import java.util.*;
025: import java.lang.Double;
026: import com.lutris.logging.Logger;
027: import com.lutris.logging.LogChannel;
028: import com.lutris.appserver.server.Enhydra;
029: import com.lutris.util.Config;
030: import com.lutris.util.ConfigException;
031: import golfShop.data.item.ItemDO;
032: import golfShop.data.item.ItemStore;
033:
034: /**
035: *
036: * @author Scott Pirie
037: * @version $Revision: 1.1 $
038: */
039: public class ItemStoreFile extends ItemStore {
040:
041: /*----------------------------------------------------------------------*/
042: // Class Data
043: /*----------------------------------------------------------------------*/
044:
045: private static Vector allItems = new Vector();
046:
047: /*----------------------------------------------------------------------*/
048: // Class Methods
049: /*----------------------------------------------------------------------*/
050: protected void initializeItemStore() {
051: }
052:
053: protected void initializeItemStore(String dir) {
054:
055: // Find all items in the inventory tree
056: findAllItems(dir, "");
057: }
058:
059: protected boolean isItemInStore(long findId) {
060: Enumeration e = allItems.elements();
061: while (e.hasMoreElements()) {
062: ItemDO c = (ItemDO) e.nextElement();
063: if (c.getObjectId() == findId)
064: return true;
065: }
066: return false;
067: }
068:
069: protected ItemDO findItemInStore(long findId) {
070:
071: Enumeration e = allItems.elements();
072: while (e.hasMoreElements()) {
073: ItemDO c = (ItemDO) e.nextElement();
074: if (c.getObjectId() == findId)
075: return c;
076: }
077: return null;
078: }
079:
080: /*----------------------------------------------------------------------*/
081: // Private Methods
082: /*----------------------------------------------------------------------*/
083:
084: private static void findAllItems(String root, String dir) {
085: String names[] = new String[0]; // list of files in a diretory
086: String filename;
087: File path, fpath;
088:
089: path = new File(root
090: + ((dir.length()) > 0 ? File.separator + dir : ""));
091:
092: // get list of files in this dir
093:
094: names = path.list();
095:
096: for (int i = 0; i < names.length; i++) {
097: filename = root
098: + ((dir.length()) > 0 ? File.separator + dir : "")
099: + File.separator + names[i];
100:
101: fpath = new File(filename);
102: if (fpath.isDirectory()) {
103: // recursively descend dir tree
104: findAllItems(root, dir + File.separator + names[i]);
105: }
106: if (fpath.isFile()) {
107: try {
108:
109: // Read details from file
110: BufferedReader br = new BufferedReader(
111: new FileReader(filename));
112:
113: allItems.addElement(new ItemDO((Long
114: .parseLong(names[i])), br.readLine(),
115: (Double.valueOf(br.readLine()))
116: .doubleValue(), br.readLine(), br
117: .readLine()));
118: } catch (Exception ioErr) {
119: LogChannel chan = Enhydra.getLogChannel();
120: if (chan != null)
121: chan.write(Logger.ERROR,
122: "Error reading products.\n"
123: + "Couln't open file "
124: + filename);
125: }
126: }
127:
128: }
129: }
130:
131: }
|