示例#1
0
        public IMetadataRetrievable ImportSource(string psDatabaseTable, ObjectContext poDbContext)
        {
            WonkaBreImportSource NewImportSource = new WonkaBreImportSource();
            HashSet <string>     KeyColNames     = new HashSet <string>();

            if (!String.IsNullOrEmpty(psDatabaseTable) && (poDbContext != null))
            {
                if (moCachedImports.ContainsKey(psDatabaseTable))
                {
                    return(moCachedImports[psDatabaseTable]);
                }

                var tables =
                    poDbContext.MetadataWorkspace.GetItems(DataSpace.CSpace).Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType);

                foreach (var TmpTable in tables)
                {
                    EntityType TmpEntityType = (EntityType)TmpTable;

                    if (TmpEntityType.Name == psDatabaseTable)
                    {
                        var KeyCols = TmpEntityType.KeyMembers;
                        foreach (var KeyCol in KeyCols)
                        {
                            KeyColNames.Add(KeyCol.Name);
                        }

                        break;
                    }
                }

                var columns =
                    from meta in poDbContext.MetadataWorkspace.GetItems(DataSpace.CSpace).Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType)
                    from p in (meta as EntityType).Properties.Where(p => p.DeclaringType.Name == psDatabaseTable)
                    select new
                {
                    colName   = p.Name,
                    colType   = p.TypeUsage.EdmType,
                    doc       = p.Documentation,
                    maxLength = p.MaxLength,
                    precision = p.Precision,
                    scale     = p.Scale,
                    defValue  = p.DefaultValue,
                    props     = p.MetadataProperties
                };

                foreach (var TmpCol in columns)
                {
                    string sTmpColName = TmpCol.colName;
                    var    Props       = TmpCol.props;

                    WonkaRefAttr TmpWonkaAttr = new WonkaRefAttr();

                    TmpWonkaAttr.AttrId   = GenerateNewAttrId();
                    TmpWonkaAttr.AttrName = sTmpColName;
                    TmpWonkaAttr.ColName  = sTmpColName;
                    TmpWonkaAttr.TabName  = psDatabaseTable;

                    TmpWonkaAttr.DefaultValue = Convert.ToString(TmpCol.defValue);
                    TmpWonkaAttr.Description  = (TmpCol.doc != null) ? TmpCol.doc.LongDescription : "";

                    TmpWonkaAttr.IsDate    = IsTypeDate(TmpCol.colType);
                    TmpWonkaAttr.IsNumeric = IsTypeNumeric(TmpCol.colType);
                    TmpWonkaAttr.IsDecimal = IsTypeDecimal(TmpCol.colType);

                    if (TmpWonkaAttr.IsNumeric || TmpWonkaAttr.IsDecimal)
                    {
                        TmpWonkaAttr.Precision = (int)((TmpCol.precision != null) ? TmpCol.precision : 0);
                        TmpWonkaAttr.Scale     = (int)((TmpCol.scale != null) ? TmpCol.scale : 0);
                    }

                    TmpWonkaAttr.MaxLength = (TmpCol.maxLength != null) ? (int)TmpCol.maxLength : 0;

                    TmpWonkaAttr.FieldId   = TmpWonkaAttr.AttrId + 1000;
                    TmpWonkaAttr.GroupId   = CONST_DEFAULT_GROUP_ID;
                    TmpWonkaAttr.IsAudited = true;

                    TmpWonkaAttr.IsKey = KeyColNames.Contains(TmpWonkaAttr.AttrName);

                    NewImportSource.AddAttribute(TmpWonkaAttr);
                }

                if (NewImportSource.GetAttrCache().Count <= 0)
                {
                    throw new WonkaBreException(0, 0, "ERROR!  Could not import the schema because the Reader's field count was zero.");
                }

                WonkaRefGroup NewImportGroup = new WonkaRefGroup();

                NewImportGroup.GroupId        = CONST_DEFAULT_GROUP_ID;
                NewImportGroup.GroupName      = psDatabaseTable;
                NewImportGroup.KeyTabCols     = KeyColNames;
                NewImportGroup.ProductTabName = psDatabaseTable;
                NewImportSource.AddGroup(NewImportGroup);

                WonkaRefSource GuestSource = new WonkaRefSource();

                GuestSource.SourceId   = 1;
                GuestSource.SourceName = "Guest";
                GuestSource.Status     = "Active";
                NewImportSource.AddSource(GuestSource);

                foreach (WonkaRefAttr TempAttr in NewImportSource.GetAttrCache())
                {
                    WonkaRefField NewImportField = new WonkaRefField();

                    NewImportField.FieldId     = TempAttr.FieldId;
                    NewImportField.FieldName   = TempAttr.AttrName;
                    NewImportField.Description = TempAttr.Description;
                    NewImportField.GroupId     = CONST_DEFAULT_GROUP_ID;
                    NewImportField.DisplayName = TempAttr.AttrName;
                    NewImportField.AttrIds.Add(TempAttr.AttrId);
                    NewImportSource.AddField(NewImportField);

                    WonkaRefSourceField NewImportSrcFld = new WonkaRefSourceField();

                    NewImportSrcFld.SourceFieldId = 10000 + NewImportField.FieldId;
                    NewImportSrcFld.SourceId      = 1;
                    NewImportSrcFld.FieldId       = NewImportField.FieldId;
                    NewImportSrcFld.SecurityLevel = CONST_SEC_LEVEL_READ;
                    NewImportSource.AddSourceField(NewImportSrcFld);
                }
            }
            else
            {
                throw new WonkaBreException(0, 0, "ERROR!  Could not import the schema for the database table.");
            }

            PopulateDefaults();

            return(NewImportSource);
        }