public void CreateFacility(int locationId, ref Facility facility)
        {
            //Save the facility record
            var insertedRowId = _da.Value.Facility.Insert(facility);

            if (insertedRowId == 0)
            {
                throw new Exception("Failed to create facility record");
            }
            else
            {
                facility.Id = insertedRowId;
            }

            //Link the facility and location records
            var locationFacilityLink = new LinkObjectMaster()
            {
                MasterLinkId = locationId,
                MasterLinkType = LinkType.Location,
                ChildLinkId = facility.Id,
                ChildLinkType = LinkType.Facility
            };

            //Save the link record
            insertedRowId = _da.Value.Link.Insert(locationFacilityLink);

            if (insertedRowId == 0)
            {
                //Roll back the inserts as it's failed
                //Delete the facility record
                _da.Value.Facility.Delete(facility);

                throw new Exception("Failed to create location facility link record, transaction rolled back");
            }
        }
        public void CreateCourtAndLinkToFacility(int facilityId, ref Court court)
        {
            //Save the court record
            var insertedRowId = _da.Value.Court.Insert(court);

            if (insertedRowId == 0)
            {
                throw new Exception("Failed to create court record");
            }
            else
            {
                court.Id = insertedRowId;
            }

            //Link the court and facility records
            var facilityCourtLink = new LinkObjectMaster()
            {
                MasterLinkId = facilityId,
                MasterLinkType = LinkType.Facility,
                ChildLinkId = court.Id,
                ChildLinkType = LinkType.Court
            };

            //Save the link record
            insertedRowId = _da.Value.Link.Insert(facilityCourtLink);

            if (insertedRowId == 0)
            {
                //Roll back the inserts as it's failed
                //Delete the court record
                _da.Value.Court.Delete(court);

                throw new Exception("Failed to create facility court link record, transaction rolled back");
            }
        }
        public LinkObjectMaster GetById(int id)
        {
            var linkObject = new LinkObjectMaster
            {
                Id = 0
            };

            return linkObject;
        }
        public void Delete(LinkObjectMaster deleteThis)
        {
            _dataEngine.InitialiseParameterList();
            _dataEngine.AddParameter("@Id", deleteThis.Id.ToString());

            _sqlToExecute = "DELETE FROM [dbo].[LinkObjectMaster] WHERE Id = " + _dataEngine.GetParametersForQuery();

            if (!_dataEngine.ExecuteSql(_sqlToExecute))
                throw new Exception("Link - Delete failed");
        }
        public void CreateCustomer(ref Customer customer, ref Address address)
        {
            //Save the customer record
            var insertedRowId = _da.Value.Customer.Insert(customer);

            if (insertedRowId == 0)
            {
                throw new Exception("Failed to create customer record");
            }
            else
            {
                customer.Id = insertedRowId;
            }

            //Save the address record
            if (address.Id == 0)
            {
                //Create a new address record
                insertedRowId = _da.Value.Address.Insert(address);

                if (insertedRowId == 0)
                {
                    throw new Exception("Failed to create address record");
                }
                else
                {
                    address.Id = insertedRowId;
                }
            }

            //Link the customer and address records
            var customerAddressLink = new LinkObjectMaster()
            {
                MasterLinkId = customer.Id,
                MasterLinkType = LinkType.Customer,
                ChildLinkId = address.Id,
                ChildLinkType = LinkType.Address
            };

            //Save the link record
            insertedRowId = _da.Value.Link.Insert(customerAddressLink);

            if (insertedRowId == 0)
            {
                //Roll back the inserts as it's failed
                //Delete the customer record
                _da.Value.Customer.Delete(customer);

                //Delete the address record
                _da.Value.Address.Delete(address);

                throw new Exception("Failed to create customer address link record, transaction rolled back");
            }
        }
 public void Delete(LinkObjectMaster deleteThis)
 {
     try
     {
         if (deleteThis.Id > 0)
         {
             _db.Links.DeleteOnSubmit(deleteThis);
             _db.SubmitChanges(ConflictMode.FailOnFirstConflict);
         }
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }
        /// <summary>
        /// Creates the object from the data returned from the database
        /// </summary>
        /// <returns></returns>
        private LinkObjectMaster CreateLinkObjectFromData()
        {
            var linkObject = new LinkObjectMaster
            {
                Id = int.Parse(_dataEngine.Dr["Id"].ToString()),
                MasterLinkType = (LinkType)int.Parse(_dataEngine.Dr["MasterLinkTypeId"].ToString()),
                MasterLinkId = _dataEngine.Dr["MasterLinkTypeId"] == DBNull.Value ? (int?)null : int.Parse(_dataEngine.Dr["MasterLinkId"].ToString()),
                ChildLinkType = (LinkType)int.Parse(_dataEngine.Dr["ChildLinkTypeId"].ToString()),
                ChildLinkId = _dataEngine.Dr["ChildLinkId"] == DBNull.Value ? (int?)null : int.Parse(_dataEngine.Dr["ChildLinkId"].ToString())
            };

            return linkObject;
        }
        public void Update(LinkObjectMaster saveThis)
        {
            _dataEngine.InitialiseParameterList();
            _dataEngine.AddParameter("@MasterLinkTypeId", ((int)saveThis.MasterLinkType).ToString());
            _dataEngine.AddParameter("@MasterLinkId", saveThis.MasterLinkId.ToString());
            _dataEngine.AddParameter("@ChildLinkTypeId", ((int)saveThis.ChildLinkType).ToString());
            _dataEngine.AddParameter("@ChildLinkId", saveThis.ChildLinkId.ToString());

            _sqlToExecute = "UPDATE [dbo].[LinkObjectMaster] SET ";
            _sqlToExecute += "[MasterLinkTypeId] = @MasterLinkTypeId";
            _sqlToExecute += ",[MasterLinkId] = @MasterLinkId";
            _sqlToExecute += ",[ChildLinkTypeId] = @ChildLinkTypeId";
            _sqlToExecute += ",[ChildLinkId] = @ChildLinkId ";
            _sqlToExecute += "WHERE [Id] = " + saveThis.Id;

            if (!_dataEngine.ExecuteSql(_sqlToExecute))
                throw new Exception("Link - Update failed");
        }
        public int Insert(LinkObjectMaster saveThis)
        {
            _dataEngine.InitialiseParameterList();
            _dataEngine.AddParameter("@MasterLinkTypeId", ((int)saveThis.MasterLinkType).ToString());
            _dataEngine.AddParameter("@MasterLinkId", saveThis.MasterLinkId.ToString());
            _dataEngine.AddParameter("@ChildLinkTypeId", ((int)saveThis.ChildLinkType).ToString());
            _dataEngine.AddParameter("@ChildLinkId", saveThis.ChildLinkId.ToString());

            _sqlToExecute = "INSERT INTO [dbo].[LinkObjectMaster] ";
            _sqlToExecute += "([MasterLinkTypeId]";
            _sqlToExecute += ",[MasterLinkId]";
            _sqlToExecute += ",[ChildLinkTypeId]";
            _sqlToExecute += ",[ChildLinkId]) ";
            _sqlToExecute += "OUTPUT INSERTED.Id ";
            _sqlToExecute += "VALUES ";
            _sqlToExecute += "(";
            _sqlToExecute += _dataEngine.GetParametersForQuery();
            _sqlToExecute += ")";

            int insertedRowId = 0;

            if (!_dataEngine.ExecuteSql(_sqlToExecute, out insertedRowId))
                throw new Exception("Link - Save failed");

            return insertedRowId;
        }
        public void CreateLocation(int customerId, ref Location location, ref Address address)
        {
            //Check a valid customer id has been passed
            try
            {
                var customer = _da.Value.Customer.GetById(customerId);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            //Create a new location record
            var insertedRowId = _da.Value.Location.Insert(location);

            if (insertedRowId == 0)
            {
                throw new Exception("Failed to create location record");
            }
            else
            {
                location.Id = insertedRowId;
            }

            //Link the customer and location
            var customerLocationLink = new LinkObjectMaster()
            {
                MasterLinkId = customerId,
                MasterLinkType = LinkType.Customer,
                ChildLinkId = location.Id,
                ChildLinkType = LinkType.Location
            };

            //Save the link record
            insertedRowId = _da.Value.Link.Insert(customerLocationLink);

            if (insertedRowId == 0)
            {
                //Roll back the inserts as it's failed
                //Delete the location record
                _da.Value.Location.Delete(location);

                throw new Exception("Failed to create customer location link record, transaction rolled back");
            }

            //Save the address record
            var addressCreated = false;
            if (address.Id == 0)
            {
                //Create a new address record
                insertedRowId = _da.Value.Address.Insert(address);

                if (insertedRowId == 0)
                {
                    throw new Exception("Failed to create address record");
                }
                else
                {
                    address.Id = insertedRowId;
                    addressCreated = true;
                }
            }

            //Link the location and address records
            var locationAddressLink = new LinkObjectMaster()
            {
                MasterLinkId = location.Id,
                MasterLinkType = LinkType.Location,
                ChildLinkId = address.Id,
                ChildLinkType = LinkType.Address
            };

            //Save the link record
            insertedRowId = _da.Value.Link.Insert(locationAddressLink);

            if (insertedRowId == 0)
            {
                //Roll back the inserts as it's failed
                //Delete the location record
                _da.Value.Location.Delete(location);

                if (addressCreated)
                    _da.Value.Address.Delete(address);

                throw new Exception("Failed to create location address link record, transaction rolled back");
            }
        }
        public void LinkFacilityScheduleToFacility(int facilityId, int facilityScheduleId)
        {
            //Link the facility and facility schedule records
            var facilityFacilityScheduleLink = new LinkObjectMaster()
            {
                MasterLinkId = facilityId,
                MasterLinkType = LinkType.Facility,
                ChildLinkId = facilityScheduleId,
                ChildLinkType = LinkType.FacilitySchedule
            };

            //Save the link record
            var insertedRowId = _da.Value.Link.Insert(facilityFacilityScheduleLink);

            if (insertedRowId == 0)
            {
                throw new Exception("Failed to link facility schedule id " + facilityScheduleId + " to facility id " + facilityId);
            }
        }
 public void Update(LinkObjectMaster updateThis)
 {
     try
     {
         _db.Links.Attach(updateThis);
         _db.Refresh(RefreshMode.KeepCurrentValues, updateThis);
         _db.SubmitChanges();
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }
 public int Insert(LinkObjectMaster saveThis)
 {
     try
        {
        _db.Links.InsertOnSubmit(saveThis);
        _db.SubmitChanges();
        return saveThis.Id;
        }
        catch (Exception e)
        {
        throw new Exception(e.Message);
        }
 }