Ejemplo n.º 1
0
        public HttpResponseMessageWrapper<Folder> InsertFolder(HttpRequestMessage req)
        {
            Operation operation = null;
            HttpStatusCode code = AuthenticateUser(req);
            if (code != HttpStatusCode.OK)
            {   // user not authenticated
                return ReturnResult<Folder>(req, operation, code);
            }

            Folder clientFolder = null;
            code = ProcessRequestBody<Folder>(req, out clientFolder, out operation);
            if (code != HttpStatusCode.OK)  // error encountered processing body
                return ReturnResult<Folder>(req, operation, code);

            // default ItemTypeID to Task if not set
            if (clientFolder.ItemTypeID == null || clientFolder.ItemTypeID == Guid.Empty)
                clientFolder.ItemTypeID = SystemItemTypes.Task;

            // this operation isn't meant to do more than just insert the new folder
            // therefore make sure items collection is empty
            if (clientFolder.Items != null)
                clientFolder.Items.Clear();

            // if the current user isn't in the FolderUsers collection, add it now (must have at least one FolderUser)
            bool addFolderUser = false;
            if (clientFolder.FolderUsers == null || clientFolder.FolderUsers.Count < 1)
                addFolderUser = true;
            else
            {
                var folderUsers = clientFolder.FolderUsers.Where<FolderUser>(gu => gu.UserID == clientFolder.UserID);
                if (folderUsers == null || folderUsers.Count() < 1)
                    addFolderUser = true;
            }
            if (addFolderUser)
            {
                FolderUser fu = new FolderUser()
                {
                    ID = Guid.NewGuid(),
                    UserID = clientFolder.UserID,
                    FolderID = clientFolder.ID,
                    PermissionID = 3 /* full */
                };
                if (clientFolder.FolderUsers == null)
                    clientFolder.FolderUsers = new List<FolderUser>();
                clientFolder.FolderUsers.Add(fu);
            }

            try
            {
                var folder = this.StorageContext.Folders.Add(clientFolder);
                if (folder == null || this.StorageContext.SaveChanges() < 1)
                {
                    TraceLog.TraceError("Internal Server Error (database operation did not succeed)");
                    return ReturnResult<Folder>(req, operation, HttpStatusCode.InternalServerError);
                }
                else
                {
                    TraceLog.TraceInfo("Created");
                    return ReturnResult<Folder>(req, operation, folder, HttpStatusCode.Created);
                }
            }
            catch (Exception ex)
            {
                // check for the condition where the folder is already in the database
                // in that case, return 202 Accepted; otherwise, return 409 Conflict
                try
                {
                    var dbFolder = this.StorageContext.Folders.Single(g => g.ID == clientFolder.ID);
                    if (dbFolder.Name == clientFolder.Name &&
                        dbFolder.UserID == clientFolder.UserID)
                    {
                        TraceLog.TraceInfo("Accepted, entity exists : Exception[" + ex.Message + "]");
                        return ReturnResult<Folder>(req, operation, dbFolder, HttpStatusCode.Accepted);
                    }
                    else
                    {
                        TraceLog.TraceException("Conflict, entity in database did not match", ex);
                        return ReturnResult<Folder>(req, operation, HttpStatusCode.Conflict);
                    }
                }
                catch (Exception e)
                {
                    // folder not inserted - return 409 Conflict
                    TraceLog.TraceException(String.Format("Conflict, entity was not in database : Exception[{0}]", ex.Message), e);
                    return ReturnResult<Folder>(req, operation, HttpStatusCode.Conflict);
                }
            }
        }
Ejemplo n.º 2
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;
     }
 }
