You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by nt...@apache.org on 2019/10/18 09:00:15 UTC

[cayenne] 02/03: CAY-2632 Modeler: issue saving cgen path for maven project

This is an automated email from the ASF dual-hosted git repository.

ntimofeev pushed a commit to branch STABLE-4.1
in repository https://gitbox.apache.org/repos/asf/cayenne.git

commit 9253705d6e84ac25b444acfa7660009deb1874a1
Author: Nikita Timofeev <st...@gmail.com>
AuthorDate: Fri Oct 18 11:15:55 2019 +0300

    CAY-2632 Modeler: issue saving cgen path for maven project
    
    (cherry picked from commit 014fbb3d0185b6afa4173fc518133a15bb728671)
---
 RELEASE-NOTES.txt                                  |  1 +
 .../apache/cayenne/gen/xml/CgenSaverDelegate.java  | 57 ++++++++--------
 .../cayenne/gen/xml/CgenSaverDelegateTest.java     | 76 ++++++++++++++++++++++
 .../editor/cgen/CodeGeneratorControllerBase.java   | 66 +++++++++----------
 .../editor/cgen/domain/CgenTabController.java      |  2 +-
 5 files changed, 137 insertions(+), 65 deletions(-)

diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index 762d98c..5a62f15 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -17,6 +17,7 @@ Bug Fixes:
 CAY-2627 Modeler: ObjRelationship creation dialog ignores delete rule
 CAY-2628 dbimport: unable to add several relationships to existing entity
 CAY-2631 Can no longer use "byte[]" as root of scalar SQLSelect
+CAY-2632 Modeler: issue saving cgen path for maven project
 
 ----------------------------------
 Release: 4.1.RC1
diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/xml/CgenSaverDelegate.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/xml/CgenSaverDelegate.java
index 037b566..4f016da 100644
--- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/xml/CgenSaverDelegate.java
+++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/xml/CgenSaverDelegate.java
@@ -33,7 +33,7 @@ import java.nio.file.Paths;
 /**
  * @since 4.1
  */
