private static void AddCustomFieldToItems(string customType, string schemaKey, string newFieldName, bool isList)
        {
            List <string> itemKeys = GetItemsOfSchemaType(schemaKey);
            Dictionary <string, object> itemData;

            foreach (string itemKey in itemKeys)
            {
                if (AllItems.TryGetValue(itemKey, out itemData))
                {
                    if (isList)
                    {
                        itemData.Add(newFieldName, new List <object>());
                        itemData.Add(string.Format(GDEConstants.MetaDataFormat, GDEConstants.TypePrefix, newFieldName), customType);
                        itemData.Add(string.Format(GDEConstants.MetaDataFormat, GDEConstants.IsListPrefix, newFieldName), true);
                    }
                    else
                    {
                        itemData.Add(newFieldName, "null");
                        itemData.Add(string.Format(GDEConstants.MetaDataFormat, GDEConstants.TypePrefix, newFieldName), customType);
                    }

                    ItemsNeedSave = true;
                }
            }
        }
        public static bool RenameSchemaField(string oldFieldKey, string newFieldKey, string schemaKey, Dictionary <string, object> schemaData, out string error)
        {
            bool result = true;

            if (!IsFieldNameValid(schemaData, newFieldKey, out error))
            {
                result = false;
            }
            else if (schemaData.ContainsKey(newFieldKey))
            {
                result = false;
                error  = "Field name already exists.";
            }
            else
            {
                // Do rename
                RenameField(oldFieldKey, newFieldKey, schemaData);

                // Remove the schema key from the listbyfieldname List
                List <string> schemaKeyList;
                if (ListByFieldName.TryGetValue(oldFieldKey, out schemaKeyList))
                {
                    schemaKeyList.Remove(schemaKey);
                    if (schemaKeyList.Count == 0)
                    {
                        ListByFieldName.Remove(oldFieldKey);
                    }
                }

                // Add the schema key to the listbyfieldname List under the new field name
                if (ListByFieldName.TryGetValue(newFieldKey, out schemaKeyList))
                {
                    schemaKeyList.Add(schemaKey);
                }
                else
                {
                    List <string> newListByFieldName = new List <string>()
                    {
                        schemaKey
                    };
                    ListByFieldName.Add(newFieldKey, newListByFieldName);
                }

                // Rename the fields in any existing items with this schema
                List <string> itemKeys = GetItemsOfSchemaType(schemaKey);
                foreach (string itemKey in itemKeys)
                {
                    Dictionary <string, object> itemData;
                    if (AllItems.TryGetValue(itemKey, out itemData))
                    {
                        RenameField(oldFieldKey, newFieldKey, itemData);
                    }
                }
            }

            ItemsNeedSave   |= result;
            SchemasNeedSave |= result;

            return(result);
        }
        public static string GetSchemaForItem(string itemKey)
        {
            string schema = "";
            Dictionary <string, object> itemData;

            if (AllItems.TryGetValue(itemKey, out itemData))
            {
                object temp;
                if (itemData.TryGetValue(GDEConstants.SchemaKey, out temp))
                {
                    schema = temp as string;
                }
            }

            return(schema);
        }
        private static void RemoveFieldFromItems(string schemaKey, string deleteFieldName)
        {
            List <string> itemKeys = GetItemsOfSchemaType(schemaKey);
            Dictionary <string, object> itemData;

            foreach (string itemKey in itemKeys)
            {
                if (AllItems.TryGetValue(itemKey, out itemData))
                {
                    itemData.Remove(deleteFieldName);
                    itemData.Remove(string.Format(GDEConstants.MetaDataFormat, GDEConstants.TypePrefix, deleteFieldName));
                    itemData.Remove(string.Format(GDEConstants.MetaDataFormat, GDEConstants.IsListPrefix, deleteFieldName));

                    ItemsNeedSave = true;
                }
            }
        }
        // Returns a list of item keys that are of the List<> type
        public static List <string> ItemListFieldKeys(string itemKey)
        {
            List <string> fieldKeys = new List <string>();
            Dictionary <string, object> data;

            if (AllItems.TryGetValue(itemKey, out data))
            {
                foreach (KeyValuePair <string, object> field in data)
                {
                    if (field.Key.StartsWith(GDEConstants.IsListPrefix))
                    {
                        fieldKeys.Add(field.Key.Replace(GDEConstants.IsListPrefix, ""));
                    }
                }
            }

            return(fieldKeys);
        }