Ejemplo n.º 3
0
        public static List<Folder> DefaultFolders(User currentUser)
        {
            List<Folder> folders = new List<Folder>();
            Dictionary<Guid, object> defaultLists = new Dictionary<Guid, object>();
            DateTime now = DateTime.Now;
            Item item;
            Folder folder;
            Guid folderID = Guid.NewGuid();

            #if !CLIENT
            FolderUser folderUser;
            folderUser = new FolderUser() { ID = Guid.NewGuid(), FolderID = folderID, UserID = currentUser.ID, PermissionID = Permissions.Full };
            #endif
            // create Activities folder
            folder = new Folder()
            {
                ID = folderID,
                SortOrder = 1000,
                Name = UserEntities.Activities,
                ItemTypeID = SystemItemTypes.Appointment,
            #if CLIENT
                Items = new ObservableCollection<Item>(),
            #else
                Items = new List<Item>(),
                UserID = currentUser.ID,
                FolderUsers = new List<FolderUser>() { folderUser }
            #endif
            };
            folders.Add(folder);
            // make this defaultList for Appointments
            defaultLists.Add(SystemItemTypes.Appointment, folder);

            // create Tasks list
            item = new Item()
            {
                ID = Guid.NewGuid(),
                SortOrder = 1000,
                Name = UserEntities.Tasks,
                FolderID = folder.ID,
                IsList = true,
                ItemTypeID = SystemItemTypes.Task,
                ParentID = null,
                Created = now,
                LastModified = now,
            #if !CLIENT
                UserID = currentUser.ID,
            #endif
            };
            folder.Items.Add(item);
            // make this defaultList for Tasks
            defaultLists.Add(SystemItemTypes.Task, item);

            // create Learn Zaplify task
            item = new Item()
            {
                ID = Guid.NewGuid(),
                SortOrder = 2000,
                Name = "Learn about Zaplify!",
                FolderID = folder.ID,
                IsList = false,
                ItemTypeID = SystemItemTypes.Task,
                ParentID = item.ID,
                Created = now,
                LastModified = now,
            #if CLIENT
                FieldValues = new ObservableCollection<FieldValue>(),
            #else
                FieldValues = new List<FieldValue>(),
                UserID = currentUser.ID,
            #endif
            };
            folder.Items.Add(item);

            #if CLIENT
            item.GetFieldValue(FieldNames.DueDate, true).Value = DateTime.Today.Date.ToString("MM/dd/yyyy");
            item.GetFieldValue(FieldNames.Description, true).Value = "Get connected with Zaplify.";
            #else
            item.FieldValues.Add(Item.CreateFieldValue(item.ID, FieldNames.DueDate, DateTime.Today.Date.ToString("MM/dd/yyyy")));
            item.FieldValues.Add(Item.CreateFieldValue(item.ID, FieldNames.Description, "Get connected with Zaplify."));
            #endif

            folderID = Guid.NewGuid();
            #if !CLIENT
            folderUser = new FolderUser() { ID = Guid.NewGuid(), FolderID = folderID, UserID = currentUser.ID, PermissionID = Permissions.Full };
            #endif
            // create Lists folder
            folder = new Folder()
            {
                ID = folderID,
                SortOrder = 2000,
                Name = UserEntities.Lists,
                ItemTypeID = SystemItemTypes.ListItem,
            #if CLIENT
                Items = new ObservableCollection<Item>(),
            #else
                Items = new List<Item>(),
                UserID = currentUser.ID,
                FolderUsers = new List<FolderUser>() { folderUser }
            #endif
            };
            folders.Add(folder);
            // make this defaultList for ListItems
            defaultLists.Add(SystemItemTypes.ListItem, folder);

            // create Groceries list
            item = new Item()
            {
                ID = Guid.NewGuid(),
                SortOrder = 3000,
                Name = UserEntities.Groceries,
                FolderID = folder.ID,
                IsList = true,
                ItemTypeID = SystemItemTypes.Grocery,
                ParentID = null,
                Created = now,
                LastModified = now,
            #if !CLIENT
                UserID = currentUser.ID,
            #endif
            };
            folder.Items.Add(item);
            // make this defaultList for GroceryItems
            defaultLists.Add(SystemItemTypes.Grocery, item);

            folderID = Guid.NewGuid();
            #if !CLIENT
            folderUser = new FolderUser() { ID = Guid.NewGuid(), FolderID = folderID, UserID = currentUser.ID, PermissionID = Permissions.Full };
            #endif
            // create People folder
            folder = new Folder()
            {
                ID = folderID,
                SortOrder = 3000,
                Name = UserEntities.People,
                ItemTypeID = SystemItemTypes.Contact,
            #if CLIENT
                Items = new ObservableCollection<Item>(),
            #else
                Items = new List<Item>(),
                UserID = currentUser.ID,
                FolderUsers = new List<FolderUser>() { folderUser }
            #endif
            };
            folders.Add(folder);
            // make this defaultList for Contacts
            defaultLists.Add(SystemItemTypes.Contact, folder);

            folderID = Guid.NewGuid();
            #if !CLIENT
            folderUser = new FolderUser() { ID = Guid.NewGuid(), FolderID = folderID, UserID = currentUser.ID, PermissionID = Permissions.Full };
            #endif
            // create Places folder
            folder = new Folder()
            {
                ID = folderID,
                SortOrder = 4000,
                Name = UserEntities.Places,
                ItemTypeID = SystemItemTypes.Location,
            #if CLIENT
                Items = new ObservableCollection<Item>(),
            #else
                Items = new List<Item>(),
                UserID = currentUser.ID,
                FolderUsers = new List<FolderUser>() { folderUser }
            #endif
            };
            folders.Add(folder);
            // make this defaultList for Locations
            defaultLists.Add(SystemItemTypes.Location, folder);

            #if !CLIENT
            folderID = Guid.NewGuid();
            folderUser = new FolderUser() { ID = Guid.NewGuid(), FolderID = folderID, UserID = currentUser.ID, PermissionID = Permissions.Full };
            // create $WebClient folder
            folder = new Folder()
            {
                ID = folderID,
                SortOrder = 0,
                Name = SystemEntities.WebClient,
                ItemTypeID = SystemItemTypes.NameValue,
                Items = new List<Item>(),
                UserID = currentUser.ID,
                FolderUsers = new List<FolderUser>() { folderUser }
            };
            folders.Add(folder);
            #endif

            folderID = Guid.NewGuid();
            #if !CLIENT
            folderUser = new FolderUser() { ID = Guid.NewGuid(), FolderID = folderID, UserID = currentUser.ID, PermissionID = Permissions.Full };
            #endif
            // create $PhoneClient folder
            folder = new Folder()
            {
                ID = folderID,
                SortOrder = 0,
                Name = SystemEntities.PhoneClient,
                ItemTypeID = SystemItemTypes.NameValue,
            #if CLIENT
                Items = new ObservableCollection<Item>(),
            #else
                Items = new List<Item>(),
                UserID = currentUser.ID,
                FolderUsers = new List<FolderUser>() { folderUser }
            #endif
            };
            folders.Add(folder);

            folderID = Guid.NewGuid();
            #if !CLIENT
            folderUser = new FolderUser() { ID = Guid.NewGuid(), FolderID = folderID, UserID = currentUser.ID, PermissionID = Permissions.Full };
            #endif
            // create $Client folder
            folder = new Folder()
            {
                ID = folderID,
                SortOrder = 0,
                Name = SystemEntities.Client,
                ItemTypeID = SystemItemTypes.NameValue,
            #if CLIENT
                Items = new ObservableCollection<Item>(),
            #else
                Items = new List<Item>(),
                UserID = currentUser.ID,
                FolderUsers = new List<FolderUser>() { folderUser }
            #endif
            };
            folders.Add(folder);

            // create DefaultLists list
            var defaultListItemID = Guid.NewGuid();
            item = new Item()
            {
                ID = defaultListItemID,
                SortOrder = 0,
                Name = SystemEntities.DefaultLists,
                FolderID = folder.ID,
                IsList = true,
                ItemTypeID = SystemItemTypes.Reference,
                ParentID = null,
                Created = now,
                LastModified = now,
            #if !CLIENT
                UserID = currentUser.ID,
            #endif
            };
            folder.Items.Add(item);

            // add defaultList References for each ItemType
            int sortOrder = 1;
            foreach (var keyValue in defaultLists)
            {
                item = new Item()
                {
                    ID = Guid.NewGuid(),
                    SortOrder = sortOrder++,
                    Name = SystemItemTypes.Names[keyValue.Key],
                    FolderID = folder.ID,
                    IsList = false,
                    ItemTypeID = SystemItemTypes.Reference,
                    ParentID = defaultListItemID,
                    Created = now,
                    LastModified = now,
            #if CLIENT
                    FieldValues = new ObservableCollection<FieldValue>(),
            #else
                    FieldValues = new List<FieldValue>(),
                    UserID = currentUser.ID,
            #endif
                };

                if (keyValue.Value is Folder)
                {
                    Folder folderToRef = (Folder)keyValue.Value;
            #if CLIENT
                    item.GetFieldValue(FieldNames.EntityRef, true).Value = folderToRef.ID.ToString();
                    item.GetFieldValue(FieldNames.EntityType, true).Value = EntityTypes.Folder;
                    item.GetFieldValue(FieldNames.Value, true).Value = keyValue.Key.ToString();
            #else
                    item.FieldValues.Add(Item.CreateFieldValue(item.ID, FieldNames.EntityRef, folderToRef.ID.ToString()));
                    item.FieldValues.Add(Item.CreateFieldValue(item.ID, FieldNames.EntityType, EntityTypes.Folder));
                    item.FieldValues.Add(Item.CreateFieldValue(item.ID, FieldNames.Value, keyValue.Key.ToString()));
            #endif
                }
                if (keyValue.Value is Item)
                {
                    Item itemToRef = (Item)keyValue.Value;
            #if CLIENT
                    item.GetFieldValue(FieldNames.EntityRef, true).Value = itemToRef.ID.ToString();
                    item.GetFieldValue(FieldNames.EntityType, true).Value = EntityTypes.Item;
                    item.GetFieldValue(FieldNames.Value, true).Value = keyValue.Key.ToString();
            #else
                    item.FieldValues.Add(Item.CreateFieldValue(item.ID, FieldNames.EntityRef, itemToRef.ID.ToString()));
                    item.FieldValues.Add(Item.CreateFieldValue(item.ID, FieldNames.EntityType, EntityTypes.Item));
                    item.FieldValues.Add(Item.CreateFieldValue(item.ID, FieldNames.Value, keyValue.Key.ToString()));
            #endif
                }

                folder.Items.Add(item);
            }

            return folders;
        }