aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.PMR/Connection/KeepAliveResponse.cs
blob: ff352253203cd0c565f59c3b41ecedd107e5a6d9 (plain)
1
2
3
4
5
6
7
8
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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Tango.CodeGeneration;
using Tango.DAL.Remote.DB;
using Tango.Settings;
using Humanizer;

namespace Tango.DAL.Observables
{
    public class ObservablesGenerator
    {
        public void GenerateCSharp(String targetPath)
        {
            //Generate Entities...
            foreach (var table in typeof(RemoteDB).GetProperties().Where(x => typeof(IEnumerable).IsAssignableFrom(x.PropertyType)))
            {
                EntityCodeFile codeFile = new EntityCodeFile(ObservableEntity.DalNameToStandardName(table.Name).SingularizeMVC())
                {
                    EntityName = table.Name.SingularizeMVC(),
                    TableName = table.Name,
                };

                foreach (var field in table.PropertyType.GenericTypeArguments.First().GetProperties().Skip(4))
                {
                    EntityCodeFileField codeField = new EntityCodeFileField();
                    codeField.FieldName = field.Name;
                    codeField.Name = ObservableEntity.DalNameToStandardName(field.Name);
                    codeField.Description = field.Name.Replace("_", " ").ToLower();


                    if (field.PropertyType.IsGenericType)
                    {
                        codeField.Type = String.Format("ObservableCollection<{0}>", ObservableEntity.DalNameToStandardName(field.PropertyType.GenericTypeArguments.Single().Name).SingularizeMVC());
                        codeField.Construct = true;
                    }
                    else
                    {
                        if (field.PropertyType.IsClass && field.PropertyType != typeof(String))
                        {
                            codeField.Type = ObservableEntity.DalNameToStandardName(field.PropertyType.Name).SingularizeMVC();
                        }
                        else
                        {
                            codeField.Type = field.PropertyType.Name;
                        }
                    }
                    codeFile.Fields.Add(codeField);
                }

                String code = codeFile.GenerateCode();

                String entitiesPath = Path.Combine(targetPath, "Entities");
                Directory.CreateDirectory(entitiesPath);
                File.WriteAllText(Path.Combine(entitiesPath, codeFile.Name + ".cs"), code);
            }
            //Generate Entities...

            //Generate Enumerations...
            using (RemoteDB db = new RemoteDB(SettingsManager.Default.DataBase.SQLServerAddress, false))
            {
                foreach (var tableProp in db.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsGenericType))
                {
                    try
                    {
                        var a = tableProp.PropertyType.GenericTypeArguments.FirstOrDefault();
                        var codeProp = tableProp.PropertyType.GenericTypeArguments.First().GetProperty("CODE");
                        var nameProp = tableProp.PropertyType.GenericTypeArguments.First().GetProperty("NAME");
                        var descriptionProp = tableProp.PropertyType.GenericTypeArguments.First().GetProperty("DESCRIPTION");

                        if (codeProp != null && nameProp != null)
                        {
                            EnumerationFile enumFile = new EnumerationFile();
                            enumFile.Name = ObservableEntity.DalNameToStandardName(tableProp.Name);

                            foreach (var row in tableProp.GetValue(db) as IEnumerable)
                            {
                                EnumerationField field = new EnumerationField();
                                field.Name = nameProp.GetValue(row).ToString().Replace(" ", "");
                                field.Value = (int)codeProp.GetValue(row);

                                if (descriptionProp != null)
                                {
                                    field.Description = descriptionProp.GetValue(row).ToString();
                                }
                                else
                                {
                                    field.Description = nameProp.GetValue(row).ToString();
                                }
                                enumFile.Fields.Add(field);
                            }

                            String enumerationsPath = Path.Combine(targetPath, "Enumerations");
                            Directory.CreateDirectory(enumerationsPath);
                            String code = enumFile.GenerateCode();
                            File.WriteAllText(Path.Combine(enumerationsPath, enumFile.Name + ".cs"), code);
                        }
                    }
                    catch { }
                }
            }
            //Generate Enumerations...

            //Generate Observables Adapter Extensions...
            ObservablesAdapterFile adapterFile = new ObservablesAdapterFile();
            adapterFile.Name = nameof(ObservablesEntitiesAdapter);

            foreach (var table in typeof(RemoteDB).GetProperties().Where(x => typeof(IEnumerable).IsAssignableFrom(x.PropertyType)))
            {
                adapterFile.Properties.Add(new Property()
                {
                    Name = ObservableEntity.DalNameToStandardName(table.Name),
                    Type = String.Format("ObservableCollection<{0}>", ObservableEntity.DalNameToStandardName(table.PropertyType.GenericTypeArguments.Single().Name).SingularizeMVC()),
                });
            }

            String adapterCode = adapterFile.GenerateCode();
            File.WriteAllText(Path.Combine(targetPath, "ObservablesEntitiesAdapterExtension.cs"), adapterCode);
            //Generate Observables Adapter Extensions...
        }

