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:
027: package org.cougaar.planning.examples;
028:
029: import java.sql.*;
030: import java.util.*;
031: import org.cougaar.planning.ldm.asset.*;
032: import org.cougaar.planning.plugin.legacy.SimplePlugin;
033:
034: // Simple plugin to read a JDBC datasource
035: // and create instances of specified prototypes
036:
037: // Data table schema:
038: // create table Assets (
039: // Organization String,
040: // Prototype String,
041: // UniqueID String,
042: // Quantity Int -- If > 1, autogenerate ID's using UniqueID as prefix
043: // );
044: //
045: public class AssetCreationJdbcPlugin extends SimplePlugin {
046: // Perform the initial query for instance directives, and create objects
047: // accordingly
048: public void setupSubscriptions() {
049: // Set up database access parameters
050: Vector params = getParameters();
051:
052: String driver_classname = (params.size() >= 1 ? (String) getParameters()
053: .elementAt(1)
054: : "sun.jdbc.odbc.JdbcOdbcDriver");
055: String datasource_url = (params.size() >= 2 ? (String) getParameters()
056: .elementAt(2)
057: : "jdbc:odbc:ASSETS");
058: String datasource_username = (params.size() >= 3 ? (String) getParameters()
059: .elementAt(3)
060: : "");
061: String datasource_password = (params.size() >= 4 ? (String) getParameters()
062: .elementAt(4)
063: : "");
064:
065: createAssets(driver_classname, datasource_url,
066: datasource_username, datasource_password);
067:
068: }
069:
070: /**
071: * Create assets by connecting to 'Assets' table (as above)
072: * specified by given driver/url/user/password
073: * Create instance for each prototype listed for this organization
074: * Auto-generate ID's from given if quantity > 1
075: */
076: private void createAssets(String driver_classname,
077: String datasource_url, String datasource_username,
078: String datasource_password) {
079: System.out.println("Loading driver " + driver_classname);
080:
081: try {
082: Class driver = Class.forName(driver_classname);
083: } catch (ClassNotFoundException e) {
084: System.out.println("Could not load driver : "
085: + driver_classname);
086: e.printStackTrace();
087: }
088:
089: System.out.println("Connecting to the datasource : "
090: + datasource_url);
091: try {
092: Connection conn = DriverManager.getConnection(
093: datasource_url, datasource_username,
094: datasource_password);
095:
096: Statement stmt = conn.createStatement();
097:
098: String myOrgName = getMessageAddress().getAddress();
099:
100: String query = "select Organization, Prototype, UniqueID, Quantity from Assets";
101: ResultSet rset = stmt.executeQuery(query);
102:
103: while (rset.next()) {
104: String Organization = rset.getString(1);
105: String Prototype = rset.getString(2);
106: String UniqueID = rset.getString(3);
107: int Quantity = rset.getInt(4);
108:
109: if (Organization.equals(myOrgName)) {
110: for (int i = 1; i <= Quantity; i++) {
111: String newID = UniqueID;
112:
113: // Generate a new ID for each element in quantity, using
114: // UniqueID as prefix.
115: // '0-pad' the string based on string length of quantity value
116: if (Quantity > 1) {
117: int quantity_length = Integer.toString(
118: Quantity).length();
119: String count = Integer.toString(i);
120: while (count.length() < quantity_length)
121: count = '0' + count;
122: newID = UniqueID + count;
123: }
124:
125: // Create instance from prototype and ID
126: Asset asset = theLDMF.createInstance(Prototype,
127: newID);
128: // System.out.println("Asset = " + asset);
129: publishAdd(asset);
130: }
131: }
132: }
133:
134: conn.close();
135: } catch (SQLException sqle) {
136: System.out.println("SQL Exception");
137: sqle.printStackTrace();
138: }
139:
140: }
141:
142: // This plugin doesn't need to run in real-time, just at startup time
143: public void execute() {
144: }
145:
146: }
|