Class: Blufin::YmlJavaModelWriter

Inherits:
YmlWriterBase show all
Defined in:
lib/core/yml_writers/yml_java_model_writer.rb

Constant Summary collapse

SERVICE =
Blufin::SiteServices::API
PACKAGE =
"#{Blufin::SiteServices::PACKAGE_AUTO_GENERATED}.model"
PLACEHOLDER_SITE_NAME =
'{{--SITE-NAME--}}'
PLACEHOLDER_SITE_DOMAIN =
'{{--SITE-DOMAIN--}}'
PLACEHOLDER_OBJECT =
'{{--OBJECT--}}'
PLACEHOLDER_OBJECT_LOWER =
'{{--OBJECT-LOWER--}}'
PLACEHOLDER_CLASS_PROPERTIES =
'{{--CLASS-PROPERTIES--}}'
PLACEHOLDER_CONSTRUCTOR_CONTENT =
'{{--CONSTRUCTOR-CONTENT--}}'
PLACEHOLDER_IMPORT =
'{{--IMPORT--}}'
PLACEHOLDER_RESOURCE_DEPTHS =
'{{--RESOURCE-DEPTHS--}}'
PLACEHOLDER_GET =
'{{--DEPENDENT-GET--}}'
PLACEHOLDER_GET_CALLS =
'{{--DEPENDENT-GET-CALLS--}}'
PLACEHOLDER_POST_ID_CAPTURE =
'{{--DEPENDENT-POST-ID-CAPTURE--}}'
PLACEHOLDER_POST_INITIAL =
'{{--DEPENDENT-POST-INITIAL--}}'
PLACEHOLDER_POST =
'{{--DEPENDENT-POST--}}'
PLACEHOLDER_PUT =
'{{--DEPENDENT-PUT--}}'
PLACEHOLDER_PUT_INITIAL =
'{{--DEPENDENT-PUT-INITIAL--}}'
PLACEHOLDER_PUT_ORPHAN_REMOVAL =
'{{--DEPENDENT-PUT-ORPHAN-REMOVAL--}}'
PLACEHOLDER_PATCH =
'{{--DEPENDENT-PATCH--}}'
PLACEHOLDER_DELETE =
'{{--DEPENDENT-DELETE--}}'
PLACEHOLDER_SCHEMA_UPPERCASE =
'{{--PLACEHOLDER-SCHEMA-UPPERCASE}--}'
PLACEHOLDER_CONTENT =
'{{--CONTENT--}}'

Constants inherited from YmlWriterBase

Blufin::YmlWriterBase::AUTO_TYPES, Blufin::YmlWriterBase::PLACEHOLDER_CLASS, 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

Constructor Details

#initialize(site, schema_data, schema_resources) ⇒ Object

Returns void.

Raises:

  • (RuntimeError)


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
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
# File 'lib/core/yml_writers/yml_java_model_writer.rb', line 30

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_domain_gsub = @site_domain.strip == '' ? '' : "#{@site_domain}"
    @site_location    = "#{Blufin::SiteResolver::get_site_location(@site)}/"

    # Wipe out all previous files.
    Blufin::YmlSchemaValidator::VALID_SCHEMAS_GENERATE.each do |schema|
        path_to_wipe_out = "#{get_java_path(@site, schema, SERVICE, PACKAGE)}"
        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

    @template = <<TEMPLATE
package #{PLACEHOLDER_SITE_DOMAIN}.#{PLACEHOLDER_SITE_NAME}.api.#{PACKAGE};

