Exemplo n.º 1
0
 // get or create a List by value in given folder for given user
 public Item GetOrCreateListByValue(User user, Folder folder, string value, string name, Guid? itemTypeID = null)
 {
     if (itemTypeID == null) { itemTypeID = SystemItemTypes.NameValue; }
     try
     {   // get the list with given value in given folder
         if (Items.Any(i => i.UserID == user.ID && i.FolderID == folder.ID &&
             i.FieldValues.Any(fv => fv.FieldName == FieldNames.Value && fv.Value == value)))
         {
             return Items.Single(i => i.UserID == user.ID && i.FolderID == folder.ID &&
                 i.FieldValues.Any(fv => fv.FieldName == FieldNames.Value && fv.Value == value));
         }
         else
         {   // create new list with given value and name in given folder
             DateTime now = DateTime.UtcNow;
             var list = new Item()
             {
                 ID = Guid.NewGuid(),
                 Name = name,
                 FolderID = folder.ID,
                 UserID = user.ID,
                 IsList = true,
                 ItemTypeID = itemTypeID.Value,
                 ParentID = null,
                 Created = now,
                 LastModified = now,
                 FieldValues = new List<FieldValue>()
             };
             list.GetFieldValue(FieldNames.Value, true).Value = value;
             Items.Add(list);
             SaveChanges();
             TraceLog.TraceInfo(string.Format("Created list by value '{0}' in folder '{1}' for user '{2}'", value, folder.Name, user.Name));
             return list;
         }
     }
     catch (Exception ex)
     {
         TraceLog.TraceException(string.Format("Could not find or create list by value '{0}' in folder '{1}' for user '{2}'", value, folder.Name, user.Name), ex);
         return null;
     }
 }
Exemplo n.º 2
0
        static void ImportItems(UserStorageContext context, Folder folder, Folder jsonFolder, List<Item> jsonItems, Guid? parentID, int level)
        {
            // process each of the items passed in
            foreach (var jsonItem in jsonItems)
            {
                // reset some of the fields in the serialized structure to the database values
                jsonItem.UserID = folder.UserID;
                jsonItem.FolderID = folder.ID;

                // get the children of the current item from the serialized folder
                var jsonChildren = jsonFolder.Items.Where(i => i.ParentID == jsonItem.ID).ToList();
                if (jsonChildren.Count > 0 || jsonItem.IsList)
                {
                    // process the item as a list - find the database list by name
                    var list = folder.Items.FirstOrDefault(i => i.Name == jsonItem.Name && i.ParentID == parentID);
                    if (list != null)
                    {
                        // list found in database - change the ID of the serialized list to match the database
                        for (int i = 0; i < level; i++)
                            Console.Write("    ");
                        Console.Write(jsonItem.IsList ? "Found list: " : "Found item: ");
                        Console.WriteLine(jsonItem.Name);
                        jsonItem.ID = list.ID;
                    }
                    else
                    {
                        // list not found in database - add the list to the DB using a new ID (in case the old one still exists in the DB)
                        var id = Guid.NewGuid();
                        NewID[jsonItem.ID] = id;
                        jsonItem.ID = id;
                        list = context.Items.Add(jsonItem);
                        context.SaveChanges();

                        for (int i = 0; i < level; i++)
                            Console.Write("    ");
                        Console.Write(jsonItem.IsList ? "Added list: " : "Added item: ");
                        Console.WriteLine(jsonItem.Name);
                    }

                    // fix the parent ID's for all the children
                    foreach (var i in jsonChildren)
                        i.ParentID = jsonItem.ID;

                    // recursively import this list's children
                    ImportItems(context, folder, jsonFolder, jsonChildren, jsonItem.ID, level + 1);

                    // fix any fieldvalues that are pointing to old guids
                    foreach (var fv in jsonItem.FieldValues)
                    {
                        Guid guid;
                        if (Guid.TryParse(fv.Value, out guid))
                        {
                            Guid newID;
                            if (NewID.TryGetValue(new Guid(fv.Value), out newID))
                                fv.Value = newID.ToString();
                        }
                    }
                    context.SaveChanges();
                }
                else
                {
                    // this is a singleton (not a list) - add it to the DB using a new ID (in case the old one still exists in the DB)
                    var id = Guid.NewGuid();
                    NewID[jsonItem.ID] = id;

                    // if this is a reference, fix the ref ID
                    if (jsonItem.ItemTypeID == SystemItemTypes.Reference)
                    {
                        var refID = jsonItem.GetFieldValue(FieldNames.EntityRef);
                        if (refID != null && !String.IsNullOrEmpty(refID.Value))
                        {
                            Guid newID;
                            if (NewID.TryGetValue(new Guid(refID.Value), out newID))
                                refID.Value = newID.ToString();
                        }
                    }

                    jsonItem.ID = id;
                    context.Items.Add(jsonItem);
                    context.SaveChanges();
                    for (int i = 0; i < level; i++)
                        Console.Write("    ");
                    Console.WriteLine("Added item " + jsonItem.Name);
                }
            }
        }
Exemplo n.º 3
0
 // get or create a List by name in given folder for given user
 public Item GetOrCreateList(User user, Folder folder, string name, Guid? itemTypeID = null)
 {
     return GetOrCreateItem(user, folder, name, itemTypeID, true);
 }
