001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.etl.project.anttasks;
042:
043: import org.netbeans.modules.masterindex.plugin.TargetDBSchemaGenerator;
044: import java.io.BufferedWriter;
045: import java.io.File;
046: import java.io.FileWriter;
047: import java.io.IOException;
048: import java.util.ArrayList;
049: import javax.swing.JFrame;
050: import javax.xml.parsers.DocumentBuilder;
051: import javax.xml.parsers.DocumentBuilderFactory;
052: import javax.xml.parsers.ParserConfigurationException;
053: import org.w3c.dom.Element;
054: import org.w3c.dom.Node;
055: import org.w3c.dom.NodeList;
056: import org.xml.sax.SAXException;
057: import org.apache.tools.ant.Task;
058: import net.java.hulp.i18n.Logger;
059: import org.netbeans.modules.etl.logger.Localizer;
060: import org.netbeans.modules.etl.logger.LogUtil;
061:
062: /**
063: *
064: * @author Manish
065: */
066: public class MasterIndexDesignTime extends Task {
067:
068: private static String USER_DIR = System.getProperty("user.dir");
069: private static String fs = System.getProperty("file.separator");
070: private static String CONFIG_DIR = USER_DIR + fs + "src" + fs
071: + "com" + fs + "stc" + fs + "eindex" + fs + "config";
072: private static String OBJECTDEF = "object.xml";
073: private static String QUERY_FILE_PATH = CONFIG_DIR + fs
074: + "query.txt";
075: private static String QNAME_PREFXI = "Enterprise.SystemObject";
076: Element docroot = null;
077: private String parent = null;
078: private String[] children = null;
079: // Structures
080: ArrayList selectFields = new ArrayList();
081: String parentPrimaryKey = null;
082: ArrayList childrenForeignKey = new ArrayList();
083: ArrayList orderByFields = new ArrayList();
084: private static transient final Logger mLogger = LogUtil
085: .getLogger(MasterIndexDesignTime.class.getName());
086: private static transient final Localizer mLoc = Localizer.get();
087:
088: /** Creates a new instance of Main */
089: public MasterIndexDesignTime() {
090: }
091:
092: private void parseObjectDef() {
093:
094: try {
095: DocumentBuilder root = DocumentBuilderFactory.newInstance()
096: .newDocumentBuilder();
097: docroot = root.parse(CONFIG_DIR + fs + OBJECTDEF)
098: .getDocumentElement();
099: } catch (SAXException ex) {
100: mLogger.infoNoloc(mLoc.t("SAX Exception : " + CONFIG_DIR
101: + fs + OBJECTDEF));
102: } catch (IOException ex) {
103: mLogger.infoNoloc(mLoc.t("Cannot I/O document : "
104: + CONFIG_DIR + fs + OBJECTDEF));
105: } catch (ParserConfigurationException ex) {
106: mLogger.infoNoloc(mLoc.t("Cannot Parse document : "
107: + CONFIG_DIR + fs + OBJECTDEF));
108: }
109:
110: // Parse Relations
111: parseRelations();
112:
113: // Find nodes
114: NodeList nl1 = docroot.getElementsByTagName("nodes");
115: for (int i = 0; i < nl1.getLength(); i++) {
116: Element nodes_element = (Element) nl1.item(i);
117:
118: NodeList taglist = nodes_element
119: .getElementsByTagName("tag");
120: String tag_value = taglist.item(0).getTextContent();
121:
122: //Get the qualified names for the fields
123: String qualname = getQualifiedName(tag_value);
124: //Find Fileds
125: NodeList fieldlist = nodes_element
126: .getElementsByTagName("fields");
127: for (int j = 0; j < fieldlist.getLength(); j++) {
128: String filed_name = ((Element) fieldlist.item(j))
129: .getElementsByTagName("field-name").item(0)
130: .getTextContent();
131: this .selectFields.add(qualname + "." + filed_name);
132: }
133: }
134: //System.out.println("Fields are :: " + this.selectFields.toString());
135: }
136:
137: private String getQualifiedName(String tag_value) {
138: String ret = null;
139: if (tag_value.equals(parent)) {
140: // Its a parent
141: ret = QNAME_PREFXI + "." + this .parent;
142: } else {
143: // Its a child
144: ret = QNAME_PREFXI + "." + this .parent + "." + tag_value;
145: }
146:
147: return ret;
148: }
149:
150: private void parseRelations() {
151: String qualifiedname = null;
152:
153: // Read the relationssips tag and create the qualified name for this node
154: NodeList relations = docroot
155: .getElementsByTagName("relationships");
156: // Find Parent
157: this .parent = ((Element) relations.item(0))
158: .getElementsByTagName("name").item(0).getTextContent();
159:
160: // Find Children
161: NodeList childrenList = ((Element) relations.item(0))
162: .getElementsByTagName("children");
163: children = new String[childrenList.getLength()]; //Set the children Array
164: for (int i = 0; i < childrenList.getLength(); i++) {
165: Node child = childrenList.item(i);
166: children[i] = child.getTextContent();
167: }
168: }
169:
170: private void createOrderBy() {
171: this .orderByFields.add(QNAME_PREFXI + "." + "Person.PersonId");
172: }
173:
174: private void createConditions() {
175: this .parentPrimaryKey = QNAME_PREFXI + "." + "Person.PersonId";
176: this .childrenForeignKey.add(QNAME_PREFXI + "."
177: + "Person.Address.PersonId");
178: this .childrenForeignKey.add(QNAME_PREFXI + "."
179: + "Person.Phone.PersonId");
180: }
181:
182: private void queryWriter() {
183: try {
184: StringBuffer sbuf = new StringBuffer();
185: File queryFile = new File(QUERY_FILE_PATH);
186: BufferedWriter out = new BufferedWriter(new FileWriter(
187: queryFile));
188: // Write Selects
189: for (int i = 0; i < this .selectFields.size(); i++) {
190: sbuf.append("SELECTFIELD" + (i + 1) + "="
191: + this .selectFields.get(i) + "\n");
192: }
193: // Write conditions
194: for (int i = 0; i < this .childrenForeignKey.size(); i++) {
195: sbuf.append("CONDITION" + (i + 1) + "="
196: + this .parentPrimaryKey + "="
197: + this .childrenForeignKey.get(i) + "\n");
198: }
199: //Write Order By
200: sbuf.append("ORDERBYFIELD=" + this .parentPrimaryKey);
201:
202: //Write to File
203: out.write(sbuf.toString());
204: out.close();
205: } catch (Exception ex) {
206: ex.printStackTrace();
207: }
208: }
209:
210: private static String objDefn = null;
211:
212: public static String getobjDefnLocn() {
213: return objDefn;
214: }
215:
216: public void execute() {
217: mLogger
218: .infoNoloc(mLoc
219: .t("eTLeView Design Time - Query Builder [START] ...\n"));
220: ChooseLocationDialog dialog = new ChooseLocationDialog(
221: new JFrame(), true);
222: dialog.setVisible(true);
223: if ((dialog.getDBLocation()) != null
224: && (dialog.getDBName()) != null
225: && (dialog.getObjectDefinition()) != null) {
226: TargetDBSchemaGenerator dbgen = TargetDBSchemaGenerator
227: .getTargetDBSchemaGenerator();
228: objDefn = dialog.getObjectDefinition();
229: boolean fileIsValid = dbgen.setEViewConfigFilePath(dialog
230: .getObjectDefinition(), "object.xml");//("C:/temp/eviewconfig", "objectdef.xml");
231: if (fileIsValid) {
232: File file = new File(dialog.getDBLocation());
233: if (!file.exists()) {
234: file.mkdirs();
235: }
236: dbgen.createTargetDB(dialog.getDBLocation(), dialog
237: .getDBName());//C:\temp\AAADB
238: }
239: System.out
240: .println("\neTLeView Design Time - Query Builder [END].");
241: mLogger
242: .infoNoloc(mLoc
243: .t("\neTLeView Design Time - Query Builder [END]."));
244: } else {
245: mLogger
246: .infoNoloc(mLoc
247: .t("The dialog is cancelled or any one of the fields are empty"));
248: }
249: }
250:
251: /**
252: * @param args the command line arguments
253: */
254: public static void main(String[] args) {
255: MasterIndexDesignTime main = new MasterIndexDesignTime();
256: main.execute();
257: }
258: }
|