private ConfigurationDocument GetConfigurationDocument(ConfigurationDocumentKey documentKey, bool prior)
        {
            IQueryable <ConfigurationDocument> query = from d in Context.ConfigurationDocuments select d;

            query = !string.IsNullOrEmpty(documentKey.InstanceKey)
                ? query.Where(d => d.InstanceKey == documentKey.InstanceKey)
                : query.Where(d => d.InstanceKey == null);

            query = !string.IsNullOrEmpty(documentKey.User)
                ? query.Where(d => d.User == documentKey.User)
                : query.Where(d => d.User == null);

            query = query.Where(d => d.DocumentName == documentKey.DocumentName);

            var paddedVersionString = VersionUtils.ToPaddedVersionString(documentKey.Version, false, false);

            if (prior)
            {
                query = query.Where(d => d.DocumentVersionString.CompareTo(paddedVersionString) < 0);
                //You want the most recent prior version.
                query = query.OrderByDescending(d => d.DocumentVersionString);
            }
            else
            {
                query = query.Where(d => d.DocumentVersionString == paddedVersionString);
            }

            return(query.FirstOrDefault());
        }
Пример #2
0
        public void PutSettingsValues(SettingsGroupDescriptor group, string user, string instanceKey,
                                      Dictionary <string, string> dirtyValues)
        {
            using (var context = new DataAccessContext())
            {
                // next we obtain any previously stored configuration document for this settings group
                var documentKey = new ConfigurationDocumentKey(group.Name, group.Version, user, instanceKey);
                var broker      = context.GetConfigurationDocumentBroker();

                var values = new Dictionary <string, string>();
                var parser = new SettingsParser();

                var document = broker.GetConfigurationDocument(documentKey);
                if (document == null)
                {
                    document = new ConfigurationDocument
                    {
                        CreationTime          = Platform.Time,
                        DocumentName          = group.Name,
                        DocumentVersionString = VersionUtils.ToPaddedVersionString(group.Version, false, false),
                        User         = user,
                        DocumentText = string.Empty
                    };
                    broker.AddConfigurationDocument(document);
                }
                else
                {
                    // parse document
                    parser.FromXml(document.DocumentText, values);
                }

                // update the values that have changed
                foreach (var kvp in dirtyValues)
                {
                    values[kvp.Key] = kvp.Value;
                }

                try
                {
                    if (values.Count > 0)
                    {
                        // generate the document, update local cache and server
                        document.DocumentText = parser.ToXml(values);
                        context.Commit();
                    }
                }
                catch (EndpointNotFoundException e)
                {
                    Platform.Log(LogLevel.Debug, e, "Unable to save settings to configuration service.");
                }
            }
        }