コード例 #1
0
        public void Update(T item)
        {
            MPRSession session = MPRController.StartSession();

            MPRController.Save <T>(session, item);
            MPRController.EndSession(session, true);
        }
コード例 #2
0
        protected void itemDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            // Because ObjectDataSource loses all the non-displayed values, as well as composite values,
            // we need to reload them here from the object.
            log.Debug(String.Format("Updating item {0}", e.Keys["Id"]));

            MPRSession session = MPRController.StartSession();
            MPItem     item    = MPRController.RetrieveById <MPItem>(session, (Int64)e.Keys["Id"]);

            item.Name              = (string)e.NewValues["Name"];
            item.Description       = (string)e.NewValues["Description"];
            item.DescriptionShort  = (string)e.NewValues["DescriptionShort"];
            item.Author            = (string)e.NewValues["Author"];
            item.Homepage          = (string)e.NewValues["Homepage"];
            item.License           = (string)e.NewValues["License"];
            item.LicenseMustAccept = (bool)e.NewValues["LicenseMustAccept"];
            item.Tags              = MPRController.GetTags(session, ((TextBox)itemDetailsView.FindControl("tagsTextBox")).Text);
            MPRController.Save <MPItem>(session, item);
            MPRController.EndSession(session, true);

            log.Info(String.Format("Updated item {0} ({1})", e.Keys["Id"], e.NewValues["Name"]));

            // Manually reset the form to view format
            e.Cancel = true;
            itemDetailsView.ChangeMode(DetailsViewMode.ReadOnly);
        }
コード例 #3
0
        public void TestUserAdd()
        {
            MPRSession session = MPRController.StartSession();

            MPUser user = new MPUser();

            user.Name   = "joe submitter";
            user.EMail  = "mp-test@localhost";
            user.Handle = "submitter";

            IList <MPUserPermission> permissions = session.Session
                                                   .CreateQuery("from MPUserPermission perms where perms.Name in ('Download', 'Add')")
                                                   .List <MPUserPermission>();

            user.Permissions.AddAll(permissions);

            MPRController.Save <MPUser>(session, user);

            user = new MPUser();

            user.Name   = "james admin";
            user.EMail  = "mp-test@localhost";
            user.Handle = "admin";

            permissions = session.Session
                          .CreateQuery("from MPUserPermission perms where perms.Name in ('Download', 'Add', 'Approve', 'Delete')")
                          .List <MPUserPermission>();
            user.Permissions.AddAll(permissions);

            MPRController.Save <MPUser>(session, user);


            MPRController.EndSession(session, true);
        }
コード例 #4
0
        public static MPUser GetCurrentUser()
        {
            MPUser user = (MPUser)HttpContext.Current.Session["MPUser"];

            if (user != null)
            {
                return(user);
            }

            string handle = HttpContext.Current.User.Identity.Name;

            if (handle != null)
            {
                // user has been authenticated before (marked remember me)
                // TODO: move this logic to the PostAuthenticateRequest event handler

                MPRSession session = MPRController.StartSession();

                IList <MPUser> users = MPRController.RetrieveEquals <MPUser>(session, "Handle", handle);
                if (users.Count == 1)
                {
                    user           = users[0];
                    user.LastLogin = DateTime.Now;
                    MPRController.Save <MPUser>(session, user);
                    MPRController.EndSession(session, true);

                    HttpContext.Current.Session["MPUser"] = user;
                    return(user);
                }

                MPRController.EndSession(session, false);
            }

            throw new InvalidUserException("The session doesn't contain a valid user");
        }
コード例 #5
0
        public IList <T> GetByForeignKey(string key, Int64 value, string sortKey, MPRController.Direction direction)
        {
            MPRSession session = MPRController.StartSession();
            IList <T>  items   = MPRController.RetrieveByForeignKey <T>(session, key, value, sortKey, direction);

            MPRController.EndSession(session, true);
            return(items);
        }
コード例 #6
0
        public IList <T> GetAll()
        {
            MPRSession session = MPRController.StartSession();
            IList <T>  items   = MPRController.RetrieveAll <T>(session);

            MPRController.EndSession(session, true);
            return(items);
        }
