コード例 #1
0
        /// <summary>
        /// Convert model to TableStruct and add in the list to create.
        /// </summary>
        /// <typeparam name="T">DataTable</typeparam>
        /// <param name="model">Model</param>
        /// <param name="form">Create type of form</param>
        public void Add <T>(T model, Form form) where T : k.sap.Models.DataTable
        {
            TableStruct table;

            #region Table struct
            BoUTBTableType boUTBTableType;
            if (model.Properties.TableType == G.DataBase.SAPTables.TableType.System)
            {
                boUTBTableType = (BoUTBTableType)G.DataBase.SAPTables.TableType.bott_NoObject;
            }
            else
            {
                boUTBTableType = (BoUTBTableType)model.Properties.TableType;
            }

            table = new TableStruct(
                model.Properties.Name,
                model.Properties.Description,
                model.Properties.TableName,
                boUTBTableType,
                model.Properties.Level,
                model.Properties.IsSystem,
                form);
            #endregion

            #region Column struct
            foreach (var field in model.GetType().GetFields())
            {
                var col = new ColumnStruct();

                foreach (object attr in field.GetCustomAttributes(true))
                {
                    if (attr != null && attr is SAPColumnAttribute)
                    {
                        var sapcol = attr as SAPColumnAttribute;
                        if (!sapcol.SapColumn)
                        {
                            var customer_tag  = k.G.DataBase.Tags.Namespace;
                            var def_userfield = k.G.DataBase.Tags.DefPrefixUserField;

                            // Removing SAP and Customer tag in the column name
                            col.Name = field.Name.StartsWith(def_userfield) ? field.Name.Replace(def_userfield, null) : field.Name;

                            if (model.Properties.IsSystem)
                            {
                                col.Name = col.Name.StartsWith(customer_tag) ? col.Name : k.db.Factory.Scripts.Namespace($"!!{col.Name}");
                            }

                            col.Description = k.db.Factory.Scripts.Namespace($"!!: {sapcol.Description}");
                            col.Size        = sapcol.Size;
                            col.SetFieldType(sapcol.Type);

                            #region Valid Values
                            if (attr != null && attr is SAPColumnValidValuesAttribute)
                            {
                                var sapVValues = attr as SAPColumnValidValuesAttribute;
                                col.ValidValues = sapVValues.GetValidValues();
                            }
                            #endregion
                        }
                    }
                }

                if (!String.IsNullOrEmpty(col.Name))
                {
                    table.Columns.Add(col);
                }
            }

            Tables.Add(table);
            #endregion
        }
コード例 #2
0
        private bool CreateTable(TableStruct table, bool test)
        {
            if (table.IsSystem)
            {
                return(false);
            }

            // Add or update only if necessary
            var tableNameWithoutAt = k.db.Factory.Scripts.Namespace(table.Table.Replace("@", ""));

            var foo = k.db.Factory.ResultSet.GetRow(R.CredID, Content.Queries.OUTB.Details_1, tableNameWithoutAt);

            if (foo != null)
            {
                if (foo["Descr"].ToString() == table.Description)
                {
                    if (foo["ObjectType"].ToIntOrNull() == (int)table.TableType)
                    {
                        return(false);
                    }
                    else if (test)
                    {
                        return(true);
                    }
                }
            }

            if (!P.CreateTableAndFields)
            {
                throw new KDIException(LOG, E.Message.CreateTableFields_0);
            }


            #region Error -1120 : Error: Ref count for this object is higher then 0
            // Solution : https://archive.sap.com/discussions/thread/1958196
            var oRS = k.sap.DI.Conn.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset) as SAPbobsCOM.Recordset;

            System.Runtime.InteropServices.Marshal.ReleaseComObject(oRS);
            oRS = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            #endregion


            var oMD = k.sap.DI.Conn.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserTables) as SAPbobsCOM.UserTablesMD;

            try
            {
                var update = oMD.GetByKey(tableNameWithoutAt);

                if (!update)
                {
                    oMD.TableName = tableNameWithoutAt;
                    oMD.TableType = table.TableType;
                }
                oMD.TableDescription = table.Description;

                int res;
                if (update)
                {
                    res = oMD.Update();
                }
                else
                {
                    res = oMD.Add();
                }


                var track = k.Diagnostic.TrackMessages("Model:", table.ToJson(), oMD.GetType().Name, oMD.GetAsXML());

                if (res != 0)
                {
                    var error = k.sap.DI.GetLastErrorDescription();
                    k.Diagnostic.Error(LOG, track, $"({res}) {error}.");
                    throw new Exception($"Error to create {tableNameWithoutAt} table. Error code {res}", new Exception(error));
                }
                else
                {
                    var bar = update ? "updated" : "created";
                    k.Diagnostic.Debug(this, track, $"{tableNameWithoutAt} table has been {bar} as {table.TableType.ToString()}");

                    return(true);
                }
            }
            catch (Exception ex)
            {
                k.Diagnostic.Error(LOG, ex);
                throw ex;
            }
            finally
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oMD);
                oMD = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }