Class: Blufin::YmlJavaResourceDataWriter

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

Constant Summary collapse

SERVICE =
Blufin::SiteServices::API
PACKAGE =
Blufin::SiteServices::PACKAGE_AUTO_GENERATED
PLACEHOLDER_CLASS =
'{{--CLASS--}}'
PLACEHOLDER_GET =
'{{--GET--}}'
PLACEHOLDER_META_DATA =
'{{--GET-META-DATA--}}'
PLACEHOLDER_DEPTH =
'{{--GET-DEPTH--}}'
PLACEHOLDER_CONTENT_GET =
'{{--CONTENT-GET--}}'
PLACEHOLDER_CONTENT_META_DATA =
'{{--CONTENT-META-DATA--}}'
PLACEHOLDER_CONTENT_DEPTH =
'{{--CONTENT-DEPTH--}}'

Constants inherited from YmlWriterBase

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

Constructor Details

#initialize(site, schema_resources) ⇒ Object

Initialize the class.

Raises:

  • (RuntimeError)


18
19
20
21
22
23
24
25
26
27
28
29
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
# File 'lib/core/yml_writers/yml_java_resource_data_writer.rb', line 18

def initialize(site, schema_resources)

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

    @schema_resources      = schema_resources
    @site                  = Blufin::SiteResolver::validate_site(site)
    @site_name             = Blufin::SiteResolver::get_site_name(@site)
    @site_name_camel_cased = Blufin::SiteResolver::get_site_name_camel_cased(@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)}/"

    @template = <<TEMPLATE
package #{PLACEHOLDER_PACKAGE};

import org.blufin.base.annotations.Singleton;
import org.blufin.api.base.AbstractResourceData;
import org.blufin.base.exceptions.BlufinServerException;
import org.blufin.base.exceptions.BlufinClientException;
import org.blufin.sdk.exceptions.ResourceNotFoundException;
import org.blufin.base.enums.SchemaType;
import org.blufin.sdk.base.PersistentDto;
import org.blufin.sdk.base.AbstractMetaData;
import org.blufin.api.base.AbstractValidator;
import org.blufin.sdk.base.ResourceData;
import org.blufin.sdk.response.ApiResponse;
import org.blufin.sdk.rest.GetRequest;#{PLACEHOLDER_IMPORT}
import org.springframework.stereotype.Service;
import lombok.Getter;

import java.text.MessageFormat;

@Service
public class #{PLACEHOLDER_CLASS} extends AbstractResourceData implements ResourceData {

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

    @Singleton
    private #{PLACEHOLDER_CLASS}() {}

    @Override\n#{PLACEHOLDER_GET}

    @Override\n#{PLACEHOLDER_META_DATA}

    @Override\n#{PLACEHOLDER_DEPTH}}
TEMPLATE

    @template_execute_get = <<TEMPLATE
    public ApiResponse<? extends PersistentDto> executeGet(String endPoint, GetRequest getRequest) throws BlufinClientException, BlufinServerException {

switch (endPoint) {

#{PLACEHOLDER_CONTENT_GET}
    default:
        throw new RuntimeException(MessageFormat.format("Unrecognized end-point: {0}", endPoint));
}
    }
TEMPLATE

    @template_get_meta_data = <<TEMPLATE
    public AbstractMetaData getMetaData(String endPoint) throws ResourceNotFoundException {

switch (endPoint) {

#{PLACEHOLDER_CONTENT_META_DATA}
    default:
        throw new ResourceNotFoundException();
}
    }
TEMPLATE

    @template_get_depth = <<TEMPLATE
    public int getDepth(String table) {

switch (table) {

#{PLACEHOLDER_CONTENT_DEPTH}
    default:
        throw new RuntimeException(MessageFormat.format("Unrecognized table: {0}", table));
}
    }
TEMPLATE

    @template_execute_get_blank = <<TEMPLATE
    public ApiResponse<? extends PersistentDto> executeGet(String endPoint, GetRequest getRequest) throws BlufinClientException, BlufinServerException {

return null;
    }
TEMPLATE

    @template_get_meta_data_blank = <<TEMPLATE
    public AbstractMetaData getMetaData(String endPoint) throws ResourceNotFoundException {

return null;
    }
TEMPLATE

    @template_get_depth_blank = <<TEMPLATE
    public int getDepth(String table) {

return null;
    }
TEMPLATE

end

Instance Method Details

#writeObject

Write the file(s).

Returns:

  • void



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

