001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.etl.project;
020:
021: import java.io.File;
022: import java.io.IOException;
023:
024: import java.sql.Connection;
025: import java.sql.SQLException;
026: import org.openide.filesystems.FileObject;
027: import org.openide.filesystems.FileUtil;
028:
029: import org.netbeans.api.project.Project;
030: import org.netbeans.api.project.ProjectManager;
031: import org.netbeans.spi.project.support.ant.AntProjectHelper;
032: import org.netbeans.spi.project.support.ant.EditableProperties;
033: import org.netbeans.spi.project.support.ant.ProjectGenerator;
034:
035: import org.netbeans.modules.compapp.projects.base.ui.customizer.IcanproProjectProperties;
036: import net.java.hulp.i18n.Logger;
037: import org.netbeans.modules.etl.logger.Localizer;
038: import org.netbeans.modules.etl.logger.LogUtil;
039: import org.netbeans.modules.mashup.tables.wizard.MashupTableWizardIterator;
040: import org.netbeans.modules.sql.framework.common.utils.DBExplorerUtil;
041: import org.openide.DialogDisplayer;
042: import org.openide.NotifyDescriptor;
043: import org.openide.util.Utilities;
044: import org.w3c.dom.Document;
045: import org.w3c.dom.Element;
046:
047: /**
048: * Create a fresh EjbProject from scratch or by importing and exisitng web module
049: * in one of the recognized directory structures.
050: *
051: * @author Pavel Buzek
052: */
053: public class EtlproProjectGenerator {
054:
055: private static transient final Logger mLogger = LogUtil
056: .getLogger(EtlproProjectGenerator.class.getName());
057: private static transient final Localizer mLoc = Localizer.get();
058: private static final String nbBundle1 = mLoc
059: .t("PRSR001: collaborations");
060: private static final String nbBundle2 = mLoc.t("PRSR001: conf");
061: private static final String nbBundle3 = mLoc.t("PRSR001: setup");
062: private static final String nbBundle4 = mLoc.t("PRSR001: bpelasa");
063: private static final String nbBundle5 = mLoc.t("PRSR001: build");
064: private static final String nbBundle6 = mLoc.t("PRSR001: data");
065: private static final String nbBundle7 = mLoc
066: .t("PRSR001: databases");
067: private static final String nbBundle8 = mLoc
068: .t("PRSR001: nbproject");
069: private static final String nbBundle9 = mLoc
070: .t("PRSR001: jdbc:axiondb:");
071: //Trimming the initial spaces
072: private static final String DEFAULT_DOC_BASE_FOLDER = Localizer
073: .parse(nbBundle2).trim(); //NOI18N
074: private static final String DEFAULT_SRC_FOLDER = Localizer.parse(
075: nbBundle1).trim(); //NOI18N
076: private static final String DEFAULT_RESOURCE_FOLDER = Localizer
077: .parse(nbBundle3).trim(); //NOI18N
078: private static final String DEFAULT_BPELASA_FOLDER = Localizer
079: .parse(nbBundle4).trim(); //NOI18N
080: private static final String DEFAULT_BUILD_DIR = Localizer.parse(
081: nbBundle5).trim(); //NOI18N
082: private static final String DEFAULT_DATA_DIR = Localizer.parse(
083: nbBundle6).trim(); //NOI18N
084: private static final String DEFAULT_DATABASES_DIR = Localizer
085: .parse(nbBundle7).trim(); //NOI18N
086: private static final String DEFAULT_NBPROJECT_DIR = Localizer
087: .parse(nbBundle8).trim(); //NOI18N
088: private static final String DEFAULT_FLATFILE_JDBC_URL_PREFIX = Localizer
089: .parse(nbBundle9).trim();
090: private static FileObject dbObj = null;
091: private static File databases = null;
092: private static FileObject data = null;
093: public static String PRJ_LOCATION_DIR = "";
094:
095: private EtlproProjectGenerator() {
096: }
097:
098: /**
099: * Create a new empty J2SE project.
100: * @param dir the top-level directory (need not yet exist but if it does it must be empty)
101: * @param name the code name for the project
102: * @return the helper object permitting it to be further customized
103: * @throws IOException in case something went wrong
104: */
105: public static AntProjectHelper createProject(File dir, String name,
106: String j2eeLevel) throws IOException {
107: dir.mkdirs();
108: // XXX clumsy way to refresh, but otherwise it doesn't work for new folders
109: File rootF = dir;
110: PRJ_LOCATION_DIR = rootF.getPath();
111: while (rootF.getParentFile() != null) {
112: rootF = rootF.getParentFile();
113: }
114: FileObject fo = FileUtil.toFileObject(rootF);
115: assert fo != null : "At least disk roots must be mounted! "
116: + rootF;
117: fo.getFileSystem().refresh(false);
118: //fo = FileUtil.toFileObject(dir);
119: fo = FileUtil.toFileObject(new File(dir, ""));
120:
121: // vlv # 113228
122: if (fo == null) {
123: throw new IOException("Can't create " + dir.getName());
124: }
125: assert fo.isFolder() : "Not really a dir: " + dir;
126: assert fo.getChildren().length == 0 : "Dir must have been empty: "
127: + dir;
128: AntProjectHelper h = setupProject(fo, name, j2eeLevel);
129: fo.createFolder(DEFAULT_SRC_FOLDER); // NOI18N
130: data = fo.createFolder(DEFAULT_DATA_DIR); // NOI18N
131:
132: databases = new File(PRJ_LOCATION_DIR + "\\"
133: + DEFAULT_NBPROJECT_DIR + "\\" + "private" + "\\"
134: + DEFAULT_DATABASES_DIR);
135: dbObj = FileUtil.createFolder(databases);
136: //dbObj.lock();
137: MashupTableWizardIterator.setProjectInfo(name,
138: PRJ_LOCATION_DIR, true);
139:
140: String dbName = databases.getPath();
141: createDefaultDatabase(dbName);
142:
143: EditableProperties ep = h
144: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
145: ep
146: .put(IcanproProjectProperties.SOURCE_ROOT,
147: DEFAULT_SRC_FOLDER); //NOI18N
148: ep.setProperty(IcanproProjectProperties.META_INF, "${"
149: + IcanproProjectProperties.SOURCE_ROOT + "}/"
150: + DEFAULT_DOC_BASE_FOLDER); //NOI18N
151: ep.setProperty(IcanproProjectProperties.SRC_DIR, "${"
152: + IcanproProjectProperties.SOURCE_ROOT + "}"); //NOI18N
153: ep.setProperty(IcanproProjectProperties.RESOURCE_DIR,
154: DEFAULT_RESOURCE_FOLDER);
155: h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
156:
157: Project p = ProjectManager.getDefault().findProject(
158: h.getProjectDirectory());
159: ProjectManager.getDefault().saveProject(p);
160:
161: return h;
162: }
163:
164: private static AntProjectHelper setupProject(FileObject dirFO,
165: String name, String j2eeLevel) throws IOException {
166: AntProjectHelper h = ProjectGenerator.createProject(dirFO,
167: EtlproProjectType.TYPE);
168: Element data = h.getPrimaryConfigurationData(true);
169: Document doc = data.getOwnerDocument();
170: Element nameEl = doc.createElementNS(
171: EtlproProjectType.PROJECT_CONFIGURATION_NAMESPACE,
172: "name"); // NOI18N
173: nameEl.appendChild(doc.createTextNode(name));
174: data.appendChild(nameEl);
175: Element minant = doc.createElementNS(
176: EtlproProjectType.PROJECT_CONFIGURATION_NAMESPACE,
177: "minimum-ant-version"); // NOI18N
178: minant.appendChild(doc.createTextNode("1.6")); // NOI18N
179: data.appendChild(minant);
180: h.putPrimaryConfigurationData(data, true);
181:
182: EditableProperties ep = h
183: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
184: // ep.setProperty(IcanproProjectProperties.JAVAC_CLASSPATH, "${libs.j2ee14.classpath}");
185: ep.setProperty(IcanproProjectProperties.DIST_DIR, "dist");
186: ep.setProperty(IcanproProjectProperties.DIST_JAR, "${"
187: + IcanproProjectProperties.DIST_DIR + "}/" + name
188: + ".zip");
189: ep.setProperty(IcanproProjectProperties.J2EE_PLATFORM,
190: j2eeLevel);
191: ep
192: .setProperty(IcanproProjectProperties.JAR_NAME, name
193: + ".jar");
194: ep.setProperty(IcanproProjectProperties.JAR_COMPRESS, "false");
195:
196: // Deployment deployment = Deployment.getDefault ();
197: // String serverInstanceID = deployment.getDefaultServerInstanceID ();
198: // ep.setProperty(IcanproProjectProperties.J2EE_SERVER_TYPE, deployment.getServerID (serverInstanceID));
199: ep.setProperty(IcanproProjectProperties.JAVAC_SOURCE, "1.4");
200: ep.setProperty(IcanproProjectProperties.JAVAC_DEBUG, "true");
201: ep.setProperty(IcanproProjectProperties.JAVAC_DEPRECATION,
202: "false");
203:
204: ep.setProperty(IcanproProjectProperties.JAVAC_TARGET, "1.4");
205:
206: ep.setProperty(IcanproProjectProperties.BUILD_DIR,
207: DEFAULT_BUILD_DIR);
208: ep.setProperty(IcanproProjectProperties.BUILD_GENERATED_DIR,
209: "${" + IcanproProjectProperties.BUILD_DIR
210: + "}/generated");
211: ep.setProperty(IcanproProjectProperties.BUILD_CLASSES_DIR, "${"
212: + IcanproProjectProperties.BUILD_DIR + "}/jar");
213: ep.setProperty(IcanproProjectProperties.BUILD_CLASSES_EXCLUDES,
214: "**/*.java,**/*.form,**/.nbattrs");
215: ep.setProperty(IcanproProjectProperties.DIST_JAVADOC_DIR, "${"
216: + IcanproProjectProperties.DIST_DIR + "}/javadoc");
217: ep.setProperty(IcanproProjectProperties.JAVA_PLATFORM,
218: "default_platform");
219: ep.setProperty(IcanproProjectProperties.DEBUG_CLASSPATH, "${"
220: + IcanproProjectProperties.JAVAC_CLASSPATH + "}:${"
221: + IcanproProjectProperties.BUILD_CLASSES_DIR + "}");
222:
223: //============= Start of IcanPro========================================//
224: ep.setProperty(IcanproProjectProperties.JBI_SETYPE_PREFIX,
225: "sun-etl-engine"); // NOI18N
226: ep.setProperty(IcanproProjectProperties.ASSEMBLY_UNIT_ALIAS,
227: "This Assembly Unit"); // NOI18N
228: ep.setProperty(
229: IcanproProjectProperties.ASSEMBLY_UNIT_DESCRIPTION,
230: "Represents this Assembly Unit"); // NOI18N
231: ep
232: .setProperty(
233: IcanproProjectProperties.APPLICATION_SUB_ASSEMBLY_ALIAS,
234: "This Application Sub-Assembly"); // NOI18N
235: ep
236: .setProperty(
237: IcanproProjectProperties.APPLICATION_SUB_ASSEMBLY_DESCRIPTION,
238: "This represents the Application Sub-Assembly"); // NOI18N
239: ep.setProperty(
240: IcanproProjectProperties.JBI_COMPONENT_CONF_ROOT,
241: "nbproject/private"); // NOI18N
242: ep.setProperty(
243: IcanproProjectProperties.JBI_DEPLOYMENT_CONF_ROOT,
244: "nbproject/deployment"); // NOI18N
245:
246: ep.setProperty(IcanproProjectProperties.BC_DEPLOYMENT_JAR, "${"
247: + IcanproProjectProperties.BUILD_DIR + "}/"
248: + "BCDeployment.jar");
249: ep.setProperty(IcanproProjectProperties.SE_DEPLOYMENT_JAR, "${"
250: + IcanproProjectProperties.BUILD_DIR + "}/"
251: + "SEDeployment.jar");
252: //============= End of IcanPro========================================//
253:
254: h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
255:
256: ep = h.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
257: //ep.setProperty(IcanproProjectProperties.J2EE_SERVER_INSTANCE, serverInstanceID);
258: //============= Start of IcanPro========================================//
259: ep.setProperty(
260: IcanproProjectProperties.JBI_COMPONENT_CONF_FILE,
261: "ComponentInformation.xml"); // NOI18N
262: ep.setProperty(
263: IcanproProjectProperties.JBI_DEPLOYMENT_CONF_FILE,
264: "default.xml"); // NOI18N
265: //============= End of IcanPro========================================//
266:
267: h.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, ep);
268: Project p = ProjectManager.getDefault().findProject(dirFO);
269: ProjectManager.getDefault().saveProject(p);
270: return h;
271: }
272:
273: private static void createDefaultDatabase(String name) {
274: String url = DEFAULT_FLATFILE_JDBC_URL_PREFIX + "Default" + ":"
275: + name + "\\" + "Default";
276: File f = new File(name + "\\" + "Default");
277: char[] ch = name.toCharArray();
278: if (ch == null) {
279: String nbBundle10 = mLoc
280: .t("PRSR001: No Database name specified.");
281: NotifyDescriptor d = new NotifyDescriptor.Message(Localizer
282: .parse(nbBundle10),
283: NotifyDescriptor.INFORMATION_MESSAGE);
284: DialogDisplayer.getDefault().notify(d);
285: } else if (f.exists()) {
286: String nbBundle11 = mLoc.t(
287: "PRSR001: Database {0} already exists.", name);
288: NotifyDescriptor d = new NotifyDescriptor.Message(Localizer
289: .parse(nbBundle11),
290: NotifyDescriptor.INFORMATION_MESSAGE);
291: DialogDisplayer.getDefault().notify(d);
292: } else {
293: Connection conn = null;
294: try {
295: conn = DBExplorerUtil
296: .createConnection(
297: "org.axiondb.jdbc.AxionDriver", url,
298: "sa", "sa");
299: } catch (Exception ex) {
300: String nbBundle12 = mLoc
301: .t("PRSR001: Axion driver could not be loaded.");
302: NotifyDescriptor d = new NotifyDescriptor.Message(
303: Localizer.parse(nbBundle12),
304: NotifyDescriptor.INFORMATION_MESSAGE);
305: DialogDisplayer.getDefault().notify(d);
306: } finally {
307: try {
308: if (conn != null) {
309: conn.createStatement().execute("shutdown");
310: conn.close();
311: }
312: } catch (SQLException ex) {
313: conn = null;
314: }
315: }
316: }
317: }
318:
319: //Need for Migration - Start
320: public static File getDatabasesFolder() {
321: return databases;
322: }
323:
324: public static String getDatabasesFolderPath() {
325: //return databases.getPath();
326: String path = FileUtil.toFile(dbObj).getAbsolutePath();
327: /*if (Utilities.isWindows()) {
328: path = path.replace("\\", "/"); // NOI18N
329: }*/
330: return path;
331: }
332:
333: public static String getDataFolderPath() {
334: //return data.getPath();
335: String path = FileUtil.toFile(data).getAbsolutePath();
336: if (Utilities.isWindows()) {
337: path = path.replace("\\", "/"); // NOI18N
338: }
339: return path;
340: }
341: //Need for Migration - End
342: }
|