Exemplo n.º 4
0
 // get or create an Item by name in given folder for given user (include FieldValues if NOT a List)
 public Item GetOrCreateItem(User user, Folder folder, string name, Guid? itemTypeID = null, bool isList = false)
 {
     if (itemTypeID == null) { itemTypeID = SystemItemTypes.NameValue; }
     try
     {   // get the list with given name in given folder
         if (Items.Any(i => i.UserID == user.ID && i.FolderID == folder.ID && i.Name == name))
         {
             if (isList)
                 return Items.Single(i => i.UserID == user.ID && i.FolderID == folder.ID && i.Name == name);
             else
                 return Items.Include("FieldValues").Single(i => i.UserID == user.ID && i.FolderID == folder.ID && i.Name == name);
         }
         else
         {   // create new item with given name in given folder
             DateTime now = DateTime.UtcNow;
             var item = new Item()
             {
                 ID = Guid.NewGuid(),
                 Name = name,
                 FolderID = folder.ID,
                 UserID = user.ID,
                 IsList = isList,
                 ItemTypeID = itemTypeID.Value,
                 ParentID = null,
                 Created = now,
                 LastModified = now
             };
             Items.Add(item);
             SaveChanges();
             TraceLog.TraceInfo(string.Format("Created item named '{0}' in folder '{1}' for user '{2}'", name, folder.Name, user.Name));
             return item;
         }
     }
     catch (Exception ex)
     {
         TraceLog.TraceException(string.Format("Could not find or create item named '{0}' in folder '{1}' for user '{2}'", name, folder.Name, user.Name), ex);
         return null;
     }
 }
Exemplo n.º 5
0
 // get or create a Folder by name for given user
 public Folder GetOrCreateFolder(User user, string name, Guid itemTypeID)
 {
     try
     {   // get the folder by name for user
         if (Folders.Any(f => f.UserID == user.ID && f.Name == name))
         {
             return Folders.Single(f => f.UserID == user.ID && f.Name == name);
         }
         else
         {   // create the folder with given name and itemTypeID for user
             var folderUser = new FolderUser() { ID = Guid.NewGuid(), FolderID = Guid.NewGuid(), UserID = user.ID, PermissionID = BuiltSteady.Zaplify.Shared.Entities.Permissions.Full };
             var folder = new Folder()
             {
                 ID = folderUser.FolderID,
                 SortOrder = 0,
                 Name = name,
                 UserID = user.ID,
                 ItemTypeID = itemTypeID,
                 Items = new List<Item>(),
                 FolderUsers = new List<FolderUser>() { folderUser }
             };
             Folders.Add(folder);
             SaveChanges();
             TraceLog.TraceInfo(string.Format("Created folder named '{0}' for user '{1}'", name, user.Name));
             return folder;
         }
     }
     catch (Exception ex)
     {
         TraceLog.TraceException(string.Format("Could not find or create folder named '{0}' for user '{1}'", name, user.Name), ex);
         return null;
     }
 }
Exemplo n.º 6
0
        // Update the requested folder with values from the new folder
        // Currently, the algorithm updates only if the server's current value is equal
        // to the original value passed in.
        // NOTE: the server value for folders currently does not include the Item collection
        // because we did not .Include() it in the EF query.  This works well so that the update
        // loop bypasses the Items collection - we are only updating scalar values.
        private bool Update(Folder requestedFolder, Folder originalFolder, Folder newFolder)
        {
            bool updated = false;
            // TODO: timestamps!
            Type t = requestedFolder.GetType();
            foreach (PropertyInfo pi in t.GetProperties())
            {
                object serverValue = pi.GetValue(requestedFolder, null);
                object origValue = pi.GetValue(originalFolder, null);
                object newValue = pi.GetValue(newFolder, null);

                // if the value has changed, process further
                if (!Object.Equals(origValue, newValue))
                {
                    // if the server has the original value, make the update
                    if (Object.Equals(serverValue, origValue))
                    {
                        pi.SetValue(requestedFolder, newValue, null);
                        updated = true;
                    }
                }
            }

            return updated;
        }
Exemplo n.º 7
0
 /// <summary>
 /// Delete all items in a folder.  While there are any items, we use the ItemResource's recursive
 /// delete static methods to delete each item and any potential children.  This way we ensure that 
 /// the deletes are done in the proper order.
 /// </summary>
 /// <param name="folder"></param>
 /// <returns></returns>
 private bool DeleteItems(Folder folder)
 {
     bool multipleItemsDeleted = false;
     while (StorageContext.Items.Any(i => i.FolderID == folder.ID))
     {
         var item = StorageContext.Items.First(i => i.FolderID == folder.ID);
         // delete all the items with ParentID of this item.ID (recursively, from the bottom up)
         multipleItemsDeleted = ItemResource.DeleteItemChildrenRecursively(StorageContext, item);
         // delete all ItemRef FieldValues with Value of this item.ID
         multipleItemsDeleted |= ItemResource.DeleteItemReferences(CurrentUser, StorageContext, item);
         StorageContext.Items.Remove(item);
         StorageContext.SaveChanges();
     }
     return multipleItemsDeleted;
 }