private static string AddVisualProperties(PivotViewerItem item, Uri baseUri, string originalImagePath, bool isDefaultBase) { if (originalImagePath == null) { return(null); } var separatorIndex = originalImagePath.LastIndexOf(CollectionIndexSeparator, StringComparison.OrdinalIgnoreCase); if (separatorIndex == -1) { throw new InvalidDataException($"The path {originalImagePath} is not path to an image in a DeepZoom collection."); } var imageNumberString = originalImagePath.Substring(separatorIndex + CollectionIndexSeparator.Length); if (string.IsNullOrEmpty(imageNumberString)) { throw new InvalidDataException($"The path {originalImagePath} has nothing after the {CollectionIndexSeparator}."); } if (!int.TryParse(imageNumberString, NumberStyles.Any, CultureInfo.InvariantCulture, out var imageNumber) || imageNumber < 0) { throw new InvalidDataException($"The path {originalImagePath.Substring(separatorIndex)} is not a valid image path."); } string absoluteUri; if (separatorIndex == 0) { if (baseUri == null || isDefaultBase) { throw new InvalidDataException($"No default collection was given for {imageNumber}."); } absoluteUri = baseUri.AbsoluteUri; } else { absoluteUri = originalImagePath.Substring(0, separatorIndex); } try { var uri = new Uri(baseUri, absoluteUri); item.Properties[PivotViewerProperty.VisualCollectionSourceKey] = new PivotViewerPropertyValue(uri.ToString()); item.Properties[PivotViewerProperty.VisualImageIdKey] = new PivotViewerPropertyValue(imageNumber); return(string.Concat(uri, CollectionIndexSeparator, imageNumber)); } catch (Exception ex) when(!(ex is InvalidDataException)) { } return(null); }
private static void ParseItemExtension(Item rcItem, PivotViewerItem pvItem, Uri baseHrefUri) { if (rcItem.Extension == null) { return; } if (rcItem.Extension.Related != null) { var comparables = new List <IComparable>(rcItem.Extension.Related.Length); foreach (var link in rcItem.Extension.Related) { comparables.Add(ParseHyperlink(baseHrefUri, link)); } pvItem.Properties.Add(PivotViewerProperty.RelatedCollectionKey, new PivotViewerPropertyValue(comparables)); } if (rcItem.Extension.Copyright != null) { var link = ParseHyperlink(baseHrefUri, rcItem.Extension.Copyright); pvItem.Properties.Add(PivotViewerProperty.CopyrightKey, new PivotViewerPropertyValue(link)); } }
private void ParseFacetValuesOnItem(Uri baseHrefUri, Item remoteItem, PivotViewerItem pvItem, ICollection <PivotViewerProperty> allowableFields) { if (remoteItem?.Facets?.Facet == null) { return; } foreach (Facet _facet in remoteItem.Facets.Facet) { if (string.IsNullOrEmpty(_facet.Name)) { throw new InvalidDataException($"Filters must have a name, error on item id {remoteItem.Id}."); } if (!facetCategories.TryGetValue(_facet.Name.ToLower(CultureInfo.InvariantCulture), out var variantField)) { throw new InvalidDataException($"The property named '{_facet.Name}' on item {remoteItem.Id} was not defined in the filter categories."); } if (allowableFields != null) { if (!allowableFields.Contains(variantField)) { throw new InvalidDataException($"Facets in the supplement file cannot be filter visible or wordwheel visible. Cannot assign a value to filter '{variantField.FieldName}' on item {remoteItem.Id} in the supplement file."); } if (pvItem.Properties.ContainsKey(variantField)) { throw new InvalidDataException($"Cannot assign a value to filter '{variantField.FieldName}' on item {remoteItem.Id} since that item already has values assigned in the primary file."); } } var comparables = new List <IComparable>(); switch (variantField.PropertyType) { case PivotViewerPropertyType.DateTime: if (_facet.DateTime?.Length > 0) { comparables.AddRange(_facet.DateTime.Select(v => (IComparable)v.Value)); } break; case PivotViewerPropertyType.Decimal: if (_facet.Number?.Length > 0) { comparables.AddRange(_facet.Number.Select(v => (IComparable)v.Value)); } break; case PivotViewerPropertyType.Text: var strings = variantField.IsWrappingText ? _facet.LongString?.Select(v => v.Value) : _facet.String?.Select(v => PivotViewerProperty.NewlineRegex.Replace(v.Value, " ")); if (strings != null) { comparables.AddRange(strings.Select(v => v.Trim()).Where(v => !string.IsNullOrEmpty(v)).Cast <IComparable>()); } break; case PivotViewerPropertyType.Link: if (_facet.Link?.Length > 0) { comparables.AddRange(_facet.Link.Select(v => (IComparable)ParseHyperlink(baseHrefUri, v))); } break; default: throw new InvalidDataException($"Unknown property type {variantField.PropertyType}."); } if (comparables.Count == 0) { throw new InvalidDataException($"No {variantField.PropertyType} values were provided for {variantField}."); } pvItem.Properties.Add(variantField, new PivotViewerPropertyValue(comparables)); } }
private void ParseItems() { foreach (var itemList in remoteCollection.Items) { if (itemList.Item == null) { continue; } var hrefBaseUri = itemList.HrefBase != null ? new Uri(UriSource, itemList.HrefBase) : UriSource; var imageBaseUri = UriSource; var isDefaultBase = true; if (itemList.ImgBase != null) { if (itemList.ImgBase.Contains(CollectionIndexSeparator)) { throw new InvalidDataException($"The image base {itemList.ImgBase} cannot contain a collection index."); } imageBaseUri = new Uri(UriSource, itemList.ImgBase); isDefaultBase = false; } foreach (var item in itemList.Item) { if (pivotViewerItems.ContainsKey(item.Id)) { throw new InvalidDataException($"Item ID {item.Id} was not unique."); } var pivotViewerItem = new PivotViewerItem(); if (item.Name != null) { pivotViewerItem.Properties.Add(PivotViewerProperty.NameKey, new PivotViewerPropertyValue(item.Name)); } if (item.Description != null) { pivotViewerItem.Properties.Add(PivotViewerProperty.DescriptionKey, new PivotViewerPropertyValue(item.Description)); } if (!string.IsNullOrEmpty(item.Id)) { pivotViewerItem.Id = item.Id; } if (item.Img != null) { var serializedImage = AddVisualProperties(pivotViewerItem, imageBaseUri, item.Img, isDefaultBase); if (serializedImage != null) { usedImages.Add(serializedImage); } } ParseFacetValuesOnItem(hrefBaseUri, item, pivotViewerItem, null); if (item.Href != null) { var href = new Uri(hrefBaseUri, item.Href); pivotViewerItem.Properties.Add(PivotViewerProperty.HrefKey, new PivotViewerPropertyValue(href.AbsoluteUri)); } ParseItemExtension(item, pivotViewerItem, hrefBaseUri); if (pivotViewerItem.Copyright == null) { pivotViewerItem.Copyright = Copyright; } pivotViewerItems.Add(pivotViewerItem.Id, pivotViewerItem); foreach (var property in pivotViewerItem.Properties.Keys) { if (!pivotViewerItemProperties.TryGetValue(property.FieldName, out var pivotViewerProperty)) { pivotViewerItemProperties.Add(property.FieldName, property); } else if (!property.Equals(pivotViewerProperty)) { throw new ArgumentException($"A property with id {property.FieldName} already exists"); } } } } }