Exemplo n.º 1
0
        public void GetSpecificForeignKey_Should_ExecuteCorrectly()
        {
            // Arrange
            var lookupId = Guid.NewGuid();
            var dao      = new ShimEPMData
            {
                GetTableSqlConnection           = _ => new DataTable(),
                GetClientReportingConnectionGet = () => new SqlConnection()
            };

            ShimSPWeb.AllInstances.ListsGet = _ => new ShimSPListCollection
            {
                ItemGetGuid = guid =>
                {
                    return(guid == lookupId
                        ? new ShimSPList()
                        : null);
                }
            };
            ShimSPSite.AllInstances.AllWebsGet = _ =>
            {
                var list = new List <SPWeb>
                {
                    new ShimSPWeb
                    {
                        ListsGet = () => new ShimSPListCollection
                        {
                            ItemGetGuid = guid => new ShimSPList()
                        }
                    }
                }.AsEnumerable();
                return(new ShimSPWebCollection().Bind(list));
            };
            ShimReportBiz.AllInstances.HasForeignKeyEPMDataStringString =
                (_, reportData, table, column) => true;
            ShimDataTable.AllInstances.RowsGet = _ => new ShimDataRowCollection
            {
                CountGet     = () => 1,
                ItemGetInt32 = index => new ShimDataRow
                {
                    ItemGetString = name => DummyString
                },
                GetEnumerator = () => new List <DataRow>
                {
                    new ShimDataRow
                    {
                        ItemGetString = name => DummyString
                    }
                }.GetEnumerator()
            };
            ShimSPList.AllInstances.FieldsGet = _ => new ShimSPFieldCollection
            {
                GetFieldByInternalNameString = name => new ShimSPFieldLookup
                {
                    LookupListGet = () => lookupId.ToString()
                }
            };
            ShimSPField.AllInstances.TypeAsStringGet = _ => DummyString;

            // Act
            var result = reportBiz.GetSpecificForeignKey(dao, DummyGuid.ToString(), DummyString, DummyString);

            // Assert
            result.ShouldSatisfyAllConditions(
                () => result.ShouldNotBeNull(),
                () => result.Rows.Count.ShouldBeGreaterThan(0));
        }
Exemplo n.º 2
0
        private bool UpdateField(SPListEventProperties properties)
        {
            bool   isSuccessful    = true;
            string tableName       = string.Empty;
            string ssTableName     = string.Empty;
            var    rb              = new ReportBiz(properties.SiteId);
            bool   isUpdateRequire = false;

            try
            {
                var rd   = new ReportData(properties.SiteId);
                var cols = new ColumnDefCollection();
                // Actual table name
                tableName = rd.GetTableName(properties.ListId);
                // Snapshot table name
                ssTableName = rd.GetTableNameSnapshot(properties.ListId);
                // Create column definition collection to process with
                cols.AddColumn(properties.Field);

                var DAO = new EPMData(properties.SiteId);
                // Update only when field type is changed from single to multi value and vice-verse
                if (IsUpdateRequire(DAO, properties, tableName, cols[0].SqlColumnName))
                {
                    isUpdateRequire = true;

                    // Get Foreign Key
                    DataTable foreignKeyTable = rb.GetSpecificForeignKey(DAO, properties.ListId.ToString(), tableName, cols[0].SqlColumnName);

                    // Delete Foreign Key
                    if (foreignKeyTable.Rows.Count > 0)
                    {
                        rb.AddORRemoveForeignKey(DAO, foreignKeyTable.Rows[0], false);
                    }

                    // Delete & Add Field
                    rd.UpdateColumns(tableName, cols);
                    rd.UpdateColumns(ssTableName, cols);
                    rd.UpdateListColumns(properties.ListId, cols);

                    // Add Foreign Key only if lookup type is NOT allowed multi select
                    if (foreignKeyTable.Rows.Count > 0 && !properties.Field.TypeAsString.Equals("LookupMulti", StringComparison.InvariantCultureIgnoreCase))
                    {
                        rb.AddORRemoveForeignKey(DAO, foreignKeyTable.Rows[0], true);
                    }
                }
                DAO.Dispose();
                rd.Dispose();
            }
            catch (Exception ex)
            {
                var DAO = new EPMData(properties.SiteId);
                DAO.LogStatus(properties.ListId.ToString(), properties.FieldName,
                              "Database column update attempt: Unable to update column " + properties.FieldName + ". " + ex.Message,
                              ex.StackTrace, 2, 5, Guid.NewGuid().ToString()); //Logged in the RefreshAll event log.
                DAO.Dispose();
            }

            if (properties.Field is SPFieldLookup && isUpdateRequire)
            {
                #region Update all foreign keys
                try
                {
                    //FOREIGN IMPLEMENTATION -- START
                    var DAO = new EPMData(properties.SiteId);
                    rb.UpdateForeignKeys(DAO);
                    DAO.Dispose();
                    // -- END
                }
                catch (Exception ex)
                {
                    isSuccessful = false;
                    SPSecurity.RunWithElevatedPrivileges(delegate
                    {
                        if (!EventLog.SourceExists("EPMLive Reporting - UpdateForeignKeys"))
                        {
                            EventLog.CreateEventSource("EPMLive Reporting - UpdateForeignKeys", "EPM Live");
                        }

                        LogEntry(ex, "EPMLive Reporting - UpdateForeignKeys");
                    });
                }
                #endregion

                #region Cleanup list after field data type update to retain value
                try
                {
                    // List clean up -- START
                    var _DAO = new EPMData(properties.SiteId);
                    CleanupListAfterFieldUpdate(_DAO, properties.ListTitle);
                    // -- END
                }
                catch (Exception ex)
                {
                    isSuccessful = false;
                    SPSecurity.RunWithElevatedPrivileges(delegate
                    {
                        if (!EventLog.SourceExists("EPMLive Reporting - CleanupListAfterFieldUpdate"))
                        {
                            EventLog.CreateEventSource("EPMLive Reporting - CleanupListAfterFieldUpdate", "EPM Live");
                        }

                        LogEntry(ex, "EPMLive Reporting - CleanupListAfterFieldUpdate");
                    });
                }
                #endregion
            }
            return(isSuccessful);
        }