001: /**
002: * <copyright>
003: *
004: * Copyright 2001-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: */package org.cougaar.tools.csmart.recipe;
026:
027: import org.cougaar.tools.csmart.core.cdata.ComponentData;
028: import org.cougaar.tools.csmart.core.db.PopulateDb;
029: import org.cougaar.tools.csmart.core.property.Property;
030:
031: import java.io.Serializable;
032: import java.net.URL;
033: import java.sql.SQLException;
034: import java.util.Set;
035:
036: public class ParameterInsertionRecipe extends RecipeBase implements
037: Serializable {
038:
039: private static final String DESCRIPTION_RESOURCE_NAME = "parameter-insertion-recipe-description.html";
040: private static final String BACKUP_DESCRIPTION = "ParameterInsertionRecipe provides a method for inserting new Parameters into a Component";
041:
042: private static final String PROP_QUERY = "Agent Query";
043: private static final String PROP_QUERY_DFLT = "";
044: private static final String PROP_QUERY_DESC = "Query for Agents to whose components to add a parameter";
045:
046: private static final String PROP_PLUGINNAME = "Plugin Name";
047: private static final String PROP_PLUGINNAME_DFLT = "";
048: private static final String PROP_PLUGINNAME_DESC = "Name of the Plugin(s) to add a Parameter to";
049:
050: private static final String PROP_PARAMETER = "Parameter";
051: private static final String PROP_PARAMETER_DFLT = "";
052: private static final String PROP_PARAMETER_DESC = "The new Parameter";
053:
054: private Property propQuery;
055: private Property propPluginName;
056: private Property propParameter;
057:
058: public ParameterInsertionRecipe() {
059: super ("Parameter Insertion Recipe");
060: }
061:
062: public ParameterInsertionRecipe(String name) {
063: super (name);
064: }
065:
066: public void initProperties() {
067: propQuery = addRecipeQueryProperty(PROP_QUERY, PROP_QUERY_DFLT);
068: propQuery.setToolTip(PROP_QUERY_DESC);
069:
070: propPluginName = addProperty(PROP_PLUGINNAME,
071: PROP_PLUGINNAME_DFLT);
072: propPluginName.setToolTip(PROP_PLUGINNAME_DESC);
073:
074: propParameter = addProperty(PROP_PARAMETER, PROP_PARAMETER_DFLT);
075: propParameter.setToolTip(PROP_PARAMETER_DESC);
076:
077: }
078:
079: /**
080: * Gets the name of the html help file for this component.
081: *
082: * @return an <code>URL</code> value
083: */
084: public URL getDescription() {
085: return getClass().getResource(DESCRIPTION_RESOURCE_NAME);
086: }
087:
088: private Property addRecipeQueryProperty(String name, String dflt) {
089: Property prop = addProperty(new RecipeQueryProperty(this , name,
090: dflt));
091: prop.setPropertyClass(String.class);
092: return prop;
093: }
094:
095: public ComponentData modifyComponentData(ComponentData data,
096: PopulateDb pdb) {
097: try {
098: Set targets = pdb.executeQuery(propQuery.getValue()
099: .toString());
100: modifyComponentData(data, pdb, targets);
101: } catch (SQLException sqle) {
102: if (log.isErrorEnabled()) {
103: log.error("Exception", sqle);
104: }
105: }
106: return data;
107: }
108:
109: private void modifyComponentData(ComponentData data,
110: PopulateDb pdb, Set targets) throws SQLException {
111: // FIXME!!
112: // This should ask PopulateDb to construct the alib id.
113: // Also of course this isn't enough
114: String pluginAlib = data.getName() + "|"
115: + propPluginName.getValue().toString();
116: // FIXME: This code does not check that the newly modified child is not now a duplicate within the Agent!
117:
118: // FIXME: Is the slot really NAME or CLASS or must it be the same?!!!
119: if (targets.contains(pdb.getComponentAlibId(data))) {
120: ComponentData[] children = data.getChildren();
121: for (int i = 0; i < children.length; i++) {
122: //if (children[i].getName().equals(pluginAlib)) {
123: // Compare the plugins name, not the alib id, right?
124: if (children[i].getAlibID() != null
125: && children[i].getAlibID().equals(pluginAlib)) {
126: if (log.isDebugEnabled()) {
127: log.debug("Got match for pluginalibid: "
128: + pluginAlib + " on child "
129: + children[i].toString());
130: }
131: children[i].addParameter(propParameter.getValue()
132: .toString());
133: continue;
134: } else if (children[i].getClassName().equals(
135: propPluginName.getValue().toString())) {
136: if (log.isDebugEnabled()) {
137: log.debug("Got match for plugin class: "
138: + children[i].toString());
139: }
140: // FIXME: If this agent has 2 plugins with the same class,
141: // which do I do? This currently does the first only
142: // FIXME: Make sure this parameter isnt already there?
143: children[i].addParameter(propParameter.getValue()
144: .toString());
145: continue;
146: } else if (children[i].getName().equals(
147: propPluginName.getValue().toString())) {
148: if (log.isDebugEnabled()) {
149: log.debug("Got match for plugin name: "
150: + children[i].toString());
151: }
152: // FIXME: If this agent has 2 plugins with the same name,
153: // which do I do? This currently does the first only
154: // FIXME: Make sure this parameter isnt already there?
155: children[i].addParameter(propParameter.getValue()
156: .toString());
157: continue;
158: } else if (children[i].getName().equals(
159: data.getName() + "|"
160: + propPluginName.getValue().toString())) {
161: if (log.isDebugEnabled()) {
162: log
163: .debug("Got match for plugin name with parent name in front: "
164: + children[i].toString());
165: }
166: // FIXME: If this agent has 2 plugins with the same name,
167: // which do I do? This currently does the first only
168: // FIXME: Make sure this parameter isnt already there?
169: children[i].addParameter(propParameter.getValue()
170: .toString());
171: continue;
172: // Here is the broken original version....
173: // } else if (children[i].getName().equals(pluginAlib)) {
174: // if(log.isDebugEnabled()) {
175: // log.debug("Got match AGAINST NAME(?) for pluginalibid: " +
176: // pluginAlib + " on child " + children[i].toString());
177: // }
178: // children[i].addParameter(propParameter.getValue().toString());
179: // break;
180: }
181: }
182: }
183: if (data.childCount() > 0) {
184: // for each child, call this same method.
185: ComponentData[] children = data.getChildren();
186: for (int i = 0; i < children.length; i++) {
187: modifyComponentData(children[i], pdb, targets);
188: }
189: }
190: }
191:
192: }// ParameterInsertionRecipe
|