From 355ee727bea27297d1c9e166d69873a81778525d Mon Sep 17 00:00:00 2001 From: Brad Gonzales Date: Thu, 2 May 2013 17:39:36 -0500 Subject: [PATCH] Issue #1970 added check for Abstract Entity sql TABLE declarations in SchemaManager. Amend: Removed second create index pattern in SchemaManager and added a contains check instead. Added a StringUtil replace method. In StringUtil replace method, fixed increment of targetIndex before loop indexOf check. Change-Id: Ic3538db7298e295470afad2b6cf63ea9bf0fc5fd Former-commit-id: 1ed7ce737e6a8d42db2c70892fc2173b5000b44e --- .../raytheon/edex/db/purge/SchemaManager.java | 63 ++++--------------- .../common/blocks/RedbookBlockBuilder.java | 5 +- .../raytheon/uf/common/util/StringUtil.java | 24 +++++++ 3 files changed, 38 insertions(+), 54 deletions(-) diff --git a/edexOsgi/com.raytheon.edex.common/src/com/raytheon/edex/db/purge/SchemaManager.java b/edexOsgi/com.raytheon.edex.common/src/com/raytheon/edex/db/purge/SchemaManager.java index f73c8296dc..107318c0bf 100644 --- a/edexOsgi/com.raytheon.edex.common/src/com/raytheon/edex/db/purge/SchemaManager.java +++ b/edexOsgi/com.raytheon.edex.common/src/com/raytheon/edex/db/purge/SchemaManager.java @@ -43,6 +43,7 @@ import org.hibernate.AnnotationException; import com.raytheon.uf.common.dataplugin.PluginException; import com.raytheon.uf.common.serialization.ISerializableObject; import com.raytheon.uf.common.serialization.SerializableManager; +import com.raytheon.uf.common.util.StringUtil; import com.raytheon.uf.edex.core.EDEXUtil; import com.raytheon.uf.edex.core.props.PropertiesFactory; import com.raytheon.uf.edex.database.DatabasePluginProperties; @@ -67,8 +68,8 @@ import com.raytheon.uf.edex.database.plugin.PluginVersionDao; * 10/8/2008 1532 bphillip Initial checkin * 2/9/2009 1990 bphillip Fixed index creation * 03/20/09 njensen Implemented IPluginRegistryChanged - * Mar 02, 2013 1970 bgonzale Updated createIndexTableNamePattern to match text preceeding - * %TABLE%. + * Mar 02, 2013 1970 bgonzale Added check for abstract entities in sql index naming. + * Removed unused private method populateSchema. * * * @author bphillip @@ -86,6 +87,8 @@ public class SchemaManager implements IDatabasePluginRegistryChanged { */ private static final long pluginLockTimeOutMillis = 120000; + private static final String TABLE = "%TABLE%"; + /** The singleton instance */ private static SchemaManager instance; @@ -102,7 +105,7 @@ public class SchemaManager implements IDatabasePluginRegistryChanged { .compile("^create (?:table |index |sequence )(?:[A-Za-z_0-9]*\\.)?(.+?)(?: .*)?$"); private Pattern createIndexTableNamePattern = Pattern - .compile("^create index \\w*?%TABLE%.+? on (.+?) .*$"); + .compile("^create index %TABLE%.+? on (.+?) .*$"); /** * Gets the singleton instance @@ -128,52 +131,6 @@ public class SchemaManager implements IDatabasePluginRegistryChanged { .getEnvValue("PLUGINDIR"); } - private PluginSchema populateSchema(String pluginName, String database, - PluginSchema schema, List tableNames) { - List ddls = null; - - for (String sql : ddls) { - for (String table : tableNames) { - if (sql.startsWith("create table " + table.toLowerCase() + " ")) { - schema.addCreateSql(sql); - break; - } else if (sql.startsWith("drop table " + table.toLowerCase() - + ";")) { - sql = sql.replace("drop table ", "drop table if exists "); - schema.addDropSql(sql.replace(";", " cascade;")); - break; - } else if (sql.startsWith("create index") - && sql.contains(" on " + table.toLowerCase())) { - if (sql.contains("%TABLE%")) { - sql = sql.replaceFirst("%TABLE%", table.toLowerCase()); - } - String dropIndexSql = sql.replace("create index", - "drop index if exists"); - dropIndexSql = dropIndexSql.substring(0, - dropIndexSql.indexOf(" on ")) - + ";"; - sql = dropIndexSql + sql; - schema.addCreateSql(sql); - break; - } else if (sql.startsWith("alter table " + table.toLowerCase() - + " ") - && sql.contains(" drop ")) { - schema.addDropSql(sql); - break; - } else if (sql.startsWith("alter table " + table.toLowerCase() - + " ") - && sql.contains(" add ")) { - if (sql.contains("foreign key")) { - sql = sql.replace(";", " ON DELETE CASCADE;"); - } - schema.addCreateSql(sql); - break; - } - } - } - return schema; - } - /** * Runs all scripts for a particular plugin * @@ -349,8 +306,12 @@ public class SchemaManager implements IDatabasePluginRegistryChanged { if (sql.startsWith("create index")) { Matcher matcher = createIndexTableNamePattern.matcher(sql); if (matcher.matches()) { - createSql.set(i, - sql.replace("%TABLE%", matcher.group(1))); + createSql.set(i, StringUtil.replace(sql, TABLE, + matcher.group(1))); + } else if (sql.contains(TABLE)) { + // replace %TABLE% in sql statements with an empty + // string + createSql.set(i, StringUtil.replace(sql, TABLE, "")); } } } diff --git a/edexOsgi/com.raytheon.edex.plugin.redbook/src/com/raytheon/edex/plugin/redbook/common/blocks/RedbookBlockBuilder.java b/edexOsgi/com.raytheon.edex.plugin.redbook/src/com/raytheon/edex/plugin/redbook/common/blocks/RedbookBlockBuilder.java index 9038facfc5..d8e0eb8692 100644 --- a/edexOsgi/com.raytheon.edex.plugin.redbook/src/com/raytheon/edex/plugin/redbook/common/blocks/RedbookBlockBuilder.java +++ b/edexOsgi/com.raytheon.edex.plugin.redbook/src/com/raytheon/edex/plugin/redbook/common/blocks/RedbookBlockBuilder.java @@ -26,7 +26,6 @@ import java.util.Map; import java.util.Properties; import com.raytheon.edex.plugin.redbook.common.blocks.RedbookBlock.RedbookBlockFactory; -import com.raytheon.edex.plugin.redbook.decoder.RedbookFcstMap; import com.raytheon.uf.common.status.IUFStatusHandler; import com.raytheon.uf.common.status.UFStatus; import com.raytheon.uf.common.util.PropertiesUtil; @@ -55,8 +54,8 @@ import com.raytheon.uf.common.util.ReflectionUtil; * @version 1.0 */ public class RedbookBlockBuilder { - private static final transient IUFStatusHandler statusHandler = UFStatus - .getHandler(RedbookFcstMap.class); + private static final IUFStatusHandler statusHandler = UFStatus + .getHandler(RedbookBlockBuilder.class); private static final int MIN_REMAINING = 4; diff --git a/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/StringUtil.java b/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/StringUtil.java index abdf2af446..5a832113f1 100644 --- a/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/StringUtil.java +++ b/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/StringUtil.java @@ -38,6 +38,7 @@ import org.apache.commons.lang.StringUtils; * Oct 20, 2011 rferrel Initial creation * Jul 13, 2012 740 djohnson Add join. * Nov 09, 2012 1322 djohnson Add NEWLINE, createMessage. + * Mar 02, 2013 1970 bgonzale Added fast string replacement method. * * * @@ -185,4 +186,27 @@ public final class StringUtil { return msg.toString(); } + + /** + * Fast replacement of all String target elements in String source with + * String replacement. + * + * @param source + * String that instances will be replaced in. + * @param target + * @param replacement + * @return a new String equivalent to source with target Strings replaced by + * String replacement + */ + public static String replace(final String source, final String target, + final String replacement) { + int targetIndex = 0; + StringBuilder sb = new StringBuilder(source); + + while ((targetIndex = sb.indexOf(target, targetIndex)) > -1) { + sb.replace(targetIndex, targetIndex + target.length(), replacement); + targetIndex += replacement.length(); + } + return sb.toString(); + } }