Exemplo n.º 1
0
        /// <summary>
        /// Gets the protein with the given project id.
        /// </summary>
        /// <param name="projectId">The project id of the protein to return.</param>
        /// <param name="allowRefresh">true to allow this Get method to refresh the service.</param>
        /// <returns>The protein object with the given project id or null if the protein does not exist.</returns>
        public Protein Get(int projectId, bool allowRefresh)
        {
            if (_dictionary.ContainsKey(projectId))
            {
                return(_dictionary[projectId]);
            }

            if (!allowRefresh || projectId == 0 || CheckProjectsNotFound(projectId))
            {
                return(null);
            }

            if (_downloader != null)
            {
                Logger.InfoFormat("Project ID {0} triggering project data refresh.", projectId);

                try
                {
                    Refresh();
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat(ex, "{0}", ex.Message);
                }

                if (_dictionary.ContainsKey(projectId))
                {
                    // remove it from the not found list and return it
                    _projectsNotFound.Remove(projectId);
                    return(_dictionary[projectId]);
                }

                // update the last download attempt date
                _projectsNotFound[projectId] = DateTime.Now;
            }

            return(null);
        }
        /// <summary>
        /// Gets the protein with the given project id.
        /// </summary>
        /// <param name="projectId">The project id of the protein to return.</param>
        /// <param name="allowRefresh">true to allow this Get method to refresh the service.</param>
        /// <returns>The protein object with the given project id or null if the protein does not exist.</returns>
        public Protein Get(int projectId, bool allowRefresh)
        {
            if (_dictionary.ContainsKey(projectId))
            {
                return(_dictionary[projectId]);
            }

            if (projectId == 0 || !allowRefresh || !CanRefresh(projectId))
            {
                return(null);
            }

            if (_downloader != null)
            {
                Logger.Info(String.Format("Project ID {0} triggering project data refresh.", projectId));

                try
                {
                    Refresh(null);
                }
                catch (Exception ex)
                {
                    Logger.Error(ex.Message, ex);
                }

                if (_dictionary.ContainsKey(projectId))
                {
                    return(_dictionary[projectId]);
                }

                // update the last download attempt date
                _projectsNotFound[projectId] = DateTime.UtcNow;
            }

            return(null);
        }
        /// <summary>
        /// Refreshes the service data and returns a collection of objects detailing how the service data was changed.
        /// </summary>
        /// <param name="progress">The object used to report refresh progress.</param>
        /// <returns>A collection of objects detailing how the service data was changed</returns>
        public IReadOnlyCollection <ProteinDictionaryChange> Refresh(IProgress <ProgressInfo> progress)
        {
            IReadOnlyCollection <ProteinDictionaryChange> dictionaryChanges;

            using (var stream = new MemoryStream())
            {
                Logger.Info("Downloading new project data from Stanford...");
                _downloader.Download(stream, progress);
                stream.Position = 0;

                var serializer    = new ProjectSummaryJsonDeserializer();
                var newDictionary = ProteinDictionary.CreateFromExisting(_dictionary, serializer.Deserialize(stream));
                dictionaryChanges = newDictionary.Changes;
                _dictionary       = newDictionary;
            }

            foreach (var info in dictionaryChanges.Where(info => info.Result != ProteinDictionaryChangeResult.NoChange))
            {
                Logger.Info(info.ToString());
            }

            var now = DateTime.UtcNow;

            foreach (var key in _projectsNotFound.Keys.ToList())
            {
                if (_dictionary.ContainsKey(key))
                {
                    _projectsNotFound.Remove(key);
                }
                else
                {
                    _projectsNotFound[key] = now;
                }
            }
            _lastRefreshTime = now;

            Write();
            return(dictionaryChanges);
        }