コード例 #7
0
        public IList <T> GetById(Int64 id)
        {
            MPRSession session = MPRController.StartSession();
            List <T>   items   = new List <T>();

            items.Add(MPRController.RetrieveById <T>(session, id));
            MPRController.EndSession(session, true);
            return(items);
        }
コード例 #8
0
        public Int64 Insert(T item)
        {
            MPRSession session = MPRController.StartSession();
            Int64      newId;

            MPRController.Save <T>(session, item, out newId);
            MPRController.EndSession(session, true);
            return(newId);
        }
コード例 #9
0
        public void TestGetAllTypes()
        {
            MPRSession         session = MPRController.StartSession();
            IList <MPItemType> types   = MPRController.RetrieveAll <MPItemType>(session);

            foreach (MPItemType type in types)
            {
                System.Console.WriteLine("{0} : {1} : {2}", type.Id, type.Name, type.Description);
            }
            MPRController.EndSession(session, true);
        }
コード例 #10
0
        public void TestDisplayItemsAll()
        {
            MPRSession session = MPRController.StartSession();

            IList <DisplayItem> items = DisplayController.RetrieveAll <DisplayItem>(session, "LastUpdated", DisplayController.SortDirection.Descending);

            foreach (DisplayItem item in items)
            {
                System.Console.Write("{0} : {1} : {2} : ", item.Name, item.DescriptionShort, item.Rating);
                System.Console.WriteLine("{0} : {1}", item.Downloads, item.LastUpdated);
            }
        }
コード例 #11
0
        public void TestControllerTags()
        {
            MPRSession    session = MPRController.StartSession();
            IList <MPTag> tags    = MPRController.RetrieveAll <MPTag>(session);

            foreach (MPTag tag in tags)
            {
                System.Console.WriteLine("{0} : {1}", tag.Name, tag.Description);
            }

            MPRController.EndSession(session, true);
        }
コード例 #12
0
        public void TestForeignKey()
        {
            MPRSession            session  = MPRController.StartSession();
            IList <MPItemVersion> versions =
                MPRController.RetrieveByForeignKey <MPItemVersion>(session, "Item", 11);

            foreach (MPItemVersion version in versions)
            {
                System.Console.WriteLine("{0} : {1}", version.Version, version.UpdateDate);
            }

            MPRController.EndSession(session, true);
        }
コード例 #13
0
        public void GetCategoriesForIdList()
        {
            MPRSession   session = MPRController.StartSession();
            List <Int64> ids     = new List <Int64>();

            ids.Add(5); ids.Add(7); ids.Add(8);
            IList <MPCategory> categories = MPRController.RetrieveByIdList <MPCategory>(session, ids);

            Assert.That(categories.Count, Is.EqualTo(3));

            foreach (MPCategory category in categories)
            {
                System.Console.WriteLine("{0} : {1} : {2}", category.Id, category.Name, category.Description);
            }
        }
コード例 #14
0
        public void GetCategoriesForType()
        {
            Int64 typeId = 4;

            MPRSession session = MPRController.StartSession();

            IList <MPCategory> categories = MPRController.RetrieveByForeignKey <MPCategory>(session, "Type", typeId);

            foreach (MPCategory category in categories)
            {
                System.Console.WriteLine("{0}:{1}:{2};", category.Id, category.Name, category.Description);
            }

            MPRController.EndSession(session, true);
        }
コード例 #15
0
        protected void versionDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            log.Debug(String.Format("Inserting version to item {0}", itemDetailsView.SelectedValue));

            FileUpload fileUpload = (FileUpload)versionDetailsView.FindControl("fileUpload");

            if ((fileUpload.PostedFile == null) || (fileUpload.PostedFile.ContentLength == 0))
            {
                statusLabel.Text    = "Please upload a file for the new version";
                statusLabel.Visible = true;
                e.Cancel            = true;
                return;
            }

            // Save the file
            string targetFilename = FileManager.GetSaveLocation(fileUpload.PostedFile.FileName);

            try
            {
                fileUpload.PostedFile.SaveAs(targetFilename);
            }
            catch (Exception ex)
            {
                log.Error("Unable to save file to local system: " + ex.ToString());
                statusLabel.Text    = "Error while trying to save file";
                statusLabel.Visible = true;
                e.Cancel            = true;
                return;
            }

            // TODO: Persist target filename somewhere
            ViewState["TargetFileName"]     = System.IO.Path.GetFileName(fileUpload.PostedFile.FileName);
            ViewState["TargetFileLocation"] = targetFilename;

            MPRSession session = MPRController.StartSession();
            MPItem     item    = MPRController.RetrieveById <MPItem>(session, (Int64)itemDetailsView.SelectedValue);

            e.Values["Item"]       = item;
            e.Values["Uploader"]   = SessionUtils.GetCurrentUser();
            e.Values["UpdateDate"] = DateTime.Now;

            MPRController.EndSession(session, true);

            log.Info(String.Format("Added new version to item {0}", itemDetailsView.SelectedValue));

            statusLabel.Text    = "Version added";
            statusLabel.Visible = true;
        }