import org.blufin.api.base.AbstractResource;
import org.blufin.base.enums.SchemaType;
import org.blufin.api.base.AbstractModel;
import org.blufin.base.exceptions.BlufinServerException;
import org.blufin.base.exceptions.BlufinClientException;
import org.blufin.base.exceptions.BlufinNotImplementedException;
import org.blufin.base.helper.IdSet;
import lombok.Getter;
import org.blufin.api.base.model.GetModel;
import org.blufin.api.base.model.PostModel;
import org.blufin.api.base.model.PutModel;
import org.blufin.api.base.model.PatchModel;
import org.blufin.api.base.model.DeleteModel;
import org.blufin.base.helper.Pair;
import org.blufin.core.server.db.ConnectionFactory;
import org.blufin.sdk.response.ApiResponsePagination;
import org.blufin.api.helper.IdExtractor;
import org.blufin.base.helper.IdSet;
import org.blufin.sdk.rest.GetRequest;
import org.blufin.sdk.rest.PostRequest;
import org.blufin.sdk.rest.PutRequest;
import org.blufin.sdk.rest.PatchRequest;
import org.blufin.sdk.rest.DeleteRequest;
import org.blufin.sdk.response.ApiResponse;
import org.blufin.sdk.response.ResourceModificationResponse;
import org.blufin.sdk.base.PersistentDto;
import #{PLACEHOLDER_SITE_DOMAIN}.#{PLACEHOLDER_SITE_NAME}.api.#{Blufin::SiteServices::PACKAGE_AUTO_GENERATED}.dao.#{PLACEHOLDER_OBJECT}Dao;
import #{PLACEHOLDER_SITE_DOMAIN}.#{PLACEHOLDER_SITE_NAME}.sdk.dto.#{PLACEHOLDER_OBJECT};
import java.sql.Connection;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.commons.dbutils.DbUtils;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import java.sql.Connection;
import java.sql.SQLException;
import org.springframework.stereotype.Service;
import java.util.Map;#{PLACEHOLDER_IMPORT}

@Service
public final class #{PLACEHOLDER_OBJECT}Model extends AbstractModel<#{PLACEHOLDER_OBJECT}> {

    @Getter
    private static final #{PLACEHOLDER_OBJECT}Model instance = new #{PLACEHOLDER_OBJECT}Model();

#{PLACEHOLDER_CLASS_PROPERTIES}

    private #{PLACEHOLDER_OBJECT}Model() {

#{PLACEHOLDER_CONSTRUCTOR_CONTENT}
    }
#{PLACEHOLDER_CONTENT}}
TEMPLATE

    @template_get = <<TEMPLATE

    @Override
    protected ApiResponse<#{PLACEHOLDER_OBJECT}> get(Connection connection, GetRequest request) throws BlufinServerException {

Pair<ApiResponsePagination, List<#{PLACEHOLDER_OBJECT}>> #{PLACEHOLDER_OBJECT_LOWER}Response = #{PLACEHOLDER_OBJECT_LOWER}Dao.get(connection, request);
#{PLACEHOLDER_GET}
return buildApiResponse(#{PLACEHOLDER_OBJECT_LOWER}Response.getKey(), #{PLACEHOLDER_OBJECT_LOWER}Response.getValue());
    }
TEMPLATE

    @template_post = <<TEMPLATE

    @Override
    protected void post(Connection connection, PostRequest<#{PLACEHOLDER_OBJECT}> request) throws BlufinServerException {

#{PLACEHOLDER_POST_ID_CAPTURE}for (#{PLACEHOLDER_OBJECT} #{PLACEHOLDER_OBJECT_LOWER} : request.getPayload()) {

#{PLACEHOLDER_POST_INITIAL}#{PLACEHOLDER_POST}        }
    }
TEMPLATE

    @template_put = <<TEMPLATE

    @Override
    protected void put(Connection connection, PutRequest<#{PLACEHOLDER_OBJECT}> request) throws BlufinServerException {

#{PLACEHOLDER_PUT_ORPHAN_REMOVAL}for (#{PLACEHOLDER_OBJECT} #{PLACEHOLDER_OBJECT_LOWER} : request.getPayload()) {

#{PLACEHOLDER_PUT_INITIAL}#{PLACEHOLDER_PUT}        }
    }
