public void ImportTags(IEnumerable <ModuleItem> items) { foreach (var item in items.Where(i => i.GlobalTagId != -1)) { if (!Classes.ContainsKey(item.ClassCode)) { Classes.Add(item.ClassCode, new TagClass(item.ClassCode, item.ClassName)); ItemsByClass.Add(item.ClassCode, new List <ModuleItem>()); } if (!ItemsById.ContainsKey(item.GlobalTagId)) { ItemsById.Add(item.GlobalTagId, item); ItemsByClass[item.ClassCode].Add(item); InstancesById.Add(item.GlobalTagId, new List <ModuleItem> { item }); } if (!InstancesById[item.GlobalTagId].Any(i => i.Module.FileName == item.Module.FileName)) { InstancesById[item.GlobalTagId].Add(item); } } }
/// <summary> /// Removes an item from the database completely. /// </summary> /// <param name="itemDefinition">Used to identify the particular item</param> /// <param name="context"></param> /// <returns></returns> public override bool DeleteItem(ItemDefinition itemDefinition, CallContext context) { if (ItemsById.ContainsKey(itemDefinition.ID)) { // recursively remove descendants foreach (var child in ItemsByParentId.Where(item => item.Key == itemDefinition.ID)) { DeleteItem( new ItemDefinition(ParseId(child.Value.ID), child.Value.Name, ParseId(child.Value.TemplateID), ParseId(child.Value.BranchId)), context); } // remove the item ItemsById.Remove(itemDefinition.ID); foreach ( var remove in ItemsByParentId.Where( it => it.Value != null && it.Value.ID.Equals(GetIdAsString(itemDefinition.ID))).ToList()) { ItemsByParentId.Remove(remove); } return(true); } return(false); }
/// <summary> /// Create a new item as a child of another item. /// Note that this does not create any versions or field values. /// </summary> /// <param name="itemID">The item ID (not the parent's)</param> /// <param name="itemName">The name of the new item</param> /// <param name="templateID">The ID of the content item that represents its template</param> /// <param name="parent">The parent item's definition</param> /// <param name="context"></param> /// <returns></returns> public override bool CreateItem(ID itemID, string itemName, ID templateID, ItemDefinition parent, CallContext context) { if (ItemsById.ContainsKey(itemID)) { // item already exists return(false); } if (parent != null) { if (!ItemsById.ContainsKey(parent.ID)) { // parent item does not exist in this provider return(false); } } SyncItem newItem = new SyncItem() { ID = GetIdAsString(itemID), Name = itemName, TemplateID = GetIdAsString(templateID), ParentID = GetIdAsString(parent != null ? parent.ID : new ID(Guid.Empty)), ItemPath = parent == null?string.Format("/{0}", itemName) : string.Format("{0}/{1}", GetItemPath(parent.ID), itemName), BranchId = GetIdAsString(new ID(Guid.Empty)), DatabaseName = Database.Name }; AddItem(newItem); AddVersion(new ItemDefinition(itemID, itemName, templateID, new ID(Guid.Empty)), new VersionUri(Language.Invariant, null), context); return(true); }
/// <summary> /// Get the ID of the parent of an item. /// </summary> /// <param name="itemDefinition">Used to identify the particular item</param> /// <param name="context"></param> /// <returns></returns> public override ID GetParentID(ItemDefinition itemDefinition, CallContext context) { if (ItemsById.ContainsKey(itemDefinition.ID) && ItemsById[itemDefinition.ID].Versions != null) { return(string.IsNullOrWhiteSpace(ItemsById[itemDefinition.ID].ParentID) ? null : ParseId(ItemsById[itemDefinition.ID].ParentID)); } return(null); }
/// <summary> /// Adds an item to the in-memory set of items. /// </summary> /// <param name="syncItem"></param> protected void AddItem(SyncItem syncItem) { ID itemId = ParseId(syncItem.ID); if (!ItemsById.ContainsKey(itemId)) { ItemsById.Add(itemId, syncItem); ItemsByParentId.Add(new KeyValuePair <ID, SyncItem>(ParseId(syncItem.ParentID), syncItem)); } }
/// <summary> /// Creates a new version for a content item in a particular language. /// </summary> /// <param name="itemDefinition">Used to identify the particular item</param> /// <param name="baseVersion">The version to copy off of</param> /// <param name="context"></param> /// <returns></returns> public override int AddVersion(ItemDefinition itemDefinition, VersionUri baseVersion, CallContext context) { if (!ItemsById.ContainsKey(itemDefinition.ID)) { return(-1); } SyncItem current = ItemsById[itemDefinition.ID]; int num = -1; Language baseVersionLanguage = baseVersion.Language; if (Language.Invariant.Equals(baseVersionLanguage)) { baseVersionLanguage = LanguageManager.DefaultLanguage; } if (baseVersion.Version != null && baseVersion.Version.Number > 0) { // copy version SyncVersion matchingVersion = (current.Versions ?? new List <SyncVersion>()) .OrderByDescending(vr => int.Parse(vr.Version)) .FirstOrDefault(vr => vr.Language.Equals(baseVersionLanguage.Name)); int?maxVersionNumber = matchingVersion != null?int.Parse(matchingVersion.Version) : null as int?; num = maxVersionNumber.HasValue && maxVersionNumber > 0 ? maxVersionNumber.Value + 1 : -1; if (num > 0) { SyncVersion newVersion = current.AddVersion(matchingVersion.Language, num.ToString(), matchingVersion.Revision); IList <SyncField> currentFieldValues = matchingVersion.Fields; foreach (SyncField fieldValue in currentFieldValues) { newVersion.AddField(fieldValue.FieldID, fieldValue.FieldName, fieldValue.FieldKey, fieldValue.FieldValue, true); } } } if (num == -1) { num = 1; // add blank version current.AddVersion(baseVersionLanguage.Name, num.ToString(), Guid.NewGuid().ToString()); } return(num); }
/// <summary> /// Changes the in-memory data when an item is moved to a different position in the tree. /// </summary> /// <param name="itemDefinition"></param> /// <param name="destination"></param> /// <param name="context"></param> /// <returns></returns> public override bool MoveItem(ItemDefinition itemDefinition, ItemDefinition destination, CallContext context) { if (!ItemsById.ContainsKey(itemDefinition.ID) || !ItemsById.ContainsKey(destination.ID)) { return(false); } SyncItem item = ItemsById[itemDefinition.ID]; ID parentId = ParseId(item.ParentID); SyncItem destinationIt = ItemsById[destination.ID]; ItemsByParentId.RemoveAll(pair => pair.Key == parentId && pair.Value == item); ItemsByParentId.Add(new KeyValuePair <ID, SyncItem>(destination.ID, item)); item.ItemPath = ItemsById.ContainsKey(parentId) ? string.Format("{0}/{1}", GetItemPath(parentId), item.Name) : string.Format("/{0}", item.Name); return(true); }
/// <summary> /// Returns a definition containing the id, name, template id, branch id and parent id of the Item that corresponds /// with the itemId parameter. /// </summary> /// <param name="itemId">The item id to search for</param> /// <param name="context"></param> /// <returns></returns> public override ItemDefinition GetItemDefinition(ID itemId, CallContext context) { if (ItemsById.ContainsKey(itemId)) { var data = new PrefetchData( new ItemDefinition( itemId, ItemsById[itemId].Name, ParseId(ItemsById[itemId].TemplateID) ?? ID.Null, ParseId(ItemsById[itemId].BranchId) ?? ID.Null), ParseId(ItemsById[itemId].ParentID) ?? ID.Null); if (data != null) { return(data.ItemDefinition); } } return(null); }
/// <summary> /// Get a list of all the item's fields and their values. /// </summary> /// <param name="itemDefinition">Used to identify the particular item</param> /// <param name="versionUri">The language and version of the item to get field values for</param> /// <param name="context"></param> /// <returns></returns> public override FieldList GetItemFields(ItemDefinition itemDefinition, VersionUri versionUri, CallContext context) { Language language = versionUri.Language; if (Language.Invariant.Equals(language)) { language = LanguageManager.DefaultLanguage; } if (ItemsById.ContainsKey(itemDefinition.ID)) { var fields = new FieldList(); foreach (SyncField sharedField in ItemsById[itemDefinition.ID].SharedFields) { fields.Add(ParseId(sharedField.FieldID), sharedField.FieldValue); } if (ItemsById[itemDefinition.ID].Versions != null) { foreach (SyncVersion version in ItemsById[itemDefinition.ID].Versions) { if (language.Name.Equals(version.Language) && versionUri.Version.Number.ToString().Equals(version.Version)) { foreach (SyncField fieldValue in version.Fields) { fields.Add(ParseId(fieldValue.FieldID), fieldValue.FieldValue); } break; } } } return(fields); } return(null); }
/// <summary> /// Get a list of all available versions in different languages. /// </summary> /// <param name="itemDefinition">Used to identify the particular item</param> /// <param name="context"></param> /// <returns></returns> public override VersionUriList GetItemVersions(ItemDefinition itemDefinition, CallContext context) { if (ItemsById.ContainsKey(itemDefinition.ID) && ItemsById[itemDefinition.ID].Versions != null) { var versionsList = new List <VersionUri>(); foreach (SyncVersion version in ItemsById[itemDefinition.ID].Versions) { var newVersionUri = new VersionUri( LanguageManager.GetLanguage(version.Language), new Version(version.Version)); versionsList.Add(newVersionUri); } var versions = new VersionUriList(); foreach (VersionUri version in versionsList) { versions.Add(version); } return(versions); } return(null); }
private string GetItemPath(ID id) { return(ItemsById.ContainsKey(id) ? string.Format("{0}/{1}", GetItemPath(ParseId(ItemsById[id].ParentID)), ItemsById[id].Name) : string.Empty); }
/// <summary> /// Save changes that were made to an item to the database. /// </summary> /// <param name="itemDefinition">Used to identify the particular item</param> /// <param name="changes">A holder object that keeps track of the changes</param> /// <param name="context"></param> /// <returns></returns> public override bool SaveItem(ItemDefinition itemDefinition, ItemChanges changes, CallContext context) { if (!ItemsById.ContainsKey(itemDefinition.ID)) { return(false); } SyncItem current = ItemsById[itemDefinition.ID]; if (changes.HasPropertiesChanged) { current.Name = StringUtil.GetString(changes.GetPropertyValue("name"), itemDefinition.Name); var templateId = MainUtil.GetObject(changes.GetPropertyValue("templateid"), itemDefinition.TemplateID) as ID; current.TemplateID = templateId != ID.Null ? GetIdAsString(templateId) : null; var branchId = MainUtil.GetObject(changes.GetPropertyValue("branchid"), itemDefinition.BranchId) as ID; current.BranchId = branchId != ID.Null ? GetIdAsString(branchId) : null; } if (changes.HasFieldsChanged) { foreach (FieldChange change in changes.FieldChanges) { string changeFieldId = GetIdAsString(change.FieldID); IEnumerable <SyncField> matchingSharedFields = current.SharedFields.Where(fv => changeFieldId.Equals(fv.FieldID)); IEnumerable <SyncVersion> matchingVersions = current.Versions .Where( vr => vr.Version.Equals(change.Version.Number.ToString()) && vr.Language.Equals(change.Language.Name)); var matchingNonSharedFields = matchingVersions .SelectMany(vr => vr.Fields.Select(fl => new { Ver = vr, Field = fl })) .Where(fv => changeFieldId.Equals(fv.Field.FieldID)); if (change.RemoveField) { if (matchingSharedFields.Any()) { current.SharedFields.Remove(matchingSharedFields.First()); } if (matchingNonSharedFields.Any()) { matchingNonSharedFields.First() .Ver.RemoveField(matchingNonSharedFields.First().Field.FieldName); } } else { bool changeMade = false; if (matchingSharedFields.Any()) { matchingSharedFields.First().FieldValue = change.Value; changeMade = true; } if (matchingNonSharedFields.Any()) { matchingNonSharedFields.First().Field.FieldValue = change.Value; changeMade = true; } if (!changeMade && change.Definition != null) { if (change.Definition.IsShared || change.Definition.IsUnversioned) { current.AddSharedField(changeFieldId, change.Definition.Name, change.Definition.Key, change.Value, true); } else if (matchingVersions.Any()) { matchingVersions.First() .AddField(changeFieldId, change.Definition.Name, change.Definition.Key, change.Value, true); } } } } } return(true); }