001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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.tools.csmart.ui.community;
028:
029: import org.cougaar.tools.csmart.core.db.DBUtils;
030: import org.cougaar.tools.csmart.ui.viewer.CSMART;
031: import org.cougaar.util.log.Logger;
032:
033: import java.sql.Connection;
034: import java.sql.ResultSet;
035: import java.sql.SQLException;
036: import java.sql.Statement;
037: import java.util.ArrayList;
038: import java.util.Collections;
039: import java.util.HashMap;
040: import java.util.Map;
041:
042: /**
043: * Methods for interfacing to database and supporting community displays.
044: * These methods do not modify the community table display.
045: */
046:
047: public class CommunityDBUtils {
048: private static final String GET_COMMUNITIES_QUERY = "queryCommunities";
049: private static final String GET_MY_COMMUNITIES_QUERY = "queryMyCommunities";
050: private static final String GET_ENTITIES_QUERY = "queryEntities";
051: private static final String GET_ENTITY_TYPE_QUERY = "queryEntityType";
052: private static final String INSERT_COMMUNITY_INFO_QUERY = "queryInsertCommunityInfo";
053: private static final String INSERT_COMMUNITY_ATTRIBUTE_QUERY = "queryInsertCommunityAttribute";
054: private static final String INSERT_ENTITY_INFO_QUERY = "queryInsertEntityInfo";
055: private static final String INSERT_ENTITY_ATTRIBUTE_QUERY = "queryInsertEntityAttribute";
056: private static final String IS_COMMUNITY_IN_USE_QUERY = "queryIsCommunityInUse";
057: private static final String UPDATE_COMMUNITY_ATTRIBUTE_ID_QUERY = "queryUpdateCommunityAttributeId";
058: private static final String UPDATE_COMMUNITY_ATTRIBUTE_VALUE_QUERY = "queryUpdateCommunityAttributeValue";
059: private static final String UPDATE_ENTITY_ATTRIBUTE_ID_QUERY = "queryUpdateEntityAttributeId";
060: private static final String UPDATE_ENTITY_ATTRIBUTE_VALUE_QUERY = "queryUpdateEntityAttributeValue";
061: private static final String DELETE_COMMUNITY_INFO_QUERY = "queryDeleteCommunityInfo";
062: private static final String DELETE_ENTITY_INFO_QUERY = "queryDeleteEntityInfo";
063: private static final String DELETE_COMMUNITY_ATTRIBUTE_QUERY = "queryDeleteCommunityAttribute";
064: private static final String DELETE_ENTITY_ATTRIBUTE_QUERY = "queryDeleteEntityAttribute";
065:
066: private static ArrayList doQuery(String query, Map substitutions) {
067: Logger log = CSMART
068: .createLogger("org.cougaar.tools.csmart.ui.community.CommunityDBUtils");
069: ArrayList results = new ArrayList();
070: try {
071: Connection conn = DBUtils.getConnection();
072: try {
073: Statement stmt = conn.createStatement();
074: query = DBUtils.getQuery(query, substitutions);
075: // execute query that may or may not return results
076: if (stmt.execute(query)) {
077: ResultSet rs = stmt.getResultSet();
078: while (rs.next())
079: results.add(rs.getString(1));
080: rs.close();
081: }
082: stmt.close();
083: } finally {
084: conn.close();
085: }
086: } catch (SQLException se) {
087: if (se.toString().indexOf("Duplicate") != -1
088: || se.toString().indexOf("Invalid argument value") != -1)
089: throw new IllegalArgumentException(se.toString());
090: else if (log.isErrorEnabled()) {
091: log.error("Caught SQL exception executing query: "
092: + query, se);
093: }
094: }
095: if (results == null || results.isEmpty()
096: && log.isDebugEnabled())
097: log.debug("doQuery got no results for " + query);
098: return results;
099: }
100:
101: /**
102: * Get all known community IDs
103: **/
104: public static ArrayList getCommunities() {
105: ArrayList communityIds = doQuery(GET_COMMUNITIES_QUERY,
106: new HashMap());
107: Collections.sort(communityIds);
108: return communityIds;
109: }
110:
111: /**
112: * Get all community IDs for this experiment
113: **/
114: public static ArrayList getCommunitiesForExperiment(
115: String assemblyId) {
116: Map substitutions = DatabaseTableModel
117: .getSubstitutions(assemblyId);
118: ArrayList communityIds = doQuery(GET_MY_COMMUNITIES_QUERY,
119: substitutions);
120: Collections.sort(communityIds);
121: return communityIds;
122: }
123:
124: /**
125: * Get all Entity IDs in the given community
126: *
127: * @param communityId a <code>String</code> value
128: * @param assemblyId a <code>String</code> Assembly to limit ourselves to
129: * @return an <code>ArrayList</code> value
130: */
131: public static ArrayList getEntities(String communityId,
132: String assemblyId) {
133: Map substitutions = DatabaseTableModel
134: .getSubstitutions(assemblyId);
135: substitutions.put(":community_id", communityId);
136: ArrayList entityIds = doQuery(GET_ENTITIES_QUERY, substitutions);
137: Collections.sort(entityIds);
138: return entityIds;
139: }
140:
141: /**
142: *
143: * @param entityId a <code>String</code> value
144: * @param assemblyId a <code>String</code> Assembly to limit ourselves to
145: * @return a <code>String</code> value
146: */
147: public static String getEntityType(String entityId,
148: String assemblyId) {
149: Map substitutions = DatabaseTableModel
150: .getSubstitutions(assemblyId);
151: substitutions.put(":entity_id", entityId);
152: ArrayList results = doQuery(GET_ENTITY_TYPE_QUERY,
153: substitutions);
154: if (results.size() != 0)
155: return (String) results.get(0);
156: return "Entity"; // default
157: }
158:
159: /**
160: *
161: * @param communityId a <code>String</code> value
162: * @param assemblyId a <code>String</code> Assembly to limit ourselves to
163: * @return a <code>boolean</code> value
164: */
165: public static boolean isCommunityInUse(String communityId,
166: String assemblyId) {
167: Map substitutions = DatabaseTableModel
168: .getSubstitutions(assemblyId);
169: substitutions.put(":community_id", communityId);
170: ArrayList results = doQuery(IS_COMMUNITY_IN_USE_QUERY,
171: substitutions);
172: return (results.size() != 0);
173: }
174:
175: /**
176: * Insert the community into the database.
177: * @param communityId the community
178: * @param communityType the community type
179: * @param assemblyId a <code>String</code> Assembly to limit ourselves to
180: */
181: public static void insertCommunityInfo(String communityId,
182: String communityType, String assemblyId) {
183: Map substitutions = DatabaseTableModel
184: .getSubstitutions(assemblyId);
185: substitutions.put(":community_id", communityId);
186: substitutions.put(":community_type", communityType);
187: doQuery(INSERT_COMMUNITY_INFO_QUERY, substitutions);
188: }
189:
190: /**
191: * Insert a new parameter for the community into the database.
192: * @param communityId the community
193: * @param assemblyId a <code>String</code> Assembly to limit ourselves to
194: */
195: public static void insertCommunityAttribute(String communityId,
196: String assemblyId) {
197: Map substitutions = DatabaseTableModel
198: .getSubstitutions(assemblyId);
199: substitutions.put(":community_id", communityId);
200: doQuery(INSERT_COMMUNITY_ATTRIBUTE_QUERY, substitutions);
201: }
202:
203: /**
204: * Insert the entity into the database.
205: * @param communityId the community
206: * @param entityId the ID of the entity
207: * @param attributeName the attribute to enter
208: * @param attributeValue the value of the new attribute
209: * @param assemblyId a <code>String</code> Assembly to limit ourselves to
210: */
211: public static void insertEntityInfo(String communityId,
212: String entityId, String attributeName,
213: String attributeValue, String assemblyId) {
214: Map substitutions = DatabaseTableModel
215: .getSubstitutions(assemblyId);
216: substitutions.put(":community_id", communityId);
217: substitutions.put(":entity_id", entityId);
218: substitutions.put(":attribute_id", attributeName);
219: substitutions.put(":attribute_value", attributeValue);
220: doQuery(INSERT_ENTITY_INFO_QUERY, substitutions);
221: }
222:
223: /**
224: * Insert a new parameter for the entity into the database.
225: * @param communityId the community
226: * @param entityId the entity
227: * @param assemblyId a <code>String</code> Assembly to limit ourselves to
228: */
229: public static void insertEntityAttribute(String communityId,
230: String entityId, String assemblyId) {
231: Map substitutions = DatabaseTableModel
232: .getSubstitutions(assemblyId);
233: substitutions.put(":community_id", communityId);
234: substitutions.put(":entity_id", entityId);
235: doQuery(INSERT_ENTITY_ATTRIBUTE_QUERY, substitutions);
236: }
237:
238: public static void setCommunityAttributeId(String communityId,
239: String attributeId, String prevAttributeId,
240: String attributeValue, String assemblyId) {
241: Map substitutions = DatabaseTableModel
242: .getSubstitutions(assemblyId);
243: substitutions.put(":community_id", communityId);
244: substitutions.put(":attribute_id", attributeId);
245: substitutions.put(":prev_attribute_id", prevAttributeId);
246: substitutions.put(":attribute_value", attributeValue);
247: doQuery(UPDATE_COMMUNITY_ATTRIBUTE_ID_QUERY, substitutions);
248: }
249:
250: public static void setCommunityAttributeValue(String communityId,
251: String attributeValue, String attributeId,
252: String prevAttributeValue, String assemblyId) {
253: Map substitutions = DatabaseTableModel
254: .getSubstitutions(assemblyId);
255: substitutions.put(":community_id", communityId);
256: substitutions.put(":attribute_value", attributeValue);
257: substitutions.put(":attribute_id", attributeId);
258: substitutions.put(":prev_attribute_value", prevAttributeValue);
259: doQuery(UPDATE_COMMUNITY_ATTRIBUTE_VALUE_QUERY, substitutions);
260: }
261:
262: public static void setEntityAttributeId(String communityId,
263: String entityId, String attributeId,
264: String prevAttributeId, String attributeValue,
265: String assemblyId) {
266: Map substitutions = DatabaseTableModel
267: .getSubstitutions(assemblyId);
268: substitutions.put(":community_id", communityId);
269: substitutions.put(":entity_id", entityId);
270: substitutions.put(":attribute_id", attributeId);
271: substitutions.put(":prev_attribute_id", prevAttributeId);
272: substitutions.put(":attribute_value", attributeValue);
273: doQuery(UPDATE_ENTITY_ATTRIBUTE_ID_QUERY, substitutions);
274: }
275:
276: public static void setEntityAttributeValue(String communityId,
277: String entityId, String attributeValue, String attributeId,
278: String prevAttributeValue, String assemblyId) {
279: Map substitutions = DatabaseTableModel
280: .getSubstitutions(assemblyId);
281: substitutions.put(":community_id", communityId);
282: substitutions.put(":entity_id", entityId);
283: substitutions.put(":attribute_value", attributeValue);
284: substitutions.put(":attribute_id", attributeId);
285: substitutions.put(":prev_attribute_value", prevAttributeValue);
286: doQuery(UPDATE_ENTITY_ATTRIBUTE_VALUE_QUERY, substitutions);
287: }
288:
289: public static void deleteCommunityInfo(String communityId,
290: String assemblyId) {
291: Map substitutions = DatabaseTableModel
292: .getSubstitutions(assemblyId);
293: substitutions.put(":community_id", communityId);
294: doQuery(DELETE_COMMUNITY_INFO_QUERY, substitutions);
295: }
296:
297: public static void deleteEntityInfo(String communityId,
298: String entityId, String assemblyId) {
299: Map substitutions = DatabaseTableModel
300: .getSubstitutions(assemblyId);
301: substitutions.put(":community_id", communityId);
302: substitutions.put(":entity_id", entityId);
303: doQuery(DELETE_ENTITY_INFO_QUERY, substitutions);
304: }
305:
306: public static void deleteCommunityAttribute(String communityId,
307: String attributeId, String attributeValue, String assemblyId) {
308: Map substitutions = DatabaseTableModel
309: .getSubstitutions(assemblyId);
310: substitutions.put(":community_id", communityId);
311: substitutions.put(":attribute_id", attributeId);
312: substitutions.put(":attribute_value", attributeValue);
313: doQuery(DELETE_COMMUNITY_ATTRIBUTE_QUERY, substitutions);
314: }
315:
316: public static void deleteEntityAttribute(String communityId,
317: String entityId, String attributeId, String attributeValue,
318: String assemblyId) {
319: Map substitutions = DatabaseTableModel
320: .getSubstitutions(assemblyId);
321: substitutions.put(":community_id", communityId);
322: substitutions.put(":entity_id", entityId);
323: substitutions.put(":attribute_id", attributeId);
324: substitutions.put(":attribute_value", attributeValue);
325: doQuery(DELETE_ENTITY_ATTRIBUTE_QUERY, substitutions);
326: }
327: }
|