class Blufin::YmlJavaMetaWriter

Constants

META_DATA
PACKAGE
SERVICE

Public Class Methods

new(site, schema_data, schema_resources) click to toggle source

@return void

# File lib/core/yml_writers/yml_java_meta_writer.rb, line 10
def initialize(site, schema_data, schema_resources)

    @schema_data      = schema_data
    @schema_resources = schema_resources

    raise RuntimeError, 'Could not find valid @schema_data.' if @schema_data.nil? || !@schema_data.is_a?(Hash)
    raise RuntimeError, 'Could not find valid @schema_resources.' if @schema_resources.nil? || !@schema_resources.is_a?(Hash)

    @site          = Blufin::SiteResolver::validate_site(site)
    @site_name     = Blufin::SiteResolver::get_site_name(@site)
    @site_domain   = Blufin::SiteResolver::get_site_domain(@site)
    @site_location = "#{Blufin::SiteResolver::get_site_location(@site)}/"

    @embedded_data = Blufin::SiteEmbedded::get_data
    @embedded_path = "#{Blufin::Config::get_path('Paths', 'BlufinJava')}/#{Blufin::ScannerJavaEmbeddedObjects::PATH_TO_EMBEDDED}"

    @yml_enum_scanner = Blufin::ScannerJavaEnums.new(@site)

    # Wipe out all previous files.
    Blufin::YmlSchemaValidator::VALID_SCHEMAS_GENERATE.each do |schema|
        paths_to_wipe_out = %W(#{get_java_path(@site, schema, SERVICE, PACKAGE)})
        paths_to_wipe_out.each do |path_to_wipe_out|
            if Blufin::Files::path_exists(path_to_wipe_out)
                if Blufin::Files::get_files_in_dir(path_to_wipe_out).any?
                    Blufin::Terminal::command('rm *', path_to_wipe_out, false, false)
                end
            end
        end
    end

end

Public Instance Methods

write() click to toggle source

@return void

# File lib/core/yml_writers/yml_java_meta_writer.rb, line 43
def write

    @schema_data.each do |schema, schema_data|

        schema_data.each do |(table, table_data)|

            if table_data.is_a?(Hash) && table_data.length > 0

                @import_statements = ['import org.blufin.sdk.base.AbstractMetaData;', 'import org.blufin.base.enums.DataType;']
                @child_type        = nil

                consts = []
                fields = []

                table_data.each do |column_name, column_data|

                    @type                 = nil
                    @flag                 = nil
                    @fkey                 = nil
                    @link                 = nil
                    @encrypted            = nil
                    @max_length           = nil
                    @transient            = nil
                    @child_of             = nil
                    @description          = nil
                    @required             = nil
                    @required_if          = nil
                    @decimal_distribution = nil
                    @enum_name            = nil
                    @enum_values          = nil

                    # Handle Placeholders
                    if column_name =~ /\A(#{Blufin::YmlSchemaValidator::VALID_SCHEMAS_REGEX})\.[a-z_]+\z/
                        column_name_dup = "#{column_name.dup.gsub('[]', '').split('.')[1]}"
                        consts << "    public static final String FIELD_#{column_name_dup.upcase} = \"#{column_name_dup}\";"
                        fields << "        metaData.put(FIELD_#{column_name_dup.upcase}, new HashMap<String, Object>() {{"
                    elsif column_name =~ /\A(#{Blufin::YmlSchemaValidator::VALID_SCHEMAS_REGEX})\.[a-z_]+\[\]\z/
                        column_name_dup = "#{column_name.dup.gsub('[]', '').split('.')[1]}_list"
                        consts << "    public static final String FIELD_#{column_name_dup.upcase} = \"#{column_name_dup}\";"
                        fields << "        metaData.put(FIELD_#{column_name_dup.upcase}, new HashMap<String, Object>() {{"
                    elsif column_name =~ /\A[a-z_.]+\[#{Blufin::YmlSchemaValidator::LINK}\]/
                        column_name_dup = "#{column_name.dup.gsub("[#{Blufin::YmlSchemaValidator::LINK}]", '').split('.')[1]}_list"
                        consts << "    public static final String FIELD_#{column_name_dup.upcase} = \"#{column_name_dup}\";"
                        fields << "        metaData.put(FIELD_#{column_name_dup.upcase}, new HashMap<String, Object>() {{"
                    else
                        consts << "    public static final String FIELD_#{column_name.upcase} = \"#{column_name}\";"
                        fields << "        metaData.put(FIELD_#{column_name.upcase}, new HashMap<String, Object>() {{"
                    end

                    unless column_data.nil?

                        column_data.each do |key, value|

                            case key.to_s
                                when Blufin::YmlSchemaValidator::DESCRIPTION
                                    handle_description(value)
                                when Blufin::YmlSchemaValidator::TYPE
                                    handle_type(value, schema, table, column_name, column_data)
                                when Blufin::YmlSchemaValidator::FLAG
                                    handle_flag(value)
                                when Blufin::YmlSchemaValidator::FKEY
                                    handle_fkey(value)
                                when Blufin::YmlSchemaValidator::REQUIRED
                                    handle_required(schema, table, value, table_data)
                                when Blufin::YmlSchemaValidator::REQUIRED_IF
                                    handle_required_if(schema, table, value, table_data)
                                when Blufin::YmlSchemaValidator::ENCRYPTED
                                    handle_encrypted(value)
                                when Blufin::YmlSchemaValidator::TRANSIENT
                                    handle_transient(value)
                                when Blufin::YmlSchemaValidator::CHILD_OF
                                    handle_child_of(value)
                                when Blufin::YmlSchemaValidator::CHILD_TYPE # For constructor parameter(s).
                                    handle_child_type(value)
                                else
                                    raise RuntimeError, "Unrecognized column key in #{__FILE__}: #{key}"
                            end

                        end

                    end

                    # Handle fields that don't have a 'type' -- IE: Where type is automatically implied.
                    handle_type_where_implied(table, column_name)

                    # Check for critical errors...
                    raise RuntimeError, "A MetaData field would've rendered without a 'TYPE' \xe2\x86\x92 #{schema}.#{table} \xe2\x86\x92 #{column_name}" if @type.nil?

                    fields << @type unless @type.nil?
                    fields << @flag unless @flag.nil?
                    fields << @fkey unless @fkey.nil?
                    fields << @link unless @link.nil?
                    fields << @encrypted unless @encrypted.nil?
                    fields << @max_length unless @max_length.nil?
                    fields << @transient unless @transient.nil?
                    fields << @child_of unless @child_of.nil?
                    fields << @description unless @description.nil?
                    fields << @required unless @required.nil?
                    fields << @required_if unless @required_if.nil?
                    fields << @decimal_distribution unless @decimal_distribution.nil?
                    fields << @enum_name unless @enum_name.nil?
                    fields << @enum_values unless @enum_values.nil?
                    fields << "        }});\n"

                end

                hierarchy_result      = extract_hierarchy(schema, table, @schema_resources["#{schema}.#{table}"][:dependents], @schema_resources.keys)
                hierarchy             = hierarchy_result[0]
                hierarchy_nested_only = hierarchy_result[1]

                class_name     = "#{Blufin::Strings::snake_case_to_camel_case(table)}#{Blufin::YmlJavaMetaWriter::META_DATA}"
                content        = ["package #{get_package(@site, schema, PACKAGE, SERVICE)};", '']
                full_file_path = "#{get_java_path(@site, schema, SERVICE, PACKAGE)}/#{class_name}.java"

                # Generate the content. Shared method between Embedded and non-embedded writers.
                content        = generate_content(content, '', consts, fields, hierarchy, hierarchy_nested_only, schema, table, class_name, @import_statements, @child_type)

                # Write the file.
                write_file_java(full_file_path, content, schema == Blufin::YmlSchemaValidator::MOCK).gsub(@site_location, '')

            end

        end

    end

end