-public class CgenSaverDelegate extends BaseSaverDelegate{
+public class CgenSaverDelegate extends BaseSaverDelegate {
 
     private DataChannelMetaData metaData;
 
@@ -45,42 +45,43 @@ public class CgenSaverDelegate extends BaseSaverDelegate{
     public Void visitDataMap(DataMap dataMap) {
         CgenConfiguration cgen = metaData.get(dataMap, CgenConfiguration.class);
         if(cgen != null){
-            resolveOutputDir(cgen);
+            resolveOutputDir(getBaseDirectory().getURL(), cgen);
             encoder.nested(cgen, getParentDelegate());
         }
         return null;
     }
 
-    private void resolveOutputDir(CgenConfiguration cgenConfiguration) {
-        if(cgenConfiguration.getRootPath() == null) {
+    static void resolveOutputDir(URL baseURL, CgenConfiguration cgenConfiguration) {
+        if(baseURL == null) {
             return;
         }
-        URL url = getBaseDirectory().getURL();
-        if(url != null) {
-            Path resourcePath;
-            try {
-                resourcePath = Paths.get(url.toURI());
-            } catch (URISyntaxException e) {
-                throw new CayenneRuntimeException("Unable to resolve output path", e);
+
+        Path resourcePath;
+        try {
+            resourcePath = Paths.get(baseURL.toURI());
+        } catch (URISyntaxException e) {
+            throw new CayenneRuntimeException("Unable to resolve output path", e);
+        }
+        if(Files.isRegularFile(resourcePath)) {
+            resourcePath = resourcePath.getParent();
+        }
+        Path oldRoot = cgenConfiguration.getRootPath();
+        if(oldRoot == null) {
+            cgenConfiguration.setRootPath(resourcePath);
+        }
+        Path prevPath = cgenConfiguration.buildPath();
+        if(prevPath != null) {
+            if(prevPath.isAbsolute()) {
+                Path relPath = resourcePath.relativize(prevPath).normalize();
+                cgenConfiguration.setRelPath(relPath);
             }
-            if(Files.isRegularFile(resourcePath)) {
-                resourcePath = resourcePath.getParent();
+            Path templatePath = Paths.get(cgenConfiguration.getTemplate());
+            if(templatePath.isAbsolute()) {
+                cgenConfiguration.setTemplate(resourcePath.relativize(templatePath).normalize().toString());
             }
-            cgenConfiguration.setRootPath(resourcePath);
-            Path prevPath = cgenConfiguration.buildPath();
-            if(prevPath != null) {
-                if(prevPath.isAbsolute()) {
-                    Path relPath = resourcePath.relativize(prevPath).normalize();
-                    cgenConfiguration.setRelPath(relPath);
-                }
-                Path templatePath = Paths.get(cgenConfiguration.getTemplate());
-                if(templatePath.isAbsolute()) {
-                    cgenConfiguration.setTemplate(resourcePath.relativize(templatePath).normalize().toString());
-                }
-                Path superTemplatePath = Paths.get(cgenConfiguration.getSuperTemplate());
-                if(superTemplatePath.isAbsolute()) {
-                    cgenConfiguration.setSuperTemplate(resourcePath.relativize(superTemplatePath).normalize().toString());
-                }
+            Path superTemplatePath = Paths.get(cgenConfiguration.getSuperTemplate());
+            if(superTemplatePath.isAbsolute()) {
+                cgenConfiguration.setSuperTemplate(resourcePath.relativize(superTemplatePath).normalize().toString());
             }
         }
     }
diff --git a/cayenne-cgen/src/test/java/org/apache/cayenne/gen/xml/CgenSaverDelegateTest.java b/cayenne-cgen/src/test/java/org/apache/cayenne/gen/xml/CgenSaverDelegateTest.java
new file mode 100644
index 0000000..9158d73
--- /dev/null
+++ b/cayenne-cgen/src/test/java/org/apache/cayenne/gen/xml/CgenSaverDelegateTest.java
@@ -0,0 +1,76 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+
+package org.apache.cayenne.gen.xml;
+
+import java.net.URL;
+import java.nio.file.Paths;
+
+import org.apache.cayenne.gen.CgenConfiguration;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * @since 4.2
+ */
+public class CgenSaverDelegateTest {
+
+    @Test
+    public void testExistingRootOverride() throws Exception {
+        CgenConfiguration config = new CgenConfiguration(false);
+
+        config.setRootPath(Paths.get("/tmp/src/main/java"));
+        URL baseURL = Paths.get("/tmp/src/main/resources").toUri().toURL();
+
+        CgenSaverDelegate.resolveOutputDir(baseURL, config);
+
+        assertEquals(Paths.get("/tmp/src/main/java"), config.getRootPath());
+        assertEquals(Paths.get("../java"), config.getRelPath());
+    }
+
+    @Test
+    public void testExistingRootAndRelPath() throws Exception {
+        CgenConfiguration config = new CgenConfiguration(false);
+
+        config.setRootPath(Paths.get("/tmp/src/main/java"));
+        config.setRelPath(Paths.get(""));
+
+        URL baseURL = Paths.get("/tmp/src/main/resources").toUri().toURL();
+
+        CgenSaverDelegate.resolveOutputDir(baseURL, config);
+
+        assertEquals(Paths.get("/tmp/src/main/java"), config.getRootPath());
+        assertEquals(Paths.get("../java"), config.getRelPath());
+    }
+
+    @Test
+    public void testEmptyRoot() throws Exception {
+        CgenConfiguration config = new CgenConfiguration(false);
+
+        URL baseURL = Paths.get("/tmp/src/main/resources").toUri().toURL();
+
+        CgenSaverDelegate.resolveOutputDir(baseURL, config);
+
+        assertEquals(Paths.get("/tmp/src/main/resources"), config.getRootPath());
+        assertEquals(Paths.get(""), config.getRelPath());
+    }
+
+
+}
\ No newline at end of file
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/cgen/CodeGeneratorControllerBase.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/cgen/CodeGeneratorControllerBase.java
index ba410ec..b5ea708 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/cgen/CodeGeneratorControllerBase.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/cgen/CodeGeneratorControllerBase.java
@@ -121,51 +121,45 @@ public abstract class CodeGeneratorControllerBase extends CayenneController {
             return cgenConfiguration;
         }
 
-        try {
-            cgenConfiguration = new CgenConfiguration(false);
-            cgenConfiguration.setForce(true);
-            cgenConfiguration.setDataMap(map);
-
-            Path basePath = Paths.get(ModelerUtil.initOutputFolder());
+        cgenConfiguration = new CgenConfiguration(false);
+        cgenConfiguration.setForce(true);
+        cgenConfiguration.setDataMap(map);
 
-            // no destination folder
-            if (basePath == null) {
-                JOptionPane.showMessageDialog(this.getView(), "Select directory for source files.");
-                return null;
-            }
+        Path basePath = Paths.get(ModelerUtil.initOutputFolder());
 
-            // no such folder
-            if (!Files.exists(basePath)) {
+        // no such folder
+        if (!Files.exists(basePath)) {
+            try {
                 Files.createDirectories(basePath);
-            }
-
-            // not a directory
-            if (!Files.isDirectory(basePath)) {
-                JOptionPane.showMessageDialog(this.getView(), basePath + " is not a valid directory.");
+            } catch (IOException e) {
+                JOptionPane.showMessageDialog(getView(), "Can't create directory. Select a different one.");
                 return null;
             }
+        }
 
-            cgenConfiguration.setRootPath(basePath);
-            Preferences preferences = application.getPreferencesNode(GeneralPreferences.class, "");
-            if (preferences != null) {
-                cgenConfiguration.setEncoding(preferences.get(GeneralPreferences.ENCODING_PREFERENCE, null));
-            }
-            addToSelectedEntities(map.getObjEntities()
-                    .stream()
-                    .map(Entity::getName)
-                    .collect(Collectors.toList()));
-            addToSelectedEmbeddables(map.getEmbeddables()
-                    .stream()
-                    .map(Embeddable::getClassName)
-                    .collect(Collectors.toList()));
-           getApplication().getMetaData().add(map, cgenConfiguration);
-           projectController.setDirty(true);
-        } catch (IOException exception) {
-            JOptionPane.showMessageDialog(this.getView(), "Can't create directory. " +
-                    ". Select a different one.");
+        // not a directory
+        if (!Files.isDirectory(basePath)) {
+            JOptionPane.showMessageDialog(this.getView(), basePath + " is not a valid directory.");
             return null;
         }
 
+        cgenConfiguration.setRootPath(basePath);
+        Preferences preferences = application.getPreferencesNode(GeneralPreferences.class, "");
+        if (preferences != null) {
+            cgenConfiguration.setEncoding(preferences.get(GeneralPreferences.ENCODING_PREFERENCE, null));
+        }
+
+        addToSelectedEntities(map.getObjEntities()
+                .stream()
+                .map(Entity::getName)
+                .collect(Collectors.toList()));
+        addToSelectedEmbeddables(map.getEmbeddables()
+                .stream()
+                .map(Embeddable::getClassName)
+                .collect(Collectors.toList()));
+       getApplication().getMetaData().add(map, cgenConfiguration);
+       projectController.setDirty(true);
+
         return cgenConfiguration;
     }
 
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/cgen/domain/CgenTabController.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/cgen/domain/CgenTabController.java
index caa53d2..c255795 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/cgen/domain/CgenTabController.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/cgen/domain/CgenTabController.java
@@ -80,7 +80,6 @@ public class CgenTabController extends GeneratorsTabController<CgenConfiguration
 
     public CgenConfiguration createConfiguration(DataMap dataMap) {
         CgenConfiguration cgenConfiguration = new CgenConfiguration(false);
-        Application.getInstance().getInjector().injectMembers(cgenConfiguration);
         cgenConfiguration.setDataMap(dataMap);
         Path basePath = Paths.get(ModelerUtil.initOutputFolder());
 
@@ -105,6 +104,7 @@ public class CgenTabController extends GeneratorsTabController<CgenConfiguration
         if (preferences != null) {
             cgenConfiguration.setEncoding(preferences.get(GeneralPreferences.ENCODING_PREFERENCE, null));
         }
+
         cgenConfiguration.resolveExcludeEntities();
         cgenConfiguration.resolveExcludeEmbeddables();
         return cgenConfiguration;