001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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.tools.csmart.util;
027:
028: import org.cougaar.bootstrap.Bootstrapper;
029: import org.cougaar.util.DBConnectionPool;
030: import org.cougaar.util.DBProperties;
031: import org.cougaar.util.Parameters;
032:
033: import java.io.IOException;
034: import java.sql.Connection;
035: import java.sql.ResultSet;
036: import java.sql.SQLException;
037: import java.sql.Statement;
038:
039: /**
040: * Utility class for use by csmart/bin/exportExperiment script
041: * to ensure that ALIB IDs are correct for complex recipes.
042: * invoke with name of experiment being exported.
043: * DB tempcopy must exist.
044: * Uses CSMART.q & cougaar.rc to get DB connection info.
045: **/
046: public class ExportExperimentHelper {
047:
048: // get the alib_ids that need to be updated - one per
049: // complex recipe
050: private static String getAlibQuery = "SELECT DISTINCT "
051: + "AA.COMPONENT_ALIB_ID "
052: + " FROM "
053: + " alib_component AA, "
054: + " asb_component_hierarchy AH, "
055: + " expt_trial_mod_recipe EA, "
056: + " lib_mod_recipe_arg MA, "
057: + " expt_trial ET, "
058: + " expt_experiment E "
059: + " WHERE "
060: + " E.NAME = ':exptName' "
061: + " AND E.EXPT_ID = ET.EXPT_ID "
062: + " AND ET.TRIAL_ID = EA.TRIAL_ID "
063: + " AND EA.MOD_RECIPE_LIB_ID = MA.MOD_RECIPE_LIB_ID "
064: + " AND MA.ARG_NAME = 'Assembly Id' "
065: + " AND MA.ARG_VALUE = AH.ASSEMBLY_ID "
066: + " AND (AH.COMPONENT_ALIB_ID = AA.COMPONENT_ALIB_ID OR AH.PARENT_COMPONENT_ALIB_ID = AA.COMPONENT_ALIB_ID) "
067: + " AND AA.COMPONENT_TYPE = 'recipe'";
068:
069: // Queries to update the alib_ids to match the exported
070: // ID names
071: private static String[] updateAlibQueries = {
072: "UPDATE tempcopy.alib_component SET COMPONENT_ALIB_ID = CONCAT(':old_alib_id', '-cpy') WHERE COMPONENT_ALIB_ID = ':old_alib_id'",
073: "UPDATE tempcopy.alib_component SET COMPONENT_NAME = CONCAT(':old_alib_id', '-cpy') WHERE COMPONENT_NAME = ':old_alib_id'",
074: "UPDATE tempcopy.asb_component_arg SET COMPONENT_ALIB_ID = CONCAT(':old_alib_id', '-cpy') WHERE COMPONENT_ALIB_ID = ':old_alib_id'",
075: "UPDATE tempcopy.asb_component_hierarchy SET COMPONENT_ALIB_ID = CONCAT(':old_alib_id', '-cpy') WHERE COMPONENT_ALIB_ID = ':old_alib_id'",
076: "UPDATE tempcopy.asb_component_hierarchy SET PARENT_COMPONENT_ALIB_ID = CONCAT(':old_alib_id', '-cpy') WHERE PARENT_COMPONENT_ALIB_ID = ':old_alib_id'" };
077:
078: public static void main(String[] args) {
079: if ("true".equals(System.getProperty(
080: "org.cougaar.useBootstrapper", "true"))) {
081: Bootstrapper.launch(ExportExperimentHelper.class.getName(),
082: args);
083: } else {
084: launch(args);
085: }
086: }
087:
088: public static void launch(String[] args) {
089: boolean debug = false;
090: if (args.length < 1) {
091: System.out
092: .println("ExportExperimentHelper error. Expected first argument of a valid experiment name, second optional argument to turn on debug messages.");
093: System.exit(-1);
094: } else if (args.length == 2)
095: debug = true;
096:
097: String eName = args[0];
098:
099: if (debug)
100: System.out.println("Using experiment name " + eName);
101:
102: // Get the corrected query for ALIB IDs, using the experiment name
103: String getAQuery = ExportExperimentHelper
104: .parseQuery(":exptName", eName,
105: ExportExperimentHelper.getAlibQuery);
106: if (getAQuery == null) {
107: System.out
108: .println("ExportExperimentHelper couldnt parse out query to get ALIB IDs, using experiment name "
109: + eName
110: + ". Check that you supplied a valid experiment name.");
111: System.exit(-1);
112: } else {
113: if (debug)
114: System.out.println("Parsed ALIB ID query = "
115: + getAQuery);
116: }
117:
118: // Now the real work
119: Connection conn = null;
120: try {
121: conn = ExportExperimentHelper.getConnection();
122: try {
123: Statement stmt = conn.createStatement();
124: ResultSet rs = stmt.executeQuery(getAQuery);
125: String updQuery = null;
126: Statement stmt2 = conn.createStatement();
127: ResultSet rs2 = null;
128:
129: // For each ALIB ID that needs updating
130: while (rs.next()) {
131: // For each update query
132: for (int i = 0; i < ExportExperimentHelper.updateAlibQueries.length; i++) {
133: // Get a corrected query
134: updQuery = ExportExperimentHelper
135: .parseQuery(
136: ":old_alib_id",
137: rs.getString(1),
138: ExportExperimentHelper.updateAlibQueries[i]);
139: if (updQuery != null) {
140: if (debug)
141: System.out
142: .println("Doing update query = "
143: + updQuery);
144: // Do the update
145: try {
146: rs2 = stmt2.executeQuery(updQuery);
147: } finally {
148: rs2.close();
149: }
150: }
151: } // loop over update queries
152: } // loop over ALIB IDs to update
153: rs.close();
154: stmt2.close();
155: stmt.close();
156: } finally {
157: conn.close();
158: }
159: } catch (SQLException sqe) {
160: }
161:
162: } // done with launch
163:
164: /**
165: * Cribbed from DBUtils - get a connection to the db
166: * specified by CSMART.q, without using the logger.
167: **/
168: public static Connection getConnection() {
169: DBProperties dbProps;
170: String database;
171: String username;
172: String password;
173: Connection conn = null;
174: try {
175: dbProps = DBProperties.readQueryFile("CSMART.q", "csmart");
176: database = dbProps.getProperty("database");
177: username = dbProps.getProperty("username");
178: password = dbProps.getProperty("password");
179: String dbtype = dbProps.getDBType();
180: String driverParam = "driver." + dbtype;
181: String driverClass = Parameters.findParameter(driverParam);
182: if (driverClass == null) {
183: System.err
184: .println("ExportExperimentHelper found bad driver specified in cougaar.rc and CSMART.q. Check your instalation. Driver was "
185: + driverParam);
186: System.exit(-1);
187: }
188: Class.forName(driverClass);
189: conn = DBConnectionPool.getConnection(database, username,
190: password);
191: } catch (IOException e) {
192: e.printStackTrace();
193: System.exit(-1);
194: } catch (ClassNotFoundException ce) {
195: ce.printStackTrace();
196: System.exit(-1);
197: } catch (SQLException sqe) {
198: sqe.printStackTrace();
199: System.exit(-1);
200: }
201: return conn;
202: }
203:
204: /**
205: * Replace all instances of key with subst in the given
206: * String query, returning the result.
207: **/
208: public static String parseQuery(String key, String subst,
209: String query) {
210: StringBuffer tmp = new StringBuffer(query);
211: if (subst == null || key == null || key.equals(""))
212: return query;
213: int ix = 0;
214: while ((ix = tmp.indexOf(key, ix)) >= 0) {
215: tmp.replace(ix, ix + key.length(), subst);
216: ix = ix + key.length();
217: }
218: return tmp.substring(0);
219: }
220:
221: }
|