        public void GenerateJava(String targetPath)
        {
            //Generate Entities...
            foreach (var table in typeof(RemoteDB).GetProperties().Where(x => typeof(IEnumerable).IsAssignableFrom(x.PropertyType)))
            {
                EntityCodeFileJava codeFile = new EntityCodeFileJava(ObservableEntity.DalNameToStandardName(table.Name).Singularize(false))
                {
                    EntityName = table.Name.Singularize(false),
                    TableName = table.Name,
                };



                foreach (var field in table.PropertyType.GenericTypeArguments.First().GetProperties().Skip(4).Where(x => !x.Name.Contains("GUID")))
                {
                    EntityCodeFileField codeField = new EntityCodeFileField();
                    codeField.FieldName = field.Name.Singularize(false);
                    codeField.Name = ObservableEntity.DalNameToStandardName(field.Name.Singularize(false));
                    codeField.Description = FirstCharacterToLower(ObservableEntity.DalNameToStandardName(field.Name).Singularize(false));


                    if (field.PropertyType.IsGenericType)
                    {
                        continue;
                    }
                    else
                    {
                        if (field.PropertyType.IsClass && field.PropertyType != typeof(String))
                        {
                            codeField.Type = ObservableEntity.DalNameToStandardName(field.PropertyType.Name).Singularize(false);
                            codeField.Construct = true;
                        }
                        else
                        {
                            codeField.Type = field.PropertyType.Name == "Int32" ? "int" : field.PropertyType.Name;
                        }
                    }
                    codeFile.Fields.Add(codeField);
                }

                codeFile.IndentResult = false;
                String code = codeFile.GenerateCode();

                String entitiesPath = Path.Combine(targetPath, "entities");
                Directory.CreateDirectory(entitiesPath);
                File.WriteAllText(Path.Combine(entitiesPath, codeFile.Name + ".java"), code);
            }
            //Generate Entities...

            //Generate Enumerations...
            using (RemoteDB db = new RemoteDB(SettingsManager.Default.DataBase.SQLServerAddress, false))
            {
                foreach (var tableProp in db.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsGenericType))
                {
                    try
                    {
                        var a = tableProp.PropertyType.GenericTypeArguments.FirstOrDefault();
                        var codeProp = tableProp.PropertyType.GenericTypeArguments.First().GetProperty("CODE");
                        var nameProp = tableProp.PropertyType.GenericTypeArguments.First().GetProperty("NAME");
                        var descriptionProp = tableProp.PropertyType.GenericTypeArguments.First().GetProperty("DESCRIPTION");

                        if (codeProp != null && nameProp != null)
                        {
                            EnumerationFileJava enumFile = new EnumerationFileJava();
                            enumFile.Name = ObservableEntity.DalNameToStandardName(tableProp.Name);

                            foreach (var row in tableProp.GetValue(db) as IEnumerable)
                            {
                                EnumerationField field = new EnumerationField();
                                field.Name = nameProp.GetValue(row).ToString().Replace(" ", "");
                                field.Value = (int)codeProp.GetValue(row);

                                if (descriptionProp != null)
                                {
                                    field.Description = descriptionProp.GetValue(row).ToString();
                                }
                                else
                                {
                                    field.Description = nameProp.GetValue(row).ToString();
                                }
                                enumFile.Fields.Add(field);
                            }

                            String enumerationsPath = Path.Combine(targetPath, "enumerations");
                            Directory.CreateDirectory(enumerationsPath);
                            String code = enumFile.GenerateCode();
                            File.WriteAllText(Path.Combine(enumerationsPath, enumFile.Name + ".java"), code);
                        }
                    }
                    catch { }
                }
            }
            //Generate Enumerations...

            //Generate DAO...
            TangoDAOJavaFile daoFile = new TangoDAOJavaFile();
            daoFile.Name = "TangoDAO";

            foreach (var table in typeof(RemoteDB).GetProperties().Where(x => typeof(IEnumerable).IsAssignableFrom(x.PropertyType)))
            {
                daoFile.Entities.Add(new TangoDAOJavaFile.TangoDAOEntity()
                {
                     Name = ObservableEntity.DalNameToStandardName(table.Name).Singularize(false),
                     TableName = ObservableEntity.DalNameToStandardName(table.Name),
                });
            }

            String daoCode = daoFile.GenerateCode();
            String daoFolder = Path.Combine(targetPath, "dao");
            Directory.CreateDirectory(daoFolder);
            File.WriteAllText(Path.Combine(daoFolder, "TangoDAO.java"), daoCode);
            //Generate DAO...
        }

        private static string FirstCharacterToLower(string str)
        {
            if (String.IsNullOrEmpty(str) || Char.IsLower(str, 0))
                return str;

            return Char.ToLowerInvariant(str[0]) + str.Substring(1);
        }
    }
}