Class: Blufin::YmlJavaDtoWriter
- Inherits:
-
YmlWriterBase
- Object
- YmlWriterBase
- Blufin::YmlJavaDtoWriter
- Defined in:
- lib/core/yml_writers/yml_java_dto_writer.rb
Constant Summary collapse
- SERVICE =
Blufin::SiteServices::SDK_INTERNAL
- PACKAGE =
'dto'
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
-
#initialize(site, schema_data, schema_resources) ⇒ Object
constructor
Void.
-
#write ⇒ Object
noinspection RubyUnusedLocalVariable.
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.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/core/yml_writers/yml_java_dto_writer.rb', line 9 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)}/" # 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
#write ⇒ Object
noinspection RubyUnusedLocalVariable
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 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/core/yml_writers/yml_java_dto_writer.rb', line 37 def write @schema_data.each do |schema, schema_data| schema_data.each_with_index do |(table, table_data), idx| if table_data.is_a?(Hash) && table_data.length > 0 import_statements = [] instance_variables = [] special_getters = [] class_name = Blufin::Strings::snake_case_to_camel_case(table) table_data.each do |column_name, column_data| if column_name =~ /\A(#{Blufin::YmlSchemaValidator::VALID_SCHEMAS_REGEX})\.[a-z_]+\[\]\z/ || column_name =~ /\A(#{Blufin::YmlSchemaValidator::VALID_SCHEMAS_REGEX})\.[a-z_]+\z/ column_name_dup = column_name.dup.split('.')[1] field_camel_case = Blufin::Strings.snake_case_to_camel_case(column_name_dup) field_camel_case_lower = Blufin::Strings.snake_case_to_camel_case_lower(column_name_dup) elsif column_name =~ /\A[a-z_.]+\[#{Blufin::YmlSchemaValidator::LINK}\]/ column_name_dup = column_name.dup.gsub("[#{Blufin::YmlSchemaValidator::LINK}]", '[]').split('.')[1] field_camel_case = Blufin::Strings.snake_case_to_camel_case(column_name_dup) field_camel_case_lower = Blufin::Strings.snake_case_to_camel_case_lower(column_name_dup) else field_camel_case = Blufin::Strings.snake_case_to_camel_case(column_name.dup) field_camel_case_lower = Blufin::Strings.snake_case_to_camel_case_lower(column_name.dup) end # Handle Placeholders if column_name =~ /\A(#{Blufin::YmlSchemaValidator::VALID_SCHEMAS_REGEX})\.[a-z_]+\[\]\z/ || column_name =~ /\A[a-z_.]+\[#{Blufin::YmlSchemaValidator::LINK}\]/ field_camel_case = field_camel_case.gsub('[]', '') field_camel_case_lower = field_camel_case_lower.gsub('[]', '') instance_variables << " private List<#{field_camel_case}> #{field_camel_case_lower}List = new ArrayList<>();#{write_column_description(column_data)}" import_statements << 'import java.util.HashMap;' import_statements << 'import java.util.Map;' import_statements << 'import java.util.ArrayList;' import_statements << 'import java.util.List;' next elsif column_name =~ /\A(#{Blufin::YmlSchemaValidator::VALID_SCHEMAS_REGEX})\.[a-z_]+\z/ instance_variables << " private #{field_camel_case} #{field_camel_case_lower};#{write_column_description(column_data)}" next end special_instance_variable = [] type = column_data[Blufin::YmlSchemaValidator::TYPE] begin nullable = column_data[Blufin::YmlSchemaValidator::FLAG].split(' ').include?(Blufin::YmlSchemaValidator::FLAG_NULLABLE) rescue nullable = false end if type == Blufin::YmlSchemaValidator::TYPE_BOOLEAN import_statements << 'import lombok.AccessLevel;' import_statements << 'import lombok.Getter;' # special_instance_variable << '' unless instance_variables.last == '' special_instance_variable << ' @Getter(AccessLevel.NONE)' special_instance_variable << " private Boolean #{field_camel_case_lower};#{write_column_description(column_data)}" # special_instance_variable << '' special_getters << '' special_getters << " public Boolean is#{field_camel_case}() {" special_getters << '' special_getters << " return #{field_camel_case_lower};" special_getters << ' }' elsif type == Blufin::YmlSchemaValidator::TYPE_DATETIME type = 'ZonedDateTime' import_statements << 'import java.time.ZonedDateTime;' elsif [ Blufin::YmlSchemaValidator::TYPE_DATETIME_INSERT, Blufin::YmlSchemaValidator::TYPE_DATETIME_UPDATE ].include?(type) import_statements << 'import java.time.ZonedDateTime;' import_statements << 'import org.blufin.base.annotations.Transient;' # special_instance_variable << '' unless instance_variables.last == '' special_instance_variable << ' @Transient' special_instance_variable << " private ZonedDateTime #{field_camel_case_lower};" elsif type == Blufin::YmlSchemaValidator::TYPE_DATE type = 'LocalDate' import_statements << 'import java.time.LocalDate;' elsif type =~ Blufin::YmlSchemaValidator::REGEX_DECIMAL type = 'BigDecimal' import_statements << 'import java.math.BigDecimal;' elsif type == Blufin::YmlSchemaValidator::TYPE_INT_TINY type = 'Byte' elsif type == Blufin::YmlSchemaValidator::TYPE_INT_SMALL type = 'Short' elsif type == Blufin::YmlSchemaValidator::TYPE_INT_BIG type = 'Long' elsif [Blufin::YmlSchemaValidator::TYPE_INT, Blufin::YmlSchemaValidator::TYPE_INT_AUTO].include?(type) type = 'Integer' elsif [Blufin::YmlSchemaValidator::TYPE_TEXT, Blufin::YmlSchemaValidator::TYPE_TEXT_LONG].include?(type) type = 'String' elsif type == Blufin::ScannerJavaEmbeddedObjects::OBJECT import_statements << 'import org.blufin.base.annotations.Transient;' # special_instance_variable << '' unless instance_variables.last == '' special_instance_variable << ' @Transient' special_instance_variable << " private #{Blufin::Strings::snake_case_to_camel_case(column_data[Blufin::YmlSchemaValidator::TRANSIENT][1])} #{field_camel_case_lower};" # special_instance_variable << '' elsif type =~ Blufin::YmlSchemaValidator::REGEX_ENUM enum_class_name = "#{class_name}#{field_camel_case}" import_statements << "import #{get_package(@site, schema, Blufin::YmlJavaEnumWriter::PACKAGE_AUTO, SERVICE)}.#{enum_class_name};" type = enum_class_name elsif type =~ Blufin::YmlSchemaValidator::REGEX_ENUM_CUSTOM enum_class_name = Blufin::YmlCommon::enum_name_extractor(type) import_statements << "import #{get_package(@site, schema, Blufin::YmlJavaEnumWriter::PACKAGE_CUSTOM, SERVICE)}.#{enum_class_name};" type = enum_class_name elsif type =~ Blufin::YmlSchemaValidator::REGEX_ENUM_SYSTEM enum_class_name = Blufin::YmlCommon::enum_name_extractor(type) import_statements << "import #{Blufin::SiteServices::PACKAGE_SYSTEM_ENUMS}.#{enum_class_name};" type = enum_class_name elsif type =~ Blufin::YmlSchemaValidator::REGEX_VARCHAR type = 'String' else raise RuntimeError, "Unrecognized type in #{__FILE__}: #{type}" end if special_instance_variable.length > 0 special_instance_variable.each { |n| instance_variables << n } else instance_variables << " private #{type} #{field_camel_case_lower};#{write_column_description(column_data)}" end end import_statements.uniq! import_statements.sort! content = [ "package #{get_package(@site, schema, PACKAGE, SERVICE)};", 'import com.fasterxml.jackson.annotation.JsonIgnore;', 'import org.blufin.sdk.base.PersistentDto;' ] import_statements << 'import lombok.Data;' import_statements << 'import lombok.NoArgsConstructor;' if import_statements.any? content = content.push(*import_statements) content << '' end resource_hash = @schema_resources["#{schema}.#{table}"] resource_type = resource_hash[:type] parent_element = nil if resource_type == Blufin::YmlSchemaValidator::RESOURCE_TYPE_PARENT # DO NOTHING (for now) elsif resource_type == Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT || resource_type == Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT_LIST raise RuntimeError, "resource_hash[:tree].length is less than 2. Something is not right \xe2\x86\x92 #{resource_hash[:tree].inspect}" if resource_hash[:tree].length < 2 parent_element = Blufin::Strings::snake_case_to_camel_case_lower(resource_hash[:tree][resource_hash[:tree].length - 2]) elsif resource_type == Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT_LINK # TODO - CANNOT BE NIL ... BUT WHY? 22/12/2015 ... STILL HAVEN'T LOOKED INTO THIS -> 02/18/2017 parent_element = nil else raise RuntimeError, "Unrecognized type: #{resource_type}" end content << '@Data' content << '@NoArgsConstructor' content << "public final class #{class_name} extends PersistentDto {" content << '' content = content.push(*instance_variables) content = content.push(*special_getters) if special_getters.length > 0 unless parent_element.nil? content << '' content << ' @Override' content << ' @JsonIgnore' content << ' public Integer getParentId() {' content << '' content << " return this.#{parent_element}Id;" content << ' }' content << '' content << ' @Override' content << ' @JsonIgnore' content << ' public void setParentId(Integer id) {' content << '' content << " this.#{parent_element}Id = id;" content << ' }' end content << '}' full_file_path = "#{get_java_path(@site, schema, SERVICE, PACKAGE)}/#{class_name}.java" write_file_java(full_file_path, content, schema == Blufin::YmlSchemaValidator::MOCK).gsub(@site_location, '') end end end end |