Class: Blufin::YmlMetaWriterBase

Inherits:
YmlWriterBase show all
Defined in:
lib/core/yml/yml_meta_writer_base.rb

Constant Summary collapse

TYPE =
'TYPE'
MAX_LENGTH =
'MAX_LENGTH'
FLAG =
'FLAG'
FLAG_AUTO_INCREMENT =
'FLAG_AUTO_INCREMENT'
FLAG_INDEX =
'FLAG_INDEX'
FLAG_NULLABLE =
'FLAG_NULLABLE'
FLAG_PRIMARY_KEY =
'FLAG_PRIMARY_KEY'
FLAG_UNIQUE =
'FLAG_UNIQUE'
FKEY =
'FKEY'
'LINK'
DESCRIPTION =
'DESCRIPTION'
REQUIRED =
'REQUIRED'
REQUIRED_IF =
'REQUIRED_IF'
ENCRYPTED =
'ENCRYPTED'
TRANSIENT =
'TRANSIENT'
CHILD_OF =
'CHILD_OF'
CURRENCY_CODE =
'CURRENCY_CODE'
DECIMAL_DISTRIBUTION =
'DECIMAL_DISTRIBUTION'
ENUM_NAME =
'ENUM_NAME'
ENUM_VALUES =
'ENUM_VALUES'

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 YmlWriterBase

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

Instance Method Details

#extract_hierarchy(schema, table, dependents_array, schema_tables_array) ⇒ Object

This build the “hierarchy” Array in the MetaData constructors. schema_tables_array is an Array like [app.data_center, app.db, common.cron]…



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# 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) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# 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) ⇒ Object



175
176
177
# 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) ⇒ Object

Raises:

  • (RuntimeError)


179
180
181
182
# 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) ⇒ Object



26
27
28
# 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) ⇒ Object

Raises:

  • (RuntimeError)


166
167
168
169
# 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) ⇒ Object



137
138
139
# 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) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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) ⇒ Object



141
142
143
# 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) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# 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) ⇒ Object



171
172
173
# 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) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
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
# 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) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# 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