http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/map/naming/DefaultNameGenerator.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/map/naming/DefaultNameGenerator.java b/cayenne-tools/src/main/java/org/apache/cayenne/map/naming/DefaultNameGenerator.java index 5d5a26d..af76188 100644 --- a/cayenne-tools/src/main/java/org/apache/cayenne/map/naming/DefaultNameGenerator.java +++ b/cayenne-tools/src/main/java/org/apache/cayenne/map/naming/DefaultNameGenerator.java @@ -26,8 +26,7 @@ import org.apache.cayenne.map.DbRelationship; import org.jvnet.inflector.Noun; /** - * SmartNameGenerator is a strategy for generating names of entities, attributes - * etc. + * A strategy for generating names of entities, attributes etc. * * @since 4.0 */ @@ -44,8 +43,7 @@ public class DefaultNameGenerator implements ObjectNameGenerator { * by default we use english language rules here. uppercase is * required for NameConverter to work properly */ - name = Noun.pluralOf(key.getFKTableName().toLowerCase(), - Locale.ENGLISH).toUpperCase(); + name = Noun.pluralOf(key.getFKTableName().toLowerCase(), Locale.ENGLISH).toUpperCase(); } catch (Exception inflectorError) { /** * seems that Inflector cannot be trusted. For instance, it @@ -61,11 +59,9 @@ public class DefaultNameGenerator implements ObjectNameGenerator { // trim "ID" in the end if (fkColName == null) { name = key.getPKTableName(); - } else if (fkColName.toUpperCase().endsWith("_ID") - && fkColName.length() > 3) { + } else if (fkColName.toUpperCase().endsWith("_ID") && fkColName.length() > 3) { name = fkColName.substring(0, fkColName.length() - 3); - } else if (fkColName.toUpperCase().endsWith("ID") - && fkColName.length() > 2) { + } else if (fkColName.toUpperCase().endsWith("ID") && fkColName.length() > 2) { name = fkColName.substring(0, fkColName.length() - 2); } else { /**
http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/AntDataPortDelegate.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/AntDataPortDelegate.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/AntDataPortDelegate.java index 27ea1b2..828d744 100644 --- a/cayenne-tools/src/main/java/org/apache/cayenne/tools/AntDataPortDelegate.java +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/AntDataPortDelegate.java @@ -25,6 +25,7 @@ import java.util.regex.Pattern; import org.apache.cayenne.access.DataPort; import org.apache.cayenne.access.DataPortDelegate; +import org.apache.cayenne.access.loader.NamePatternMatcher; import org.apache.cayenne.map.DataMap; import org.apache.cayenne.map.DbEntity; import org.apache.cayenne.query.Query; @@ -60,10 +61,10 @@ class AntDataPortDelegate implements DataPortDelegate { String includeEntitiesPattern, String excludeEntitiesPattern) { this.parentTask = parentTask; - this.namePatternMatcher = new NamePatternMatcher(new AntLogger( - parentTask), includeEntitiesPattern, excludeEntitiesPattern); + AntLogger logger = new AntLogger(parentTask); - this.mapFilters = namePatternMatcher.createPatterns(mapsPattern); + this.namePatternMatcher = NamePatternMatcher.build(logger, includeEntitiesPattern, excludeEntitiesPattern); + this.mapFilters = NamePatternMatcher.createPatterns(logger, mapsPattern); } /** http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneGeneratorEntityFilterAction.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneGeneratorEntityFilterAction.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneGeneratorEntityFilterAction.java index fd297fc..49329a7 100644 --- a/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneGeneratorEntityFilterAction.java +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneGeneratorEntityFilterAction.java @@ -22,8 +22,8 @@ import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; -import java.util.List; +import org.apache.cayenne.access.loader.NameFilter; import org.apache.cayenne.map.DataMap; import org.apache.cayenne.map.Embeddable; import org.apache.cayenne.map.ObjEntity; @@ -36,12 +36,11 @@ import org.apache.cayenne.map.ObjEntity; */ class CayenneGeneratorEntityFilterAction { - private NamePatternMatcher nameFilter; + private NameFilter nameFilter; private boolean client; Collection<Embeddable> getFilteredEmbeddables(DataMap mainDataMap) { - List<Embeddable> embeddables = new ArrayList<Embeddable>(mainDataMap - .getEmbeddables()); + Collection<Embeddable> embeddables = new ArrayList<Embeddable>(mainDataMap.getEmbeddables()); // filter out excluded entities... Iterator<Embeddable> it = embeddables.iterator(); @@ -62,20 +61,13 @@ class CayenneGeneratorEntityFilterAction { Collection<ObjEntity> getFilteredEntities(DataMap mainDataMap) throws MalformedURLException { - List<ObjEntity> entities = new ArrayList<ObjEntity>(mainDataMap.getObjEntities()); + Collection<ObjEntity> entities = new ArrayList<ObjEntity>(mainDataMap.getObjEntities()); // filter out excluded entities... Iterator<ObjEntity> it = entities.iterator(); - while (it.hasNext()) { ObjEntity e = it.next(); - if (e.isGeneric()) { - it.remove(); - } - else if (client && !e.isClientAllowed()) { - it.remove(); - } - else if (!nameFilter.isIncluded(e.getName())) { + if (e.isGeneric() || client && !e.isClientAllowed() || !nameFilter.isIncluded(e.getName())) { it.remove(); } } @@ -87,7 +79,7 @@ class CayenneGeneratorEntityFilterAction { this.client = client; } - public void setNameFilter(NamePatternMatcher nameFilter) { + public void setNameFilter(NameFilter nameFilter) { this.nameFilter = nameFilter; } } http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java index e4e3787..dcd36e6 100644 --- a/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java @@ -20,6 +20,7 @@ package org.apache.cayenne.tools; import java.io.File; +import org.apache.cayenne.access.loader.NamePatternMatcher; import org.apache.cayenne.gen.ArtifactsGenerationMode; import org.apache.cayenne.gen.ClassGenerationAction; import org.apache.cayenne.gen.ClientClassGenerationAction; @@ -115,10 +116,7 @@ public class CayenneGeneratorTask extends CayenneTask { CayenneGeneratorEntityFilterAction filterAction = new CayenneGeneratorEntityFilterAction(); filterAction.setClient(client); - filterAction.setNameFilter(new NamePatternMatcher( - logger, - includeEntitiesPattern, - excludeEntitiesPattern)); + filterAction.setNameFilter(NamePatternMatcher.build(logger, includeEntitiesPattern, excludeEntitiesPattern)); try { http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java index b641759..e9f698e 100644 --- a/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java @@ -1,26 +1,38 @@ -/***************************************************************** - * 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 +/* + * 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 + * 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. - ****************************************************************/ + * 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.tools; import java.io.File; +import org.apache.cayenne.access.loader.filters.EntityFilters; +import org.apache.cayenne.access.loader.filters.FilterFactory; +import org.apache.cayenne.conn.DataSourceInfo; +import org.apache.cayenne.tools.dbimport.config.Catalog; +import org.apache.cayenne.tools.dbimport.config.ExcludeColumn; +import org.apache.cayenne.tools.dbimport.config.ExcludeProcedure; +import org.apache.cayenne.tools.dbimport.config.FiltersConfigBuilder; +import org.apache.cayenne.tools.dbimport.config.IncludeColumn; +import org.apache.cayenne.tools.dbimport.config.IncludeProcedure; +import org.apache.cayenne.tools.dbimport.config.IncludeTable; +import org.apache.cayenne.tools.dbimport.config.ReverseEngineering; +import org.apache.cayenne.tools.dbimport.config.Schema; import org.apache.cayenne.di.DIBootstrap; import org.apache.cayenne.di.Injector; import org.apache.cayenne.map.naming.DefaultNameGenerator; @@ -36,42 +48,34 @@ import org.apache.tools.ant.Task; public class DbImporterTask extends Task { - private final DbImportConfiguration parameters; + private final DbImportConfiguration config; - /** - * @deprecated since 4.0 in favor of "schema" - */ - @Deprecated - private String schemaName; + private final ReverseEngineering reverseEngineering = new ReverseEngineering(); - /** - * @deprecated since 4.0 in favor of "meaningfulPkTable" - */ - @Deprecated - private boolean meaningfulPk; + private final EntityFilters.Builder filterBuilder = new EntityFilters.Builder(); public DbImporterTask() { - parameters = new DbImportConfiguration(); - parameters.setOverwrite(true); - parameters.setImportProcedures(false); - parameters.setUsePrimitives(true); - parameters.setNamingStrategy(DefaultNameGenerator.class.getName()); + config = new DbImportConfiguration(); + config.setOverwrite(true); + config.setUsePrimitives(true); + config.setNamingStrategy(DefaultNameGenerator.class.getName()); } @Override public void execute() { - initSchema(); - initMeaningfulPkTables(); + config.setFiltersConfig(new FiltersConfigBuilder(reverseEngineering) + .add(filterBuilder.build()) + .filtersConfig()); validateAttributes(); Log logger = new AntLogger(this); - parameters.setLogger(logger); + config.setLogger(logger); Injector injector = DIBootstrap.createInjector(new ToolsModule(logger), new DbImportModule()); try { - injector.getInstance(DbImportAction.class).execute(parameters); + injector.getInstance(DbImportAction.class).execute(config); } catch (Exception ex) { Throwable th = Util.unwindException(ex); @@ -96,16 +100,17 @@ public class DbImporterTask extends Task { protected void validateAttributes() throws BuildException { StringBuilder error = new StringBuilder(""); - if (parameters.getDataMapFile() == null) { + if (config.getDataMapFile() == null) { error.append("The 'map' attribute must be set.\n"); } - if (parameters.getDriver() == null) { + DataSourceInfo dataSourceInfo = config.getDataSourceInfo(); + if (dataSourceInfo.getJdbcDriver() == null) { error.append("The 'driver' attribute must be set.\n"); } - if (parameters.getUrl() == null) { - error.append("The 'adapter' attribute must be set.\n"); + if (dataSourceInfo.getDataSourceUrl() == null) { + error.append("The 'url' attribute must be set.\n"); } if (error.length() > 0) { @@ -117,122 +122,151 @@ public class DbImporterTask extends Task { * @since 4.0 */ public void setOverwrite(boolean overwrite) { - parameters.setOverwrite(overwrite); + config.setOverwrite(overwrite); } /** * @deprecated since 4.0 use {@link #setSchema(String)} */ + @Deprecated public void setSchemaName(String schemaName) { - this.schemaName = schemaName; + this.setSchema(schemaName); } /** * @since 4.0 */ public void setSchema(String schema) { - parameters.setSchema(schema); + filterBuilder.schema(schema); } /** * @since 4.0 */ public void setDefaultPackage(String defaultPackage) { - parameters.setDefaultPackage(defaultPackage); + config.setDefaultPackage(defaultPackage); } public void setTablePattern(String tablePattern) { - parameters.setTablePattern(tablePattern); + filterBuilder.includeTables(tablePattern); } public void setImportProcedures(boolean importProcedures) { - parameters.setImportProcedures(importProcedures); + filterBuilder.setProceduresFilters(importProcedures ? FilterFactory.TRUE : FilterFactory.NULL); } public void setProcedurePattern(String procedurePattern) { - parameters.setProcedurePattern(procedurePattern); + filterBuilder.includeProcedures(procedurePattern); } /** * @deprecated since 4.0 use {@link #setMeaningfulPkTables(String)} */ public void setMeaningfulPk(boolean meaningfulPk) { - this.meaningfulPk = meaningfulPk; + log("'meaningfulPk' property is deprecated. Use 'meaningfulPkTables' pattern instead", Project.MSG_WARN); + + if (meaningfulPk) { + setMeaningfulPkTables("*"); + } } /** * @since 4.0 */ public void setMeaningfulPkTables(String meaningfulPkTables) { - parameters.setMeaningfulPkTables(meaningfulPkTables); + config.setMeaningfulPkTables(meaningfulPkTables); } public void setNamingStrategy(String namingStrategy) { - parameters.setNamingStrategy(namingStrategy); + config.setNamingStrategy(namingStrategy); } public void setAdapter(String adapter) { - parameters.setAdapter(adapter); + config.setAdapter(adapter); } public void setDriver(String driver) { - parameters.setDriver(driver); + config.setDriver(driver); } public void setMap(File map) { - parameters.setDataMapFile(map); + config.setDataMapFile(map); } public void setPassword(String password) { - parameters.setPassword(password); + config.setPassword(password); } public void setUrl(String url) { - parameters.setUrl(url); + config.setUrl(url); } public void setUserName(String username) { - parameters.setUsername(username); + config.setUsername(username); } /** * @since 4.0 */ public void setIncludeTables(String includeTables) { - parameters.setIncludeTables(includeTables); + filterBuilder.includeTables(includeTables); } /** * @since 4.0 */ public void setExcludeTables(String excludeTables) { - parameters.setExcludeTables(excludeTables); + filterBuilder.excludeTables(excludeTables); } /** * @since 4.0 */ public void setUsePrimitives(boolean usePrimitives) { - parameters.setUsePrimitives(usePrimitives); + config.setUsePrimitives(usePrimitives); } - private void initSchema() { - if (schemaName != null) { - log("'schemaName' property is deprecated. Use 'schema' instead", Project.MSG_WARN); - } + public void addConfiguredIncludeColumn(IncludeColumn includeColumn) { + reverseEngineering.addIncludeColumn(includeColumn); + } - if (parameters.getSchema() == null) { - parameters.setSchema(schemaName); - } + public void addConfiguredExcludeColumn(ExcludeColumn excludeColumn) { + reverseEngineering.addExcludeColumn(excludeColumn); } - private void initMeaningfulPkTables() { - if (meaningfulPk) { - log("'meaningfulPk' property is deprecated. Use 'meaningfulPkTables' pattern instead", Project.MSG_WARN); - } + public void addConfiguredIncludeTable(IncludeTable includeTable) { + reverseEngineering.addIncludeTable(includeTable); + } - if (parameters.getMeaningfulPkTables() == null && meaningfulPk) { - parameters.setMeaningfulPkTables("*"); - } + public void addConfiguredExcludeTable(ExcludeTable excludeTable) { + reverseEngineering.addExcludeTable(excludeTable); + } + + public void addConfiguredIncludeProcedure(IncludeProcedure includeProcedure) { + reverseEngineering.addIncludeProcedure(includeProcedure); + } + + public void addConfiguredExcludeProcedure(ExcludeProcedure excludeProcedure) { + reverseEngineering.addExcludeProcedure(excludeProcedure); + } + + public void addConfiguredSchema(Schema schema) { + reverseEngineering.addSchema(schema); + } + + public void addConfiguredCatalog(Catalog catalog) { + reverseEngineering.addCatalog(catalog); + } + + public ReverseEngineering getReverseEngineering() { + return reverseEngineering; + } + + public File getMap() { + return config.getDataMapFile(); + } + + public DbImportConfiguration toParameters() { + return config; } } http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/ExcludeTable.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/ExcludeTable.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/ExcludeTable.java new file mode 100644 index 0000000..22003a7 --- /dev/null +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/ExcludeTable.java @@ -0,0 +1,33 @@ +/***************************************************************** + * 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.tools; + +import org.apache.cayenne.tools.dbimport.config.PatternParam; + +/** + * @since 3.2. + */ +public class ExcludeTable extends PatternParam { + public ExcludeTable() { + } + + public ExcludeTable(String pattern) { + super(pattern); + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/NamePatternMatcher.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/NamePatternMatcher.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/NamePatternMatcher.java deleted file mode 100644 index a567fb3..0000000 --- a/cayenne-tools/src/main/java/org/apache/cayenne/tools/NamePatternMatcher.java +++ /dev/null @@ -1,294 +0,0 @@ -/***************************************************************** - * 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.tools; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.StringTokenizer; -import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; - -import org.apache.cayenne.util.CayenneMapEntry; -import org.apache.commons.logging.Log; - -/** - * Provides name pattern matching functionality. - * - * @since 1.2 - */ -public class NamePatternMatcher { - - protected Log logger; - - protected Pattern[] itemIncludeFilters; - protected Pattern[] itemExcludeFilters; - - public NamePatternMatcher(Log logger, String includePattern, String excludePattern) { - this.logger = logger; - this.itemIncludeFilters = createPatterns(includePattern); - this.itemExcludeFilters = createPatterns(excludePattern); - } - - /** - * Applies preconfigured list of filters to the list, removing entities that do not - * pass the filter. - * - * @deprecated since 3.0 still used by AntDataPortDelegate, which itself should - * probably be deprecated - */ - @Deprecated - List<?> filter(List<?> items) { - if (items == null || items.isEmpty()) { - return items; - } - - if ((itemIncludeFilters.length == 0) && (itemExcludeFilters.length == 0)) { - return items; - } - - Iterator<?> it = items.iterator(); - while (it.hasNext()) { - CayenneMapEntry entity = (CayenneMapEntry) it.next(); - - if (!passedIncludeFilter(entity)) { - it.remove(); - continue; - } - - if (!passedExcludeFilter(entity)) { - it.remove(); - } - } - - return items; - } - - /** - * Returns true if the entity matches any one of the "include" patterns, or if there - * is no "include" patterns defined. - * - * @deprecated since 3.0. still used by AntDataPortDelegate, which itself should - * probably be deprecated - */ - @Deprecated - private boolean passedIncludeFilter(CayenneMapEntry item) { - if (itemIncludeFilters.length == 0) { - return true; - } - - String itemName = item.getName(); - for (Pattern itemIncludeFilter : itemIncludeFilters) { - if (itemIncludeFilter.matcher(itemName).find()) { - return true; - } - } - - return false; - } - - /** - * Returns true if the entity does not match any one of the "exclude" patterns, or if - * there is no "exclude" patterns defined. - * - * @deprecated since 3.0 - */ - @Deprecated - private boolean passedExcludeFilter(CayenneMapEntry item) { - if (itemExcludeFilters.length == 0) { - return true; - } - - String itemName = item.getName(); - for (Pattern itemExcludeFilter : itemExcludeFilters) { - if (itemExcludeFilter.matcher(itemName).find()) { - return false; - } - } - - return true; - } - - /** - * Returns an array of Patterns. Takes a comma-separated list of patterns, attempting - * to convert them to the java.util.regex.Pattern syntax. E.g. - * <p> - * <code>"billing_*,user?"</code> will become an array of two expressions: - * <p> - * <code>^billing_.*$</code><br> - * <code>^user.?$</code><br> - */ - public Pattern[] createPatterns(String patternString) { - String[] patternStrings = tokenizePattern(patternString); - List<Pattern> patterns = new ArrayList<Pattern>(patternStrings.length); - - for (int i = 0; i < patternStrings.length; i++) { - - // test the pattern - try { - patterns.add(Pattern.compile(patternStrings[i])); - } - catch (PatternSyntaxException e) { - - if (logger != null) { - logger.warn("Ignoring invalid pattern [" - + patternStrings[i] - + "], reason: " - + e.getMessage()); - } - } - } - - return patterns.toArray(new Pattern[patterns.size()]); - } - - /** - * Returns an array of valid regular expressions. Takes a comma-separated list of - * patterns, attempting to convert them to the java.util.regex.Pattern syntax. E.g. - * <p> - * <code>"billing_*,user?"</code> will become an array of two expressions: - * <p> - * <code>^billing_.*$</code><br> - * <code>^user.?$</code><br> - */ - public String[] tokenizePattern(String pattern) { - if (pattern != null && pattern.length() > 0) { - StringTokenizer toks = new StringTokenizer(pattern, ","); - - int len = toks.countTokens(); - if (len == 0) { - return new String[0]; - } - - List<String> patterns = new ArrayList<String>(len); - for (int i = 0; i < len; i++) { - String nextPattern = toks.nextToken(); - StringBuilder buffer = new StringBuilder(); - - // convert * into regex syntax - // e.g. abc*x becomes ^abc.*x$ - // or abc?x becomes ^abc.?x$ - buffer.append("^"); - for (int j = 0; j < nextPattern.length(); j++) { - char nextChar = nextPattern.charAt(j); - if (nextChar == '*' || nextChar == '?') { - buffer.append('.'); - } - buffer.append(nextChar); - } - buffer.append("$"); - patterns.add(buffer.toString()); - } - - return patterns.toArray(new String[patterns.size()]); - } - else { - return new String[0]; - } - } - - /** - * Returns true if a given object property satisfies the include/exclude patterns. - * - * @since 3.0 - */ - public boolean isIncluded(String string) { - - if ((itemIncludeFilters.length == 0) && (itemExcludeFilters.length == 0)) { - return true; - } - - if (!passedIncludeFilter(string)) { - return false; - } - - if (!passedExcludeFilter(string)) { - return false; - } - - return true; - } - - /** - * Returns true if an object matches any one of the "include" patterns, or if there is - * no "include" patterns defined. - * - * @since 3.0 - */ - boolean passedIncludeFilter(String item) { - if (itemIncludeFilters.length == 0) { - return true; - } - - for (Pattern itemIncludeFilter : itemIncludeFilters) { - if (itemIncludeFilter.matcher(item).find()) { - return true; - } - } - - return false; - } - - /** - * Returns true if an object does not match any one of the "exclude" patterns, or if - * there is no "exclude" patterns defined. - * - * @since 3.0 - */ - boolean passedExcludeFilter(String item) { - if (itemExcludeFilters.length == 0) { - return true; - } - - for (Pattern itemExcludeFilter : itemExcludeFilters) { - if (itemExcludeFilter.matcher(item).find()) { - return false; - } - } - - return true; - } - - public static String replaceWildcardInStringWithString( - String wildcard, - String pattern, - String replacement) { - if (null == pattern || null == wildcard) - return pattern; - - StringBuilder buffer = new StringBuilder(); - int lastPos = 0; - int wildCardPos = pattern.indexOf(wildcard); - while (-1 != wildCardPos) { - if (lastPos != wildCardPos) { - buffer.append(pattern.substring(lastPos, wildCardPos)); - } - buffer.append(replacement); - lastPos += wildCardPos + wildcard.length(); - wildCardPos = pattern.indexOf(wildcard, lastPos); - } - - if (lastPos < pattern.length()) { - buffer.append(pattern.substring(lastPos)); - } - - return buffer.toString(); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java index 66acb77..bb94fcd 100644 --- a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java @@ -1,21 +1,21 @@ -/***************************************************************** - * 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 +/* + * 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 + * 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. - ****************************************************************/ + * 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.tools.dbimport; import org.apache.cayenne.access.DbLoader; @@ -67,7 +67,8 @@ public class DbImportAction { private final DbAdapterFactory adapterFactory; private final MapLoader mapLoader; - public DbImportAction(@Inject Log logger, @Inject ProjectSaver projectSaver, + public DbImportAction(@Inject Log logger, + @Inject ProjectSaver projectSaver, @Inject DataSourceFactory dataSourceFactory, @Inject DbAdapterFactory adapterFactory, @Inject MapLoader mapLoader) { @@ -78,30 +79,29 @@ public class DbImportAction { this.mapLoader = mapLoader; } - public void execute(DbImportConfiguration parameters) throws Exception { + public void execute(DbImportConfiguration config) throws Exception { if (logger.isInfoEnabled()) { - logger.debug(String.format("DB connection - [driver: %s, url: %s, username: %s, password: %s]", - parameters.getDriver(), parameters.getUrl(), parameters.getUsername(), "XXXXX")); + logger.debug("DB connection: " + config.getDataSourceInfo()); } if (logger.isDebugEnabled()) { - parameters.log(); + logger.debug(config); } - DataNodeDescriptor dataNodeDescriptor = parameters.createDataNodeDescriptor(); + DataNodeDescriptor dataNodeDescriptor = config.createDataNodeDescriptor(); DataSource dataSource = dataSourceFactory.getDataSource(dataNodeDescriptor); DbAdapter adapter = adapterFactory.createAdapter(dataNodeDescriptor, dataSource); - DataMap loadedFomDb = load(parameters, adapter, dataSource.getConnection()); + DataMap loadedFomDb = load(config, adapter, dataSource.getConnection()); if (loadedFomDb == null) { logger.info("Nothing was loaded from db."); return; } - DataMap existing = loadExistingDataMap(parameters.getDataMapFile()); + DataMap existing = loadExistingDataMap(config.getDataMapFile()); if (existing == null) { - saveLoaded(loadedFomDb); + saveLoaded(config.initializeDataMap(loadedFomDb)); } else { MergerFactory mergerFactory = adapter.mergerFactory(); @@ -111,7 +111,7 @@ public class DbImportAction { return; } - saveLoaded(execute(parameters.createMergeDelegate(), existing, log(reverse(mergerFactory, mergeTokens)))); + saveLoaded(execute(config.createMergeDelegate(), existing, log(reverse(mergerFactory, mergeTokens)))); } } @@ -184,18 +184,13 @@ public class DbImportAction { projectSaver.save(project); } - private DataMap load(DbImportConfiguration parameters, DbAdapter adapter, Connection connection) throws Exception { - DataMap dataMap = parameters.createDataMap(); + private DataMap load(DbImportConfiguration config, DbAdapter adapter, Connection connection) throws Exception { + DataMap dataMap = config.createDataMap(); try { - DbLoader loader = parameters.createLoader(adapter, connection, parameters.createLoaderDelegate()); - - String[] types = loader.getDefaultTableTypes(); - loader.load(dataMap, parameters.getCatalog(), parameters.getSchema(), parameters.getTablePattern(), types); + DbLoader loader = config.createLoader(adapter, connection, config.createLoaderDelegate()); - if (parameters.isImportProcedures()) { - loader.loadProcedures(dataMap, parameters.getCatalog(), parameters.getSchema(), parameters.getProcedurePattern()); - } + dataMap = loader.load(config.getDbLoaderConfig()); } finally { if (connection != null) { connection.close(); http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportConfiguration.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportConfiguration.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportConfiguration.java index 4781466..35fba65 100644 --- a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportConfiguration.java +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportConfiguration.java @@ -1,49 +1,51 @@ -/***************************************************************** - * 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 +/* + * 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 + * 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. - ****************************************************************/ + * 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.tools.dbimport; import org.apache.cayenne.CayenneRuntimeException; import org.apache.cayenne.access.DbLoader; -import org.apache.cayenne.access.DbLoaderConfiguration; +import org.apache.cayenne.access.loader.DbLoaderConfiguration; import org.apache.cayenne.access.DbLoaderDelegate; -import org.apache.cayenne.access.DefaultDbLoaderDelegate; +import org.apache.cayenne.access.loader.DefaultDbLoaderDelegate; +import org.apache.cayenne.access.loader.NameFilter; +import org.apache.cayenne.access.loader.filters.DbPath; +import org.apache.cayenne.access.loader.filters.FiltersConfig; import org.apache.cayenne.configuration.DataNodeDescriptor; import org.apache.cayenne.conn.DataSourceInfo; import org.apache.cayenne.dba.DbAdapter; import org.apache.cayenne.map.DataMap; import org.apache.cayenne.map.DbEntity; import org.apache.cayenne.map.EntityResolver; -import org.apache.cayenne.map.MapLoader; import org.apache.cayenne.map.naming.ObjectNameGenerator; import org.apache.cayenne.merge.DefaultModelMergeDelegate; import org.apache.cayenne.merge.ModelMergeDelegate; import org.apache.cayenne.resource.URLResource; -import org.apache.cayenne.tools.NamePatternMatcher; +import org.apache.cayenne.access.loader.NamePatternMatcher; import org.apache.cayenne.util.EntityMergeSupport; import org.apache.commons.logging.Log; -import org.xml.sax.InputSource; import java.io.File; import java.io.IOException; -import java.net.URL; +import java.net.MalformedURLException; import java.sql.Connection; import java.util.Collections; +import java.util.List; import static org.apache.commons.lang.StringUtils.isNotEmpty; @@ -70,69 +72,25 @@ public class DbImportConfiguration { */ private boolean overwrite; - /** - * DB schema to use for DB importing. - */ - private DbLoaderConfiguration dbLoaderConfiguration = new DbLoaderConfiguration(); - - /** - * Pattern for tables to import from DB - */ - private String tablePattern; - - /** - * Indicates whether stored procedures should be imported. - */ - private boolean importProcedures; - - /** - * Pattern for stored procedures to import from DB. This is only meaningful - * if <code>importProcedures</code> is set to <code>true</code>. - */ - private String procedurePattern; - private String meaningfulPkTables; /** - * Java class implementing org.apache.cayenne.map.naming.NamingStrategy. - * This is used to specify how ObjEntities will be mapped from the imported - * DB schema. - */ - private String namingStrategy; - - /** * Java class implementing org.apache.cayenne.dba.DbAdapter. This attribute * is optional, the default is AutoAdapter, i.e. Cayenne would try to guess * the DB type. */ private String adapter; - /** - * A class of JDBC driver to use for the target database. - */ - private String driver; + private boolean usePrimitives; - /** - * JDBC connection URL of a target database. - */ - private String url; + private Log logger; - /** - * Database user name. - */ - private String username; + private final DataSourceInfo dataSourceInfo = new DataSourceInfo(); /** - * Database user password. + * DB schema to use for DB importing. */ - private String password; - - private String includeTables; - private String excludeTables; - - private boolean usePrimitives; - - private Log logger; + private final DbLoaderConfiguration dbLoaderConfiguration = new DbLoaderConfiguration(); public Log getLogger() { return logger; @@ -166,52 +124,12 @@ public class DbImportConfiguration { this.overwrite = overwrite; } - public String getCatalog() { - return dbLoaderConfiguration.getCatalog(); - } - - public void setCatalog(String catalog) { - this.dbLoaderConfiguration.setCatalog(catalog); - } - - public void setSchema(String schema) { - dbLoaderConfiguration.setSchema(schema); - } - - public String getSchema() { - return dbLoaderConfiguration.getSchema(); - } - - public String getTablePattern() { - return tablePattern; - } - - public void setTablePattern(String tablePattern) { - this.tablePattern = tablePattern; - } - - public boolean isImportProcedures() { - return importProcedures; - } - - public void setImportProcedures(boolean importProcedures) { - this.importProcedures = importProcedures; - } - - public String getProcedurePattern() { - return procedurePattern; - } - - public void setProcedurePattern(String procedurePattern) { - this.procedurePattern = procedurePattern; - } - public String getNamingStrategy() { - return namingStrategy; + return dbLoaderConfiguration.getNamingStrategy(); } public void setNamingStrategy(String namingStrategy) { - this.namingStrategy = namingStrategy; + dbLoaderConfiguration.setNamingStrategy(namingStrategy); } public String getAdapter() { @@ -222,54 +140,6 @@ public class DbImportConfiguration { this.adapter = adapter; } - public String getDriver() { - return driver; - } - - public void setDriver(String driver) { - this.driver = driver; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getIncludeTables() { - return includeTables; - } - - public void setIncludeTables(String includeTables) { - this.includeTables = includeTables; - } - - public String getExcludeTables() { - return excludeTables; - } - - public void setExcludeTables(String excludeTables) { - this.excludeTables = excludeTables; - } - /** * Returns a comma-separated list of Perl5 regular expressions that match * table names for which {@link DbImportAction} should include ObjAttribute @@ -294,19 +164,14 @@ public class DbImportConfiguration { public DbLoader createLoader(DbAdapter adapter, Connection connection, DbLoaderDelegate loaderDelegate) throws InstantiationException, IllegalAccessException, ClassNotFoundException { - final NamePatternMatcher nameFilter = new NamePatternMatcher(logger, getIncludeTables(), getExcludeTables()); - final NamePatternMatcher meaningfulPkFilter = new NamePatternMatcher(logger, getMeaningfulPkTables(), + final NameFilter meaningfulPkFilter = NamePatternMatcher.build(logger, getMeaningfulPkTables(), getMeaningfulPkTables() != null ? null : "*"); DbLoader loader = new DbLoader(connection, adapter, loaderDelegate) { - @Override - public boolean includeTableName(String tableName) { - return nameFilter.isIncluded(tableName); - } @Override protected EntityMergeSupport createEntityMerger(DataMap map) { - EntityMergeSupport emSupport = new EntityMergeSupport(map, nameGenerator, true) { + EntityMergeSupport emSupport = new EntityMergeSupport(map, getNameGenerator(), true) { @Override protected boolean removePK(DbEntity dbEntity) { @@ -329,20 +194,42 @@ public class DbImportConfiguration { return loader; } - public DataSourceInfo createDataSourceInfo() { - DataSourceInfo dataSourceInfo = new DataSourceInfo(); - dataSourceInfo.setDataSourceUrl(getUrl()); - dataSourceInfo.setJdbcDriver(getDriver()); - dataSourceInfo.setUserName(getUsername()); - dataSourceInfo.setPassword(getPassword()); + public void setDriver(String jdbcDriver) { + dataSourceInfo.setJdbcDriver(jdbcDriver); + } + + public String getDriver() { + return dataSourceInfo.getJdbcDriver(); + } - return dataSourceInfo; + public void setPassword(String password) { + dataSourceInfo.setPassword(password); + } + + public String getPassword() { + return dataSourceInfo.getPassword(); + } + + public void setUsername(String userName) { + dataSourceInfo.setUserName(userName); + } + + public String getUsername() { + return dataSourceInfo.getUserName(); + } + + public void setUrl(String dataSourceUrl) { + dataSourceInfo.setDataSourceUrl(dataSourceUrl); + } + + public String getUrl() { + return dataSourceInfo.getDataSourceUrl(); } public DataNodeDescriptor createDataNodeDescriptor() { DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor(); nodeDescriptor.setAdapterType(getAdapter()); - nodeDescriptor.setDataSourceDescriptor(createDataSourceInfo()); + nodeDescriptor.setDataSourceDescriptor(dataSourceInfo); return nodeDescriptor; } @@ -352,14 +239,11 @@ public class DbImportConfiguration { throw new NullPointerException("Null DataMap File."); } - String name = dataMapFile.getName(); - if (!name.endsWith(DATA_MAP_LOCATION_SUFFIX)) { - throw new CayenneRuntimeException("DataMap file name must end with '%s': '%s'", DATA_MAP_LOCATION_SUFFIX, - name); - } + return initializeDataMap(new DataMap()); + } - String dataMapName = name.substring(0, name.length() - DATA_MAP_LOCATION_SUFFIX.length()); - DataMap dataMap = new DataMap(dataMapName); + public DataMap initializeDataMap(DataMap dataMap) throws MalformedURLException { + dataMap.setName(getDataMapName()); dataMap.setConfigurationSource(new URLResource(dataMapFile.toURI().toURL())); dataMap.setNamespace(new EntityResolver(Collections.singleton(dataMap))); @@ -372,25 +256,37 @@ public class DbImportConfiguration { dataMap.setDefaultPackage(defaultPackage); } - // do not override default catalog of existing DataMap unless it is - // explicitly requested by the plugin caller, and the provided catalog is - // not a pattern - String catalog = getCatalog(); - if (isNotEmpty(catalog) && catalog.indexOf('%') < 0) { - dataMap.setDefaultCatalog(catalog); - } + List<DbPath> dbPaths = dbLoaderConfiguration.getFiltersConfig().getDbPaths(); + if (!dbPaths.isEmpty()) { + // do not override default catalog of existing DataMap unless it is + // explicitly requested by the plugin caller, and the provided catalog is + // not a pattern + String catalog = dbPaths.get(0).catalog; + if (isNotEmpty(catalog) && catalog.indexOf('%') < 0) { + dataMap.setDefaultCatalog(catalog); + } - // do not override default schema of existing DataMap unless it is - // explicitly requested by the plugin caller, and the provided schema is - // not a pattern - String schema = getSchema(); - if (isNotEmpty(schema) && schema.indexOf('%') < 0) { - dataMap.setDefaultSchema(schema); + // do not override default schema of existing DataMap unless it is + // explicitly requested by the plugin caller, and the provided schema is + // not a pattern + String schema = dbPaths.get(0).schema; + if (isNotEmpty(schema) && schema.indexOf('%') < 0) { + dataMap.setDefaultSchema(schema); + } } return dataMap; } + public String getDataMapName() { + String name = dataMapFile.getName(); + if (!name.endsWith(DATA_MAP_LOCATION_SUFFIX)) { + throw new CayenneRuntimeException("DataMap file name must end with '%s': '%s'", DATA_MAP_LOCATION_SUFFIX, + name); + } + return name.substring(0, name.length() - DATA_MAP_LOCATION_SUFFIX.length()); + } + public ModelMergeDelegate createMergeDelegate() { return new DefaultModelMergeDelegate(); } @@ -399,19 +295,25 @@ public class DbImportConfiguration { return new DefaultDbLoaderDelegate(); } - public void log() { - logger.debug("Importer options - map: " + getDataMapFile()); - logger.debug("Importer options - overwrite: " + isOverwrite()); - logger.debug("Importer options - adapter: " + getAdapter()); - logger.debug("Importer options - catalog: " + getCatalog()); - logger.debug("Importer options - schema: " + getSchema()); - logger.debug("Importer options - defaultPackage: " + getDefaultPackage()); - logger.debug("Importer options - tablePattern: " + getTablePattern()); - logger.debug("Importer options - importProcedures: " + isImportProcedures()); - logger.debug("Importer options - procedurePattern: " + getProcedurePattern()); - logger.debug("Importer options - meaningfulPkTables: " + getMeaningfulPkTables()); - logger.debug("Importer options - namingStrategy: " + getNamingStrategy()); - logger.debug("Importer options - includeTables: " + getIncludeTables()); - logger.debug("Importer options - excludeTables: " + getExcludeTables()); + public DbLoaderConfiguration getDbLoaderConfig() { + return dbLoaderConfiguration; + } + + public void setFiltersConfig(FiltersConfig filtersConfig) { + dbLoaderConfiguration.setFiltersConfig(filtersConfig); + } + + @Override + public String toString() { + StringBuilder res = new StringBuilder("Importer options:"); + for (String line : dbLoaderConfiguration.toString().split("\n")) { + res.append(" ").append(line).append("\n"); + } + + return res.toString(); + } + + public DataSourceInfo getDataSourceInfo() { + return dataSourceInfo; } } http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/AntNestedElement.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/AntNestedElement.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/AntNestedElement.java new file mode 100644 index 0000000..fad83d1 --- /dev/null +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/AntNestedElement.java @@ -0,0 +1,38 @@ +/***************************************************************** + * 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.tools.dbimport.config; + +/** + * Additional class to handle <name> element under <catalog> and <schema> + * required for ant configuration + * + * @since 3.2. + */ +public class AntNestedElement { + + private String name; + + public String getName() { + return name; + } + + public void addText(String str) { + name = str; + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/Catalog.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/Catalog.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/Catalog.java new file mode 100644 index 0000000..8a01502 --- /dev/null +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/Catalog.java @@ -0,0 +1,98 @@ +/***************************************************************** + * 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.tools.dbimport.config; + +import java.util.Collection; +import java.util.LinkedList; + +/** + * @since 3.2. + */ +public class Catalog extends FilterContainer { + + private String name; + + private Collection<Schema> schemas = new LinkedList<Schema>(); + + public Catalog() { + } + + public Catalog(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Collection<Schema> getSchemas() { + return schemas; + } + + public void setSchemas(Collection<Schema> schemas) { + this.schemas = schemas; + } + + public void addSchema(Schema schema) { + this.schemas.add(schema); + } + + public void set(String name) { + setName(name); + } + + public void addConfiguredName(AntNestedElement name) { + setName(name.getName()); + } + + public void addText(String name) { + if (name.trim().isEmpty()) { + return; + } + + setName(name); + } + + public Catalog schema(Schema name) { + addSchema(name); + return this; + } + + @Override + public boolean isEmptyContainer() { + if (!super.isEmptyContainer()) { + return false; + } + + if (schemas.isEmpty()) { + return true; + } + + for (Schema schema : schemas) { + if (!schema.isEmptyContainer()) { + return false; + } + } + return true; + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/DefaultReverseEngineeringLoader.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/DefaultReverseEngineeringLoader.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/DefaultReverseEngineeringLoader.java new file mode 100644 index 0000000..22ed0a6 --- /dev/null +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/DefaultReverseEngineeringLoader.java @@ -0,0 +1,212 @@ +/***************************************************************** + * 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.tools.dbimport.config; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.cayenne.tools.ExcludeTable; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.Element; + +import org.apache.cayenne.CayenneRuntimeException; +import org.apache.cayenne.resource.Resource; +import org.xml.sax.SAXException; + +import java.io.IOException; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +/** + * @since 3.2. + */ +public class DefaultReverseEngineeringLoader implements ReverseEngineeringLoader { + + private static final Log LOG = LogFactory.getLog(ReverseEngineeringLoader.class); + + @Override + public ReverseEngineering load(Resource configurationResource) throws CayenneRuntimeException { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + + try { + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(configurationResource.getURL().openStream()); + + ReverseEngineering engineering = new ReverseEngineering(); + + Element root = doc.getDocumentElement(); + engineering.setCatalogs(loadCatalogs(root)); + engineering.setSchemas(loadSchemas(root)); + engineering.setIncludeTables(loadIncludeTables(root)); + engineering.setExcludeTables(loadExcludeTables(root)); + engineering.setIncludeColumns(loadIncludeColumns(root)); + engineering.setExcludeColumns(loadExcludeColumns(root)); + engineering.setIncludeProcedures(loadIncludeProcedures(root)); + engineering.setExcludeProcedures(loadExcludeProcedures(root)); + + return engineering; + } catch (ParserConfigurationException e) { + LOG.info(e.getMessage(), e); + } catch (SAXException e) { + LOG.info(e.getMessage(), e); + } catch (IOException e) { + LOG.info(e.getMessage(), e); + } + + + return null; + } + + private Collection<ExcludeProcedure> loadExcludeProcedures(Node parent) { + return loadPatternParams(ExcludeProcedure.class, getElementsByTagName(parent, "excludeProcedure")); + } + + private Collection<IncludeProcedure> loadIncludeProcedures(Node parent) { + return loadPatternParams(IncludeProcedure.class, getElementsByTagName(parent, "includeProcedure")); + } + + private Collection<ExcludeColumn> loadExcludeColumns(Node parent) { + return loadPatternParams(ExcludeColumn.class, getElementsByTagName(parent, "excludeColumn")); + } + + private Collection<IncludeColumn> loadIncludeColumns(Node parent) { + return loadPatternParams(IncludeColumn.class, getElementsByTagName(parent, "includeColumn")); + } + + private Collection<ExcludeTable> loadExcludeTables(Node parent) { + return loadPatternParams(ExcludeTable.class, getElementsByTagName(parent, "excludeTable")); + } + + private Collection<IncludeTable> loadIncludeTables(Node parent) { + List<Node> includeTables = getElementsByTagName(parent, "includeTable"); + Collection<IncludeTable> res = new LinkedList<IncludeTable>(); + for (Node node : includeTables) { + IncludeTable includeTable = new IncludeTable(); + + includeTable.setPattern(loadPattern(node)); + includeTable.setIncludeColumns(loadIncludeColumns(node)); + includeTable.setExcludeColumns(loadExcludeColumns(node)); + res.add(includeTable); + } + return res; + } + + private Collection<Schema> loadSchemas(Node parent) { + List<Node> schemas = getElementsByTagName(parent, "schema"); + Collection<Schema> res = new LinkedList<Schema>(); + for (Node schemaNode : schemas) { + Schema schema = new Schema(); + + schema.setName(loadName(schemaNode)); + schema.setIncludeTables(loadIncludeTables(schemaNode)); + schema.setExcludeTables(loadExcludeTables(schemaNode)); + schema.setIncludeColumns(loadIncludeColumns(schemaNode)); + schema.setExcludeColumns(loadExcludeColumns(schemaNode)); + schema.setIncludeProcedures(loadIncludeProcedures(schemaNode)); + schema.setExcludeProcedures(loadExcludeProcedures(schemaNode)); + res.add(schema); + } + + return res; + } + + private Collection<Catalog> loadCatalogs(Node parent) { + List<Node> catalogs = getElementsByTagName(parent, "catalog"); + Collection<Catalog> res = new LinkedList<Catalog>(); + for (Node catalogNode : catalogs) { + Catalog catalog = new Catalog(); + + catalog.setName(loadName(catalogNode)); + catalog.setSchemas(loadSchemas(catalogNode)); + catalog.setIncludeTables(loadIncludeTables(catalogNode)); + catalog.setExcludeTables(loadExcludeTables(catalogNode)); + catalog.setIncludeColumns(loadIncludeColumns(catalogNode)); + catalog.setExcludeColumns(loadExcludeColumns(catalogNode)); + catalog.setIncludeProcedures(loadIncludeProcedures(catalogNode)); + catalog.setExcludeProcedures(loadExcludeProcedures(catalogNode)); + + res.add(catalog); + } + + return res; + } + + private String loadName(Node catalogNode) { + return loadByName(catalogNode, "name"); + } + + private String loadPattern(Node catalogNode) { + return loadByName(catalogNode, "pattern"); + } + + private String loadByName(Node node, String attrName) { + Node name = node.getAttributes().getNamedItem(attrName); + if (name != null) { + return name.getTextContent(); + } + + String content = node.getTextContent().trim(); + if (!content.isEmpty()) { + return content; + } + + List<Node> names = getElementsByTagName(node, attrName); + if (names.isEmpty()) { + return null; + } + + return names.get(0).getTextContent(); + } + + private <T extends PatternParam> Collection<T> loadPatternParams(Class<T> clazz, List<Node> nodes) { + Collection<T> res = new LinkedList<T>(); + for (Node node : nodes) { + try { + T obj = clazz.newInstance(); + obj.setPattern(loadPattern(node)); + + res.add(obj); + } catch (InstantiationException e) { + LOG.info(e.getMessage(), e); + } catch (IllegalAccessException e) { + LOG.info(e.getMessage(), e); + } + } + return res; + } + + private List<Node> getElementsByTagName(Node catalogNode, String name) { + List<Node> nodes = new LinkedList<Node>(); + NodeList childNodes = catalogNode.getChildNodes(); + for (int i = 0; i < childNodes.getLength(); i++) { + Node item = childNodes.item(i); + if (name.equals(item.getNodeName())) { + nodes.add(item); + } + } + + return nodes; + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/DefaultTypeMapperBuilder.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/DefaultTypeMapperBuilder.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/DefaultTypeMapperBuilder.java new file mode 100644 index 0000000..df51c97 --- /dev/null +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/DefaultTypeMapperBuilder.java @@ -0,0 +1,82 @@ +/***************************************************************** + * 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.tools.dbimport.config; + +import org.apache.cayenne.access.loader.mapper.DbType; +import org.apache.cayenne.access.loader.mapper.DefaultJdbc2JavaTypeMapper; +import org.apache.cayenne.access.loader.mapper.Jdbc2JavaTypeMapper; +import org.apache.commons.lang.ClassUtils; +import org.apache.commons.logging.Log; + +import static org.apache.commons.lang.StringUtils.isBlank; + +/** + * @since 3.2. + */ +public class DefaultTypeMapperBuilder { + + private final DefaultJdbc2JavaTypeMapper mapper; + private final Log logger; + + public DefaultTypeMapperBuilder(Log logger, TypeMapper typeMapper) { + this.logger = logger; + this.mapper = createMapper(typeMapper.getMapperClassName()); + + for (Type type : typeMapper.getTypes()) { + this.mapper.add(buildType(type), type.getJava()); + } + } + + private DbType buildType(Type type) { + return new DbType( + type.getJdbc(), + type.getLength(), + type.getPrecision(), + type.getScale(), + type.getNotNull() + ); + } + + private DefaultJdbc2JavaTypeMapper createMapper(String className) { + if (!isBlank(className)) { + try { + return (DefaultJdbc2JavaTypeMapper) ClassUtils.getClass(Thread.currentThread() + .getContextClassLoader(), className).newInstance(); + } catch (ClassNotFoundException e) { + logger.error("Can't load class '" + className + "': ", e); + } catch (InstantiationException e) { + logger.error("Can't instantiate '" + className + "' make sure it has default constructor.", e); + } catch (IllegalAccessException e) { + logger.error("Can't instantiate '" + className + "' make sure it has default constructor.", e); + } + } + + return new DefaultJdbc2JavaTypeMapper(); + } + + public DefaultTypeMapperBuilder setUsePrimitives(Boolean usePrimitives) { + mapper.setUsePrimitives(usePrimitives); + + return this; + } + + public Jdbc2JavaTypeMapper build() { + return mapper; + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/ExcludeColumn.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/ExcludeColumn.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/ExcludeColumn.java new file mode 100644 index 0000000..648645f --- /dev/null +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/ExcludeColumn.java @@ -0,0 +1,31 @@ +/***************************************************************** + * 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.tools.dbimport.config; + +/** + * @since 3.2. + */ +public class ExcludeColumn extends PatternParam { + public ExcludeColumn() { + } + + public ExcludeColumn(String pattern) { + super(pattern); + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/ExcludeProcedure.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/ExcludeProcedure.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/ExcludeProcedure.java new file mode 100644 index 0000000..ec8d5aa --- /dev/null +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/ExcludeProcedure.java @@ -0,0 +1,31 @@ +/***************************************************************** + * 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.tools.dbimport.config; + +/** + * @since 3.2. + */ +public class ExcludeProcedure extends PatternParam { + public ExcludeProcedure() { + } + + public ExcludeProcedure(String pattern) { + super(pattern); + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/FilterContainer.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/FilterContainer.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/FilterContainer.java new file mode 100644 index 0000000..ff9ddd7 --- /dev/null +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/FilterContainer.java @@ -0,0 +1,116 @@ +/***************************************************************** + * 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.tools.dbimport.config; + +import org.apache.cayenne.tools.ExcludeTable; + +import java.util.Collection; +import java.util.LinkedList; + +/** + * @since 3.2. + */ +public class FilterContainer { + + private Collection<IncludeTable> includeTables = new LinkedList<IncludeTable>(); + private Collection<ExcludeTable> excludeTables = new LinkedList<ExcludeTable>(); + private Collection<IncludeColumn> includeColumns = new LinkedList<IncludeColumn>(); + private Collection<ExcludeColumn> excludeColumns = new LinkedList<ExcludeColumn>(); + private Collection<IncludeProcedure> includeProcedures = new LinkedList<IncludeProcedure>(); + private Collection<ExcludeProcedure> excludeProcedures = new LinkedList<ExcludeProcedure>(); + + public Collection<IncludeTable> getIncludeTables() { + return includeTables; + } + + public void setIncludeTables(Collection<IncludeTable> includeTables) { + this.includeTables = includeTables; + } + + public Collection<ExcludeTable> getExcludeTables() { + return excludeTables; + } + + public void setExcludeTables(Collection<ExcludeTable> excludeTables) { + this.excludeTables = excludeTables; + } + + public Collection<IncludeColumn> getIncludeColumns() { + return includeColumns; + } + + public void setIncludeColumns(Collection<IncludeColumn> includeColumns) { + this.includeColumns = includeColumns; + } + + public Collection<ExcludeColumn> getExcludeColumns() { + return excludeColumns; + } + + public void setExcludeColumns(Collection<ExcludeColumn> excludeColumns) { + this.excludeColumns = excludeColumns; + } + + public Collection<IncludeProcedure> getIncludeProcedures() { + return includeProcedures; + } + + public void setIncludeProcedures(Collection<IncludeProcedure> includeProcedures) { + this.includeProcedures = includeProcedures; + } + + public Collection<ExcludeProcedure> getExcludeProcedures() { + return excludeProcedures; + } + + public void setExcludeProcedures(Collection<ExcludeProcedure> excludeProcedures) { + this.excludeProcedures = excludeProcedures; + } + + + public void addIncludeColumn(IncludeColumn includeColumn) { + this.includeColumns.add(includeColumn); + } + + public void addExcludeColumn(ExcludeColumn excludeColumn) { + this.excludeColumns.add(excludeColumn); + } + + public void addIncludeTable(IncludeTable includeTable) { + this.includeTables.add(includeTable); + } + + public void addExcludeTable(ExcludeTable excludeTable) { + this.excludeTables.add(excludeTable); + } + + public void addIncludeProcedure(IncludeProcedure includeProcedure) { + this.includeProcedures.add(includeProcedure); + } + + public void addExcludeProcedure(ExcludeProcedure excludeProcedure) { + this.excludeProcedures.add(excludeProcedure); + } + + public boolean isEmptyContainer() { + return includeColumns.isEmpty() && excludeColumns.isEmpty() + && includeTables.isEmpty() && excludeTables.isEmpty() + && includeProcedures.isEmpty() && excludeProcedures.isEmpty(); + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2474202/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/FiltersConfigBuilder.java ---------------------------------------------------------------------- diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/FiltersConfigBuilder.java b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/FiltersConfigBuilder.java new file mode 100644 index 0000000..7b5819d --- /dev/null +++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/config/FiltersConfigBuilder.java @@ -0,0 +1,171 @@ +/***************************************************************** + * 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.tools.dbimport.config; + +import static org.apache.cayenne.access.loader.filters.FilterFactory.NULL; +import static org.apache.cayenne.access.loader.filters.FilterFactory.TRUE; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +import org.apache.cayenne.access.loader.filters.DbPath; +import org.apache.cayenne.access.loader.filters.EntityFilters; +import org.apache.cayenne.access.loader.filters.Filter; +import org.apache.cayenne.access.loader.filters.FilterFactory; +import org.apache.cayenne.access.loader.filters.FiltersConfig; +import org.apache.cayenne.access.loader.filters.ListFilter; + +/** +* @since 3.2. +*/ +public final class FiltersConfigBuilder { + + private final ReverseEngineering engineering; + private final List<EntityFilters> filters = new LinkedList<EntityFilters>(); + + public FiltersConfigBuilder(ReverseEngineering engineering) { + this.engineering = engineering; + } + + public FiltersConfigBuilder add(EntityFilters filter) { + if (!filter.isEmpty()) { + this.filters.add(filter); + } else if (!filter.getDbPath().equals(new DbPath())) { + this.filters.add(defaultFilter(filter.getDbPath())); + } + + return this; + } + + public FiltersConfig filtersConfig() { + DbPath path = new DbPath(); + + filters.addAll(processFilters(path, engineering)); + filters.addAll(processSchemas(path, engineering.getSchemas())); + filters.addAll(processCatalog(path, engineering.getCatalogs())); + + if (filters.isEmpty()) { + filters.add(defaultFilter(path)); + } + return new FiltersConfig(filters); + } + + private EntityFilters defaultFilter(DbPath path) { + return new EntityFilters(path, TRUE, TRUE, NULL); + } + + private Collection<? extends EntityFilters> processSchemas(DbPath root, Collection<Schema> schemas) { + List<EntityFilters> filters = new LinkedList<EntityFilters>(); + for (Schema schema : schemas) { + DbPath path = new DbPath(root.catalog, schema.getName()); + List<EntityFilters> schemaFilters = processFilters(path, schema); + if (schemaFilters.isEmpty()) { + schemaFilters.add(defaultFilter(path)); + } + + filters.addAll(schemaFilters); + } + + return filters; + } + + private Collection<? extends EntityFilters> processCatalog(DbPath root, Collection<Catalog> catalogs) { + List<EntityFilters> filters = new LinkedList<EntityFilters>(); + for (Catalog catalog: catalogs) { + DbPath path = new DbPath(catalog.getName()); + + List<EntityFilters> catalogFilters = new LinkedList<EntityFilters>(); + catalogFilters.addAll(processFilters(path, catalog)); + catalogFilters.addAll(processSchemas(path, catalog.getSchemas())); + + if (catalogFilters.isEmpty()) { + catalogFilters.add(defaultFilter(path)); + } + + filters.addAll(catalogFilters); + } + + return filters; + } + + private List<EntityFilters> processFilters(DbPath root, FilterContainer container) { + LinkedList<EntityFilters> res = new LinkedList<EntityFilters>(); + res.addAll(processTableFilters(root, container.getIncludeTables())); + + EntityFilters filter = new EntityFilters( + root, + processIncludes(container.getIncludeTables()).join(processExcludes(container.getExcludeTables())), + processIncludes(container.getIncludeColumns()).join(processExcludes(container.getExcludeColumns())), + processIncludes(container.getIncludeProcedures()).join(processExcludes(container.getExcludeProcedures())) + ); + + if (!filter.isEmpty()) { + res.add(filter); + } + + return res; + } + + private List<EntityFilters> processTableFilters(DbPath root, Collection<IncludeTable> tables) { + List<EntityFilters> list = new LinkedList<EntityFilters>(); + for (IncludeTable includeTable : tables) { + Filter<String> filter = TRUE + .join(processIncludes(includeTable.getIncludeColumns())) + .join(processExcludes(includeTable.getExcludeColumns())); + + DbPath dbPath = new DbPath(root.catalog, root.schema, includeTable.getPattern()); + list.add(new EntityFilters(dbPath, NULL, filter, NULL)); + } + return list; + } + + private Filter<String> processIncludes(Collection<? extends PatternParam> filters) { + return processFilters("include", filters); + } + + private Filter<String> processExcludes(Collection<? extends PatternParam> excludeProcedures) { + return processFilters("exclude", excludeProcedures); + } + + private Filter<String> processFilters(String factoryMethodName, Collection<? extends PatternParam> includeProcedures) { + Method factoryMethod; + try { + factoryMethod = FilterFactory.class.getMethod(factoryMethodName, String.class); + } catch (NoSuchMethodException e) { + throw new IllegalStateException(e); + } + + Collection<Filter<String>> filters = new LinkedList<Filter<String>>(); + for (PatternParam includeProcedure : includeProcedures) { + try { + filters.add((Filter<String>) factoryMethod.invoke(FilterFactory.class, includeProcedure.getPattern())); + } catch (Exception e) { + // TODO log / process exact parsing exception + e.printStackTrace(); + } + } + + if (filters.isEmpty()) { + return NULL; + } + return new ListFilter<String>(filters); + } +}