001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 Mobile Intelligence Corp
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.core.db;
027:
028: import org.cougaar.tools.csmart.ui.viewer.CSMART;
029: import org.cougaar.tools.csmart.util.XMLUtils;
030: import org.cougaar.util.ConfigFinder;
031: import org.cougaar.util.DBConnectionPool;
032: import org.cougaar.util.DBProperties;
033: import org.cougaar.util.Parameters;
034: import org.cougaar.util.log.Logger;
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Node;
037: import org.w3c.dom.NodeList;
038:
039: import java.io.File;
040: import java.io.IOException;
041: import java.io.RandomAccessFile;
042: import java.sql.Connection;
043: import java.sql.ResultSet;
044: import java.sql.SQLException;
045: import java.sql.Statement;
046: import java.util.ArrayList;
047: import java.util.Collections;
048: import java.util.Enumeration;
049: import java.util.HashMap;
050: import java.util.Hashtable;
051: import java.util.List;
052: import java.util.Map;
053:
054: /**
055: * Utility methods for importing/exporting community definitions in
056: * Configuration database.
057: */
058: public class CommunityDbUtils {
059:
060: private static final String selectQFILE = "community.q";
061: private static final String csmartQFILE = "CSMART.q";
062: private static Logger log;
063:
064: static {
065: log = CSMART
066: .createLogger("org.cougaar.tools.csmart.core.db.CommunityDbUtils");
067: }
068:
069: /**
070: * Get assemblyID of community associated with specified experiment
071: * @param experimentName Name of Experiment
072: * @return Assembly id of community associated with
073: * named experiment
074: */
075: public static String getAssemblyID(String experimentName) {
076: Map substitutions = new HashMap();
077: String result = null;
078: substitutions.put(":experimentName", experimentName);
079: Connection conn = null;
080: try {
081: conn = DBUtils.getConnection(selectQFILE);
082: Statement st = conn.createStatement();
083: String query = DBUtils.getQuery("queryAssemblyID",
084: substitutions, selectQFILE);
085: ResultSet rs = st.executeQuery(query);
086: while (rs.next())
087: result = rs.getString(1);
088: rs.close();
089: st.close();
090: } catch (Exception e) {
091: log.error(e.getMessage(), e.fillInStackTrace());
092: } finally {
093: if (conn != null) {
094: try {
095: conn.close();
096: } catch (SQLException e) {
097: }
098: }
099: }
100: return result;
101: }
102:
103: /**
104: * Takes a File handle on the XML file to import, the Assembly ID to which
105: * this data should be merged. Duplicate rows are ignored.
106: * The method use .q file to directly update the database.
107: * @param xmlFile Name of xml file containing community descriptions
108: * @param assemblyID Assembly ID of community descriptions to export
109: * @return True if operation was successful
110: **/
111: public static boolean importCommunityXML(File xmlFile,
112: String assemblyID) {
113: if (log.isDebugEnabled()) {
114: log.debug("importcommXML with file " + xmlFile
115: + " and ASB " + assemblyID);
116: }
117: XMLUtils xml = new XMLUtils();
118: Document doc;
119: try {
120: doc = xml.loadXMLFile(xmlFile);
121: } catch (Exception e) {
122: log.error("Error when parse xml file: " + xmlFile);
123: return false;
124: }
125:
126: // loadXMLFile returns null on error
127: if (doc == null) {
128: return false;
129: }
130:
131: Hashtable calist = new Hashtable(); //table saves communities and their attributes
132: Hashtable celist = new Hashtable(); //table saves communities and their entities
133: NodeList communities = doc.getElementsByTagName("Community");
134:
135: for (int i = 0; i < communities.getLength(); i++) {
136: Node community = communities.item(i);
137: String communityName = community.getAttributes()
138: .getNamedItem("Name").getNodeValue();
139: List attrs = new ArrayList();
140: Hashtable entities = new Hashtable();
141: NodeList children = community.getChildNodes();
142: for (int j = 0; j < children.getLength(); j++) {
143: Node child = children.item(j);
144: if (child.getLocalName() == null)
145: continue;
146: if (child.getLocalName().equals("Attribute")) //attribute of community
147: {
148: Attribute attr = new Attribute(child
149: .getAttributes().getNamedItem("ID")
150: .getNodeValue(), child.getAttributes()
151: .getNamedItem("Value").getNodeValue());
152: attrs.add(attr);
153: }
154:
155: if (child.getLocalName().equals("Entity")) {
156: String entityName = child.getAttributes()
157: .getNamedItem("Name").getNodeValue();
158: List eattrs = new ArrayList();
159: NodeList entitychildren = child.getChildNodes();
160: for (int m = 0; m < entitychildren.getLength(); m++) {
161: Node entitychild = entitychildren.item(m);
162: if (entitychild.getLocalName() == null)
163: continue;
164: if (entitychild.getLocalName().equals(
165: "Attribute")) {
166: Attribute attr = new Attribute(entitychild
167: .getAttributes().getNamedItem("ID")
168: .getNodeValue(), entitychild
169: .getAttributes().getNamedItem(
170: "Value").getNodeValue());
171: eattrs.add(attr);
172: }
173: }
174: entities.put(entityName, eattrs);
175: }
176: }
177:
178: if (attrs.size() > 0)
179: calist.put(communityName, attrs);
180: if (entities.size() > 0)
181: celist.put(communityName, entities);
182: }
183:
184: if (log.isDebugEnabled()) {
185: log.debug("About to importCommAtt");
186: }
187:
188: boolean flag1 = importCommunityAttribute(calist, assemblyID);
189: boolean flag2 = importCommunityEntity(celist, assemblyID);
190: return flag1 && flag2;
191: }
192:
193: /**
194: * Takes the name of the XML File to create and write. Expect this "name" to include
195: * full path information. It will usually be someplace under CIP/results.
196: * Also takes the assemblyID in the database from which to retrieve community information.
197: * @param xmlFileFullName Path to xml file to create
198: * @param assemblyID Assembly ID of community descriptions to export
199: * @return True if operation was successful
200: **/
201: public static boolean dumpCommunityXML(String xmlFileFullName,
202: String assemblyID) {
203: if (log.isDebugEnabled()) {
204: log.debug("dumpCommXML with filename: " + xmlFileFullName
205: + " and ASB " + assemblyID);
206: }
207:
208: File file = new File(xmlFileFullName);
209: if (file == null) {
210: if (log.isErrorEnabled())
211: log.error("dumpCommXML: unable to get file "
212: + xmlFileFullName);
213: return false;
214: }
215:
216: try {
217: if (file.exists()) {
218: file.delete();
219: file.createNewFile();
220: }
221: } catch (IOException e) {
222: log.error("dumpCommXML: try to overwrite the file "
223: + xmlFileFullName, e);
224: return false;
225: }
226:
227: RandomAccessFile rfile = null;
228: try {
229: rfile = new RandomAccessFile(file, "rw");
230: } catch (IOException e) {
231: log.error("Invalid file " + xmlFileFullName, e);
232: return false;
233: }
234:
235: Connection conn = null;
236: try {
237: conn = DBUtils.getConnection(csmartQFILE);
238: } catch (Exception e) {
239: log.error("try to get connection to database.", e
240: .fillInStackTrace());
241: return false;
242: }
243:
244: Map substitutions = new HashMap();
245: String assemblyMatch = DBUtils.getListMatch(assemblyID);
246: if (assemblyMatch != null)
247: substitutions.put(":assembly_match:", assemblyMatch);
248: Hashtable community_attrs = getDataFromCommunityTable(
249: assemblyID, conn, substitutions);
250:
251: Hashtable community_entities = null;
252: if (community_attrs.size() == 0) {
253: // No communities for this Assembly
254: try {
255: if (conn != null)
256: conn.close();
257: } catch (SQLException e) {
258: log.error("try to close connection to database", e
259: .fillInStackTrace());
260: }
261: // Don't bother looking up the entities
262: community_entities = new Hashtable();
263: } else {
264: community_entities = getDataFromEntityTable(assemblyID,
265: conn, substitutions);
266: }
267:
268: try {
269: if (conn != null)
270: conn.close();
271: } catch (SQLException e) {
272: log.error("try to close connection to database", e
273: .fillInStackTrace());
274: }
275:
276: boolean res = writeXmlFile(rfile, community_attrs,
277: community_entities);
278:
279: // Must close the file
280: try {
281: rfile.close();
282: } catch (IOException ioe) {
283: }
284:
285: return res;
286: }
287:
288: /**
289: * Save all community information from table community_attribute with specified assemblyID in a hashtable.
290: * @param assemblyID search community for this assemblyID
291: * @param conn connection to the database
292: * @param substitutions the map contains arguments need to fill in the query
293: * @return a collection saves all data from table community_attribute
294: */
295: private static Hashtable getDataFromCommunityTable(
296: String assemblyID, Connection conn, Map substitutions) {
297: Hashtable communities = new Hashtable();
298: Statement st = null;
299: try {
300: st = conn.createStatement();
301: List communityNames = getAllCommunities(st, substitutions);
302:
303: if (communityNames.size() == 0)
304: return new Hashtable();
305:
306: for (int i = 0; i < communityNames.size(); i++) {
307: List attrs = new ArrayList(); //save all attributes of one community
308: String name = (String) communityNames.get(i);
309: substitutions.put(":community_id", name);
310: String query = DBUtils.getQuery("queryCommunityInfo",
311: substitutions, csmartQFILE);
312: ResultSet rs = st.executeQuery(query);
313: while (rs.next())
314: attrs.add(new Attribute(rs.getString(2), rs
315: .getString(3)));
316: rs.close();
317: communities.put(name, attrs);
318: }
319: } catch (Exception e) {
320: log
321: .error("try to get data from table community_attribute.");
322: return new Hashtable();
323: } finally {
324: try {
325: if (st != null)
326: st.close();
327: } catch (Exception e) {
328: }
329: }
330: return communities;
331: }
332:
333: /**
334: * Save all entity information with given assemblyID from table community_entity_attribute
335: * in a hashtable.
336: * @param assemblyID search for community entities with this assemblyID
337: * @param conn connection to the database
338: * @param substitutions the map contains arguments need to fill in the query
339: * @return a collection saves all data from table community_entity_attribute
340: */
341: private static Hashtable getDataFromEntityTable(String assemblyID,
342: Connection conn, Map substitutions) {
343: Hashtable communities = new Hashtable();
344: Statement st = null;
345: try {
346: st = conn.createStatement();
347: List communityNames = getAllCommunities(st, substitutions);
348:
349: for (int i = 0; i < communityNames.size(); i++) {
350: Hashtable entities = new Hashtable(); //save all entities
351: String name = (String) communityNames.get(i);
352: substitutions.put(":community_id", name);
353: List entityNames = getAllEntities(name, st,
354: substitutions); //all entity names
355:
356: for (int j = 0; j < entityNames.size(); j++) {
357: String entityName = (String) entityNames.get(j);
358: substitutions.put(":entity_id", entityName);
359: List attrs = new ArrayList(); //save attritures of this entity
360: String query = DBUtils.getQuery("queryEntityInfo",
361: substitutions, csmartQFILE);
362: ResultSet rs = st.executeQuery(query);
363: while (rs.next())
364: attrs.add(new Attribute(rs.getString(2), rs
365: .getString(3)));
366: rs.close();
367: entities.put(entityName, attrs);
368: }
369: communities.put(name, entities);
370: }
371: } catch (Exception e) {
372: log
373: .error(
374: "try to get data from table community_entity_attribute.",
375: e.fillInStackTrace());
376: } finally {
377: if (st != null) {
378: try {
379: st.close();
380: } catch (SQLException e) {
381: }
382: }
383: }
384: return communities;
385: }
386:
387: /**
388: * Write community information fetched from the database into a xml file.
389: * @param rfile the full path xml file to write
390: * @param community_attrs a hashtable contains all communities and attributes.
391: * @param community_entities a hashtable contains all communities and their entities.
392: * @return a boolean value indicates if the export is succeed.
393: */
394: private static boolean writeXmlFile(RandomAccessFile rfile,
395: Hashtable community_attrs, Hashtable community_entities) {
396: try {
397: rfile.write(version.getBytes());
398: rfile.write(dtd.getBytes());
399: rfile.write("<Communities>\n".getBytes());
400:
401: List communities = sortKeys(community_attrs);
402: for (int i = 0; i < communities.size(); i++) {
403: String communityID = (String) communities.get(i);
404: rfile.write(new String(" <Community Name=\""
405: + communityID + "\" >\n").getBytes());
406: if (community_attrs.containsKey(communityID)) {
407: List attrs = (List) community_attrs
408: .get(communityID);
409: if (!writeAttributes(rfile, attrs, " "))
410: return false;
411: }
412:
413: if (community_entities.containsKey(communityID)) {
414: Hashtable entities = (Hashtable) community_entities
415: .get(communityID);
416: List entityNames = sortKeys(entities);
417: for (int j = 0; j < entityNames.size(); j++) {
418: String entityID = (String) entityNames.get(j);
419: List entity_attrs = (List) entities
420: .get(entityID);
421: rfile.write(new String(" <Entity Name=\""
422: + entityID + "\" >\n").getBytes());
423: if (!writeAttributes(rfile, entity_attrs,
424: " "))
425: return false;
426: rfile.write(" </Entity>\n".getBytes());
427: }
428: }
429: rfile.write(" </Community>\n".getBytes());
430: } // end of loop over communities
431:
432: rfile.write("</Communities>\n".getBytes());
433: return true;
434: } catch (IOException e) {
435: log.error("try to dump to xml file.", e.fillInStackTrace());
436: return false;
437: }
438: }
439:
440: /**
441: * Write attributes of one community or entity into a file.
442: * @param file the export xml file
443: * @param attrs a list of attributes
444: * @param space
445: * @return a boolean value indicates if the processing is succeed
446: */
447: private static boolean writeAttributes(RandomAccessFile file,
448: List attrs, String space) {
449: try {
450: for (int i = 0; i < attrs.size(); i++) //write attributes of the community
451: {
452: Attribute attr = (Attribute) attrs.get(i);
453: file.write(new String(space + "<Attribute ID=\""
454: + attr.name + "\" Value=\"" + attr.value
455: + "\" />\n").getBytes());
456: }
457: } catch (IOException e) {
458: log.error("try to dump to xml file.", e.fillInStackTrace());
459: return false;
460: }
461: return true;
462: }
463:
464: /**
465: * Sort keys of one hashtable. This method is called when writing data into a xml file,
466: * both the community names and entity names are sorted.
467: * @param a the hashtable
468: * @return a sorted list contains all keys of hashtable.
469: */
470: private static List sortKeys(Hashtable a) {
471: List keys = new ArrayList();
472: for (Enumeration enums = a.keys(); enums.hasMoreElements();)
473: keys.add((String) enums.nextElement());
474: Collections.sort(keys);
475: return keys;
476: }
477:
478: /**
479: * Get names of all communities of specified assemblyIDs.
480: * @param st statement generated by connection to the database
481: * @param substitutions the map contains arguments need to fill in the query
482: * @return a list saves names of all fetched communities.
483: */
484: private static List getAllCommunities(Statement st,
485: Map substitutions) {
486: List communityNames = new ArrayList(); //save all community names
487: ResultSet rs = null;
488: try {
489: String query = DBUtils.getQuery("queryMyCommunities",
490: substitutions, csmartQFILE);
491: rs = st.executeQuery(query);
492: while (rs.next())
493: communityNames.add(rs.getString(1));
494: rs.close();
495: } catch (Exception e) {
496: log
497: .error("try to get all communities from table community_attribute");
498: } finally {
499: try {
500: rs.close();
501: } catch (SQLException e) {
502: }
503: }
504:
505: if (communityNames.size() == 0) {
506: if (log.isDebugEnabled())
507: log.debug("No communities for assembly");
508: }
509: return communityNames;
510: }
511:
512: /**
513: * Get names of all entities of given assemblyID and communityID.
514: * @param communityName the community need be searched to fetch entities
515: * @param st statement generated by the conneciton to database
516: * @param substitutions the map contains arguments need to fill in the query
517: * @return a list of names of all entities of given community and assemblyID
518: */
519: private static List getAllEntities(String communityName,
520: Statement st, Map substitutions) {
521: List entityNames = new ArrayList(); //all entity names
522: ResultSet rs = null;
523: try {
524: String query = DBUtils.getQuery("queryEntities",
525: substitutions, csmartQFILE);
526: rs = st.executeQuery(query);
527: while (rs.next())
528: //get all entities of this community
529: entityNames.add(rs.getString(1));
530: } catch (Exception e) {
531: log.error("try to get all entities of community: "
532: + communityName, e.fillInStackTrace());
533: //e.printStackTrace();
534: } finally {
535: try {
536: if (rs != null)
537: rs.close();
538: } catch (SQLException e) {
539: }
540: }
541: return entityNames;
542: }
543:
544: /**
545: * Import communities and their attributes into table community_attribute for specified assemblyID.
546: * @param communities a hashtable contains communities and their attributes fetched from xml file
547: * @param assemblyID save these communities for this assemblyID
548: * @return a boolean value indicates if the processing is succeed
549: */
550: private static boolean importCommunityAttribute(
551: Hashtable communities, String assemblyID) {
552: Map substitutions = new HashMap();
553: Connection conn = null;
554: boolean ret = true;
555: try {
556: conn = DBUtils.getConnection(selectQFILE);
557: Statement st = conn.createStatement();
558: for (Enumeration enums = communities.keys(); enums
559: .hasMoreElements();) {
560: String name = (String) enums.nextElement();
561: List attrs = (List) communities.get(name);
562: for (int i = 0; i < attrs.size(); i++) {
563: Attribute pair = (Attribute) attrs.get(i);
564: substitutions.put(":assembly_id:", assemblyID);
565: substitutions.put(":community_id", name);
566: substitutions.put("CommunityType", pair.name);
567: substitutions.put(":community_type", pair.value);
568: String query = DBUtils.getQuery(
569: "queryCommunityInfo", substitutions,
570: selectQFILE);
571: ResultSet rs = st.executeQuery(query);
572: if (rs.next()) {
573: //it's a duplicate row, ignore it
574: if (rs.getString(1).equals(assemblyID)) {
575: rs.close();
576: continue;
577: }
578: }
579: rs.close();
580: query = DBUtils.getQuery(
581: "queryInsertCommunityInfo", substitutions,
582: csmartQFILE);
583: st.executeUpdate(query);
584: }
585: }
586: st.close();
587: } catch (Exception e) {
588: log.error("try to import community attributes.", e
589: .fillInStackTrace());
590: ret = false;
591: } finally {
592: if (conn != null) {
593: try {
594: conn.close();
595: } catch (SQLException e) {
596: }
597: }
598: }
599: return ret;
600: }
601:
602: /**
603: * Import entities and their attributes into table community_entity_attribute for specified assemblyID.
604: * @param communities a hashtable saves data of communities and entities from xml file.
605: * @param assemblyID save these entities with this assemblyID
606: * @return a boolean value indicates if the processing is succeed
607: */
608: private static boolean importCommunityEntity(Hashtable communities,
609: String assemblyID) {
610: Map substitutions = new HashMap();
611: Connection conn = null;
612: boolean ret = true;
613: try {
614: conn = DBUtils.getConnection(selectQFILE);
615: Statement st = conn.createStatement();
616: for (Enumeration enums = communities.keys(); enums
617: .hasMoreElements();) {
618: String name = (String) enums.nextElement();
619: Hashtable entities = (Hashtable) communities.get(name);
620: for (Enumeration en = entities.keys(); en
621: .hasMoreElements();) {
622: String ename = (String) en.nextElement();
623: List attrs = (List) entities.get(ename);
624: for (int i = 0; i < attrs.size(); i++) {
625: Attribute pair = (Attribute) attrs.get(i);
626: substitutions.put(":assembly_id:", assemblyID);
627: substitutions.put(":community_id", name);
628: substitutions.put(":entity_id", ename);
629: substitutions.put(":attribute_id", pair.name);
630: substitutions.put(":attribute_value",
631: pair.value);
632: String query = DBUtils.getQuery(
633: "queryEntityInfo", substitutions,
634: selectQFILE);
635: ResultSet rs = st.executeQuery(query);
636: if (rs.next())
637: if (rs.getString(1).equals(assemblyID)) //it's a duplicate row, ignore it
638: {
639: rs.close();
640: continue;
641: }
642: rs.close();
643: query = DBUtils.getQuery(
644: "queryInsertEntityInfo", substitutions,
645: csmartQFILE);
646: st.executeUpdate(query);
647: }
648: }
649: }
650: st.close();
651: } catch (Exception e) {
652: log.error("try to import community entities.", e
653: .fillInStackTrace());
654: ret = false;
655: } finally {
656: if (conn != null) {
657: try {
658: conn.close();
659: } catch (SQLException e) {
660: }
661: }
662: }
663: return ret;
664: }
665:
666: /**
667: * Get assemblyID from the argument of command line. If the argument is assembly=xxx,
668: * then assemblyID is xxx. if argument is experiment=xxx, then need to fetch the
669: * assemblyID of this experiment.
670: * @param args auguments fetched from the command line.
671: * @return the assemblyID.
672: */
673: private static String getAssemblyIDFromArgument(String args) {
674: String assemblyID = "";
675: if (args.substring(0, args.indexOf("=")).equals("assembly"))
676: assemblyID = args.substring(args.indexOf("=") + 1, args
677: .length());
678: else if (args.substring(0, args.indexOf("=")).equals(
679: "experiment"))
680: assemblyID = getAssemblyID(args.substring(
681: args.indexOf("=") + 1, args.length()));
682: else {
683: log.error("Invalid arguments. Type 'help' to get help.");
684: System.exit(0);
685: }
686: return assemblyID;
687: }
688:
689: /**
690: * Clear all records with specified assemblyID from table community_attribute and community_entity_attribute.
691: * This method is used when command line has argument db=replace.
692: * @param assemblyID
693: */
694: private static void clearRecordsInCommunity(String assemblyID) {
695: Map substitutions = new HashMap();
696: String assemblyMatch = DBUtils.getListMatch(assemblyID);
697: if (assemblyMatch != null)
698: substitutions.put(":assembly_match:", assemblyMatch);
699: Connection conn = null;
700: try {
701: conn = DBUtils.getConnection(selectQFILE);
702: Statement st = conn.createStatement();
703: substitutions.put(":assembly_id:", assemblyID);
704: List communities = new ArrayList();
705: String query = DBUtils.getQuery("queryMyCommunities",
706: substitutions, csmartQFILE);
707: ResultSet rs = st.executeQuery(query);
708: while (rs.next())
709: communities.add(rs.getString(1));
710: rs.close();
711: for (int i = 0; i < communities.size(); i++) {
712: String name = (String) communities.get(i);
713: substitutions.put(":community_id", name);
714: query = DBUtils.getQuery("queryDeleteCommunityInfo",
715: substitutions, csmartQFILE);
716: st.executeUpdate(query);
717: List entities = getAllEntities(name, st, substitutions);
718: for (int j = 0; j < entities.size(); j++) {
719: substitutions.put(":entity_id", (String) entities
720: .get(j));
721: query = DBUtils.getQuery("queryDeleteEntityInfo",
722: substitutions, csmartQFILE);
723: st.executeUpdate(query);
724: }
725: }
726: st.close();
727: } catch (Exception e) {
728: log.error("try to clear records in community tables. ", e
729: .fillInStackTrace());
730: } finally {
731: if (conn != null) {
732: try {
733: conn.close();
734: } catch (SQLException e) {
735: }
736: }
737: }
738: }
739:
740: public static void main(String[] args) {
741: if (args.length == 0) {
742: System.out.println("No parameters. Type '-h' to get help.");
743: System.exit(0);
744: } else if (args[0].equals("-h")) {
745: System.out
746: .println(" help display this help and exit\n"
747: + " export file=XmlFileName assembly=assemblyID create an xml file from community associated with assemblyID\n"
748: + " export file=XmlFileName experiment=experimentName create an xml file from community associated with experiment\n"
749: + " import file=XmlFileName assembly=assemblyID db='merge'or'replace'\n"
750: + " insert the community defined in the xml file into database with given assemblyID\n"
751: + " import file=XmlFileName experiment=experimentName db='merge'or'replace'\n"
752: + " insert the community defined in the xml file into database with given assemblyID\n");
753: System.exit(0);
754: } else if (args[0].equals("import")) {
755: if (args.length != 4) {
756: System.err
757: .println("Invalid arguments. Type 'help' to get help.");
758: System.exit(0);
759: }
760:
761: File xmlFile = ConfigFinder.getInstance().locateFile(
762: args[1].substring(args[1].indexOf("=") + 1, args[1]
763: .length()));
764: String assemblyID = getAssemblyIDFromArgument(args[2]);
765: if (args[3].substring(args[3].indexOf("=") + 1,
766: args[3].length()).equals("replace"))
767: clearRecordsInCommunity(assemblyID);
768:
769: if (xmlFile != null)
770: importCommunityXML(xmlFile, assemblyID);
771: } else if (args[0].equals("export")) {
772: if (args.length != 3) {
773: System.err
774: .println("Invalid arguments. Type 'help' to get help.");
775: System.exit(0);
776: }
777:
778: dumpCommunityXML(args[1].substring(
779: args[1].indexOf("=") + 1, args[1].length()),
780: getAssemblyIDFromArgument(args[2]));
781: } else {
782: System.out
783: .println("Invalid operation. Type 'help' for help.");
784: System.exit(0);
785: }
786: }
787:
788: private static class Attribute {
789: public String name;
790: public String value;
791:
792: public Attribute(String name, String value) {
793: this .name = name;
794: this .value = value;
795: }
796: }
797:
798: private static final String dtd = "<!DOCTYPE Communities [\n"
799: + "<!ELEMENT Communities (Community+)>\n"
800: + "<!ELEMENT Community (Attribute+, Entity*)>\n"
801: + "<!ATTLIST Community Name CDATA #REQUIRED>\n\n"
802: + "<!ELEMENT Entity (Attribute*)>\n"
803: + "<!ATTLIST Entity Name CDATA #REQUIRED>\n\n"
804: + "<!ELEMENT Attribute EMPTY>\n"
805: + "<!ATTLIST Attribute ID CDATA #REQUIRED>\n"
806: + "<!ATTLIST Attribute Value CDATA #REQUIRED>\n" + "]>\n\n";
807: private static final String version = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
808: }
|