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.plugin.inventory;
027:
028: import java.util.Calendar;
029: import java.util.GregorianCalendar;
030: import java.util.TimeZone;
031: import java.io.FileWriter;
032: import java.io.File;
033: import java.io.IOException;
034: import java.util.ArrayList;
035: import java.util.Date;
036:
037: import org.cougaar.core.service.LoggingService;
038: import org.cougaar.glm.ldm.asset.Organization;
039: import org.cougaar.planning.ldm.asset.Asset;
040:
041: /**
042: * <pre>
043: *
044: * The LogisticsInventoryLogger is a specialized logger seperate from
045: * the infrastructure logging facility that logs to files the special
046: * csv files for Inventory testing.
047: *
048: * There is a one to one correspondance between these and the inventories
049: * managed by an inventory plugin. Most of the logging is invoked by
050: * the LogisticsInventoryBG. The LogisticsInventoryWriter nows how to
051: * write to one of these streams.
052: *
053: * @see LogisticsInventoryBG
054: *
055: **/
056:
057: public class LogisticsInventoryLogger extends FileWriter {
058:
059: protected static String datestamp = null;
060: protected static String baseDir = null;
061: protected String fileId;
062:
063: protected LoggingService logger;
064:
065: public LogisticsInventoryLogger(File aFile, boolean append,
066: UtilsProvider invPlugin) throws IOException {
067: super (aFile, append);
068: logger = invPlugin.getLoggingService(this );
069: }
070:
071: public static LogisticsInventoryLogger createInventoryLogger(
072: Asset invAsset, Organization anOrg, boolean doAppend,
073: UtilsProvider invPlugin) {
074: LogisticsInventoryLogger newLogger = null;
075: LoggingService classLogger = invPlugin
076: .getLoggingService(LogisticsInventoryLogger.class);
077: initializeClass(classLogger);
078: //Initialize the file to COUGAAR_WORKSPACE\inventory\organizationid\datestamp\NSNinv.csv
079: String orgId = anOrg.getItemIdentificationPG()
080: .getItemIdentification();
081: orgId = orgId.replaceAll("UIC/", "");
082: String pathId = baseDir + File.separator + "inventory"
083: + File.separator + orgId + File.separator + datestamp;
084: String item = invAsset.getTypeIdentificationPG()
085: .getTypeIdentification();
086: item = item.replaceAll("/", "-");
087: String fileId = pathId + File.separator + item + "inv.csv";
088: File csvFile = new File(fileId);
089: File pathDirs = new File(pathId);
090: try {
091: pathDirs.mkdirs();
092: csvFile.createNewFile();
093: newLogger = new LogisticsInventoryLogger(csvFile, doAppend,
094: invPlugin);
095: } catch (Exception e) {
096: classLogger.error("Error creating csv file " + fileId, e);
097: }
098:
099: return newLogger;
100:
101: }
102:
103: protected void finalize() throws Throwable {
104: flush();
105: close();
106: super .finalize();
107: }
108:
109: private static String prependSingle(int digit) {
110: if (digit < 10) {
111: return "0" + digit;
112: } else {
113: return "" + digit;
114: }
115: }
116:
117: private static void initializeClass(LoggingService classLogger) {
118: if (datestamp == null) {
119: //TimeZone est = TimeZone.getTimeZone("EST");
120: TimeZone gmt = TimeZone.getDefault();
121: GregorianCalendar now = new GregorianCalendar(gmt);
122: now.setTime(new Date());
123: datestamp = "" + now.get(Calendar.YEAR);
124: datestamp += prependSingle(now.get(Calendar.MONTH) + 1);
125: datestamp += prependSingle(now.get(Calendar.DAY_OF_MONTH));
126: datestamp += prependSingle(now.get(Calendar.HOUR_OF_DAY));
127: datestamp += prependSingle(now.get(Calendar.MINUTE));
128: baseDir = System.getProperty("org.cougaar.workspace");
129: if ((baseDir == null) || (baseDir.equals(""))) {
130: classLogger
131: .error("System property org.cougaar.workspace is null, please set cougaar workspace");
132: baseDir = ".";
133: }
134: }
135: }
136:
137: }
|