コード例 #16
0
        public string GetTags()
        {
            MPRSession    session = MPRController.StartSession();
            IList <MPTag> tags    = MPRController.RetrieveAll <MPTag>(session);

            string result = "";

            foreach (MPTag tag in tags)
            {
                result += System.String.Format("{0}:{1}:{2};", tag.Id, tag.Name, tag.Description);
            }

            MPRController.EndSession(session, true);

            return(result);
        }
コード例 #17
0
        public string GetCategories()
        {
            MPRSession         session    = MPRController.StartSession();
            IList <MPCategory> categories = MPRController.RetrieveAll <MPCategory>(session);

            string result = "";

            foreach (MPCategory category in categories)
            {
                result += System.String.Format("{0}:{1}:{2};", category.Id, category.Name, category.Type);
            }

            MPRController.EndSession(session, true);

            return(result);
        }
コード例 #18
0
        public void TestDisplayItemFake()
        {
            MPRSession session = MPRController.StartSession();
            IQuery     query   = session.Session.CreateSQLQuery(
                "select 'myname' as Name, 'my description' as DescriptionShort, 3.5 as Rating, 13500 as Downloads, str_to_date('4/5/2009','%d/%m/%Y') as LastUpdate"
                )
                                 .SetResultTransformer(Transformers.AliasToBean(typeof(DisplayItem)));
            IList <DisplayItem> items = query.List <DisplayItem>();


            foreach (DisplayItem item in items)
            {
                System.Console.Write("{0} : {1} : {2} : ", item.Name, item.DescriptionShort, item.Rating);
                System.Console.WriteLine("{0} : {1}", item.Downloads, item.LastUpdated);
            }
        }
コード例 #19
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MPRSession session = MPRController.StartSession();

                IList <DisplayItem> items = DisplayController.RetrieveAll <DisplayItem>(session, "LastUpdated", DisplayController.SortDirection.Descending);

                itemsGridView.DataSource = items;
                string[] keys = { "Id" };
                itemsGridView.DataKeyNames = keys;
                itemsGridView.DataBind();

                MPRController.EndSession(session, true);
            }
        }
コード例 #20
0
        public void TestTagsParse()
        {
            //string tagsList = "video,music,test2,test3";
            string       tagsList = "video,music,test1";
            MPRSession   session  = MPRController.StartSession();
            ISet <MPTag> tags     = MPRController.GetTags(session, tagsList);

            Assert.That(tags.Count, Is.EqualTo(3));

            foreach (MPTag tag in tags)
            {
                System.Console.WriteLine("{0} : {1}", tag.Id, tag.Name);
            }

            MPRController.EndSession(session, true);
        }
コード例 #21
0
        public string GetTypes()
        {
            MPRSession         session = MPRController.StartSession();
            IList <MPItemType> types   = MPRController.RetrieveAll <MPItemType>(session);

            string result = "";

            foreach (MPItemType type in types)
            {
                result += System.String.Format("{0}:{1}:{2};", type.Id, type.Name, type.Description);
            }

            MPRController.EndSession(session, true);

            return(result);
        }
コード例 #22
0
        public void TestGetUserByHandle()
        {
            string handle = "admin";

            MPRSession session = MPRController.StartSession();

            IList <MPUser> users = MPRController.RetrieveEquals <MPUser>(session, "Handle", handle);

            Assert.That(users.Count, Is.EqualTo(1));

            MPUser user = users[0];

            System.Console.WriteLine("User {0}:\n{1}\n{2}\n{3} permissions", user.Handle, user.Name, user.EMail, user.Permissions.Count);

            Assert.That(user.Permissions.Count, Is.EqualTo(4));

            MPRController.EndSession(session, true);
        }
