class Blufin::YmlJavaResourceDataWriter

Constants

PACKAGE
PLACEHOLDER_CLASS
PLACEHOLDER_CONTENT_DEPTH
PLACEHOLDER_CONTENT_GET
PLACEHOLDER_CONTENT_META_DATA
PLACEHOLDER_DEPTH
PLACEHOLDER_GET
PLACEHOLDER_META_DATA
SERVICE

Public Class Methods

new(site, schema_resources) click to toggle source

Initialize the class. @return void

# 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

Public Instance Methods

write() click to toggle source

Write the file(s). @return void

# 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_meta_data    = []
        contents_depth        = []
        schemas_used_get      = []
        schemas_used_metadata = []

        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)
                contents_meta_data << "            case \"#{resource_data[:resource]}\":"
                contents_meta_data << "            case \"#{resource_data[:resource]}/list\":" if [Blufin::YmlSchemaValidator::RESOURCE_TYPE_PARENT, Blufin::YmlSchemaValidator::RESOURCE_TYPE_OBJECT_LIST].include?(resource_data[:type])
                contents_meta_data << "                return #{object}MetaData.getInstance();"
                schemas_used_metadata << 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_meta_data))
        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?
        schemas_used_metadata.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 schemas_used_metadata.any?
        end
        contents = contents.gsub(PLACEHOLDER_IMPORT, schemas_import)

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

    end

end

Private Instance Methods

get_keys_for_schemas(keys, schemas) click to toggle source

Returns only the keys for the specified schema(s). @return Array

# File lib/core/yml_writers/yml_java_resource_data_writer.rb, line 240
def get_keys_for_schemas(keys, schemas)
    keys_for_schema = []
    keys.each do |key|
        if schemas.include?(key.split('.')[0])
            keys_for_schema << key
        end
    end
    keys_for_schema
end