This is an automated email from the ASF dual-hosted git repository. ntimofeev pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/cayenne.git
The following commit(s) were added to refs/heads/master by this push: new d6376569c Change method to add DataMap to the generated artifacts + minor refactoring in cgen d6376569c is described below commit d6376569c28c07f94e1d772139368b28610d365a Author: Nikita Timofeev <stari...@gmail.com> AuthorDate: Thu Dec 8 12:19:22 2022 +0300 Change method to add DataMap to the generated artifacts + minor refactoring in cgen --- .../apache/cayenne/tools/CayenneGeneratorTask.java | 2 +- .../apache/cayenne/gen/ClassGenerationAction.java | 569 ++++++++++----------- .../cayenne/gen/ClassGeneratorResourceLoader.java | 17 +- .../java/org/apache/cayenne/tools/CgenTask.java | 2 +- .../apache/cayenne/tools/CayenneGeneratorMojo.java | 2 +- .../DataMapArtefactsConfigurator.java | 19 +- .../templateeditor/ArtifactsConfiguratorTest.java | 2 +- 7 files changed, 296 insertions(+), 317 deletions(-) diff --git a/cayenne-ant/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java b/cayenne-ant/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java index 9fd856ed2..be63d2ce2 100644 --- a/cayenne-ant/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java +++ b/cayenne-ant/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java @@ -137,7 +137,7 @@ public class CayenneGeneratorTask extends CayenneTask { } else { generatorAction.addEntities(filterEntityAction.getFilteredEntities(dataMap)); generatorAction.addEmbeddables(filterEmbeddableAction.getFilteredEmbeddables(dataMap)); - generatorAction.addQueries(dataMap.getQueryDescriptors()); + generatorAction.addDataMap(dataMap); } generatorAction.execute(); } diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java index b31048473..07330a825 100644 --- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java +++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java @@ -20,9 +20,9 @@ package org.apache.cayenne.gen; import org.apache.cayenne.CayenneRuntimeException; +import org.apache.cayenne.map.DataMap; import org.apache.cayenne.map.Embeddable; import org.apache.cayenne.map.ObjEntity; -import org.apache.cayenne.map.QueryDescriptor; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; @@ -46,56 +46,55 @@ import java.nio.file.Path; import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.Objects; import java.util.Properties; import java.util.stream.Collectors; public class ClassGenerationAction { - public static final String SUPERCLASS_PREFIX = "_"; - private static final String WILDCARD = "*"; + public static final String SUPERCLASS_PREFIX = "_"; + private static final String WILDCARD = "*"; private static final String CUSTOM_TEMPLATE_REPO = "customTemplateRepo"; - /** - * @since 4.1 - */ - protected CgenConfiguration cgenConfiguration; - protected Logger logger; + /** + * @since 4.1 + */ + protected CgenConfiguration cgenConfiguration; + protected Logger logger; // runtime ivars protected Context context; protected Map<String, Template> templateCache; private ToolsUtilsFactory utilsFactory; - private MetadataUtils metadataUtils; - - /** - Optionally allows user-defined tools besides {@link ImportUtils} for working with velocity templates.<br/> - To use this feature, either set the java system property {@code -Dorg.apache.velocity.tools=tools.properties} - or set the {@code externalToolConfig} property to "tools.properties" in {@code CgenConfiguration}. Then - create the file "tools.properties" in the working directory or in the root of the classpath with content - like this: - <pre> - tools.toolbox = application - tools.application.myTool = com.mycompany.MyTool</pre> - Then the methods in the MyTool class will be available for use in the template like ${myTool.myMethod(arg)} - */ - public ClassGenerationAction(CgenConfiguration cgenConfig) { - this.cgenConfiguration = cgenConfig; - String toolConfigFile = cgenConfig.getExternalToolConfig(); - - if (System.getProperty("org.apache.velocity.tools") != null || toolConfigFile != null) { - ToolManager manager = new ToolManager(true, true); - if (toolConfigFile != null) { - FactoryConfiguration config = ConfigurationUtils.find(toolConfigFile); - manager.getToolboxFactory().configure(config); - } - this.context = manager.createContext(); - } else { - this.context = new VelocityContext(); - } - this.templateCache = new HashMap<>(5); - } + private MetadataUtils metadataUtils; + + /** + * Optionally allows user-defined tools besides {@link ImportUtils} for working with velocity templates.<br/> + * To use this feature, either set the java system property {@code -Dorg.apache.velocity.tools=tools.properties} + * or set the {@code externalToolConfig} property to "tools.properties" in {@code CgenConfiguration}. Then + * create the file "tools.properties" in the working directory or in the root of the classpath with content + * like this: + * <pre> + * tools.toolbox = application + * tools.application.myTool = com.mycompany.MyTool</pre> + * Then the methods in the MyTool class will be available for use in the template like ${myTool.myMethod(arg)} + */ + public ClassGenerationAction(CgenConfiguration cgenConfig) { + this.cgenConfiguration = cgenConfig; + String toolConfigFile = cgenConfig.getExternalToolConfig(); + + if (System.getProperty("org.apache.velocity.tools") != null || toolConfigFile != null) { + ToolManager manager = new ToolManager(true, true); + if (toolConfigFile != null) { + FactoryConfiguration config = ConfigurationUtils.find(toolConfigFile); + manager.getToolboxFactory().configure(config); + } + this.context = manager.createContext(); + } else { + this.context = new VelocityContext(); + } + this.templateCache = new HashMap<>(5); + } /** * VelocityContext initialization method called once per artifact. @@ -134,108 +133,108 @@ public class ClassGenerationAction { context.put(Artifact.CREATE_PK_PROPERTIES, cgenConfiguration.isCreatePKProperties()); } - /** - * VelocityContext initialization method called once per each artifact and - * template type combination. - */ - void resetContextForArtifactTemplate(Artifact artifact) { + /** + * VelocityContext initialization method called once per each artifact and + * template type combination. + */ + void resetContextForArtifactTemplate(Artifact artifact) { ImportUtils importUtils = utilsFactory.createImportUtils(); context.put(Artifact.IMPORT_UTILS_KEY, importUtils); - context.put(Artifact.PROPERTY_UTILS_KEY, utilsFactory.createPropertyUtils(logger, importUtils)); - context.put(Artifact.METADATA_UTILS_KEY, metadataUtils); - artifact.postInitContext(context); - } - - /** - * Adds entities to the internal entity list. - * @param entities collection - * - * @since 4.0 throws exception - */ - public void addEntities(Collection<ObjEntity> entities) { - if (entities != null) { - for (ObjEntity entity : entities) { - cgenConfiguration.addArtifact(new EntityArtifact(entity)); - } - } - } - - public void addEmbeddables(Collection<Embeddable> embeddables) { - if (embeddables != null) { - for (Embeddable embeddable : embeddables) { - cgenConfiguration.addArtifact(new EmbeddableArtifact(embeddable)); - } - } - } - - public void addQueries(Collection<QueryDescriptor> queries) { - if (cgenConfiguration.getArtifactsGenerationMode().equals(ArtifactsGenerationMode.ALL.getLabel())) { - // TODO: andrus 10.12.2010 - why not also check for empty query list?? - // Or create a better API for enabling DataMapArtifact - if (queries != null) { - Artifact artifact = new DataMapArtifact(cgenConfiguration.getDataMap(), queries); - if(!cgenConfiguration.getArtifacts().contains(artifact)) { - cgenConfiguration.addArtifact(artifact); - } - } - } - } - - /** - * @since 4.1 - */ - public void prepareArtifacts() { - cgenConfiguration.getArtifacts().clear(); - addEntities(cgenConfiguration.getEntities().stream() - .map(entity -> cgenConfiguration.getDataMap().getObjEntity(entity)) - .collect(Collectors.toList())); - addEmbeddables(cgenConfiguration.getEmbeddables().stream() - .map(embeddable -> cgenConfiguration.getDataMap().getEmbeddable(embeddable)) - .collect(Collectors.toList())); - addQueries(cgenConfiguration.getDataMap().getQueryDescriptors()); + context.put(Artifact.PROPERTY_UTILS_KEY, utilsFactory.createPropertyUtils(logger, importUtils)); + context.put(Artifact.METADATA_UTILS_KEY, metadataUtils); + artifact.postInitContext(context); } - /** - * Executes class generation once per each artifact. - */ - public void execute() throws Exception { + /** + * Adds entities to the internal entity list. + * + * @param entities collection + * @since 4.0 throws exception + */ + public void addEntities(Collection<ObjEntity> entities) { + if (entities != null) { + for (ObjEntity entity : entities) { + cgenConfiguration.addArtifact(new EntityArtifact(entity)); + } + } + } + + public void addEmbeddables(Collection<Embeddable> embeddables) { + if (embeddables != null) { + for (Embeddable embeddable : embeddables) { + cgenConfiguration.addArtifact(new EmbeddableArtifact(embeddable)); + } + } + } + + /** + * @param dataMap to add to the list of artifacts to generate + * @since 5.0 replaces removed {@code addQueries()} method + */ + public void addDataMap(DataMap dataMap) { + // data map should be used only in ArtifactsGenerationMode.ALL + if (!cgenConfiguration.getArtifactsGenerationMode().equals(ArtifactsGenerationMode.ALL.getLabel())) { + return; + } + + Artifact artifact = new DataMapArtifact(cgenConfiguration.getDataMap(), dataMap.getQueryDescriptors()); + if (!cgenConfiguration.getArtifacts().contains(artifact)) { + cgenConfiguration.addArtifact(artifact); + } + } - validateAttributes(); + /** + * @since 4.1 + */ + public void prepareArtifacts() { + cgenConfiguration.getArtifacts().clear(); + addEntities(cgenConfiguration.getEntities().stream() + .map(entity -> cgenConfiguration.getDataMap().getObjEntity(entity)) + .collect(Collectors.toList())); + addEmbeddables(cgenConfiguration.getEmbeddables().stream() + .map(embeddable -> cgenConfiguration.getDataMap().getEmbeddable(embeddable)) + .collect(Collectors.toList())); + addDataMap(cgenConfiguration.getDataMap()); + } - try { - for (Artifact artifact : cgenConfiguration.getArtifacts()) { - execute(artifact); - } - } finally { - // must reset engine at the end of class generator run to avoid - // memory - // leaks and stale templates - templateCache.clear(); - } - } + /** + * Executes class generation once per each artifact. + */ + public void execute() throws Exception { - /** - * Executes class generation for a single artifact. - */ - protected void execute(Artifact artifact) throws Exception { + validateAttributes(); - resetContextForArtifact(artifact); + try { + for (Artifact artifact : cgenConfiguration.getArtifacts()) { + execute(artifact); + } + } finally { + // must reset engine at the end of class generator run to avoid memory leaks and stale templates + templateCache.clear(); + } + } - ArtifactGenerationMode artifactMode = cgenConfiguration.isMakePairs() ? ArtifactGenerationMode.GENERATION_GAP - : ArtifactGenerationMode.SINGLE_CLASS; + /** + * Executes class generation for a single artifact. + */ + protected void execute(Artifact artifact) throws Exception { - TemplateType[] templateTypes = artifact.getTemplateTypes(artifactMode); - for (TemplateType type : templateTypes) { + resetContextForArtifact(artifact); - try (Writer out = openWriter(type)) { - if (out != null) { + ArtifactGenerationMode artifactMode = cgenConfiguration.isMakePairs() + ? ArtifactGenerationMode.GENERATION_GAP + : ArtifactGenerationMode.SINGLE_CLASS; - resetContextForArtifactTemplate(artifact); - getTemplate(type).merge(context, out); - } - } - } - } + TemplateType[] templateTypes = artifact.getTemplateTypes(artifactMode); + for (TemplateType type : templateTypes) { + try (Writer out = openWriter(type)) { + if (out != null) { + resetContextForArtifactTemplate(artifact); + getTemplate(type).merge(context, out); + } + } + } + } protected Template getTemplate(TemplateType type) { Properties props = new Properties(); @@ -248,198 +247,196 @@ public class ClassGenerationAction { protected void initVelocityProperties(Properties props, TemplateType type) { CgenTemplate template = cgenConfiguration.getTemplateByType(type); if (template.isFile()) { - props.put("resource.loaders", "cayenne"); + props.put(RuntimeConstants.RESOURCE_LOADERS, "cayenne"); props.put("resource.loader.cayenne.class", ClassGeneratorResourceLoader.class.getName()); props.put("resource.loader.cayenne.cache", "false"); - if (cgenConfiguration.getRootPath() != null) { - props.put("resource.loader.cayenne.root", cgenConfiguration.getRootPath().toString()); - } + props.put("resource.loader.cayenne.root", cgenConfiguration.getRootPath()); } else { - props.setProperty(RuntimeConstants.RESOURCE_LOADERS, "string"); - props.setProperty("resource.loader.string.class", StringResourceLoader.class.getName()); - props.setProperty("resource.loader.string.repository.name", CUSTOM_TEMPLATE_REPO); + props.put(RuntimeConstants.RESOURCE_LOADERS, "string"); + props.put("resource.loader.string.class", StringResourceLoader.class.getName()); + props.put("resource.loader.string.repository.name", CUSTOM_TEMPLATE_REPO); putTemplateTextInRepository(template); } } private void putTemplateTextInRepository(CgenTemplate template) { - StringResourceLoader.setRepository(CUSTOM_TEMPLATE_REPO, new StringResourceRepositoryImpl()); - StringResourceRepository repo = StringResourceLoader.getRepository(CUSTOM_TEMPLATE_REPO); + StringResourceRepository repo = new StringResourceRepositoryImpl(); repo.putStringResource(template.getName(), template.getData()); + StringResourceLoader.setRepository(CUSTOM_TEMPLATE_REPO, repo); } - /** - * Validates the state of this class generator. - * Throws CayenneRuntimeException if it is in an inconsistent state. - * Called internally from "execute". - */ - protected void validateAttributes() { - Path dir = cgenConfiguration.buildOutputPath(); - if (dir == null) { - throw new CayenneRuntimeException("Output directory is not set."); - } - if(Files.notExists(dir)) { - try { - Files.createDirectories(dir); - } catch (IOException e) { - throw new CayenneRuntimeException("Can't create output directory '%s'", dir); - } - } - - if (!Files.isDirectory(dir)) { - throw new CayenneRuntimeException("'%s' is not a directory.", dir); - } - - if (!Files.isWritable(dir)) { - throw new CayenneRuntimeException("No write permission for the output directory '%s'", dir); - } - } - - /** - * Opens a Writer to write generated output. Returned Writer is mapped to a - * filesystem file (although subclasses may override that). File location is - * determined from the current state of VelocityContext and the TemplateType - * passed as a parameter. Writer encoding is determined from the value of - * the "encoding" property. - */ - protected Writer openWriter(TemplateType templateType) throws Exception { - - File outFile = (templateType.isSuperclass()) ? fileForSuperclass() : fileForClass(); - if (outFile == null) { - return null; - } - - if (logger != null) { - String label = templateType.isSuperclass() ? "superclass" : "class"; - logger.info("Generating " + label + " file: " + outFile.getCanonicalPath()); - } - - // return writer with specified encoding - FileOutputStream out = new FileOutputStream(outFile); - - return (cgenConfiguration.getEncoding() != null) ? new OutputStreamWriter(out, cgenConfiguration.getEncoding()) : new OutputStreamWriter(out); - } - - /** - * Returns a target file where a generated superclass must be saved. If null - * is returned, class shouldn't be generated. - */ - private File fileForSuperclass() throws Exception { - - String packageName = (String) context.get(Artifact.SUPER_PACKAGE_KEY); - String className = (String) context.get(Artifact.SUPER_CLASS_KEY); - - String filename = StringUtils.getInstance().replaceWildcardInStringWithString(WILDCARD, cgenConfiguration.getOutputPattern(), className); - File dest = new File(mkpath(cgenConfiguration.buildOutputPath().toFile(), packageName), filename); + /** + * Validates the state of this class generator. + * Throws CayenneRuntimeException if it is in an inconsistent state. + * Called internally from "execute". + */ + protected void validateAttributes() { + Path dir = cgenConfiguration.buildOutputPath(); + if (dir == null) { + throw new CayenneRuntimeException("Output directory is not set."); + } + if (Files.notExists(dir)) { + try { + Files.createDirectories(dir); + } catch (IOException e) { + throw new CayenneRuntimeException("Can't create output directory '%s'", dir); + } + } + + if (!Files.isDirectory(dir)) { + throw new CayenneRuntimeException("'%s' is not a directory.", dir); + } + + if (!Files.isWritable(dir)) { + throw new CayenneRuntimeException("No write permission for the output directory '%s'", dir); + } + } + + /** + * Opens a Writer to write generated output. Returned Writer is mapped to a + * filesystem file (although subclasses may override that). File location is + * determined from the current state of VelocityContext and the TemplateType + * passed as a parameter. Writer encoding is determined from the value of + * the "encoding" property. + */ + protected Writer openWriter(TemplateType templateType) throws Exception { + + File outFile = (templateType.isSuperclass()) ? fileForSuperclass() : fileForClass(); + if (outFile == null) { + return null; + } + + if (logger != null) { + String label = templateType.isSuperclass() ? "superclass" : "class"; + logger.info("Generating " + label + " file: " + outFile.getCanonicalPath()); + } + + // return writer with specified encoding + FileOutputStream out = new FileOutputStream(outFile); + + return (cgenConfiguration.getEncoding() != null) ? new OutputStreamWriter(out, cgenConfiguration.getEncoding()) : new OutputStreamWriter(out); + } + + /** + * Returns a target file where a generated superclass must be saved. If null + * is returned, class shouldn't be generated. + */ + private File fileForSuperclass() throws Exception { + + String packageName = (String) context.get(Artifact.SUPER_PACKAGE_KEY); + String className = (String) context.get(Artifact.SUPER_CLASS_KEY); + + String filename = StringUtils.getInstance().replaceWildcardInStringWithString(WILDCARD, cgenConfiguration.getOutputPattern(), className); + File dest = new File(mkpath(cgenConfiguration.buildOutputPath().toFile(), packageName), filename); if (dest.exists() && !fileNeedUpdate(dest, cgenConfiguration.getSuperTemplate().getData())) { return null; } - return dest; - } + return dest; + } - /** - * Returns a target file where a generated class must be saved. If null is - * returned, class shouldn't be generated. - */ - private File fileForClass() throws Exception { + /** + * Returns a target file where a generated class must be saved. If null is + * returned, class shouldn't be generated. + */ + private File fileForClass() throws Exception { - String packageName = (String) context.get(Artifact.SUB_PACKAGE_KEY); - String className = (String) context.get(Artifact.SUB_CLASS_KEY); + String packageName = (String) context.get(Artifact.SUB_PACKAGE_KEY); + String className = (String) context.get(Artifact.SUB_CLASS_KEY); - String filename = StringUtils.getInstance().replaceWildcardInStringWithString(WILDCARD, cgenConfiguration.getOutputPattern(), className); - File dest = new File(mkpath(cgenConfiguration.buildOutputPath().toFile(), packageName), filename); + String filename = StringUtils.getInstance().replaceWildcardInStringWithString(WILDCARD, cgenConfiguration.getOutputPattern(), className); + File dest = new File(mkpath(cgenConfiguration.buildOutputPath().toFile(), packageName), filename); - if (dest.exists()) { - // no overwrite of subclasses - if (cgenConfiguration.isMakePairs()) { - return null; - } + if (dest.exists()) { + // no overwrite of subclasses + if (cgenConfiguration.isMakePairs()) { + return null; + } - // skip if said so - if (!cgenConfiguration.isOverwrite()) { - return null; - } + // skip if said so + if (!cgenConfiguration.isOverwrite()) { + return null; + } if (!fileNeedUpdate(dest, cgenConfiguration.getTemplate().getData())) { return null; } } - return dest; - } + return dest; + } - /** - * Ignore if the destination is newer than the map - * (internal timestamp), i.e. has been generated after the map was - * last saved AND the template is older than the destination file - */ - protected boolean fileNeedUpdate(File dest, String templateFileName) { - if(cgenConfiguration.isForce()) { - return true; - } + /** + * Ignore if the destination is newer than the map + * (internal timestamp), i.e. has been generated after the map was + * last saved AND the template is older than the destination file + */ + protected boolean fileNeedUpdate(File dest, String templateFileName) { + if (cgenConfiguration.isForce()) { + return true; + } - if (isOld(dest)) { + if (isOld(dest)) { if (templateFileName == null) { - return false; + return false; } File templateFile = new File(templateFileName); - return templateFile.lastModified() >= dest.lastModified(); + return templateFile.lastModified() >= dest.lastModified(); } - return true; - } - - /** - * Is file modified after internal timestamp (usually equal to mtime of datamap file) - */ - protected boolean isOld(File file) { - return file.lastModified() > cgenConfiguration.getTimestamp(); - } - - /** - * Returns a File object corresponding to a directory where files that - * belong to <code>pkgName</code> package should reside. Creates any missing - * diectories below <code>dest</code>. - */ - private File mkpath(File dest, String pkgName) throws Exception { - - if (!cgenConfiguration.isUsePkgPath() || pkgName == null) { - return dest; - } - - String path = pkgName.replace('.', File.separatorChar); - File fullPath = new File(dest, path); - if (!fullPath.isDirectory() && !fullPath.mkdirs()) { - throw new Exception("Error making path: " + fullPath); - } - - return fullPath; - } - - /** - * Injects an optional logger that will be used to trace generated files at - * the info level. - */ - public void setLogger(Logger logger) { - this.logger = logger; - } - - /** - * @since 4.1 - */ - public CgenConfiguration getCgenConfiguration() { - return cgenConfiguration; - } - - /** - * Sets an optional shared VelocityContext. Useful with tools like VPP that - * can set custom values in the context, not known to Cayenne. - */ - public void setContext(Context context) { - this.context = context; - } + return true; + } + + /** + * Is file modified after internal timestamp (usually equal to mtime of datamap file) + */ + protected boolean isOld(File file) { + return file.lastModified() > cgenConfiguration.getTimestamp(); + } + + /** + * Returns a File object corresponding to a directory where files that + * belong to <code>pkgName</code> package should reside. Creates any missing + * diectories below <code>dest</code>. + */ + private File mkpath(File dest, String pkgName) throws Exception { + + if (!cgenConfiguration.isUsePkgPath() || pkgName == null) { + return dest; + } + + String path = pkgName.replace('.', File.separatorChar); + File fullPath = new File(dest, path); + if (!fullPath.isDirectory() && !fullPath.mkdirs()) { + throw new Exception("Error making path: " + fullPath); + } + + return fullPath; + } + + /** + * Injects an optional logger that will be used to trace generated files at + * the info level. + */ + public void setLogger(Logger logger) { + this.logger = logger; + } + + /** + * @since 4.1 + */ + public CgenConfiguration getCgenConfiguration() { + return cgenConfiguration; + } + + /** + * Sets an optional shared VelocityContext. Useful with tools like VPP that + * can set custom values in the context, not known to Cayenne. + */ + public void setContext(Context context) { + this.context = context; + } /** * @since 4.1 diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGeneratorResourceLoader.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGeneratorResourceLoader.java index 50c8d59ca..8e72b0b33 100644 --- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGeneratorResourceLoader.java +++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGeneratorResourceLoader.java @@ -39,18 +39,14 @@ import org.apache.velocity.util.ExtProperties; * * @since 1.2 */ -// must be public top-level class as it is -// instantiated via reflection by Velocity +// must be public top-level class as it is instantiated via reflection by Velocity public class ClassGeneratorResourceLoader extends FileResourceLoader { private Path root; @Override public void init(ExtProperties configuration) { - String rootPathStr = configuration.getString("root"); - if(rootPathStr != null) { - root = Paths.get(rootPathStr); - } + root = (Path)configuration.getProperty("root"); } /** @@ -63,8 +59,7 @@ public class ClassGeneratorResourceLoader extends FileResourceLoader { * </ol> */ @Override - public synchronized Reader getResourceReader(String name, String charset) - throws ResourceNotFoundException { + public synchronized Reader getResourceReader(String name, String charset) throws ResourceNotFoundException { Reader stream; @@ -103,9 +98,9 @@ public class ClassGeneratorResourceLoader extends FileResourceLoader { protected Reader loadFromAbsPath(String name) { try { File file = new File(name); - return (file.canRead()) ? new BufferedReader(new FileReader(file - .getAbsolutePath())) : null; - + return file.canRead() + ? new BufferedReader(new FileReader(file.getAbsolutePath())) + : null; } catch (FileNotFoundException fnfe) { return null; } diff --git a/cayenne-gradle-plugin/src/main/java/org/apache/cayenne/tools/CgenTask.java b/cayenne-gradle-plugin/src/main/java/org/apache/cayenne/tools/CgenTask.java index 32c626924..35f553856 100644 --- a/cayenne-gradle-plugin/src/main/java/org/apache/cayenne/tools/CgenTask.java +++ b/cayenne-gradle-plugin/src/main/java/org/apache/cayenne/tools/CgenTask.java @@ -290,7 +290,7 @@ public class CgenTask extends BaseCayenneTask { } else { generator.addEntities(filterEntityAction.getFilteredEntities(dataMap)); generator.addEmbeddables(filterEmbeddableAction.getFilteredEmbeddables(dataMap)); - generator.addQueries(dataMap.getQueryDescriptors()); + generator.addDataMap(dataMap); } generator.execute(); } diff --git a/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java b/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java index 78b3d2d19..a85a3a4e4 100644 --- a/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java +++ b/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java @@ -288,7 +288,7 @@ public class CayenneGeneratorMojo extends AbstractMojo { } else { generator.addEntities(filterEntityAction.getFilteredEntities(dataMap)); generator.addEmbeddables(filterEmbeddableAction.getFilteredEmbeddables(dataMap)); - generator.addQueries(dataMap.getQueryDescriptors()); + generator.addDataMap(dataMap); } generator.execute(); } diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/cgen/templateeditor/DataMapArtefactsConfigurator.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/cgen/templateeditor/DataMapArtefactsConfigurator.java index df3a07af2..114cd2ca4 100644 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/cgen/templateeditor/DataMapArtefactsConfigurator.java +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/cgen/templateeditor/DataMapArtefactsConfigurator.java @@ -21,11 +21,9 @@ package org.apache.cayenne.modeler.editor.cgen.templateeditor; import org.apache.cayenne.gen.ClassGenerationAction; import org.apache.cayenne.map.DataMap; -import org.apache.cayenne.map.QueryDescriptor; import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; /** * @since 5.0 @@ -34,23 +32,12 @@ public class DataMapArtefactsConfigurator implements ArtefactsConfigurator { @Override public void config(ClassGenerationAction action, String artifactName) { - action.addQueries(Collections.singleton(getSelectedEntity(artifactName, action))); + action.addDataMap(action.getCgenConfiguration().getDataMap()); } + @Override public List<String> getArtifactsNames(DataMap dataMap) { - if (dataMap != null) { - return dataMap.getQueryDescriptors().stream() - .map(QueryDescriptor::getName) - .collect(Collectors.toList()); - } - return Collections.emptyList(); + return Collections.singletonList(dataMap.getName()); } - private QueryDescriptor getSelectedEntity(String artifactName, ClassGenerationAction action) { - DataMap dataMap = action.getCgenConfiguration().getDataMap(); - if (dataMap != null) { - return dataMap.getQueryDescriptor(artifactName); - } - return null; - } } diff --git a/modeler/cayenne-modeler/src/test/java/org/apache/cayenne/modeler/editor/templateeditor/ArtifactsConfiguratorTest.java b/modeler/cayenne-modeler/src/test/java/org/apache/cayenne/modeler/editor/templateeditor/ArtifactsConfiguratorTest.java index d6607974a..9a583a30f 100644 --- a/modeler/cayenne-modeler/src/test/java/org/apache/cayenne/modeler/editor/templateeditor/ArtifactsConfiguratorTest.java +++ b/modeler/cayenne-modeler/src/test/java/org/apache/cayenne/modeler/editor/templateeditor/ArtifactsConfiguratorTest.java @@ -82,7 +82,7 @@ public class ArtifactsConfiguratorTest { assertTrue(entityArtifactsNames.contains("objEntity")); assertTrue(embeddableArtifactsNames.contains("embeddable")); - assertTrue(dataMapArtifactsNames.contains("queryDescriptor")); + assertTrue(dataMapArtifactsNames.contains("dataMap")); } private DataMap configureDataMap() {