}                                       //The Book that the window is currently displaying the contents of

        /// <summary>
        /// Constructor for BookDetailsWindow
        /// Initializes controls, fields and Properties depending on the contents of the Book
        /// and whether the user is a librarian or patron
        /// </summary>
        /// <param name="book">Book which details will be displayed</param>
        /// <param name="controller">Controller for the Library Client</param>
        /// <param name="isNewBook">If true, notifies the window that the Book object to be displayed
        /// is a new Book, or already exists in the database.</param>
        public BookDetails(Book book, LibraryClientController controller, bool isNewBook)
        {
            InitializeComponent();

            //Initialize fields and Properties
            this.controller     = controller;
            Book                = book;
            originalAuthor      = Book.Author;
            changedAuthor       = null;
            edited              = false;
            searchResultsWindow = null;
            this.isNewBook      = isNewBook;

            //Subscribe to OnDatabaseError vent
            this.controller.OnDatabaseError += OnDatabaseErrorHandler;

            //Initialize Controls
            InitializeButtons();

            ErrorLabel.Content = string.Empty;
            SetUserType();

            PopulateWindow();

            //If the user is a librarian and Book is new, automatically enable edit buttons
            if (controller.IsLibrarian && isNewBook)
            {
                EnableEditing();
            }
        }
        //Returns the newly created Author object when opration is successful, returns null otherwise.
        /// <summary>
        /// Creates a new Author and MAuthor object with the properties set to the arguments passed.
        /// Author and MAuthor are added to the database context and local collection respectively.
        /// Raises <event>OnDatabaseError</event> when exception is caught from attempting to change database context.
        /// </summary>
        /// <param name="firstName">First name of author</param>
        /// <param name="lastName">Last name of author</param>
        /// <param name="bio">Bio of author</param>
        /// <returns>Returns true if operation is successful, false otherwise</returns>
        public Author CreateAuthor(string firstName, string lastName, string bio)
        {
            try
            {
                //create person
                Person addPerson = new Person();
                addPerson.FirstName = firstName;
                addPerson.LastName  = lastName;

                //add to database to get personID
                context.People.Add(addPerson);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                //Rase OnDatabaseError event
                while (ex != null)
                {
                    ex = ex.InnerException;
                }
                DatabaseEventArgs e = new DatabaseEventArgs(ex.Message);
                OnDatabaseErrorRaised(e);
                return(null);
            }

            try
            {
                //retrieve Person from database with assigned ID
                Author result    = new Author();
                Person addPerson = (from p in context.People
                                    where p.FirstName == firstName && p.LastName == lastName
                                    select p).First();

                result.Person   = addPerson;
                result.PersonId = addPerson.PersonId;
                result.Bio      = bio;

                //Add author to people list
                MAuthor ma = new MAuthor(result);
                peopleList.Add(ma);

                //Add author to database
                context.Authors.Add(result);
                context.SaveChanges();

                return(result);
            }
            catch (Exception ex)
            {
                //Raise OnDatabaseError event
                while (ex != null)
                {
                    ex = ex.InnerException;
                }
                DatabaseEventArgs e = new DatabaseEventArgs(ex.Message);
                OnDatabaseErrorRaised(e);
                return(null);
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Displays a confirmation dialog.  If user confirms selection, change appropriate Properties and close the window
 /// </summary>
 private void SelectAuthor()
 {
     if (MessageBox.Show("Are you sure you would like to make this change?", "Confirm", MessageBoxButton.YesNo,
                         MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
     {
         Result       = (MAuthor)AuthorsListBox.SelectedItem;
         DialogResult = true;
         this.Close();
     }
 }
        /// <summary>
        /// Event handler for EditAuthorButton click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditAuthorButton_Click(object sender, RoutedEventArgs e)
        {
            //Opens a new EditAuthorDialog dialog window.
            EditAuthorDialog ead = new EditAuthorDialog(this, controller);

            if ((bool)ead.ShowDialog())     //if dialog returns true (changes confirmed)
            {
                changedAuthor = ead.Result; //set changed author to the result of dialog window

                //reflect change in control
                AuthorTextBox.Clear();
                AuthorTextBox.Text = String.Format("{0} {1}", changedAuthor.FirstName, changedAuthor.LastName);
            }
        }
        /// <summary>
        /// Assigns the Book object with Author associated with the MAuthor passed in the argument.
        /// </summary>
        /// <param name="assignAuthor">MAuthor that corresponds to the Entity Author to be assigned to Book</param>
        private void AssignAuthor(MAuthor assignAuthor)
        {
            //change Author Property
            Book.Author          = assignAuthor.dbAuthor;
            Book.Author_PersonId = assignAuthor.dbAuthor.PersonId;

            //Save Changes to database
            controller.Save();

            //remove book from original MAuthor
            foreach (MAuthor a in controller.GetAuthorsList())
            {
                if (a.Books.Remove(Book))
                {
                    break;
                }
            }

            //Add book to new MAuthor
            assignAuthor.Books.Add(Book);
        }
        /// <summary>
        /// Retrieves all Librarians, Cardholders, and Authors from database context to generate local memory objects.
        /// Populates local memory collections with the newly created objects.
        /// </summary>
        private void LoadPeopleList()
        {
            //Initialize peopleList
            peopleList               = new PeopleList();
            LibrarianUserIDs         = new Dictionary <string, string>();
            CardholderLibraryCardIDs = new Dictionary <string, MCardholder>();

            //Librarians
            var dbLibrarians = (from l in context.Librarians
                                select l).ToList();

            foreach (Librarian l in dbLibrarians)
            {
                MLibrarian ml = new MLibrarian(l);
                peopleList.Add(ml);
                LibrarianUserIDs.Add(ml.UserID, ml.Password);
            }

            //Cardholders
            var dbCardholders = (from ch in context.Cardholders
                                 select ch).ToList();

            foreach (Cardholder ch in dbCardholders)
            {
                MCardholder mch = new MCardholder(ch);
                peopleList.Add(mch);
                CardholderLibraryCardIDs.Add(mch.LibraryCardId, mch);
            }

            //Authors
            var dbAuthors = (from a in context.Authors
                             select a).ToList();

            foreach (Author a in dbAuthors)
            {
                MAuthor ma = new MAuthor(a);
                peopleList.Add(ma);
            }
        }