private bool OverrideVersion(ItemVersion v, ItemVersion over) { bool changed = false; foreach (var versioner in Versioners) { if (over.ContainsKey(versioner.VersionKey)) { IUser u = LyniconSecurityManager.Current.User; var legalVals = versioner.GetAllowedVersions(u); var overVal = over[versioner.VersionKey]; if (legalVals.Any(lVal => lVal.Equals(overVal))) { if (!v.ContainsKey(versioner.VersionKey) || (v[versioner.VersionKey] == null && overVal != null) || !v[versioner.VersionKey].Equals(overVal)) { v[versioner.VersionKey] = overVal; changed = true; } } } } return(changed); }
/// <summary> /// Get a list of VersionSelectionViewModels to create a version selector for a given user and a given current version /// </summary> /// <param name="u">The user</param> /// <param name="currVersion">The current version</param> /// <param name="abbreviated">Whether the display is abbreviated (not implemented)</param> /// <returns>List of VersionSelectionViewModels</returns> public List <VersionSelectionViewModel> SelectionViewModel(IUser u, ItemVersion currVersion, bool abbreviated) { var vm = new List <VersionSelectionViewModel>(); foreach (var v in Versioners.Where(v => currVersion.ContainsKey(v.VersionKey) && currVersion[v.VersionKey] != null).OrderBy(v => v.VersionKey)) { var versions = v.GetAllowedVersions(u); var selectList = new List <SelectListItem>(); string cssClass = null; foreach (object o in versions) { var iv = new ItemVersion { { v.VersionKey, o } }; var dv = v.DisplayItemVersion(iv); cssClass = dv.CssClass; var sli = new SelectListItem(); sli.Text = (abbreviated ? dv.Text + "|" : "") + dv.ListItem; sli.Value = JsonConvert.SerializeObject(o); sli.Selected = currVersion[v.VersionKey] != null && currVersion[v.VersionKey].Equals(o); selectList.Add(sli); } vm.Add(new VersionSelectionViewModel { Title = v.VersionKey, SelectList = selectList, VersionKey = v.VersionKey, CssClass = cssClass }); } return(vm); }
/// <summary> /// Make the ItemVersion applicable to a given type by removing any inapplicable keys /// </summary> /// <param name="version">The original ItemVersion</param> /// <param name="t">The type to which to make it applicable</param> /// <returns>The modified, applicable ItemVersion</returns> public ItemVersion GetApplicableVersion(ItemVersion version, Type t) { ItemVersion res = new ItemVersion(Versioners .Where(vnr => version.ContainsKey(vnr.VersionKey) && vnr.Versionable(t)) .ToDictionary(vnr => vnr.VersionKey, vnr => version[vnr.VersionKey])); return(res); }
/// <summary> /// Get all the displays for all the keys in an ItemVersion /// </summary> /// <param name="version">The ItemVersion to display</param> /// <returns>The VersionDisplays for all the keys of the ItemVersion</returns> public List <VersionDisplay> DisplayVersion(ItemVersion version) { return(Versioners .OrderBy(v => v.VersionKey) .Where(v => version.ContainsKey(v.VersionKey)) .Select(v => v.DisplayItemVersion(version)) .Where(vi => vi != null && !string.IsNullOrEmpty(vi.Text)) .ToList()); }
/// <summary> /// Set the container to have the ItemVersion supplied /// </summary> /// <param name="version">ItemVersion to set on the container</param> /// <param name="container">The container to set the ItemVersion for</param> public void SetVersion(ItemVersion version, object container) { foreach (var versioner in Versioners) { if (version.ContainsKey(versioner.VersionKey) && versioner.Versionable(container)) { versioner.SetItemVersion(version, container); } } }
/// <summary> /// There is a specific ItemVersion which is contained by both this ItemVersion and the argument /// </summary> /// <param name="other">The other ItemVersion</param> /// <returns>Whether an common specific ItemVersion exists</returns> public bool Overlaps(ItemVersion other) { foreach (var key in this.Keys) { if (other.ContainsKey(key) && other[key] != null && this[key] != null && !other[key].Equals(this[key])) { return(false); } } return(true); }
/// <summary> /// Where the other version contains a key, and this version contains a key, set this version's key to the other's value /// </summary> /// <param name="other">The version to overlay on this one</param> /// <returns></returns> public ItemVersion Overlay(ItemVersion other) { var overlaid = new ItemVersion(this); other.Do(kvp => { if (overlaid.ContainsKey(kvp.Key)) { overlaid[kvp.Key] = kvp.Value; } }); return(overlaid); }
/// <summary> /// Expand the abstract version to a list of fully-specified versions /// with all the keys of all the registered versions /// </summary> /// <param name="iv">Abstract version to expand</param> /// <returns>List of fully-specified versions</returns> public List <ItemVersion> ContainingVersions(ItemVersion iv) { var expIv = new ItemVersion(iv); foreach (var versioner in Versioners) { if (!iv.ContainsKey(versioner.VersionKey)) { expIv.Add(versioner.VersionKey, null); } } return(expIv.Expand()); }
private ItemVersion OverrideVersion(ItemVersion overridden, ItemVersion over) { bool changed = false; var v = overridden.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); // detected if we have recursed into this (on the current thread/request) and don't apply override if so bool blocked = RequestThreadCache.Current.ContainsKey(overrideBlockKey + UniqueId) ? ((bool?)RequestThreadCache.Current[overrideBlockKey + UniqueId] ?? false) : false; if (over.Count > 0 && !blocked) { IUser u = null; RequestThreadCache.Current[overrideBlockKey + UniqueId] = true; try { u = SecurityManager.Current?.User; } finally { RequestThreadCache.Current[overrideBlockKey + UniqueId] = false; } foreach (var versioner in Versioners) { if (over.ContainsKey(versioner.VersionKey)) { var legalVals = u == null ? new List <object> { versioner.PublicVersionValue } : versioner.GetAllowedVersions(u); var overVal = over[versioner.VersionKey]; if (legalVals.Any(lVal => lVal == null || lVal.Equals(overVal))) { if (!v.ContainsKey(versioner.VersionKey) || (v[versioner.VersionKey] == null && overVal != null) || !v[versioner.VersionKey].Equals(overVal)) { v[versioner.VersionKey] = overVal; changed = true; } } } } } return(changed ? new ItemVersion(v) : null); }
/// <summary> /// Make the ItemVersion applicable to a given type by removing any inapplicable keys /// </summary> /// <param name="version">The original ItemVersion</param> /// <param name="t">The type to which to make it applicable</param> /// <returns>The modified, applicable ItemVersion</returns> public ItemVersion GetApplicableVersion(ItemVersion version, Type t) { ItemVersion res = new ItemVersion(); foreach (var versioner in Versioners) { if (!version.ContainsKey(versioner.VersionKey)) { continue; } if (versioner.Versionable(t)) { res.Add(versioner.VersionKey, version[versioner.VersionKey]); } } return(res); }
public bool Equals(ItemVersion other) { if (other == null || this.Values.Where(v => v != null).Count() != other.Values.Where(v => v != null).Count()) { return(false); } foreach (var kvp in this) { if (kvp.Value == null) { continue; } if (!other.ContainsKey(kvp.Key) || other[kvp.Key] == null || !other[kvp.Key].Equals(kvp.Value)) { return(false); } } return(true); }
/// <summary> /// Find all specific ItemVersions which are applicable to a type /// </summary> /// <param name="t">The type</param> /// <returns>All applicable ItemVersion</returns> public List <ItemVersion> MatchingVersions(Type t) { var vsn = new ItemVersion(this); var other = VersionManager.Instance.VersionForType(t); foreach (var key in this.Keys) { if (!other.ContainsKey(key)) { vsn.Remove(key); } } foreach (var key in other.Keys) { if (!vsn.ContainsKey(key)) { vsn.Add(key, null); } } return(vsn.Expand()); }