Ejemplo n.º 1
0
 public ActionResult InstallCategory(GalleryCategory category)
 {
     var jsResult = new JsInstallResult();
     object o = GalleryProcessor.InstallCategory(this.StorageContext, Storage.NewSuggestionsContext, this.CurrentUser, null, null, category);
     if (o != null)
     {
         Folder folder = o as Folder;
         Item item = o as Item;
         if (folder != null) { jsResult.FolderID = folder.ID; }
         if (item != null) { jsResult.FolderID = item.FolderID; jsResult.ItemID = item.ID; }
     }
     else
     {
         jsResult.StatusCode = HttpStatusCode.NotFound;
     }
     JsonResult result = new JsonResult();
     result.Data = jsResult;
     return result;
 }
Ejemplo n.º 2
0
        // may return either Folder or Item (caller should verify)
        public static object InstallCategory(UserStorageContext userContext, SuggestionsStorageContext suggestionsContext, User user, Folder category, Guid? parentID, GalleryCategory newCategory)
        {
            object result = null;
            try
            {
                // find the gallery category in the database (by ID if present, otherwise by name)
                GalleryCategory galleryCategory = newCategory.ID > 0 ?
                    suggestionsContext.GalleryCategories.FirstOrDefault(gc => gc.ID == newCategory.ID) :
                    suggestionsContext.GalleryCategories.FirstOrDefault(gc => gc.Name == newCategory.Name);
                if (galleryCategory == null)
                {
                    TraceLog.TraceError("InstallCategory could not find Category ID " + newCategory.ID);
                    return result;
                }

                // if the caller didn't pass in a Category, merge with an existing folder (by name), or create a new folder
                if (category == null)
                {
                    category = CreateCategory(userContext, user, newCategory.Name);
                    parentID = null;
                    result = category;
                }
                else
                {
                    // otherwise, create this (sub)category as a subitem
                    DateTime now = DateTime.Now;
                    var subCategory = new Item()
                    {
                        ID = Guid.NewGuid(),
                        Name = galleryCategory.Name,
                        FolderID = category.ID,
                        ParentID = parentID,
                        UserID = category.UserID,
                        ItemTypeID = SystemItemTypes.Category,
                        IsList = true,
                        Created = now,
                        LastModified = now,
                    };
                    result = subCategory;

                    // make this the last subcategory in the category
                    float sortOrder = (userContext.Items.Any(i => i.UserID == category.UserID && i.FolderID == category.ID &&
                                                                  i.ItemTypeID == SystemItemTypes.Category && (parentID.HasValue ? i.ParentID == parentID : i.ParentID == null)) ?
                        userContext.Items.Where(i => i.UserID == category.UserID && i.FolderID == category.ID &&
                                                     i.ItemTypeID == SystemItemTypes.Category && (parentID.HasValue ? i.ParentID == parentID : i.ParentID == null)).
                        Select(i => i.SortOrder).
                        Max() :
                        0f) + 1000f;
                    subCategory.SortOrder = sortOrder;

                    userContext.Items.Add(subCategory);
                    userContext.SaveChanges();
                    parentID = subCategory.ID;
                }

                // process all of the subcategories under this (sub)category in the database
                var subCategories = suggestionsContext.GalleryCategories.Where(gc => gc.ParentID == galleryCategory.ID);
                foreach (var sc in subCategories)
                    InstallCategory(userContext, suggestionsContext, user, category, parentID, sc);

                // process all the Activities under this (sub)category
                var activities = suggestionsContext.GalleryActivities.Where(ga => ga.CategoryID == galleryCategory.ID);
                foreach (var activity in activities)
                    InstallActivity(userContext, suggestionsContext, category, parentID, activity);
            }
            catch (Exception ex)
            {
                TraceLog.TraceException("InstallCategory failed", ex);
                return result;
            }
            return result;
        }
Ejemplo n.º 3
0
        static GalleryCategory CreateCategory(string dirname, int? parentID, ref int currID, bool inGallery)
        {
            bool cdBack = false;
            const string prefix = @".\";
            const string suffix = @".json";

            try
            {
                Directory.SetCurrentDirectory(dirname);
                cdBack = true;

                // construct category name
                string categoryName = dirname.StartsWith(prefix) ? dirname.Substring(prefix.Length) : dirname;
                var index = categoryName.IndexOf('-');
                if (index >= 0)
                    categoryName = categoryName.Substring(index + 1);

                var galleryCategory = new GalleryCategory()
                {
                    ID = currID++,
                    Name = categoryName,
                    ParentID = parentID,
                    InGallery = inGallery,
                    Activities = new List<GalleryActivity>(),
                    Subcategories = new List<GalleryCategory>()
                };

                foreach (var dir in Directory.EnumerateDirectories(@"."))
                    galleryCategory.Subcategories.Add(CreateCategory(dir, galleryCategory.ID, ref currID, inGallery));

                foreach (var filename in Directory.EnumerateFiles(@".", @"*.json"))
                {
                    using (var file = File.Open(filename, FileMode.Open, FileAccess.Read))
                    using (var reader = new StreamReader(file))
                    {
                        // strip ".\" off the beginning of the filename, and the ".json" extension
                        string activityName = filename.StartsWith(prefix) ? filename.Substring(prefix.Length) : filename;
                        activityName = activityName.Replace(suffix, "");

                        string activityDef = reader.ReadToEnd();
                        if (!String.IsNullOrEmpty(activityDef))
                        {
                            // crack the definition open and retrieve the name if available
                            var value = JObject.Parse(activityDef);
                            if (!String.IsNullOrEmpty((string)value["Name"]))
                                activityName = (string)value["Name"];
                            var filter = value["Filter"] != null ? value["Filter"].ToString() : null;

                            int activityID = 0;
                            try
                            {
                                activityID = value["ID"].Value<int>();
                            }
                            catch (Exception ex)
                            {
                                TraceLog.TraceException(String.Format("Activity definition for {0} is missing an ID", activityName), ex);
                                continue;
                            }
                            if (activityID <= 0)
                            {
                                TraceLog.TraceError(String.Format("Activity definition for {0} is missing an ID", activityName));
                                continue;
                            }

                            galleryCategory.Activities.Add(new GalleryActivity()
                            {
                                ID = activityID,
                                Name = activityName,
                                Definition = activityDef,
                                CategoryID = galleryCategory.ID,
                                InGallery = inGallery,
                                Filter = filter
                            });
                        }
                    }
                }
                cdBack = false;
                Directory.SetCurrentDirectory(@"..");
                return galleryCategory;
            }
            catch (Exception ex)
            {
                TraceLog.TraceException("Reading gallery failed", ex);
                if (cdBack)
                    Directory.SetCurrentDirectory(@"..");
                return null;
            }
        }