Exemplo n.º 1
0
        /// <summary>
        /// Create the store.
        /// </summary>
        /// <param name="learningStore">The learning store to use.</param>
        /// <param name="cacheSettings">Cache settings, including the file system location where the packages in the store can be cached.
        /// </param>
        /// <remarks>
        /// </remarks>
        public SharePointPackageStore(LearningStore learningStore, SharePointCacheSettings cacheSettings)
            : base(learningStore)
        {
            Utilities.ValidateParameterNonNull("learningStore", learningStore);
            Utilities.ValidateParameterNonNull("cacheSettings", cacheSettings);

            m_cacheSettings = cacheSettings;
        }
        private static SlkSettings LoadSettings(SlkSPSiteMapping mapping, SPSite site, XmlSchema xmlSchema)
        {
            // create a LearningStore. Read is in privileged scope so irrelevant what key is.
            // Cannot use current user as may be be called in a page PreInit event when it's not necessarily valid
            string        learningStoreKey = "SHAREPOINT\\System";
            LearningStore learningStore    = new LearningStore(mapping.DatabaseConnectionString, learningStoreKey, ImpersonationBehavior.UseOriginalIdentity);

            // read the SLK Settings file from the database into <settings>
            SlkSettings settings;

            using (LearningStorePrivilegedScope privilegedScope = new LearningStorePrivilegedScope())
            {
                LearningStoreJob   job   = learningStore.CreateJob();
                LearningStoreQuery query = learningStore.CreateQuery(Schema.SiteSettingsItem.ItemTypeName);
                query.AddColumn(Schema.SiteSettingsItem.SettingsXml);
                query.AddColumn(Schema.SiteSettingsItem.SettingsXmlLastModified);
                query.AddCondition(Schema.SiteSettingsItem.SiteGuid, LearningStoreConditionOperator.Equal, mapping.SPSiteGuid);
                job.PerformQuery(query);
                DataRowCollection dataRows = job.Execute <DataTable>().Rows;
                if (dataRows.Count != 1)
                {
                    throw new SafeToDisplayException(SlkCulture.GetResources().SlkSettingsNotFound, site.Url);
                }
                DataRow  dataRow                 = dataRows[0];
                string   settingsXml             = (string)dataRow[0];
                DateTime settingsXmlLastModified = ((DateTime)dataRow[1]);
                using (StringReader stringReader = new StringReader(settingsXml))
                {
                    XmlReaderSettings xmlSettings = new XmlReaderSettings();
                    xmlSettings.Schemas.Add(xmlSchema);
                    xmlSettings.ValidationType = ValidationType.Schema;
                    using (XmlReader xmlReader = XmlReader.Create(stringReader, xmlSettings))
                    {
                        settings = new SlkSettings(xmlReader, settingsXmlLastModified);
                    }
                }
            }

            return(settings);
        }
