001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.logistics.ui.inventory;
027:
028: import org.cougaar.logistics.ui.inventory.data.InventoryData;
029: import org.cougaar.logistics.ui.inventory.data.InventoryPreferenceData;
030: import org.cougaar.logistics.plugin.inventory.LogisticsInventoryFormatter;
031:
032: import java.util.Hashtable;
033: import java.io.File;
034: import java.io.BufferedReader;
035: import java.io.FileReader;
036: import java.io.IOException;
037:
038: public class InventoryUnitConversionTable {
039:
040: Hashtable converterTable;
041:
042: public final static int ITEM_INDEX = 0;
043: public final static int UNIT_FROM_INDEX = 1;
044: public final static int UNIT_TO_INDEX = 2;
045: public final static int FACTOR_INDEX = 3;
046:
047: public InventoryUnitConversionTable() {
048: converterTable = new Hashtable();
049:
050: addEntry(LogisticsInventoryFormatter.WATER_ITEM_ID,
051: InventoryPreferenceData.CASES_LABEL,
052: InventoryPreferenceData.BOTTLES_LABEL, 12);
053:
054: addEntry(LogisticsInventoryFormatter.WATER_ITEM_ID,
055: InventoryPreferenceData.CASES_LABEL,
056: InventoryPreferenceData.LITERS_LABEL, 12);
057:
058: }
059:
060: /**
061:
062: private void add(String item, InventoryUnitConverter converter) {
063: Hashtable unitTable = (Hashtable) converterTable.get(item);
064: if(unitTable == null) {
065: unitTable = new Hashtable();
066: converterTable.put(item,unitTable);
067: }
068: unitTable.put(converter.getBaseUnit(), unitTable);
069: }
070:
071: **/
072:
073: public void parseAndAddFile(String fileStr) {
074: File file = new File(fileStr);
075: if (file.exists()) {
076: parse(file);
077: }
078: }
079:
080: public void parse(File file) {
081: try {
082: BufferedReader br = new BufferedReader(new FileReader(file));
083:
084: String nextLine = br.readLine();
085: while (nextLine != null) {
086: parseAndAddEntry(nextLine + "\n");
087: nextLine = br.readLine();
088: }
089: br.close();
090: } catch (IOException ioe) {
091: throw new RuntimeException(ioe);
092: }
093:
094: }
095:
096: protected void parseAndAddEntry(String line) {
097: String lines[] = line.split(",");
098: String item = lines[ITEM_INDEX];
099: String fromUnit = lines[UNIT_FROM_INDEX];
100: String toUnit = lines[UNIT_TO_INDEX];
101: double factor = Double.parseDouble(lines[FACTOR_INDEX]);
102:
103: addEntry(item, fromUnit, toUnit, factor);
104: }
105:
106: private void addEntry(String item, String fromUnit, String toUnit,
107: double factor) {
108: Hashtable unitTable = (Hashtable) converterTable.get(item);
109: if (unitTable == null) {
110: unitTable = new Hashtable();
111: converterTable.put(item, unitTable);
112: }
113: InventoryUnitConverter converter = (InventoryUnitConverter) unitTable
114: .get(fromUnit);
115: if (converter == null) {
116: converter = new InventoryUnitConverter(fromUnit);
117: unitTable.put(fromUnit, converter);
118: }
119: converter.addFactor(toUnit, factor);
120: }
121:
122: private InventoryUnitConverter getConverterForData(
123: InventoryData data) {
124: return getConverter(data.getItem(), data.getUnit());
125: }
126:
127: private InventoryUnitConverter getConverter(String item, String unit) {
128: Hashtable unitTable = (Hashtable) converterTable.get(item);
129: InventoryUnitConverter converter = null;
130: if (unitTable != null) {
131: converter = (InventoryUnitConverter) unitTable.get(unit);
132: }
133: if (converter == null) {
134: /** TODO: MWD This is a hack until we are assured the files that are
135: * being examined are produced by the current LogisticsInventoryFormatter.
136: * and have the proper units (most notably water) in the xml contents.
137: * In due time Jan 2004, take out the next if statement and only
138: * return a newly built converter.
139: **/
140: if (unitTable != null && unitTable.size() == 1) {
141: converter = (InventoryUnitConverter) unitTable
142: .elements().nextElement();
143: } else {
144: converter = new InventoryUnitConverter(unit);
145: }
146: }
147: return converter;
148: }
149:
150: public InventoryUnitConversion getConversionForData(
151: InventoryData data, InventoryPreferenceData prefs) {
152: InventoryUnitConverter converter = getConverterForData(data);
153: return converter.getConversionForUnit(prefs
154: .getPreferredUnit(data));
155: }
156:
157: }
|