Exemple #6
0
 /// <summary>
 /// Try to get a markdownitem with the given TypeWrapper lookup key
 /// </summary>
 /// <param name="wrapper">The key to look up the markdownitem</param>
 /// <param name="value">If found the IMarkdownItem</param>
 /// <returns>True if found or false if not found</returns>
 public bool TryGetValue(TypeWrapper wrapper, out IMarkdownItem value)
 {
     return(AllItems.TryGetValue(wrapper.GetId(), out value));
 }
        public static bool RenameItem(string oldItemKey, string newItemKey, Dictionary <string, object> data, out string error)
        {
            bool result = true;

            if (IsItemNameValid(newItemKey, out error))
            {
                Dictionary <string, object> itemData;
                if (AllItems.TryGetValue(oldItemKey, out itemData))
                {
                    Dictionary <string, object> itemDataCopy = itemData.DeepCopy();

                    // First remove the item from the dictionary
                    RemoveItem(oldItemKey);

                    // Then add the item data under the new item key
                    if (!AddItem(newItemKey, itemDataCopy, out error))
                    {
                        // Add the item back under the old key if this step failed
                        AddItem(oldItemKey, itemDataCopy, out error);
                        result = false;
                    }

                    // Update any items that have a reference to this item
                    string itemSchemaType;
                    itemDataCopy.TryGetString(GDEConstants.SchemaKey, out itemSchemaType);
                    foreach (string curItemKey in AllItems.Keys)
                    {
                        Dictionary <string, object> curItemData;
                        AllItems.TryGetValue(curItemKey, out curItemData);

                        if (curItemData == null)
                        {
                            continue;
                        }

                        // Update any single field references: ex. custom_type myField = "oldKey"
                        List <string> fieldsOfSchemaType = ItemFieldKeysOfType(curItemKey, itemSchemaType);
                        foreach (string itemFieldKey in fieldsOfSchemaType)
                        {
                            string curItemFieldValue;
                            curItemData.TryGetString(itemFieldKey, out curItemFieldValue);

                            if (!string.IsNullOrEmpty(curItemFieldValue) && curItemFieldValue.Equals(oldItemKey))
                            {
                                curItemData.TryAddOrUpdateValue(itemFieldKey, newItemKey);
                            }
                        }

                        // Update any references that are part of a list
                        fieldsOfSchemaType = ItemListFieldKeysOfType(curItemKey, itemSchemaType);
                        foreach (string itemFieldKey in fieldsOfSchemaType)
                        {
                            object        temp;
                            List <object> valueList = null;
                            curItemData.TryGetValue(itemFieldKey, out temp);
                            valueList = temp as List <object>;

                            if (valueList != null)
                            {
                                List <int> indexes = valueList.AllIndexesOf(oldItemKey);
                                foreach (int index in indexes)
                                {
                                    valueList[index] = newItemKey;
                                }
                            }
                        }
                    }
                }
                else
                {
                    error  = "Failted to read item data.";
                    result = false;
                }
            }
            else
            {
                result = false;
            }

            ItemsNeedSave |= result;

            return(result);
        }
        public static bool RenameSchema(string oldSchemaKey, string newSchemaKey, out string error)
        {
            bool result = true;

            if (IsSchemaNameValid(newSchemaKey, out error))
            {
                Dictionary <string, object> schemaData;
                if (AllSchemas.TryGetValue(oldSchemaKey, out schemaData))
                {
                    List <string> itemsWithSchema = GetItemsOfSchemaType(oldSchemaKey);
                    Dictionary <string, object> schemaDataCopy = schemaData.DeepCopy();

                    // First remove the schema from the dictionary
                    RemoveSchema(oldSchemaKey, false);

                    // Then add the schema data under the new schema key
                    if (AddSchema(newSchemaKey, schemaDataCopy, out error))
                    {
                        List <string> itemBySchemaList;
                        ItemListBySchema.TryGetValue(newSchemaKey, out itemBySchemaList);

                        // Update the schema key on any existing items
                        foreach (string itemKey in itemsWithSchema)
                        {
                            Dictionary <string, object> itemData;
                            if (AllItems.TryGetValue(itemKey, out itemData))
                            {
                                itemData.TryAddOrUpdateValue(GDEConstants.SchemaKey, newSchemaKey);
                            }
                            itemBySchemaList.Add(itemKey);
                        }

                        // Update any custom fields in schemas that had the old schema name
                        foreach (string curSchemaKey in AllSchemas.Keys)
                        {
                            List <string> fieldsOfSchemaType = SchemaFieldKeysOfType(curSchemaKey, oldSchemaKey);
                            fieldsOfSchemaType.AddRange(SchemaListFieldKeysOfType(curSchemaKey, oldSchemaKey));

                            if (fieldsOfSchemaType.Count > 0)
                            {
                                Dictionary <string, object> curSchemaData;
                                AllSchemas.TryGetValue(curSchemaKey, out curSchemaData);

                                if (curSchemaData == null)
                                {
                                    continue;
                                }

                                foreach (string schemaFieldKey in fieldsOfSchemaType)
                                {
                                    curSchemaData.TryAddOrUpdateValue(string.Format(GDEConstants.MetaDataFormat, GDEConstants.TypePrefix, schemaFieldKey), newSchemaKey);
                                }
                            }
                        }

                        // Lastly, update any custom fields that had the old schema name
                        foreach (string curItemKey in AllItems.Keys)
                        {
                            List <string> fieldsOfSchemaType = ItemFieldKeysOfType(curItemKey, oldSchemaKey);
                            fieldsOfSchemaType.AddRange(ItemListFieldKeysOfType(curItemKey, oldSchemaKey));

                            if (fieldsOfSchemaType.Count > 0)
                            {
                                Dictionary <string, object> curItemData;
                                AllItems.TryGetValue(curItemKey, out curItemData);

                                if (curItemData == null)
                                {
                                    continue;
                                }

                                foreach (string itemFieldKey in fieldsOfSchemaType)
                                {
                                    curItemData.TryAddOrUpdateValue(string.Format(GDEConstants.MetaDataFormat, GDEConstants.TypePrefix, itemFieldKey), newSchemaKey);
                                }
                            }
                        }
                    }
                    else
                    {
                        // Add the schema back under the old key if this step failed
                        AddSchema(oldSchemaKey, schemaDataCopy, out error);
                        result = false;
                    }
                }
                else
                {
                    error  = "Failed to read schema data.";
                    result = false;
                }
            }
            else
            {
                result = false;
            }

            SchemasNeedSave |= result;
            ItemsNeedSave   |= result;

            return(result);
        }
Exemple #9
0
 public static FoodItem GetItemById(FoodItemType type, int ItemId)
 {
     FoodItem result = null; AllItems.TryGetValue(GetCacheID(type, ItemId), out result); return(result);
 }