コード例 #23
0
        protected void itemsGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (itemsGridView.SelectedRow != null)
            {
                MPRSession session = MPRController.StartSession();

                MPItem item = MPRController.RetrieveById <MPItem>(session, (Int64)itemsGridView.SelectedValue);

                if (item == null)
                {
                    throw new InvalidFormDataException("Asked to provide details for invalid item id");
                }

                // TODO: clean up binding to a single object
                List <MPItem> items = new List <MPItem>();
                items.Add(item);
                itemDetailsView.DataSource = items;
                itemDetailsView.DataBind();

                versionDetailsView.DataSource = item.Versions;
                versionDetailsView.DataBind();

                commentsRepeater.DataSource = item.Comments;
                commentsRepeater.DataBind();

                // TODO: Only if submitter or has permission
                singleItemHyperLink.NavigateUrl = "~/single_item.aspx?itemid=" + item.Id.ToString();
                singleItemHyperLink.Visible     = true;

                commentAddTextBox.Text    = "";
                commentAddTextBox.Visible = true;
                commentAddButton.Visible  = true;

                MPRController.EndSession(session, true);
            }
            else
            {
                singleItemHyperLink.NavigateUrl = "";
                singleItemHyperLink.Visible     = false;
                commentAddTextBox.Visible       = false;
                commentAddButton.Visible        = false;
                commentAddLabel.Visible         = false;
            }
        }
コード例 #24
0
        protected void FillCategoriesForSelectedItemType(MPRSession session)
        {
            if (typesList.SelectedValue == null)
            {
                return;
            }

            // Remove the previous categories
            categoriesList.Items.Clear();

            // Fill the categories for the selected type
            Int64 typeId;

            Int64.TryParse(typesList.SelectedValue, out typeId);
            IList <MPCategory> categories = MPRController.RetrieveByForeignKey <MPCategory>(session, "Type", typeId);

            foreach (MPCategory category in categories)
            {
                categoriesList.Items.Add(new ListItem(category.Name, category.Id.ToString()));
            }
        }
コード例 #25
0
        public void TestPermissionAdd()
        {
            string[,] permissions =
            {
                { "Download", "Can download files"                             },
                { "Add",      "Can upload files"                               },
                { "Approve",  "Allowed to approve awaiting items in the queue" },
                { "Delete",   "Allowed to delete items"                        },
            };

            MPRSession session = MPRController.StartSession();

            for (int i = 0; i <= permissions.GetUpperBound(0); i++)
            {
                MPUserPermission permission = new MPUserPermission();
                permission.Name        = permissions[i, 0];
                permission.Description = permissions[i, 1];

                MPRController.Save <MPUserPermission>(session, permission);
            }
            MPRController.EndSession(session, true);
        }
コード例 #26
0
        protected void versionDataSource_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            string filename = (string)ViewState["TargetFileName"];
            string location = (string)ViewState["TargetFileLocation"];


            MPRSession    session = MPRController.StartSession();
            MPItemVersion version = MPRController.RetrieveById <MPItemVersion>(session, (Int64)e.ReturnValue);

            // Add the new file to the Repository
            MPFile mpfile = new MPFile();

            mpfile.ItemVersion = version;
            mpfile.Filename    = filename;
            mpfile.Location    = location;
            version.Files.Add(mpfile);

            MPRController.Save <MPItemVersion>(session, version);
            MPRController.EndSession(session, true);

            ViewState.Remove("TargetFileName");
            ViewState.Remove("TargetFileLocation");
        }
コード例 #27
0
        protected void versionDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            // Because ObjectDataSource loses all the non-displayed values, as well as composite values,
            // we need to reload them here from the object.
            log.Debug(String.Format("Updating version {0}", e.Keys["Id"]));

            MPRSession    session = MPRController.StartSession();
            MPItemVersion version = MPRController.RetrieveById <MPItemVersion>(session, (Int64)e.Keys["Id"]);

            version.Version      = (string)e.NewValues["Version"];
            version.MPVersionMin = (string)e.NewValues["MPVersionMin"];
            version.MPVersionMax = (string)e.NewValues["MPVersionMax"];
            version.ReleaseNotes = (string)e.NewValues["ReleaseNotes"];
            version.UpdateDate   = DateTime.Now;
            MPRController.Save <MPItemVersion>(session, version);
            MPRController.EndSession(session, true);

            log.Info(String.Format("Updated version {0} ({1})", e.Keys["Id"], e.NewValues["Version"]));

            // Manually reset the form to view format
            e.Cancel = true;
            versionDetailsView.ChangeMode(DetailsViewMode.ReadOnly);
        }
