/// <summary> /// Inserts an item to the Json File System /// </summary> /// <param name="jsonRootFolder">Full path to the folder containing all Json Item Files</param> /// <param name="item">Item to write to file system</param> /// <param name="itemType">Type of item to write. Pass using typeof()</param> /// <param name="itemId">Unique id for item</param> /// <param name="itemName">Name of item</param> /// <returns></returns> public bool InsertItem(string jsonRootFolder, object item, string itemTypeFolderName, Type itemType, string itemId, string itemName) { if (string.IsNullOrEmpty(jsonRootFolder)) { throw new ArgumentNullException("ERROR: jsonRootFolder is required"); } if (item == null) { throw new ArgumentNullException("ERROR: item is required"); } if (string.IsNullOrEmpty(itemTypeFolderName)) { throw new ArgumentNullException("ERROR: itemTypeFolderName is required"); } if (itemType == null) { throw new ArgumentNullException("ERROR: itemType is required"); } if (string.IsNullOrEmpty(itemId)) { throw new ArgumentNullException("ERROR: itemId is required"); } if (string.IsNullOrEmpty(itemName)) { throw new ArgumentNullException("ERROR: itemName is required"); } try { bool priorEnableReadCache = EnableReadCache; EnableReadCache = false; // Load the assets and then add the asset and then save it. var jsonFileSystemManager = new JsonFileSystemManager(); var items = jsonFileSystemManager.LoadAllJsonFileItems(jsonRootFolder, itemTypeFolderName); // The way this works is if you add the item twice on save it would end up saving the one latest in the list var jsonFileItem = GetJsonItemFileForItem(item, itemId, itemType); items.Add(jsonFileItem); bool returnValue = jsonFileSystemManager.SaveAllJsonFileItems(jsonRootFolder, itemTypeFolderName, itemType.Name, items); EnableReadCache = priorEnableReadCache; return(returnValue); } catch (Exception exception) { throw new Exception("ERROR Inserting " + itemType.Name + ": (" + itemId + "," + itemName + ")", exception); } }
/// <summary> /// Writes some items to the file system. Skipping existing items /// </summary> /// <param name="jsonRootFolder"></param> /// <param name="itemsToAdd"></param> /// <param name="itemType"></param> /// <param name="itemId"></param> /// <param name="itemName"></param> /// <returns></returns> public bool InsertItems(string jsonRootFolder, List <JsonItemFile> itemsToAdd, string itemTypeFolderName, Type itemType) { if (string.IsNullOrEmpty(jsonRootFolder)) { throw new ArgumentNullException("ERROR: jsonRootFolder is required"); } if (itemsToAdd == null) { throw new ArgumentNullException("ERROR: itemToAdd is required"); } if (string.IsNullOrEmpty(itemTypeFolderName)) { throw new ArgumentNullException("ERROR: itemTypeFolderName is required"); } if (itemType == null) { throw new ArgumentNullException("ERROR: itemType is required"); } try { bool priorEnableReadCache = EnableReadCache; EnableReadCache = false; // Load the assets and then add the asset and then save it. var jsonFileSystemManager = new JsonFileSystemManager(); List <JsonItemFile> items = jsonFileSystemManager.LoadAllJsonFileItems(jsonRootFolder, itemTypeFolderName); foreach (var item in itemsToAdd) { if (items.FirstOrDefault(x => x.JsonFileName == item.JsonFileName) == null) { items.Add(item); } } bool returnValue = jsonFileSystemManager.SaveAllJsonFileItems(jsonRootFolder, itemTypeFolderName, itemType.Name, items); EnableReadCache = priorEnableReadCache; return(returnValue); } catch (Exception exception) { throw new Exception("ERROR Inserting " + itemType.Name + "s", exception); } }
/// <summary> /// Writes some items to the file system. Skipping existing items /// </summary> /// <param name="jsonRootFolder"></param> /// <param name="itemsToAdd"></param> /// <param name="itemType"></param> /// <param name="itemId"></param> /// <param name="itemName"></param> /// <returns></returns> public bool InsertItems(string jsonRootFolder, List <JsonItemFile> itemsToAdd, string itemTypeFolderName, Type itemType, bool overwrite) { if (string.IsNullOrEmpty(jsonRootFolder)) { throw new ArgumentNullException("ERROR: jsonRootFolder is required"); } if (itemsToAdd == null) { throw new ArgumentNullException("ERROR: itemToAdds is required"); } if (string.IsNullOrEmpty(itemTypeFolderName)) { throw new ArgumentNullException("ERROR: itemTypeFolderName is required"); } if (itemType == null) { throw new ArgumentNullException("ERROR: itemType is required"); } // if we are not overwriting items if (overwrite == false) { return(InsertItems(jsonRootFolder, itemsToAdd, itemTypeFolderName, itemType)); } // if we are overwriting items try { bool priorEnableReadCache = EnableReadCache; EnableReadCache = false; // Load the assets var jsonFileSystemManager = new JsonFileSystemManager(); List <JsonItemFile> items = jsonFileSystemManager.LoadAllJsonFileItems(jsonRootFolder, itemTypeFolderName); List <JsonItemFile> itemsToSave = new List <JsonItemFile>(); // Ensure all the ones we are adding are in the save list. // Only include existing items that are not replaced by our inserts foreach (var item in itemsToAdd) { // Ensure item filename ends in .json if (item.JsonFileName.EndsWith(".json") == false) { item.JsonFileName = item.JsonFileName + ".json"; } itemsToSave.Add(item); } // Only include existing items that are not replaced by our inserts foreach (var item in items) { // Ensure item filename ends in .json if (item.JsonFileName.EndsWith(".json") == false) { item.JsonFileName = item.JsonFileName + ".json"; } if (itemsToSave.FirstOrDefault(x => x.JsonFileName == item.JsonFileName) == null) { itemsToSave.Add(item); } } // save everything back to file system. bool returnValue = jsonFileSystemManager.SaveAllJsonFileItems(jsonRootFolder, itemTypeFolderName, itemType.Name, itemsToSave); EnableReadCache = priorEnableReadCache; return(returnValue); } catch (Exception exception) { throw new Exception("ERROR Inserting " + itemType.Name + "s", exception); } }