/// <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> /// <param name="eventLog"></param> /// <returns></returns> public bool InsertItems(string jsonRootFolder, List <JsonItemFile> itemsToAdd, Type itemType, IEventLog eventLog) { 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, itemType.Name, eventLog); foreach (var item in itemsToAdd) { if (items.FirstOrDefault(x => x.JsonFileName == item.JsonFileName) == null) { items.Add(item); } } bool returnValue = jsonFileSystemManager.SaveAllJsonFileItems(jsonRootFolder, itemType.Name, items, eventLog); EnableReadCache = priorEnableReadCache; return(returnValue); } catch (Exception exception) { if (eventLog != null) { eventLog.LogEvent(0, "ERROR Inserting " + itemType.Name + "s : " + exception); } return(false); } }
/// <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> /// <param name="eventLog"></param> /// <returns></returns> public bool InsertItem(string jsonRootFolder, object item, Type itemType, string itemId, string itemName, IEventLog eventLog) { 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, itemType.Name, eventLog); var jsonFileItem = GetJsonItemFileForItem(item, itemId, itemType, eventLog); items.Add(jsonFileItem); bool returnValue = jsonFileSystemManager.SaveAllJsonFileItems(jsonRootFolder, itemType.Name, items, eventLog); EnableReadCache = priorEnableReadCache; return(returnValue); } catch (Exception exception) { if (eventLog != null) { eventLog.LogEvent(0, "ERROR Inserting " + itemType.Name + ": (" + itemId + "," + itemName + ") - " + exception); } return(false); } }
/// <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> /// <param name="eventLog"></param> /// <returns></returns> public bool InsertItems(string jsonRootFolder, List <JsonItemFile> itemsToAdd, Type itemType, bool overwrite, IEventLog eventLog) { // if we are not overwriting items if (overwrite == false) { return(InsertItems(jsonRootFolder, itemsToAdd, itemType, eventLog)); } // if we are overwriting items try { bool priorEnableReadCache = EnableReadCache; EnableReadCache = false; // Load the assets var jsonFileSystemManager = new JsonFileSystemManager(); List <JsonItemFile> items = jsonFileSystemManager.LoadAllJsonFileItems(jsonRootFolder, itemType.Name, eventLog); 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, itemType.Name, itemsToSave, eventLog); EnableReadCache = priorEnableReadCache; return(returnValue); } catch (Exception exception) { if (eventLog != null) { eventLog.LogEvent(0, "ERROR Inserting " + itemType.Name + "s : " + exception); } return(false); } }