TEMPLATE

    @template_patch = <<TEMPLATE

    @Override
    protected void patch(Connection connection, PatchRequest request) throws BlufinServerException {

#{PLACEHOLDER_PATCH}
    }
TEMPLATE

    @template_delete = <<TEMPLATE

    @Override
    protected void delete(Connection connection, DeleteRequest request) throws BlufinServerException {

#{PLACEHOLDER_DELETE}
    }
TEMPLATE

    @template_get_blank = <<TEMPLATE

    @Override
    protected ApiResponse<#{PLACEHOLDER_OBJECT}> get(Connection connection, GetRequest request) throws BlufinServerException {

throw new BlufinNotImplementedException();
    }
TEMPLATE

    @template_post_blank = <<TEMPLATE

    @Override
    protected void post(Connection connection, PostRequest<#{PLACEHOLDER_OBJECT}> request) throws BlufinServerException {

throw new BlufinNotImplementedException();
    }
TEMPLATE

    @template_put_blank = <<TEMPLATE

    @Override
    protected void put(Connection connection, PutRequest<#{PLACEHOLDER_OBJECT}> request) throws BlufinServerException {

throw new BlufinNotImplementedException();
    }
TEMPLATE

    @template_patch_blank = <<TEMPLATE

    @Override
    protected void patch(Connection connection, PatchRequest request) throws BlufinServerException {

throw new BlufinNotImplementedException();
    }
TEMPLATE

    @template_delete_blank = <<TEMPLATE

    @Override
    protected void delete(Connection connection, DeleteRequest request) throws BlufinServerException {

throw new BlufinNotImplementedException();
    }
TEMPLATE

end

Instance Method Details

#writeObject

Returns void.

Returns:

  • void



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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/core/yml_writers/yml_java_model_writer.rb', line 212

