Class: Blufin::YmlJavaMetaWriter

Inherits:
YmlMetaWriterBase show all
Defined in:
lib/core/yml_writers/yml_java_meta_writer.rb

Constant Summary collapse

SERVICE =
Blufin::SiteServices::SDK_INTERNAL
PACKAGE =
'metadata'
META_DATA =
'MetaData'

Constants inherited from YmlMetaWriterBase

Blufin::YmlMetaWriterBase::CHILD_OF, Blufin::YmlMetaWriterBase::CURRENCY_CODE, Blufin::YmlMetaWriterBase::DECIMAL_DISTRIBUTION, Blufin::YmlMetaWriterBase::DESCRIPTION, Blufin::YmlMetaWriterBase::ENCRYPTED, Blufin::YmlMetaWriterBase::ENUM_NAME, Blufin::YmlMetaWriterBase::ENUM_VALUES, Blufin::YmlMetaWriterBase::FKEY, Blufin::YmlMetaWriterBase::FLAG, Blufin::YmlMetaWriterBase::FLAG_AUTO_INCREMENT, Blufin::YmlMetaWriterBase::FLAG_INDEX, Blufin::YmlMetaWriterBase::FLAG_NULLABLE, Blufin::YmlMetaWriterBase::FLAG_PRIMARY_KEY, Blufin::YmlMetaWriterBase::FLAG_UNIQUE, Blufin::YmlMetaWriterBase::LINK, Blufin::YmlMetaWriterBase::MAX_LENGTH, Blufin::YmlMetaWriterBase::REQUIRED, Blufin::YmlMetaWriterBase::REQUIRED_IF, Blufin::YmlMetaWriterBase::TRANSIENT, Blufin::YmlMetaWriterBase::TYPE

Constants inherited from YmlWriterBase

Blufin::YmlWriterBase::AUTO_TYPES, Blufin::YmlWriterBase::PLACEHOLDER_CLASS, Blufin::YmlWriterBase::PLACEHOLDER_IMPORT, Blufin::YmlWriterBase::PLACEHOLDER_PACKAGE, Blufin::YmlWriterBase::PLACEHOLDER_SCHEMA

Instance Method Summary collapse

Methods inherited from YmlMetaWriterBase

#extract_hierarchy, #generate_content, #handle_child_of, #handle_child_type, #handle_description, #handle_encrypted, #handle_fkey, #handle_flag, #handle_required, #handle_required_if, #handle_transient, #handle_type, #handle_type_where_implied

Methods inherited from YmlWriterBase

#get_base_path, #get_java_path, #get_package, #get_package_from_file, #write_file_java

Constructor Details

#initialize(site, schema_data, schema_resources) ⇒ Object

Returns void.

Raises:

  • (RuntimeError)

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

Instance Method Details

#writeObject

Returns void.

Returns:

  • void


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# 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