Exemplo n.º 3
0
        public LearningStoreQuery CreateQuery(LearningStore learningStore, bool countOnly, MacroResolver macroResolver, out int[,] columnMap)
        {
            // Check parameters
            if (learningStore == null)
            {
                throw new ArgumentNullException("learningStore");
            }

            // create a new LearningStore query
            LearningStoreQuery query = learningStore.CreateQuery(ViewName);

            // add column(s) to the query
            if (countOnly)
            {
                // add a single column -- the caller only wants the count of rows; if the SLK Settings
                // file specified a column to use in this situation, use it, otherwise use the first
                // column in the query
                if (CountViewColumnName != null)
                {
                    query.AddColumn(CountViewColumnName);
                }
                else
                {
                    query.AddColumn(Columns[0].ViewColumnName);
                }
                columnMap = null;
            }
            else
            {
                // add columns to the query as specified in this query definition; create <columnMap>
                // in the process; <viewColumnsAdded> is used to avoid adding duplicate view columns,
                // and to map view column names to their column index in the query results
                columnMap = new int[m_columnDefinitions.Count, ColumnDefinition.MaxViewColumns];
                Dictionary <string, int> viewColumnsAdded = new Dictionary <string, int>(m_columnDefinitions.Count * ColumnDefinition.MaxViewColumns);
                int iColumnDefinition = 0;
                foreach (ColumnDefinition column in Columns)
                {
                    for (int iViewColumn = 0; iViewColumn < ColumnDefinition.MaxViewColumns; iViewColumn++)
                    {
                        string viewColumnName = column.m_viewColumnNames[iViewColumn];
                        if (viewColumnName == null)
                        {
                            continue;
                        }
                        int iQueryColumn;
                        if (!viewColumnsAdded.TryGetValue(viewColumnName, out iQueryColumn))
                        {
                            try
                            {
                                query.AddColumn(viewColumnName);
                            }
                            catch (InvalidOperationException)
                            {
                                throw new SlkSettingsException(column.LineNumber, culture.Resources.SlkUtilitiesColumnNotDefined, viewColumnName, ViewName);
                            }
                            iQueryColumn = viewColumnsAdded.Count;
                            viewColumnsAdded.Add(viewColumnName, iQueryColumn);
                        }
                        columnMap[iColumnDefinition, iViewColumn] = iQueryColumn;
                    }

                    iColumnDefinition++;
                }
            }

            // add conditions to the query as specified in this query definition
            foreach (ConditionDefinition condition in Conditions)
            {
                object value = null;

                if (condition.MacroName != null)
                {
                    if (macroResolver != null)
                    {
                        value = macroResolver.Resolve(condition.MacroName);
                    }

                    if (value == null) // Either macroResolver is null or macro resolves to null
                    {
                        if (condition.NoConditionOnNull)
                        {
                            // No need to add condition
                            continue;
                        }
                        else
                        {
                            throw new SlkSettingsException(condition.LineNumber, culture.Resources.SlkUtilitiesMacroNotDefined, condition.MacroName);
                        }
                    }
                }
                else if (condition.Value != null)
                {
                    value = new XmlValue(condition.Value);
                }
                else
                {
                    value = null;
                }

                query.AddCondition(condition.ViewColumnName, condition.Operator, value);
            }

            // add sorts to the query as specified in this query definition (unless the caller only
            // wants a count of rows)
            if (!countOnly)
            {
                foreach (SortDefinition sort in Sorts)
                {
                    query.AddSort(sort.ViewColumnName,
                                  sort.Ascending ? LearningStoreSortDirection.Ascending
                            : LearningStoreSortDirection.Descending);
                }
            }

            // done
            return(query);
        }
        static void UpdateSlkSettings(string connectionString, Guid spSiteGuid, string settingsFileContents, string defaultSettingsFileContents)
        {
            // make sure we can access LearningStore; while we're at it, find out if there's a row
            // corresponding to this SPSite in the SiteSettingsItem table
            LearningStore      learningStore = new LearningStore(connectionString, "", true);
            LearningStoreJob   job           = learningStore.CreateJob();
            LearningStoreQuery query         = learningStore.CreateQuery(Schema.SiteSettingsItem.ItemTypeName);

            query.AddColumn(Schema.SiteSettingsItem.SettingsXml);
            query.AddCondition(Schema.SiteSettingsItem.SiteGuid, LearningStoreConditionOperator.Equal, spSiteGuid);
            job.PerformQuery(query);
            DataRowCollection results = job.Execute <DataTable>().Rows;

            if (results.Count == 0)
            {
                // this SPSite isn't listed in the SiteSettingsItem table, so we need to add a row
                if (settingsFileContents == null)
                {
                    settingsFileContents = defaultSettingsFileContents;
                }
            }
            else
            {
                object currentSettingsFileContents = results[0][Schema.SiteSettingsItem.SettingsXml];
                if ((currentSettingsFileContents == null) ||
                    (currentSettingsFileContents is DBNull) ||
                    (((string)currentSettingsFileContents).Length == 0))
                {
                    // the SLK Settings for this SPSite are missing, so we need to add them
                    if (settingsFileContents == null)
                    {
                        settingsFileContents = defaultSettingsFileContents;
                    }
                }
            }

            // upload the SLK Settings file if needed
            if (settingsFileContents != null)
            {
                // load "SlkSettings.xsd" from a resource into <xmlSchema>
                XmlSchema xmlSchema;
                using (StringReader schemaStringReader = new StringReader(SlkCulture.GetDefaultResources().SlkSettingsSchema))
                {
                    xmlSchema = XmlSchema.Read(schemaStringReader,
                                               delegate(object sender2, ValidationEventArgs e2)
                    {
                        // ignore warnings (already displayed when SLK Settings file was uploaded)
                    });
                }

                // validate <settingsFileContents>
                using (StringReader stringReader = new StringReader(settingsFileContents))
                {
                    XmlReaderSettings xmlSettings = new XmlReaderSettings();
                    xmlSettings.Schemas.Add(xmlSchema);
                    xmlSettings.ValidationType = ValidationType.Schema;
                    using (XmlReader xmlReader = XmlReader.Create(stringReader, xmlSettings))
                    {
                        try
                        {
                            SlkSettings settings = new SlkSettings(xmlReader, DateTime.MinValue);
                        }
                        catch (SlkSettingsException ex)
                        {
                            throw new SafeToDisplayException(LoadCulture(spSiteGuid).Resources.SlkSettingsFileError, ex.Message);
                        }
                    }
                }

                // store <settingsFileContents> in the database
                job = learningStore.CreateJob();
                Dictionary <string, object> uniqueProperties = new Dictionary <string, object>();
                uniqueProperties.Add(Schema.SiteSettingsItem.SiteGuid, spSiteGuid);
                Dictionary <string, object> updateProperties = new Dictionary <string, object>();
                updateProperties.Add(Schema.SiteSettingsItem.SettingsXml, settingsFileContents);
                updateProperties.Add(Schema.SiteSettingsItem.SettingsXmlLastModified,
                                     DateTime.Now.ToUniversalTime());
                job.AddOrUpdateItem(Schema.SiteSettingsItem.ItemTypeName, uniqueProperties,
                                    updateProperties);
                job.Execute();
            }
        }