<commit_before>-----------------------------------------------------------------------
--  Gen -- Code Generator
--  Copyright (C) 2009, 2010, 2011, 2012 <NAME>
--  Written by <NAME> (<EMAIL>)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Directories;
with Ada.IO_Exceptions;
with Ada.Strings.Fixed;

with Input_Sources.File;

with DOM.Core;
with DOM.Core.Documents;
with DOM.Readers;
with Sax.Readers;

with ASF.Requests.Mockup;
with ASF.Responses.Mockup;
with ASF.Components.Root;
with ASF.Components.Base;

with Util.Beans.Basic;
with Util.Strings.Vectors;
with EL.Functions;
with EL.Utils;
with EL.Contexts.Default;

with Gen.Utils;
with Gen.Configs;
with Gen.Model;
with Gen.Model.Tables;
with Gen.Model.Mappings;
with Gen.Commands.Templates;

with Util.Strings;
with Util.Files;
with Util.Strings.Transforms;
with Util.Log.Loggers;

package body Gen.Generator is

   use ASF;
   use Util.Log;

   Log : constant Loggers.Logger := Loggers.Create ("Gen.Generator");

   RESULT_DIR : constant String := "generator.output.dir";

   function To_Ada_Type (Value : in Util.Beans.Objects.Object;
                         Param : in Util.Beans.Objects.Object) return Util.Beans.Objects.Object;
   function Indent (Value : Util.Beans.Objects.Object) return Util.Beans.Objects.Object;

   --  EL Function to translate a model type to the key enum value
   function To_Key_Enum (Name : Util.Beans.Objects.Object) return Util.Beans.Objects.Object;

   --  EL function to create an Ada identifier from a file name
   function To_Ada_Ident (Value : Util.Beans.Objects.Object) return Util.Beans.Objects.Object;

   --  EL function to indent the code
   function To_Sql_Type (Value : Util.Beans.Objects.Object) return Util.Beans.Objects.Object;

   --  EL function to format an Ada comment
   function Comment (Value : Util.Beans.Objects.Object) return Util.Beans.Objects.Object;

   --  EL function to return a singular form of a name
   function To_Singular (Value : in Util.Beans.Objects.Object) return Util.Beans.Objects.Object;

   --  Returns a string resulting from replacing in an input string all occurrences of
   --  a "Item" string into an "By" substring.
   function Replace (Value, Item, By : in Util.Beans.Objects.Object)
                     return Util.Beans.Objects.Object;

   procedure Set_Functions (Mapper : in out EL.Functions.Function_Mapper'Class);

   --  ------------------------------
   --  EL Function to translate a model type to an Ada implementation type
   --  Param values:
   --    0 : Get the type for a record declaration
   --    1 : Get the type for a parameter declaration or a return type
   --    2 : Get the type for the generation of the ADO.Statements.Get procedure name
   --  ------------------------------
   function To_Ada_Type (Value : in Util.Beans.Objects.Object;
                         Param : in Util.Beans.Objects.Object) return Util.Beans.Objects.Object is
      use Gen.Model.Tables;
      use Gen.Model.Mappings;
      use Gen.Model;

      function To_Ada_Type (Value : in String) return Util.Beans.Objects.Object;

--        Def    : constant Definition_Access := To_Definition_Access (Value);
      Column : Column_Definition_Access := null; --  To_Definition_Access (Value);

      function To_Ada_Type (Value : in String) return Util.Beans.Objects.Object is
      begin
         if Value = "String" or Value = "java.lang.String" then
            return Util.Beans.Objects.To_Object
              (String '("Ada.Strings.Unbounded.Unbounded_String"));
         elsif Value = "Integer" or Value = "int" or Value = "java.lang.Integer" then
            return Util.Beans.Objects.To_Object (String '("Integer"));
         elsif Value = "Timestamp" then
            if Util.Beans.Objects.To_Integer (Param) = 2 then
               return Util.Beans.Objects.To_Object (String '("Time"));
            else
               return Util.Beans.Objects.To_Object (String '("Ada.Calendar.Time"));
            end if;
         else
            return Util.Beans.Objects.To_Object (Value);
         end if;
      end To_Ada_Type;

      Ptr : constant access Util.Beans.Basic.Readonly_Bean'Class
        := Util.Beans.Objects.To_Bean (Value);
      Type_Mapping : Gen.Model.Mappings.Mapping_Definition_Access;
   begin
      if Ptr /= null and then Ptr.all in Column_Definition'Class then
         Column := Column_Definition'Class (Ptr.all)'Unchecked_Access;
      else
         Column := null;
      end if;
      if Column /= null then
         Type_Mapping := Column.Get_Type_Mapping;
         if Type_Mapping /= null then
            if Type_Mapping.Kind = T_DATE and Util.Beans.Objects.To_Integer (Param) = 2 then
               return Util.Beans.Objects.To_Object (String '("Time"));
            elsif Type_Mapping.Kind = T_TABLE then
               if Util.Beans.Objects.To_Integer (Param) = 1 then
                  return Util.Beans.Objects.To_Object (Column.Get_Type & "_Ref'Class");
               else
                  return Util.Beans.Objects.To_Object (Type_Mapping.Target & "_Ref");
               end if;
            else
               return Util.Beans.Objects.To_Object (Type_Mapping.Target);
            end if;
         elsif Column.Is_Basic_Type then
            return To_Ada_Type (Column.Get_Type);
         elsif Util.Beans.Objects.To_Integer (Param) = 1 then
            return Util.Beans.Objects.To_Object (Column.Get_Type & "_Ref'Class");
         else
            return Util.Beans.Objects.To_Object (Column.Get_Type & "_Ref");
         end if;
      else
         return To_Ada_Type (Util.Beans.Objects.To_String (Value));
      end if;
   end To_Ada_Type;

   KEY_INTEGER_LABEL : constant String := "KEY_INTEGER";
   KEY_STRING_LABEL  : constant String := "KEY_STRING";

   --  ------------------------------
   --  EL Function to translate a model type to the key enum value
   --  ------------------------------
   function To_Key_Enum (Name : Util.Beans.Objects.Object) return Util.Beans.Objects.Object is
      Value : constant String := Util.Beans.Objects.To_String (Name);
   begin
      if Value = "Integer" or Value = "int" or Value = "Identifier"<commit_msg>Take into account the <<use foreign key>> stereotype when generating the Ada code<commit_after>-----------------------------------------------------------------------
--  Gen -- Code Generator
--  Copyright (C) 2009, 2010, 2011, 2012 <NAME>
--  Written by <NAME> (<EMAIL>)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Directories;
with Ada.IO_Exceptions;
with Ada.Strings.Fixed;

with Input_Sources.File;

with DOM.Core;
with DOM.Core.Documents;
with DOM.Readers;
with Sax.Readers;

with ASF.Requests.Mockup;
with ASF.Responses.Mockup;
with ASF.Components.Root;
with ASF.Components.Base;

with Util.Beans.Basic;
with Util.Strings.Vectors;
with EL.Functions;
with EL.Utils;
with EL.Contexts.Default;

with Gen.Utils;
with Gen.Configs;
with Gen.Model;
with Gen.Model.Tables;
with Gen.Model.Mappings;
with Gen.Commands.Templates;

with Util.Strings;
with Util.Files;
with Util.Strings.Transforms;
with Util.Log.Loggers;

package body Gen.Generator is

   use ASF;
   use Util.Log;

   Log : constant Loggers.Logger := Loggers.Create ("Gen.Generator");

   RESULT_DIR : constant String := "generator.output.dir";

   function To_Ada_Type (Value : in Util.Beans.Objects.Object;
                         Param : in Util.Beans.Objects.Object) return Util.Beans.Objects.Object;
   function Indent (Value : Util.Beans.Objects.Object) return Util.Beans.Objects.Object;

   --  EL Function to translate a model type to the key enum value
   function To_Key_Enum (Name : Util.Beans.Objects.Object) return Util.Beans.Objects.Object;

   --  EL function to create an Ada identifier from a file name
   function To_Ada_Ident (Value : Util.Beans.Objects.Object) return Util.Beans.Objects.Object;

   --  EL function to indent the code
   function To_Sql_Type (Value : Util.Beans.Objects.Object) return Util.Beans.Objects.Object;

   --  EL function to format an Ada comment
   function Comment (Value : Util.Beans.Objects.Object) return Util.Beans.Objects.Object;

   --  EL function to return a singular form of a name
   function To_Singular (Value : in Util.Beans.Objects.Object) return Util.Beans.Objects.Object;

   --  Returns a string resulting from replacing in an input string all occurrences of
   --  a "Item" string into an "By" substring.
   function Replace (Value, Item, By : in Util.Beans.Objects.Object)
                     return Util.Beans.Objects.Object;

   procedure Set_Functions (Mapper : in out EL.Functions.Function_Mapper'Class);

   --  ------------------------------
   --  EL Function to translate a model type to an Ada implementation type
   --  Param values:
   --    0 : Get the type for a record declaration
   --    1 : Get the type for a parameter declaration or a return type
   --    2 : Get the type for the generation of the ADO.Statements.Get procedure name
   --  ------------------------------
   function To_Ada_Type (Value : in Util.Beans.Objects.Object;
                         Param : in Util.Beans.Objects.Object) return Util.Beans.Objects.Object is
      use Gen.Model.Tables;
      use Gen.Model.Mappings;
      use Gen.Model;

      function To_Ada_Type (Value : in String) return Util.Beans.Objects.Object;

--        Def    : constant Definition_Access := To_Definition_Access (Value);
      Column : Column_Definition_Access := null; --  To_Definition_Access (Value);

      function To_Ada_Type (Value : in String) return Util.Beans.Objects.Object is
      begin
         if Value = "String" or Value = "java.lang.String" then
            return Util.Beans.Objects.To_Object
              (String '("Ada.Strings.Unbounded.Unbounded_String"));
         elsif Value = "Integer" or Value = "int" or Value = "java.lang.Integer" then
            return Util.Beans.Objects.To_Object (String '("Integer"));
         elsif Value = "Timestamp" then
            if Util.Beans.Objects.To_Integer (Param) = 2 then
               return Util.Beans.Objects.To_Object (String '("Time"));
            else
               return Util.Beans.Objects.To_Object (String '("Ada.Calendar.Time"));
            end if;
         else
            return Util.Beans.Objects.To_Object (Value);
         end if;
      end To_Ada_Type;

      Ptr : constant access Util.Beans.Basic.Readonly_Bean'Class
        := Util.Beans.Objects.To_Bean (Value);
      Type_Mapping : Gen.Model.Mappings.Mapping_Definition_Access;
   begin
      if Ptr /= null and then Ptr.all in Column_Definition'Class then
         Column := Column_Definition'Class (Ptr.all)'Unchecked_Access;
      else
         Column := null;
      end if;
      if Column /= null then
         Type_Mapping := Column.Get_Type_Mapping;
         if Type_Mapping /= null then

            if Type_Mapping.Kind = T_DATE and Util.Beans.Objects.To_Integer (Param) = 2 then
               return Util.Beans.Objects.To_Object (String '("Time"));

            elsif Type_Mapping.Kind /= T_TABLE then
               return Util.Beans.Objects.To_Object (Type_Mapping.Target);

            elsif Column.Use_Foreign_Key_Type then
               return Util.Beans.Objects.To_Object (Type_Mapping.Target);

            elsif Util.Beans.Objects.To_Integer (Param) = 1 then
               return Util.Beans.Objects.To_Object (Column.Get_Type & "_Ref'Class");

            else
               return Util.Beans.Objects.To_Object (Type_Mapping.Target & "_Ref");
            end if;
         elsif Column.Is_Basic_Type then
            return To_Ada_Type (Column.Get_Type);
         elsif Util.Beans.Objects.To_Integer (Param) = 1 then
            return Util.Beans.Objects.To_Object (Column.Get_Type & "_Ref'Class");
         else
            return Util.Beans.Objects.To_Object (Column.Get_Type & "_Ref");
         end if;
      else
         return To_Ada_Type (Util.Beans.Objects.To_String (Value));
      end if;
   end To_Ada_Type;

   KEY_INTEGER_LABEL : constant String := "KEY_INTEGER";
   KEY_STRING_LABEL  : constant String := "KEY_STRING";

   --  ------------------------------
   --  EL Function to translate a model type to the key enum value
   --  ------------------------------
   function To_Key_Enum (Name : Util.Beans.Objects.Object) return Util.Beans.Objects.Object is
      Value : constant String := Util.Beans.Objects.To_String (Name);
   begin
      if Value = "Integer" or Value = "int" or Value = "Identifier"
{'fraction_non_alphanumeric': 0.13042840375586853, 'fraction_numerical': 0.003961267605633803, 'mean_word_length': 2.314612205203015, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '881807', 'n_tokens_mistral': 3942, 'n_tokens_neox': 3813, 'n_words': 1176}