class Blufin::YmlMetaWriterBase
Constants
- CHILD_OF
- CURRENCY_CODE
- DECIMAL_DISTRIBUTION
- DESCRIPTION
- ENCRYPTED
- ENUM_NAME
- ENUM_VALUES
- FKEY
- FLAG
- FLAG_AUTO_INCREMENT
- FLAG_INDEX
- FLAG_NULLABLE
- FLAG_PRIMARY_KEY
- FLAG_UNIQUE
- LINK
- MAX_LENGTH
- REQUIRED
- REQUIRED_IF
- TRANSIENT
- TYPE
Public Instance Methods
extract_hierarchy(schema, table, dependents_array, schema_tables_array)
click to toggle source
This build the “hierarchy” Array in the MetaData constructors. schema_tables_array is an Array like [app.data_center, app.db, common.cron]… @return Array (of Data)
# File lib/core/yml/yml_meta_writer_base.rb, line 187 def extract_hierarchy(schema, table, dependents_array, schema_tables_array) hierarchy = ["\"#{table}\""] hierarchy_nested_only = [] unless dependents_array.nil? dependents_array.each do |dependent| ds_array = [] ds_current = '' ds_buffer = '' ds = dependent.split('_') ds_counter = 0 ds_levels = [] ds.each do |n| ds_current += "_#{n}" ds_current = ds_current.gsub(/^_/, '') ds_buffer += "_#{n}" ds_buffer = ds_buffer.gsub(/^_/, '') if schema_tables_array.include?("#{schema}.#{ds_current}") if ds_current =~ /^#{table}/ ds_counter = ds_counter + 1 ds_array << ds_buffer ds_levels << [ds_counter, ds_array.join('_')] ds_buffer = '' end end end hierarchy << "\"#{ds_array.join('/')}\"" hierarchy_nested_only << ds_array.join('_') end end hierarchy.uniq! hierarchy.sort! [hierarchy, hierarchy_nested_only] end
generate_content(content, embedded, consts, fields, hierarchy, hierarchy_nested_only, schema, table, class_name, import_statements, child_type)
click to toggle source
# File lib/core/yml/yml_meta_writer_base.rb, line 221 def generate_content(content, embedded, consts, fields, hierarchy, hierarchy_nested_only, schema, table, class_name, import_statements, child_type) parent = @schema_resources["#{schema}.#{table}"][:parent] parent = (parent.nil? || parent.strip == '') ? 'null' : "\"#{parent}\"" if import_statements.any? import_statements.uniq! content = content.push(*import_statements) content << '' end content << 'import java.util.HashMap;' content << 'import java.text.MessageFormat;' content << 'import lombok.Getter;' content << 'import org.blufin.base.enums.SchemaType;' content << '' content << "public final class #{class_name} extends Abstract#{Blufin::YmlJavaMetaWriter::META_DATA} {" content << '' content << ' @Getter' content << " private static final #{class_name} instance = new #{class_name}();" content << '' if consts.any? consts.each do |const| content << const end content << '' end content << " private #{class_name}() {" content << '' # Hacky-fix for test classes. DO NOT remove this or Java won't compile. constructor_schema = (schema == Blufin::YmlSchemaValidator::MOCK) ? 'null' : "SchemaType.#{schema.upcase}" content << " super(#{constructor_schema}, \"#{table}\", #{parent}, Arrays.asList(#{hierarchy.join(', ')}), #{child_type.nil? ? 'null' : child_type});" content << '' if fields.any? fields.each_with_index do |field, idx| if idx == fields.length - 1 content << field[0...-1] else content << field end end end content << ' }' if hierarchy_nested_only.any? content << '' content << ' @Override' content << ' public AbstractMetaData getNestedMetaData(String nestedTable) {' content << '' content << ' switch (nestedTable) {' content << '' hierarchy_nested_only.each do |n| content << " case \"#{n}\":" content << " return #{embedded}#{Blufin::Strings::snake_case_to_camel_case(n)}MetaData.getInstance();" content << '' end content << ' default:' content << " throw new RuntimeException(MessageFormat.format(\"#{embedded}#{class_name} doesn''t have nested metadata for table: {0}\", nestedTable));" content << ' }' content << ' }' else content << '' content << ' @Override' content << ' public AbstractMetaData getNestedMetaData(String nestedTable) {' content << '' content << " throw new RuntimeException(\"Nested metadata not available for #{embedded}#{class_name}. This method should never be called manually.\");" content << ' }' end content << '}' content end
handle_child_of(value)
click to toggle source
# File lib/core/yml/yml_meta_writer_base.rb, line 175 def handle_child_of(value) @child_of = " put(#{CHILD_OF}, \"#{value}\");" end
handle_child_type(value)
click to toggle source
# File lib/core/yml/yml_meta_writer_base.rb, line 179 def handle_child_type(value) raise RuntimeError, "@child_type is being set twice! Was originally: #{@child_type} , now being set to: #{value}" unless @child_type.nil? @child_type = value end
handle_description(value)
click to toggle source
# File lib/core/yml/yml_meta_writer_base.rb, line 26 def handle_description(value) @description = " put(#{DESCRIPTION}, \"#{value.gsub('"', '\"')}\");" if value != '' && !value.nil? end
handle_encrypted(value)
click to toggle source
# File lib/core/yml/yml_meta_writer_base.rb, line 166 def handle_encrypted(value) raise RuntimeError, "Encrypted value is something other than 'true' \xe2\x86\x92 #{value}" unless value == 'true' || value == true @encrypted = " put(#{ENCRYPTED}, true);" end
handle_fkey(value)
click to toggle source
# File lib/core/yml/yml_meta_writer_base.rb, line 137 def handle_fkey(value) @fkey = " put(#{FKEY}, \"#{value.to_s.gsub('"', '\"')}\");" end
handle_flag(value)
click to toggle source
# File lib/core/yml/yml_meta_writer_base.rb, line 116 def handle_flag(value) if value != '' && !value.nil? flags_array = [] flags_data = Blufin::YmlCommon::extract_flags(value)[0] flags_array << FLAG_AUTO_INCREMENT unless flags_data.auto_increment.nil? flags_array << FLAG_INDEX unless flags_data.index.nil? flags_array << FLAG_NULLABLE unless flags_data.nullable.nil? flags_array << FLAG_PRIMARY_KEY unless flags_data.primary_key.nil? flags_array << FLAG_UNIQUE unless flags_data.unique.nil? if flags_array.any? @import_statements << 'import java.util.Arrays;' @flag = " put(#{FLAG}, Arrays.asList(" flags_array.each do |flag| @flag += "#{flag}, " end @flag = @flag[0...-2] @flag += '));' end end end
handle_required(schema, table, value, table_data)
click to toggle source
# File lib/core/yml/yml_meta_writer_base.rb, line 141 def handle_required(schema, table, value, table_data) @required = " put(#{REQUIRED}, true);" end
handle_required_if(schema, table, value, table_data)
click to toggle source
# File lib/core/yml/yml_meta_writer_base.rb, line 145 def handle_required_if(schema, table, value, table_data) value_split = value.split('=') enum_value = table_data[value_split[0]][Blufin::YmlSchemaValidator::TYPE] if enum_value =~ /#{Blufin::YmlSchemaValidator::REGEX_ENUM}/ enum_class_name = "#{Blufin::Strings::snake_case_to_camel_case(table)}#{Blufin::Strings.snake_case_to_camel_case(value_split[0].dup)}" enum_package = get_package(@site, schema, 'enums', Blufin::SiteServices::SDK) elsif enum_value =~ /#{Blufin::YmlSchemaValidator::REGEX_ENUM_CUSTOM}/ enum_class_name = Blufin::Strings.snake_case_to_camel_case(value_split[0].dup) enum_package = get_package(@site, schema, 'enums', Blufin::SiteServices::SDK) elsif enum_value =~ /#{Blufin::YmlSchemaValidator::REGEX_ENUM_SYSTEM}/ enum_class_name = Blufin::YmlCommon::enum_name_extractor(enum_value) enum_package = Blufin::SiteServices::PACKAGE_SYSTEM_ENUMS else raise RuntimeError, "'#{enum_value}' doesn't match regex for ENUM, ENUM_CUSTOM or ENUM_SYSTEM." end @required_if = " put(#{REQUIRED_IF}, Pair.of(FIELD_#{value_split[0].upcase}, #{enum_class_name}.#{value_split[1].upcase}));" @import_statements << 'import org.blufin.base.helper.Pair;' @import_statements << "import #{enum_package}.#{enum_class_name};" end
handle_transient(value)
click to toggle source
# File lib/core/yml/yml_meta_writer_base.rb, line 171 def handle_transient(value) @transient = " put(#{TRANSIENT}, \"#{value[0]}.#{value[1]}\");" end
handle_type(value, schema, table, column_name, column_data)
click to toggle source
# File lib/core/yml/yml_meta_writer_base.rb, line 30 def handle_type(value, schema, table, column_name, column_data) converted_type = value max_length = nil if value == Blufin::YmlSchemaValidator::TYPE_BOOLEAN elsif Blufin::YmlSchemaValidator::DATETIME_TYPES.include?(value) if value == Blufin::YmlSchemaValidator::TYPE_DATETIME_INSERT || value == Blufin::YmlSchemaValidator::TYPE_DATETIME_UPDATE converted_type = 'DATETIME_INSERT' if value == Blufin::YmlSchemaValidator::TYPE_DATETIME_INSERT converted_type = 'DATETIME_UPDATE' if value == Blufin::YmlSchemaValidator::TYPE_DATETIME_UPDATE @transient = " put(#{TRANSIENT}, true);" end elsif value =~ Blufin::YmlSchemaValidator::REGEX_DECIMAL decimal_m, decimal_d = Blufin::YmlCommon::decimal_extract_values(value) converted_type = 'DECIMAL' # DECIMAL(2,2) => "0.25" => length: 4 characters. max_length = decimal_m + 1 + (decimal_m == decimal_d ? 1 : 0) # DECIMAL(4,2) => "42.25" => length: 5 characters. @import_statements << 'import org.blufin.base.helper.Pair;' @decimal_distribution = " put(#{DECIMAL_DISTRIBUTION}, Pair.of(#{decimal_m.to_i}, #{decimal_d.to_i}));" elsif Blufin::YmlSchemaValidator::INT_TYPES.include?(value) converted_type = 'INT_AUTO' if value == Blufin::YmlSchemaValidator::TYPE_INT_AUTO elsif value == Blufin::YmlSchemaValidator::TYPE_INT_TINY converted_type = 'INT_TINY' elsif value == Blufin::YmlSchemaValidator::TYPE_INT_SMALL converted_type = 'INT_SMALL' elsif value == Blufin::YmlSchemaValidator::TYPE_INT_BIG converted_type = 'INT_BIG' elsif Blufin::YmlSchemaValidator::TEXT_TYPES.include?(value) # Do nothing. elsif value =~ Blufin::YmlSchemaValidator::REGEX_VARCHAR converted_type = 'VARCHAR' max_length = Blufin::Strings::extract_using_regex(value, /\(\d+\)\z/, %w{( )}) elsif value =~ Blufin::YmlSchemaValidator::REGEX_ENUM enum_values = Blufin::YmlCommon::enum_value_extractor(value, @site) converted_type = 'ENUM' max_length = enum_values.max_by(&:length).length @import_statements << 'import java.util.Arrays;' @enum_name = " put(#{ENUM_NAME}, \"#{Blufin::Strings::snake_case_to_camel_case(table)}#{Blufin::Strings::snake_case_to_camel_case(column_name)}\");" @enum_values = " put(#{ENUM_VALUES}, Arrays.asList(\"#{enum_values.join('", "')}\"));" elsif value =~ Blufin::YmlSchemaValidator::REGEX_ENUM_CUSTOM custom_enum_name = Blufin::YmlCommon::enum_name_extractor(value) enum_values = @yml_enum_scanner.get_enum_custom_values_for(custom_enum_name) converted_type = 'ENUM_CUSTOM' max_length = enum_values.max_by(&:length).length @import_statements << "import #{get_package(@site, schema, 'enums', Blufin::SiteServices::SDK)}.#{custom_enum_name};" @enum_name = " put(#{ENUM_NAME}, \"#{custom_enum_name}\");" @enum_values = " put(#{ENUM_VALUES}, getEnumValues(#{custom_enum_name}.values()));" elsif value =~ Blufin::YmlSchemaValidator::REGEX_ENUM_SYSTEM system_enum_name = Blufin::YmlCommon::enum_name_extractor(value) enum_values = @yml_enum_scanner.get_enum_system_values_for(system_enum_name) converted_type = 'ENUM_SYSTEM' max_length = enum_values.max_by(&:length).length @import_statements << "import #{Blufin::SiteServices::PACKAGE_SYSTEM_ENUMS}.#{system_enum_name};" @enum_name = " put(#{ENUM_NAME}, \"#{system_enum_name}\");" @enum_values = " put(#{ENUM_VALUES}, getEnumValues(#{system_enum_name}.values()));" elsif value == Blufin::YmlSchemaValidator::TYPE_ENUM_SYSTEM # This only applies to Embedded Objects. enum_values = @yml_enum_scanner.get_enum_system_values_for(column_data[:type_java]) converted_type = 'ENUM_SYSTEM' max_length = enum_values.max_by(&:length).length @import_statements << "import #{Blufin::SiteServices::PACKAGE_SYSTEM_ENUMS}.#{column_data[:type_java]};" @enum_name = " put(#{ENUM_NAME}, \"#{column_data[:type_java]}\");" @enum_values = " put(#{ENUM_VALUES}, getEnumValues(#{column_data[:type_java]}.values()));" elsif [ Blufin::ScannerJavaEmbeddedObjects::OBJECT, Blufin::ScannerJavaEmbeddedObjects::OBJECT_LIST, Blufin::ScannerJavaEmbeddedObjects::OBJECT_LINK, ].include?(value) # Do Nothing. Fix for @Embedded Meta-Writer. else raise RuntimeError, "Unrecognized type in #{__FILE__}: #{value}" end @type = " put(#{TYPE}, DataType.#{converted_type});" @max_length = " put(#{MAX_LENGTH}, #{max_length.to_i});" unless max_length.nil? end
handle_type_where_implied(table, column_name)
click to toggle source
# File lib/core/yml/yml_meta_writer_base.rb, line 105 def handle_type_where_implied(table, column_name) if column_name =~ /\A(#{Blufin::YmlSchemaValidator::VALID_SCHEMAS_REGEX})\.[a-z_]+\z/ @type = " put(#{TYPE}, DataType.OBJECT);" elsif column_name =~ /\A(#{Blufin::YmlSchemaValidator::VALID_SCHEMAS_REGEX})\.[a-z_]+\[\]\z/ @type = " put(#{TYPE}, DataType.OBJECT_LIST);" elsif column_name =~ /\A[a-z_.]+\[#{Blufin::YmlSchemaValidator::LINK}\]/ @type = " put(#{TYPE}, DataType.OBJECT_LINK);" @link = " put(#{LINK}, \"#{Blufin::YmlCommon::get_link_table_name(table, column_name)}\");" end end