def write

    @schema_data.each do |schema, schema_data|

        schema_data.keys.each do |table|

            object       = Blufin::Strings::snake_case_to_camel_case(table)
            object_lower = Blufin::Strings::snake_case_to_camel_case_lower(table)

            resource_data = @schema_resources["#{schema}.#{table}"]

            @has_get    = YmlCommon::has_http_method(Blufin::YmlConfigValidator::GET, resource_data, Blufin::YmlSchemaValidator::CONFIG_INTERNAL)
            @has_post   = YmlCommon::has_http_method(Blufin::YmlConfigValidator::POST, resource_data, Blufin::YmlSchemaValidator::CONFIG_INTERNAL)
            @has_put    = YmlCommon::has_http_method(Blufin::YmlConfigValidator::PUT, resource_data, Blufin::YmlSchemaValidator::CONFIG_INTERNAL)
            @has_patch  = YmlCommon::has_http_method(Blufin::YmlConfigValidator::PATCH, resource_data, Blufin::YmlSchemaValidator::CONFIG_INTERNAL)
            @has_delete = YmlCommon::has_http_method(Blufin::YmlConfigValidator::DELETE, resource_data, Blufin::YmlSchemaValidator::CONFIG_INTERNAL)

            # Skip if this resource has no HTTP Methods.
            next unless Blufin::YmlCommon::has_at_least_one_http_method(resource_data, Blufin::YmlSchemaValidator::CONFIG_INTERNAL)

            @import_statements  = []
            @daos               = [table]
            @contents_get       = []
            @contents_get_calls = ''
            @contents_post      = []
            @contents_put       = []
            @contents_patch     = []
            @contents_delete    = []

            contents       = @template
            contents_inner = ''
            contents_inner += @has_get ? @template_get : @template_get_blank
            contents_inner += @has_post ? @template_post : @template_post_blank
            contents_inner += @has_put ? @template_put : @template_put_blank
            contents_inner += @has_patch ? @template_patch : @template_patch_blank
            contents_inner += @has_delete ? @template_delete : @template_delete_blank

            # # TODO - REMOVE (Once POST/PUT/DELETE fully working)
            # if "#{schema}.#{table}" == 'mock.mock_nested_multiple' || "#{schema}.#{table}" == 'app.root_multiple'
            #
            #     # TODO - REMOVE (Once POST/PUT/DELETE fully working)
            #     puts "\x1B[38;5;198m#{schema}.#{table}\x1B[0m"
            #     puts resource_data.to_yaml

            @contents_patch << "        #{PLACEHOLDER_OBJECT_LOWER}Dao.patch(connection, request.getPayLoad());" if @has_patch
            @contents_delete << "        #{PLACEHOLDER_OBJECT_LOWER}Dao.delete(connection, request.getIds());" if @has_delete

            @has_children    = false
            @parent          = resource_data[:parent]
            @child_type      = Blufin::YmlCommon::extract_child_type_from_schema_data(@schema_data, schema, table)
            @child_type_java = Blufin::YmlCommon::extract_child_type_from_schema_data_for_java(@schema_data, schema, table)

            if !resource_data[:dependents].nil? && resource_data[:dependents].any?

                @objects      = {}
                @has_children = true

                resource_data[:dependents].each do |dependent|
                    dependent_data                  = @schema_resources["#{schema}.#{dependent}"]
                    depth                           = dependent_data[:depth] - @schema_resources["#{schema}.#{table}"][:depth]
                    parent_object                   = dependent_data[:tree][dependent_data[:tree].length - 2]
                    dependent_data[:depth_relative] = depth
                    @objects[parent_object]         = [] unless @objects[parent_object].is_a?(Array)
                    @objects[parent_object] << dependent_data
                end

                if @has_get

                    @contents_get << ''
                    @contents_get << "        if (#{PLACEHOLDER_OBJECT_LOWER}Response.getValue().size() > 0) {"
                    @contents_get << ''
                    @contents_get << "            List<Integer> #{PLACEHOLDER_OBJECT_LOWER}Ids = IdExtractor.extractIds(#{PLACEHOLDER_OBJECT_LOWER}Response.getValue());"
                    @contents_get << ''
                    @contents_get << PLACEHOLDER_GET_CALLS
                    @contents_get << ''
                    @contents_get << '            DbUtils.closeQuietly(connection);'
                    @contents_get << ''
                    @contents_get << "            for (#{PLACEHOLDER_OBJECT} #{PLACEHOLDER_OBJECT_LOWER} : #{PLACEHOLDER_OBJECT_LOWER}Response.getValue()) {"

                    loop_tree(table)

                    @contents_get << '            }'
                    @contents_get << '        }'
                    @contents_get << ''

                end

                # Add trailing new line(s) for code-formatting purposes.
                @contents_post[@contents_post.length - 1] += "\n" if @contents_post.any? && @has_post
                if @contents_put.any? && @has_put
                    @contents_put.unshift('')
                    @contents_put[@contents_put.length - 1] += "\n"
                end

            end

            # end # TODO - REMOVE

            post_id_capture = ''

            if @child_type == Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT
                post_initial = "            int #{object_lower}Id = #{object_lower}Dao.post(connection, #{object_lower});\n\n            request.addIdToResponse(#{object_lower}Id);\n"
                put_initial  = "            putObjectElement(connection, #{PLACEHOLDER_OBJECT_LOWER}, #{PLACEHOLDER_OBJECT_LOWER}.getParentId(), #{PLACEHOLDER_OBJECT_LOWER}Dao);\n\n            request.addIdToResponse(#{PLACEHOLDER_OBJECT_LOWER}.getId());\n"
            elsif @child_type == Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT_LIST
                post_id_capture = "request.addParentDataToResponse(request.getPayload(), \"#{table}\", \"#{@parent}_id\");\n\n        "
                post_initial    = "            int #{object_lower}Id = #{object_lower}Dao.post(connection, #{object_lower});\n"
                put_initial     = "            #{PLACEHOLDER_OBJECT_LOWER}.setId(putListElement(connection, #{PLACEHOLDER_OBJECT_LOWER}, #{PLACEHOLDER_OBJECT_LOWER}Dao));\n\n            request.addIdToResponse(#{PLACEHOLDER_OBJECT_LOWER}.getId());\n"
            else
                post_initial = "            int #{object_lower}Id = #{object_lower}Dao.post(connection, #{object_lower});\n\n            request.addIdToResponse(#{object_lower}Id);\n"
                put_initial  = "            putObjectElement(connection, #{PLACEHOLDER_OBJECT_LOWER}, #{PLACEHOLDER_OBJECT_LOWER}Dao);\n\n            request.addIdToResponse(#{PLACEHOLDER_OBJECT_LOWER}.getId());\n"
            end

            put_orphan_removal = @child_type_java.nil? || @child_type != Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT_LIST ? '' : "removeOrphans(connection, request.getPayload(), #{object_lower}Dao);\n\n        "

            contents_inner = contents_inner.gsub(PLACEHOLDER_GET, Blufin::YmlCommon::convert_line_array_to_string(@contents_get))
            contents_inner = contents_inner.gsub(PLACEHOLDER_POST_ID_CAPTURE, post_id_capture)
            contents_inner = contents_inner.gsub(PLACEHOLDER_POST_INITIAL, post_initial)
            contents_inner = contents_inner.gsub(PLACEHOLDER_POST, Blufin::YmlCommon::convert_line_array_to_string(@contents_post))
            contents_inner = contents_inner.gsub(PLACEHOLDER_PUT, Blufin::YmlCommon::convert_line_array_to_string(@contents_put))
            contents_inner = contents_inner.gsub(PLACEHOLDER_PUT_INITIAL, put_initial)
            contents_inner = contents_inner.gsub(PLACEHOLDER_PUT_ORPHAN_REMOVAL, put_orphan_removal)
            contents_inner = contents_inner.gsub(PLACEHOLDER_PATCH, Blufin::YmlCommon::convert_line_array_to_string(@contents_patch))
            contents_inner = contents_inner.gsub(PLACEHOLDER_DELETE, Blufin::YmlCommon::convert_line_array_to_string(@contents_delete))

            @import_statements.sort!
            @import_statements.uniq!

            contents = contents.gsub(PLACEHOLDER_CONTENT, contents_inner.to_s)
            contents = contents.gsub(PLACEHOLDER_CLASS_PROPERTIES, Blufin::YmlCommon::convert_line_array_to_string(get_class_properties))
            contents = contents.gsub(PLACEHOLDER_CONSTRUCTOR_CONTENT, Blufin::YmlCommon::convert_line_array_to_string(get_constructor_content))
            contents = contents.gsub(PLACEHOLDER_GET_CALLS, @contents_get_calls)

            contents = contents.gsub(PLACEHOLDER_IMPORT, @import_statements.any? ? "\n#{@import_statements.join("\n")}" : '')
            contents = contents.gsub(PLACEHOLDER_SITE_NAME, @site_name.gsub('-', '.'))
            contents = contents.gsub(PLACEHOLDER_SITE_DOMAIN, @site_domain_gsub)
            contents = contents.gsub(PLACEHOLDER_SCHEMA, schema)
            contents = contents.gsub(PLACEHOLDER_SCHEMA_UPPERCASE, schema.upcase)
            contents = contents.gsub(PLACEHOLDER_OBJECT, object)
            contents = contents.gsub(PLACEHOLDER_OBJECT_LOWER, object_lower)

            # Hacky-fix for test classes. DO NOT remove this or Java won't compile.
            contents = contents.gsub('SchemaType.MOCK', 'null') if schema == Blufin::YmlSchemaValidator::MOCK

            write_file_java("#{get_java_path(@site, schema, SERVICE, PACKAGE)}/#{object}Model.java", Blufin::YmlCommon::convert_string_to_line_array(contents), schema == Blufin::YmlSchemaValidator::MOCK)

        end

    end

end