/// <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>
        /// 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 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>
 /// gets customer from Customers table in DB by ID
 /// </remarks>
 /// <param name="customerId"></param>
 /// <returns> customer object</returns>
 public CustomerBO getCustomerById(int customerId)
 {
     _log.Trace("in getCustomerById()");
     using (var db = new Keskus_baasEntities())
     {
         return new CustomerBO(db.Customers
             .Where(x => x.CustomertID == customerId)
             .FirstOrDefault());
     }
 }
 /// <remarks>
 /// gets room object from DB table by name
 /// </remarks>
 /// <param name="roomName"></param>
 /// <returns>room object</returns>
 public RoomBO getRoomByName(string roomName)
 {
     _log.Trace("in getRoomByName()");
     using (var db = new Keskus_baasEntities())
     {
         return new RoomBO(db.Rooms
             .Where(x => x.Name == roomName)
             .FirstOrDefault());
     }
 }
 /// <remarks>
 /// get all Administrators from Administrators table
 /// </remarks>
 /// <returns> list of admins</returns>
 public List<AdministratorBO> getAllFromTable()
 {
     _log.Trace("in getAllFromTable()");
     using (var db = new Keskus_baasEntities())
     {
         List<AdministratorBO> admins = db.Administrators
             .ToList()
             .Select(x => new AdministratorBO(x))
             .ToList();
         return admins;
     }
 }
 /// <remarks>
 ///Gets all entries from Contacts table
 /// </remarks>
 /// <returns>list of contacts</returns>
 public List<ContactBO> getAllFromTable()
 {
     _log.Trace("in getAllFromTable()");
     using (var db = new Keskus_baasEntities())
     {
         List<ContactBO> tableContent = db.Contacts
             .ToList()
             .Select(x => new ContactBO(x))
             .ToList();
         return tableContent;
     }
 }
 /// <remarks>
 /// gets all customer contacts from table by customer ID
 /// </remarks>
 /// <param name="customerId"></param>
 /// <returns>list of contacts</returns>
 public List<ContactBO> getAllFromTableByCustomerID(int customerId)
 {
     _log.Trace("in getAllFromTableByCustomerID()");
     using (var db = new Keskus_baasEntities())
     {
         List<ContactBO> tableContent = db.Contacts
             .ToList()
             .Select(x => new ContactBO(x))
             .Where(x => x.CustomerID == customerId)
             .ToList();
         return tableContent;
     }
 }
        /// <remarks>
        /// gets all existing confirmed Booking database entries, that are not archived and will take plase today or later
        /// </remarks>
        /// <returns> list of bookings</returns>
        public List<BookingBO> getConfirmedFromTable()
        {
            _log.Trace("in getConfirmedFromTable()");
            using (var db = new Keskus_baasEntities())
            {
                List<BookingBO> tableContent = db.Bookings
                    .Where(x => x.Confirmed != null && x.Archived==null && DateTime.Compare(x.Date, DateTime.Today) >= 0)
                    .ToList()
                    .Select(x => new BookingBO(x))
                    .ToList();

                return tableContent;
            }
        }
 /// <remarks>
 /// gets ContactType name by ID from DB
 /// </remarks>
 /// <returns>name of contactType as string</returns>
 public string getNameById (int id)
 {
     _log.Trace("in getNameById()");
     using (var db = new Keskus_baasEntities())
     {
         DAL.ContactType details = db.ContactTypes
             .Where(x => x.ContactTypeID == id)
             .FirstOrDefault();
         if (details != null)
         {
             return details.Name;
         }
         return "";
     }
 }
        public BookingBO(Keskus.DAL.Booking booking)
        {
            this.BookingID = booking.BookingID;
            this.Date = booking.Date;
            this.RoomID = booking.RoomID;
            this.CustomerID = booking.CustomerID;
            this.Participants = booking.Participants;
            this.Created = booking.Created;
            this.Confirmed = booking.Confirmed;
            this.AdditionalInfo = booking.AdditionalInfo;
            this.Archived = booking.Archived;

            using (var db = new Keskus_baasEntities())
            {
                var getCustomer = db.Customers
                    .Where(x => x.CustomertID == booking.CustomerID)
                    .FirstOrDefault();
                //getting klient PIC name from Klients database
                this.CustomerContactPerson = getCustomer.ContactPerson;

                //getting klient ORG name from Klients database
                this.CustomerCompany = getCustomer.CompanyName;

                
                var getRoom = db.Rooms
                   .Where(x => x.RoomID == booking.RoomID)
                   .FirstOrDefault();

                //getting Room name from Ruums database
                this.Room = getRoom.Name;

                //getting Room status from Ruums database
                this.RoomActive = getRoom.Active;

                
                var getAdmin = db.Administrators
                   .Where(x => x.AdminID == booking.AdminID)
                   .FirstOrDefault();

                //getting Admin name from Administraators database
                this.Admin = getAdmin.Name;
            }

        }
 /// <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>
        /// 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>
 /// gets rooms with property Active == true from DB table
 /// </remarks>
 /// <returns>list of active rooms</returns>
 public List<RoomBO> getActiveRooms()
 {
     using (var db = new Keskus_baasEntities())
     {
         var activeRooms = db.Rooms
             .Where(x => x.Active == true)
             .ToList()
             .Select(x => new RoomBO(x))
             .ToList();
         if (activeRooms != null) return activeRooms;
         else return null;
         
     }
 }
 /// <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));
         }
     }
 }
 /// <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>
 /// searches administrator entry in DB table by ID
 /// </remarks>
 /// <param name="adminId"></param>
 /// <returns>Administrator object or null, if object not found</returns>
 public AdministratorBO findAdminByID (int adminId)
 {
     _log.Trace("in findAdminByID()");
     using (var db = new Keskus_baasEntities())
     {
         var admin = db.Administrators
             .Where(x => x.AdminID == adminId)
             .FirstOrDefault();
         if (admin != null)
         {
             return new AdministratorBO(admin);
         }
         _log.Trace("in findAdminByID() - admin == null");
         return null;
     }
 }
        /// <remarks>
        /// gets all customers from Customers table in DB by matching part of customer company name
        /// </remarks>
        /// <param name="customerId"></param>
        /// <returns> list of customers</returns>
        public List<CustomerBO> searchCustomerByCompany(string searchCompany)
        {
            _log.Trace("in searchCustomerByCompany()");
            string searchCompToLower = searchCompany.ToLower();
            List<CustomerBO> foundCustomers = new List<CustomerBO>();

            using (var db = new Keskus_baasEntities())
            {
                foreach (var customer in db.Customers)
                {
                    if (customer.CompanyName.ToLower().Contains(searchCompToLower))
                    {
                        foundCustomers.Add(new CustomerBO(customer));
                    }
                }
            }
            return foundCustomers;

        }
        /// <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>
        /// searches admin entry in Administartors table by admin name
        /// </remarks>
        /// <param name="name"></param>
        /// <returns>Administrator object or null, if object not found</returns>
        public AdministratorBO getAdminByName(string name)
        {
            _log.Trace("in getAdminByName()");

            using (var db = new Keskus_baasEntities())
            {
                var admin = db.Administrators
                    .Where(x => x.Name == name)
                    .FirstOrDefault();
                if (admin != null)
                {
                    return new AdministratorBO(admin);
                }
                _log.Trace("in getAdminByName() - admin == null");
                return null;
            }
        }
        /// <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>
 /// gets first customer from Customers table in DB by customer company name
 /// </remarks>
 /// <param name="customerId"></param>
 /// <returns> customer object</returns>
 public CustomerBO getCustomerByName(string customerName)
 {
     _log.Trace("in getCustomerByName()");
     using (var db = new Keskus_baasEntities())
     {
         return new CustomerBO(db.Customers
             .Where(x => x.CompanyName == customerName)
             .FirstOrDefault());
     }
 }