コード例 #28
0
        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            e.Authenticated = false;

            // TODO: replace with real authentication
            if (Login1.Password != "password")
            {
                return;
            }

            // Add user to session
            string         handle  = Login1.UserName;
            MPRSession     session = MPRController.StartSession();
            IList <MPUser> users   = MPRController.RetrieveEquals <MPUser>(session, "Handle", handle);

            if (users.Count != 1) // Either none or a serious error
            {
                MPRController.EndSession(session, false);
                return;
            }

            MPUser user = users[0];

            user.LastLogin = DateTime.Now;
            MPRController.Save <MPUser>(session, user);

            HttpContext.Current.Session["MPUser"] = user;

            MPRController.EndSession(session, true);

            e.Authenticated = true;

            log.Info(String.Format("User {0} has logged in", user.Handle));

            FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);
        }
コード例 #29
0
        protected void commentAddButton_Click(object sender, EventArgs e)
        {
            if (commentAddTextBox.Text.Length == 0)
            {
                commentAddLabel.Text    = "Please enter a comment";
                commentAddLabel.Visible = true;
                return;
            }

            if (itemsGridView.SelectedRow == null)
            {
                throw new InvalidFormDataException("Comment can only be added to a selected item");
            }

            MPRSession session = MPRController.StartSession();

            MPItem item = MPRController.RetrieveById <MPItem>(session, (Int64)itemsGridView.SelectedValue);

            if (item == null)
            {
                throw new InvalidFormDataException("Asked to provide details for invalid item id");
            }

            MPItemComment comment = new MPItemComment();

            comment.User = SessionUtils.GetCurrentUser();
            comment.Text = commentAddTextBox.Text;
            item.Comments.Add(comment);
            MPRController.Save <MPItem>(session, item);

            MPRController.EndSession(session, true);

            commentAddTextBox.Text  = "";
            commentAddLabel.Text    = "Comment added. Thank you!";
            commentAddLabel.Visible = true;
        }
コード例 #30
0
        protected void Page_Load(object sender, EventArgs e)
        {
            MPUser user = SessionUtils.GetCurrentUser();

            if (IsPostBack)
            {
                uploadStatusLabel.Text = null;

                if (!user.hasPermission("Add"))
                {
                    uploadStatusLabel.Text = String.Format("User {0} has no permission to add items", user.Handle);
                    return;
                }

                // TODO: Validate form
                // TODO: If file is MPE, try to fill fields from file

                // Handle upload
                if ((fileUpload.PostedFile != null) && (fileUpload.PostedFile.ContentLength > 0))
                {
                    if (HandleUploadedFile())
                    {
                        uploadStatusLabel.Text = "Upload successful.";
                    }
                    else
                    {
                        // Upload failed. Status should already contain the cause.
                        if (uploadStatusLabel.Text == null)
                        {
                            uploadStatusLabel.Text = "Upload failed. Reason unknown.";
                        }
                    }
                }
                else // no file. Just page update
                {
                    MPRSession session = MPRController.StartSession();
                    FillCategoriesForSelectedItemType(session);
                    MPRController.EndSession(session, true);
                }
            }
            else // not PostBack - initial page setup
            {
                // Load user from session
                authorTextBox.Text = user.Handle;

                // Fill development status according to ENum
                developmentStatusDropDownList.DataSource = Enum.GetNames(typeof(MPItemVersion.MPDevelopmentStatus));
                developmentStatusDropDownList.DataBind();

                // Load item types
                MPRSession         session = MPRController.StartSession();
                IList <MPItemType> types   = MPRController.RetrieveAll <MPItemType>(session);
                foreach (MPItemType type in types)
                {
                    typesList.Items.Add(new ListItem(type.Name, type.Id.ToString()));
                }

                FillCategoriesForSelectedItemType(session);

                MPRController.EndSession(session, true);
            }
        }