[jbosstools-commits] JBoss Tools SVN: r41840 - in trunk/jst/plugins/org.jboss.tools.jst.web.ui: templates/Datasource and 1 other directory.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Fri Jun 8 15:47:47 EDT 2012


Author: scabanovich
Date: 2012-06-08 15:47:47 -0400 (Fri, 08 Jun 2012)
New Revision: 41840

Modified:
   trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/wizards/newfile/NewDSXMLWizard.java
   trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-as7.xml
   trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-default.xml
Log:
JBIDE-11394
https://issues.jboss.org/browse/JBIDE-11394
Implemented inserting content of existing persistence.xml into template.



Modified: trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/wizards/newfile/NewDSXMLWizard.java
===================================================================
--- trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/wizards/newfile/NewDSXMLWizard.java	2012-06-08 19:43:13 UTC (rev 41839)
+++ trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/wizards/newfile/NewDSXMLWizard.java	2012-06-08 19:47:47 UTC (rev 41840)
@@ -99,6 +99,7 @@
 import org.jboss.tools.common.ui.widget.editor.IFieldEditor;
 import org.jboss.tools.common.ui.widget.editor.IFieldEditorFactory;
 import org.jboss.tools.common.ui.widget.editor.ITaggedFieldEditor;
+import org.jboss.tools.common.util.FileUtil;
 import org.jboss.tools.common.zip.UnzipOperation;
 import org.jboss.tools.jst.web.ui.Messages;
 import org.jboss.tools.jst.web.ui.WebUiPlugin;
@@ -526,7 +527,7 @@
 			if("true".equals(registerEditor.getValueAsString())) { //$NON-NLS-1$
 				final IFile persistenceFile = findPersistenceXMLHandle();
 				if(persistenceFile != null) {
-					final InputStream content = getPersistanceXMLContent(persistenceFile);
+					final InputStream content = getPersistanceXMLContent(persistenceFile, newFile);
 					
 					op = new IRunnableWithProgress() {
 						public void run(IProgressMonitor monitor) {
@@ -606,8 +607,13 @@
 			return null;
 		}
 	
-		private InputStream getPersistanceXMLContent(IFile persistenceFile) {
+		private InputStream getPersistanceXMLContent(IFile persistenceFile, IFile newFile) {
 			try {
+				FilterSet contentFilter = new FilterSet();
+				contentFilter.addFilter("content", getOldPersistenceContent(persistenceFile)); //$NON-NLS-1$
+				contentFilter.addFilter("dsSourceLocation", newFile.getFullPath().removeFirstSegments(1).toString()); //$NON-NLS-1$
+				viewFilterSetCollection.addFilterSet(contentFilter);
+				
 				File homePath = DSDataModelProvider.getTemplatesFolder();
 				String templatePath = (NewDSXMLWizardFactory.AS_7_TEMPLATE.equals(templateSelEditor.getValueAsString()))
 					? "/Datasource/persistence-xml-as7.xml" //$NON-NLS-1$
@@ -622,6 +628,8 @@
 				return sr.getInputStream();
 			} catch (IOException e) {
 				WebUiPlugin.getDefault().logError(e);
+			} catch (CoreException e) {
+				WebUiPlugin.getDefault().logError(e);
 			}
 
 			return null;
@@ -629,6 +637,32 @@
 
 	}
 
+	private String getOldPersistenceContent(IFile persistenceFile) throws IOException, CoreException {
+		String content = ""; //$NON-NLS-1$
+		if(!persistenceFile.exists()) {
+			return content;
+		}
+		String start = "<persistence-unit"; //$NON-NLS-1$
+		String end = "</persistence-unit>"; //$NON-NLS-1$
+		String cc = FileUtil.readStream(persistenceFile);
+		int b = cc.indexOf(start);
+		int e = cc.lastIndexOf(end);
+		if(b > 0 && e > b) {
+			while(b > 0 && Character.isWhitespace(cc.charAt(b - 1))) {
+				b--;
+			}
+			content = cc.substring(b, e + end.length());
+		}		
+		int i = content.indexOf("\"primary\""); //$NON-NLS-1$
+		if(i > 0) {
+			int k = 1;
+			while(content.indexOf("\"primary_" + k + "\"") > 0) k++; //$NON-NLS-1$ //$NON-NLS-2$
+			content = content.substring(0, i + 8) + "_" + k + content.substring(i + 8); //$NON-NLS-1$
+		}
+
+		return content;
+	}
+
 	/**
 	 * @return
 	 */
@@ -636,7 +670,7 @@
 		List<String> values = getConnectionProfileNameList();
 		String defaultDs = NewDSXMLWizardFactory.EMPTY_PROFILE; // Use preference, or dialog settings?
 		return values.contains(defaultDs) ? defaultDs
-				: !values.isEmpty() ? values.get(0) : NewDSXMLWizardFactory.EMPTY_PROFILE; //$NON-NLS-1$
+				: !values.isEmpty() ? values.get(0) : NewDSXMLWizardFactory.EMPTY_PROFILE;
 	}
 
 	private static List<String> getConnectionProfileNameList() {

Modified: trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-as7.xml
===================================================================
--- trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-as7.xml	2012-06-08 19:43:13 UTC (rev 41839)
+++ trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-as7.xml	2012-06-08 19:47:47 UTC (rev 41840)
@@ -8,7 +8,7 @@
       <!-- If you are running in a production environment, add a managed 
          data source, this example data source is just for development and testing! -->
       <!-- The datasource is deployed as ${ds-deployment-location}, you
-         can find it in the source at ${ds-source-location} -->
+         can find it in the source at @dsSourceLocation@ -->
       <jta-data-source>java:jboss/datasources/@datasourcename@</jta-data-source>
       <properties>
          <!-- Properties for Hibernate -->
@@ -18,5 +18,5 @@
          <!-- Print's out the SQL statement Hibernate is using to the console, useful for debugging -->
          <property name="hibernate.show_sql" value="false" />
       </properties>
-   </persistence-unit>
+   </persistence-unit>@content@
 </persistence>

Modified: trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-default.xml
===================================================================
--- trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-default.xml	2012-06-08 19:43:13 UTC (rev 41839)
+++ trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-default.xml	2012-06-08 19:47:47 UTC (rev 41840)
@@ -8,7 +8,7 @@
       <!-- If you are running in a production environment, add a managed 
          data source, this example data source is just for development and testing! -->
       <!-- The datasource is deployed as ${ds-deployment-location}, you
-         can find it in the source at ${ds-source-location} -->
+         can find it in the source at @dsSourceLocation@ -->
       <jta-data-source>@projectName at Datasource</jta-data-source>
       <properties>
          <!-- Properties for Hibernate -->
@@ -18,5 +18,5 @@
          <!-- Print's out the SQL statement Hibernate is using to the console, useful for debugging -->
          <property name="hibernate.show_sql" value="false" />
       </properties>
-   </persistence-unit>
+   </persistence-unit>@content@
 </persistence>



More information about the jbosstools-commits mailing list