def write

    resource_data = 'ResourceData'

    options = [
        [[Blufin::YmlSchemaValidator::APP, Blufin::YmlSchemaValidator::COMMON, Blufin::YmlSchemaValidator::CONFIG], "#{get_java_path(@site, nil, SERVICE, PACKAGE)}/config", "#{@site_name_camel_cased}#{resource_data}", "#{@site_domain_gsub}#{@site_name.gsub('-', '.')}.api.#{Blufin::SiteServices::PACKAGE_AUTO_GENERATED}.config"],
        [%W(#{Blufin::YmlSchemaValidator::MOCK}), "#{Blufin::Config::get_path('Paths', 'BlufinJava')}/blufin-api/src/test/java/org/blufin/mock", "Mock#{resource_data}", 'org.blufin.mock']
    ]

    # Loops through twice, once to write the App data and once to write the Blufin Data.
    options.each_with_index do |option, idx|

        opt_schemas           = option[0]
        opt_file_location     = option[1]
        opt_class_name        = option[2]
        opt_package           = option[3]
        opt_file              = "#{opt_file_location}/#{opt_class_name}.java"
        contents_get          = []
            = []
        contents_depth        = []
        schemas_used_get      = []
         = []

        contents = @template

        # Wipe out all previous file.
        Blufin::Files::delete_file(opt_file) if Blufin::Files::file_exists(opt_file)

        if get_keys_for_schemas(@schema_resources.keys, opt_schemas).length == 0
            contents = contents.gsub(PLACEHOLDER_GET, @template_execute_get_blank)
            contents = contents.gsub(PLACEHOLDER_META_DATA, @template_get_meta_data_blank)
            contents = contents.gsub(PLACEHOLDER_DEPTH, @template_get_depth_blank)
        else
            contents = contents.gsub(PLACEHOLDER_GET, @template_execute_get)
            contents = contents.gsub(PLACEHOLDER_META_DATA, @template_get_meta_data)
            contents = contents.gsub(PLACEHOLDER_DEPTH, @template_get_depth)
        end

        @schema_resources.each do |schema_table, resource_data|

            schema = resource_data[:schema]

            next unless opt_schemas.include?(schema)

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

            object = Blufin::Strings::snake_case_to_camel_case(resource_data[:table])

            contents_depth << "            case \"#{resource_data[:resource].gsub('/', '_').gsub('-', '_')}\":"
            contents_depth << "                return #{resource_data[:depth]};"

            if resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::GET)
                contents_get << "            case \"#{resource_data[:resource]}\":"
                contents_get << "                return executeGet(getRequest, #{object}Model.getInstance(), #{idx == 1 ? 'null' : "SchemaType.#{schema.upcase}"});"
                schemas_used_get << schema
            end

            if resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::POST) || resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::PUT) || resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::PATCH) || resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::DELETE)
                 << "            case \"#{resource_data[:resource]}\":"
                 << "            case \"#{resource_data[:resource]}/list\":" if [Blufin::YmlSchemaValidator::RESOURCE_TYPE_PARENT, Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT_LIST].include?(resource_data[:type])
                 << "                return #{object}MetaData.getInstance();"
                 << schema
            end

            if resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::POST)
                # TODO - POST
            end

            if resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::PUT)
                # TODO - PUT
            end

            if resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::PATCH)
                # TODO - PATCH
            end

            if resource_data[:methods_internal].has_key?(Blufin::YmlConfigValidator::DELETE)
                # TODO - DELETE
            end

        end

        contents.gsub!(PLACEHOLDER_CLASS, opt_class_name)
        contents.gsub!(PLACEHOLDER_PACKAGE, opt_package)
        contents.gsub!(PLACEHOLDER_CONTENT_GET, Blufin::YmlCommon::convert_line_array_to_string(contents_get))
        contents.gsub!(PLACEHOLDER_CONTENT_META_DATA, Blufin::YmlCommon::convert_line_array_to_string())
        contents.gsub!(PLACEHOLDER_CONTENT_DEPTH, Blufin::YmlCommon::convert_line_array_to_string(contents_depth))

        # Find out what schemas are being used and import accordingly.
        schemas_import = ''
        schemas_used_get.uniq!.sort! if schemas_used_get.any?
        .uniq!.sort! if schemas_used_get.any?
        if idx == 1
            contents       = contents.gsub("\s*package\s+[A-Za-z0-9-_.]+(#{Blufin::SiteServices::PACKAGE_AUTO_GENERATED};)\s*", 'package org.blufin.mock;')
            schemas_import += "\nimport org.blufin.mock.model.*;"
            schemas_import += "\nimport org.blufin.mock.metadata.*;"
        else
            schemas_import += "\nimport #{@site_domain_gsub}#{@site_name.gsub('-', '.')}.api.#{Blufin::SiteServices::PACKAGE_AUTO_GENERATED}.model.*;" if schemas_used_get.any?
            schemas_import += "\nimport #{@site_domain_gsub}#{@site_name.gsub('-', '.')}.sdk.metadata.*;" if .any?
        end
        contents = contents.gsub(PLACEHOLDER_IMPORT, schemas_import)

        write_file_java(opt_file, Blufin::YmlCommon::convert_string_to_line_array(contents))

    end

end