public void Read(string id) { Feed <FeedEntry> result = new Feed <FeedEntry>(); //result.Updated = GetRandomDate(); result.Url = _request.Uri.ToString(); result.Author.Name = "Northwind Adapter"; string resource = TrimApostophes(id); if (resource != null) { _request.Response.FeedEntry = _wrapper.GetFeedEntry(resource); if (_request.Response.FeedEntry != null && !_request.Response.FeedEntry.IsDeleted && isActive(_request.Response.FeedEntry)) { _request.Response.FeedEntry.Title = _request.Response.FeedEntry.ToString(); } else { throw new DiagnosesException(Severity.Error, resource + " not found", DiagnosisCode.DatasetNotFound); } } else { result.Category.Scheme = "http://schemas.sage.com/sdata/categories"; result.Category.Term = "collection"; result.Category.Label = "Resource Collection"; string[] ids = _wrapper.GetFeed(); long start = Math.Max(0, _request.Uri.StartIndex - 1); //Startindex is 1-based long max = _request.Uri.Count == null ? ids.Length : Math.Min((long)_request.Uri.Count + start, (long)ids.Length); long entryCount = _request.Uri.Count == null ? DEFAULT_COUNT : (long)_request.Uri.Count; _request.Uri.Count = entryCount; for (long i = start; result.Entries.Count < entryCount && i < ids.Length; i++) { string entryId = ids[i]; FeedEntry entry = _wrapper.GetFeedEntry(entryId); if (entry != null) { entry.Title = entry.ToString(); entry.Links.AddRange(LinkFactory.CreateEntryLinks(_context, entry)); entry.Updated = DateTime.Now; result.Entries.Add(entry); } } result.Title = result.Entries.Count + " " + _context.ResourceKind.ToString(); HandlePaging(_request, result, ids); //FeedLink link = new FeedLink(_request.Uri.AppendPath("$schema").ToString(), LinkType.Schema, MediaType.Xml, "Schema"); FeedLink[] links = LinkFactory.CreateFeedLinks(_context, _request.Uri.ToString()); result.Links.AddRange(links); result.Updated = DateTime.Now; _request.Response.Feed = result; } }
/// <summary> /// /// </summary> /// <param name="config"></param> /// <returns></returns> /// <remarks>This method is not threadsafe as the performer must be finished when calling this method.</remarks> public Feed <FeedEntry> GetFeed(NorthwindConfig config, PageInfo normalizedPageInfo) { Feed <FeedEntry> syncSourceFeed; SdataContext sdataContext; SupportedResourceKinds resource; string resourceKind; string EndPoint; Guid trackingId; List <CorrelatedResSyncInfo> correlatedResSyncInfos; sdataContext = _parentPerformer._requestContext.SdataContext; resource = _parentPerformer._requestContext.ResourceKind; resourceKind = resource.ToString(); correlatedResSyncInfos = _parentPerformer._asyncStateObj.CorrelatedResSyncInfos; EndPoint = _parentPerformer._requestContext.DatasetLink + resourceKind; trackingId = _parentPerformer._requestContext.TrackingId; //ISyncSyncDigestInfoStore syncDigestStore = RequestReceiver.NorthwindAdapter.StoreLocator.GetSyncDigestStore(sdataContext); ISyncSyncDigestInfoStore syncDigestStore = NorthwindAdapter.StoreLocator.GetSyncDigestStore(sdataContext); SyncDigestInfo syncDigestInfo = syncDigestStore.Get(resourceKind); // Create a new Feed instance syncSourceFeed = new Feed <FeedEntry>();//FeedComponentFactory.Create<ISyncSourceFeed>(); syncSourceFeed.Author = new FeedAuthor(); syncSourceFeed.Author.Name = "Northwind Adapter"; syncSourceFeed.Category = new FeedCategory("http://schemas.sage.com/sdata/categories", "collection", "Resource Collection"); #region Digest syncSourceFeed.Digest = new Digest(); syncSourceFeed.Digest.Entries = (syncDigestInfo == null) ? new DigestEntry[0] : new DigestEntry[syncDigestInfo.Count]; // set digest origin syncSourceFeed.Digest.Origin = _parentPerformer._requestContext.OriginEndPoint; if (syncDigestInfo != null) { // convert and set digest entries from synch store object to feed object for (int i = 0; i < syncDigestInfo.Count; i++) { syncSourceFeed.Digest.Entries[i] = new DigestEntry(); syncSourceFeed.Digest.Entries[i].ConflictPriority = syncDigestInfo[i].ConflictPriority; syncSourceFeed.Digest.Entries[i].EndPoint = syncDigestInfo[i].EndPoint; syncSourceFeed.Digest.Entries[i].Tick = (int)syncDigestInfo[i].Tick; syncSourceFeed.Digest.Entries[i].Stamp = DateTime.Now; } } #endregion #region Entries // retrieve the data connection wrapper IFeedEntryEntityWrapper wrapper = FeedEntryWrapperFactory.Create(resource, _parentPerformer._requestContext); IEnumerator <CorrelatedResSyncInfo> correlationEnumerator = PagingHelpers.GetPagedEnumerator <CorrelatedResSyncInfo>(normalizedPageInfo, correlatedResSyncInfos.ToArray()); while (correlationEnumerator.MoveNext()) { syncSourceFeed.Entries.Add(this.BuildFeedEntry(correlationEnumerator.Current, wrapper)); } #endregion // initialize the feed string feedUrl = string.Format("{0}/$syncSource('{1}')", EndPoint, trackingId); string feedUrlWithoutQuery = (new Uri(feedUrl)).GetLeftPart(UriPartial.Path); // the url without query // set id tag syncSourceFeed.Id = feedUrl; // set title tag syncSourceFeed.Title = string.Format("{0} synchronization source feed {1}", resourceKind.ToString(), trackingId); // set update // syncSourceFeed.Updated = DateTime.Now; // set syncMode syncSourceFeed.SyncMode = SyncMode.catchUp;// syncModeenum.catchUp; // add links (general) syncSourceFeed.Links.AddRange(LinkFactory.CreateFeedLinks(_parentPerformer._requestContext, feedUrl)); #region PAGING & OPENSEARCH // add links (paging) syncSourceFeed.Links.AddRange(LinkFactory.CreatePagingLinks(normalizedPageInfo, correlatedResSyncInfos.Count, feedUrlWithoutQuery)); /* OPENSEARCH */ syncSourceFeed.ItemsPerPage = normalizedPageInfo.Count; syncSourceFeed.StartIndex = normalizedPageInfo.StartIndex; syncSourceFeed.TotalResults = correlatedResSyncInfos.Count; #endregion return(syncSourceFeed); }
public void Read() { string resourceKindName; EntryRequestType entryRequestType; // Multi or Single bool includeResourcePayloads; ICorrelatedResSyncInfoStore correlatedResSyncStore; CorrelatedResSyncInfo[] correlatedResSyncInfos; int totalResult; // number of correlations found PageInfo normalizedPageInfo; // normalized pageInfo -> will be used for paging, etc. resourceKindName = _requestContext.ResourceKind.ToString(); // multi or single entry request? entryRequestType = (String.IsNullOrEmpty(_requestContext.ResourceKey)) ? EntryRequestType.Multi : EntryRequestType.Single; // get store to request the correlations correlatedResSyncStore = NorthwindAdapter.StoreLocator.GetCorrelatedResSyncStore(_requestContext.SdataContext); // receive correlated resource synchronization entries if (entryRequestType == EntryRequestType.Multi) { int pageNumber = PagingHelpers.GetPageNumber(_request.Uri.StartIndex, _request.Uri.Count); correlatedResSyncInfos = correlatedResSyncStore.GetPaged(resourceKindName, pageNumber, (int)(_request.Uri.Count ?? DEFAULT_ITEMS_PER_PAGE), out totalResult); } else { Guid uuid = (Guid)TypeDescriptor.GetConverter(typeof(Guid)).ConvertFrom(_requestContext.ResourceKey); correlatedResSyncInfos = correlatedResSyncStore.GetByUuid(resourceKindName, new Guid[] { uuid }); totalResult = correlatedResSyncInfos.Length; if (totalResult > 1) { throw new ApplicationException("More than one resource for uuid exists."); } if (totalResult == 0) { throw new RequestException("No resource found"); } } // If the service consumer only needs to retrieve the URL, not the actual payload, // it may do so by adding an empty select parameter to its request: // a) select parameter not exist -> return deep resource payload // b) select exists and empty -> return no resource payload // c) select exists and not empty -> return deep resource payload as we do not yet support payload filtering // with select parameter. string tmpValue; // ?select includeResourcePayloads = true; // default value, but check for select parameter now if (_requestContext.SdataUri.QueryArgs.TryGetValue("select", out tmpValue)) { if (string.IsNullOrEmpty(_requestContext.SdataUri.QueryArgs["select"])) { includeResourcePayloads = false; } } if (entryRequestType == EntryRequestType.Single) { IFeedEntryEntityWrapper wrapper = null; //if (includeResourcePayloads) //TODO: Comment this in as soon as there is a possibility to send empty payload objects wrapper = FeedEntryWrapperFactory.Create(_requestContext.ResourceKind, _requestContext); _request.Response.FeedEntry = this.BuildFeedEntryForCorrelation(correlatedResSyncInfos[0], wrapper); _request.Response.ContentType = MediaType.AtomEntry; } else { // Create an empty feed Feed <FeedEntry> feed = new Feed <FeedEntry>(); feed.Author = new FeedAuthor(); feed.Author.Name = "Northwind Adapter"; feed.Category = new FeedCategory("http://schemas.sage.com/sdata/categories", "collection", "Resource Collection"); DateTime updateTime = DateTime.Now; string feedUrl = _requestContext.SdataUri.ToString(); // the url requested string feedUrlWithoutQuery = (new Uri(feedUrl)).GetLeftPart(UriPartial.Path); // the url without query normalizedPageInfo = PagingHelpers.Normalize(_request.Uri.StartIndex, _request.Uri.Count, totalResult); IFeedEntryEntityWrapper wrapper = null; //if (includeResourcePayloads) //TODO: Comment this in as soon as there is a possibility to send empty payload objects wrapper = FeedEntryWrapperFactory.Create(_requestContext.ResourceKind, _requestContext); // create a feed entry for each correlation. IEnumerator <CorrelatedResSyncInfo> pagedCorrelationEnumerator = PagingHelpers.GetPagedEnumerator <CorrelatedResSyncInfo>(normalizedPageInfo, correlatedResSyncInfos); while (pagedCorrelationEnumerator.MoveNext()) { // Create and append a feed entry per each id FeedEntry entry = this.BuildFeedEntryForCorrelation(pagedCorrelationEnumerator.Current, wrapper); if (entry != null) { feed.Entries.Add(entry); } } // set feed title feed.Title = string.Format("{0} linked {1}", feed.Entries.Count, resourceKindName); // set feed update feed.Updated = updateTime; // set id feed.Id = feedUrl; // add links (general) feed.Links.AddRange(LinkFactory.CreateFeedLinks(_requestContext, feedUrl)); #region PAGING & OPENSEARCH // add links (paging) feed.Links.AddRange(LinkFactory.CreatePagingLinks(normalizedPageInfo, totalResult, feedUrlWithoutQuery)); /* OPENSEARCH */ feed.ItemsPerPage = normalizedPageInfo.Count; feed.StartIndex = normalizedPageInfo.StartIndex; feed.TotalResults = totalResult; #endregion _request.Response.Feed = feed; _request.Response.ContentType = MediaType.Atom; } }
/// <summary> /// /// </summary> /// <param name="config"></param> /// <returns></returns> /// <remarks>This method is not threadsafe as the performer must be finished when calling this method.</remarks> public Feed <FeedEntry> GetFeed(NorthwindConfig config, PageInfo normalizedPageInfo) { Feed <FeedEntry> syncTargetFeed = new Feed <FeedEntry>(); syncTargetFeed.Author = new FeedAuthor(); syncTargetFeed.Author.Name = "Northwind Adapter"; syncTargetFeed.Category = new FeedCategory("http://schemas.sage.com/sdata/categories", "collection", "Resource Collection"); SdataContext sdataContext; SupportedResourceKinds resource; string resourceKind; string EndPoint; Guid trackingId; List <SdataTransactionResult> transactinResults; sdataContext = _parentPerformer._requestContext.SdataContext; resource = _parentPerformer._requestContext.ResourceKind; resourceKind = resource.ToString(); transactinResults = _parentPerformer._asyncStateObj.TransactionResults; EndPoint = _parentPerformer._requestContext.DatasetLink + resourceKind;; trackingId = _parentPerformer._requestContext.TrackingId; // Create a new Feed instance // retrieve the data connection wrapper IFeedEntryEntityWrapper wrapper = FeedEntryWrapperFactory.Create(resource, _parentPerformer._requestContext); IEnumerator <SdataTransactionResult> transactionResultEnumerator = PagingHelpers.GetPagedEnumerator <SdataTransactionResult>(normalizedPageInfo, transactinResults.ToArray()); while (transactionResultEnumerator.MoveNext()) { syncTargetFeed.Entries.Add(this.BuildFeedEntry(transactionResultEnumerator.Current, wrapper)); } // initialize the feed string feedUrl = string.Format("{0}/$syncTarget('{1}')", EndPoint, trackingId); string feedUrlWithoutQuery = (new Uri(feedUrl)).GetLeftPart(UriPartial.Path); // the url without query // set id tag syncTargetFeed.Id = feedUrl; // set title tag syncTargetFeed.Title = string.Format("{0} synchronization target feed {1}", resourceKind.ToString(), trackingId); // set update #warning implement this //syncTargetFeed.Updated = DateTime.Now; // set syncMode syncTargetFeed.SyncMode = SyncMode.catchUp; // add links (general) syncTargetFeed.Links.AddRange(LinkFactory.CreateFeedLinks(_parentPerformer._requestContext, feedUrl)); #region PAGING & OPENSEARCH // add links (paging) syncTargetFeed.Links.AddRange(LinkFactory.CreatePagingLinks(normalizedPageInfo, transactinResults.Count, feedUrlWithoutQuery)); /* OPENSEARCH */ syncTargetFeed.ItemsPerPage = normalizedPageInfo.Count; syncTargetFeed.StartIndex = normalizedPageInfo.StartIndex; syncTargetFeed.TotalResults = transactinResults.Count; #endregion return(syncTargetFeed); }