/// <remarks>
 /// adds new entry to Contacts table
 /// </remarks>
 /// <param name="customerID"></param>
 /// <param name="contactTypeID"></param>
 /// <param name="value"></param>
 /// <param name="created"></param>
 public void addNew (int customerID, int contactTypeID, string value, DateTime created)
 {
     _log.Trace("in addNew()");
     using (Keskus_baasEntities db = new Keskus_baasEntities())
     {
         DAL.Contact contact = new DAL.Contact
         {
             CustomerID = customerID,
             ContactTypeID = contactTypeID,
             Value = value,
             Created = created
         };
         db.Contacts.Add(contact);
        
         try
         {
             db.SaveChanges();
             _userlog.Trace(string.Format("Contact was added on {0} for customer {1}: type {2} - value {3}", contact.Created, contact.CustomerID, contact.ContactTypeID, contact.Value ));
         }
         catch (Exception ex)
         {
             _log.Trace(string.Format("addNew() - An error occurred: '{0}'", ex));
         }
     }
 }
        /// <remarks>
        /// Add new Administrator entry
        /// </remarks>
        /// <param name="name"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="changeAllowed"></param>
        public void addNew (string name, string userName, string password, bool changeAllowed)
        {
            
            _log.Trace("in addNew()");
            //encrypt password to hash
            string hPassword = EncryptPassword.computeHash(password, new MD5CryptoServiceProvider());

            using (Keskus_baasEntities db = new Keskus_baasEntities())
            {
                DAL.Administrator newAdmin = new DAL.Administrator
                {
                    Name = name,
                    CanChange = changeAllowed,
                    Password = hPassword,
                    Username = userName
                };
                db.Administrators.Add(newAdmin);
                try
                {
                    db.SaveChanges();
                    //userlog registeres new db entry added event 
                    _userlog.Trace(string.Format("New admin added: {0}, {1}, {2}", newAdmin.Name, newAdmin.Username, newAdmin.CanChange));
                }
                catch (Exception ex)
                {
                    _log.Trace(string.Format("addNew() - An error occurred: '{0}'", ex));
                }
            }
        }
 /// <remarks>
 /// adds new ContactType entry to DB
 /// </remarks>
 /// 
 public void addNew(string name, bool compulsory)
 {
     _log.Trace("in addNew()");
     using (Keskus_baasEntities db = new Keskus_baasEntities())
     {
         DAL.ContactType contactType = new DAL.ContactType
         {
             Name = name,
             Compulsory = compulsory
         };
         db.ContactTypes.Add(contactType);
         try
         {
             db.SaveChanges();
             _userlog.Trace(string.Format("ContactType added: {0}, {1}", contactType.Name, contactType.Compulsory));
         }
         catch (Exception ex)
         {
             _log.Trace(string.Format("addNew() - An error occurred: '{0}'", ex));
         }
     }
 }
 /// <remarks>
 /// adds new Customer ntry to DB
 /// </remarks>
 /// <param name="companyName"></param>
 /// <param name="contactPerson"></param>
 /// <returns> customer ID as integer </returns>
 public int addNew(string companyName, string contactPerson)
 {
     _log.Trace("in addNew()");
     using (Keskus_baasEntities db = new Keskus_baasEntities())
     {
         DAL.Customer customer = new DAL.Customer
         {
             CompanyName = companyName,
             ContactPerson = contactPerson
         };
         db.Customers.Add(customer);
         try
         {
             db.SaveChanges();
             _userlog.Trace(string.Format("New customer added. ID {0}: {1}, {2}", customer.CustomertID, customer.CompanyName, customer.ContactPerson));
             return customer.CustomertID;
         }
         catch (Exception ex)
         {
             _log.Trace(string.Format("addNew() - An error occurred: '{0}'", ex));
         }
     }
     return 0;
 }
 /// <remarks>
 /// clears Contacts Table. 
 /// Used for development of app, to ensure uniform data in DB on all dev stations
 /// </remarks>
 public void emptyTable()
 {
     _log.Trace("in emptyTable()");
     using (Keskus_baasEntities db = new Keskus_baasEntities())
     {
         foreach (var row in db.Contacts)
             db.Contacts.Remove(row);
         db.SaveChanges();
     }
 }
        /// <remarks>
        /// updates contact entry in DB with data provided by user
        /// </remarks>
        /// <param name="contactID"></param>
        /// <param name="customerID"></param>
        /// <param name="contactTypeID"></param>
        /// <param name="value"></param>
        /// <param name="created"></param>
        public void UpdateById(int contactID, int customerID, int contactTypeID, string value, DateTime created)
        {
            _log.Trace("in UpdateById()");
            using (Keskus_baasEntities db = new Keskus_baasEntities())
            {
                DAL.Contact toUpdate = (from x in db.Contacts
                                    where x.ContactID == contactID
                                        select x).FirstOrDefault();
                

                _userlog.Trace(string.Format("Contact changed from: {0}, {1}, {2}", toUpdate.CustomerID, toUpdate.ContactTypeID, toUpdate.Value));

                toUpdate.CustomerID = customerID;
                toUpdate.ContactTypeID = contactTypeID;
                toUpdate.Value = value;
                toUpdate.Created = created;
                try
                {
                    db.SaveChanges();
                    _userlog.Trace(string.Format("Contact changed to: {0}, {1}, {2}", toUpdate.CustomerID, toUpdate.ContactTypeID, toUpdate.Value));

                }
                catch (Exception ex)
                {
                    _log.Trace(string.Format("UpdateById() - An error occurred: '{0}'", ex));
                }
            }
        }
        /// <remarks>
        /// adds new Booking entry to database
        /// </remarks>
        /// <param name="date"></param>
        /// <param name="roomID"></param>
        /// <param name="customerID"></param>
        /// <param name="participants"></param>
        /// <param name="created"></param>
        /// <param name="adminID"></param>
        /// <param name="addInfo"></param>
        public void addNew(DateTime date, int roomID, int customerID, int participants, DateTime created, int adminID, string addInfo )
        {
            _log.Trace("in addNew()");
            using (Keskus_baasEntities db = new Keskus_baasEntities())
            {
                DAL.Booking booking = new DAL.Booking
                {
                    Date = date,
                    RoomID = roomID,
                    CustomerID = customerID,
                    Participants = participants,              
                    Created = created,
                    AdminID = adminID,
                    AdditionalInfo = addInfo,
                    //by default Archived and Confirmed values are null
                    Archived = null,
                    Confirmed = null

                };
                db.Bookings.Add(booking);
                try
                {
                    db.SaveChanges();
                    _userlog.Trace(string.Format("Added new booking {0} by {1} on {2}: date {3}, room:{4}, customer:{5}, participants: {6}, additional: {7} ", booking.BookingID, booking.Administrator, booking.Created, booking.Date, booking.Room, booking.Customer, booking.Participants, booking.AdditionalInfo));
                }
                catch (Exception ex)
                {
                    _log.Trace(string.Format("addNew() - An error occurred: '{0}'", ex));
                }
            }
        }
        /// <remarks>
        /// Sets existing DB booking entry property Archived to current DateTime value
        /// </remarks>
        /// <param name="booking"></param>
        public void archiveBooking(BookingBO booking)
        {
            _log.Trace("in archiveBooking()");
            using (Keskus_baasEntities db = new Keskus_baasEntities())
            {
                DAL.Booking toArchive = (from x in db.Bookings
                                         where x.BookingID == booking.BookingID
                                         select x).FirstOrDefault();

                toArchive.Archived = DateTime.Now;
                try
                {
                    db.SaveChanges();
                    _userlog.Trace(string.Format("Booking {0} is archived on {1} ", toArchive.BookingID, toArchive.Archived));
                }
                catch (Exception ex)
                {
                    _log.Trace(string.Format("archiveBooking() - An error occurred: '{0}'", ex));
                }
            }
        }
        /// <remarks>
        /// updates existing booking entry data in DB by ID
        /// </remarks>
        /// <param name="bookingId"></param>
        /// <param name="date"></param>
        /// <param name="roomID"></param>
        /// <param name="customerID"></param>
        /// <param name="participants"></param>
        /// <param name="addInfo"></param>
        public void UpdateById(int bookingId, DateTime date, int roomID, int customerID, int participants, string addInfo)
        {
            _log.Trace("in UpdateById()");
            using (Keskus_baasEntities db = new Keskus_baasEntities())
            {
                DAL.Booking toUpdate = (from x in db.Bookings
                                         where x.BookingID == bookingId
                                         select x).FirstOrDefault();

                _userlog.Trace(string.Format("Booking {0} of {1} updated, from: date {2}, room:{3},  participants: {4}, additional: {5} ", toUpdate.BookingID, toUpdate.CustomerID, toUpdate.Date, toUpdate.Room, toUpdate.Participants, toUpdate.AdditionalInfo));
                toUpdate.Date = date;
                toUpdate.RoomID = roomID;
                toUpdate.Participants = participants;
                toUpdate.CustomerID = customerID;
                toUpdate.AdditionalInfo = addInfo;

                try
                {
                    db.SaveChanges();
                    _userlog.Trace(string.Format("Booking {0} of {1} updated, to: date {2}, room:{3},  participants: {4}, additional: {5} ", toUpdate.BookingID, toUpdate.CustomerID, toUpdate.Date, toUpdate.Room, toUpdate.Participants, toUpdate.AdditionalInfo));
                }
                catch (Exception ex)
                {
                    _log.Trace(string.Format("UpdateById() - An error occurred: '{0}'", ex));
                }
            }
        }
        /// <remarks>
        /// updates the existing customer entry, with data provided by user
        /// </remarks>
        /// <param name="customerID"></param>
        /// <param name="companyName"></param>
        /// <param name="contactPerson"></param>
        public void UpdateById(int customerID, string companyName, string contactPerson)
        {
            _log.Trace("in UpdateById()");
            using (Keskus_baasEntities db = new Keskus_baasEntities())
            {
                DAL.Customer toUpdate = (from x in db.Customers
                                         where x.CustomertID == customerID
                                         select x).FirstOrDefault();

                
                _userlog.Trace(string.Format("Customer updated, from: {0}, {1}", toUpdate.CompanyName, toUpdate.ContactPerson));
                toUpdate.CustomertID = customerID;
                toUpdate.CompanyName = companyName;
                toUpdate.ContactPerson = contactPerson;

                try
                {
                    db.SaveChanges();
                    _userlog.Trace(string.Format("Customer updated, to: {0}, {1}", toUpdate.CompanyName, toUpdate.ContactPerson));
                }
                catch (Exception ex)
                {
                    _log.Trace(string.Format("UpdateById() - An error occurred: '{0}'", ex));
                }
            }
        }
        /// <remarks>
        /// rewrites existing room object property valued with user input
        /// </remarks>
        /// <param name="id"></param>
        /// <param name="active"></param>
        /// <param name="seats"></param>
        /// <param name="name"></param>
        public void updateRoom(int id, bool active, int seats, String name)
        {
            using (Keskus_baasEntities db = new Keskus_baasEntities())
            {
                DAL.Room updated = (from x in db.Rooms
                                    where x.RoomID == id
                                    select x).FirstOrDefault();
                
                if (updated != null)
                {
                    // userlog registers old values before update
                    _userlog.Trace(string.Format("Room updated, from: {0}, {1}, {2}", updated.Name, updated.Seats, updated.Active));

                    updated.Active = active;
                    updated.Seats = seats;
                    updated.Name = name;
                }
 
                try
                {
                    db.SaveChanges();
                    _userlog.Trace(string.Format("Room updated, to: {0}, {1}, {2}", updated.Name, updated.Seats, updated.Active));
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: '{0}'", e);
                    throw;
                }


            }
        }
 /// <remarks>
 /// adds new Room entry to DB
 /// Used for development of app, to ensure uniform data in DB on all dev stations
 /// </remarks>
 public void addNew(string name, int seats, bool active)
 {
     _log.Trace("in addNew()");
     using (Keskus_baasEntities db = new Keskus_baasEntities())
     {
         DAL.Room room = new DAL.Room
         {
             Name = name,
             Seats = seats,
             Active = active
         };
         db.Rooms.Add(room);
         try
         {
             db.SaveChanges();
             //userlog registeres new db entry added event 
             _userlog.Trace(string.Format("New room added: {0}, {1}, {2}", room.Name, room.Seats, room.Active));
         }
         catch (Exception ex)
         {
             _log.Trace(string.Format("addNew() - An error occurred: '{0}'", ex));
         }
     }
 }